Next

Prev

Prev-tail

Tail

Up

Chapter 24
Mindstorms NXT

This documentation contains information about the LEGO Mindstorms NXT Urbi engine. The first two chapters provide a short tutorial for using Urbi to control the TriBot robot. The remainder of the documentation contains an exhaustive reference for the LEGO Mindstorms NXT Urbi engine devices.

 24.1 Launching Urbi for Mindstorms NXT
 24.2 First steps with Urbi to control Mindstorms NXT
  24.2.1 Make basic movements
  24.2.2 Improving the movements
  24.2.3 Reading sensors
  24.2.4 Tagging commands
  24.2.5 Playing sounds
  24.2.6 Cyclic moves
  24.2.7 Parallelism
  24.2.8 Using functions
  24.2.9 Loading files
  24.2.10 Conclusion
 24.3 Default layout reference
  24.3.1 Motors
  24.3.2 Sensors
  24.3.3 Battery
  24.3.4 Beeper
  24.3.5 Command
 24.4 How to make its own layout
  24.4.1 Instancing Motors
  24.4.2 Instancing sensors
  24.4.3 Other devices
 24.5 Available UObject Devices
  24.5.1 Servo
  24.5.2 UltraSonicSensor
  24.5.3 SoundSensor
  24.5.4 LightSensor
  24.5.5 Switch
  24.5.6 Battery
  24.5.7 Beeper
  24.5.8 Command
  24.5.9 Instances
  24.5.10 Groups

24.1 Launching Urbi for Mindstorms NXT

First you should build the TriBot model from the LEGO Mindstorms NXT basic model.

Install the engine Mindstorms by untaring it into the Urbi engine runtime folder. Follow instructions in INSTALL and README files

The file ‘TriBot.u’ is loaded by default when server is launched. You can remove it by modifying global.u.

24.2 First steps with Urbi to control Mindstorms NXT

This section is a short tutorial for using Urbi with the LEGO TriBot model with the default configuration provided with the Urbi server. Users familiar with Urbi should look directly to the next section for an extensive list of the available devices.

24.2.1 Make basic movements

We will first move the wheels of the robot.

In Urbi, all the motor’s speed and the sensor’s value in a robot are associated with variables. You can set the motor’s speed or get the sensor’s value by assigning or reading the values of the corresponding variables.

In the default server layout for the TriBot model, the left wheel is assigned to variable wheelL and the right wheel is assigned to wheelR1 . The values of these variables control the corresponding wheel speed. First put the TriBot upside-down to avoid any accident, then you can move the left wheel just doing:

 
wheelL.speed = 50;  

Don’t omit the semicolon at the end of the line, or the command will not be executed. You can also move the right wheel (a negative value is a move backward):

 
wheelR.speed = -50;  

A group is also defined so that you can give orders to several motors at the same time. In our case, the group wheels contains both robot wheels. Setting wheels to a value will set both wheels to this value:

 
wheels.speed = 0;  

will stop both wheels. The third motor of the TriBot is associated with the claw variable.

24.2.2 Improving the movements

The commands given so far simply set a speed to the wheels, but you can use Urbi to make precise sequences of movements. It is for example possible to set a speed for a given time, then stop.

To achieve that, Urbi provides a command to wait for a given time: sleep (duration). And you can make sequences of commands separated with semicolon that will be executed one after another. So the following line:

 
wheels.speed = 50; 
sleep(10s); 
wheels.speed = 0;  

will make the robot to go forward during 10 seconds then stop.

It is also possible with Urbi to control the way the values will change. In the previous examples, the motor speed goes from 0 to 50 as fast as possible. You can use a modifier to the variable assignation to control the way the variable value will change. For example, if you want to reach a value in a given time, use the time: modifier:

 
wheels.speed = 50 time:3s; 
wheels.speed = 0 time:3s;  

Using this code, the robot will accelerate to reach the speed of 50 after 3 seconds, then slow down to stop after another 3 seconds. Other modifiers are available in Urbi, they are described in the Urbi tutorial available at http://www.gostai.com/doc.php.

24.2.3 Reading sensors

In all the previous steps the commands don’t take the environment into account. To do that, you need to get the sensor values. In Urbi, you just have to type the associated variable name. For example when you type:

 
sonar.val;  

