-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGPSNmea.cpp
464 lines (406 loc) · 13 KB
/
GPSNmea.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
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program 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 General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA *
* *
***************************************************************************
* *
* (c) Copyright, 2011-2012, Elijah Brown *
* *
***************************************************************************
* *
* Filename: GPSNmea.cpp *
* *
***************************************************************************/
#include "main.h"
/// Reserve memory for the singleton object.
static GPSNmea nmeaSingletonObject;
/**
* Constructor.
*/
GPSNmea::GPSNmea() {
this->dataReadyFlag = false;
this->utcOffset = 0;
this->timeOfWeek = 0;
this->gpsParseState = STARTOFMESSAGE;
this->data.navType = 0xff;
}
/**bool
* Get a pointer to the NMEA GPS Engine object.
*/
GPSNmea *GPSNmea::GetInstance() {
return &nmeaSingletonObject;
}
/**
* Get a pointer to the GPSData object that contains the latest fix information.
*
* @return pointer to GPSData object
*/
GPSData *GPSNmea::Data() {
return &this->data;
}
/**
* Determine if new GPS message is ready to process. This function is a one shot and
* typically returns true once a second for each GPS position fix.
*
* @return true if new message available; otherwise false
*/
bool_t GPSNmea::IsDataReady() {
if (this->dataReadyFlag) {
this->dataReadyFlag = false;
return true;
} //END if
return false;
}
void GPSNmea::GpsPassthru() {
uint8_t value;
while (UART1::GetInstance()->IsCharReady() || UART0::GetInstance()->IsCharReady()) {
// forward data from the GPS to the exteranl UART
if(UART1::GetInstance()->IsCharReady()) {
value = UART1::GetInstance()->ReadChar();
UART0::GetInstance()->WriteChar(value);
}
if(UART0::GetInstance()->IsCharReady()) {
// forward data from the external UART to the GPS
value = UART0::GetInstance()->ReadChar();
UART1::GetInstance()->WriteChar(value);
}
}
}
/**
* Read the serial FIFO and process complete GPS messages.
*/
void GPSNmea::Update() {
uint32_t value;
// This state machine handles each character as it is read from the GPS serial port.
while (HasData()) {
// Get the character value.
value = ReadData();
//this->gpsBuffer[this->gpsIndex++] = value;
switch (gpsParseState) {
///////////////////////////////////////////////////////////////////////
// Search for start of message '$'
case STARTOFMESSAGE:
if (value == '$') {
calcChecksum = 0; // reset checksum
index = 0; // reset index
gpsParseState = COMMAND;
}
break;
///////////////////////////////////////////////////////////////////////
// Retrieve command (NMEA Address)
case COMMAND:
if (value != ',' && value != '*') {
commandBuffer[index++] = value;
calcChecksum ^= value;
// Check for command overflow
if (index >= MAX_CMD_LEN)
gpsParseState = STARTOFMESSAGE;
} else {
commandBuffer[index] = '\0'; // terminate command
calcChecksum ^= value;
index = 0;
gpsParseState = DATA; // goto get data state
}
break;
// Store data and check for end of sentence or checksum flag
case DATA:
if (value == '*') { // checksum flag?
dataBuffer[index] = '\0';
gpsParseState = CHECKSUM_1;
} else {
// Check for end of sentence with no checksum
if (value == '\r') {
dataBuffer[index] = '\0';
ProcessCommand(commandBuffer, dataBuffer);
gpsParseState = STARTOFMESSAGE;
return;
}
//
// Store data and calculate checksum
//
calcChecksum ^= value;
dataBuffer[index] = value;
if (++index >= MAX_DATA_LEN) // Check for buffer overflow
gpsParseState = STARTOFMESSAGE;
}
break;
case CHECKSUM_1:
if ((value - '0') <= 9)
receivedChecksum = (value - '0') << 4;
else
receivedChecksum = (value - 'A' + 10) << 4;
gpsParseState = CHECKSUM_2;
break;
case CHECKSUM_2:
if ((value - '0') <= 9)
receivedChecksum |= (value - '0');
else
receivedChecksum |= (value - 'A' + 10);
//FIXME: re-add verifying the checksum after testing is finished
//if (calcChecksum == receivedChecksum)
ProcessCommand(commandBuffer, dataBuffer);
gpsParseState = STARTOFMESSAGE;
break;
///////////////////////////////////////////////////////////////////////
default:
gpsParseState = STARTOFMESSAGE;
break;
}
} // END while
}
/**
* Process NMEA sentence - switch on the NMEA command and call the
* appropriate processor
*/
void GPSNmea::ProcessCommand(uint8_t *pCommand, uint8_t *pData) {
/*
* GPGGA
*/
if (!strcmp((char *) pCommand, "GPGGA"))
ProcessGPGGA(pData);
/*
* GPRMC
*/
else if (!strcmp((char *) pCommand, "GPRMC"))
ProcessGPRMC(pData);
//m_dwCommandCount++;
//return TRUE;
}
/**
* This function will get the specified field in a NMEA string.
*
* @param pData Pointer to NMEA string
* @param pField Pointer to returned field
* @param nfieldNum Field offset to get
* @param nMaxFieldLen Maximum number of bytes pField can handle
*/
bool GPSNmea::GetField(uint8_t *pData, uint8_t *pField, int8_t nFieldNum, int8_t nMaxFieldLen) {
/*
* Validate params
*/
if (pData == NULL || pField == NULL || nMaxFieldLen <= 0) {
return false;
}
/*
* Go to the beginning of the selected field
*/
int i = 0;
int nField = 0;
while (nField != nFieldNum && pData[i]) {
if (pData[i] == ',') {
nField++;
}
i++;
if (pData[i] == NULL) {
pField[0] = '\0';
return false;
}
}
if (pData[i] == ',' || pData[i] == '*') {
pField[0] = '\0';
return false;
}
/*
* copy field from pData to Field
*/
int i2 = 0;
while (pData[i] != ',' && pData[i] != '*' && pData[i]) {
pField[i2] = pData[i];
i2++;
i++;
/*
* check if field is too big to fit on passed parameter. If it is,
* crop returned field to its max length.
*/
if (i2 >= nMaxFieldLen) {
i2 = nMaxFieldLen - 1;
break;
}
}
pField[i2] = '\0';
return true;
}
void GPSNmea::ProcessGPGGA(uint8_t *pData) {
uint8_t pField[MAXFIELD];
char pBuff[10];
// Time (currently not used)
/*if (GetField(pData, pField, 0, MAXFIELD)) {
// Hour
pBuff[0] = pField[0];
pBuff[1] = pField[1];
pBuff[2] = '\0';
data.hours = atoi(pBuff);
// minute
pBuff[0] = pField[2];
pBuff[1] = pField[3];
pBuff[2] = '\0';
data.minutes = atoi(pBuff);
// Second
pBuff[0] = pField[4];
pBuff[1] = pField[5];
pBuff[2] = '\0';
data.seconds = atoi(pBuff);
} */
// Latitude (currently not used)
/*if (GetField(pData, pField, 1, MAXFIELD)) {
data.latitude = lroundf(10000000 * atof((char *) pField + 2) / 60.0);
pField[2] = '\0';
data.latitude += lroundf(10000000 * atof((char *) pField));
}
if (GetField(pData, pField, 2, MAXFIELD)) {
if (pField[0] == 'S')
data.latitude = -data.latitude;
}
// Longitude (currently not used)
if (GetField(pData, pField, 3, MAXFIELD)) {
data.longitude = lroundf(10000000 * atof((char *) pField + 3) / 60.0);
pField[3] = '\0';
data.longitude += lroundf(10000000 * atof((char *) pField));
}
if (GetField(pData, pField, 4, MAXFIELD)) {
if (pField[0] == 'W')
data.longitude = -data.longitude;
} */
// Satellites in use
if (GetField(pData, pField, 6, MAXFIELD)) {
pBuff[0] = pField[0];
pBuff[1] = pField[1];
pBuff[2] = '\0';
data.trackedSats = atoi(pBuff);
}
// HDOP
if (GetField(pData, pField, 7, MAXFIELD)) {
data.dop = lroundf(atof((CHAR *) pField) * 10); // FIXME: is this the right units?
}
// Altitude
if (GetField(pData, pField, 8, MAXFIELD)) {
data.altitude = lroundf(atof((CHAR *) pField) * 100);
}
// Set the data-ready flag.
this->dataReadyFlag = true;
}
void GPSNmea::ProcessGPRMC(uint8_t *pData)
{
CHAR pBuff[10];
BYTE pField[MAXFIELD];
// Time
if(GetField(pData, pField, 0, MAXFIELD))
{
// Hour
pBuff[0] = pField[0];
pBuff[1] = pField[1];
pBuff[2] = '\0';
data.hours = atoi(pBuff);
// minute
pBuff[0] = pField[2];
pBuff[1] = pField[3];
pBuff[2] = '\0';
data.minutes = atoi(pBuff);
// Second
pBuff[0] = pField[4];
pBuff[1] = pField[5];
pBuff[2] = '\0';
data.seconds = atoi(pBuff);
}
//
// Data valid
//
if(GetField(pData, pField, 1, MAXFIELD)) {
if(pField[0] == 'A') {
if(data.altitude > 0)
data.fixType = data.Fix3D;
else
data.fixType = data.Fix2D;
}
else
data.fixType = data.NoFix;
}
//
// latitude
//
if(GetField(pData, pField, 2, MAXFIELD))
{
data.latitude = lroundf(10000000 * atof((char *) pField + 2) / 60.0);
pField[2] = '\0';
data.latitude += lroundf(10000000 * atof((char *) pField));
}
if(GetField(pData, pField, 3, MAXFIELD))
{
if(pField[0] == 'S')
{
data.latitude = -data.latitude;
}
}
//
// Longitude
//
if(GetField(pData, pField, 4, MAXFIELD))
{
data.longitude = lroundf(10000000 * atof((CHAR *)pField+3) / 60.0);
pField[3] = '\0';
data.longitude += lroundf(10000000 * atof((CHAR *)pField));
}
if(GetField(pData, pField, 5, MAXFIELD))
{
if(pField[0] == 'W')
{
data.longitude = -data.longitude;
}
}
//
// Ground speed
//
if(GetField(pData, pField, 6, MAXFIELD))
{ // convert to cm/s
data.speed = lroundf(51.4444 * atof((CHAR *)pField));
}
else
{
data.speed = 0.0;
}
//
// course over ground, degrees true converted 0.01 degree
//
if(GetField(pData, pField, 7, MAXFIELD))
{
data.heading = lroundf(100 * atof((CHAR *)pField));
}
else
{
data.heading = 0.0;
}
//
// Date
//
if(GetField(pData, pField, 8, MAXFIELD))
{
// Day
pBuff[0] = pField[0];
pBuff[1] = pField[1];
pBuff[2] = '\0';
data.day = atoi(pBuff);
// Month
pBuff[0] = pField[2];
pBuff[1] = pField[3];
pBuff[2] = '\0';
data.month = atoi(pBuff);
// Year (Only two digits. I wonder why?)
pBuff[0] = pField[4];
pBuff[1] = pField[5];
pBuff[2] = '\0';
data.year = atoi(pBuff);
data.year += 2000; // make 4 digit date
}
}