Parsing GPS data

I just got the Adafruit GPS shield as my goal is to build an rc car that can follow waypoints. I understand how to set up the GPS and print the raw data via serial but I don’t understand how to extract the longitude and latitude to calculate distance.

According to Adafruit once you parse the data you can simply use GPS.latitude, GPS.longitude to get the data.
They provide an example but it uses a bunch of language that I don’t understand. Specifically #ifdef. I’m fairly new to programming as well.

The other issue I’m running into it that if I write GPS.read() ; to read the GPS I get an error in the compiler saying that GPS wasn’t declared. In Adafruits example it was never declared but works.

Can anyone help explain to me how to parse the data? I just can’t follow the code so I don’t know what it’s doing.

Here’s their example

#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>


SoftwareSerial mySerial(8, 7);

Adafruit_GPS GPS(&mySerial);
// If using hardware serial (e.g. Arduino Mega), comment
// out the above six lines and enable this line instead:
//Adafruit_GPS GPS(&Serial1);


// Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
// Set to 'true' if you want to debug and listen to the raw GPS sentences. 
#define GPSECHO  true

// this keeps track of whether we're using the interrupt
// off by default!
boolean usingInterrupt = false;
void useInterrupt(boolean); // Func prototype keeps Arduino 0023 happy

void setup()  
{
    
  // connect at 115200 so we can read the GPS fast enough and echo without dropping chars
  // also spit it out
  Serial.begin(115200);
  Serial.println("Adafruit GPS library basic test!");

  // 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
  GPS.begin(9600);
  
  // uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  // uncomment this line to turn on only the "minimum recommended" data
  //GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
  // For parsing data, we don't suggest using anything but either RMC only or RMC+GGA since
  // the parser doesn't care about other sentences at this time
  
  // Set the update rate
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);   // 1 Hz update rate
  // For the parsing code to work nicely and have time to sort thru the data, and
  // print it out we don't suggest using anything higher than 1 Hz

  // Request updates on antenna status, comment out to keep quiet
  GPS.sendCommand(PGCMD_ANTENNA);

  // the nice thing about this code is you can have a timer0 interrupt go off
  // every 1 millisecond, and read data from the GPS for you. that makes the
  // loop code a heck of a lot easier!
  useInterrupt(true);

  delay(1000);
  // Ask for firmware version
  mySerial.println(PMTK_Q_RELEASE);
}


// Interrupt is called once a millisecond, looks for any new GPS data, and stores it
SIGNAL(TIMER0_COMPA_vect) {
  char c = GPS.read();
  // if you want to debug, this is a good time to do it!
#ifdef UDR0
  if (GPSECHO)
    if (c) UDR0 = c;  
    // writing direct to UDR0 is much much faster than Serial.print 
    // but only one character can be written at a time. 
#endif
}

void useInterrupt(boolean v) {
  if (v) {
    // Timer0 is already used for millis() - we'll just interrupt somewhere
    // in the middle and call the "Compare A" function above
    OCR0A = 0xAF;
    TIMSK0 |= _BV(OCIE0A);
    usingInterrupt = true;
  } else {
    // do not call the interrupt function COMPA anymore
    TIMSK0 &= ~_BV(OCIE0A);
    usingInterrupt = false;
  }
}

uint32_t timer = millis();
void loop()                     // run over and over again
{
  // in case you are not using the interrupt above, you'll
  // need to 'hand query' the GPS, not suggested :(
  if (! usingInterrupt) {
    // read data from the GPS in the 'main loop'
    char c = GPS.read();
    // if you want to debug, this is a good time to do it!
    if (GPSECHO)
      if (c) Serial.print(c);
  }
  
  // if a sentence is received, we can check the checksum, parse it...
  if (GPS.newNMEAreceived()) {
    // a tricky thing here is if we print the NMEA sentence, or data
    // we end up not listening and catching other sentences! 
    // so be very wary if using OUTPUT_ALLDATA and trytng to print out data
    //Serial.println(GPS.lastNMEA());   // this also sets the newNMEAreceived() flag to false
  
    if (!GPS.parse(GPS.lastNMEA()))   // this also sets the newNMEAreceived() flag to false
      return;  // we can fail to parse a sentence in which case we should just wait for another
  }

  // if millis() or timer wraps around, we'll just reset it
  if (timer > millis())  timer = millis();

  // approximately every 2 seconds or so, print out the current stats
  if (millis() - timer > 2000) { 
    timer = millis(); // reset the timer
    
    Serial.print("\nTime: ");
    Serial.print(GPS.hour, DEC); Serial.print(':');
    Serial.print(GPS.minute, DEC); Serial.print(':');
    Serial.print(GPS.seconds, DEC); Serial.print('.');
    Serial.println(GPS.milliseconds);
    Serial.print("Date: ");
    Serial.print(GPS.day, DEC); Serial.print('/');
    Serial.print(GPS.month, DEC); Serial.print("/20");
    Serial.println(GPS.year, DEC);
    Serial.print("Fix: "); Serial.print((int)GPS.fix);
    Serial.print(" quality: "); Serial.println((int)GPS.fixquality); 
    if (GPS.fix) {
      Serial.print("Location: ");
      Serial.print(GPS.latitude, 4); Serial.print(GPS.lat);
      Serial.print(", "); 
      Serial.print(GPS.longitude, 4); Serial.println(GPS.lon);
      
      Serial.print("Speed (knots): "); Serial.println(GPS.speed);
      Serial.print("Angle: "); Serial.println(GPS.angle);
      Serial.print("Altitude: "); Serial.println(GPS.altitude);
      Serial.print("Satellites: "); Serial.println((int)GPS.satellites);
    }
  }
}

