Thymio Programming Interface¶
This page describes the programming capabilities of Thymio. It lists the different variables and functions and indicates to which elements of the robot they refer.
Thymio has 7 distance sensors around its periphery. An array of 7
prox.horizontal, holds the values of these sensors:
prox.horizontal: front left
prox.horizontal: front middle-left
prox.horizontal: front middle
prox.horizontal: front middle-right
prox.horizontal: front right
prox.horizontal: back left
prox.horizontal: back right
The values in this array vary from 0 (the robot does not see anything)
to several thousand (the robot is very close to an obstacle). Thymio
updates this array at a frequency of 10 Hz, and generates the
event after every update.
Thymio holds 2 ground distance sensors. These sensors are located at the front of the robot. As black grounds appear like no ground at all (black absorbs the infrared light), these sensors can be used to follow a line on the ground. Three arrays hold the values of these sensors:
prox.ground.ambiant: ambient light intensity at the ground, varies between 0 (no light) and 1023 (maximum light)
prox.ground.reflected: amount of light received when the sensor emits infrared, varies between 0 (no reflected light) and 1023 (maximum reflected light)
prox.ground.delta: difference between reflected light and ambient light, linked to the distance and to the ground colour.
For each array, the index 0 corresponds to the left sensor and the index
1 to the right sensor. As with the distance sensors, Thymio updates this
array at a frequency of 10 Hz and generates the (same)
after every update.
Thymio can use its horizontal infrared distance sensors to communicate a
value to peer robots within a range of about 15 cm. This value is sent
at 10 Hz while processing the distance sensors. Thymio sends an 11-bit
value (but future firmware could use one of the bits for internal use,
thus it is better to stay within 10 bits). To use the communication,
prox.comm.enable(state) function, with 1 in
enable communication or 0 to turn it off. If the communication is
enabled, the value in the
prox.comm.tx variable is transmitted every
100 ms. When Thymio receives a value, the event
prox.comm is fired
and the value is in the
Thymio contains a 3-axes accelerometer. An array of 3 variables,
acc, holds the values of the acceleration along these 3 axes:
acc: x-axis (from right to left, positive towards left)
acc: y-axis (from front to back, positive towards the rear)
acc: z-axis (from top to bottom, positive towards ground)
The values in this array vary from -32 to 32, with 1 g (the
acceleration of the earth’s
corresponding to the value 23. Thymio updates this array at a frequency
of 16 Hz, and generates the
acc event after every update. Moreover,
when a shock is detected, a
tap event is emitted.
temperature variable holds the current temperature in tenths of
a degree Celsius. Thymio updates this value at 1 Hz and generates the
temperature event after every update.
Thymio provides two user-defined timers. An array of 2 values,
timer.period, allows to specify the period of the timers in ms:
timer.period: period of timer 0 in milliseconds
timer.period: period of timer 1 in milliseconds
The timer starts the countdown when it is initialized.
When the period expires, the timer generates a
timer1 event. These events are managed in the same way as all the
others and cannot interrupt an already executing event handler.
If you restart a program with a timer, the timer could still be counting down and an event can occur before you expect it. This is not usually a problem if you use a timer that expires repeatedly at short intervals, because you can set a state variable to ignore timer events until you are ready. It is recommended that you not use the timer for a single (long) interval because the results can be inconsistent.
Thymio holds many LEDs spread around its body. Most of them are associated with sensors and can highlight their activations: by default, the intensity of the LED is linked to the sensor value. However, once LEDs are used in the code, the programmer takes over control and they no longer reflect the sensor values.
Native functions allow the various LEDs to be controlled. For all LEDs, their intensity values range from 0 (off) to 32 (fully lit).
The LED circle on top of the robot¶
8 yellow LEDs make up a circle on top of the robot, around the buttons.
Default activation: reflects the values of the accelerometer. All LEDs are off when the robot is horizontal. When the robot tilts, a single LED shows the lowest point, with an intensity proportional to the tilt angle.
leds.circle(led 0, led 1, led 2, led 3, led 4, led 5, led 6, led 7)where
led 0sets the intensity of the LED at the front of the robot, the others are numbered clockwise.
The RGB LEDs¶
There are two RGB LEDs on the top of robot, driven together. These are the LEDs that show the behaviour of the robot. There are two other RGB LEDs on the bottom of the robot, which can be driven separately.
Default activation: off when in Aseba mode.
leds.top(red, green, blue)sets the intensities of the top LEDs.
leds.bottom.left(red, green, blue)sets the intensities of the bottom-left LED.
leds.bottom.right(red, green, blue)sets the intensities of the bottom-right LED.
The LEDs of proximity sensors¶
Every proximity sensor has a companion red LED on its side (the front sensor has two LEDs, one on each side).
Default activation: on when an object is close to the associated sensor, with an intensity inversely proportional to the distance.
leds.prox.h(led 1, led 2, led 3, led 4, led 5, led 6, led 7, led 8)sets the LEDs of the front and back horizontal sensors.
led 6correspond to the front LEDs, from left to right, while
led 8correspond to the left and right back LEDs.
leds.prox.v(led 1, led 2)sets the LEDs associated with the bottom sensors, left and right.
The LED of the RC receiver¶
This red LED is located close to the remote-control (infrared) receiver.
Default activation: blinks when the robot receives an RC5 code.
leds.rc(led)controls this LED.
The LEDs of the temperature sensor¶
These two LEDs (one red and one blue) are located close to the temperature sensor.
Default activation: red if the temperature is over 28°C, red and blue between 28° and 15°, blue if the temperature is below 15°.
leds.temperature(red, blue)controls this LED.
The microphone LED¶
This blue LED is located close to the microphone.
Default activation: off.
leds.sound(led)controls this LED.
There are also other LEDs that the user cannot control:
- 3 green LEDs on the top of the robot show the battery voltage
- a blue and a red LED on the back of the robot show the charge status
- a red LED on the back of the robot shows the SD-card status
You can change the wheel speeds by writing in these variables:
motor.left.target: requested speed for left wheel
motor.right.target: requested speed for right wheel
You can read the real wheel speeds from these variables:
motor.left.speed: real speed of left wheel
motor.right.speed: real speed of right wheel
The values range from -500 to 500. A value of 500 approximately
corresponds to a linear speed of 20 cm/s. You can read the value of the
motor commands from the variables
mic.intensityshows the current microphone intensity (in the range 0 to 255), while variable
mic.thresholdcontains the limit intensity for the event. If
mic.threshold, then the event
Playing and recording sounds¶
You can play synthetic or system sounds. Moreover, if you have installed
a micro-SD card
formatted as FAT, you can
record and play your own sounds. The files are stored in the micro-SD
card, in wave format, 8-bit
unsigned, 8 kHz. When Thymio finishes playing a sound requested through
Aseba, it fires the event
sound.finished. It does not fire an event
if playing is interrupted or if a new sound is played.
The native function
sound.freq plays a frequency, specified in Hz,
for a certain duration, specified in 1/60 s. Specifying a 0 duration
plays the sound continuously and specifying a -1 duration stops the
Changing the primary wave¶
Synthetic sound generation works by re-sampling a primary wave. By
default, it is a triangular wave, but you can define your own wave using
sound.wave native function. This function takes as input an
array of 142 samples, with values from -128 to 127. This buffer should
represent one wave of the tonic frequency specified in
As Thymio plays sounds at 7812.5 Hz, this array is played completely at
the frequency of 7812.5/142 = ~55 Hz. Playing a sound of a higher
frequency skips samples in the array.
You can record sounds using the
sound.record native function. This
function takes as parameter a record number from 0 to 32767. Files are
stored on the micro-SD card under the name
x is the
parameter passed to the
sound.record function. To stop a recording,
sound.record function with the value of -1.
You can replay a recorded sound using the
function. This function takes as parameter a record number from 0 to
32767 and will replay file
Rx.wav from the SD card where
the parameter passed to the
sound.replay function. To stop a replay,
sound.replay function with the value of -1.
Duration (from firmware version 11)¶
You can retrieve the duration of a recorded sound using the
sound.duration(x,duration) native function. Its first parameter,
x, is a number from 0 to 32767 which is the index of file
from the SD card. The result in 1/10 of seconds is put in the variable
duration as second parameter.
Creating sound on your computer¶
You can create sounds for Thymio using your computer. An efficient way to do so is to use the Audacity software, version 1.3, which exists for various operating systems. Here are the steps to create a sound compatible with the Thymio:
- Once Audacity has started, change the project rate from 44100 Hz (default) to 8000 Hz. This setting is located at the bottom-left of Audacity’s window.
- Record your sound with the red record key in the top-left part of the window. You should see the cursor advancing and the wave changing. Stop with the stop button.
- Your sound should be in mono (Tracks->Stereo to Mono)
- Go to the File menu under Export…
- Give a file name, for instance
P0.wavfor the first file to play using the
- Choose other uncompressed files as format format.
- Under options, choose a WAV (Microsoft) header and as Encoding, choose Unsigned 8 bit PCM.
- Make sure that no metadata values ares set.
- Save or copy the file to the micro-SD card.
Here’s an instructional video on how to do the above.
You can play a user-defined sound using the
function, which takes a record number from 0 to 32767 as parameter. The
file must be available on the micro-SD card under the name
x is the parameter passed to the
sound.play function. To
stop playing a sound, call the
sound.play function with the value
You can play a system sound using the
sound.system native function,
which takes a record number from 0 to 32767 as parameter. Some sounds
are available in the firmware (see below), but you can overwrite these
sounds and add new ones using the SD-card. In this case, the file must
x is the parameter passed to the
sound.system function. To stop playing a sound, call the
sound.system function with the value -1.
System sound library¶
The following sounds are available:
||stop playing sound|
||shutdown sound (this sound is not reconfigurable)|
||arrow button sound|
||central button sound|
||free-fall (scary) sound|
||target ok for friendly behaviour|
||target detect for friendly behaviour|
Thymio contains a receiver for infrared remote controls compatible with
the RC5 protocol. When Thymio
receives an RC5 code, it generates the
rc5 event. In this case, the
rc5.command are updated.
Read and write data from the SD card¶
If an SD card is present, the variable
sd.present is set to 1
(otherwise 0), and Thymio can read and write data to files. Only a
single file can be open at any given time. The unit of reading/writing
is a signed 16-bit binary value. The functions provided are:
sd.open(x,status): opens the file
Ux.DAT. The value
xshould be a number between [0:32767], using -1 closes the currently open file. A value of 0 is written in the
statusvariable if the operation was successful, -1 if the operation has failed.
sd.write(data,written): attempts to write the complete
dataarray in the currently opened file. The number of values written is returned in the
writtenparameter. It should be equal to the size of
data, except if the card was full, or if the file was larger than 4 Gb, or no file was open.
sd.read(data,read): reads and fills the
dataarray from the currently opened file. The number of values read is returned in the
readparameter. It should be equal to the size of
data, except when the end of the file is encountered or no file was open.
sd.seek(position,status): moves the current read and write pointers in the currently opened file. The cursor is moved to the absolute
positionin the opened file. The valid range is [0:65535]. It is currently not possible to seek to a position after 65535. A value of 0 is written in the
statusvariable if the operation was successful, -1 if the operation has failed.
The format consist of a simple concatenation of the signed 16-bit binary values.
Note: do not remove the SD card while the robot is turned on. Always power-off the robot before removing the SD card.
Loading a program from the SD card¶
Thymio can load a program from the SD card. When it boots, Thymio loads
vmcode.abo from the SD card if present.
To obtain the
vmcode.abo file from your .aesl file, open Aseba
Studio and open your program (let’s call it
click on (1) “Tool”, then (2) “Save binary code…”, then (3)
“…of thymio”. You will see a dialog box opening (4). Choose a place
where to save your file and that’s it, you saved
the .abo format. Don’t forget to call it
vmcode.abo if you want your
Thymio to read it when it starts.
Table of local events¶
||back arrow was pressed or released||upon action||
||left arrow was pressed or released||upon action||
||central button was pressed or released||upon action||
||front arrow was pressed or released||upon action||
||right arrow was pressed or released||upon action||
||button values have been probed||50||
||proximity sensors were read||10||
||value received from IR sensors||upon value reception||
||a shock was detected||upon shock||
||the accelerometer was read||16||
||ambient sound intensity was above threshold||when condition is true||
||a sound started by aseba has finished playing by itself||when sound finishes|
||temperature was read||1||
||the infrared remote-control receiver got a signal||upon signal reception||
||PID is executed||100||
||when timer 0 period expires||user-defined|
||when timer 1 period expires||user-defined|