thread bug
Fork of GroveGPS by
GroveGPS.h@2:e3f570014ab4, 2018-05-31 (annotated)
- Committer:
- JimCarver
- Date:
- Thu May 31 16:28:32 2018 +0000
- Revision:
- 2:e3f570014ab4
- Parent:
- 1:7ad98913098c
bug
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
michaelray | 0:56d6407653a7 | 1 | #ifndef _GROVE_GPS_H_ |
michaelray | 0:56d6407653a7 | 2 | #define _GROVE_GPS_H_ |
michaelray | 0:56d6407653a7 | 3 | |
michaelray | 0:56d6407653a7 | 4 | #include "mbed.h" |
michaelray | 0:56d6407653a7 | 5 | #include <stdlib.h> |
michaelray | 0:56d6407653a7 | 6 | #include <string> |
michaelray | 0:56d6407653a7 | 7 | |
JimCarver | 1:7ad98913098c | 8 | |
michaelray | 0:56d6407653a7 | 9 | class GroveGPS { |
michaelray | 0:56d6407653a7 | 10 | |
michaelray | 0:56d6407653a7 | 11 | public: |
michaelray | 0:56d6407653a7 | 12 | |
JimCarver | 1:7ad98913098c | 13 | GroveGPS() : _last_line("") { |
michaelray | 0:56d6407653a7 | 14 | |
michaelray | 0:56d6407653a7 | 15 | } |
michaelray | 0:56d6407653a7 | 16 | |
JimCarver | 1:7ad98913098c | 17 | std::string _last_line; |
JimCarver | 1:7ad98913098c | 18 | |
michaelray | 0:56d6407653a7 | 19 | struct GGA { |
michaelray | 0:56d6407653a7 | 20 | double utc_time; // Format: hhmmss.sss |
michaelray | 0:56d6407653a7 | 21 | double latitude; // Format: ddmm.mmmm |
michaelray | 0:56d6407653a7 | 22 | char ns_indicator; // Format: N=north or S=south |
michaelray | 0:56d6407653a7 | 23 | double longitude; // Format: dddmm.mmmm |
michaelray | 0:56d6407653a7 | 24 | char ew_indicator; // Format: E=east or W=west |
michaelray | 0:56d6407653a7 | 25 | int position_fix; // Options: [0=not available, 1=GPS SPS mode, 2=Differential GPS, 6=dead reckoning] |
michaelray | 0:56d6407653a7 | 26 | int sats_used; // Range: 0-12 |
michaelray | 0:56d6407653a7 | 27 | double hdop; // Horizontal Dilution of Precision |
michaelray | 0:56d6407653a7 | 28 | double msl_altitude; |
michaelray | 0:56d6407653a7 | 29 | char msl_altitude_units; |
michaelray | 0:56d6407653a7 | 30 | double geoid_separation; |
michaelray | 0:56d6407653a7 | 31 | char geoid_separation_units; |
michaelray | 0:56d6407653a7 | 32 | long age_of_diff; |
michaelray | 0:56d6407653a7 | 33 | long diff_ref_station_id; |
JimCarver | 1:7ad98913098c | 34 | int new_flag; |
michaelray | 0:56d6407653a7 | 35 | std::string checksum; |
michaelray | 0:56d6407653a7 | 36 | } gps_gga; |
JimCarver | 1:7ad98913098c | 37 | |
JimCarver | 1:7ad98913098c | 38 | struct ZDA { |
JimCarver | 1:7ad98913098c | 39 | double utc_time; // Format: hhmmss.sss |
JimCarver | 1:7ad98913098c | 40 | int day; |
JimCarver | 1:7ad98913098c | 41 | int month; |
JimCarver | 1:7ad98913098c | 42 | int year; |
JimCarver | 1:7ad98913098c | 43 | int new_flag; |
JimCarver | 1:7ad98913098c | 44 | std::string checksum; |
JimCarver | 1:7ad98913098c | 45 | } gps_zda; |
JimCarver | 1:7ad98913098c | 46 | |
JimCarver | 1:7ad98913098c | 47 | struct VTG { |
JimCarver | 1:7ad98913098c | 48 | double course1; |
JimCarver | 1:7ad98913098c | 49 | int truec; |
JimCarver | 1:7ad98913098c | 50 | double course2; |
JimCarver | 1:7ad98913098c | 51 | int magnetic; |
JimCarver | 1:7ad98913098c | 52 | double speed1; |
JimCarver | 1:7ad98913098c | 53 | int knots; |
JimCarver | 1:7ad98913098c | 54 | double speed2; |
JimCarver | 1:7ad98913098c | 55 | int kmh; |
JimCarver | 1:7ad98913098c | 56 | int new_flag; |
JimCarver | 1:7ad98913098c | 57 | std::string checksum; |
JimCarver | 1:7ad98913098c | 58 | } gps_vtg; |
michaelray | 0:56d6407653a7 | 59 | |
michaelray | 0:56d6407653a7 | 60 | void getTimestamp(char* buffer) { |
michaelray | 0:56d6407653a7 | 61 | if (gps_gga.position_fix==0) |
michaelray | 0:56d6407653a7 | 62 | sprintf(buffer, "N/A"); |
michaelray | 0:56d6407653a7 | 63 | else |
JimCarver | 1:7ad98913098c | 64 | sprintf(buffer, "%f", gps_gga.utc_time); |
JimCarver | 1:7ad98913098c | 65 | } |
JimCarver | 1:7ad98913098c | 66 | |
JimCarver | 1:7ad98913098c | 67 | void getAltitude(char* buffer) { |
JimCarver | 1:7ad98913098c | 68 | if (gps_gga.position_fix==0) |
JimCarver | 1:7ad98913098c | 69 | sprintf(buffer, "N/A"); |
JimCarver | 1:7ad98913098c | 70 | else |
JimCarver | 1:7ad98913098c | 71 | sprintf(buffer, "%f", gps_gga.msl_altitude); |
JimCarver | 1:7ad98913098c | 72 | } |
JimCarver | 1:7ad98913098c | 73 | |
JimCarver | 1:7ad98913098c | 74 | void getCourse(char* buffer) { |
JimCarver | 1:7ad98913098c | 75 | if (gps_gga.position_fix==0) |
JimCarver | 1:7ad98913098c | 76 | sprintf(buffer, "N/A"); |
JimCarver | 1:7ad98913098c | 77 | else |
JimCarver | 1:7ad98913098c | 78 | sprintf(buffer, "%f", gps_vtg.course1); |
JimCarver | 1:7ad98913098c | 79 | } |
JimCarver | 1:7ad98913098c | 80 | |
JimCarver | 1:7ad98913098c | 81 | void getSpeed(char* buffer) { |
JimCarver | 1:7ad98913098c | 82 | if (gps_gga.position_fix==0) |
JimCarver | 1:7ad98913098c | 83 | sprintf(buffer, "N/A"); |
JimCarver | 1:7ad98913098c | 84 | else |
JimCarver | 1:7ad98913098c | 85 | sprintf(buffer, "%f", gps_vtg.speed1); |
JimCarver | 1:7ad98913098c | 86 | } |
JimCarver | 1:7ad98913098c | 87 | |
JimCarver | 1:7ad98913098c | 88 | |
JimCarver | 1:7ad98913098c | 89 | void getUncertanty(char* buffer) { |
JimCarver | 1:7ad98913098c | 90 | if (gps_gga.position_fix==0) |
JimCarver | 1:7ad98913098c | 91 | sprintf(buffer, "N/A"); |
JimCarver | 1:7ad98913098c | 92 | else |
JimCarver | 1:7ad98913098c | 93 | sprintf(buffer, "%f", gps_gga.hdop); |
JimCarver | 1:7ad98913098c | 94 | } |
JimCarver | 1:7ad98913098c | 95 | |
JimCarver | 1:7ad98913098c | 96 | void getLatitude(char* buffer) { |
JimCarver | 1:7ad98913098c | 97 | double coordinate = convertGPSToDecimal(gps_gga.latitude); |
JimCarver | 1:7ad98913098c | 98 | if (gps_gga.position_fix==0) { |
JimCarver | 1:7ad98913098c | 99 | sprintf(buffer, "N/A"); |
JimCarver | 1:7ad98913098c | 100 | } else { |
JimCarver | 1:7ad98913098c | 101 | if(gps_gga.ns_indicator == 'S') coordinate *= -1.000; |
JimCarver | 1:7ad98913098c | 102 | sprintf(buffer, "%f", coordinate); |
JimCarver | 1:7ad98913098c | 103 | } |
michaelray | 0:56d6407653a7 | 104 | } |
michaelray | 0:56d6407653a7 | 105 | |
michaelray | 0:56d6407653a7 | 106 | void getLongitude(char* buffer) { |
michaelray | 0:56d6407653a7 | 107 | double coordinate = convertGPSToDecimal(gps_gga.longitude); |
michaelray | 0:56d6407653a7 | 108 | if (gps_gga.position_fix==0) |
michaelray | 0:56d6407653a7 | 109 | sprintf(buffer, "N/A"); |
JimCarver | 1:7ad98913098c | 110 | else { |
JimCarver | 1:7ad98913098c | 111 | if(gps_gga.ew_indicator == 'W') coordinate *= -1.0000; |
JimCarver | 1:7ad98913098c | 112 | sprintf(buffer, "%f", coordinate); |
JimCarver | 1:7ad98913098c | 113 | } |
michaelray | 0:56d6407653a7 | 114 | } |
michaelray | 0:56d6407653a7 | 115 | |
michaelray | 0:56d6407653a7 | 116 | |
michaelray | 0:56d6407653a7 | 117 | double convertGPSToDecimal(double coordinate) { |
michaelray | 0:56d6407653a7 | 118 | int degrees = coordinate/100.0; |
michaelray | 0:56d6407653a7 | 119 | int minutes = ((int)coordinate) % 100; |
michaelray | 0:56d6407653a7 | 120 | double seconds = coordinate - ((int)coordinate); |
michaelray | 0:56d6407653a7 | 121 | return degrees + (minutes+seconds)/60; |
michaelray | 0:56d6407653a7 | 122 | |
michaelray | 0:56d6407653a7 | 123 | } |
michaelray | 0:56d6407653a7 | 124 | |
JimCarver | 1:7ad98913098c | 125 | void parseVTG() { |
JimCarver | 1:7ad98913098c | 126 | char* pEnd; |
JimCarver | 1:7ad98913098c | 127 | for (int i=0; i<5; i++) { |
JimCarver | 1:7ad98913098c | 128 | std::string current_item = _last_line.substr(0,_last_line.find(",")); |
JimCarver | 1:7ad98913098c | 129 | _last_line = _last_line.substr(_last_line.find(",")+1); |
JimCarver | 1:7ad98913098c | 130 | if (i==0) { // NMEA Tag |
JimCarver | 1:7ad98913098c | 131 | } else if (i==1) { // UTC time |
JimCarver | 1:7ad98913098c | 132 | gps_vtg.course1 = strtod(current_item.c_str(), &pEnd); |
JimCarver | 1:7ad98913098c | 133 | } else if (i==3) { |
JimCarver | 1:7ad98913098c | 134 | gps_vtg.course2 = strtod(current_item.c_str(), &pEnd); |
JimCarver | 1:7ad98913098c | 135 | } else if (i==5) { |
JimCarver | 1:7ad98913098c | 136 | gps_vtg.speed1 = strtod(current_item.c_str(), &pEnd); |
JimCarver | 1:7ad98913098c | 137 | } else if (i==7) { |
JimCarver | 1:7ad98913098c | 138 | gps_vtg.speed2 = strtod(current_item.c_str(), &pEnd); |
JimCarver | 1:7ad98913098c | 139 | } |
JimCarver | 1:7ad98913098c | 140 | if(gps_vtg.course2 > gps_vtg.course1) gps_vtg.course1 = gps_vtg.course2; |
JimCarver | 1:7ad98913098c | 141 | if(gps_vtg.speed2 > gps_vtg.speed1) gps_vtg.speed1 = gps_vtg.speed2; |
michaelray | 0:56d6407653a7 | 142 | } |
JimCarver | 1:7ad98913098c | 143 | gps_vtg.new_flag = 1; |
JimCarver | 1:7ad98913098c | 144 | } |
JimCarver | 1:7ad98913098c | 145 | |
JimCarver | 1:7ad98913098c | 146 | void parseZDA() { |
JimCarver | 1:7ad98913098c | 147 | char* pEnd; |
JimCarver | 1:7ad98913098c | 148 | for (int i=0; i<5; i++) { |
JimCarver | 1:7ad98913098c | 149 | std::string current_item = _last_line.substr(0,_last_line.find(",")); |
JimCarver | 1:7ad98913098c | 150 | _last_line = _last_line.substr(_last_line.find(",")+1); |
JimCarver | 1:7ad98913098c | 151 | if (i==0) { // NMEA Tag |
JimCarver | 1:7ad98913098c | 152 | } else if (i==1) { // UTC time |
JimCarver | 1:7ad98913098c | 153 | gps_zda.utc_time = strtod(current_item.c_str(), &pEnd); |
JimCarver | 1:7ad98913098c | 154 | } else if (i==2) { |
JimCarver | 1:7ad98913098c | 155 | gps_zda.day = strtod(current_item.c_str(), &pEnd); |
JimCarver | 1:7ad98913098c | 156 | } else if (i==3) { |
JimCarver | 1:7ad98913098c | 157 | gps_zda.month = strtod(current_item.c_str(), &pEnd); |
JimCarver | 1:7ad98913098c | 158 | } else if (i==4) { |
JimCarver | 1:7ad98913098c | 159 | gps_zda.year = strtod(current_item.c_str(), &pEnd); |
JimCarver | 1:7ad98913098c | 160 | } |
JimCarver | 1:7ad98913098c | 161 | } |
JimCarver | 1:7ad98913098c | 162 | gps_zda.new_flag = 1; |
michaelray | 0:56d6407653a7 | 163 | } |
michaelray | 0:56d6407653a7 | 164 | |
michaelray | 0:56d6407653a7 | 165 | void parseGGA() { |
michaelray | 0:56d6407653a7 | 166 | char* pEnd; |
michaelray | 0:56d6407653a7 | 167 | for (int i=0; i<14; i++) { |
michaelray | 0:56d6407653a7 | 168 | std::string current_item = _last_line.substr(0,_last_line.find(",")); |
michaelray | 0:56d6407653a7 | 169 | _last_line = _last_line.substr(_last_line.find(",")+1); |
michaelray | 0:56d6407653a7 | 170 | if (i==0) { // NMEA Tag |
michaelray | 0:56d6407653a7 | 171 | } else if (i==1) { // UTC time |
michaelray | 0:56d6407653a7 | 172 | gps_gga.utc_time = strtod(current_item.c_str(), &pEnd); |
michaelray | 0:56d6407653a7 | 173 | } else if (i==2) { // Latitude |
michaelray | 0:56d6407653a7 | 174 | gps_gga.latitude = strtod(current_item.c_str(), &pEnd); |
michaelray | 0:56d6407653a7 | 175 | } else if (i==3) { // Latitude North/South indicator |
michaelray | 0:56d6407653a7 | 176 | gps_gga.ns_indicator = current_item[0]; |
michaelray | 0:56d6407653a7 | 177 | } else if (i==4) { // Longitude |
michaelray | 0:56d6407653a7 | 178 | gps_gga.longitude = strtod(current_item.c_str(), &pEnd); |
michaelray | 0:56d6407653a7 | 179 | } else if (i==5) { // Longitude indicator |
michaelray | 0:56d6407653a7 | 180 | gps_gga.ew_indicator = current_item[0]; |
michaelray | 0:56d6407653a7 | 181 | } else if (i==6) { |
michaelray | 0:56d6407653a7 | 182 | gps_gga.position_fix = strtod(current_item.c_str(), &pEnd); |
JimCarver | 1:7ad98913098c | 183 | }else if (i==7) { |
JimCarver | 1:7ad98913098c | 184 | gps_gga.sats_used = strtod(current_item.c_str(), &pEnd); |
JimCarver | 1:7ad98913098c | 185 | } else if (i==8) { // Uncertanty |
JimCarver | 1:7ad98913098c | 186 | gps_gga.hdop = strtod(current_item.c_str(), &pEnd); |
JimCarver | 1:7ad98913098c | 187 | } else if (i==9) { // Altitude mean sea level |
JimCarver | 1:7ad98913098c | 188 | gps_gga.msl_altitude = strtod(current_item.c_str(), &pEnd); |
JimCarver | 1:7ad98913098c | 189 | } else if (i==11) { |
JimCarver | 1:7ad98913098c | 190 | gps_gga.geoid_separation = strtod(current_item.c_str(), &pEnd); |
JimCarver | 1:7ad98913098c | 191 | gps_gga.msl_altitude += gps_gga.geoid_separation; |
michaelray | 0:56d6407653a7 | 192 | } |
michaelray | 0:56d6407653a7 | 193 | } |
JimCarver | 1:7ad98913098c | 194 | gps_gga.new_flag = 1; |
michaelray | 0:56d6407653a7 | 195 | } |
JimCarver | 1:7ad98913098c | 196 | |
michaelray | 0:56d6407653a7 | 197 | }; |
michaelray | 0:56d6407653a7 | 198 | |
michaelray | 0:56d6407653a7 | 199 | #endif |