Line following
This took about 3-4 hours to complete and most of it was programming. The wiring was easy except for having to put power to white and information to red on the line followers. The wiring for the first program was simple because there weren't many wires but I can see where wire management could play a role in this one (I probably could have done better with mine).
I kept the two servos hooked up because we needed these to make the robot move. I used an additional five PWM's for the line followers and one for the switch. I also had to use a 10kohm resistor for the switch.
The programming was again the hardest part. It took a while to learn how to use the RCTime function and how it works. After looking on the arduino website and having someone help me with it, I was able to understand how it all works and why it works. The logic was fairly easy because we did a very similar program in another robotics course. Being able to keep some of the same functions from the first program saved a little time. The Serial Monitor was helpful for debugging the program because I was able to look at exactly what each line follower was reading. This is what I came up with.
#include <Servo.h> //adds servo library
Servo rightServo;
Servo leftServo;
Servo leftServo;
int pos = 180; //Sets position to 180. Used to move motor forward or backwards
int color =15;
int leftMost = 11; //Sets variable corresponding to PWM slot
int rightMost = 10;
int innerRight = 9;
int innerLeft = 8;
int buttonSlot = 7;
int color =15;
int leftMost = 11; //Sets variable corresponding to PWM slot
int rightMost = 10;
int innerRight = 9;
int innerLeft = 8;
int buttonSlot = 7;
int buttonValue = 0;
void straight() //Function for robot to go straight
{
leftServo.write(pos);
rightServo.write(-pos);
}
{
leftServo.write(pos);
rightServo.write(-pos);
}
void leftTurn() //Function for robot to go left
{
leftServo.write(-pos);
rightServo.write(-pos);
}
{
leftServo.write(-pos);
rightServo.write(-pos);
}
void rightTurn() //Function for robot to go right
{
leftServo.write(pos);
rightServo.write(pos);
}
{
leftServo.write(pos);
rightServo.write(pos);
}
void halt() //Function to stop robot
{
rightServo.write(90);
leftServo.write(90);
}
{
rightServo.write(90);
leftServo.write(90);
}
void setup()
{
Serial.begin(115200);
rightServo.attach(12); //Sets the right servo to I/O 12
leftServo.attach(13); //Sets the left servo to I/O 13
}
{
Serial.begin(115200);
rightServo.attach(12); //Sets the right servo to I/O 12
leftServo.attach(13); //Sets the left servo to I/O 13
}
long RCTime (int sensIN) //This is used to read the line followers. Similar to charging and discharging a capacitor
{
long result = 0;
pinMode(sensIN, OUTPUT);
digitalWrite(sensIN, HIGH);
delay(1);
pinMode (sensIN, INPUT);
digitalWrite(sensIN, LOW);
while (digitalRead(sensIN))
{
result++;
}
return result;
}
{
long result = 0;
pinMode(sensIN, OUTPUT);
digitalWrite(sensIN, HIGH);
delay(1);
pinMode (sensIN, INPUT);
digitalWrite(sensIN, LOW);
while (digitalRead(sensIN))
{
result++;
}
return result;
}
void loop()
{
halt();
buttonValue = digitalRead(buttonSlot); //Reads the value of the button
while (buttonValue == 1) //Doesn't start until the button is on
{
delay(1000);
while(true) //Loops forever
{
Serial.print("leftMost"); //Prints the values of all four line followers for easy troubleshooting
Serial.println(RCTime(leftMost));
Serial.print("rightMost");
Serial.println(RCTime(rightMost));
Serial.print("innerLeft");
Serial.println(RCTime(innerLeft));
Serial.print("innerRight");
Serial.println(RCTime(innerRight));
Serial.println();
delay(10);
if(RCTime(innerRight) > color || RCTime(innerLeft) > color && (RCTime(leftMost) < color && RCTime(rightMost) < color)) //if the robot is moving anong like it should
{
straight();
}
if((RCTime(innerLeft) < color && RCTime(innerRight) < color) && (RCTime(leftMost) > color)) //if the robot goes too far right
{
leftTurn();
}
if((RCTime(innerLeft) < color && RCTime(innerRight) < color) && (RCTime(rightMost) > color)) //if the robot goes too far left
{
rightTurn();
}
}
}
}
{
halt();
buttonValue = digitalRead(buttonSlot); //Reads the value of the button
while (buttonValue == 1) //Doesn't start until the button is on
{
delay(1000);
while(true) //Loops forever
{
Serial.print("leftMost"); //Prints the values of all four line followers for easy troubleshooting
Serial.println(RCTime(leftMost));
Serial.print("rightMost");
Serial.println(RCTime(rightMost));
Serial.print("innerLeft");
Serial.println(RCTime(innerLeft));
Serial.print("innerRight");
Serial.println(RCTime(innerRight));
Serial.println();
delay(10);
if(RCTime(innerRight) > color || RCTime(innerLeft) > color && (RCTime(leftMost) < color && RCTime(rightMost) < color)) //if the robot is moving anong like it should
{
straight();
}
if((RCTime(innerLeft) < color && RCTime(innerRight) < color) && (RCTime(leftMost) > color)) //if the robot goes too far right
{
leftTurn();
}
if((RCTime(innerLeft) < color && RCTime(innerRight) < color) && (RCTime(rightMost) > color)) //if the robot goes too far left
{
rightTurn();
}
}
}
}
No comments:
Post a Comment