The server returns a message with the ultrasonic sensor value:

 
[00146711] 48  

In this message, 146711 is the time (from the server clock) at which the value was read, 48.00000 is the value of the sonar sensor, in this case the distance to the obstacle in front of it. You also need a way to react to the sensor values. In Urbi, this is achieved using event catching commands. These commands check some condition and launch other commands when these conditions are verified. For example the following command:

 
at (sonar.val < 50) 
  wheels.speed = 0;  

will stop the robot if an obstacle is detected at less than 50 centimeters. Note that this command remains active in the server. If you move the robot and set a forward speed:

 
wheels.speed = 50;  

the robot will stop again when another obstacle is encountered. The other sensors available for the TriBot are the sound sensor decibel, the bumper sensor bumper and the light sensor light. The following command:

 
at (bumper.val == 1) 
  wheels.speed = -50;  

will make the robot go backward if something hits the bumper. Note that the at command reacts only when the condition becomes true. If you want to loop the execution of an action whenever the condition is true, use the whenever command (see Section 24.2.5).

24.2.4 Tagging commands

The at commands you entered before are still active in the server. You need a way to stop them if you want to do something else. The tagging mechanism will enable you to do that. A tag in a name associated to a command. To tag a command with the name mytag, create it and simply prefix the command. For example:

 
var mytag = Tag.new; 
mytag: at (decibel.val > 0.7) 
  wheels.speed = 0;  

will launch the event catching command using the decibel sensor with tag mytag. Later you will be able to stop that command:

 
mytag.stop;  

will stop the previous command and the robot will not react to sound anymore. It if also possible to freeze temporarily a command:

 
mytag.freeze;  

will stop the commands tagged with mytag, but it will not be deleted, and

 
mytag.unfreeze;  

will resume the command.

24.2.5 Playing sounds

The LEGO Mindstorms NXT is able to play sounds. The object beeper is associated with this capacity. However, you can’t just assign a value to the variable beeper, because playing a sound requires several parameters. You therefore have to call a method of the beeper object:

 
beeper.play(200, 3s);  

This will play a beep at 200Hz during 3 seconds (beep’s frequency must be between 200Hz and 14000Hz). The parameters of the method may also be the result of operations, or depend on other variables. For example, it is possible to play a sound which frequency depends on the obstacle distance:

 
var mytag = Tag.new; 
mytag: whenever (sonar.val < 100) 
  beeper.play(200 + 6 * sonar.val, 3ms);  

This plays beeps with a lower frequency as the obstacle comes closer (under 1 meter).

24.2.6 Cyclic moves

We now introduce a modifier in Urbi that makes it possible to do cyclic moves of sinusoidal shape. This modifier has the particularity to make the assignation never terminate. To be able to enter new commands after this modifier is used, you need to put this command in background so that the Urbi server continues processing new commands. This is achieved by terminating the command with a comma instead a semicolon. More details about these command separators will be given in Section 24.2.7. The sinus modifier can be used this way:

 
var mytag = Tag.new; 
mytag: wheels.speed = 0 sin:10s ampli:100,  

This command will make the speed of the wheels oscillate around 0, between -100 et 100 with a period of 10s. Use a tag to be able to stop the command. To make the speed change from 0 to 100 with a period of 3 seconds, use the command:

 
var tag2 = Tag.new; 
tag2: wheels.speed = 50 sin:3s ampli:50,  

24.2.7 Parallelism

Urbi handles command parallelism in a very simple way. Parallelism is enforced through various command separators:

This explain why the comma was necessary in the previous section. If we finish a command that last forever with a semicolon, the server will wait forever before starting a new command. If you happen to make this mistake, it is still possible to open a new connection to the Urbi server using another client and using the stop tag; command.

Here is an example where we use parallelism to move the TriBot awkwardly while playing sounds:

 
wheelR.speed = 0 sin:3s ampli:50 & 
wheelL.speed = 0 sin:4s ampli:30 & 
beeper.play(200,3s),  

Parallelism handling is a key feature of Urbi, which has in fact four command separators. See the Urbi tutorial available at http://www.gostai.com/doc.php for more information.

24.2.8 Using functions

