Video 1: Our Redbot following a black line to the finish line.
The goal of our Redbot was for it to be able to track a line using its IR (light) sensors and avoid obstacles using its bumper sensors to successfully lead a person out of a fallen building. To do this, we needed to code our Redbot. Specifically, if the sensors detect black (or dark) in front of them, have both motors go 100. If the sensors detect black on the left, have right motor go 100 for 3 seconds. And so forth. As shown in Video 1, our Redbot was able to follow a line with close to no issues (aside from it getting stuck in the paper, which was a flaw in the track). However, we didn't have time to add the IR sensors to the Redbot, which made it so it couldn't manuever around obstacles. If we would've put an obstacle on the course, it wouldn't have known what to do with it.
Because of our code, the Redbot was able to easily follow the instructions and follow the black line.
#include <RedBot.h>
RedBotSensor left = RedBotSensor(A3); // initialize a left sensor object on A3
RedBotSensor center = RedBotSensor(A6); // initialize a center sensor object on A6
RedBotSensor right = RedBotSensor(A7); // initialize a right sensor object on A7
// constants that are used in the code. LINETHRESHOLD is the level to detect
// if the sensor is on the line or not. If the sensor value is greater than this
// the sensor is above a DARK line.
//
// SPEED sets the nominal speed
#define LINETHRESHOLD 800
#define SPEED 60 // sets the nominal speed. Set to any number from 0 - 255.
RedBotMotors motors;
int leftSpeed; // variable used to store the leftMotor speed
int rightSpeed; // variable used to store the rightMotor speed
void setup()
{
Serial.begin(9600);
Serial.println("Welcome to experiment 6.2 - Line Following");
Serial.println("------------------------------------------");
delay(2000);
Serial.println("IR Sensor Readings: ");
delay(500);
}
void loop()
{
Serial.print(left.read());
Serial.print("\t"); // tab character
Serial.print(center.read());
Serial.print("\t"); // tab character
Serial.print(right.read());
Serial.println();
// if on the line drive left and right at the same speed (left is CCW / right is CW)
if(center.read() > LINETHRESHOLD)
{
leftSpeed = -SPEED;
rightSpeed = SPEED;
}
// if the line is under the right sensor, adjust relative speeds to turn to the right
else if(right.read() > LINETHRESHOLD)
{
leftSpeed = -(SPEED + 50);
rightSpeed = SPEED - 50;
}
// if the line is under the left sensor, adjust relative speeds to turn to the left
else if(left.read() > LINETHRESHOLD)
{
leftSpeed = -(SPEED - 50);
rightSpeed = SPEED + 50;
}
// if all sensors are on black or up in the air, stop the motors.
// otherwise, run motors given the control speeds above.
if((left.read() > LINETHRESHOLD) && (center.read() > LINETHRESHOLD) && (right.read() > LINETHRESHOLD) )
{
motors.stop();
}
else
{
motors.leftMotor(leftSpeed);
motors.rightMotor(rightSpeed);
}
delay(0); // add a delay to decrease sensitivity.
}
Fig. 1: Our pseudo code for the Redbot.
Pseudo code is an essential component of coding. As a simpler but still professional version of true computer code, it's extremely easy to write for beginners. For our Redbot, we had to come up with as many possible situations as possible and come up with solutions for them. For example, what would happen if our Redbot didn't sense any black? What speed is best for turning? We answered all these questions on a whiteboard as depicted in Figure 1 so they could be translated into the code above.