This assignment was to teach us about the Parallax Ultrasonic Sensor and how to use it to navigate. We also used a positional servo motor to spin the ultrasonic sensor so the robot is able to sweep its surroundings for objects and then move in a clear path. I was not able to make a video of it moving because of some external factors but the robot does do all of the necessary movements and controls.
This assignment took me about 4 hours to complete. I unplugged the line followers so I didn't use too much power from them and so the wiring wasn't so cluttered. The ultrasonic sensor and additional servo were easily installed because they were exactly the same as the other servos. I only had to use four PWM slots for all of the devices so that also freed up my wires.
It was easier to program and troubleshoot by taking a couple of lines of code at a time. This let me isolate my problems and fine-tune my turns and sensing. The Serial.println also helped with troubleshooting so I knew what the robot was seeing. I was able to use some previous functions in this program but I had to come up with new code for the loop. This program showed me how important indenting and whitespace while programming is. (The indenting never looks the same on my blog.) This is the code that I came up with after it was all working.
/*
Brian Swanson, Assignment 3
Using the Ultrasonic sensor to move about a room
Using two continuous servos and one positional
Using one ultrasonic sensor
*/
#include <SoftwareSerial.h> //Including the necessary libraries
#include <Servo.h>
Servo rightServo; //Defining the servos
Servo leftServo;
Servo pingServ;
long pingTime = 0; //setting constants and variables
const int Ping = 6;
int pos = 180;
void setup()
{
Serial.begin(115200);
rightServo.attach(12); //Sets the right servo to I/O 12
leftServo.attach(13); //Sets left servo to I/O 13
pingServ.attach(5); //Setsthe ping servo to I/O 5
}
void straight() //Function for robot to go straight
{
leftServo.write(pos);
rightServo.write(-pos);
}
void leftTurn() //Function for robot to go left
{
leftServo.write(-pos);
rightServo.write(-pos);
delay(250);
}
void rightTurn() //Function for robot to go right
{
leftServo.write(pos);
rightServo.write(pos);
delay(250);
}
void halt() //Function for robot to stop
{
leftServo.write(90);
rightServo.write(90);
delay(250);
}
void turnAround() //Function to turn around
{
leftServo.write(pos);
rightServo.write(pos);
delay(450);
}
void sensStraight() //Function to turn the ultrasonic straight and then read a distance
{
pingServ.write(83);
delay(500);
sensDist();
}
void sensLeft() //Function to turn the ultrasonic left and then read a distance
{
pingServ.write(165);
delay(500);
sensDist();
}
void sensRight() //Function to turn the ultrasonic right and then read a distance
{
pingServ.write(0);
delay(500);
sensDist();
}
void sensDist() //Function to read a distance from the ultrasonic
{
pinMode(Ping, OUTPUT);
digitalWrite(Ping, LOW);
delayMicroseconds(2);
digitalWrite(Ping, HIGH);
delayMicroseconds(5);
digitalWrite(Ping, LOW);
delayMicroseconds(2);
pinMode(Ping, INPUT);
pingTime = pulseIn(Ping, HIGH, 2800);
if (pingTime == 0)
{
Serial.println("Nothing");
}
else
{
pingTime = pingTime/2/74;
Serial.println(pingTime);
}
delay(10);
}
void loop ()
{
sensStraight();
if(pingTime > 7 || pingTime == 0) //looks straight until object is 7 or less inches away
{
sensStraight();
straight();
}
else //if straight is obstructed, looks left
{
halt();
sensLeft();
delay(100);
if(pingTime > 7 || pingTime == 0) //if left is clear, turn left and loop back to top
{
leftTurn();
sensStraight();
straight();
}
else //if left is obstructed, look right
{
halt();
sensRight();
delay(500);
if(pingTime > 7 || pingTime == 0) //if right is clear, turn right and loop back to top
{
rightTurn();
sensStraight();
straight();
}
else //if straight, left, and right are obstructed, turn around and loop back to top
{
turnAround();
}
}
}
}
No comments:
Post a Comment