MathCS - Robotics

NXT/Java Software Setup

The NXT kit comes with its own programming software, called NXT-G, where you can create programs by visually connecting icons together without having to type any instructions. That type of programming is very easy to learn, but is limited; complex programs require a more flexible approach.

You can, in fact, program the NXT in many different programming languages, including Java, a modern, very flexible and widely adapted language developed by SUN Microsystem that still is relatively easy to learn. Thus, we will choose Java as our programming language for the NXT.

Since the that language does not come with the NXT kit, you will need to download and install a number of software pieces that in the end will all work together to allow the easy creation of NXT programs in Java. Installation requires multiple steps and is not for the faint of heart - you should have experience with downloading and installing program, but if you follow the instructions carefully you should have no problems. Still, if someone "in the know" offers to do this installation for you, do accept his/her offer.

The following steps apply only to Windows (32 bit). For Mac instructions, see below.

Step 1: Downloading the software

First, download all of the following software to your laptop (we assume you are using a Windows-based system). Make sure to download it to a location that you remember (e.g. your desktop).

If you happen to have installed the original NXT 1.1 or higher software you do not need to download the LEGO NXT driver; it is included in that software. Note that the last two files are compressed files (zip) - you should also save them your disk, not open them (at this time).

Step 2: Installing the software

Installing, configuring, and testing is a multi-step and rather technical process. I would recommend that you schedule an appointment so that I can install the software for you. It will take me about 10 minutes, assuming you have the above software already downloaded. But if you prefer and are technically fearless, you could do the installation/configuration yourself by following the steps below carefully.

2.1. Install the Java compiler: double-click the downloaded file jdk.exe and follow the instructions. It will take some time and you have to accept a number of default options. You will be brought to an optional registration screen when everything is finished: simply close that screen without registering.

2.2. Install the 7-Zip utility: You need to unzip some files.Windows 7 has an "unzip" utility build in, but it is faulty (can not handle long file names). So, double-click on the 7-zip.exe program you downloaded and follow the instructions.

2.3. Install the Eclipse Java editor: right-click the downloaded file and pick "7-Zip | Open archive". A window will open up and you should drag the folder "eclipse" to your drive C on your laptop. This will take a while to complete. Then rename that folder that just got created eclipseNXT. Finally, open the folder, find the file "eclipse" and create a shortcut by right-clicking. Move that shortcut to your desktop.

2.4. Install the LEGO NXT driver: You can skip this step if you happen to have the original NXT software version 1.1 or higher installed (not likely). If you do need to install the driver, double-click on the downloaded file A window will open so that you can drag the folder onto your desktop. Open the newly created folder, double-click on the "setup.exe" file and follow the instructions.

2.5. Update your path: You have to properly notify the operating system that you have installed a new Java compiler. Double-click the (compressed) utility "winpathed" program and open the "winpathed" folder. IMPORTANT: If you are using Window Vista or above, right-click on "winpathed" and select "Run as Administrator", otherwise just double-click on the "winpathed" program.

Win Path Editor program

Click on the little "plus" sign and browse to the folder c:\Program Files\Java\jdk1.6.0_30\bin. Then click "Choose' to add that path as the last entry to the list. Click "Save", then quit the program by pressing ALT-F4. If you get an "Access denied" message, make sure you run the program with administrator rights. You can now delete the "winpathed" utility program from your disk, we won't need it again.

2.6. Install the LeJOS Java - NXT components: double-click the downloaded file leJOS.exe. Accept all defaults. In the last step another dialog will open to "Install NXJ Firmware in NXT". If you have an NXT brick, plug it in and turn it on. Once Windows recognizes the new device, continue to "flush" the firmware as prompted. If you do not currently have an NXT brick, just close/cancel the firmware flush. When you are done, either way, the LeJOS installation program will also quit.

You now have installed all the software you need. To save space, you could delete the installation programs you downloaded in step 1.

Next, go to Step 3: Configuring the software.

If you have a Mac, you could download Mac versions of the Java Compiler, the Eclipse IDE, and the NXT USB driver. However, LeJOS for Mac/Linux would need to be compiled from scratch, which is possible but beyond our scope here. Therefore, you should install and start a Windows Emulator on your Mac, either Parallels or Boot Camp would be fine. Once the emulator is running properly, follow the above installation instructions as if you were using a Windows machine.

Step 3: Configuring the software

Our next step is to configure the different software packages so that they can work together. That used to be tricky, but now there is a shortcut: using the Eclipse LeJOS plugin.

3.1. Start Eclipse: Double-click on the shortcut you create for Eclipse in step 2.3. Alternatively, open the folder eclipseNXT on your drive C: and double-click on the (first) "eclipse" program. You will be prompted to select a workspace. Accept the default and you should soon see the Eclipse welcome screen.

3.2. Setup your Eclipse Plugin: Click on the Help menu and select "Install new Software ...". Click the "Add" button near the top right side and type "Lejos Plugin" for Name and "" for Location (you might want to copy-and-paste this location), then click on Okay. You should see the module "LeJOS NXJ Support" - check the box for that module and click "Next". Follow the instructions (you will need to accept several default options and licenses) and the plugin will install, which will take some time.

If all goes well, the Eclipse plugin, which will provide the go-between between Eclipse, Java, the LeJOS software, and your NXT brick, will install and configure itself properly.

Step 4: Testing the software

At this point everything should be in place and we can create and run our first program. This is usually a three step process: (1) create a program (and a new project if necessary) in the Java programming language, (2) translate it to machine code and download it to your NXT, and (3) execute the program on your NXT (and hope for the best). Let's go:

4.1. Create a new project: start Eclipse, if it is not already up. After picking the default workspace you should see the default Eclipse screen. Click on "File | New | Project". Expand the "Lejos" project type and select "Lejos NXT Project". Click on "Next", provide a name such as "Test" for your new project, and click "Finish".

You should now have a new project folder called "Test", shown on the left. That folder can contain multiple programs, so in our next step we will create a simple sample program to start with.

4.2. Create a new program: Right-click on the NXT project and select "New | Class". Name your class "Test" (spelled just like this including capitalization), check the entry to create the method stub "public static void main(String args[])", and un-check "Generate comments".

 Testing your first rogram

Then click 'Finish". You will see a "default" program that looks like the following:

Default Program 

Erase the green and blue comments, move the opening curly brackets to a new line by themselves and type in the lines shown below in bold so that your program looks exactly like the one below - including all commas (there are none), semi-colons (there are plenty), capitalization, spaces, regular parenthesis, and curly brackets. The code that changes is shown on bold.

import lejos.nxt.*;

public class Test 
	public static void main(String[] args)
		LCD.drawString("I am ALIVE", 1, 1);

You should see no errors (i.e. no red squiggly underlining), so save your program.

4.3. Translate and transfer: Next you need to translate it into machine code that the NXT can understand. Plugin you NXT brick into your computer via the USB cable, turn it on (you should hear a beep from the NXT and shortly after that a "ding" from your computer), then click the green "play" (also known as the "run") button. If all goes well, your program is translated to machine code, transferred to your brick, and executed: you will see the specified string show up on the LCD display of your brick for 5 seconds (5000 ms). Then it will return to normal.

If you do not have an NXT brick yet, you can not complete this step. You can hope that it would work, if you had a brick, though :-)

CONGRATULATIONS (I hope). You have installed the tools needed to program your NXT in Java. Thankfully you will not need to perform these steps again.

Next: building a robot