<<

Installing Lejos and running Java on Mindstorm NXT Using the IDE

This is a tutorial to show you how to install, configure and run all the software necessary for you to develop Java programs for the NXT on a PC. Java is much more powerful and flexible than the original NXT-G software that LEGO provides. We will be using the Eclipse editor to write the actual Java code and to download this into the NXT. This combination enables you to conveniently write software and to quickly test it on the NXT.

Install on Windows XP/Vista Platform

Install on Mac OS X

Windows XP/Vista Platform Instructions

Table of contents

1. INSTALL USB DRIVERS 2. INSTALL LEJOS ON YOUR 3. SETTING ENVIRONMENT VARIABLES 4. INSTALLING LEJOS ON YOUR MINDSTORM NXT 5. INSTALLING ECLIPSE 6. WRITE A HELLO WORLD PROGRAM, COMPILE AND UPLOAD IT TO YOUR NXT

Before we begin: Make sure you have the Lego Mindstorms NXT software installed on your computer prior to following this tutorial. Lejos does depend on some of those files. This tutorial assumes that you have the Java JDK installed on your computer. You can check to see if you have Java installed by typing „java –version‟ at the command prompt. If you receive an “Unknown Command” error message then you need to install the latest version. During the creation of this tutorial, JDK 6.22 was the latest update and may be found at: http://java.sun.com/javase/downloads/index.jsp

1. INSTALL USB DRIVERS

In order to download and install the USB drivers go to the following website: http://libusb-win32.sourceforge.net/ - downloads Download the executable file that has bin in the name. When you run this file it will automatically install. Next, connect the NXT to the computer with the USB cable. Check the correct installation of the driver by checking the device manager. Right-click on “My Computer” and select “Properties”. Switch to the hardware tab. Click on “Device manager“. It should list “Lego Devices => Lego Mindstorms NXT”. Restart your computer before moving to the next section.

2. INSTALL LEJOS ON YOUR COMPUTER

The latest version of Lejos may be found at http://lejos.sourceforge.net/ . Look for the „Downloads‟ link under NXT Brick and save the Win32 leJOS NXJ version to your desktop.

Extract the contents of the zip file into the following: :\lejos_nxj

3. SETTING ENVIRONMENT VARIABLES

We now need to inform Java of the existence of the Lejos library. In Windows XP, Right-click on “My Computer” and select “Properties”. Click on the “Advanced” tab. Click on “Environment Variables” at the bottom. Create a new “System variable” by clicking on “New” (see figure below) at the bottom. Do NOT create a “User variable” at the top.

Type NXJ_HOME as the variable name and add the Lejos directory C:\lejos_nxj. Click OK.

You should also check to make sure the environment variable JAVA_HOME has been set up. It is usually added automatically when you install the SE JDK (Java Developers Kit). If it is not then use the same steps as above when you set up the NXJ_HOME variable except the name will be JAVA_HOME and the Variable value will be the path to your JDK installation unless you changed this path during install it should be something (depends on your java version) like: C:\Program Files\Java\jdk1.6.0

Next, we need to add the LEJOS_HOME variable to the system variable “Path”. Select “Path” from the list and click on “Edit”. Add to the end of the variable: ;C:\lejos_nxj\bin;%NXJ_HOME%\bin If you added the JAVA_HOME variable then add: %JAVA_HOME%\bin;C:\lejos_nxj\bin;%NXJ_HOME%\bin The different values are separated by a colon and no colon is necessary at the end of the line. Now we will test if Lejos has been installed correctly by using the DOS command prompt. Type in: lejosdl If the result looks like the one in the picture below then everything is fine.

4. INSTALLING LEJOS ON YOUR MINDSTORM NXT

Now you have to place the NXT brick in reset mode. First, you should have a paperclip and unfold part of it so approximately 1 inch of it is straight. While the NXT brick is on, place the paperclip and press and hold down on the reset button for 5 seconds, the screen will go blank. You should also hear a faint clicking noise coming from the NXT brick.

The above image shows where the reset button is on the NXT brick. If you look carefully you will see a small silver button which is fairly simple to depress using a paperclip or toothpick. This button allows you to place the NXT brick in reset mode.

Make sure your NXT is on and connected to your computer via the USB cord.

In the command prompt set your path to the leJOS folder C:\lejos_nxj and type the following in the command prompt: nxjflash

You should receive a message like the following in your command prompt.

Make sure to power your brick on again (e.g. restart). You should hear a different start-up sound than you are used to. You should also see the Lejos “J” followed by the Lejos menu.

Congratulations, you have completed the first major steps towards programming your NXT(s) using Java! Now we have to set up Eclipse (I.D.E.).

5. INSTALLING ECLIPSE

Download Eclipse 3.4.1 (or the latest version) using the following link: http://www.eclipse.org/downloads/

Read the install dialogue boxes; allow Eclipse to install in its default location and be sure to read/agree with any software agreements. When you start Eclipse you will be asked to choose a workspace. The workspace will contain all the files that you create. In our example we use the directory “C:\leJOS_workspace”. You will then be greeted with the welcome screen.

CREATING TOOLS IN ECLIPSE a) We will now create a Hello World program and upload it into the NXT. First, you need to create a new Java project. Click on “File => New => Java Project”. Give the project a new name, but avoid space characters. We called ours “lejosProject”. Click Finish. b) Next, you need to turn this project into a Lejos project. Right-click onto the project and select “Properties”. Select “Java Build Path” on the left and click on the “Libraries” tab. Afterwards, click on “Add External JARs…” and locate the “lib” directory in your “C:\lejos_nxj” directory. Select “classes.jar” and press “Open”. The Lejos library will now be listed. c) We stay in the properties window and move to the “Java ” section listed in the left panel. Check the box “Enable project specific settings” and choose level 1.3 for the compiler compliance level. This will urge the compiler to optimize for an earlier version of Java. This earlier version is more suitable for the NXT since it was developed for embedded systems. It requires much less resources than the latest version of Java. Click on “Apply” and then ”OK” to leave the properties. d) The next step is to configure Eclipse for compiling and uploading the software into the NXT. Click on “Run => External Tools => External Tools Configurations…”. e) Select “Program” on the left and then click on the “New” icon above. Name the tool „leJOS_Compile‟ on top. In the “Main” tab, enter the location of the “nxjc.bat” file that should be in the “-nxj\bin\” directory by clicking on “Browse File System”. Press Open. For the Working Directory click Browse Workspace and choose your leJOS NXJ project or type in ${project_loc}. Under arguments type in ${java_type_name}.java then click Apply.

Now, create a tool that uploads the code to the NXT select Run > External Tools > Open External Tools Dialogue. Select Program item and click the new button and call it „NXJ_Upload‟. In the “Main” tab, enter the location of the “lejosdl.bat” file that should be in the “lejos-nxj\bin\” directory by clicking on “Browse File System”. Press Open. For the Working Directory click Browse Workspace and choose your leJOS NXJ project or type in ${project_loc}. Under arguments type in ${java_type_name} -v then click Apply.

You can now compile and upload code to your NXT. Use the Lejos_Compile tool to compile the code then turn your NXT on, link it to your pc using the USB cable, and use the NXJ_Upload tool to upload the code. Once you upload the file you should hear music begin to play.

6. WRITE A HELLO WORLD PROGRAM, COMPILE AND UPLOAD IT TO YOUR NXT

First, we need to create a project. Click on New => Java Project and be sure to select „Use project folder as root for sources and class files. Next, click on “New => Class”. Name it “HelloWorld” and check the box “public static void(String[] args)”. You will receive an almost empty class. Eclipse is a very smart Java editor that tries to compile your program while you write. Similar to MS Word, it will underline errors with a red line. Sometimes this automatic compilation does not work and you want to force Eclipse to compile. You can achieve this by simply saving the file. You now need to enter a few lines of Java code. Your finished code should look like this: import lejos.nxt.LCD; public class HelloWorld {

public static void main(String[] args) {

LCD.drawString(“Hello World!”, 2, 2); LCD.refresh(); while(true); {}

}

}

This will set your NXT to sleep for 5 seconds before the program ends. You will now see “Hello World!” for five seconds and then the program ends. Be sure your NXT is plugged in to your computer via USB and that the NXT is on. You also need to select your main class window in the middle before starting the compiling procedure, so that Eclipse knows which class you want to compile. Once compiled, you can now upload your code to the NXT.

EXTRA STEPS FOR WINDOWS VISTA Before you start, make sure the „User account control‟ is turned off. Go to Control Panel → User Accounts „Turn User Account Control on or off‟ Note: You can also compile and upload your code from the command prompt. nxjc – compile a program usage: nxjc (make sure you are in the directory of your java program) nxjupload – upload your program (make sure you upload the nxj file in the directory) Usage: nxjupload [-b|--bluetooth] [-u|--] [-n|--name] [ -r|--run]

Install on Mac OS X Instructions

Table of Contents

1. INSTALL JAVA 2. INSTALL LEGO USB DRIVER ON MAC 3. INSTALL AND CONFIGURE LEJOS 4. INSTALL AND CONFIGURE ECLIPSE 5. CREATE HELLO WORLD PROGRAM AND LOAD IT ON THE NXT