There is a GPS application TinyGPS++ http://arduiniana.org/libraries/tinygpsplus/ that might be of great use to you. It has been my "Go To" solution for reading the GPS NMEA string, quickly and easily.

Doc

It has been my "Go To" solution for reading the GPS NMEA string, quickly and easily.

For reading AND for doing the parsing when the sentence is complete, and for knowing when the sentence is complete. Try it; you'lll like it.

Me Again... I do hope? that you understand that making GPS position measurements are ONLY valid outside where the GPS receiver has a good view of the whole sky and +/- 10 meters accuracy is considered fair to good... So, If your "Way-points" need to have better accuracy than 10 meters you might consider another means of using the GPS data... I'd Highly recommend you reading this first... https://www.google.com/search?q=Wiki+GPS+Accuracy&rlz=1C1CHFX_enUS594US594&oq=Wiki+GPS+Accuracy&aqs=chrome..69i57.18839j0j4&sourceid=chrome&es_sm=122&ie=UTF-8... "Wiki GPS Accuracy" was my search phrase.. There were 8 responses, ALL have a bit or two that would be useful for your project. Google First, Arduino Forum, Second. Mr Google is your friend....

Doc

Ah that helped a lot thanks! The TinyGPS++ is amazing, I’m getting the data I need easily without any fuss.
Now it’s onto trying to get it to follow waypoints haha

Looking at the TinyGPS page they have an example for calculating distance to a waypoint and course from the current location.
When I write the code like they have I get a compile error of “expected primary expression before ‘.’ token”
It doesn’t like

double courseto = TinyGPSPlus.courseTo(gps.location.lat(), gps.location.lng(), tower_lat, tower_lng) ;

but I’m not sure what’s wrong as the example from the TinyGPS page is exactly the same.

In it’s entirety

#include <TinyGPS++.h>
#include <SoftwareSerial.h>

TinyGPSPlus gps ;

SoftwareSerial mySerial(8, 7) ;

void setup()
{
  Serial.begin(115200) ;
  mySerial.begin(9600) ;
  
  Serial.println("running") ;
  
}

void loop()
{
  while (mySerial.available() > 0)
  {
    gps.encode(mySerial.read()) ;
    
  const double tower_lat = 48.85826 ;
  const double tower_lng = 2.294516 ;
  double courseto = TinyGPSPlus.courseTo(gps.location.lat(), gps.location.lng(), tower_lat, tower_lng) ;
  Serial.println(courseto) ;
  }
}

So after working some more on it with the TinyGPS++ library I've ran into a weird issue. I wrote a bit of code to get longitude and latitude and it was working fine until it stopped. Code wise nothings changed but it gives me 0.00 for longitude and latitude. If I comment out the bit for longitude it will give me latitude after a few seconds, and vise versa for longitude.

I know it has a fix as the led tells me so and if I load up the parsing or echo sketch example it works perfectly. What could have changed?

The answer is what makes programming fun and useful (Because it does cause one to think)... I don't have any suggestions... However you might go back to the TinyGPS++ page and leave a question... Mr Hart seems to be a very nice and helpful person in all I've read on his blog... were I to guess it might be a re-arrangement of the order of the called functions that is at the root of your problem... and although I've used many GPS devices (Still have 4 working devices) I've found that sometimes reordering one or more functions fixed several issues I've had in the past... But, A coder, I ain't... I am an old hardware designer learning the language just to keep young and hopefully stave off the eventual fossilization of the mind... Original high current rectifier http://commons.wikimedia.org/wiki/File:Tungar_bulbs.jpg# They had a really pretty blue glow that could cause UV burns to the skin... when used @ high (10 - 20A) current, if the exposure was long enough

Doc

