Using the sensors
Now we will use the proximity sensors to prevent the robot from falling off the table. There are nine proximity sensors on each Thymio II: 5 at the front, 2 underneath and 2 at the back. The ones underneath can be used to stop at the edge of the table.
Proximity sensors
A proximity sensor can measure the distance to nearby objects. To do this two different components are used: an infrared emitter and a receiver. The emitter radiates infrared light (invisible to us) and the receiver measures how much of this light is reflected and comes back. If an object is close, a large part of the light will be reflected from it and returns to the robot. If the object is farther away, only a small part of the light comes back. In this way it is possible to measure the distance to objects.
In our case, the bottom sensors will be used to detect the table. Put the robot on the table and observe the value of prox.ground.delta (click refresh) shown in Aseba Studio (Memory, variables).
Now do it again while holding the robot in the air. You can see the value changes a lot.
When the robot is on the table, the values are about 600 (a lot of light comes back). When it is lifted up, the values are much smaller (not much light). So we will add at the bottom of our code this condition :
If the value of one of the two sensors is too low (no table detected), we stop the motors. Now you can see that the robot stops at the edge of the table.
The debugger
It may be that while testing the robot, we want to see in more details what is happening. This can help us to correct errors in our code or to run the instructions step by step. In our case we shall look what happens when the robot reaches the edge of the table.
In Aseba Studio, if we do a right-click on the line
call leds.top(32, 0, 0)
we can select Set breakpoint
The breakpoint is shown by a red square at the left.
This means that when we arrive at this point, the robot will wait until we tell it to continue.
Let's try it. We first have to remove the tick in the auto box if we want to keep the values at the breakpoint. Once the breakpoint is set, we can make the robot move to the edge of the table. It stops; we can observe the situation at this moment, for example we see here that the bottom sensors measured the values 151 and 45 (not much light, so the table is not being detected).
As soon as we click again on Run the robot continues (it lights up red) until the next breakpoint. We can remove the breakpoint by a right-click on the breakpointed line and then selecting Clear breakpoint.