Lego NXT and LeJOS NXJ - University of Essex
Lego NXT and LeJOS NXJ - University of Essex
Lego NXT and LeJOS NXJ - University of Essex
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong><br />
Dongbing Gu<br />
School <strong>of</strong> Computer Science <strong>and</strong> Electronic Engineering<br />
<strong>University</strong> <strong>of</strong> <strong>Essex</strong><br />
UK<br />
Spring 2013<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 1 / 37
Outline<br />
1 Why <strong>Lego</strong> is different: A History Tour<br />
2 LEGO Mindstorms <strong>NXT</strong>: Brick<br />
3 LEGO Mindstorms <strong>NXT</strong>: Sensors<br />
4 LEGO Mindstorms <strong>NXT</strong>: Motors<br />
5 LEGO Mindstorms <strong>NXT</strong>: Communications<br />
6 Programming Solutions for the LEGO Mindstorms <strong>NXT</strong><br />
7 Writing your first <strong>LeJOS</strong> <strong>NXJ</strong> program<br />
8 <strong>LeJOS</strong> <strong>NXJ</strong> Programming Environment<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 2 / 37
Why <strong>Lego</strong> is different<br />
If you wanted a task to be done by a robot, you first had to build<br />
your robot, <strong>and</strong> then you would be able to program it.<br />
Not easy<br />
mechanical design <strong>and</strong> manufacture,<br />
electronics design <strong>and</strong> manufacture,<br />
system integration,<br />
<strong>and</strong> then you could program the robot, <strong>of</strong>ten in assembly language.<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 3 / 37
Why <strong>Lego</strong> is different<br />
Now, with <strong>Lego</strong>, most <strong>of</strong> the hard bits disappear: you just stick <strong>Lego</strong><br />
bits together to make the robot you want.<br />
NO mechanical design <strong>and</strong> manufacture,<br />
NO electronics design <strong>and</strong> manufacture,<br />
NO system integration,<br />
<strong>and</strong> you can program the robot in almost any language you want: C,<br />
Java, Lisp, Ada, Prolog, Logo, <strong>and</strong> even assembler.<br />
easy.<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 4 / 37
<strong>Lego</strong> Mindstorms RCX<br />
At MIT in the 1980s, the Mechanical Engineering Department started<br />
a course called 2.70: ‘Introduction to Design’.<br />
An in-house <strong>Lego</strong> design team, obviously inspired by the MIT work,<br />
developed the <strong>Lego</strong> RCX brick.<br />
Released in 1998 in various kits.<br />
All kits contain:<br />
an RCX brick with 3 sensor inputs <strong>and</strong><br />
3 motor outputs,<br />
2-way IR communication between<br />
RCX <strong>and</strong> PC,<br />
touch, light, rotation, temperature<br />
sensors, motors,<br />
<strong>Lego</strong> parts.<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 5 / 37
LEGO Mindstorms <strong>NXT</strong>: Brick<br />
<strong>Lego</strong> Mindstorms <strong>NXT</strong> was released by <strong>Lego</strong> in late July 2006<br />
A new version, <strong>Lego</strong> Mindstorms <strong>NXT</strong> 2.0, was announced in January<br />
2009, featuring an advanced color sensor <strong>and</strong> other upgraded<br />
capabilities.<br />
All kits contain:<br />
a 32-bit ARM7 microcontroller<br />
(AT91SAM7S256).<br />
100 x 64 pixel LCD display.<br />
4 buttons for user interface.<br />
Sound channel with 8-bit resolution<br />
<strong>and</strong> up to 8 kHz sampling rate.<br />
wireless Bluetooth <strong>and</strong> a USB port.<br />
4 sensor input ports<br />
3 PWM motor output ports with built<br />
in encoders (1 degree resolution).<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 6 / 37
On Board Memory<br />
The <strong>NXT</strong> brick contains 64kB <strong>of</strong> RAM (R<strong>and</strong>om Access Memory).<br />
This holds information as long as the battery is connected.<br />
It contains 256K flash <strong>and</strong> used for two things:<br />
The firmware that forms a kind <strong>of</strong> operating system. It is uploaded to<br />
the flash memory via the USB driver or Bluetooth link.<br />
The user programs, compiled on the PC <strong>and</strong> uploaded via the USB<br />
driver or Bluetooth link. Note only limited memory is available for<br />
these!<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 7 / 37
Sensor Ports<br />
Sensors on the <strong>NXT</strong> can be either analog or digital sensors.<br />
For analog sensors, one <strong>of</strong> the sensor input wires contains an analog<br />
value representing the value <strong>of</strong> the sensor.<br />
Digital sensors are more intelligent <strong>and</strong> typically contain a<br />
microprocessor. The sensor communicates with the <strong>NXT</strong> using the<br />
I2C serial communications protocol.<br />
Each <strong>of</strong> the four sensor ports on the <strong>NXT</strong> can be configured as either<br />
a digital or analog sensor.<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 8 / 37
RGB Color Light Sensors<br />
The <strong>Lego</strong> sensor only responds to light<br />
coming from the front. It is a<br />
directional sensor.<br />
The RGB color sensor contains three<br />
light emitting diodes (LED).<br />
If there is a reflective object in front <strong>of</strong><br />
the sensor, the sensor will respond to<br />
the LED light reflected from the object<br />
The RGB color sensor has a number <strong>of</strong><br />
modes.<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 9 / 37
RGB Color Light Sensors<br />
In red, green or blue reflective modes the sensor only switches on<br />
that colored LED <strong>and</strong> then measures the reflected light.<br />
In ambient mode, all the LEDs are switched <strong>of</strong>f <strong>and</strong> so the ambient<br />
light level is measured.<br />
In full mode, all 3 LEDs are cycled <strong>and</strong> then a number 1-6 is<br />
calculated for the nearest color.<br />
1 Black<br />
2 Blue<br />
3 Green<br />
4 Yellow<br />
5 Red<br />
6 White<br />
It reports color name <strong>and</strong> light intensity ranging between 1 <strong>and</strong> 100.<br />
(In practice you get much less.)<br />
It outputs colors: red, green, <strong>and</strong> blue.<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 10 / 37
Touch Sensors<br />
It reports pressed or released.<br />
It can be used to detect if robot bumps into an obstacle.<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 11 / 37
Ultrasonic (Sonar) Sensors<br />
It produces a ultrasonic pulse wave <strong>and</strong> listen to the echo.<br />
It reports distances with a range <strong>of</strong> about 5cm to 250cm <strong>and</strong> an<br />
accuracy <strong>of</strong> +/ − 3cm.<br />
It can be used to report distances or detecting obstacles.<br />
In practice, direction ambiguity is a serious problem. More on this will<br />
be explained in sensor <strong>and</strong> noise lecture.<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 12 / 37
Compass Sensors<br />
It measures the earth’s magnetic field <strong>and</strong> calculates a magnetic<br />
heading to report direction.<br />
Calculates to the nearest 1 ◦ <strong>and</strong> returned as a number from 0 to 359.<br />
Updates the heading 100 times per second.<br />
Influenced by local magnetic interference. Objects such as metal<br />
items, motors, batteries <strong>and</strong> wires can cause magnetic interference.<br />
Has a built in calibration function that calculates correction <strong>of</strong>fsets.<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 13 / 37
LEGO Mindstorms <strong>NXT</strong>: Motors<br />
<strong>Lego</strong> motors are DC (direct current) serve motors.<br />
If the terminals are not connected to anything, the motor can spin<br />
freely. It has a good gearbox, so it can spin for a while.<br />
If a voltage is applied to the terminals, then current will flow in the<br />
motor coils. This current will generate a TORQUE that exerts a force<br />
on the motor that tries to make it rotate. The direction <strong>of</strong> this force<br />
depends on the POLARITY <strong>of</strong> the voltage.<br />
However, if the motor terminals are simply connected together, the<br />
motor will no longer spin freely, but will be braked as if a voltage in<br />
the opposite direction had been applied.<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 14 / 37
Motors<br />
<strong>Lego</strong> calls power settings ‘Power levels’. They go from 0 to 100.<br />
For low settings, a slight increase in load can cause a motor to stop.<br />
Because <strong>of</strong> the high inertia <strong>of</strong> the motor, changes in settings take<br />
some time to have an effect.<br />
For light loads, the speed does not seem to vary much between power<br />
settings.<br />
The curves below shows rotation speed vs. motor power level under<br />
different loads.<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 15 / 37
Motors<br />
The <strong>NXT</strong> cable has six wires: white (motor 1), black (motor 2), red<br />
(ground), green (power), yellow (tacho 1), blue (tacho 2).<br />
The motor speed is controlled by pulse-width modulation (pwm),<br />
which works by driving the motor with a variable cycle square wave.<br />
This effectively turns the motor on <strong>and</strong> <strong>of</strong>f, fast. The longer it is on,<br />
the more torque it will generate <strong>and</strong> the faster it will go.<br />
The yellow <strong>and</strong> blue wires are connected to the quadrature encoder.<br />
They return square wave pulses that are 90 degree out <strong>of</strong> phase.<br />
By measuring the frequency <strong>of</strong> the square wave oscillation, you can<br />
compute the rotation.<br />
One cycle corresponds to 2 degree <strong>of</strong> rotation.<br />
By tracking both square wave, you can identify quarter cycles, which<br />
gives you a resolution <strong>of</strong> 0.5 degree.<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 16 / 37
Tachometer<br />
A tachometer is an instrument that measures the rotation speed <strong>of</strong> a<br />
shaft or disk, as in a motor or other machine.<br />
The <strong>NXT</strong> motor has a built in tachometer that keeps track <strong>of</strong> the<br />
current angle (in degrees) <strong>of</strong> the motor axle.<br />
Built-in rotation sensors 360 counts/rotation.<br />
Rotation sensor provides valued information for motion control <strong>and</strong><br />
navigation. More will be detailed in robot navigation lecture.<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 17 / 37
USB, BlueTooth, <strong>and</strong> Loudspeaker<br />
The <strong>NXT</strong> brick contains a full speed USB port (12Mbit/s) <strong>and</strong><br />
BlueTooth wireless communication (Bluetooth Class II V2.0<br />
compliant).<br />
They can be used for uploading programs.<br />
They can also be programmed to communicate with other devices.<br />
BlueTooth allow you to connect three devices at the same time.<br />
Loudspeaker with 8 kHz sound quality.<br />
Sound channel with 8-bit resolution <strong>and</strong> 8 KHz sample rate.<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 18 / 37
Programming Solutions for the LEGO Mindstorms <strong>NXT</strong><br />
Different programming environments are available for LEGO <strong>NXT</strong>.<br />
Most <strong>of</strong> them can be programmed <strong>and</strong> downloaded onboard: <strong>NXT</strong>-G,<br />
ROBOLAB, RobotC, <strong>LeJOS</strong>.<br />
There are some which can be used for remote control: iComm<strong>and</strong>,<br />
<strong>NXT</strong> Python.<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 19 / 37
<strong>NXT</strong>-G<br />
<strong>NXT</strong>-G is a graphical programming environment developed by<br />
National Instruments.<br />
Writing an <strong>NXT</strong>-G program is very much like creating a flowchart.<br />
You write a program by dragging icons (‘code blocks’) that describe<br />
different behaviors, e.g., turn motor A on at 75 percent <strong>of</strong> full power,<br />
<strong>and</strong> connect them with lines to describe the program behavior.<br />
Using a variety <strong>of</strong> code blocks, you can control motors, introduce<br />
delays, play sounds <strong>and</strong> direct the flow <strong>of</strong> your code according to the<br />
state <strong>of</strong> sensors <strong>and</strong> timers, etc.<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 20 / 37
ROBOLAB<br />
ROBOLAB was originally developed by Tufts <strong>University</strong> for LEGO for<br />
the first generation LEGO Mindstorms RCX microprocessor brick. It<br />
was extensively enhanced <strong>and</strong> revised to support both the RCX <strong>and</strong><br />
the second-generation <strong>NXT</strong>.<br />
ROBOLAB is another graphical environment, although it is not as<br />
intuitive as the <strong>NXT</strong>-G language.<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 21 / 37
ROBOTC<br />
ROBOTC is industry-st<strong>and</strong>ard C language, developed by the Robotics<br />
Academy at Carnegie Mellon <strong>University</strong>.<br />
ROBOTC has a similar graphical programming capability, but with it,<br />
you drag <strong>and</strong> drop text.<br />
ROBOTC has a powerful interactive real-time debugger that<br />
significantly reduces the time it takes to debug programs.<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 22 / 37
<strong>NXJ</strong><br />
<strong>NXJ</strong> is a Java implementation for the <strong>NXT</strong>. It is st<strong>and</strong>ard Java but<br />
with a much smaller Class library.<br />
<strong>NXJ</strong> programs are written <strong>and</strong> compiled on the PC. The compiled<br />
programs are then transferred to the <strong>NXT</strong> where they can be<br />
executed.<br />
<strong>LeJOS</strong> is a tiny (about 16 Kb) Open Source Java-based operating<br />
system, including a virtual machine for the execution <strong>of</strong> Java<br />
bytecode, an API on top <strong>of</strong> VM, additional s<strong>of</strong>tware tools.<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 23 / 37
<strong>LeJOS</strong> <strong>NXJ</strong><br />
<strong>LeJOS</strong> <strong>NXJ</strong> is a Java programming environment for the LEGO<br />
MINDSTORMS <strong>NXT</strong>. It allows you to program LEGO robots in Java.<br />
It includes all the classes in the <strong>NXJ</strong> API as well as tools used to<br />
upload code to the <strong>NXT</strong> brick.<br />
As <strong>LeJOS</strong> is a firmware replacement, it must be flashed onto the<br />
<strong>NXT</strong>.<br />
There are two steps:<br />
1 javac compiles the source file (.java) into bytecode, creating a file<br />
(.class).<br />
2 java runs the bytecode in the file (.class) on the java virtual machine.<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 24 / 37
Java Language<br />
All code in Java is organized in objects. S<strong>of</strong>tware objects consist <strong>of</strong><br />
internal state variables <strong>and</strong> methods (functions) that operate on the<br />
internal state variables.<br />
A class is the blueprint from which individual objects are created. This<br />
means that all Java code is embedded within class data structures.<br />
You create Java objects in instantiating (making an instance <strong>of</strong>) a<br />
Java class.<br />
A variable in Java can use a class as its data-type. This means that<br />
once you create an object (i.e. an instance <strong>of</strong> a Java class) you can<br />
store that object in a variable that uses the same class as its<br />
data-type.<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 25 / 37
The HelloWorld Program<br />
A simple Hello World program, a HelloWorld class:<br />
public class HelloWorld<br />
{<br />
}<br />
<strong>LeJOS</strong> requires the st<strong>and</strong>ard main method for the program entry<br />
point:<br />
public class HelloWorld {<br />
public static void main (String[] args) {<br />
}<br />
}<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 26 / 37
LCD Display<br />
<strong>LeJOS</strong> <strong>NXJ</strong> supports the st<strong>and</strong>ard java System.out.println method<br />
<strong>and</strong> scroll the output on the <strong>NXT</strong> LCD screen.<br />
public class HelloWorld {<br />
public static void main (String[] args) {<br />
System.out.println(“Hello World”);<br />
}<br />
}<br />
If you run this program as it is, it will display Hello World <strong>and</strong> then<br />
immediately return to the menu, so you will not be able to see what is<br />
displayed.<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 27 / 37
Button class<br />
Button is in the lejos.nxt package. We can either include<br />
lejos.nxt.Button or lejos.nxt.* to allow any <strong>of</strong> the st<strong>and</strong>ard lejos.nxt<br />
classes to be used in the program.<br />
The Button class has a method waitForPress() that waits for any<br />
button to be pressed. You can find out what methods a class<br />
supports by looking at the API documentation.<br />
http://lejos.sourceforge.net/p_technologies/nxt/nxj/api/index.html<br />
The complete HelloWorld program is:<br />
import lejos.nxt.*;<br />
public class HelloWorld {<br />
public static void main (String[] args) {<br />
System.out.println(“Hello World”);<br />
Button.waitForPress();<br />
}<br />
}<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 28 / 37
System class<br />
In Java, the System class provides a set <strong>of</strong> methods that the user can<br />
invoke when they want to interact with the Java operating system. By<br />
default, one object <strong>of</strong> type System is created for each Java program.<br />
For example, using some <strong>of</strong> its methods, you can obtain the current<br />
time <strong>and</strong> the settings <strong>of</strong> various properties associated with the system.<br />
System also contains three predefined system variables, in, out, <strong>and</strong><br />
err.<br />
System.in refers to the st<strong>and</strong>ard in; by default this is the keyboard;<br />
System.out refers to the st<strong>and</strong>ard output;<br />
System.err refers to the st<strong>and</strong>ard error stream.<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 29 / 37
<strong>LeJOS</strong> <strong>NXJ</strong> Programming Environment<br />
S<strong>of</strong>tware Packages used in This Course<br />
Java language: Java SE (St<strong>and</strong>ard Edition) <strong>and</strong> JRE (Java Runtime<br />
Environment).<br />
Java firmware: <strong>LeJOS</strong> <strong>NXJ</strong>.<br />
Java program uploading link: Mindstorms <strong>NXT</strong> USB driver or<br />
Bluetooth driver.<br />
Java IDE: Eclipse IDE.<br />
The lab computers have already been installed <strong>and</strong> configured for<br />
these tools.<br />
If you want to instal <strong>and</strong> configure your own computer, see a detailed<br />
configuration instruction:<br />
http://www.bartneck.de/2008/03/04/java-lego-nxt-eclipse-tutorial/<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 30 / 37
<strong>LeJOS</strong> <strong>NXJ</strong> Main Menu<br />
When the <strong>NXT</strong> switch on, it will display the <strong>LeJOS</strong> logo before<br />
changing to the main menu.<br />
After a few second, it will display the main menu. Now it is ready to<br />
run Java programs.<br />
If it does not, the <strong>LeJOS</strong> <strong>NXJ</strong> firmware should be uploaded into flash<br />
memory. You can following this link to do so:<br />
http://www.bartneck.de/2008/03/04/java-lego-nxt-eclipse-tutorial/.<br />
You need to look at the main menu in order to use it.<br />
The link to the menu system is:<br />
http://lejos.sourceforge.net/nxt/nxj/tutorial/MenuSystem/MenuSystem.htm.<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 31 / 37
Configure a Eclipse Project<br />
Starting Eclipse IDE for<br />
Classic.<br />
click on<br />
File→New→Project<br />
<strong>and</strong> Select Java Project<br />
<strong>and</strong> click next.<br />
Name it Test. You will<br />
receive an almost empty<br />
class.<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 32 / 37
Configure Eclipse Project<br />
Then turn this project into a<br />
<strong>LeJOS</strong> project.<br />
Right-click onto the project<br />
<strong>and</strong> select Properties. Select<br />
Java Build Path on the left.<br />
Click on the Libraries tab.<br />
Afterwards, click on Add<br />
External JARs <strong>and</strong> locate<br />
the ‘lib’ directory in<br />
ProgramFiles\lejos <strong>NXJ</strong><br />
directory. Select classes.jar<br />
<strong>and</strong> press Open. The <strong>LeJOS</strong><br />
library will now be listed.<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 33 / 37
Java Source File<br />
Click on<br />
File→New→Class<br />
Name it ‘HelloWorld’<br />
<strong>and</strong> check public static<br />
void main(String[]<br />
args).<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 34 / 37
Java Source File<br />
You now need to enter a few lines <strong>of</strong> Java code. In the first line add:<br />
import lejos.nxt.LCD;<br />
Next add the following lines to the main method:<br />
LCD.drawString(Hello World!, 2, 2);<br />
LCD.refresh();<br />
while(true)<br />
Eclipse is a very smart Java editor that tries to compile your program<br />
while you write. It will underline errors with a red line.<br />
Sometimes this automatic compilation does not work <strong>and</strong> you want to<br />
force Eclipse to compile. You can achieve this by simply saving the<br />
file.<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 35 / 37
Java Program Uploading<br />
Now you can save the program <strong>and</strong> then upload it into the <strong>NXT</strong>.<br />
Before you start the uploading procedure you must make sure that<br />
the <strong>NXT</strong> is connected to the computer <strong>and</strong> that the <strong>NXT</strong> is<br />
recognized by the operating system. You also need to select your<br />
main class window in the middle before starting the uploading<br />
procedure, so that Eclipse knows which class you want to download.<br />
Notice that the program will run forever. To stop it you need to reset<br />
the <strong>NXT</strong> by holding the two middle buttons for a longer time. This<br />
will then terminate the USB connection, so you need to wait a little<br />
for it to reconnect before you upload a new program.<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 36 / 37
Java Program Improvement<br />
Of course there are better ways <strong>of</strong> ending your program, such as<br />
listening to a button press or after a certain delay. Replace the<br />
endless while loop with this:<br />
try {<br />
Thread.sleep(5000);<br />
} catch (InterruptedException e) {<br />
}<br />
Now you should be able to use <strong>Lego</strong> <strong>NXT</strong> with simple codes. More<br />
detailed programming <strong>of</strong> <strong>Lego</strong> <strong>NXT</strong> will be given in next lecture.<br />
D. Gu (Univ. <strong>of</strong> <strong>Essex</strong>) <strong>Lego</strong> <strong>NXT</strong> <strong>and</strong> <strong>LeJOS</strong> <strong>NXJ</strong> Spring 2013 37 / 37