I wrote a bit of code to get longitude and latitude and it was working fine until it stopped.

But, you forgot to post it.

Code wise nothings changed but it gives me 0.00 for longitude and latitude. If I comment out the bit for longitude it will give me latitude after a few seconds, and vise versa for longitude.

Nothing changed from what? I'm guessing that you are running out of memory. Time to post the code you are having problems with so others can evaluate it.

I added in

if (gps.location.isUpdated())  // if the location has been updated, print the data

and that’s got it working now. Not sure why it doesn’t work without that all of a sudden but it works.

Here’s the whole code

#include <TinyGPS++.h>
#include <SoftwareSerial.h>

TinyGPSPlus gps ;

SoftwareSerial mySerial(8, 7) ; //Software serial

float longitude ; 
float latitude ; 
const double tower_long = 48.85826 ;
const double tower_lat = 2.294516 ;
void setup()
{
  Serial.begin(115200) ; 
  mySerial.begin(9600) ; // start myserial
  
  Serial.println("running") ;
  
}

void loop()
{
  while (mySerial.available() > 0) // while we are getting data from mySerial
  {
    gps.encode(mySerial.read()) ; // read gps
    if (gps.location.isUpdated())  // if the location has been updated, print the data
    {
      longitude = gps.location.lat() ;
      latitude = gps.location.lng() ;
    Serial.println(longitude) ; //print latitude
    Serial.println(latitude ) ; //print longitude
    double distanceKm = TinyGPSPlus.distanceBetween(gps.location.lat(), gps.location.lng(), tower_lat, tower_long) / 1000.0 ;
    
    }
  }
}

Reading through the TinyGPS page I found a section on figuring directions to waypoints. There’s a bit of code given to calculate the distance between two set of points but when I compile it I get an error saying it "expected a primary expression before ‘.’ token. Here’s the bit of code

 double distanceKm = TinyGPSPlus.distanceBetween(gps.location.lat(), gps.location.lng(), tower_lat, tower_long) / 1000.0 ;

What is it meaning by that?

What is it meaning by that?

Look at each of the things just before the '.' in that statement: TinyGPSPlus.distanceBetween() gps.location.lat() gps.location.lng()

The last two return values, so you could store them in variables, and then use the variables in the call to distanceBetween(), if you can't (yet) spot the problem. The compiler will then tell you specifically which of the three calls it doesn't like.

 if (gps.location.isUpdated())  // if the location has been updated, print the data
    {
      longitude = gps.location.lat() ;
      latitude = gps.location.lng() ;
    Serial.println(longitude) ; //print latitude
    Serial.println(latitude ) ; //print longitude
    double distanceKm = TinyGPSPlus.distanceBetween(latitude, longitude, tower_lat, tower_long) / 1000.0 ;

gps.location.lat() is stored in longitude and gps.location.lng() is stored in latitude. The compiler isn't telling me what it doesn't like, unless I'm missing something. Which I probably am as I'm fairly new to programming. It just says : sketch_aug03a.ino: In function 'void loop()': sketch_aug03a:32: error: expected primary-expression before '.' token

The code from the TinyGPS page is:

const double EIFFEL_TOWER_LAT = 48.85826;
const double EIFFEL_TOWER_LNG = 2.294516;
double distanceKm =
  TinyGPSPlus.distanceBetween(
    gps.location.lat(),
    gps.location.lng(),
    EIFFEL_TOWER_LAT,
    EIFFEL_TOWER_LNG) / 1000.0;
double courseTo =
  TinyGPSPlus.courseTo(
    gps.location.lat(),
    gps.location.lng(),
    EIFFEL_TOWER_LAT,
    EIFFEL_TOWER_LNG);
Serial.print("Distance (km) to Eiffel Tower: ");
Serial.println(distanceKm);
Serial.print("Course to Eiffel Tower: ");
Serial.println(courseTo);
Serial.print("Human directions: ");
Serial.println(TinyGPSPlus.cardinal(courseTo));

which won't compile either under the same issue

so when you compile that, what error do you get ?

It seems to me, that when using a static method of the TihyGPSplus class, the correct syntax would be

double d = TinyGPSplus::distanceBetween(  long1, lat1, long2, lat2 );

with the double colon instead of the decimal point between the class name and the function name.

Or maybe I am confused with java again. Anyway, if you run out of other ideas, try that and see if it works.

It seems to me, that when using a static method of the TihyGPSplus class, the correct syntax would be

Code:

double d = TinyGPSplus::distanceBetween( long1, lat1, long2, lat2 );

with the double colon instead of the decimal point between the class name and the function name.

That worked! I don't how accurate it is or if it's correct but it compiles and outputs a number, I'll check if it's right later tonight.