Line Robot - Basic lessons - Exercises - Avoid exit
One of the many exercises which will help You to program RCJ Rescue Line.
Task: avoid exiting evacuation zone.
Follow right wall in RCJ Rescue Line evacuation zone, but do not exit it. Base Your code on a combined wall-following example (compiled from 2 lessons):
void RobotLine::loop() {
if (frontLeft() < 90)
go(-50, 50);
else{
int error = (rightFront() - 100) * 0.5;
error = constrain(error, -50, 50);
go(50 + error, 50 - error);
}
}
Solution
void RobotLine::loop() {
if (frontLeft() < 90)
go(-50, 50);
else{
if (rightFront() > 400)
go(50, 50);
else{
int error = (rightFront() - 100) * 0.5;
error = constrain(error, -50, 50);
go(50 + error, 50 - error);
}
}
}
- First "if" avoids a front wall.
- If there is no wall ahead, a new "if" starts and checks if there is a big space to the right. If so, we conclude that this is an exit, which we mustn't use. Therefore, go straight ahead.
- If there is no exit, we will smoothly follow the wall.
Note that the wall following should be really smooth and that Your lidar shouldn't point too much forward. If any of these 2 assumptions is not fulfilled, the robot will measure more than 400 mm while following a wall and will continue going away from it.
Do not expect a perfect result from this program. The robot will continue going straight ahead. If the start position is not well aligned with the wall, the robot may try to go out. To solve that problem, you can use compass to make sure that the robot continues going parallel to the previous wall.