Mission 2

Mission 2

Home Mission 1 Mission 2 Mission 3 Mission 4

Obstacle Avoidance Mission

Criteria:

  1. Must Drive in an enclosed area (at least 10’x10’) without contacting walls or obstacles. The mission will last 60 seconds.
  2. Must Drive forward to waypoint 30’ directly ahead of robot. Demonstrate avoidance algorithm by continuing to original waypoint after navigating around a single obstacle. The rover should stop within 5ft of the marked waypoint and complete the mission in under 120 seconds

Mission 2A Psuedo Code:

  1. Drive Forward
  2. Have sensor scan for objects within 1 ft of the rover
  3. If either sensor detects an object, turn left
  4. If there is no object, drive forward

Mission 2A Arduino Code

#include AFMotor.h
#include Arduino.h
// Define Trig and Echo pin:
#define trigPin1 A5 //green
#define echoPin1 A4 //blue
#define trigPin2 A2
#define echoPin2 A1
// Define variables:
long duration1;
long duration2;
long distanceIN1;
long distanceIN2;
// This defines the name of the motor (dcMotor1) and port number (1).
AF_DCMotor dcMotor1(1);
AF_DCMotor dcMotor2(2);
void setup() {
// Define inputs and outputs:
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
// Set the speed for the selected DC motor. Values can range between 0 and 255.
dcMotor1.setSpeed(200);
dcMotor2.setSpeed(200);
//Begin Serial communication at a baudrate of 9600:
Serial.begin(9600);
// Clear the trigPin by setting it LOW:
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
// Trigger the sensor by setting the trigPin high for 10 microseconds:
digitalWrite(trigPin1, HIGH);
delayMicroseconds(2);
digitalWrite(trigPin1, LOW);
// Read the echoPin, pulseIn() returns the duration (length of the pulse) in microseconds:
duration1 = pulseIn(echoPin1, HIGH);
// Calculate the distance:
distanceIN1 = duration1 * 0.034 / 5.08;
// Serial.print("Distance = ");
// Serial.print(distanceIN1);
// Serial.println(" in");
// Clear the trigPin by setting it LOW:
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
// Trigger the sensor by setting the trigPin high for 10 microseconds:
digitalWrite(trigPin2, HIGH);
delayMicroseconds(2);
digitalWrite(trigPin2, LOW);
// Read the echoPin, pulseIn() returns the duration (length of the pulse) in microseconds:
duration2 = pulseIn(echoPin2, HIGH);
// Calculate the distance:
distanceIN2 = duration2 * 0.034 / 5.08;
// Serial.print("Distance = ");
// Serial.print(distanceIN2);
// Serial.println(" in");
}
void loop() {
// Clear the trigPin by setting it LOW:
digitalWrite(trigPin1, LOW);
delayMicroseconds(5);
// Trigger the sensor by setting the trigPin high for 10 microseconds:
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
// Read the echoPin, pulseIn() returns the duration (length of the pulse) in microseconds:
duration1 = pulseIn(echoPin1, HIGH);
// Calculate the distance:
distanceIN1 = duration1 * 0.034 / 5.08;
Serial.print("Distance = ");
Serial.print(distanceIN1);
Serial.println(" in");
// Clear the trigPin by setting it LOW:
digitalWrite(trigPin2, LOW);
delayMicroseconds(5);
// Trigger the sensor by setting the trigPin high for 10 microseconds:
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
// Read the echoPin, pulseIn() returns the duration (length of the pulse) in microseconds:
duration2 = pulseIn(echoPin2, HIGH);
// Calculate the distance:
distanceIN2 = duration2 * 0.034 / 5.08;
Serial.print("Distance = ");
Serial.print(distanceIN2);
Serial.println(" in");
if(distanceIN1 >= 12 && distanceIN2 >= 12){ //drive forward if either sensor detects an object to be further than 12 inches
dcMotor1.run(FORWARD); //go straight
dcMotor2.run(FORWARD);
} if(distanceIN1 <= 11 || distanceIN2 <= 11){ //turn left 30 degrees if either sensor detects an object within 11 inches
dcMotor1.run(RELEASE);
dcMotor2.run(RELEASE);
delay(1000);
dcMotor1.run(FORWARD);
dcMotor2.run(BACKWARD);
delay(450);
dcMotor1.run(RELEASE);
dcMotor2.run(RELEASE);
}
}


