With Esterel

  What you need ?

First, a robot built with the Lego Mindstorms.

You can take a look on the robots buit by my students.

You need the Esterel compiler. You can download it from here.

And an extension (shift click on the link to save it) to the Esterel compiler  wrote by Xavier Fornari, a member of the Esterel team, to allow the Esterel compiler to produce automatically the C code needed for legOS. Take a look here to see how to use it. 

  What to do ?

Start by writing your Esterel program keeping in mind the sensors you want to used. In the small example below, we use 2 touch sensors on port 1 and 3 and 2 motors on port A and C.

legOS allow us to set the speed and the direction of the motors. We can also write string to the lcd screen.

If you want to use the extension to the Esterel compiler you must use some specific names for the inputs and the outputs. Take a look on the API

  A simple example.
 
module lego1 :
input TOUCH_1, TOUCH_3;
output MOTOR_A_SPEED(integer), MOTOR_C_SPEED(integer), 
       MOTOR_A_DIR(integer), MOTOR_C_DIR(integer),
       CPUTS(string);
relation TOUCH_1 # TOUCH_3;

constant MOTOR_FWD, MOTOR_REV, MAX_SPEED : integer;

var t : integer in
   emit MOTOR_A_SPEED(MAX_SPEED/2);
   emit MOTOR_C_SPEED(MAX_SPEED/2);
   loop
      emit MOTOR_A_DIR(MOTOR_FWD);
      emit MOTOR_C_DIR(MOTOR_FWD);
      emit CPUTS("fwd");
      await [TOUCH_1 or TOUCH_3];
      present TOUCH_1 then
         t:=1;
      else
         t:=3;
      end present;
      emit MOTOR_A_DIR(MOTOR_REV);
      emit MOTOR_C_DIR(MOTOR_REV);
      emit CPUTS("rev");
      await 100 tick;        % 1 tick = 1 ms
      if t=1 then
         emit MOTOR_A_DIR(MOTOR_FWD);
         emit CPUTS("right");
      else
         emit MOTOR_C_DIR(MOTOR_FWD);
         emit CPUTS("left");
      end if;
      await 100 tick;  % 1 tick = 1 ms
end loop
end var.

 
  After.

After you have write, compile and simulate your Esterel program you may want to try it on your robot. Next, I assume you have installed the extension to the Esterel compiler.

  Acknowledgment.

Thank's to Xavier Fornari for the extension to the Esterel compiler which produces all the C code legOS needs.

Martin
Martin.Richard@emn.fr