Lab 6 - gracie-miller/BAE305-S19 GitHub Wiki
Lab 6 - Simple Robot Sensing and Movement
By: Gracie Miller and Rachel Rohrer
Summary
The overall purpose of this lab is to gain experience programming a car powered by Arduino to respond to specific stimuli. In order to get familiar with Arduino and the car, a series of tasks in which the car was programmed to drive in various ways. These tasks involved programming the car to travel in a straight line, driving in a square, driving in reverse, and then driving in a straight line until the car reached black tape, at which it would stop.
Materials
- 2 Motors
- 2 Wheels
- 2 Line Followers
- Top and Bottom Chassis
- 4 AA Batteries in a battery-holder
- Power Supply Stick
- Half Bread Board
- TB6612 H-bridge
- Arduino Board
- Lots of wires
Assembly Procedures
Assemble the car as specified in Lab 02.
After assembling the car, write code which prints an "X" next to the voltage if the sensor is over tape and displays an "O" if it is not over tape. The code for this task can be seen below:
float threshold = 3.0;
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");
if (sensorValueP > threshold) {
Serial.print ("X\t");
} else {
Serial.print ("O\t");
}
Serial.print (sensorValueD);
Serial.println ("V");
if (sensorValueD > threshold) {
Serial.print ("X\t");
} else {
Serial.print ("O\t");
}
delay(10);
}
Once this task has been completed, write code for the car to travel in a straight line. The code for this function 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;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite (STBY, HIGH);
digitalWrite (AIN1, LOW);
digitalWrite (AIN2, HIGH);
digitalWrite (BIN1, LOW);
digitalWrite (BIN2, HIGH);
analogWrite (PMWB, 175);
analogWrite (PMWA, 175);
}
After the car successfully travels in a straight line, program the car to drive in a square. The code for this function 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;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite (STBY, HIGH);
digitalWrite (AIN1, LOW);
digitalWrite (AIN2, HIGH);
digitalWrite (BIN1, LOW);
digitalWrite (BIN2, HIGH);
analogWrite (PMWB, 179); // car goes straight for 1 second.
analogWrite (PMWA, 175);
delay (1000);
analogWrite (PMWB, 175); // car turns 90 degrees during this time.
analogWrite (PMWA, 0);
delay (850);
}
After the car successfully drives in a square, write code so that the car drives approximately 10 feet in a straight line going forward and then 10 feet in a straight line going in the reverse direction. The code for this function 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;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite (STBY, HIGH);
digitalWrite (AIN1, LOW);
digitalWrite (AIN2, HIGH);
digitalWrite (BIN1, LOW);
digitalWrite (BIN2, HIGH);
analogWrite (PMWB, 179); // car goes straight for approximately 10 feet.
analogWrite (PMWA, 175);
delay (12000);
analogWrite (PMWB, 0); // car stops for 1 second;
analogWrite (PMWA, 0);
delay (1000);
digitalWrite (STBY, HIGH);
digitalWrite (AIN1, HIGH);
digitalWrite (AIN2, LOW);
digitalWrite (BIN1, HIGH);
digitalWrite (BIN2, LOW);
analogWrite (PMWB, 179); // car goes straight for approximately 10 feet in the reverse direction.
analogWrite (PMWA, 175);
delay (12000);
}
After the previous task has been successfully performed, write code so that the car drives for at least 5 feet, before reaching the black tape at which point the car will stop. The code for such a function 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.5;
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.println (sensorValueP);
digitalWrite (STBY, HIGH);
digitalWrite (AIN1, LOW);
digitalWrite (AIN2, HIGH);
digitalWrite (BIN1, LOW);
digitalWrite (BIN2, HIGH);
if (sensorValueP > threshold && sensorValueD > threshold) {
analogWrite (PMWB, 0);
analogWrite (PMWA, 0);
} else {
analogWrite (PMWB, 179);
analogWrite (PMWA, 175);
}
}
Test Equipment
- Stopwatch
- Black tape
- USB
- Laptop
Test Procedures
Before driving the car, tests should be performed to ensure that both the line sensors work correctly and respond to light. In order to do this, measure and record the voltage signal produced by the line sensors in response to ambient lighting and being covered. After these values have been measured and recorded, measure and record the voltage signal produced when each of the sensors is over black tape and over white tile. The code to record the voltage signal can be seen below:
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");
delay(10);
}
Test Results
Thresholds
The thresholds we found for the light sensors were readings of around 3.4 for when the sensors were over the tile and around 3.6 for when the sensors were over tape.
Travel Times
The time for the robot to travel 10 feet at a low voltage was 18 seconds.
The time for the robot took to travel the 10 feet at a medium voltage was 12 seconds. This was the voltage we used during the robot's tasks.
The time the robot took at a high voltage was 9 seconds.
Discussion
All of the tasks we programmed the robot to do required trial and error and continual calibration. Our two motors moved at slightly different speeds, so we had to route a different voltage to each in order for the robot to move in a straight line. This was especially prevalent during the Move Forward 10 Feet, Then Reverse 10 Feet. The motors behaved differently from one another going forward, and varied even more going in reverse. Our car often veered off when going backwards.
For the Travel in a Square Task, our challenge was to determine an appropriate delay for the portion of the loop where voltage is only going to one motor. Too long and the robot would turn too far. Too short and the robot would not turn far enough.
The Stop at the Tape task was a challenge in find the right threshold.
Given more time, we would have continued to calibrate our code. Not only did we have to account for motor variance, but the robot behaved differently as the battery began to die. With more time, we could have run more tests and found the voltages and delay times that were just right.