Lab 7 - gracie-miller/BAE305-S19 GitHub Wiki
Lab 7 - Line Follower
By: Gracie Miller and Rachel Rohrer
Summary
The goal of this lab was to apply the programming skills gained during Lab 6 and make the robot complete a course. The course was U shaped and marked by black tape on the ground. Each tip of the U shape had a horizontal line of tape. The robot was to turn around when it reached the horizontal lines, and its task was to follow along the tape twice both ways.
Challenges during this lab included finding the correct threshold to set the light sensors to, determining the most appropriate amount of time to set the wheels to turn so that they would not veer off course during a turn, and deciding how to write the code.
Materials
- The robot
- Arduino Board
Assembly Procedures
Assemble the car as specified in Lab 02. Next write the code needed for the car to follow the course. This can be seen below:
const int PMWA = 5;
const int AIN1 = 2;
const int AIN2 = 4;
const int STBY = 3;
const int PMWB = 6;
const int BIN2 = 8;
const int BIN1 = 7;
float threshold = 3.3;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
float sensorValueP = analogRead(A5) * (5.0 / 1024.0);
float sensorValueD = analogRead (A4) * (5.0 / 1024.0);
Serial.print (sensorValueP);
Serial.print ("V\t");
Serial.print (sensorValueD);
Serial.println ("V");
if (sensorValueP < threshold && sensorValueD < threshold) {
digitalWrite (STBY, HIGH);
digitalWrite (AIN1, LOW);
digitalWrite (AIN2, HIGH);
digitalWrite (BIN1, LOW);
digitalWrite (BIN2, HIGH);
analogWrite (PMWB , 154);
analogWrite (PMWA, 150);
} else if (sensorValueP > threshold && sensorValueD < threshold) {
digitalWrite (STBY, HIGH);
digitalWrite (AIN1, LOW);
digitalWrite (AIN2, HIGH);
digitalWrite (BIN1, LOW);
digitalWrite (BIN2, HIGH);
analogWrite (PMWA, 120);
analogWrite (PMWB, 200);
} else if (sensorValueD > threshold && sensorValueP < threshold) {
digitalWrite (STBY, HIGH);
digitalWrite (AIN1, LOW);
digitalWrite (AIN2, HIGH);
digitalWrite (BIN1, LOW);
digitalWrite (BIN2, HIGH);
analogWrite (PMWA, 200);
analogWrite (PMWB, 120);
} else{
digitalWrite (STBY, HIGH);
digitalWrite (AIN1, LOW);
digitalWrite (AIN2, HIGH);
digitalWrite (BIN1, HIGH);
digitalWrite (BIN2, LOW);
analogWrite (PMWB, 154);
analogWrite (PMWA, 150);
}
}
Test Equipment
- Arduino IDE
- USB cord
Test Procedures
In order to ensure that the car followed the line, the threshold to define the difference between tile and tape needed to be determined. This was done experimentally both by simply holding the car so that its line sensors were either over tape or tile and also in practice by letting the car attempt the course.
Test Results
Unfortunately, the robot never did make the journey along the tape. The robot seemed to do fine along the straight-aways, but was unsuccessful in making the turn.
Discussion
The code functions as a series of else if
statements. Before void setup
, all of the involved physical pins are established in the program. The voltage threshold that is the point the sensor defines changing from tile to tape and back is also established.
In the void loop
section of the code, serial.print
functions display the voltages from the light sensors so that what the car is "seeing" is observable and the threshold can be adjusted if necessary. The initial if
statement is for if both of the light sensors are over the tile. In this case, the car drives straight forward. The else if
statements defined that if only one light sensor is over the tile, the motor on that side spins faster to put the car back on track. The final else
defines that if both light sensors are over tape, the car spins 180 degrees and continues back along the track again.
A large portion of the lab was spent writing the code. An else if
loop was used. Initially, our code had the final else
statement define when the car was along a straight-away. We switched to the code seen above because we were having many difficulties and decided that changing the code may help.
Ultimately the car was not able to correctly follow the course outlined in tape. However, we believe this was in part due to the front end of the front not being close enough to the ground among other things. We attempted to weigh the front of the car down but the dead weight was not stable and its movement on top of the car affected the car's course. Due to the time allotment of lab, we were not able to fully calibrate the car and fix all of the issues we encountered. In addition to this, towards the end of lab, the batteries to the car started to die, and this caused a slight difference in the voltage readings that were being received and overall affected our voltage threshold.