-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathGPS.cpp
More file actions
368 lines (334 loc) · 10.6 KB
/
Copy pathGPS.cpp
File metadata and controls
368 lines (334 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// from https://raw.githubusercontent.com/DennisSc/PPS-ntp-server/master/src/GPS.cpp
#include <Arduino.h>
#include <stdlib.h>
#include <string.h>
#include "DateTime.h"
#include "GPS.h"
#include "InputCapture.h"
#include "settings.h"
#define GPS_CODE_ZDA "ZDA"
#define GPS_CODE_RMC "RMC"
#define GPS_CODE_GGA "GGA"
#define GPS_CODE_GSA "GSA"
#define GPS_CODE_GSV "GSV"
/**
* Save new date and time to private variables
*/
void GPSDateTime::commit() {
time_ = newTime_;
year_ = newYear_;
month_ = newMonth_;
day_ = newDay_;
// PPS/millis reference was already snapshotted by decodeType() on
// whichever of ZDA/RMC/GGA arrived first this cycle -- see there.
// Satellite counts are published in decode(), not here -- see there for
// why.
}
void GPSDateTime::time(const char *time) {
newTime_ = atof(time) * 100;
}
uint16_t GPSDateTime::hour() {
return time_ / 1000000;
}
uint16_t GPSDateTime::minute() {
return (time_ / 10000) % 100;
}
uint16_t GPSDateTime::second() {
return (time_ / 100) % 100;
}
void GPSDateTime::day(const char *day) {
newDay_ = atoi(day);
}
uint16_t GPSDateTime::day(void) { return day_; };
void GPSDateTime::month(const char *month) {
newMonth_ = atoi(month);
}
uint16_t GPSDateTime::month(void) { return month_; };
void GPSDateTime::year(const char *year) {
newYear_ = atoi(year);
}
uint16_t GPSDateTime::year(void) { return year_; };
void GPSDateTime::rmctime(const char *timestr) {
newTime_ = atof(timestr) * 100;
}
void GPSDateTime::rmcdate(const char *datestr) {
int date = atoi(datestr);
newDay_ = date / 10000;
newMonth_ = (date / 100) % 100;
newYear_ = date % 100 + 2000;
}
bool GPSDateTime::tmp_is_code(const char *code) {
if(tmpLen != 5) {
return false;
}
if(tmp[0] != 'G') {
return false;
}
if(tmp[1] != 'P' && tmp[1] != 'N' && tmp[1] != 'L') {
return false;
}
return strcmp(tmp + 2, code) == 0;
}
// bounds-checked append; silently truncates instead of overflowing --
// real NMEA fields never come close to GPS_FIELD_MAX_LEN
void GPSDateTime::tmp_append(char c) {
if (tmpLen < sizeof(tmp) - 1) {
tmp[tmpLen++] = c;
tmp[tmpLen] = '\0';
}
}
void GPSDateTime::decodeType() {
if (tmp_is_code(GPS_CODE_GSA)) {
validCode = inGSA;
return;
}
if (tmp_is_code(GPS_CODE_GSV)) {
sawGSV = true;
validCode = inGSV;
return;
}
bool isZDA = tmp_is_code(GPS_CODE_ZDA);
bool isRMC = tmp_is_code(GPS_CODE_RMC);
bool isGGA = tmp_is_code(GPS_CODE_GGA);
// Snapshot the PPS/local-time reference on whichever of ZDA/RMC/GGA is
// first to arrive after a given PPS pulse, rather than assuming a fixed
// sentence order/set at compile time (this replaces the old
// GPS_USES_RMC/GPS_GGA_IS_FIRST macros -- see issue #6, "code assumes
// RMC comes after GGA"). Whichever arrives first is also the closest
// available reference point to the PPS edge, so this is never worse than
// a fixed assumption and self-corrects if a module's sentence order
// differs from what was expected.
//
// "First after a pulse" is identified via InputCapture::getCaptures()
// (an ISR-incremented count of real PPS pulses seen) rather than by
// guessing from NMEA content/order: NMEA sentences don't carry an
// explicit "this is a new second" marker, and GGA isn't guaranteed to be
// either first or last in the burst, so re-arming the capture only when
// getCaptures() has actually advanced is the one signal that's correct
// regardless of sentence order or count.
if ((isZDA || isRMC || isGGA) &&
(!everCapturedPps_ || pps.getCaptures() != capturedPpsCaptures_)) {
ppsCounter_ = pps.getCount();
ppsMillis_ = pps.getMillis();
dateMillis = millis();
capturedPpsCaptures_ = pps.getCaptures();
everCapturedPps_ = true;
// A new pulse means a fresh cycle: allow exactly one commit() for it,
// even if the module emits both ZDA and RMC (some do) -- otherwise
// decode() would report the same PPS pulse as two separate updates.
committedThisPulse_ = false;
}
if (isZDA) {
validCode = inZDATimeCode;
} else if (isRMC) {
validCode = inRMCTimeCode;
} else {
// GGA carries no date and is only used above as an early capture
// trigger -- its own fields are unused -- and any other sentence type
// is ignored outright.
validCode = waitDollar;
}
}
void GPSDateTime::decodeTimeCode() {
if (validCode == inRMCTimeCode) {
// example $GPRMC,144326.00,A,5107.0017737,N,11402.3291611,W,0.080,323.3,210307,0.0,E,A*20
switch (count_) {
case 1: // time
this->rmctime(tmp);
break;
case 9:
this->rmcdate(tmp);
break;
default:
break;
}
} else { // inZDATimeCode
// example $GPZDA,174304.36,24,11,2015,00,00*66
switch (count_) {
case 1: // time
this->time(tmp);
break;
case 2: // day
this->day(tmp);
break;
case 3: // month
this->month(tmp);
break;
case 4: // year
this->year(tmp);
break;
default:
break;
}
}
}
void GPSDateTime::decodeGSA() {
// example $GPGSA,A,3,04,07,09,03,08,22,16,27,,,,,1.4,0.8,1.2*3F
switch(count_) {
case 2:
lockStatus_ = atoi(tmp);
break;
case 15:
pdop = atof(tmp);
break;
case 16:
hdop = atof(tmp);
break;
case 17:
vdop = atof(tmp);
break;
}
}
void GPSDateTime::decodeGSV() {
uint8_t writeCopy = (satellites_copy + 1) % 2;
if(count_ > 3 && count_ < 20) {
switch(count_ % 4) {
case 0: // id
satellites[writeCopy][satellites_i].id = atoi(tmp);
break;
case 1: // elevation from horizon in degrees
satellites[writeCopy][satellites_i].elevation = atoi(tmp);
break;
case 2: // azimuth from clockwise north in degrees
satellites[writeCopy][satellites_i].azimuth = atoi(tmp);
break;
case 3: // snr
satellites[writeCopy][satellites_i].snr = atoi(tmp);
if(satellites[writeCopy][satellites_i].snr >= 25) {
strongSignalNext++;
} else if(satellites[writeCopy][satellites_i].snr >= 10) {
weakSignalNext++;
} else {
noSignalNext++;
}
if(satellites_i < MAX_SATELLITES-1) satellites_i++;
break;
}
}
}
/**
* Decode NMEA lines
* @return true: finished decoding date&time
*/
bool GPSDateTime::decode() {
char c = gpsUart_->read();
if (c == '$') {
tmp[0] = '\0';
tmpLen = 0;
count_ = 0;
parity_ = 0;
validCode = getType;
isNotChecked = true;
isUpdated_ = false;
return false;
}
if (validCode == waitDollar) {
return false;
}
if (c == ',' || c == '*') {
switch(validCode) {
case getType:
decodeType();
break;
case inZDATimeCode:
case inRMCTimeCode:
decodeTimeCode();
break;
case inGSA:
decodeGSA();
break;
case inGSV:
decodeGSV();
break;
case waitDollar:
break;
}
if (c == ',') {
parity_ ^= (uint8_t) c;
}
if (c == '*') {
isNotChecked = false;
}
tmp[0] = '\0';
tmpLen = 0;
count_++;
} else if (c == '\r' || c == '\n') {
// carriage return, so check for valid parity
uint8_t checksum = strtoul( tmp, NULL, 16 );
validString = parity_ == checksum;
if (validString) {
if(validCode == inZDATimeCode || validCode == inRMCTimeCode) {
// Distinct from the commit()/committedThisPulse_ gate just below:
// this fires for every checksum-valid ZDA/RMC sentence the module
// sends, even ones commit() suppresses as a same-pulse duplicate,
// and even while PPS is lost entirely (committedThisPulse_ then
// stays permanently true, so commit() never runs again -- see
// reportedNow()/reportedUpdate()).
reportUpdated_ = true;
// Publish this cycle's satellite counts here, on every valid
// ZDA/RMC sentence -- deliberately NOT inside the
// committedThisPulse_ gate below. GSV has nothing to do with PPS,
// but used to only get flushed from commit(), so once PPS was lost
// it silently accumulated strongSignalNext/weakSignalNext/
// noSignalNext without bound for the whole holdover episode
// instead of resetting once/sec like normal -- observed as
// weakSignals jumping to 1436 after a ~5 minute holdover (bench
// testing, see DONE.md). ZDA/RMC still arrive once/sec from the
// module even while PPS is down, so this keeps the same
// once-per-second publish cadence regardless of PPS state.
if(sawGSV) { // sometimes GSV doesn't come every second
strongSignal = strongSignalNext;
weakSignal = weakSignalNext;
noSignal = noSignalNext;
sawGSV = false;
satellites_copy = (satellites_copy + 1) % 2;
satellites[satellites_copy][satellites_i].id = 0;
satellites_i = 0;
}
strongSignalNext = weakSignalNext = noSignalNext = 0;
if(!committedThisPulse_) {
this->commit();
isUpdated_ = true;
committedThisPulse_ = true;
}
}
// commit datetime
}
// end of string
tmp[0] = '\0';
tmpLen = 0;
count_ = 0;
parity_ = 0;
validCode = waitDollar;
return isUpdated_;
} else {
// ordinary char
tmp_append(c);
if (isNotChecked) {
// XOR of all characters from $ to *
parity_ ^= (uint8_t) c;
}
}
return false;
}
/**
* Return instance of DateTime class
* @return DateTime
*/
DateTime GPSDateTime::GPSnow() {
return DateTime(this->year(), this->month(), this->day(), this->hour(), this->minute(), this->second());
}
/**
* Return a DateTime built from the most recently parsed, checksum-valid
* ZDA/RMC sentence's fields -- unlike GPSnow(), this doesn't depend on
* commit() having actually run for that sentence, so it keeps advancing
* even when commit()'s once-per-PPS-pulse gating (see decode()) suppresses
* a sentence, e.g. every sentence after PPS is lost.
* @return DateTime
*/
DateTime GPSDateTime::reportedNow() {
uint16_t hour = newTime_ / 1000000;
uint16_t minute = (newTime_ / 10000) % 100;
uint16_t second = (newTime_ / 100) % 100;
return DateTime(newYear_, newMonth_, newDay_, hour, minute, second);
}