Control one Thymio with another

Control one Thymio with another

Since each Wireless Thymio is equipped with a wireless module, two Thymios can communicate so as to allow one to control the other, just as a remote control could be used. For example, one could have a remote controlled racing car.

thymioremotecontrol_presentation.jpg

Requirements

- 2x Wireless Thymios
- 1x Dongle

Preparation: Setting up the Thymios

First the two Thymios must be put into the same network. For this the Network configuration guide can be used. Once completed, the two Thymios can be programmed with just one dongle and in the same window as Aseba Studio.

thymioremotecontrol_setup.jpg

Now the two Thymios are on the same channel and can communicate with one another.

Sender code

To send a message the following command is used:

emit [message_name] [variable]

For example this code:

onevent buttons
when button.forward == 1 do
   emit button_emit 2
end

sends a message named "button_emit" containing one argument with value 2 when the Thymio's forward button is touched.

However the network needs to be aware that such a message exists and that it carries a value. To do this, a global event must be created (in Aseba Studio's righthand window), with name "button_emit" and 1 argument.

It is also possible to send several values in the same message. For example:

onevent acc
emit acc_control acc

sends the 3 accelerometer values in a message named "acc_control". In this case an "acc_control" global event must be defined with 3 arguments.

One can also decide not to send any arguments at all, for example as follows:

emit prox

and of course also declaring a global event with 0 arguments. The other Thymios in the same network will see the message and can react to it, but no value is transmitted.

Receiver code

Now we want to pick up the message which has been sent by a Thymio. It's enough to use the command:

onevent [message_name]

and to write below it the code which is to react to the event. In this part we can use the variable

event.args

which contains the values sent in the message. These are in an array (a list); so the values are accessed as event.args[0], event.args[1], etc.

For example this code:

onevent button_emit
if event.args[0] == 0 then
   call leds.top(32,0,0)
elseif event.args[0] == 1 then
   call leds.top(0,0,32)
end

will light up the Thymio's top LEDs depending on the value sent in the "button_emit" message.

Example: Remote controlled Thymio

Here is how to create the remote controlled car
The aim is simple: pilot a Thymio by using another Thymio as remote control. Among other actions we want to:

  • be able to start and stop the Thymio car;
  • be able to make the car go forwards and backwards at various speeds (e.g. 3 speeds);
  • be able to make the car turn by rotating the remote control Thymio, like a steering wheel.

Remote controlled Thymio

# Remote controller

onevent acc
emit acc_control acc
if acc[0] > 0 then
    call leds.top(acc[0]*2,0,0)
else
    call leds.top(0,abs(acc[0]*2),0)
end

onevent buttons
when button.forward == 1 do
    emit button_emit 1
end

when button.center == 1 do
    emit button_emit 0
end

when button.backward == 1 do
    emit button_emit -1
end

In this code, the sender Thymio sends two types of message.

  • Upon each "acc" event (i.e. continually)), it sends the 3 accelerometer values in an "acc_control" message . In addition, it changes the colour depending on the detected acceleration;
  • Upon each "buttons" event (when a button is touched), it sends a "button_emit" message with a value depending on the button (-1 for the backward button, 0 for the centre button, 1 for the forward button).

Thymio car

# Remote controlled car

# N.B. constant MAX_SPEED must be defined in Aseba Studio

var state = STOP
var acceleration = 0
var speed = 0

onevent acc_control
if state == START then
    motor.left.target = speed - COEFF*event.args[0]
    motor.right.target = speed + COEFF*event.args[0]
    if speed > 0 and timer.period[0] == 0 then
        call leds.bottom.left(0,speed/15,0)
        call leds.bottom.right(0,speed/15,0)
        call leds.top(0,speed/15,0)    
    elseif speed < 0 and timer.period[0] == 0 then
        call leds.bottom.left(abs(speed/15),0,0)
        call leds.bottom.right(abs(speed/15),0,0)
        call leds.top(abs(speed/15),0,0)
    end
end

onevent button_emit
if event.args[0] == 0 then
    if state == STOP then
        state = START
        speed = 0
        motor.left.target = 0
        motor.right.target = 0
    elseif state == START then
        state = STOP
        speed = 0
        motor.left.target = 0
        motor.right.target = 0
    end

elseif event.args[0] == -1 then
    speed = speed - 100
    if speed < -MAX_SPEED then
        speed = -MAX_SPEED
    end

elseif event.args[0] == 1 then
    speed = speed + 100
    if speed > MAX_SPEED then
        speed = MAX_SPEED
    end
end

onevent timer0
if state == STOP then
    call leds.buttons(32,32,32,32)
elseif state == START then
    call leds.buttons(0,0,0,0)    
end
if speed == 0 then
    call leds.bottom.left(0,0,0)
    call leds.bottom.right(0,0,0)
    call leds.top(0,0,0)
elseif speed >= 0 then
    call leds.bottom.left(0,speed/10,0)
    call leds.bottom.right(0,speed/10,0)
    call leds.top(0,speed/10,0)
elseif speed < 0 then
    call leds.bottom.left(-speed/10,0,0)
    call leds.bottom.right(-speed/10,0,0)
    call leds.top(-speed/10,0,0)
end

In the preceding code, Thymio reacts to the messages which it receives. When it receives the "button_emit" message, its reaction depends on the button touched, as specified by the value sent with the message:

  • button_emit value 0 (centre button of remote controlling Thymio): it changes state (start <-> stop);
  • button_emit value -1 (backward button of remote controlling Thymio): it reduces speed in steps of 100, down to a given minimum;
  • button_emit value 1 (forward button of remote controlling Thymio): il increases speed in steps of 100, up to a given maximum;
  • acc_control (accelerometer of remote controlling Thymio): it changes the basic speed with a coefficient derived from the acceleration value received. Putting a "+" on one wheel and a "-" on the other causes Thymio to turn.

Finally, the instructions which follow "onevent timer0" determine the colour of Thymio in the different states.

Result

Note that once programmed, the Thymios are independent of the computer and the Dongle.

Summary

  1. Set up the Thymios to be in the same network;
  2. Define the global events (righthand window of Aseba Studio) giving the [message_name] and the [number_of_arguments];
  3. Send the message using : emit [message_name] [variable]
  4. Detect the message using : onevent [message_name]
  5. Process the transmitted values using the event.args variable, which contains [number_of_arguments] values.
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License