Line Robot - Basic lessons - Exercises - How to be sure?
Sensors are not perfect. In the previous program it can happen that the robot stops, even if there is no obstacle. 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 on line, improve it, so that it never stops by mistake when there is no obstacle. Here is the previous program:
void RobotLine::loop() {
if (line(5))
go(10, 80);
else if (line(3))
go(80, 10);
else
go(60, 60);
if (frontLeft() < 60)
end();
}
Solution
void RobotLine::loop() {
if (line(5))
go(10, 80);
else if (line(3))
go(80, 10);
else
go(60, 60);
if (frontLeft() < 60){
stop();
delayMs(100);
if (frontLeft() < 60)
end();
else
go(60, 60);
}
}
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.