GroveGPS module

Dependents:   Tests_GPS

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GroveGPS.h Source File

GroveGPS.h

00001 #ifndef _GROVE_GPS_H_
00002 #define _GROVE_GPS_H_
00003 
00004 #include "mbed.h"
00005 #include <stdlib.h>
00006 #include <string>
00007 
00008 class GroveGPS {
00009 
00010 public:
00011 
00012     GroveGPS() : _last_line("") {}
00013 
00014     void readCharacter(char newCharacter) {
00015         if (newCharacter == '\n') {
00016             parseLine();
00017             _last_line = "";
00018         } else {
00019             _last_line += newCharacter;
00020         }
00021     }
00022 
00023     struct GGA {
00024         double  utc_time;       // Format: hhmmss.sss
00025         double  latitude;       // Format: ddmm.mmmm
00026         char    ns_indicator;   // Format: N=north or S=south
00027         double  longitude;      // Format: dddmm.mmmm
00028         char    ew_indicator;   // Format: E=east or W=west
00029         int position_fix;   // Options: [0=not available, 1=GPS SPS mode, 2=Differential GPS, 6=dead reckoning]
00030         int sats_used;      // Range: 0-12
00031         double  hdop;           // Horizontal Dilution of Precision
00032         double  msl_altitude;
00033         char    msl_altitude_units;
00034         double  geoid_separation;
00035         char    geoid_separation_units;
00036         long    age_of_diff;
00037         long    diff_ref_station_id;
00038         std::string  checksum;
00039     } gps_gga;
00040 
00041     void getTimestamp(char* buffer) {
00042         sprintf(buffer, "%f", gps_gga.utc_time);
00043     }
00044 
00045     void getLatitude(char* buffer) {
00046         double coordinate = convertGPSToDecimal(gps_gga.latitude);
00047         if (gps_gga.position_fix==0)
00048             sprintf(buffer, "N/A");
00049         else
00050             sprintf(buffer, "%c%f", (gps_gga.ns_indicator == 'N') ? '0' : '-', coordinate);
00051     }
00052 
00053     void getLongitude(char* buffer) {
00054         double coordinate = convertGPSToDecimal(gps_gga.longitude);
00055         if (gps_gga.position_fix==0)
00056             sprintf(buffer, "N/A");
00057         else
00058         sprintf(buffer, "%c%f", (gps_gga.ew_indicator == 'E') ? '0' : '-', coordinate);
00059     }
00060 
00061 private:
00062     std::string _last_line;
00063 
00064     double convertGPSToDecimal(double coordinate) {
00065         int degrees = coordinate/100.0;
00066         int minutes = ((int)coordinate) % 100;
00067         double seconds = coordinate - ((int)coordinate);
00068         return degrees + (minutes+seconds)/60;
00069 
00070     }
00071 
00072     void parseLine() {
00073         if (_last_line.find("GPGGA") != std::string::npos) {
00074             parseGGA();
00075         }
00076     }
00077 
00078     void parseGGA() {
00079         char* pEnd;
00080         for (int i=0; i<14; i++) {
00081             std::string current_item = _last_line.substr(0,_last_line.find(","));
00082             _last_line = _last_line.substr(_last_line.find(",")+1);
00083             if (i==0) {         // NMEA Tag
00084             } else if (i==1) {  // UTC time
00085                 gps_gga.utc_time = strtod(current_item.c_str(), &pEnd);
00086             } else if (i==2) {  // Latitude
00087                 gps_gga.latitude = strtod(current_item.c_str(), &pEnd);
00088             } else if (i==3) {  // Latitude North/South indicator
00089                 gps_gga.ns_indicator = current_item[0];
00090             } else if (i==4) {  // Longitude
00091                 gps_gga.longitude = strtod(current_item.c_str(), &pEnd);
00092             } else if (i==5) {  // Longitude indicator
00093                 gps_gga.ew_indicator = current_item[0];
00094             } else if (i==6) {
00095                 gps_gga.position_fix = strtod(current_item.c_str(), &pEnd);
00096             }
00097         }
00098     }
00099 };
00100 
00101 #endif