Mission 2B Psuedo Code:

  1. Drive Forward and start a timer
  2. Have sensor scan for objects within 1 ft of the rover
  3. Once the sensor detects the object, measure the time it took
  4. When either sensor detects an object, turn left 90 degrees
  5. Travel straight 6 feet
  6. Turn right 90 degrees
  7. If there is no object, drive forward the remainder of the time to go 30 feet
  8. Stop at 30 feet

Mission 2B Arduino Code

#include AFMotor.h
#include Arduino.h
// Define Trig and Echo pin:
#define trigPin1 A5 //green
#define echoPin1 A4 //blue
// Define variables:
long duration1;
long duration2;
int distanceIN1;
int distanceIN2;
uint32_t time_start_us = 0;
uint32_t time_end_us = 0;
int timeToGo30 = 10000;
// This defines the name of the motor (dcMotor1) and port number (1).
AF_DCMotor dcMotor1(1);
AF_DCMotor dcMotor2(2);
void setup() {
// Define inputs and outputs:
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
// Set the speed for the selected DC motor. Values can range between 0 and 255.
dcMotor1.setSpeed(255);
dcMotor2.setSpeed(255);
//Begin Serial communication at a baudrate of 9600:
Serial.begin(9600);
delay(2000);
}
void loop() {
driveForwardScan();
int timeToObstacle = driveForwardScan();
delay(1000);
dcMotor1.run(FORWARD); //turn left at obstacle
dcMotor2.run(BACKWARD);
delay(925);
dcMotor1.run(RELEASE);
dcMotor2.run(RELEASE);
delay(2000);
dcMotor1.run(FORWARD); //go straight 6'
dcMotor2.run(FORWARD);
delay(5000);
dcMotor1.run(RELEASE);
dcMotor2.run(RELEASE);
delay(2000);
dcMotor1.run(BACKWARD); //turn right
dcMotor2.run(FORWARD);
delay(800);
dcMotor1.run(RELEASE);
dcMotor2.run(RELEASE);
delay(2000);
int timeToFinish = timeToGo30 - timeToObstacle;
dcMotor1.run(FORWARD); //go straight until 30'
dcMotor2.run(FORWARD);
delay(timeToFinish);
dcMotor1.run(RELEASE);
dcMotor2.run(RELEASE);
delay(2000);
dcMotor1.run(BACKWARD); //turn right
dcMotor2.run(FORWARD);
delay(800);
dcMotor1.run(RELEASE); dcMotor2.run(RELEASE);
delay(2000);
dcMotor1.run(FORWARD); //go straight 6'
dcMotor2.run(FORWARD);
delay(2000);
dcMotor1.run(RELEASE);
dcMotor2.run(RELEASE);
delay(5000000000);
}
int driveForwardScan(){
// Clear the trigPin by setting it LOW:
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
// Trigger the sensor by setting the trigPin high for 10 microseconds:
digitalWrite(trigPin1, HIGH);
delayMicroseconds(2);
digitalWrite(trigPin1, LOW);
// Read the echoPin, pulseIn() returns the duration (length of the pulse) in microseconds:
duration1 = pulseIn(echoPin1, HIGH);
// Calculate the distance:
distanceIN1 = duration1 * 0.034 / 5.08;
Serial.print("Distance1 = ");
Serial.print(distanceIN1);
Serial.println(" in");
time_start_us = millis(); //start timer
while(distanceIN1 > 8){
dcMotor1.run(FORWARD); //go straight
dcMotor2.run(FORWARD);
// Clear the trigPin by setting it LOW:
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
// Trigger the sensor by setting the trigPin high for 10 microseconds:
digitalWrite(trigPin1, HIGH);
delayMicroseconds(2);
digitalWrite(trigPin1, LOW);
// Read the echoPin, pulseIn() returns the duration (length of the pulse) in microseconds:
duration1 = pulseIn(echoPin1, HIGH);
// Calculate the distance:
distanceIN1 = duration1 * 0.034 / 5.08;
Serial.print("Distance2 = ");
Serial.print(distanceIN1);
Serial.println(" in");
}
dcMotor1.run(RELEASE);
dcMotor2.run(RELEASE);
time_end_us = millis(); //end timer
uint32_t time_elapsed_us = (time_end_us - time_start_us) ;
Serial.print(time_elapsed_us);
return time_elapsed_us;
}


Mission 2A Results:



Mission 2B Results: