NXT Programming


Lesson 1

In this lesson we build a LEGO car and equip it with a light sensor. If not already installed then we install the leJOS Java system, [2], and use this to compile and upload a Java control program to the LEGO Mindstorms NXT control unit. The Java control program will make the car follow a black line on a white surface. All programs in this lesson can be obtained from [1].

The 9797 LEGO car

In the LEGO Mindstorms Education NXT Base Set 9797 there is a building instruction for a car, page 8 to page 22. Page 32 to page 34 shows how a light sensor can be added to the car. Build this car with a light sensor added.

Figure 1 The 9797 LEGO car with two motors.

A Java Control Program: LineFollower

The first Java program that we are going to execute on the NXT is the LineFollower.java that makes the LEGO car follow a black line on a white surface. To make the program control the car we first of all need to install the leJOS Java system for the NXT and secondly the program should be compiled and uploaded onto the NXT by means of the leJOS system. Follow the description in leJOS NXJ installation guide.

When the upload is completed the LineFollower program can be executed on the NXT. When the program is started the light percent is shown in the LCD until the LEFT button is pressed (the grey button to the left of the orange ENTER button in the middle of the NXT). When pressed the program starts to execute its control loop until the ESCAPE button is pressed ( the grey squared button below the ENTER button).

In the control loop the light percent is read and compared to a threshold value, the value of the constant blackWhiteThreshold. This means that the program assumes that the light sensor is above a black area when the light percent is below the threshold value and above a white area when the light percent is above the threshold. The actions taken is to either turn left er right. This will make the car follow a black line on a white surface when the car is placed close to the line. Or rather oscillate on the edge between black and white. The control loop is called a controller in control theory, "a controller manipulates the inputs to a system to obtain the desired effect on the output of the system", [3]. The actual controller used in the line follower is called a bang-bang controller, [4], because it only has two states, on black and on white, and an action to be performed in each state.

In the program a constant blackWhiteThreshold is used to separate light percent values into regions that are interpreted by the program as black or white. This constant should be changed if the car do not follow a black line.

Exercise 1

Try to place the light sensor above different colors and make a table of light values corresponding to the different colors. Use the light percent values for black and white to explain how the threshold value could be obtained from a reading above black and white.

Exercise 2

The light sensor is used with the red LED turned on because of the method call light.setFloodlight(true). This means that the sensor measures the reflection of the red LED. Try to turn the LED off and notice the differens in measurements obtained by making a similar table of readings above different colors. With the LED turned off the ambient light level is measured. This is e.g. usefull in day/night detection.

Exercise 3

In the program a delay of 10 msec is used between light sensor readings. We call this the sample interval. Try to let the car follow the line with a sample interval of 100 msec, 500 msec and 1000 msec. Explain what happends.

Exercise 4, Data logger

A data logger, [5], is a mechanisme that can be used to collect and record data from e.g. a sensor for later inspection. In the leJOS system a data logger can be implemented to collect data and record them in a flash file. An example of a simple data logger is DataLogger.java. By means of this data logger e.g. the light values read in the control loop can be collected and recorded in a file e.g. Sample.txt. How the DataLogger class is used can be seen in SLineFollower.java. After the program has been stopped, the data in the flash file can be transfered to the PC by means of the tool nxjbrowse. The sampled data can then be used to plot a graph of the light values:

Figure 2 Light values sampled during line following.

Try to let the car follow a line with different sample intervals and see how this influences the oscillations in the graph.

Exercise 5

In the LightSensor class the method readValue calculates a light percent according to this formula if none of the calibration methods have been used in the class:
lightPercent = 100*( 1-rawValue/1023)
The rawValue is the 10 bit digital value between 0 and 1023 that is obtained directly from the light sensor by the AD conversion (analog to digital conversion). In the program SensorPortTest.java the raw values from the light sensor is displayed on the LCD. Make some experiments that justifies the formula that describes the relation between the light percent and the raw values. Could there be a reason for using the raw values instead of the light percent?

Exercise 6

Try to use text strings directly in the calls to LCD.drawString instead of the variables right and left. Use Runtime.getRuntime().freeMemory() to display the amount of free memory on the heap during the execution. Use this to explain what happens when text strings are used directly in method calls.


Last update: 18-01-15