-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGPS_NMEA.cpp
489 lines (409 loc) · 9.79 KB
/
GPS_NMEA.cpp
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/**
* @file ?/GPS_NMEA.cpp
* @version 0.7
*
* @section License
* Copyright (C) 2014-2015, jeditekunum
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*/
#include <ctype.h>
#include "Cosa/RTT.hh"
#include "Cosa/Trace.hh"
#include "GPS_NMEA.hh"
/**
* NMEA messages consist of sentences made up of fields and a checksum.
* While there are many potential sentences, only 2, $GPRMC and $GPGGA, are
* needed to provide all the data exposed by the base class GPS. Sentences
* may arrive in any order requiring that data is temporarily accumulated
* here until all sentences needed for an update to GPS have arrived.
* We know we have associated sentences when the time fields match.
*/
GPS_NMEA::GPS_NMEA(IOStream::Device *device) :
GPS(),
m_active(false),
m_tracing(false),
m_sentence(SENTENCE_INVALID),
m_parity(0),
m_field_number(0),
m_field_offset(0),
m_checksum_field(false),
m_tmp_date(0),
m_tmp_gprmc_time(0)
#ifndef GPS_TIME_ONLY
,
m_tmp_gpgga_time(1),
m_tmp_latitude(0),
m_tmp_longitude(0),
m_tmp_altitude(0),
m_tmp_course(0),
m_tmp_speed(0),
m_tmp_satellites(0),
m_tmp_hdop(0)
#endif
{
m_device = device;
}
bool
GPS_NMEA::begin()
{
if (m_active)
return (false);
m_active = true;
return (true);
}
void
GPS_NMEA::end()
{
m_active = false;
reset();
}
bool
GPS_NMEA::active()
{
return (m_active);
}
void
GPS_NMEA::reset(void)
{
m_sentence = SENTENCE_INVALID;
m_field_number = 0;
m_field_offset = 0;
m_checksum_field = false;
m_tmp_date = 0;
m_tmp_gprmc_time = 0;
#ifndef GPS_TIME_ONLY
m_tmp_gpgga_time = 1;
m_tmp_latitude = 0;
m_tmp_longitude = 0;
m_tmp_altitude = 0;
m_tmp_course = 0;
m_tmp_speed = 0;
m_tmp_satellites = 0;
m_tmp_hdop = 0;
#endif
GPS::reset();
}
void
GPS_NMEA::begin_tracing()
{
m_tracing = true;
}
void
GPS_NMEA::end_tracing()
{
m_tracing = false;
}
#ifndef GPS_INTERRUPT_IMPL
void
GPS_NMEA::consume(void)
{
while (m_device->available())
feedchar(m_device->getchar());
}
#endif
#ifdef GPS_INTERRUPT_IMPL
int
GPS_NMEA::putchar(char c)
#else
void
GPS_NMEA::feedchar(char c)
#endif
{
if (c != '\r' && c != '\n' && (c < ' ' || c > '~'))
{
if (m_tracing)
trace << PSTR("_");
#ifdef GPS_INTERRUPT_IMPL
return (c);
#else
return;
#endif
}
if (m_tracing)
{
#ifdef GPS_INTERRUPT_IMPL
// bad idea to trace within Irq...
#endif
if (c == '$')
trace << PSTR("<- ");
trace << c;
}
if (m_sentence != SENTENCE_INVALID ||
c == '$')
{
switch(c)
{
case '$': // start of sentence
m_sentence = SENTENCE_OTHER; // unknown at this point
m_parity = 0;
m_field_number = 0;
m_field_offset = 0;
m_checksum_field = false;
break;
case ',':
m_parity ^= c;
case '*':
m_field[m_field_offset] = '\0';
process_field();
m_field_number++;
m_field_offset = 0;
m_checksum_field = c == '*';
break;
case '\r':
m_field[m_field_offset] = '\0';
process_sentence();
m_sentence = SENTENCE_INVALID; // anything until next '$' is invalid
break;
case '\n':
break;
default:
if (!m_checksum_field)
m_parity ^= c;
if (m_field_offset < sizeof(m_field) - 1)
m_field[m_field_offset++] = c;
else
{
/* Field overflow */
m_field_offset = 0;
m_sentence = SENTENCE_INVALID;
}
}
}
#ifdef GPS_INTERRUPT_IMPL
return (c);
#endif
}
#ifndef GPS_TIME_ONLY
GPS_NMEA::position_t
GPS_NMEA::parse_position(char *p)
{
position_t result;
uint32_t scaled;
uint32_t hundred_thousandths_of_minutes;
/* Result is millionths of a degree */
/* DDMM.MMMMM */
scaled = parse_and_scale(p, 5);
/* DDMMMMMMM */
/* Get minutes */
hundred_thousandths_of_minutes = scaled % 10000000;
/* Truncate minutes leaving degrees in millionths */
result = (scaled / 10000000) * 1000000;
/* Convert minutes to degrees and add back in */
result += (hundred_thousandths_of_minutes + 3) / 6;
return result;
}
#endif
int32_t
GPS_NMEA::parse_and_scale(char *p, uint8_t places)
{
uint32_t result = 0;
bool negative;
while (*p && (*p == ' ' || *p == '\t'))
p++;
negative = (*p == '-');
if (negative)
p++;
while (isdigit(*p))
result = (result * 10) + (*(p++) - '0');
if (*p == '.')
p++;
while (places--)
{
result *= 10;
if (isdigit(*p))
result += (*(p++) - '0');
}
if (negative)
result = -result;
return result;
}
void
GPS_NMEA::field(uint8_t field_number, char *new_field)
{
UNUSED(field_number);
UNUSED(new_field);
/* May be implemented by subsclasses */
}
void
GPS_NMEA::sentence(bool valid)
{
UNUSED(valid);
/* May be implemented by subsclasses */
}
IOStream&
operator<<(IOStream& outs, GPS_NMEA& gps_nmea)
{
outs << (GPS&)gps_nmea;
return (outs);
}
void
GPS_NMEA::process_field()
{
if (m_field_number == 0)
{
if (!strcmp((char*)m_field, "GPRMC"))
m_sentence = SENTENCE_GPRMC;
else
{
#ifndef GPS_TIME_ONLY
if (!strcmp((char*)m_field, "GPGGA"))
m_sentence = SENTENCE_GPGGA;
else
#endif
field(m_field_number, (char*)m_field); // Subclasses may implement other sentences
}
return;
}
switch (m_sentence)
{
case SENTENCE_GPRMC:
switch (m_field_number)
{
case 1: // Time
m_tmp_gprmc_time = parse_and_scale((char*)m_field, 3);
break;
case 2: // Validity
if (m_field[0] != 'A')
m_sentence = SENTENCE_INVALID;
break;
case 3: // Latitude
#ifndef GPS_TIME_ONLY
m_tmp_latitude = parse_position((char*)m_field);
#endif
break;
case 4: // North/South
#ifndef GPS_TIME_ONLY
if (m_field[0] == 'S')
m_tmp_latitude = -m_tmp_latitude;
#endif
break;
case 5: // Longitude
#ifndef GPS_TIME_ONLY
m_tmp_longitude = parse_position((char*)m_field);
#endif
break;
case 6: // East/West
#ifndef GPS_TIME_ONLY
if (m_field[0] == 'W')
m_tmp_longitude = -m_tmp_longitude;
#endif
break;
case 7: // Speed
#ifndef GPS_TIME_ONLY
m_tmp_speed = parse_and_scale((char*)m_field, 2);
#endif
break;
case 8: // Course
#ifndef GPS_TIME_ONLY
m_tmp_course = parse_and_scale((char*)m_field, 2);
#endif
break;
case 9: // Date
m_tmp_date = strtoul((char*)m_field, NULL, 10);
break;
case 10: // magnetic variation
case 11: // E or W
break;
case 12: // unknown
break;
default:
m_sentence = SENTENCE_INVALID;
break;
}
break;
#ifndef GPS_TIME_ONLY
case SENTENCE_GPGGA:
switch (m_field_number)
{
case 1: // Time
m_tmp_gpgga_time = parse_and_scale((char*)m_field, 3);
break;
case 2: // Latitude
m_tmp_latitude = parse_position((char*)m_field);
break;
case 3: // North/South
if (m_field[0] == 'S')
m_tmp_latitude = -m_tmp_latitude;
break;
case 4: // Longitude
m_tmp_longitude = parse_position((char*)m_field);
break;
case 5: // East/West
if (m_field[0] == 'W')
m_tmp_longitude = -m_tmp_longitude;
break;
case 6: // Fix valid?
if (m_field[0] == '0')
m_sentence = SENTENCE_INVALID;
break;
case 7: // Satellites
m_tmp_satellites = strtoul((char*)m_field, NULL, 10);
break;
case 8: // HDOP
m_tmp_hdop = parse_and_scale((char*)m_field, 2);
break;
case 9: // Altitude
m_tmp_altitude = parse_and_scale((char*)m_field, 2);
break;
case 10: // Altitude units
case 11: // Geoidal separation
case 12: // Geoidal separation units
case 13: // Age of differential data
case 14: // Differential reference station id
break;
default:
m_sentence = SENTENCE_INVALID;
break;
}
#endif
default:
break;
}
/* Subclass may implement field() to handle other sentences */
field(m_field_number, (char*)m_field);
}
void
GPS_NMEA::process_sentence()
{
uint32_t checksum;
/* Last field is the checksum */
checksum = strtoul((char*)m_field, NULL, 16);
if (checksum == m_parity)
{
#ifdef GPS_TIME_ONLY
if (m_sentence == SENTENCE_GPRMC)
#else
if (m_tmp_gprmc_time == m_tmp_gpgga_time &&
m_tmp_satellites >= GPS_MINIMUM_SATELLITES)
#endif
{
m_date = m_tmp_date;
m_time = m_tmp_gprmc_time;
#ifndef GPS_TIME_ONLY
m_latitude = m_tmp_latitude;
m_longitude = m_tmp_longitude;
m_altitude = m_tmp_altitude;
m_course = m_tmp_course;
m_speed = m_tmp_speed;
m_satellites = m_tmp_satellites;
m_hdop = m_tmp_hdop;
#endif
m_last_update = RTT::millis();
m_tmp_gprmc_time = 0;
}
/* Subclass may implement sentence() to handle other sentences */
sentence(true);
}
else
sentence(false);
}