final groupe 1
GroveGPS.h@0:5f13a3b8bc13, 2019-02-03 (annotated)
- Committer:
- GrandDiego
- Date:
- Sun Feb 03 19:36:08 2019 +0000
- Revision:
- 0:5f13a3b8bc13
final groupe 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
GrandDiego | 0:5f13a3b8bc13 | 1 | #ifndef _GROVE_GPS_H_ |
GrandDiego | 0:5f13a3b8bc13 | 2 | #define _GROVE_GPS_H_ |
GrandDiego | 0:5f13a3b8bc13 | 3 | |
GrandDiego | 0:5f13a3b8bc13 | 4 | #include "mbed.h" |
GrandDiego | 0:5f13a3b8bc13 | 5 | #include <stdlib.h> |
GrandDiego | 0:5f13a3b8bc13 | 6 | #include <string> |
GrandDiego | 0:5f13a3b8bc13 | 7 | |
GrandDiego | 0:5f13a3b8bc13 | 8 | class GroveGPS { |
GrandDiego | 0:5f13a3b8bc13 | 9 | |
GrandDiego | 0:5f13a3b8bc13 | 10 | public: |
GrandDiego | 0:5f13a3b8bc13 | 11 | |
GrandDiego | 0:5f13a3b8bc13 | 12 | GroveGPS() : _last_line("") {} |
GrandDiego | 0:5f13a3b8bc13 | 13 | |
GrandDiego | 0:5f13a3b8bc13 | 14 | void readCharacter(char newCharacter) { |
GrandDiego | 0:5f13a3b8bc13 | 15 | if (newCharacter == '\n') { |
GrandDiego | 0:5f13a3b8bc13 | 16 | parseLine(); |
GrandDiego | 0:5f13a3b8bc13 | 17 | _last_line = ""; |
GrandDiego | 0:5f13a3b8bc13 | 18 | } else { |
GrandDiego | 0:5f13a3b8bc13 | 19 | _last_line += newCharacter; |
GrandDiego | 0:5f13a3b8bc13 | 20 | } |
GrandDiego | 0:5f13a3b8bc13 | 21 | } |
GrandDiego | 0:5f13a3b8bc13 | 22 | |
GrandDiego | 0:5f13a3b8bc13 | 23 | struct GGA { |
GrandDiego | 0:5f13a3b8bc13 | 24 | double utc_time; // Format: hhmmss.sss |
GrandDiego | 0:5f13a3b8bc13 | 25 | double latitude; // Format: ddmm.mmmm |
GrandDiego | 0:5f13a3b8bc13 | 26 | char ns_indicator; // Format: N=north or S=south |
GrandDiego | 0:5f13a3b8bc13 | 27 | double longitude; // Format: dddmm.mmmm |
GrandDiego | 0:5f13a3b8bc13 | 28 | char ew_indicator; // Format: E=east or W=west |
GrandDiego | 0:5f13a3b8bc13 | 29 | int position_fix; // Options: [0=not available, 1=GPS SPS mode, 2=Differential GPS, 6=dead reckoning] |
GrandDiego | 0:5f13a3b8bc13 | 30 | int sats_used; // Range: 0-12 |
GrandDiego | 0:5f13a3b8bc13 | 31 | double hdop; // Horizontal Dilution of Precision |
GrandDiego | 0:5f13a3b8bc13 | 32 | double msl_altitude; |
GrandDiego | 0:5f13a3b8bc13 | 33 | char msl_altitude_units; |
GrandDiego | 0:5f13a3b8bc13 | 34 | double geoid_separation; |
GrandDiego | 0:5f13a3b8bc13 | 35 | char geoid_separation_units; |
GrandDiego | 0:5f13a3b8bc13 | 36 | long age_of_diff; |
GrandDiego | 0:5f13a3b8bc13 | 37 | long diff_ref_station_id; |
GrandDiego | 0:5f13a3b8bc13 | 38 | std::string checksum; |
GrandDiego | 0:5f13a3b8bc13 | 39 | } gps_gga; |
GrandDiego | 0:5f13a3b8bc13 | 40 | |
GrandDiego | 0:5f13a3b8bc13 | 41 | struct VTG { |
GrandDiego | 0:5f13a3b8bc13 | 42 | double speed_over_ground; |
GrandDiego | 0:5f13a3b8bc13 | 43 | } gps_vtg; |
GrandDiego | 0:5f13a3b8bc13 | 44 | |
GrandDiego | 0:5f13a3b8bc13 | 45 | void getTimestamp(char* buffer) { |
GrandDiego | 0:5f13a3b8bc13 | 46 | sprintf(buffer, "%f", gps_gga.utc_time); |
GrandDiego | 0:5f13a3b8bc13 | 47 | } |
GrandDiego | 0:5f13a3b8bc13 | 48 | |
GrandDiego | 0:5f13a3b8bc13 | 49 | void getLatitude(char* buffer) { |
GrandDiego | 0:5f13a3b8bc13 | 50 | double coordinate = convertGPSToDecimal(gps_gga.latitude); |
GrandDiego | 0:5f13a3b8bc13 | 51 | if (gps_gga.position_fix==0) |
GrandDiego | 0:5f13a3b8bc13 | 52 | sprintf(buffer, "N/A"); |
GrandDiego | 0:5f13a3b8bc13 | 53 | else |
GrandDiego | 0:5f13a3b8bc13 | 54 | sprintf(buffer, "%c%f", (gps_gga.ns_indicator == 'N') ? '0' : '-', coordinate); |
GrandDiego | 0:5f13a3b8bc13 | 55 | } |
GrandDiego | 0:5f13a3b8bc13 | 56 | |
GrandDiego | 0:5f13a3b8bc13 | 57 | void getLongitude(char* buffer) { |
GrandDiego | 0:5f13a3b8bc13 | 58 | double coordinate = convertGPSToDecimal(gps_gga.longitude); |
GrandDiego | 0:5f13a3b8bc13 | 59 | if (gps_gga.position_fix==0) |
GrandDiego | 0:5f13a3b8bc13 | 60 | sprintf(buffer, "N/A"); |
GrandDiego | 0:5f13a3b8bc13 | 61 | else |
GrandDiego | 0:5f13a3b8bc13 | 62 | sprintf(buffer, "%c%f", (gps_gga.ew_indicator == 'E') ? '0' : '-', coordinate); |
GrandDiego | 0:5f13a3b8bc13 | 63 | } |
GrandDiego | 0:5f13a3b8bc13 | 64 | |
GrandDiego | 0:5f13a3b8bc13 | 65 | void getAltitude(char* buffer) { |
GrandDiego | 0:5f13a3b8bc13 | 66 | if (gps_gga.position_fix==0) |
GrandDiego | 0:5f13a3b8bc13 | 67 | sprintf(buffer, "N/A"); |
GrandDiego | 0:5f13a3b8bc13 | 68 | else |
GrandDiego | 0:5f13a3b8bc13 | 69 | sprintf(buffer, "%f", gps_gga.msl_altitude); |
GrandDiego | 0:5f13a3b8bc13 | 70 | } |
GrandDiego | 0:5f13a3b8bc13 | 71 | |
GrandDiego | 0:5f13a3b8bc13 | 72 | void getSpeed(char* buffer) { |
GrandDiego | 0:5f13a3b8bc13 | 73 | if (gps_gga.position_fix==0) |
GrandDiego | 0:5f13a3b8bc13 | 74 | sprintf(buffer, "N/A"); |
GrandDiego | 0:5f13a3b8bc13 | 75 | else |
GrandDiego | 0:5f13a3b8bc13 | 76 | sprintf(buffer, "%f", gps_vtg.speed_over_ground); |
GrandDiego | 0:5f13a3b8bc13 | 77 | } |
GrandDiego | 0:5f13a3b8bc13 | 78 | |
GrandDiego | 0:5f13a3b8bc13 | 79 | |
GrandDiego | 0:5f13a3b8bc13 | 80 | private: |
GrandDiego | 0:5f13a3b8bc13 | 81 | std::string _last_line; |
GrandDiego | 0:5f13a3b8bc13 | 82 | |
GrandDiego | 0:5f13a3b8bc13 | 83 | double convertGPSToDecimal(double coordinate) { |
GrandDiego | 0:5f13a3b8bc13 | 84 | int degrees = coordinate/100.0; |
GrandDiego | 0:5f13a3b8bc13 | 85 | int minutes = ((int)coordinate) % 100; |
GrandDiego | 0:5f13a3b8bc13 | 86 | double seconds = coordinate - ((int)coordinate); |
GrandDiego | 0:5f13a3b8bc13 | 87 | return degrees + (minutes+seconds)/60; |
GrandDiego | 0:5f13a3b8bc13 | 88 | |
GrandDiego | 0:5f13a3b8bc13 | 89 | } |
GrandDiego | 0:5f13a3b8bc13 | 90 | |
GrandDiego | 0:5f13a3b8bc13 | 91 | void parseLine() { |
GrandDiego | 0:5f13a3b8bc13 | 92 | if (_last_line.find("GPGGA") != std::string::npos) { |
GrandDiego | 0:5f13a3b8bc13 | 93 | parseGGA(); |
GrandDiego | 0:5f13a3b8bc13 | 94 | } |
GrandDiego | 0:5f13a3b8bc13 | 95 | if (_last_line.find("GPVTG") != std::string::npos) { |
GrandDiego | 0:5f13a3b8bc13 | 96 | parseVTG(); |
GrandDiego | 0:5f13a3b8bc13 | 97 | } |
GrandDiego | 0:5f13a3b8bc13 | 98 | } |
GrandDiego | 0:5f13a3b8bc13 | 99 | |
GrandDiego | 0:5f13a3b8bc13 | 100 | void parseGGA() { |
GrandDiego | 0:5f13a3b8bc13 | 101 | char* pEnd; |
GrandDiego | 0:5f13a3b8bc13 | 102 | for (int i=0; i<16; i++) { |
GrandDiego | 0:5f13a3b8bc13 | 103 | std::string current_item = _last_line.substr(0,_last_line.find(",")); |
GrandDiego | 0:5f13a3b8bc13 | 104 | _last_line = _last_line.substr(_last_line.find(",")+1); |
GrandDiego | 0:5f13a3b8bc13 | 105 | if (i==0) { // NMEA Tag |
GrandDiego | 0:5f13a3b8bc13 | 106 | } else if (i==1) { // UTC time |
GrandDiego | 0:5f13a3b8bc13 | 107 | gps_gga.utc_time = strtod(current_item.c_str(), &pEnd); |
GrandDiego | 0:5f13a3b8bc13 | 108 | } else if (i==2) { // Latitude |
GrandDiego | 0:5f13a3b8bc13 | 109 | gps_gga.latitude = strtod(current_item.c_str(), &pEnd); |
GrandDiego | 0:5f13a3b8bc13 | 110 | } else if (i==3) { // Latitude North/South indicator |
GrandDiego | 0:5f13a3b8bc13 | 111 | gps_gga.ns_indicator = current_item[0]; |
GrandDiego | 0:5f13a3b8bc13 | 112 | } else if (i==4) { // Longitude |
GrandDiego | 0:5f13a3b8bc13 | 113 | gps_gga.longitude = strtod(current_item.c_str(), &pEnd); |
GrandDiego | 0:5f13a3b8bc13 | 114 | } else if (i==5) { // Longitude indicator |
GrandDiego | 0:5f13a3b8bc13 | 115 | gps_gga.ew_indicator = current_item[0]; |
GrandDiego | 0:5f13a3b8bc13 | 116 | } else if (i==6) { |
GrandDiego | 0:5f13a3b8bc13 | 117 | gps_gga.position_fix = strtod(current_item.c_str(), &pEnd); |
GrandDiego | 0:5f13a3b8bc13 | 118 | }/* else if(i==7) { |
GrandDiego | 0:5f13a3b8bc13 | 119 | gps_gga.sats_used=strtod(current_item.c_str(),&pEnd); |
GrandDiego | 0:5f13a3b8bc13 | 120 | } else if(i==8) { |
GrandDiego | 0:5f13a3b8bc13 | 121 | gps_gga.hdop=strtod(curernt_item.c_str(),&pend); |
GrandDiego | 0:5f13a3b8bc13 | 122 | }*/ else if(i==9) { |
GrandDiego | 0:5f13a3b8bc13 | 123 | gps_gga.msl_altitude=strtod(current_item.c_str(), &pEnd); |
GrandDiego | 0:5f13a3b8bc13 | 124 | }else if(i==10) { |
GrandDiego | 0:5f13a3b8bc13 | 125 | gps_gga.msl_altitude_units=current_item[0]; |
GrandDiego | 0:5f13a3b8bc13 | 126 | }/*else if(i==11) { |
GrandDiego | 0:5f13a3b8bc13 | 127 | gps_gga.geoid_separation=strtod(current_item.c_str(), &pEnd); |
GrandDiego | 0:5f13a3b8bc13 | 128 | } else if(i==12) { |
GrandDiego | 0:5f13a3b8bc13 | 129 | gps_gga.geoid_seperation_units=current_item[0]; |
GrandDiego | 0:5f13a3b8bc13 | 130 | } else if(i==13) { |
GrandDiego | 0:5f13a3b8bc13 | 131 | gps_gga.age_of_diff=strtol(current_item.c_str(), &pEnd, 10); |
GrandDiego | 0:5f13a3b8bc13 | 132 | } else if(i==14) { |
GrandDiego | 0:5f13a3b8bc13 | 133 | gps_gga.diff_ref_station_id=strtol(current_item.c_str(), &pEnd, 10); |
GrandDiego | 0:5f13a3b8bc13 | 134 | } */ |
GrandDiego | 0:5f13a3b8bc13 | 135 | } |
GrandDiego | 0:5f13a3b8bc13 | 136 | } |
GrandDiego | 0:5f13a3b8bc13 | 137 | |
GrandDiego | 0:5f13a3b8bc13 | 138 | void parseVTG() { |
GrandDiego | 0:5f13a3b8bc13 | 139 | char* pEnd; |
GrandDiego | 0:5f13a3b8bc13 | 140 | for (int i=0; i<9; i++) { |
GrandDiego | 0:5f13a3b8bc13 | 141 | std::string current_item = _last_line.substr(0,_last_line.find(",")); |
GrandDiego | 0:5f13a3b8bc13 | 142 | _last_line = _last_line.substr(_last_line.find(",")+1); |
GrandDiego | 0:5f13a3b8bc13 | 143 | if (i==0) { |
GrandDiego | 0:5f13a3b8bc13 | 144 | } else if (i==7) { |
GrandDiego | 0:5f13a3b8bc13 | 145 | gps_vtg.speed_over_ground = strtod(current_item.c_str(), &pEnd); |
GrandDiego | 0:5f13a3b8bc13 | 146 | } |
GrandDiego | 0:5f13a3b8bc13 | 147 | } |
GrandDiego | 0:5f13a3b8bc13 | 148 | } |
GrandDiego | 0:5f13a3b8bc13 | 149 | }; |
GrandDiego | 0:5f13a3b8bc13 | 150 | |
GrandDiego | 0:5f13a3b8bc13 | 151 | #endif |