Mission 3

Mission 3

Home Mission 1 Mission 2 Mission 3 Mission 4

Basic GPS Navigation

Criteria:

  1. Point due North. From an arbitrary start location and heading, navigate so that the rover is pointing due North. Point north within +/- 20° and complete the mission within 180 seconds.

Mission 3 Psuedo Code:

  1. Determine the latitude and longitude of the rover's starting point
  2. Drive forward a certain distance
  3. Determine the latitude and longitude of the rover's new position
  4. Calculate the heading angle
  5. Compare the rover's heading to the heading angle for due North (zero degrees)
  6. If the angle is less than 180 degrees, have the rover turn left the angle of the heading
  7. If the angle is greater than 180 degrees, have the rover turn right (360 degrees minus the angle of the heading)

Mission 3 Arduino Code

#include SoftwareSerial.h
#include TinyGPS++.h
#include AFMotor.h
#define RXPin A1
#define TXPin A0
#define GPSBaud 9600
#define ConsoleBaud 115200
// Create a TinyGPS++ object for the GPS device
TinyGPSPlus gps;
AF_DCMotor dcMotor1(1);
AF_DCMotor dcMotor2(2);
SoftwareSerial ss(RXPin, TXPin);
float roverLatitude1 = 45;
float roverLongitude1;
float roverLatitude2 = 45;
float roverLongitude2;
unsigned long timestamp = 0;
void setup() {
Serial.begin(ConsoleBaud);
ss.begin(GPSBaud);
dcMotor1.setSpeed(255);
dcMotor2.setSpeed(255);
}
void loop() {
while(roverLatitude1 > 40){
// Check if there is new data available from the GPS device
while (ss.available() > 0) {
// Decode the new data
gps.encode(ss.read());
}
// Check if the GPS device has a valid fix on the location
if (gps.location.isValid()) {
roverLatitude1 = gps.location.lat();
roverLongitude1 = gps.location.lng();
Serial.print("Latitude1: ");
Serial.print(roverLatitude1, 6);
Serial.print(", ");
Serial.print("Longitude1: ");
Serial.print(roverLongitude1, 6);
Serial.println(" ");
//go straight 6'
dcMotor1.run(FORWARD);
dcMotor2.run(FORWARD);
delay(10000);
dcMotor1.run(RELEASE);
dcMotor2.run(RELEASE);
}
}
while(roverLatitude2 > 40){
// Check if there is new data available from the GPS device
while (ss.available() > 0) {
// Decode the new data
gps.encode(ss.read());
}
// Check if the GPS device has a new fix on the location
if (gps.location.isUpdated()) {
roverLatitude2 = gps.location.lat();
roverLongitude2 = gps.location.lng();
Serial.print("Latitude2: ");
Serial.print(roverLatitude2, 6);
Serial.print(", ");
Serial.print("Longitude2: ");
Serial.print(roverLongitude2, 6);
Serial.println(" ");
}
}
float roverHeading = TinyGPSPlus::courseTo(
roverLatitude1, roverLongitude1, roverLatitude2, roverLongitude2);
Serial.print("Heading: ");
Serial.print(roverHeading);
//compute the duration of the turn if the heading is greater than 180 degrees
if (roverHeading <= 180) {
float turnTime = roverHeading * 8.2222;
//turn left
dcMotor1.run(FORWARD);
dcMotor2.run(BACKWARD);
delay(turnTime);
dcMotor1.run(RELEASE);
dcMotor2.run(RELEASE);
}
else { //compute the duration of the turn if the heading is greater than 180 degrees
float turnTime = (2960 - (roverHeading * 8.2222));
//turn right
dcMotor1.run(BACKWARD);
dcMotor2.run(FORWARD);
delay(turnTime);
dcMotor1.run(RELEASE);
dcMotor2.run(RELEASE);
}
delay(100000000);
}


Mission 3 Results: