When a mobile robot moves around, one often thinks that the ground is flat with obstacles of a given size that can be detected for avoidance, for instance. In real applications this is often wrong, natural ground not being flat and real obstacles having different sizes. In this type of situation slopes are a big danger. If the slope is too steep, the robot can fall on its side and be unable to move. How can the steepness of a slope be measured while the robot is running? With an accelerometer! An accelerometer is a sensor allowing accelerations to be measured, and therefore also gravity. It can measure the intensity and orientation of accelerations. Therefore it can measure the inclination of the robot with respect to gravity, a universal force which on the earth causes acceleration in the vertical direction.
The Thymio II robot has an accelerometer and can use it to measure the steepness of the slope where it is running. A nice example of terrain with variable slopes exists in every house: it is the bathtub. It will be our test environment:
In a bathtub the central ground is flat but the slope becomes steeper towards the edges. As the robot moves around, it will tilt more and more. The vertical orientation of the robot is measured by the accelerometer. The following simple code implements slope avoidance:
In this code, if no information comes from the accelerometers (acc[0] and acc[1] for lateral and front/back inclination) the robot moves forward at a speed of 130. If the robot tilts laterally (absolute value of acc[0] increases), we increase speed on one side and reduce it on the other side, making the robot turn. If the robot tilts forward or backwards (the absolute value of acc[1] increases), we decrease or increase the speed of both wheels.
With this behaviour we get the following:
In the first example the movement is jerky because of the noise on the accelerometer. To get a smoother movement it is necessary to smooth the accelerometer data. To do this, one solution is to take an average over the last four measurements, for instance like this: