Friendly Behaviour (Green)

thymioII-green.jpg

Friendly Thymio

Place your hand or an object at least 20 cm in front of the front central sensor. Slowly move your hand towards the sensor. When it detects your hand the Thymio will move towards it. At a certain distance it will stop. If you move your hand closer, the Thymio will move away from your hand. If you move your hand so that it is detected by one of the front left or right sensors, the Thymio will turn to face your hand. If the Thymio reaches the edge of a table, it will stop. The rear three yellow circle leds are lit but this is just for decoration.

Place two Thymio facing each other, both with the Friendly behaviour, they will align themselves and start changing colours!

Place two Thymio one behind the other with the rear Thymio running the Friendly behaviour (green) and the front Thymio running the Explorer behaviour (yellow). The friendly Thymio will follow the explorer Thymio!

This Aseba .aesl file contains the source code implementing this behaviour.

Here is the Blockly program for this behaviour (without colours, sound and table edge detection).


Dive into the code

The variables declaration

The variables list must be placed at the beginning of the code.
In this part of the code, we see the keyword var before each variable. This indicates to the compiler that the following word is a variable. The variables are global (they are accessible from anywhere in the code), so they need to have unique names, depending on their purpose! If you choose variables named temp1, temp2, temp3, etc., it will not be so easy to remember which one does what.
The variable names are probably obscure for you now but we will clarify them when we explore the different parts of the code.

The variables can be initialized to a specific value, as the speed one is in line 8, with the declaration var speed = 300. If a variable is not initialized as in line 4, its value is undefined (it may contain anything).

You can learn more about variables in the Aseba language reference documentation.

The timer set up

We want to show a glowing light in the body of the robot. For that, we need to update the light at regular intervals. To do so, the above code enables timer 0 with a period of 20 ms (milliseconds, 1 second = 1000 ms), meaning that every 20 ms the event timer0 will be triggered. Thymio has two timers, the 0 and the 1. These timers can be configured for periods between 1 and 32767 ms. They can be set independently and will each trigger an event, timer0 respectively timer1. This is particularly useful if you want a certain action to be performed at regular time intervals.

If you had written timer.period[1]=100, it would have meant that the event timer1 would have been triggered every 100 ms. You can learn more about timers in the programming interface reference documentation.

The code at line 16 call leds.circle(0,0,0,32,32,32,0,0) calls a Thymio API that will make part of the top LED yellow circle light up and seem like a smile!

The buttons event

This part of the code allows to start, stop, and increase or decrease the speed of Thymio.
The # symbol in a line signifies that it and the rest of the line are comments which will be ignored: it will not affect the robot's behaviour, it is there just for documenting the code.
The robot will go faster when the forward button is touched, slower when the backward button is touched and stop when the central button is touched.

We see the line onevent buttons. This means that whenever any button is pressed, the event is triggered.

In the code, we can see three similar blocks. The structure is based on the use of when … do … end. This structure is similar to the if … then … end with one exception. The when condition executes the code only if the previous value was false and the current value is true. It allows an action to be performed only if something changed !

The first block of when … do … end will be executed only when the forward button is touched, the second block when the backward button is touched and the last one when the central button is touched.

In the first block, the speed of the robot is increased by adding 50 to it. If the limit of 500 is reached the speed is no longer increased. In the second block the speed is decreased by subtracting 50 and and if the limit of -300 is reached the speed is no longer decreased. In the last one, the robot is simply stopped by setting its speed to 0 (and also the target speed to 0).

The timer event

The first line of this part of code is onevent timer0. This means that every time the event timer0 is triggered, the robot will do what is written in the block which follows. In this case, this part of the code will be executed every 20 ms as it has been set as described in the previous part.
This part will make Thymio pulse in a green colour.

Lline 38 increments the variable led_pulse. It will just add 1 to the variable every time the timer0 event is triggered.

Next we find another conditional test but this time in two parts. This one is in the common form of:

if … then … else … end

Let's break this down. It is done in four key words:

  • if: we test the variable (here the question is: is led_pulse bigger than 0?)
  • then: if the condition is true (if led_pulse is bigger than 0), we execute the following code (call leds.top(led_pulse…)…)
  • else: if the condition is false (if led_pulse is smaller than or equal to 0), we execute the following code (call leds.top(-led_pulse…))
  • end: this is simply the end of the test.

Let's take an example. Let's say that, at the beginning, led_pulse is equal to 0. Then, it will be incremented in line 38 and will become 1. The test if led_pulse > 0 will be true, so the LEDs on the top of Thymio will be set to (1, 1, 0). The next test in line 41 will be false as led_pulse is not bigger than 40, it will thus stay unmodified. Finally, as the first condition was true (line 39), the else condition will not be true.

This will continue like that 40 times in a row, then when led_pulse is equal to 41, the second condition (line 41) will be true. led_pulse will then be set to -128. The next time the timer0 event is triggered, the first condition (line 37) will be false, thus the else code will be executed. The variable temp is set to -led_pulse/4 and then it will just set the top LED brightnesses equal to temp in order to make them glow less and less brightly each time the timer0 timer is triggered, but four time slower than the increase part of the glowing cycle.

The prox event

This is the heart of this program: this part of code makes the robot behave in a "friendly" way: it will follow an object detected by its sensors if it moves away, avoiding bumping into it if the object stops, and going backward if the object comes closer. In the code there is a constant DETECT. In Aseba Studio constants are declared outside the code listing because they are global to the entire environment. In this case DETECT is equivalent to the value 500.

The table border detection

Here, the idea is to stop Thymio if it reaches the edge of a table.

We find another simple if … then … else … end conditional test. The test concerns the prox.ground.reflected[0] and prox.ground.reflected[1] variables. If you put your Thymio on a table and look at the values of these variables, they should be between 300 and 1000. Now, if you take your robot and lift it off the table, these sensors should indicate approximately 0. Knowing that, we just need to test if the ground proximity sensors are smaller than a certain threshold. If so, the robot is not on a table, it has to stop (and the bottom LEDs are lit in red thanks to call leds.bottom.left(32,0,0) and call leds.bottom.right(32,0,0)). If not, the robot is on a table, we just turn off the LEDs and let the speed be managed by the Follower controller. Note that the ground sensors are at the front of Thymio, so are no help when the robot is going backwards!

Thymio_IR_Ground_en.png

A small note concerning the ground sensors. You can see that there are three different values for the ground sensors: prox.ground.ambiant, prox.ground.reflected and prox.ground.delta. The first one measures the ambient light without emitting any IR pulse, the second one measures the reflected IR pulse and the last one shows the difference between the first two. This way, we can have a value of the reflected IR pulse without the pollution of the ambient light.

That's it !

Have fun with your Thymio !

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License