As in most programming language, it is possible to write functions in Urbi. The functions are useful for making more complex programs where the same commands are used several times. The following example defines a function which moves the robot for a given time at a given speed:

 
function Global.forward(speed,timer) 
{ 
  wheels.speed = speed; 
  sleep(timer); 
  wheels.speed = 0; 
},  

Once defined, this function can be called simply:

 
Global.forward(100,3000);  

More details about functions, objects and variables in Urbi are available in the Urbi tutorial (http://www.gostai.com/doc.php).

24.2.9 Loading files

When making larger Urbi programs, it is easier to save them in files and to command the server to load the files. The files are simple text files in which you write Urbi commands. These files should be saved in the data/ sub-directory of your sever directory. The files should use the .u extension which is the extension used for Urbi script files. An example demo.u is provided with the server. To make the Urbi server to execute this script, type the Urbi command:

 
  load("demo.u");  

24.2.10 Conclusion

This chapter has highlighted the main features of Urbi with the Mindstorms TriBot robot. The next chapter details all the devices available for the TriBot in the default layout. For more details about the Urbi language, please refer to the Urbi tutorial available at http://www.gostai.com/doc.php.

24.3 Default layout reference

This chapter describes the objects defined in the ‘TriBot.u’ layout shipped with the Urbi Mindstorms NXT server.

24.3.1 Motors

Three motors are defined, wheelL, wheelR and claw. A group, wheels, is defined to group wheelL and wheelR:

 
wheels.speed = 10;  

is equivalent to

 
  wheelL.speed = 10 & wheelR.speed = 10;  

The slot .speed allows you to set the current speed of the motor. This value is set between -100 and 100.

Motor position (in degrees) can be accessed using the .val slot. This slot is set to 0 when you turn the robot on.

24.3.2 Sensors

Sensors are grouped in the sensors group and all hardware devices (sensors + motors + battery + beeper) are grouped in the hardware group.

24.3.2.1 Bumper

The switch device in front of the robot is called bumper. Its value is 1 if it is pressed, 0 otherwise.

24.3.2.2 Sonar

The ultrasonic sensor is called sonar. Its value is the distance measured by the sensor in centimeters between 0 and 255. When the read operation fails, 255 is returned.

24.3.2.3 Decibel

The sound sensor is called decibel. Its value relates the level of ambient sound. It is between 0 and 1. Two different modes can be used by changing the .mode slot to ”DB” or ”DBA”.

DBA
is a mode measuring only frequencies between 200 and 14000Hz.
DB
measures a wider band.

Default value is ”DBA”.

24.3.2.4 Light

The light sensor is called light. The value returned is between 0 an 1 representing the amount of light measured. Three different modes are available by changing the .mode slot.

Reflector
means the sensor lights on its led and measures the light reflected.
Ambient
lights off the led and measures the ambient light.
Normal
lights off the led too and return the raw value measured.

Default value is ”Reflector”.

24.3.3 Battery

The battery device is called battery. Its value is the current battery level between 0 and 1, 1 is full charge.

24.3.4 Beeper

The beeper device is called beeper. You can request the beeper to play a sound using the play method:

 
beeper.play(frequency, time);  

frequency is an integer between 200 and 14000, which is the frequency of the sound. time is an integer, which is the duration of the sound in milliseconds. A duration of 0 is infinite. The command returns immediately. If you want to wait until the end of the beep, use:

 
{ 
  beeper.play(frequency, duration); 
  sleep(duration) 
},  

24.3.5 Command

The Command UObject is not a device. It makes it possible to send direct commands as you do with the NXT SDK. You will find more information on http:// mindstorms.lego.com/ This object has methods to send and receive data to the NXT:

 
Command.send(buff);  
where:

For example:

 
Command.send([3, 10, 10, 0 , 0]);  
will play a beep.

 
answer = Command.request(buff, size) ;  

where:

This command is designed for expert users. Using it is not recommended if you don’t know what you are doing and might result in server crashes. Here is an example:

 
answer = Command.request([7,0], 15);  

that returns the information on input port 1.

24.4 How to make its own layout

In this chapter, the method to build a custom layout for a different robot is explained. A good understanding of the objects in Urbi in useful and the reading of the Urbi tutorial is advised (available at http://www.gostai.com/doc.php).

There are three kind of devices provided by the Mindstorms NXT server: motors, sensors and other devices. There is one type of motor called Servo. There is four types of sensors: Switch, SoundSensor, UltraSonicSensor and LightSensor. Two other devices are available: Battery and Beeper. A complete description of these devices is available in Section 24.5. Writing a layout entails instancing particular objects from the devices above.

24.4.1 Instancing Motors

The Servo init constructor has the following syntax:

 
Servo.new(port);  

where port is either ”A”, ”B” or ”C”. The created object makes it possible to control the motor connected to the port in parameter. As an example, in the current ‘TriBot.u’, three Servo objects are created:

 
wheelL = Servo.new("C"); 
wheelR = Servo.new("A"); 
claw = Servo.new("B");  

The wheel left is on port C, the wheel right on A and the claw is on port B.

24.4.2 Instancing sensors

To create a Switch object, the syntax is the following:

 
Switch.new(port);  

where port can be 1, 2, 3 or 4.

In ‘TriBot.u’ the switch is used as a bumper plugged on the port 4 so the instancing is:

 
bumper  = Switch.new(4);  

UltraSonicSensor objects are created the same way:

 
UltraSonicSensor.new(port);  

port can be 1, 2, 3 or 4.

In ‘TriBot.u’ an ultrasonic sensor is connected on port 2:

 
sonar = UltraSonicSensor.new(2);  

The SoundSensor object has an additional parameter:

 
SoundSensor.new(port, mode);  

port can be 1, 2, 3 or 4. mode sets the mode of the sensor. It can be ”DB” or ”DBA”, meaning ”decibel” or ”decibel adjusted” (the last one records sounds in the frequency range 200-14000Hz). In ‘TriBot.u’, the sound sensor is on port 1 using decibel adjusted:

 
decibel = SoundSensor.new(1,"DBA");  

LightSensor also has two parameters:

 
LightSensor.new(port, mode);  

port can be 1, 2, 3 or 4. mode sets the sensor mode and it can be ”Ambient” (so the sensor measures the ambient light), ”Reflector” (the sensor lights on its led and measures the reflected light) and ”Normal” (the sensor returns its raw value).

In ‘TriBot.u’, the light sensor is in reflector mode on port 1:

 
light = LightSensor.new(3,"Reflector");  

24.4.3 Other devices

The Battery device makes it possible to get the current battery level.

 
Battery.new;  

In ‘TriBot.u’:

 
battery = Battery.new();  

The Beeper device makes it possible to emit customized beeps from the NXT speaker.

 
Beeper.new();  

In ‘TriBot.u’:

 
beeper = Beeper.new();  

24.5 Available UObject Devices

This section exhaustively describes the UObjects available in the LEGO Mindstorms NXT Urbi server.

24.5.1 Servo

Servo describes a motor. A Servo instance contains the following slots:

24.5.2 UltraSonicSensor

UltraSonicSensor describes a sonar sensor. A UltraSonicSensor instance contains the following slots:

24.5.3 SoundSensor

SoundSensor describes a sound sensor. A SoundSensor instance contains the following slots:

24.5.4 LightSensor

LightSensor describes a light sensor. A LightSensor instance contains the following slots:

24.5.5 Switch

Switch describes a touch sensor. A Switch instance contains the following slots:

24.5.6 Battery

Battery describes a battery device. A Battery instance contains the following slots:

24.5.7 Beeper

Beeper describes a speaker device. A Beeper instance contains the following methods:

24.5.8 Command

Command allows you to use expert commands. The Command UObject contains the following methods:

24.5.9 Instances

Here are the different instances in the ‘TriBot.u’ layout.




Instance UObject Description



wheelL Servo Left wheel
wheelR Servo Right wheel
claw Servo Claw
sonar UltraSoundSensor Distance sensor
decibel SoundSensor Sound sensor
light LightSensor Light sensor
bumper Switch Touch sensor
beeper Beeper Emits beeps
battery Battery Battery



24.5.10 Groups

Here are the different groups provided in the current layout.




Group Name Description



wheels The two wheels
motors All the motors
sensors All the sensors
hardware All the devices