Heather Vasapollo, Nick Deane, and Sourav Paul
Gonna have a heckin' good time.
Robot Code:
Edge Detection
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);
// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
Adafruit_DCMotor *myMotor2 = AFMS.getMotor(2);
// You can also make another motor on port M2
//Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);
//Declaration of sensors and their respective values
int sensor1 = A0;
int val1;
int sensor2 = A2;
int val2;
int sensor3 = A1;
int val3;
void setup() {
AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz
// Set the speed to start, from 0 (off) to 255 (max speed)
myMotor->setSpeed(80);
myMotor->run(FORWARD);
myMotor->run(RELEASE);
myMotor2->setSpeed(80);
myMotor2->run(FORWARD);
myMotor2->run(RELEASE);
}
void loop() {
//Reads the output from each sensor
val1 = analogRead(sensor1);
val2 = analogRead(sensor2);
val3 = analogRead(sensor3);
//serial.printIn(val1);
/*The logic behind this program is similar to the line follow code.
When the sensors detect the table, the robot simply moves forward,
but when the sensors detect the black line, the robot immediately
reverses and turns to change its course. The sensors read a value
of 200-300 for the table and around 950 for the black line. The
logic is simple, in a single if statement the robot determines
if each sensor is reading the table or a black line. If it reads
the line it turns around, else it just moves forward.*/
if (val1 > 500 || val2 > 500 || val3 > 500)
{
myMotor->run(BACKWARD);
myMotor->setSpeed(80);
myMotor2->run(BACKWARD);
myMotor2->setSpeed(80);
delay(2000); //2 second delay backup
myMotor->run(BACKWARD);
myMotor->setSpeed(80);
myMotor2->run(FORWARD);
myMotor2->setSpeed(80);
delay(2000); //2 second delay turn
}
else
{
myMotor->setSpeed(80);
myMotor->run(FORWARD);
myMotor2->setSpeed(80);
myMotor2->run(FORWARD);
}
}
Wall Detection
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);
// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
Adafruit_DCMotor *myMotor2 = AFMS.getMotor(2);
// You can also make another motor on port M2
//Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);
//Both buttons are declared and assigned boolean values.
int bumper1 = 10;
boolean bumperstate1 = 0;
int bumper2 = 6;
boolean bumperstate2 = 0;
void setup() {
Serial.begin(115200);
pinMode(bumper1, INPUT_PULLUP); //Sends input from buttons
pinMode(bumper2, INPUT_PULLUP);
AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz
}
void loop() {
//Reads the button value to the arduino
bumperstate1 = digitalRead(bumper1);
bumperstate2 = digitalRead(bumper2);
//Robot moves forward at speed 80 by default
myMotor->setSpeed(80);
myMotor->run(FORWARD);
myMotor2->setSpeed(80);
myMotor2->run(FORWARD);
//Front Contact: bumpers read 0
if (bumperstate1 < 1) //When bumper 1 hits wall
{
myMotor->run(BACKWARD);
myMotor->setSpeed(80);
myMotor2->run(BACKWARD);
myMotor2->setSpeed(80);
delay(2000); //Move backwards for 2 seconds
myMotor->run(BACKWARD);
myMotor->setSpeed(80);
myMotor2->run(FORWARD);
myMotor2->setSpeed(80);
delay(2000); //Turn for 2 seconds
}
if (bumperstate2 < 1) //When bumper 2 hits wall
{
myMotor->run(BACKWARD);
myMotor->setSpeed(80);
myMotor2->run(BACKWARD);
myMotor2->setSpeed(80);
delay(2000); //Move backwards for 2 seconds
myMotor->run(BACKWARD);
myMotor->setSpeed(80);
myMotor2->run(FORWARD);
myMotor2->setSpeed(80);
delay(2000); //Turn for 2 seconds
}
}
Line Follow
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);
// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
Adafruit_DCMotor *myMotor2 = AFMS.getMotor(2);
// You can also make another motor on port M2
//Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);
//Declaration of sensors, their output values, and their locations
int sensor1 = A0;
int val1;
int sensor2 = A2;
int val2;
int sensor3 = A1;
int val3;
void setup() {
AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz
// Set the speed to start, from 0 (off) to 255 (max speed)
myMotor->setSpeed(90);
myMotor->run(FORWARD);
myMotor->run(RELEASE);
myMotor2->setSpeed(90);
myMotor2->run(FORWARD);
myMotor2->run(RELEASE);
}
void loop() {
//Assigns the value of each sensor output to their respective vals
//Each sensor reads a number between 200 and 1000 for the table and black line
val1 = analogRead(sensor1);
val2 = analogRead(sensor2);
val3 = analogRead(sensor3);
/*Each sensor value uses a boolean operator in each nested if statement.
In this case each sensor value is being compared against 500. This is
because when the sensor is on the line it reads around 950 and when it's
on the table it reads around 200-300. As the sensor moves off the line,
for a brief moment it will read the black line and the table at the same
time, creating output values between 300 and 900. To account for this
we compared our sensor vals to 500 to prevent the logic from stopping
the robot in it's traks. */
//Go Straight
if (val1 < 500 && val2 < 500 && val3 > 500)
{
myMotor->run(FORWARD);
myMotor->setSpeed(90);
myMotor2->run(FORWARD);
myMotor2->setSpeed(90);
delay(5);
//myMotor->run(RELEASE);
//myMotor2->run(RELEASE);
}
//Right Turn
if (val1 < 500 && val2 > 500 && val3 < 500)
{
myMotor->run(BACKWARD);
myMotor->setSpeed(90);
myMotor2->run(FORWARD);
myMotor2->setSpeed(90);
delay(5);
//myMotor->run(RELEASE);
//myMotor2->run(RELEASE);
}
//Left Turn
if (val1 > 500 && val2 < 500 && val3 < 500)
{
myMotor->run(FORWARD);
myMotor->setSpeed(90);
myMotor2->run(BACKWARD);
myMotor2->setSpeed(90);
delay(5);
//myMotor->run(RELEASE);
//myMotor2->run(RELEASE);
}
/*The reasoning behind the logic is to program the robot to turn depending
on which sensor is off the black line. This causes the wheel opposite to the
sensor to turn backwards, turning the robot, while the remaining wheel continues
forwards.*/
}
Solid Model of Bot:
Exploded View of Bot:
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.