Soccer Robot C - Basic lessons - Exercises - How to be sure?
Sensors are not perfect. Reflectance sensor is fairy exact, but distance sensors may be fooled, for example by light reflected from the surface. How to alleviate this problem?
Task: make sensor's reading You can rely on.
Starting with a program that stops in the front of an obstacle, improve it, so that it (almost) never stops by mistake when there is no obstacle. Here is the program You have to improve:
void RobotSoccer::loop() {
if (setup())
go(50, 0);
if (front() < 60)
end();
}
Solution
void RobotSoccer::loop() {
if (setup())
go(50, 0);
if (front() < 60){
stop();
delayMs(100);
if (front() < 60)
end();
else
go(50, 0);
}
}
Look at the "if" after the empty line. This line is the same as before. The same applies for the next one, where robot stops. However, now we pause the program for 100 ms, to give it time to read the next lidar's measurement. A new "if" follows, that checks again if an obstacle is present. If this second reading produces the same result, we will be confident that the obstacle is here and we will end the program with end(). Otherwise ("else"), we restart the motors.