1. INSTALL JAVA

The benefit of MAC OS x is that java runtime development will be automatically installed on your operating system. To make sure you have the latest version of Java on your Mac you can use the Software Update.

2. INSTALL LEGO USB DRIVER ON MAC

Go to this website and download the MAC OS 10.5 (Leopard) firmware Fix for MAC. Install the package by clicking open the disk and follow the instructions on the installer. You can leave all settings to default.

3. INSTALL AND CONFIGURE LEJOS

Go to this website and select the second download link for /Mac OSX leJOS NXJ. Extract the file lejos_NXJ_0_x_x.tar and place the folder lejos_nxj in your home folder of the MAC system.

Now it is time to set the environmental variables for Java and Lejos. To do this we need to create or edit the .profile file. This can be done in the following way.

Open Terminal (/Applications/Utilities/Terminal) and write “sudo pico .profile.” Save the file by ctrl – x and select yes to save the data. Hit return to exit the pico mode and come back to the terminal.

There is a difference between Leopard and Snow Leopard. In order to run Lejos on Snow Leopard one should replace the bin folder with the following files.

Now we have to set the permissions. Make a script file in the leJOS directory executable by typing in terminal:

cd lejos_nxj cd bin

After that execute the command:

chmod +x * and hit return.

After you have your system you can test the connection of your NXT with the computer by plugging it in and type in terminal (/Application/Utilities/Terminal) “nxjbrowse -u”. After you have successfully established a connection with your NXT you can flash it with LejOS. Type in the terminal “nxjflashg” and hit return. Select „Start Program‟ and select yes to overwrite and delete the existing software. Now the LejOS software is installed on your NXT and you can use it to program Java files on it with for example Eclipse.

4. INSTALL AND CONFIGURE ECLIPSE

Go to the website and download Eclipse IDE for Java EE Developers. Eclipse is a Java program and does not need to be installed on the computer, instead export the .tar file to your computer somewhere where you can find it. Start the program by clicking on the Eclipse icon.

You will be welcomed by a welcome screen and first choose a location where you want your workspace to be located. You can use the default setting. You will see the normal background of eclipse.

Now you have to create a new project. File->New…->Project. Select a Java Project in the Java directory. Then you can create a project with a project name for example lejos_nxt. And select the execution environment to J2SE-1.3, because this has the best compatibility with the leJOS NXT.

Open the preferences of the project you have just created by selecting the directory and than File– >properties (Alt + return) and click the Java Build Path tap on the left. Add an External JARs by clicking on the button „Add External JARs‟. Select in the directory lejos_nxj/lib the file classes.jar.

Select the Java Compiler on the left tap and change the compiler compliance level to 1.3 because that fits better to the environment of the NXT. Apply the new settings and select ok.

Now we have set the compiler and the library files it is time to make a connection to the NXT within Eclipse. Select RUN->External Tools-> External tools configuration… and create a new launch configuration by clicking on the „New‟ button.

Name the new configuration: lejos_Download and fill in for the location:

/Users/xxxxx/lejos_nxj/bin/nxj

The xxxx should be replaced with your home directory of the computer.

For the working Directory and arguments fill in:

Users/XXXXXX/lejos_nxj/bin

${java_type_name}

Click on apply and after that close the window. Now we can create a shortcut to this new configuration by clicking on the run_lejos icon and select: organize favorites… Then click on add and select the lejos_download. Confirm it by clicking on OK and after that again on OK.

5. CREATE HELLO WORLD PROGRAM AND LOAD IT ON THE NXT

Select the directory lejos_NXT and then add a new Class by clicking File->New-> Class. Fill in the Source folder lejos_NXT/src and the name Helloworld. Also mark the public static void main(String[] args) option and leave the rest as it is. Click on Finish.

Open the HelloWorld.java by double clicking on it and copy the following code to the first line:

import lejos.nxt.*;

Place within the main function the following code:

LCD.drawString(”Hello World!”, 2, 2); LCD.refresh(); while(true);

Save the file by File->Save (cmd + s) and connect the NXT to the computer via the USB cable. Make sure the NXT is switched on and has leJOS running. Now hit the lejos_download button and the file should be uploading and will give a message when finished.

Enjoy!

Sources: http://lejos.sourceforge.net/nxj.php http://jsug.at/wiki/HowTo_LeJOS_for_Lego_Mindstorm_NXT http://www.bartneck.de/2008/03/04/java-lego-nxt-eclipse-tutorial/ http://www- lar.deis.unibo.it/people/rgrandi/menuPages/materiale%20didattico/Lego%20MindStorm%20NXT/material e_utile/LTU_LEJOS_BUTKA.pdf