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 :

  [146711:notag] 48.000000

In this message, 146711 is the time (from the server clock) at which the value was read, notag is the tag of the command, which will be described in the next section. 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 an example in the section called “Playing sounds” ).