NXT Programming

Lesson 2

In this lesson we will investigate the NXT ultrasonic sensor and use it to build and program a wall follower similar to the wall follower pp. 174-182 of Fred Martins Chapter 5, [2].

Figure 1 The NXT ultrasonic sensor.

Test of the Ultrasonic Sensor

First, you should mount the sensor on the LEGO 9797 car as described in LEGO Mindstorms Education NXT Base Set 9797 building instruction page 28 to page 30. Second, compile and upload SonicSensorTest.java, a simple sensor test program.

Exercise 1

Place the car in front of different objects at different distances and compare the distances with the readings from the sensor. Make sure to describe how the experiment is setup. E.g. as in Figure 1.1 of [6].

Exercise 2

The test program was originally compiled and uploaded with leJOS NXJ version alfa_03 as indicated in the comment to the program. Because of a known limitation of alfa_03 there is a 300 msec sample interval between readings of the distance. This limitation is no longer in the README list so try different values of the sample interval e.g. very small values and observe if it affects the readings from the sensor.

Exercise 3

The ultrasonic sensor detects objects in front of the sensor by emitting a short high-frequency sound and then listen for echoes. If an echo comes back there is an object in front. The time it takes for the echo to return can be used to measure the distance to the object. If there is no echo within some time limit the situation is interpreted as no object. The method getDistance returns 255 if there is no echo, hence no object, and otherwise a number less than 255 which is the distance in cm. Try to use the sensor with an object at a distance of up to 254 cm, can the sensor measure such a distance and under what conditions is it possible ? What is the time limit for the measurement - remember the speed of sound is 340.29 m/sec, [7]. Does this limit the usage of the sensor ?

Tracker Beam

Inspired by the "tracker beam" program in Chapter 5, [3], a first application of the sonic sensor has been programmed as a Tracker class. The Tracker class uses a simple class Car to move the car. The TrackerTester program can be uploaded to experiment with the Tracker class.

Exercise 4

Describe the behaviour of the car controlled by the Tracker class. The control loop of the Tracker class is as follows:
while ( running ) { distance = us.getDistance(); if ( distance != noObject ) { error = distance - desiredDistance; power = (int)(Pgain * error); if ( error > 0 ) { power = Math.min(minPower + power, maxPower); Car.forward(power,power); } else { power = Math.min(minPower + Math.abs(power), maxPower); Car.backward(power, power); } Delay.msDelay(sampleInterval); } }
What kind of controller has been implemented in this control loop ? What is the setpoint (SP), process variable (PV) and the manipulated variable (MV) of the controller, [4] ?

Exercise 5

The Tracker class has been used in a program RCparamTracker.java so two parameters, minPower and Pgain, of the Tracker class can be set remotely via Bluetooth from a PC GUI program TrackerController.java, Figure 2 (Remember this program should be compiled and run as a leJOS PC API program,[8]).

Figure 2 The GUI of the TrackerController program.

Try this and experiment with different values of these parameters. Try e.g. to find parameters, so the car will oscillate wildly around a distance from an object given by the constant value of the variable desiredDistance.

Wall Follower

Philippe Hurbain has build and programmed a wall follower based on the LEGO Mindstorms RCX and a home build distance sensor, [5]. He used NQC (not quite c) to program the controller for the wall follower.

Exercise 6

Try to use his program and sensor placement to write a similar program in Java and make the LEGO 9797 car follow a wall. Compare the NQC control algorithm with the different suggestions on page 179, 5.1.3 exercises, [2].


Last update: 12-02-15