Reacting to shocks

In this part, we are going to program Thymio so that it reacts to shocks and makes a sound.

The accelerometer

How can a shock be detected? Which sensor can be used for that?

Detecting shocks is very common and useful. Here are some examples:

  • in a car, the shock caused by a collision can be detected and used to inflate the airbag.
  • in a number of mobile phones, when the user taps with the finger once or several times this is detected.
  • in a portable computer, the detection of a shock can be used to protect the hard disk from being damaged.

This sort of event is detected using the accelerometer. As can be guessed from its name, the accelerometer measures accelerations, such as gravity. One can also measure a free fall and shocks.

Observe the activity of the accelerometer

Aseba Studio can make graphs of variable values. For that, we have to create our own event. If we want to observe the three axes of the accelerometer (acceleration is measured in three dimensions), we need to create an event with 3 arguments. To do that, click on the + to add an event, then give it a name (e.g. my_accelerations) and specify the number of arguments (3).


Now, in the code, we can emit the acceleration value (acc variable in the variables window, which has 3 elements), from the event acc to our new event my_accelerations which can transmit 3 arguments:

onevent acc
    emit my_accelerations acc

Next, in the Events window, we can select my_accelerations and click on the graph button.


A new window opens with a graph of the 3 accelerations. There we can see what happens if the robot is turned or if it is knocked.


Here we see the 3 axes in three different colours. When the robot is standing on its wheels, gravity is detected on the z-axis (blue). When it is put on its side, the acceleration is detected on another axis (the red one). Shocks cause the peaks in the graphs.

Using the accelerometer

The accelerometer can be used in different ways. As we have seen, the acc variable gives the values of the accelerations along the three axes, and the acc event takes place each time the accelerations are measured.

In addition there is a tap event which occurs when the robot is tapped or detects a shock of some sort. This event can be used to make a sound when the robot is tapped.

onevent tap
    call sound.system(4) # this function allows a system sound to be played

previous page | next page

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