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.
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
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.
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:
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.
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:
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.
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.
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.
There are three Motors already defined in leJOS NXJ and stored as static variables
These are controlled using:
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);
}
} import leJOS.nxt.*;
The
basic package, which contains most of the essential leJOS NXJ classes, is leJOS.nxt.*.
.
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
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:
java.lang.String
class in leJOS NXJ. 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. // 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.
Thread.sleep(1000);
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.