Robotics Lecture Notes

Installing and programming in leJOS NXJ


Goals

Today's goals are to teach you some of the odds and ends that you will need to know in order to use leJOS NXJ to program your robots.  It will include a brief introduction to command line work in DOS, information on how to set up the NXT brick to accept leJOS NXJ code, and how to actually program and run leJOS NXJ based code. 


Before I get started too much - A little bit about using the command line in DOS

During the "lecture" component for this lab I will demo the following at the appropriate time.  They are given here just as a quick reference.

The best way to get a command prompt is by selecting "Start->Run" and typing "cmd"

Once you get a prompt, there are only a handful of commands you should need

  • dir - gives you a listing of the files/subdirectories in your current directory ("ls" in Unix).

 

  • cd - changes which directory you are in ("cd" in Unix).  Type the directory name to move down into a directory and ".." to move up a directory.  Multiple directories can be combined by using back slash.  For example, when I first start up command I am in c:\Documents and Settings\robolab.  By typing. "cd ..\..\lejos_nxj\samples" I move up two levels (to the root of the C drive) and then move back down into the samples directory for leJOS NXJ.

 

  • mkdir - makes a directory ("mkdir" in Unix).  For example "mkdir robot" will make a new directory called robot in whatever directory you are currently in.

 

  • move - moves a file ("mv" in Unix).  For example "move SimpleSimon.java robot" will move the file SimpleSimon into the directory named robot.

 

  • del - deletes a file ("rm" in Unix).  For example, "del SimpleSimon.java" will remove the given file.

 

  • edit - A very simple text editor (sort of like pico in Unix).  You may use this to write or edit java code, although you may also use jGrasp since you are used to it.

 


What is leJOS NXJ and why use it?

leJOS NXJ (pronounced like you might say lay-host if you didn't pronounce the 't') is a Java based "firmware" for the NXT version of LEGO Mindstorms.  The original language, leJOS was written for the RCX system and leJOS stands for LEGO Java Operating System.  The NXT version has been adjusted to work with the newer architecture as well as to add features that many people were asking for from the earlier version(s) of leJOS.

You write code in a language that is almost Java, compile this to leJOS NXJ byte code, install the byte code on your NXT brick, and let a leJOS NXJ interpreter on the brick (stored in RAM) interpret this code down to the standard NXT machine language (stored in ROM).  This interpreter is relatively small (I haven’t seen the stats for the NXT version, but the RCX version was ~17 KB) yet allows you to write some fairly powerful code.

leJOS NXJ is not without its problems.  Some of these are trivial, and some are serious.  Of minor impact is the fact that leJOS NXJ is NOT Java.  That is, it does not support all of the Java APIs. 


leJOS NXJ-enable your NXT

(This has been done for you, but the directions are here for reference|)

To enable your NXT for the execution of leJOS NXJ programs, you will have to install the leJOS NXJ operating system. Fortunately, this is easily done:

  1. Connect the NXT to the computer via the USB connection
  2. Turn on the NXT.
  3. Open a command shell (cmd) and type the following
    • cd %nxj_home%
    • cd bin
    • nxjflashg.bat
  1. Notice that the screen of both the computer and the NXT should indicate progress.

Please note : Several sources that I have read indicate that this should NOT be done excessively as that THEORETICALLY the NXT brick may only accept a limited number of new flashes of a firmware other than the LEGO firmware.  We will NOT go through this process together in class and you should only attempt this if it becomes necessary.


Create and Run your first leJOS NXJ program

There's a simple "Hello World" style example program available in the samples\HelloWorld section of the leJOS NXJ tree:

 

            import lejos.nxt.*;

 

public class HelloWorld

{

  public static void main (String[] aArg) throws Exception

  {

     LCD.drawString("Hello World",3,4);

     LCD.refresh();

  }

}
        

To run this program, you will:

  1. Open a command shell and change to the samples\HelloWorld directory of your leJOS NXJ installation

 

  1. Compile it using the NXJ compiler instead of the base java compiler (javac):


nxjc HelloWorld.java

Notice that this produces the HelloWorld.class file.  NOTE: When you write your own code, you may use any editor you like such as jGrasp or eclipse.  However, when you compile, you really need to use nxjc rather than the IDE built in javac.  

  1. Next we will place this program on the NXT brick.  To do this, turn on the brick and plug in the USB cable between the brick and the computer.  Then type

nxj HelloWorld

This does several things

1)      Create a binary file containing the linked program (HelloWorld.nxj) 

2)      Uploads this linked program to the NXT brick. 

 

  1. Finally, to run this program, select “files” from the menu on the first screen (use the arrows to navigate through the choices and the orange button to select.  The grey button is like the “cancel” or “back” button in later screens.  From here, select “execute.”  The words "hello world" will be displayed on the NXT

 


More helpful info about leJOS NXJ code

Remember when you first started learning Java?  For many of you the first program you saw was some variant of the "Hello World" program used in part 1.  However, after seeing that you still didn't have any idea how to program in Java.  Well, you are probably in the same boat here.  That is, you have now seen a leJOS NXJ program, but you still don't know how to program with leJOS NXJ.  Fortunately, leJOS NXJ is written on top of the Java language, so getting up to speed in leJOS NXJ shouldn't take much if you already know Java.  In this portion of the lab we will take a closer look another application using the leJOS NXJ API and discuss some of its fundamental concepts.  In order to complete this part of the lab, it is recommended that you have the RoverBot from last week built.  At the very least, you should have your NXT connected to a motor via a cable mounted on port A.

Effectors/Motors

There are three Motors already defined in leJOS NXJ and stored as static variables

  • Motor.A
  • Motor.B
  • Motor.C

These are controlled using:

  • public static void forward()
  • public static void backward()
  • public static void stop()             // Works like a brake. Use sparingly
  • public static void flt()                //coasts to a stop.  Keeps the motor free to rotate if outside forces cause this.
  • public static void reverseDirection()             //a toggle of sorts

 

Plus several other commands, but let’s play with these for now.

Consider the following leJOS NXJ application.  (You may want to copy and paste this into an editor such as jGrasp or Edit so that you can actually compile and download this onto your NXT).

 

            import lejos.nxt.*;

 

public class DriveSample

{

  public static void main (String[] aArg) throws Exception

  {

     LCD.drawString("DRIVE",3,4);

     LCD.refresh();

  
                 Motor.A.setSpeed(720);

  Motor.C.setSpeed(720);

  Motor.A.forward();

  Motor.C.forward();

  Thread.sleep (1000);

  Motor.A.stop();

  Motor.C.stop();

  Motor.A.regulateSpeed(true);

  Motor.A.rotateTo( 360);

  Motor.A.rotate(-720,true);

              } 
           } 
        

The import statement

 
            import leJOS.nxt.*;
        

The basic package, which contains most of the essential leJOS NXJ classes, is leJOS.nxt.*. 

.

The entry point: main()

 
        public static void main(String[] args) 
                    throws InterruptedException {
        

As with most Java applications, the entry point of a leJOS NXJ program is the main() method. The throws an InterruptedException seems to be a possibility in many of the basic leJOS commands so it just is easier to throw it

Output

 
        LCD.drawString("DRIVE",3,4);
        

You might have noticed that the graphical user interface of the NXT is rather limited - in fact, there's only the little LCD in its middle for such a purpose.  While this is limited to a couple of rows and colums of alphanumeric characters, it is worth pointing out that you can get at least simple messages out to the user using only the NXT brick.

 

You already might have noticed two things of importance here:

  1. You can use the well-known java.lang.String class in leJOS NXJ.
  2. You use static methods of TextLCD. This is a major concept of leJOS NXJ: The classes which are directly connected to the actual NXT hardware parts - e.g. motors, sensors, buttons or the LCD - are designed to be static ones. As a result you never will construct any of these but just use the methods of the existing single instance.

Running a motor

 
        // drive forward
        Motor.A.forward();
        

This piece of code runs the motor connected to the NXT's port A in "forward" mode (the direction the motor actually spins depends on the orientation in which you mounted the connector).

Again note that you use a static instance of the Motor class - to be more precise, a static member (named A) of this class, which refers to "motor connected to port A". As you might have already guessed, the two other motors are referred to as Motor.B and Motor.C, respectively.  If the motor in question is connected to some wheel assembly accurately, your robot will now drive forward.

Keep on running

 
        Thread.sleep(1000);
        
  1. So, what's the use of this? You probably get the point…

 


Finding out more about leJOS NXJ classes

We have demonstrated only a few of the classes available using the leJOS NXJ API.  However, there are a whole wealth of classes that you may use during the course of this semester.  You will most likely want to make sure that you bookmark and frequently refer to the official leJOS NXJ API.  There is also a copy of this stored locally on each computer.  With this site alone, you should be able to complete any task asked of you this semester.