Line Robot - Basic lessons - Exercises - Ring
This lesson can be a base of a simple competition - the robot which completes the task in the shortest time is the winner.
Task: touch cans.
Mark boundaries of a ring with a black stripe on white surface. The tape should form a rectangle approximately 40 cm x 40 cm, or some other geometric figure. Position 2 soft drink cans, or some other lightweight objects, 10 cm outside of the boundary. The robot must touch every object. However, it cannot leave the ring, unless it touches the object right after. When the front wheels cross the stripe, the robot has left the ring.
Solution
void RobotLine::loop() {
if (setup())
go(50, 50);
if (line(4) && frontLeft() > 100){
go(-127, 127);
delayMs(500);
go(50, 50);
}
if (frontLeft() < 30){
delayMs(1000);
go(-127, 127);
delayMs(500);
go(50, 50);
delayMs(2000);
}
}
You will have to adjust the the speeds and time intervals.
- First "if" starts devices, MRMS reflectance sensors 9x, CAN, analog, I2C and MRMS LIDAR 2m VL53L0X, CAN Bus. It starts the motors as well.
- Second "if" checks if the robot is on the line (dark) and there is no can in front (distance > 100). In that case You cannot leave the ring. Robot rotates by 180° and returns into the ring.
- Third "if" checks if the can is right in front. As the sensor cannot read too small distances, just go ahead for 1 sec (1000 ms) and then do the maneuver as above (go back). There is just additional delay of 2 sec. (2000 ms) in the end. Why? Because the robot must go cross the stripe without rotating when it finds it. We use this crude solution: simply do not check anything during the 2 sec., interval in which the reflectance sensor should be in the ring already.
Note that "delayMs()" always determine how long the robot will be moving, not staying in place.