GPS Module

GPS Module

Home GPS Module Ultrasonic Module RF Module

GPS Module

Criteria:

  1. Use the first sketch below to display the current latitude and longitude on user request (input through serial monitor)
  2. Use the second sketch to display the distance and course to specific GPS location on user request.
  3. Use this link to follow the full tutorial. https://makezine.com/projects/gps/
  4. These programs will allow the rover to know its own position through GPS cordinates. Since it is able to use GPS coordinates, it will be able to travel to desired positions on its own.

Required Hardware


Required Libraries

Wiring Diagram


Sketch to determine the user's latitude and longitude

#include SoftwareSerial.h
#include TinyGPS++.h
#define RXPin 2 //RXPin recieves info from TX pin
#define TXPin 3 //RXPin recieves info from TX pin
#define GPSBaud 9600
#define ConsoleBaud 115200
// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);
// The TinyGPS++ object
TinyGPSPlus gps;
void setup()
{
Serial.begin(ConsoleBaud);
ss.begin(GPSBaud);
}
void loop()
{
Serial.println("Enter any key to display your current longitude and lattitude");
while(Serial.available()==0) {}
// send them to the TinyGPS++ object
while (ss.available() > 0)
gps.encode(ss.read());
// Prints the location of TinyGPS++ with longitude and latitude coordiantes
// Prints altitude
Serial.print("Location: ");
Serial.print(gps.location.lat(), 6);
Serial.print(",");
Serial.print(gps.location.lng(), 6);
Serial.print(" Altitude: ");
Serial.println(gps.altitude.meters());
// }
}

Sketch to display distance and course to specific GPS location on user request

#include SoftwareSerial.h
#include TinyGPS++.h
#define RXPin 2
#define TXPin 3
#define GPSBaud 9600
#define ConsoleBaud 115200
// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);
// The TinyGPS++ object
TinyGPSPlus gps;
unsigned long lastUpdateTime = 0;
void setup()
{
Serial.begin(ConsoleBaud);
ss.begin(GPSBaud);
}
void loop()
{
Serial.println("Enter Latitude ");
while(Serial.available()==0) {}
float userInputLat =Serial.parseInt();
Serial.println("Enter Longitude ");
while(Serial.available()==0) {}
float userInputLong =Serial.parseInt();
// send them to the TinyGPS++ object
while (ss.available() > 0)
gps.encode(ss.read());
// Every 5 seconds, do an update.
if (millis() - lastUpdateTime >= 5000)
{ lastUpdateTime = millis();
Serial.println();
// Establish our current status double distanceToDestination = TinyGPSPlus::distanceBetween(
gps.location.lat(), gps.location.lng(),userInputLat, userInputLong);
double courseToDestination = TinyGPSPlus::courseTo(
gps.location.lat(), gps.location.lng(), userInputLat, userInputLong);
const char *directionToDestination = TinyGPSPlus::cardinal(courseToDestination);
int courseChangeNeeded = (int)(360 + courseToDestination - gps.course.deg()) % 360;
Serial.print("Location: ");
Serial.print(gps.location.lat(), 6);
Serial.print(",");
Serial.print(gps.location.lng(), 6);
// debug
Serial.print("DEBUG: Course2Dest: ");
Serial.print(courseToDestination);
Serial.print(" CurCourse: ");
Serial.print(gps.course.deg());
Serial.print(" Dir2Dest: ");
Serial.print(directionToDestination);
Serial.print(" RelCourse: ");
Serial.print(courseChangeNeeded);
Serial.print(" CurSpd: ");
Serial.println(gps.speed.kmph());
// Within 20 meters of destination? We're here!
if (distanceToDestination <= 20.0)
{
Serial.println("CONGRATULATIONS: You've arrived!");
exit(1);
}
Serial.print("DISTANCE: ");
Serial.print(distanceToDestination);
Serial.println(" meters to go.");
Serial.print("INSTRUCTION: ");
// Standing still? Just indicate which direction to go.
if (gps.speed.kmph() < 2.0)
{
Serial.print("Head ");
Serial.print(directionToDestination);
Serial.println(".");
return;
}
if (courseChangeNeeded >= 345 || courseChangeNeeded < 15)
Serial.println("Keep on straight ahead!");
else if (courseChangeNeeded >= 315 && courseChangeNeeded < 345)
Serial.println("Veer slightly to the left.");
else if (courseChangeNeeded >= 15 && courseChangeNeeded < 45)
Serial.println("Veer slightly to the right.");
else if (courseChangeNeeded >= 255 && courseChangeNeeded < 315)
Serial.println("Turn to the left.");
else if (courseChangeNeeded >= 45 && courseChangeNeeded < 105)
Serial.println("Turn to the right.");
else
Serial.println("Turn completely around.");
}
}