
Installing Lejos and running Java on Lego Mindstorm NXT Using the Eclipse 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 LEGO Mindstorms 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 COMPUTER 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: c:\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 Compiler” 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 “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}.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.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages10 Page
-
File Size-