Autonomousrobot
Autonomousrobot
Why: I want to learn how to use complex if then statements in C++ to tell a robot how to
avoid walls throughout a room. This is important because it could lead to other
autonomous robots such as things that fly or walk. The minimum viable product is a
robot that drives around a room and avoids obstructions such as walls, chairs, etc.
Once I have the minimum viable product completed, I will have the robot make an
educated decision on which direction to go (left or right).
Who: I am the only one necessary for this project. I will be using the Arduino open
source prototyping platform as the brain of the project. I will also be using the Adafruit
motor shield, and I need to recognize them.
What:
Robot platform
Adafruit motor shield
Ultrasonic sensor
Servo
Computer
Caster Wheel
The Robot Platform is important in this project because it will be housing all of the
electronic components needed for the project. The platform includes the motors, the
gearboxes, the tires, and the servo mount.
Adafruits motor shield is compatible with small motors that take less than 1 amp of
current. I used a 5000 mah battery with the shield and that seemed to be fine.
The Ultrasonic sensor is the robot's eyes. I used the Maxbotix EZ0 sensor because of
its large bandwidth. The Ultrasonic sensor tell us how far away from the wall the robot
is.
The servo is important in this project because it swivels the ultrasonic sensor to check
for walls and other obstructions.
A computer is necessary for this project because that is what holds all of the code and
what compiles it to the Arduino.
The caster wheel is also important in this project because I used two wheels instead of
four on the platform. Since I used two wheels, I had to have a caster wheel in the back
to release some dragging weight.
When: This project was completed over a span of around two years. Due to power
issues and various code errors.
Where: I will complete this project in my basement, and I will test it in various rooms of
the house.
How: When the autonomous robot turns on, it centers the servo. When the robot drives
around, the ultrasonic sensor checks relative distance from a wall or obstruction. When
the robot gets within thirty inches away, the sensor checks the distance to the left. If the
distance to the nearest wall to the left is greater than 40, it goes left. If not then it goes
right.
Code(minimum viable product):
/* Louis version 1.0.1 */
#include <AFMotor.h>
#include <Servo.h>
//ultrasonic sensor variable
int Front_Sensor;
//variable for the time of the ultrasonic senor
const long interval = 100;
unsigned long previousMillis = 0;
//the servo that we are using
Servo PanServo;
AF_DCMotor LeftMotor(2);
AF_DCMotor RightMotor(3);
void setup() {
//Also begin the Serial monitor for debugging
Serial.begin(9600);
//attach the servo
PanServo.attach(9);
LeftMotor.run(RELEASE);
LeftMotor.setSpeed(255);
RightMotor.run(RELEASE);
RightMotor.setSpeed(255);
//center the servo
PanServo.write(90);
//wait three seconds
delay(3000);
gather();
}
void loop() {
//gqther the info from the sensor and parse
gather();
//start the major switch case
switch(Front_Sensor) {
//30 inches
case 30:
//rotate the servo
PanServo.write(180);
LeftMotor.run(FORWARD);
LeftMotor.setSpeed(0);
RightMotor.run(FORWARD);
RightMotor.setSpeed(0);
delay(1000);
if(Front_Sensor > 40) {
//Go left if greater than 40
LeftMotor.run(BACKWARD);
LeftMotor.setSpeed(255);
RightMotor.run(FORWARD);
RightMotor.setSpeed(255);
delay(250);
}
//if not
else {
//go right
LeftMotor.run(FORWARD);
LeftMotor.setSpeed(255);
RightMotor.run(BACKWARD);
RightMotor.setSpeed(255);
delay(250);
}
PanServo.write(90);
delay(200);
break;
//failsafe for 29 inches
case 29:
//rotate the servo
PanServo.write(180);
LeftMotor.run(FORWARD);
LeftMotor.setSpeed(0);
RightMotor.run(FORWARD);
RightMotor.setSpeed(0);
delay(1000);
if(Front_Sensor > 40) {
//Go left if greater than 40
LeftMotor.run(BACKWARD);
LeftMotor.setSpeed(255);
RightMotor.run(FORWARD);
RightMotor.setSpeed(255);
delay(250);
}
//if not
else {
//go right
LeftMotor.run(FORWARD);
LeftMotor.setSpeed(255);
RightMotor.run(BACKWARD);
RightMotor.setSpeed(255);
delay(250);
}
PanServo.write(90);
delay(200);
break;
//greator than 30
default:
LeftMotor.run(FORWARD);
LeftMotor.setSpeed(255);
RightMotor.run(FORWARD);
RightMotor.setSpeed(255);
delay(50);
gather();
}
}
//function for gathering and parsing all data
void gather() {
//timing
unsigned long currentMillis= millis();
if(currentMillis-previousMillis >= interval) {
previousMillis = currentMillis;
//gather the info from the sensor
Front_Sensor = analogRead(0);
Serial.println(Front_Sensor);
}
}
This code definitely completes the minimum viable product of avoiding walls!