Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Imaginations run wild with Java Lego robots

Learn to program the ultimate geek toy -- Lego Mindstorms -- in Java

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 2 of 5

As of this writing, the current version is beta 3. It's distributed as a .tar.gz file for Linux and a .zip file for Windows. All you need to do is unpack the file, and then add the bin subdirectory to your PATH. (I assume you have already installed the Java 2 SDK. If not, go get it from http://java.sun.com/j2se/.)

Getting to know the tools

At this point, you'll probably want to build a robot that you can use for testing your leJOS programs. If you need some help, try following along with the building instructions found at http://www.oreilly.com/catalog/lmstorms/building/. I suggest building RoboTag, a basic tank-style robot with a bumper and a light sensor; it is an excellent test bed for programming.

RoboTag, one of the robots from The Unofficial Guide to Lego Mindstorms Robots, is a good platform for experimenting with code



LeJOS offers simple tools for compiling and downloading robot programs. If you've added leJOS's bin directory to your PATH, then you are ready to use the leJOS tools.

The first step is to download the leJOS firmware to the RCX. You only need to do this once. The RCX keeps the firmware in battery-backed RAM, so it is there even after you turn off the RCX. If you switch batteries, however, you will probably need to reinstall the firmware. Just use leJOS's lejosfirmdl command. The firmware download takes a few minutes; lejosfirmdl shows the percent complete as the download progresses.

C:\>lejosfirmdl
Use --help for options.
Transferring "/Apps/lejos/bin/../bin/lejos.srec" to RCX...
100%
C:\>


Now you're ready to start coding in Java. Type in the following simple program and save it in a file called HelloRCX.java:

import josx.platform.rcx.*;
public class HelloRCX {
  public static void main(String[] args) {
    Motor.A.forward();
    Motor.C.forward();
    try { Thread.sleep(2000); }
    catch (InterruptedException ie) {}
    Motor.A.stop();
    Motor.C.stop();
  }
}


Compiling this source code is a matter of running the regular javac and telling it to use the leJOS classes. Fortunately you don't have to think about it too much; just use the lejosc utility like this:

C:\>lejosc HelloRCX.java
C:\>


If there are any errors, lejosc will report them just like the regular javac compiler, providing both the error and the line number in the source code.

If lejosc is successful, it produces .class files from your .java source code. The next challenge is to get the class files down onto the RCX so you can run your program. LeJOS encapsulates this functionality in the lejos command-line tool. You do have to specify which port to use with an environment variable, like this:

C:\>set RCXTTY=COM2
C:\>lejos HelloRCX
C:\>


As the program is downloaded to the RCX, the RCX counts upward, starting from 1. How far it counts depends on how many classes are downloaded and the size of those classes.

This is a deceptively simple step. Behind the scenes, leJOS is compiling the class you specified into leJOS native code and downloading the native code to the leJOS firmware in the RCX. Further, it has to be smart enough to package and send any other classes that are referenced in your class. This is an important point. In this example, for instance, the Motor class will also be downloaded because you have used it in your code.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources