gps
MBed_Adafruit_GPS.cpp@1:42f840c832b6, 2020-01-23 (annotated)
- Committer:
- the_nabil
- Date:
- Thu Jan 23 18:08:12 2020 +0000
- Revision:
- 1:42f840c832b6
- Parent:
- 0:a23e3099bb0a
- Child:
- 2:90f2232bbdfd
gps
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mlee350 | 0:a23e3099bb0a | 1 | /*********************************** |
mlee350 | 0:a23e3099bb0a | 2 | This is our GPS library |
mlee350 | 0:a23e3099bb0a | 3 | |
mlee350 | 0:a23e3099bb0a | 4 | Adafruit invests time and resources providing this open source code, |
mlee350 | 0:a23e3099bb0a | 5 | please support Adafruit and open-source hardware by purchasing |
mlee350 | 0:a23e3099bb0a | 6 | products from Adafruit! |
mlee350 | 0:a23e3099bb0a | 7 | |
mlee350 | 0:a23e3099bb0a | 8 | Written by Limor Fried/Ladyada for Adafruit Industries. |
mlee350 | 0:a23e3099bb0a | 9 | BSD license, check license.txt for more information |
mlee350 | 0:a23e3099bb0a | 10 | All text above must be included in any redistribution |
mlee350 | 0:a23e3099bb0a | 11 | ****************************************/ |
mlee350 | 0:a23e3099bb0a | 12 | |
mlee350 | 0:a23e3099bb0a | 13 | #include "MBed_Adafruit_GPS.h" |
mlee350 | 0:a23e3099bb0a | 14 | |
the_nabil | 1:42f840c832b6 | 15 | #define M_PI 3.14159265358979323846 |
the_nabil | 1:42f840c832b6 | 16 | |
mlee350 | 0:a23e3099bb0a | 17 | // how long are max NMEA lines to parse? |
mlee350 | 0:a23e3099bb0a | 18 | #define MAXLINELENGTH 120 |
mlee350 | 0:a23e3099bb0a | 19 | |
mlee350 | 0:a23e3099bb0a | 20 | // we double buffer: read one line in and leave one for the main program |
mlee350 | 0:a23e3099bb0a | 21 | volatile char line1[MAXLINELENGTH]; |
mlee350 | 0:a23e3099bb0a | 22 | volatile char line2[MAXLINELENGTH]; |
mlee350 | 0:a23e3099bb0a | 23 | // our index into filling the current line |
mlee350 | 0:a23e3099bb0a | 24 | volatile uint16_t lineidx=0; |
mlee350 | 0:a23e3099bb0a | 25 | // pointers to the double buffers |
mlee350 | 0:a23e3099bb0a | 26 | volatile char *currentline; |
mlee350 | 0:a23e3099bb0a | 27 | volatile char *lastline; |
mlee350 | 0:a23e3099bb0a | 28 | volatile bool recvdflag; |
mlee350 | 0:a23e3099bb0a | 29 | volatile bool inStandbyMode; |
mlee350 | 0:a23e3099bb0a | 30 | |
mlee350 | 0:a23e3099bb0a | 31 | |
mlee350 | 0:a23e3099bb0a | 32 | bool Adafruit_GPS::parse(char *nmea) { |
mlee350 | 0:a23e3099bb0a | 33 | // do checksum check |
mlee350 | 0:a23e3099bb0a | 34 | |
mlee350 | 0:a23e3099bb0a | 35 | // first look if we even have one |
mlee350 | 0:a23e3099bb0a | 36 | if (nmea[strlen(nmea)-4] == '*') { |
mlee350 | 0:a23e3099bb0a | 37 | uint16_t sum = parseHex(nmea[strlen(nmea)-3]) * 16; |
mlee350 | 0:a23e3099bb0a | 38 | sum += parseHex(nmea[strlen(nmea)-2]); |
mlee350 | 0:a23e3099bb0a | 39 | |
mlee350 | 0:a23e3099bb0a | 40 | // check checksum |
mlee350 | 0:a23e3099bb0a | 41 | for (uint8_t i=1; i < (strlen(nmea)-4); i++) { |
mlee350 | 0:a23e3099bb0a | 42 | sum ^= nmea[i]; |
mlee350 | 0:a23e3099bb0a | 43 | } |
mlee350 | 0:a23e3099bb0a | 44 | if (sum != 0) { |
mlee350 | 0:a23e3099bb0a | 45 | // bad checksum :( |
mlee350 | 0:a23e3099bb0a | 46 | //return false; |
mlee350 | 0:a23e3099bb0a | 47 | } |
mlee350 | 0:a23e3099bb0a | 48 | } |
mlee350 | 0:a23e3099bb0a | 49 | |
mlee350 | 0:a23e3099bb0a | 50 | // look for a few common sentences |
mlee350 | 0:a23e3099bb0a | 51 | if (strstr(nmea, "$GPGGA")) { |
mlee350 | 0:a23e3099bb0a | 52 | // found GGA |
mlee350 | 0:a23e3099bb0a | 53 | char *p = nmea; |
mlee350 | 0:a23e3099bb0a | 54 | // get time |
mlee350 | 0:a23e3099bb0a | 55 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 56 | float timef = atof(p); |
mlee350 | 0:a23e3099bb0a | 57 | uint32_t time = timef; |
mlee350 | 0:a23e3099bb0a | 58 | hour = time / 10000; |
mlee350 | 0:a23e3099bb0a | 59 | minute = (time % 10000) / 100; |
mlee350 | 0:a23e3099bb0a | 60 | seconds = (time % 100); |
mlee350 | 0:a23e3099bb0a | 61 | |
mlee350 | 0:a23e3099bb0a | 62 | milliseconds = fmod((double) timef, 1.0) * 1000; |
mlee350 | 0:a23e3099bb0a | 63 | |
mlee350 | 0:a23e3099bb0a | 64 | // parse out latitude |
mlee350 | 0:a23e3099bb0a | 65 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 66 | latitude = atof(p); |
mlee350 | 0:a23e3099bb0a | 67 | |
mlee350 | 0:a23e3099bb0a | 68 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 69 | if (p[0] == 'N') lat = 'N'; |
mlee350 | 0:a23e3099bb0a | 70 | else if (p[0] == 'S') lat = 'S'; |
mlee350 | 0:a23e3099bb0a | 71 | else if (p[0] == ',') lat = 0; |
mlee350 | 0:a23e3099bb0a | 72 | else return false; |
mlee350 | 0:a23e3099bb0a | 73 | |
mlee350 | 0:a23e3099bb0a | 74 | // parse out longitude |
mlee350 | 0:a23e3099bb0a | 75 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 76 | longitude = atof(p); |
mlee350 | 0:a23e3099bb0a | 77 | |
mlee350 | 0:a23e3099bb0a | 78 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 79 | if (p[0] == 'W') lon = 'W'; |
mlee350 | 0:a23e3099bb0a | 80 | else if (p[0] == 'E') lon = 'E'; |
mlee350 | 0:a23e3099bb0a | 81 | else if (p[0] == ',') lon = 0; |
mlee350 | 0:a23e3099bb0a | 82 | else return false; |
mlee350 | 0:a23e3099bb0a | 83 | |
mlee350 | 0:a23e3099bb0a | 84 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 85 | fixquality = atoi(p); |
mlee350 | 0:a23e3099bb0a | 86 | |
mlee350 | 0:a23e3099bb0a | 87 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 88 | satellites = atoi(p); |
mlee350 | 0:a23e3099bb0a | 89 | |
mlee350 | 0:a23e3099bb0a | 90 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 91 | HDOP = atof(p); |
mlee350 | 0:a23e3099bb0a | 92 | |
mlee350 | 0:a23e3099bb0a | 93 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 94 | altitude = atof(p); |
mlee350 | 0:a23e3099bb0a | 95 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 96 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 97 | geoidheight = atof(p); |
mlee350 | 0:a23e3099bb0a | 98 | return true; |
mlee350 | 0:a23e3099bb0a | 99 | } |
mlee350 | 0:a23e3099bb0a | 100 | if (strstr(nmea, "$GPRMC")) { |
mlee350 | 0:a23e3099bb0a | 101 | // found RMC |
mlee350 | 0:a23e3099bb0a | 102 | char *p = nmea; |
mlee350 | 0:a23e3099bb0a | 103 | |
mlee350 | 0:a23e3099bb0a | 104 | // get time |
mlee350 | 0:a23e3099bb0a | 105 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 106 | float timef = atof(p); |
mlee350 | 0:a23e3099bb0a | 107 | uint32_t time = timef; |
mlee350 | 0:a23e3099bb0a | 108 | hour = time / 10000; |
mlee350 | 0:a23e3099bb0a | 109 | minute = (time % 10000) / 100; |
mlee350 | 0:a23e3099bb0a | 110 | seconds = (time % 100); |
mlee350 | 0:a23e3099bb0a | 111 | |
mlee350 | 0:a23e3099bb0a | 112 | milliseconds = fmod((double) timef, 1.0) * 1000; |
mlee350 | 0:a23e3099bb0a | 113 | |
mlee350 | 0:a23e3099bb0a | 114 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 115 | // Serial.println(p); |
mlee350 | 0:a23e3099bb0a | 116 | if (p[0] == 'A') |
mlee350 | 0:a23e3099bb0a | 117 | fix = true; |
mlee350 | 0:a23e3099bb0a | 118 | else if (p[0] == 'V') |
mlee350 | 0:a23e3099bb0a | 119 | fix = false; |
mlee350 | 0:a23e3099bb0a | 120 | else |
mlee350 | 0:a23e3099bb0a | 121 | return false; |
mlee350 | 0:a23e3099bb0a | 122 | |
mlee350 | 0:a23e3099bb0a | 123 | // parse out latitude |
mlee350 | 0:a23e3099bb0a | 124 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 125 | latitude = atof(p); |
mlee350 | 0:a23e3099bb0a | 126 | |
mlee350 | 0:a23e3099bb0a | 127 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 128 | if (p[0] == 'N') lat = 'N'; |
mlee350 | 0:a23e3099bb0a | 129 | else if (p[0] == 'S') lat = 'S'; |
mlee350 | 0:a23e3099bb0a | 130 | else if (p[0] == ',') lat = 0; |
mlee350 | 0:a23e3099bb0a | 131 | else return false; |
mlee350 | 0:a23e3099bb0a | 132 | |
mlee350 | 0:a23e3099bb0a | 133 | // parse out longitude |
mlee350 | 0:a23e3099bb0a | 134 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 135 | longitude = atof(p); |
mlee350 | 0:a23e3099bb0a | 136 | |
mlee350 | 0:a23e3099bb0a | 137 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 138 | if (p[0] == 'W') lon = 'W'; |
mlee350 | 0:a23e3099bb0a | 139 | else if (p[0] == 'E') lon = 'E'; |
mlee350 | 0:a23e3099bb0a | 140 | else if (p[0] == ',') lon = 0; |
mlee350 | 0:a23e3099bb0a | 141 | else return false; |
mlee350 | 0:a23e3099bb0a | 142 | |
mlee350 | 0:a23e3099bb0a | 143 | // speed |
mlee350 | 0:a23e3099bb0a | 144 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 145 | speed = atof(p); |
mlee350 | 0:a23e3099bb0a | 146 | |
mlee350 | 0:a23e3099bb0a | 147 | // angle |
mlee350 | 0:a23e3099bb0a | 148 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 149 | angle = atof(p); |
mlee350 | 0:a23e3099bb0a | 150 | |
mlee350 | 0:a23e3099bb0a | 151 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 152 | uint32_t fulldate = atof(p); |
mlee350 | 0:a23e3099bb0a | 153 | day = fulldate / 10000; |
mlee350 | 0:a23e3099bb0a | 154 | month = (fulldate % 10000) / 100; |
mlee350 | 0:a23e3099bb0a | 155 | year = (fulldate % 100); |
mlee350 | 0:a23e3099bb0a | 156 | |
mlee350 | 0:a23e3099bb0a | 157 | // we dont parse the remaining, yet! |
mlee350 | 0:a23e3099bb0a | 158 | return true; |
mlee350 | 0:a23e3099bb0a | 159 | } |
mlee350 | 0:a23e3099bb0a | 160 | |
mlee350 | 0:a23e3099bb0a | 161 | return false; |
mlee350 | 0:a23e3099bb0a | 162 | } |
mlee350 | 0:a23e3099bb0a | 163 | |
mlee350 | 0:a23e3099bb0a | 164 | char Adafruit_GPS::read(void) { |
mlee350 | 0:a23e3099bb0a | 165 | char c = 0; |
mlee350 | 0:a23e3099bb0a | 166 | |
mlee350 | 0:a23e3099bb0a | 167 | if (paused) return c; |
mlee350 | 0:a23e3099bb0a | 168 | |
mlee350 | 0:a23e3099bb0a | 169 | if(!gpsSerial->readable()) return c; |
mlee350 | 0:a23e3099bb0a | 170 | c = gpsSerial->getc(); |
mlee350 | 0:a23e3099bb0a | 171 | |
mlee350 | 0:a23e3099bb0a | 172 | //Serial.print(c); |
mlee350 | 0:a23e3099bb0a | 173 | |
mlee350 | 0:a23e3099bb0a | 174 | if (c == '$') { |
mlee350 | 0:a23e3099bb0a | 175 | currentline[lineidx] = 0; |
mlee350 | 0:a23e3099bb0a | 176 | lineidx = 0; |
mlee350 | 0:a23e3099bb0a | 177 | } |
mlee350 | 0:a23e3099bb0a | 178 | if (c == '\n') { |
mlee350 | 0:a23e3099bb0a | 179 | currentline[lineidx] = 0; |
mlee350 | 0:a23e3099bb0a | 180 | |
mlee350 | 0:a23e3099bb0a | 181 | if (currentline == line1) { |
mlee350 | 0:a23e3099bb0a | 182 | currentline = line2; |
mlee350 | 0:a23e3099bb0a | 183 | lastline = line1; |
mlee350 | 0:a23e3099bb0a | 184 | } else { |
mlee350 | 0:a23e3099bb0a | 185 | currentline = line1; |
mlee350 | 0:a23e3099bb0a | 186 | lastline = line2; |
mlee350 | 0:a23e3099bb0a | 187 | } |
mlee350 | 0:a23e3099bb0a | 188 | |
mlee350 | 0:a23e3099bb0a | 189 | lineidx = 0; |
mlee350 | 0:a23e3099bb0a | 190 | recvdflag = true; |
mlee350 | 0:a23e3099bb0a | 191 | } |
mlee350 | 0:a23e3099bb0a | 192 | |
mlee350 | 0:a23e3099bb0a | 193 | currentline[lineidx++] = c; |
mlee350 | 0:a23e3099bb0a | 194 | if (lineidx >= MAXLINELENGTH) |
mlee350 | 0:a23e3099bb0a | 195 | lineidx = MAXLINELENGTH-1; |
mlee350 | 0:a23e3099bb0a | 196 | |
mlee350 | 0:a23e3099bb0a | 197 | return c; |
mlee350 | 0:a23e3099bb0a | 198 | } |
mlee350 | 0:a23e3099bb0a | 199 | |
mlee350 | 0:a23e3099bb0a | 200 | Adafruit_GPS::Adafruit_GPS (Serial *ser) |
mlee350 | 0:a23e3099bb0a | 201 | { |
mlee350 | 0:a23e3099bb0a | 202 | common_init(); // Set everything to common state, then... |
mlee350 | 0:a23e3099bb0a | 203 | gpsSerial = ser; // ...override gpsSwSerial with value passed. |
mlee350 | 0:a23e3099bb0a | 204 | } |
mlee350 | 0:a23e3099bb0a | 205 | |
mlee350 | 0:a23e3099bb0a | 206 | // Initialization code used by all constructor types |
mlee350 | 0:a23e3099bb0a | 207 | void Adafruit_GPS::common_init(void) { |
mlee350 | 0:a23e3099bb0a | 208 | gpsSerial = NULL; |
mlee350 | 0:a23e3099bb0a | 209 | recvdflag = false; |
mlee350 | 0:a23e3099bb0a | 210 | paused = false; |
mlee350 | 0:a23e3099bb0a | 211 | lineidx = 0; |
mlee350 | 0:a23e3099bb0a | 212 | currentline = line1; |
mlee350 | 0:a23e3099bb0a | 213 | lastline = line2; |
mlee350 | 0:a23e3099bb0a | 214 | |
mlee350 | 0:a23e3099bb0a | 215 | hour = minute = seconds = year = month = day = |
mlee350 | 0:a23e3099bb0a | 216 | fixquality = satellites = 0; // uint8_t |
mlee350 | 0:a23e3099bb0a | 217 | lat = lon = mag = 0; // char |
mlee350 | 0:a23e3099bb0a | 218 | fix = false; // bool |
mlee350 | 0:a23e3099bb0a | 219 | milliseconds = 0; // uint16_t |
mlee350 | 0:a23e3099bb0a | 220 | latitude = longitude = geoidheight = altitude = |
mlee350 | 0:a23e3099bb0a | 221 | speed = angle = magvariation = HDOP = 0.0; // float |
mlee350 | 0:a23e3099bb0a | 222 | } |
mlee350 | 0:a23e3099bb0a | 223 | |
mlee350 | 0:a23e3099bb0a | 224 | void Adafruit_GPS::begin(int baud) |
mlee350 | 0:a23e3099bb0a | 225 | { |
mlee350 | 0:a23e3099bb0a | 226 | gpsSerial->baud(baud); |
mlee350 | 0:a23e3099bb0a | 227 | wait_ms(10); |
mlee350 | 0:a23e3099bb0a | 228 | } |
mlee350 | 0:a23e3099bb0a | 229 | |
mlee350 | 0:a23e3099bb0a | 230 | void Adafruit_GPS::sendCommand(char *str) { |
mlee350 | 0:a23e3099bb0a | 231 | gpsSerial->printf("%s",str); |
mlee350 | 0:a23e3099bb0a | 232 | } |
mlee350 | 0:a23e3099bb0a | 233 | |
mlee350 | 0:a23e3099bb0a | 234 | bool Adafruit_GPS::newNMEAreceived(void) { |
mlee350 | 0:a23e3099bb0a | 235 | return recvdflag; |
mlee350 | 0:a23e3099bb0a | 236 | } |
mlee350 | 0:a23e3099bb0a | 237 | |
mlee350 | 0:a23e3099bb0a | 238 | void Adafruit_GPS::pause(bool p) { |
mlee350 | 0:a23e3099bb0a | 239 | paused = p; |
mlee350 | 0:a23e3099bb0a | 240 | } |
mlee350 | 0:a23e3099bb0a | 241 | |
mlee350 | 0:a23e3099bb0a | 242 | char *Adafruit_GPS::lastNMEA(void) { |
mlee350 | 0:a23e3099bb0a | 243 | recvdflag = false; |
mlee350 | 0:a23e3099bb0a | 244 | return (char *)lastline; |
mlee350 | 0:a23e3099bb0a | 245 | } |
mlee350 | 0:a23e3099bb0a | 246 | |
mlee350 | 0:a23e3099bb0a | 247 | // read a Hex value and return the decimal equivalent |
mlee350 | 0:a23e3099bb0a | 248 | uint8_t Adafruit_GPS::parseHex(char c) { |
mlee350 | 0:a23e3099bb0a | 249 | if (c < '0') |
mlee350 | 0:a23e3099bb0a | 250 | return 0; |
mlee350 | 0:a23e3099bb0a | 251 | if (c <= '9') |
mlee350 | 0:a23e3099bb0a | 252 | return c - '0'; |
mlee350 | 0:a23e3099bb0a | 253 | if (c < 'A') |
mlee350 | 0:a23e3099bb0a | 254 | return 0; |
mlee350 | 0:a23e3099bb0a | 255 | if (c <= 'F') |
mlee350 | 0:a23e3099bb0a | 256 | return (c - 'A')+10; |
mlee350 | 0:a23e3099bb0a | 257 | } |
mlee350 | 0:a23e3099bb0a | 258 | |
mlee350 | 0:a23e3099bb0a | 259 | bool Adafruit_GPS::waitForSentence(char *wait4me, uint8_t max) { |
mlee350 | 0:a23e3099bb0a | 260 | char str[20]; |
mlee350 | 0:a23e3099bb0a | 261 | |
mlee350 | 0:a23e3099bb0a | 262 | uint8_t i=0; |
mlee350 | 0:a23e3099bb0a | 263 | while (i < max) { |
mlee350 | 0:a23e3099bb0a | 264 | if (newNMEAreceived()) { |
mlee350 | 0:a23e3099bb0a | 265 | char *nmea = lastNMEA(); |
mlee350 | 0:a23e3099bb0a | 266 | strncpy(str, nmea, 20); |
mlee350 | 0:a23e3099bb0a | 267 | str[19] = 0; |
mlee350 | 0:a23e3099bb0a | 268 | i++; |
mlee350 | 0:a23e3099bb0a | 269 | |
mlee350 | 0:a23e3099bb0a | 270 | if (strstr(str, wait4me)) |
mlee350 | 0:a23e3099bb0a | 271 | return true; |
mlee350 | 0:a23e3099bb0a | 272 | } |
mlee350 | 0:a23e3099bb0a | 273 | } |
mlee350 | 0:a23e3099bb0a | 274 | |
mlee350 | 0:a23e3099bb0a | 275 | return false; |
mlee350 | 0:a23e3099bb0a | 276 | } |
mlee350 | 0:a23e3099bb0a | 277 | |
mlee350 | 0:a23e3099bb0a | 278 | bool Adafruit_GPS::LOCUS_StartLogger(void) { |
mlee350 | 0:a23e3099bb0a | 279 | sendCommand(PMTK_LOCUS_STARTLOG); |
mlee350 | 0:a23e3099bb0a | 280 | recvdflag = false; |
mlee350 | 0:a23e3099bb0a | 281 | return waitForSentence(PMTK_LOCUS_LOGSTARTED); |
mlee350 | 0:a23e3099bb0a | 282 | } |
mlee350 | 0:a23e3099bb0a | 283 | |
mlee350 | 0:a23e3099bb0a | 284 | bool Adafruit_GPS::LOCUS_ReadStatus(void) { |
mlee350 | 0:a23e3099bb0a | 285 | sendCommand(PMTK_LOCUS_QUERY_STATUS); |
mlee350 | 0:a23e3099bb0a | 286 | |
mlee350 | 0:a23e3099bb0a | 287 | if (! waitForSentence("$PMTKLOG")) |
mlee350 | 0:a23e3099bb0a | 288 | return false; |
mlee350 | 0:a23e3099bb0a | 289 | |
mlee350 | 0:a23e3099bb0a | 290 | char *response = lastNMEA(); |
mlee350 | 0:a23e3099bb0a | 291 | uint16_t parsed[10]; |
mlee350 | 0:a23e3099bb0a | 292 | uint8_t i; |
mlee350 | 0:a23e3099bb0a | 293 | |
mlee350 | 0:a23e3099bb0a | 294 | for (i=0; i<10; i++) parsed[i] = -1; |
mlee350 | 0:a23e3099bb0a | 295 | |
mlee350 | 0:a23e3099bb0a | 296 | response = strchr(response, ','); |
mlee350 | 0:a23e3099bb0a | 297 | for (i=0; i<10; i++) { |
mlee350 | 0:a23e3099bb0a | 298 | if (!response || (response[0] == 0) || (response[0] == '*')) |
mlee350 | 0:a23e3099bb0a | 299 | break; |
mlee350 | 0:a23e3099bb0a | 300 | response++; |
mlee350 | 0:a23e3099bb0a | 301 | parsed[i]=0; |
mlee350 | 0:a23e3099bb0a | 302 | while ((response[0] != ',') && |
mlee350 | 0:a23e3099bb0a | 303 | (response[0] != '*') && (response[0] != 0)) { |
mlee350 | 0:a23e3099bb0a | 304 | parsed[i] *= 10; |
mlee350 | 0:a23e3099bb0a | 305 | char c = response[0]; |
mlee350 | 0:a23e3099bb0a | 306 | if (isdigit(c)) |
mlee350 | 0:a23e3099bb0a | 307 | parsed[i] += c - '0'; |
mlee350 | 0:a23e3099bb0a | 308 | else |
mlee350 | 0:a23e3099bb0a | 309 | parsed[i] = c; |
mlee350 | 0:a23e3099bb0a | 310 | response++; |
mlee350 | 0:a23e3099bb0a | 311 | } |
mlee350 | 0:a23e3099bb0a | 312 | } |
mlee350 | 0:a23e3099bb0a | 313 | LOCUS_serial = parsed[0]; |
mlee350 | 0:a23e3099bb0a | 314 | LOCUS_type = parsed[1]; |
mlee350 | 0:a23e3099bb0a | 315 | if (isalpha(parsed[2])) { |
mlee350 | 0:a23e3099bb0a | 316 | parsed[2] = parsed[2] - 'a' + 10; |
mlee350 | 0:a23e3099bb0a | 317 | } |
mlee350 | 0:a23e3099bb0a | 318 | LOCUS_mode = parsed[2]; |
mlee350 | 0:a23e3099bb0a | 319 | LOCUS_config = parsed[3]; |
mlee350 | 0:a23e3099bb0a | 320 | LOCUS_interval = parsed[4]; |
mlee350 | 0:a23e3099bb0a | 321 | LOCUS_distance = parsed[5]; |
mlee350 | 0:a23e3099bb0a | 322 | LOCUS_speed = parsed[6]; |
mlee350 | 0:a23e3099bb0a | 323 | LOCUS_status = !parsed[7]; |
mlee350 | 0:a23e3099bb0a | 324 | LOCUS_records = parsed[8]; |
mlee350 | 0:a23e3099bb0a | 325 | LOCUS_percent = parsed[9]; |
mlee350 | 0:a23e3099bb0a | 326 | |
mlee350 | 0:a23e3099bb0a | 327 | return true; |
mlee350 | 0:a23e3099bb0a | 328 | } |
mlee350 | 0:a23e3099bb0a | 329 | |
mlee350 | 0:a23e3099bb0a | 330 | // Standby Mode Switches |
mlee350 | 0:a23e3099bb0a | 331 | bool Adafruit_GPS::standby(void) { |
mlee350 | 0:a23e3099bb0a | 332 | if (inStandbyMode) { |
mlee350 | 0:a23e3099bb0a | 333 | return false; // Returns false if already in standby mode, so that you do not wake it up by sending commands to GPS |
mlee350 | 0:a23e3099bb0a | 334 | } |
mlee350 | 0:a23e3099bb0a | 335 | else { |
mlee350 | 0:a23e3099bb0a | 336 | inStandbyMode = true; |
mlee350 | 0:a23e3099bb0a | 337 | sendCommand(PMTK_STANDBY); |
mlee350 | 0:a23e3099bb0a | 338 | //return waitForSentence(PMTK_STANDBY_SUCCESS); // don't seem to be fast enough to catch the message, or something else just is not working |
mlee350 | 0:a23e3099bb0a | 339 | return true; |
mlee350 | 0:a23e3099bb0a | 340 | } |
mlee350 | 0:a23e3099bb0a | 341 | } |
mlee350 | 0:a23e3099bb0a | 342 | |
mlee350 | 0:a23e3099bb0a | 343 | bool Adafruit_GPS::wakeup(void) { |
mlee350 | 0:a23e3099bb0a | 344 | if (inStandbyMode) { |
mlee350 | 0:a23e3099bb0a | 345 | inStandbyMode = false; |
mlee350 | 0:a23e3099bb0a | 346 | sendCommand(""); // send byte to wake it up |
mlee350 | 0:a23e3099bb0a | 347 | return waitForSentence(PMTK_AWAKE); |
mlee350 | 0:a23e3099bb0a | 348 | } |
mlee350 | 0:a23e3099bb0a | 349 | else { |
mlee350 | 0:a23e3099bb0a | 350 | return false; // Returns false if not in standby mode, nothing to wakeup |
mlee350 | 0:a23e3099bb0a | 351 | } |
the_nabil | 1:42f840c832b6 | 352 | } |
the_nabil | 1:42f840c832b6 | 353 | |
the_nabil | 1:42f840c832b6 | 354 | float Adafruit_GPS::coordToDegDec(float coord) { |
the_nabil | 1:42f840c832b6 | 355 | float dd, ss, codd; |
the_nabil | 1:42f840c832b6 | 356 | |
the_nabil | 1:42f840c832b6 | 357 | dd = (int)(coord/100); |
the_nabil | 1:42f840c832b6 | 358 | dd = (float)dd; |
the_nabil | 1:42f840c832b6 | 359 | ss = coord - dd*100; |
the_nabil | 1:42f840c832b6 | 360 | codd = dd + (ss/60); |
the_nabil | 1:42f840c832b6 | 361 | |
the_nabil | 1:42f840c832b6 | 362 | return codd; |
the_nabil | 1:42f840c832b6 | 363 | } |
the_nabil | 1:42f840c832b6 | 364 | |
the_nabil | 1:42f840c832b6 | 365 | float Adafruit_GPS::getDistance(float lat_1, float lon_1, float lat_2, float lon_2) { |
the_nabil | 1:42f840c832b6 | 366 | float dlat; |
the_nabil | 1:42f840c832b6 | 367 | float dlon; |
the_nabil | 1:42f840c832b6 | 368 | float a, c, d; |
the_nabil | 1:42f840c832b6 | 369 | float R = 6371000; |
the_nabil | 1:42f840c832b6 | 370 | //char u[] = "km"; |
the_nabil | 1:42f840c832b6 | 371 | |
the_nabil | 1:42f840c832b6 | 372 | dlat = (lat_2-lat_1)/100; |
the_nabil | 1:42f840c832b6 | 373 | dlon = (lon_2-lon_1)/100; |
the_nabil | 1:42f840c832b6 | 374 | |
the_nabil | 1:42f840c832b6 | 375 | dlat = dlat * (M_PI/180); |
the_nabil | 1:42f840c832b6 | 376 | dlon = dlon * (M_PI/180); |
the_nabil | 1:42f840c832b6 | 377 | |
the_nabil | 1:42f840c832b6 | 378 | lat_1 = (lat_1/100) * (M_PI/180); |
the_nabil | 1:42f840c832b6 | 379 | lat_2 = (lat_2/100) * (M_PI/180); |
the_nabil | 1:42f840c832b6 | 380 | |
the_nabil | 1:42f840c832b6 | 381 | a = pow((sin(dlat/2)),2)+cos(lat_1)*cos(lat_2)*pow((sin(dlon/2)),2); |
the_nabil | 1:42f840c832b6 | 382 | c = 2*atan2((sqrt(a)),(sqrt(1-a))); |
the_nabil | 1:42f840c832b6 | 383 | d = R*c; |
the_nabil | 1:42f840c832b6 | 384 | |
the_nabil | 1:42f840c832b6 | 385 | //if (R<1) { |
the_nabil | 1:42f840c832b6 | 386 | // u = "m" |
the_nabil | 1:42f840c832b6 | 387 | // } |
the_nabil | 1:42f840c832b6 | 388 | |
the_nabil | 1:42f840c832b6 | 389 | return d; |
the_nabil | 1:42f840c832b6 | 390 | } |
the_nabil | 1:42f840c832b6 | 391 | |
the_nabil | 1:42f840c832b6 | 392 | float Adafruit_GPS::getSteps(float distance, float height) { |
the_nabil | 1:42f840c832b6 | 393 | float steps, avg_steps; |
the_nabil | 1:42f840c832b6 | 394 | |
the_nabil | 1:42f840c832b6 | 395 | avg_steps = 0.413*(height/100); |
the_nabil | 1:42f840c832b6 | 396 | steps = distance/avg_steps; |
the_nabil | 1:42f840c832b6 | 397 | |
the_nabil | 1:42f840c832b6 | 398 | return steps; |
the_nabil | 1:42f840c832b6 | 399 | } |
the_nabil | 1:42f840c832b6 | 400 | |
the_nabil | 1:42f840c832b6 | 401 | float Adafruit_GPS::getAvgSpeed(float distance, int t) { |
the_nabil | 1:42f840c832b6 | 402 | float avg_speed; |
the_nabil | 1:42f840c832b6 | 403 | |
the_nabil | 1:42f840c832b6 | 404 | t = (float) t; |
the_nabil | 1:42f840c832b6 | 405 | |
the_nabil | 1:42f840c832b6 | 406 | avg_speed = (distance/1000)/(t/3600); |
the_nabil | 1:42f840c832b6 | 407 | |
the_nabil | 1:42f840c832b6 | 408 | return avg_speed; |
the_nabil | 1:42f840c832b6 | 409 | } |
the_nabil | 1:42f840c832b6 | 410 | |
the_nabil | 1:42f840c832b6 | 411 | /* |
the_nabil | 1:42f840c832b6 | 412 | float toRadians(float deg) { |
the_nabil | 1:42f840c832b6 | 413 | float rad; |
the_nabil | 1:42f840c832b6 | 414 | |
the_nabil | 1:42f840c832b6 | 415 | rad = deg * (M_PI/180); |
the_nabil | 1:42f840c832b6 | 416 | |
the_nabil | 1:42f840c832b6 | 417 | return rad; |
the_nabil | 1:42f840c832b6 | 418 | } |
the_nabil | 1:42f840c832b6 | 419 | */ |
the_nabil | 1:42f840c832b6 | 420 |