Final version of GPS library. Still may require some debugging regarding integer formatting, but it will be easiest to do this from an external program that is grabbing the values (the time and lat/long values in particular should be tested).
GPS.cpp@1:868c31bace68, 2015-10-17 (annotated)
- Committer:
- dhawkes
- Date:
- Sat Oct 17 23:12:26 2015 +0000
- Revision:
- 1:868c31bace68
- Parent:
- 0:8d2eedbb4c6f
See previous message - I just took some leftover begun code out.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dhawkes | 0:8d2eedbb4c6f | 1 | #include "GPS.h" |
dhawkes | 0:8d2eedbb4c6f | 2 | |
dhawkes | 0:8d2eedbb4c6f | 3 | MODSERIAL pc(P0_2, P0_3); // tx, rx |
dhawkes | 0:8d2eedbb4c6f | 4 | MODSERIAL gps(P4_28, P4_29); // tx, rx |
dhawkes | 0:8d2eedbb4c6f | 5 | |
dhawkes | 0:8d2eedbb4c6f | 6 | bool first = true; |
dhawkes | 0:8d2eedbb4c6f | 7 | bool firstTime = true; |
dhawkes | 0:8d2eedbb4c6f | 8 | bool filling = false; |
dhawkes | 0:8d2eedbb4c6f | 9 | |
dhawkes | 0:8d2eedbb4c6f | 10 | double iniTime; |
dhawkes | 0:8d2eedbb4c6f | 11 | |
dhawkes | 0:8d2eedbb4c6f | 12 | int32_t data[7]; |
dhawkes | 0:8d2eedbb4c6f | 13 | int16_t miliseconds; |
dhawkes | 0:8d2eedbb4c6f | 14 | int32_t currentTime; |
dhawkes | 0:8d2eedbb4c6f | 15 | |
dhawkes | 0:8d2eedbb4c6f | 16 | int fillIndex = 0; |
dhawkes | 0:8d2eedbb4c6f | 17 | |
dhawkes | 0:8d2eedbb4c6f | 18 | const int MAX = 82; |
dhawkes | 0:8d2eedbb4c6f | 19 | char buffer[MAX]; |
dhawkes | 0:8d2eedbb4c6f | 20 | |
dhawkes | 0:8d2eedbb4c6f | 21 | int index = 0; |
dhawkes | 0:8d2eedbb4c6f | 22 | |
dhawkes | 0:8d2eedbb4c6f | 23 | /* Relevant Data being Gathered |
dhawkes | 0:8d2eedbb4c6f | 24 | * |
dhawkes | 0:8d2eedbb4c6f | 25 | * INDEX NAME FORMAT NMEA STRING |
dhawkes | 0:8d2eedbb4c6f | 26 | * 0 Latitude ddmm.mmmm GGA |
dhawkes | 0:8d2eedbb4c6f | 27 | * 1 Longitude dddmm.mmmm GGA |
dhawkes | 0:8d2eedbb4c6f | 28 | * 2 GPS Quality x GGA |
dhawkes | 0:8d2eedbb4c6f | 29 | * 3 Satellites Used xx GGA |
dhawkes | 0:8d2eedbb4c6f | 30 | * 4 Altitude x.x GGA |
dhawkes | 0:8d2eedbb4c6f | 31 | * 5 Course x.x VTG |
dhawkes | 0:8d2eedbb4c6f | 32 | * 6 Speed in km/h x.x VTG |
dhawkes | 0:8d2eedbb4c6f | 33 | */ |
dhawkes | 0:8d2eedbb4c6f | 34 | |
dhawkes | 0:8d2eedbb4c6f | 35 | void move(char* source, char* destination, char delim) |
dhawkes | 0:8d2eedbb4c6f | 36 | { |
dhawkes | 0:8d2eedbb4c6f | 37 | int k = 0; |
dhawkes | 0:8d2eedbb4c6f | 38 | for(;index < 10000; index++) |
dhawkes | 0:8d2eedbb4c6f | 39 | { |
dhawkes | 0:8d2eedbb4c6f | 40 | if(source[index] == delim) |
dhawkes | 0:8d2eedbb4c6f | 41 | { |
dhawkes | 0:8d2eedbb4c6f | 42 | break; |
dhawkes | 0:8d2eedbb4c6f | 43 | } |
dhawkes | 0:8d2eedbb4c6f | 44 | destination[k] = source[index]; |
dhawkes | 0:8d2eedbb4c6f | 45 | //pc.putc(source[index]); |
dhawkes | 0:8d2eedbb4c6f | 46 | k++; |
dhawkes | 0:8d2eedbb4c6f | 47 | } |
dhawkes | 0:8d2eedbb4c6f | 48 | destination[k] = NULL; |
dhawkes | 0:8d2eedbb4c6f | 49 | } |
dhawkes | 0:8d2eedbb4c6f | 50 | |
dhawkes | 0:8d2eedbb4c6f | 51 | void move(char* source, char* destination, int num) |
dhawkes | 0:8d2eedbb4c6f | 52 | { |
dhawkes | 0:8d2eedbb4c6f | 53 | int k = 0; |
dhawkes | 0:8d2eedbb4c6f | 54 | for(;index < index + num; index++) |
dhawkes | 0:8d2eedbb4c6f | 55 | { |
dhawkes | 0:8d2eedbb4c6f | 56 | destination[k] = source[index]; |
dhawkes | 0:8d2eedbb4c6f | 57 | k++; |
dhawkes | 0:8d2eedbb4c6f | 58 | } |
dhawkes | 0:8d2eedbb4c6f | 59 | destination[k] = NULL; |
dhawkes | 0:8d2eedbb4c6f | 60 | } |
dhawkes | 0:8d2eedbb4c6f | 61 | |
dhawkes | 0:8d2eedbb4c6f | 62 | |
dhawkes | 0:8d2eedbb4c6f | 63 | |
dhawkes | 0:8d2eedbb4c6f | 64 | // Returns the first time value recorded as a double |
dhawkes | 0:8d2eedbb4c6f | 65 | double getInitialTime() |
dhawkes | 0:8d2eedbb4c6f | 66 | { |
dhawkes | 0:8d2eedbb4c6f | 67 | return iniTime; |
dhawkes | 0:8d2eedbb4c6f | 68 | } |
dhawkes | 0:8d2eedbb4c6f | 69 | |
dhawkes | 0:8d2eedbb4c6f | 70 | // Return the time (without miliseconds) relative to the initial time |
dhawkes | 0:8d2eedbb4c6f | 71 | int32_t getTime() |
dhawkes | 0:8d2eedbb4c6f | 72 | { |
dhawkes | 0:8d2eedbb4c6f | 73 | return currentTime; |
dhawkes | 0:8d2eedbb4c6f | 74 | } |
dhawkes | 0:8d2eedbb4c6f | 75 | |
dhawkes | 0:8d2eedbb4c6f | 76 | // Return just the miliseconds |
dhawkes | 0:8d2eedbb4c6f | 77 | int16_t getMiliseconds() |
dhawkes | 0:8d2eedbb4c6f | 78 | { |
dhawkes | 0:8d2eedbb4c6f | 79 | return miliseconds; |
dhawkes | 0:8d2eedbb4c6f | 80 | } |
dhawkes | 0:8d2eedbb4c6f | 81 | |
dhawkes | 0:8d2eedbb4c6f | 82 | // returns everything except the time |
dhawkes | 0:8d2eedbb4c6f | 83 | int32_t* getGPSData() |
dhawkes | 0:8d2eedbb4c6f | 84 | { |
dhawkes | 0:8d2eedbb4c6f | 85 | int32_t * dataPointer = data; |
dhawkes | 0:8d2eedbb4c6f | 86 | return dataPointer; |
dhawkes | 0:8d2eedbb4c6f | 87 | } |
dhawkes | 0:8d2eedbb4c6f | 88 | |
dhawkes | 0:8d2eedbb4c6f | 89 | int32_t getLatitude() |
dhawkes | 0:8d2eedbb4c6f | 90 | { |
dhawkes | 0:8d2eedbb4c6f | 91 | return data[0]; |
dhawkes | 0:8d2eedbb4c6f | 92 | } |
dhawkes | 0:8d2eedbb4c6f | 93 | |
dhawkes | 0:8d2eedbb4c6f | 94 | int32_t getLongitude() |
dhawkes | 0:8d2eedbb4c6f | 95 | { |
dhawkes | 0:8d2eedbb4c6f | 96 | return data[1]; |
dhawkes | 0:8d2eedbb4c6f | 97 | } |
dhawkes | 0:8d2eedbb4c6f | 98 | |
dhawkes | 0:8d2eedbb4c6f | 99 | int32_t getGPSQuality() |
dhawkes | 0:8d2eedbb4c6f | 100 | { |
dhawkes | 0:8d2eedbb4c6f | 101 | return data[2]; |
dhawkes | 0:8d2eedbb4c6f | 102 | } |
dhawkes | 0:8d2eedbb4c6f | 103 | |
dhawkes | 0:8d2eedbb4c6f | 104 | int32_t getSatellitesUsed() |
dhawkes | 0:8d2eedbb4c6f | 105 | { |
dhawkes | 0:8d2eedbb4c6f | 106 | return data[3]; |
dhawkes | 0:8d2eedbb4c6f | 107 | } |
dhawkes | 0:8d2eedbb4c6f | 108 | |
dhawkes | 0:8d2eedbb4c6f | 109 | int32_t getAltitude() |
dhawkes | 0:8d2eedbb4c6f | 110 | { |
dhawkes | 0:8d2eedbb4c6f | 111 | return data[4]; |
dhawkes | 0:8d2eedbb4c6f | 112 | } |
dhawkes | 0:8d2eedbb4c6f | 113 | |
dhawkes | 0:8d2eedbb4c6f | 114 | int32_t getCourse() |
dhawkes | 0:8d2eedbb4c6f | 115 | { |
dhawkes | 0:8d2eedbb4c6f | 116 | return data[5]; |
dhawkes | 0:8d2eedbb4c6f | 117 | } |
dhawkes | 0:8d2eedbb4c6f | 118 | |
dhawkes | 0:8d2eedbb4c6f | 119 | int32_t getSpeed() |
dhawkes | 0:8d2eedbb4c6f | 120 | { |
dhawkes | 0:8d2eedbb4c6f | 121 | return data[6]; |
dhawkes | 0:8d2eedbb4c6f | 122 | } |
dhawkes | 0:8d2eedbb4c6f | 123 | |
dhawkes | 0:8d2eedbb4c6f | 124 | |
dhawkes | 0:8d2eedbb4c6f | 125 | void parse() |
dhawkes | 0:8d2eedbb4c6f | 126 | { |
dhawkes | 0:8d2eedbb4c6f | 127 | double t, t2; |
dhawkes | 0:8d2eedbb4c6f | 128 | index = 1; |
dhawkes | 0:8d2eedbb4c6f | 129 | char dat[MAX + 1]; |
dhawkes | 0:8d2eedbb4c6f | 130 | move(buffer, dat, ','); |
dhawkes | 0:8d2eedbb4c6f | 131 | index++; |
dhawkes | 0:8d2eedbb4c6f | 132 | if(strcmp(dat, "GPGGA") == 0) // Is it the GGA string? |
dhawkes | 0:8d2eedbb4c6f | 133 | { |
dhawkes | 0:8d2eedbb4c6f | 134 | move(buffer, dat, ','); |
dhawkes | 0:8d2eedbb4c6f | 135 | t = strtod(dat, NULL); |
dhawkes | 0:8d2eedbb4c6f | 136 | if(firstTime) |
dhawkes | 0:8d2eedbb4c6f | 137 | { |
dhawkes | 0:8d2eedbb4c6f | 138 | firstTime = false; |
dhawkes | 0:8d2eedbb4c6f | 139 | iniTime = t; |
dhawkes | 0:8d2eedbb4c6f | 140 | } |
dhawkes | 0:8d2eedbb4c6f | 141 | |
dhawkes | 0:8d2eedbb4c6f | 142 | t = t - iniTime; |
dhawkes | 0:8d2eedbb4c6f | 143 | t2 = floor(t); |
dhawkes | 0:8d2eedbb4c6f | 144 | currentTime = t2; |
dhawkes | 0:8d2eedbb4c6f | 145 | miliseconds = (int16_t)((t - t2) * 1000); |
dhawkes | 0:8d2eedbb4c6f | 146 | |
dhawkes | 0:8d2eedbb4c6f | 147 | |
dhawkes | 0:8d2eedbb4c6f | 148 | index++; |
dhawkes | 0:8d2eedbb4c6f | 149 | |
dhawkes | 0:8d2eedbb4c6f | 150 | // latitude |
dhawkes | 0:8d2eedbb4c6f | 151 | char temp[2]; |
dhawkes | 0:8d2eedbb4c6f | 152 | temp[0] = buffer[index]; |
dhawkes | 0:8d2eedbb4c6f | 153 | index++; |
dhawkes | 0:8d2eedbb4c6f | 154 | temp[1] = buffer[index]; |
dhawkes | 0:8d2eedbb4c6f | 155 | index++; |
dhawkes | 0:8d2eedbb4c6f | 156 | float total = 0; |
dhawkes | 0:8d2eedbb4c6f | 157 | total = 60 * atof(temp); |
dhawkes | 0:8d2eedbb4c6f | 158 | move(buffer, dat, ','); |
dhawkes | 0:8d2eedbb4c6f | 159 | data[1] = (int32_t)((atof(dat)+total) * 10000); |
dhawkes | 0:8d2eedbb4c6f | 160 | index += 2; |
dhawkes | 0:8d2eedbb4c6f | 161 | if(buffer[index] == 'S') |
dhawkes | 0:8d2eedbb4c6f | 162 | { |
dhawkes | 0:8d2eedbb4c6f | 163 | data[1] *= -1; |
dhawkes | 0:8d2eedbb4c6f | 164 | } |
dhawkes | 0:8d2eedbb4c6f | 165 | |
dhawkes | 0:8d2eedbb4c6f | 166 | index++; |
dhawkes | 0:8d2eedbb4c6f | 167 | |
dhawkes | 0:8d2eedbb4c6f | 168 | // longitude |
dhawkes | 0:8d2eedbb4c6f | 169 | char temp2[3]; |
dhawkes | 0:8d2eedbb4c6f | 170 | temp2[0] = buffer[index]; |
dhawkes | 0:8d2eedbb4c6f | 171 | index++; |
dhawkes | 0:8d2eedbb4c6f | 172 | temp2[1] = buffer[index]; |
dhawkes | 0:8d2eedbb4c6f | 173 | index++; |
dhawkes | 0:8d2eedbb4c6f | 174 | temp2[2] = buffer[index]; |
dhawkes | 0:8d2eedbb4c6f | 175 | index++; |
dhawkes | 0:8d2eedbb4c6f | 176 | |
dhawkes | 0:8d2eedbb4c6f | 177 | total = 60 * atof(temp2); |
dhawkes | 0:8d2eedbb4c6f | 178 | move(buffer, dat, ','); |
dhawkes | 0:8d2eedbb4c6f | 179 | data[2] = (int32_t)((atof(dat)+total) * 10000); |
dhawkes | 0:8d2eedbb4c6f | 180 | index += 2; |
dhawkes | 0:8d2eedbb4c6f | 181 | if(buffer[index] == 'W') |
dhawkes | 0:8d2eedbb4c6f | 182 | { |
dhawkes | 0:8d2eedbb4c6f | 183 | data[1] *= -1; |
dhawkes | 0:8d2eedbb4c6f | 184 | } |
dhawkes | 0:8d2eedbb4c6f | 185 | index++; |
dhawkes | 0:8d2eedbb4c6f | 186 | |
dhawkes | 0:8d2eedbb4c6f | 187 | // quality |
dhawkes | 0:8d2eedbb4c6f | 188 | move(buffer, dat, ','); |
dhawkes | 0:8d2eedbb4c6f | 189 | data[2] = atoi(dat); |
dhawkes | 0:8d2eedbb4c6f | 190 | |
dhawkes | 0:8d2eedbb4c6f | 191 | index++; |
dhawkes | 0:8d2eedbb4c6f | 192 | |
dhawkes | 0:8d2eedbb4c6f | 193 | // satellites |
dhawkes | 0:8d2eedbb4c6f | 194 | move(buffer, dat, ','); |
dhawkes | 0:8d2eedbb4c6f | 195 | data[3] = atoi(dat); |
dhawkes | 0:8d2eedbb4c6f | 196 | |
dhawkes | 0:8d2eedbb4c6f | 197 | index++; |
dhawkes | 0:8d2eedbb4c6f | 198 | move(buffer, dat, ','); |
dhawkes | 0:8d2eedbb4c6f | 199 | |
dhawkes | 0:8d2eedbb4c6f | 200 | |
dhawkes | 0:8d2eedbb4c6f | 201 | // altitude |
dhawkes | 0:8d2eedbb4c6f | 202 | move(buffer, dat, ','); |
dhawkes | 0:8d2eedbb4c6f | 203 | data[4] = (int32_t)(atof(dat) * 10); |
dhawkes | 0:8d2eedbb4c6f | 204 | } |
dhawkes | 0:8d2eedbb4c6f | 205 | else if(strcmp(dat, "GPVTG") == 0) // Is it VTG? |
dhawkes | 0:8d2eedbb4c6f | 206 | { |
dhawkes | 0:8d2eedbb4c6f | 207 | // course |
dhawkes | 0:8d2eedbb4c6f | 208 | move(buffer, dat, ','); |
dhawkes | 0:8d2eedbb4c6f | 209 | |
dhawkes | 0:8d2eedbb4c6f | 210 | data[5] = (int32_t)(atof(dat) * 10); |
dhawkes | 0:8d2eedbb4c6f | 211 | |
dhawkes | 0:8d2eedbb4c6f | 212 | index++; |
dhawkes | 0:8d2eedbb4c6f | 213 | for(int i = 0; i < 5; i++) |
dhawkes | 0:8d2eedbb4c6f | 214 | { |
dhawkes | 0:8d2eedbb4c6f | 215 | move(buffer, dat, ','); |
dhawkes | 0:8d2eedbb4c6f | 216 | index++; |
dhawkes | 0:8d2eedbb4c6f | 217 | } |
dhawkes | 0:8d2eedbb4c6f | 218 | // speed (km/h) |
dhawkes | 0:8d2eedbb4c6f | 219 | move(buffer, dat, ','); |
dhawkes | 0:8d2eedbb4c6f | 220 | data[6] = (int32_t)(atof(dat) * 10); |
dhawkes | 0:8d2eedbb4c6f | 221 | |
dhawkes | 0:8d2eedbb4c6f | 222 | } |
dhawkes | 0:8d2eedbb4c6f | 223 | } |
dhawkes | 0:8d2eedbb4c6f | 224 | |
dhawkes | 0:8d2eedbb4c6f | 225 | bool updateValues() |
dhawkes | 0:8d2eedbb4c6f | 226 | { |
dhawkes | 0:8d2eedbb4c6f | 227 | signed char c = gps.getcNb(); |
dhawkes | 0:8d2eedbb4c6f | 228 | |
dhawkes | 0:8d2eedbb4c6f | 229 | while(c != -1) |
dhawkes | 0:8d2eedbb4c6f | 230 | { |
dhawkes | 0:8d2eedbb4c6f | 231 | if(!filling) |
dhawkes | 0:8d2eedbb4c6f | 232 | { |
dhawkes | 0:8d2eedbb4c6f | 233 | if(c == '$') |
dhawkes | 0:8d2eedbb4c6f | 234 | { |
dhawkes | 0:8d2eedbb4c6f | 235 | filling = true; |
dhawkes | 0:8d2eedbb4c6f | 236 | buffer[0] = c; |
dhawkes | 0:8d2eedbb4c6f | 237 | fillIndex = 1; |
dhawkes | 0:8d2eedbb4c6f | 238 | } |
dhawkes | 0:8d2eedbb4c6f | 239 | } |
dhawkes | 0:8d2eedbb4c6f | 240 | else |
dhawkes | 0:8d2eedbb4c6f | 241 | { |
dhawkes | 0:8d2eedbb4c6f | 242 | if(c == '\n') |
dhawkes | 0:8d2eedbb4c6f | 243 | { |
dhawkes | 0:8d2eedbb4c6f | 244 | buffer[fillIndex] = c; |
dhawkes | 0:8d2eedbb4c6f | 245 | fillIndex++; |
dhawkes | 0:8d2eedbb4c6f | 246 | filling = false; |
dhawkes | 0:8d2eedbb4c6f | 247 | parse(); |
dhawkes | 0:8d2eedbb4c6f | 248 | } |
dhawkes | 0:8d2eedbb4c6f | 249 | else |
dhawkes | 0:8d2eedbb4c6f | 250 | { |
dhawkes | 0:8d2eedbb4c6f | 251 | buffer[fillIndex] = c; |
dhawkes | 0:8d2eedbb4c6f | 252 | fillIndex++; |
dhawkes | 0:8d2eedbb4c6f | 253 | } |
dhawkes | 0:8d2eedbb4c6f | 254 | } |
dhawkes | 0:8d2eedbb4c6f | 255 | c = gps.getcNb(); |
dhawkes | 0:8d2eedbb4c6f | 256 | } |
dhawkes | 0:8d2eedbb4c6f | 257 | return true; |
dhawkes | 0:8d2eedbb4c6f | 258 | } |