-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGPSNavman.cpp
More file actions
executable file
·432 lines (351 loc) · 13.8 KB
/
GPSNavman.cpp
File metadata and controls
executable file
·432 lines (351 loc) · 13.8 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
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
/***************************************************************************
* *
* 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, 1997-2012, ANSR *
* *
***************************************************************************
* *
* Filename: GPSNavman.cpp *
* *
***************************************************************************/
#include "armlib.h"
/// Reserve memory for singleton object.
static GPSNavman navmanSingletonObject;
/**
* Constructor.
*/
GPSNavman::GPSNavman()
{
this->gpsParseState = GPSStart1;
this->dataReadyFlag = false;
this->okToSendFlag = false;
this->version[0] = 0x00;
}
/**
* Pointer to the Navman GPS engine object.
*/
GPSNavman *GPSNavman::GetInstance()
{
return &navmanSingletonObject;
}
/**
* Get a pointer to the GPSData object that contains the latest fix information.
*
* @return pointer to GPSData object
*/
GPSData *GPSNavman::Data()
{
return &this->data;
}
/**
* Flag that indicates new GPS data has been receive and is ready.
*
* @return true when new data is ready; otherwise false
*/
bool_t GPSNavman::IsDataReady()
{
if (this->dataReadyFlag)
{
this->dataReadyFlag = false;
return true;
} //END if
return false;
}
/**
* Flag that indicates the OK To Send has been received.
*
* @return true when OK To Send is received
*/
bool_t GPSNavman::IsOkToSend()
{
if (this->okToSendFlag)
{
this->okToSendFlag = false;
return true;
} //END if
return false;
}
/**
* Using the Message ID, parse the SiRF binary message.
*/
void GPSNavman::ParseMessage()
{
uint32_t i, j, index, cNo;
switch (payload[0])
{
// Measured Tracker Data Out - Message ID 4.
case 4:
// Offset to the 1st SVID field.
index = 8;
// Process 12 receive channels of data.
for (i = 0; i < 12; ++i)
{
// Sum the C/No value.
cNo = 0;
for (j = 0; j < 10; ++j)
cNo += this->payload[index + j + 5];
this->data.RxChannel (i, static_cast<uint32_t> (this->payload[index]), static_cast<uint32_t> (this->payload[index + 4]), cNo);
index += 15;
} // END for
// Set a flag to indicate a new data set is complete.
this->dataReadyFlag = true;
break;
// Software Version String (Response to Poll) - Message ID 6
case 6:
// Set the write index to the end of the string.
j = 0;
while (this->version[j] != 0)
++j;
// Append the payload bytes to the version string.
for (i = 1; this->payload[i] != 0 && i < this->payloadLength && j < (VersionSize - 1); ++i, ++j)
this->version[j] = payload[i];
// NULL terminate after the copy.
this->version[j] = 0;
// Put a <LF><CR> between each information string. The order is critical so Windoze will recognize the string in .NET.
strcat (this->version, "\r\n");
break;
// OkToSend - Message ID 18.
case 18:
this->okToSendFlag = true;
// Query the software version after we've started.
PollSoftwareVersion();
break;
// Geodetic Navigation Data - Message ID 41.
case 41:
this->data.navType = (static_cast<uint16_t> (payload[3]) << 8) | static_cast<uint16_t> (payload[4]);
switch (this->data.navType & 0x0007)
{
case 0x0003:
case 0x0005:
this->data.fixType = GPSData::Fix2D;
break;
case 0x0004:
case 0x0006:
this->data.fixType = GPSData::Fix3D;
break;
default:
this->data.fixType = GPSData::NoFix;
break;
} // END switch
this->data.weekNumber = (static_cast<uint32_t> (payload[5]) << 8) | static_cast<uint32_t> (payload[6]);
this->data.timeOfWeek = (static_cast<uint32_t> (payload[7]) << 24) | (static_cast<uint32_t> (payload[8]) << 16) | (static_cast<uint32_t> (payload[9]) << 8) | static_cast<uint32_t> (payload[10]);
this->data.year = (static_cast<uint16_t> (payload[11]) << 8) | static_cast<uint16_t> (payload[12]);
this->data.month = static_cast<uint8_t> (payload[13]);
this->data.day = static_cast<uint8_t> (payload[14]);
this->data.hours = static_cast<uint8_t> (payload[15]);
this->data.minutes = static_cast<uint8_t> (payload[16]);
this->data.seconds = ((static_cast<uint16_t> (payload[17]) << 8) | static_cast<uint16_t> (payload[18])) / 1000;
this->data.latitude = (static_cast<int32_t> (payload[23]) << 24) | (static_cast<int32_t> (payload[24]) << 16) | (static_cast<int32_t> (payload[25]) << 8) | static_cast<int32_t> (payload[26]);
this->data.longitude = (static_cast<int32_t> (payload[27]) << 24) | (static_cast<int32_t> (payload[28]) << 16) | (static_cast<int32_t> (payload[29]) << 8) | static_cast<int32_t> (payload[30]);
this->data.altitude = (static_cast<int32_t> (payload[35]) << 24) | (static_cast<int32_t> (payload[36]) << 16) | (static_cast<int32_t> (payload[37]) << 8) | static_cast<int32_t> (payload[38]);
this->data.speed = (static_cast<uint16_t> (payload[40]) << 8) | static_cast<uint16_t> (payload[41]);
this->data.heading= (static_cast<uint16_t> (payload[42]) << 8) | static_cast<uint16_t> (payload[43]);
this->data.trackedSats = static_cast<uint32_t> (payload[88]);
this->data.dop = (10 * static_cast<uint32_t> (payload[89])) / 5;
break;
// Messages we will ignore, but can't disable.
//
// Command Acknowledgment - Message ID 11.
// Extended Ephemeris Data - Message ID 56.
// Development Data - Message ID 255.
case 11:
case 56:
case 255:
break;
// Set the following messages to rate 0 so we don't have to process them again.
//
// Measure Navigation Data Out - Message ID 2.
// Response: Clock Status Data - Message ID 7.
// CPU Throughput - Message ID 9.
// DGPS Status - Format - Message ID 27.
case 2:
case 7:
case 9:
case 27:
DisableMessage (payload[0]);
break;
default:
break;
}
}
/**
* Send message to generate software version.
*/
void GPSNavman::PollSoftwareVersion()
{
uint8_t message[2] = { 132, 0 };
// Clear the version string before we request the information.
this->version[0] = 0x00;
SendMessage (message, sizeof(message));
}
/**
* Send message to disable (set the rate to 0) of the desired message.
*
* @param messageID of the message to disable
*/
void GPSNavman::DisableMessage(uint8_t messageID)
{
uint8_t message[8] = { 166, 0, messageID, 0, 0, 0, 0, 0 };
SendMessage (message, 8);
}
/**
* Generate a SiRF binary message with the header, length, payload, and
* checksum bytes.
*
* @param message pointer to binary buffer holding message.
* @param length number of bytes in the message
*/
void GPSNavman::SendMessage(uint8_t *message, uint32_t length)
{
uint32_t i, checksum;
WriteData (0xa0);
WriteData (0xa2);
WriteData (((length >> 8) & 0x7f));
WriteData (length & 0xff);
checksum = 0x0000;
for (i = 0; i < length; ++i)
{
WriteData (message[i]);
checksum += message[i];
}
WriteData (((checksum >> 8) & 0x7f));
WriteData (checksum & 0xff);
WriteData (0xb0);
WriteData (0xb3);
}
/**
* Read the serial FIFO and process any pending GPS messages.
*/
void GPSNavman::Update()
{
uint32_t value;
// State machine handles each byte as it is read from the GPS serial port.
// We are looking for a SiRF binary message.
// If we have a complete data set, all the routine to return so the calling thread can process the data set.
while (HasData() && !this->dataReadyFlag)
{
// Get the character value.
value = ReadData();
// Process based on the state machine.
switch (this->gpsParseState)
{
case GPSStart1:
if (value == 0xa0)
this->gpsParseState = GPSStart2;
break;
case GPSStart2:
if (value == 0xa2)
this->gpsParseState = GPSLengthMSB;
else
this->gpsParseState = GPSStart1;
break;
case GPSLengthMSB:
this->payloadLength = static_cast <uint32_t> (value) << 8;
this->gpsParseState = GPSLengthLSB;
break;
case GPSLengthLSB:
this->payloadLength |= static_cast <uint32_t> (value);
if (this->payloadLength == 0)
this->gpsParseState = GPSChecksumMSB;
else
this->gpsParseState = GPSPayload;
this->payloadChecksum = 0x0000;
this->payloadIndex = 0;
break;
case GPSPayload:
this->payloadChecksum += value;
if (this->payloadIndex < PayloadBufferSize)
this->payload[this->payloadIndex] = value;
if (++this->payloadIndex == this->payloadLength)
this->gpsParseState = GPSChecksumMSB;
break;
case GPSChecksumMSB:
if (((this->payloadChecksum >> 8) & 0x7f) == value)
this->gpsParseState = GPSChecksumLSB;
else
this->gpsParseState = GPSStart1;
break;
case GPSChecksumLSB:
if ((this->payloadChecksum & 0xff) == value)
this->gpsParseState = GPSEnd1;
else
this->gpsParseState = GPSStart1;
break;
case GPSEnd1:
if (value == 0xb0)
this->gpsParseState = GPSEnd2;
else
this->gpsParseState = GPSStart1;
break;
case GPSEnd2:
// Once we have the last character, convert the binary message to something usable.
if (value == 0xb3)
ParseMessage();
this->gpsParseState = GPSStart1;
break;
} // END switch
} // END while
}
/**
* Change from the NMEA-0183 mode to the SiRF Binary protocol mode at 57,600bps.
*/
void GPSNavman::SetBinaryProtocol()
{
char *out, buffer[80];
// Create the NEMA-0183 string that configures the GPS engine for SiRF Binary mode at 57,600 BPS, 8 data bits, No Parity, 1 stop bit.
strcpy (buffer, "$PSRF100,0,57600,8,1,0");
NMEA::AppendChecksum(buffer);
strcat (buffer, "\015\012");
// Write each character of the string.
out = buffer;
while (*out != 0)
WriteData (*out++);
}
/**
* Initialize the GPS engine using the select data source.
*
* @param source enumerated type of the desired data source
*/
void GPSNavman::InitializeDataSource (DataSource source)
{
uint8_t payload[25];
// Most of the payload is 0, so clear everything.
memset (payload, 0, sizeof(payload));
// Message ID and data source values.
payload[0] = 128;
payload[24] = static_cast<uint8_t> (source);
SendMessage (payload, sizeof(payload));
}
/**
* Write the current configuration settings to flash so they are retained during the next power cycle.
*/
void GPSNavman::WriteToFlash()
{
uint8_t payload[1];
payload[0] = 198;
SendMessage (payload, sizeof(payload));
}
/**
* GPS engine version information.
*
* @return pointer to NULL terminated string
*/
const char *GPSNavman::Version()
{
return this->version;
}