-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathNMEAGPS.cpp
415 lines (350 loc) · 10.3 KB
/
NMEAGPS.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
/*
-------------------------------------------------------------------------------
NMEAGPS.cpp
NMEAGPS Project
Initially created by Rowland O'Flaherty (rowlandoflaherty.com) on 5/28/12.
Version 1.0
-------------------------------------------------------------------------------
*/
//------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------
#include "NMEAGPS.h"
//------------------------------------------------------------------------------
// Lifecycle
//------------------------------------------------------------------------------
// Constructors
NMEAGPS::NMEAGPS(byte rxPin, byte txPin, long baud) {
_rxPin = rxPin;
_txPin = txPin;
_baud = baud;
}
NMEAGPS::NMEAGPS(const SoftwareSerial &gpsSerial) {
_rxPin = -1;
_txPin = -1;
_gpsSerial = gpsSerial;
_gpsSerial.listen();
}
// Destructor
NMEAGPS::~NMEAGPS() {
}
// Copy constructor
NMEAGPS::NMEAGPS(const NMEAGPS& srcObj) {
copyHelper(srcObj);
}
// Assignment operator
const NMEAGPS& NMEAGPS::operator=(const NMEAGPS& rhsObj) {
// Self-assignment check
if (this == &rhsObj) {
return (*this);
}
// Free old memory
// Copy new memory
copyHelper(rhsObj);
return (*this);
}
// Copy helper function
void NMEAGPS::copyHelper(const NMEAGPS& srcObj) {
_txPin = srcObj._txPin;
_rxPin = srcObj._rxPin;
_gpsSerial = srcObj._gpsSerial;
_buffer = srcObj._buffer;
_valid = srcObj._valid;
_year = srcObj._year;
_month = srcObj._month;
_day = srcObj._day;
_hour = srcObj._hour;
_min = srcObj._min;
_sec = srcObj._sec;
_lat = srcObj._lat;
_lon = srcObj._lon;
_alt = srcObj._alt;
_altUnits = srcObj._altUnits;
_geoid = srcObj._geoid;
_geoidUnits = srcObj._geoidUnits;
_nSats = srcObj._nSats;
_PDOP = srcObj._PDOP;
_HDOP = srcObj._HDOP;
_VDOP = srcObj._VDOP;
_status = srcObj._status;
_mode = srcObj._mode;
}
//------------------------------------------------------------------------------
// Overloaded Operators
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Public Member Functions
//------------------------------------------------------------------------------
void NMEAGPS::initialize() {
_gpsSerial = SoftwareSerial(_rxPin, _txPin);
_gpsSerial.begin(_baud);
_gpsSerial.listen();
// Initialize private variables
_valid = false;
_year = 0;
_month = 0;
_day = 0;
_hour = 0;
_min = 0;
_sec = 0;
_lat = 0;
_lon = 0;
_alt = 0;
_altUnits = '0';
_geoid = 0;
_geoidUnits = '0';
_nSats = 0;
_PDOP = 0;
_HDOP = 0;
_VDOP = 0;
}
bool NMEAGPS::update() {
bool updated;
if (readGPSStream()) {
parseBuffer();
updated = true;
} else {
updated = false;
}
return updated;
}
bool NMEAGPS::isListening() {
return _gpsSerial.isListening();
}
void NMEAGPS::listen() {
_gpsSerial.listen();
}
int NMEAGPS::available() {
return _gpsSerial.available();
}
bool NMEAGPS::overflow() {
return _gpsSerial.overflow();
}
float NMEAGPS::distanceBetween(float lat1, float lon1, float lat2, float lon2, float unitsPerMeter) {
float delta = M_PI/180.0*(lon1 - lon2);
float sdlong = sin(delta);
float cdlong = cos(delta);
lat1 = M_PI/180.0*(lat1);
lat2 = M_PI/180.0*(lat2);
float slat1 = sin(lat1);
float clat1 = cos(lat1);
float slat2 = sin(lat2);
float clat2 = cos(lat2);
delta = (clat1 * slat2) - (slat1 * clat2 * cdlong);
delta = sq(delta);
delta += sq(clat2 * sdlong);
delta = sqrt(delta);
float denom = (slat1 * slat2) + (clat1 * clat2 * cdlong);
delta = atan2(delta, denom);
return delta * 6372795 * unitsPerMeter;
}
//------------------------------------------------------------------------------
// Private Member Functions
//------------------------------------------------------------------------------
bool NMEAGPS::readGPSStream() {
if (!_gpsSerial.available()) {
return false;
}
while (_gpsSerial.available()) {
_buffer += (char)_gpsSerial.read();
}
return true;
}
void NMEAGPS::parseBuffer() {
int msgStartInd, msgEndInd, dollarInd;
String msgID;
String msg;
while (_buffer.length() > 1) {
msgStartInd = _buffer.indexOf('$'); // Check if start of message is there
if (msgStartInd == -1) {
_buffer = "";
return;
}
msgEndInd = _buffer.indexOf('\n',msgStartInd+1); // Check if end of message is there
if (msgEndInd == -1) {
break;
}
dollarInd = _buffer.indexOf('$',msgStartInd+1);
if (dollarInd != -1 && dollarInd < msgEndInd) { // Check that message has no other $ chars
_buffer = _buffer.substring(dollarInd);
msgStartInd = 0;
msgEndInd = msgEndInd - dollarInd;
}
msgID = _buffer.substring(msgStartInd+1, msgStartInd+6);
msg = _buffer.substring(msgStartInd+1,msgEndInd);
if (msgID == String("GPGGA")) {
parseGPGGA(msg);
} else if (msgID == String("GPRMC")) {
parseGPRMC(msg);
} else {
_valid = false;
}
// Trim off found message
if (_buffer.length() > msgEndInd+1) {
dollarInd = _buffer.indexOf('$',msgEndInd+1);
if (dollarInd != -1) {
_buffer = _buffer.substring(dollarInd);
} else {
_buffer = "";
}
} else {
_buffer = "";
}
_buffer = _buffer.substring(msgEndInd+1);
}
}
bool NMEAGPS::parseMsg(String &msg, String msgParts[], int nParts) {
// Get checksum value by doing Exclusive OR
int starInd = msg.indexOf('*');
char checksumMsg = 0;
if (starInd != -1) {
for (int i = 0; i < starInd; i++) {
checksumMsg ^= msg.charAt(i);
}
} else {
return (false);
}
// Get checksum characters from message
char checksumTruth;
char checksumStr[3];
if (msg.length() > starInd + 2) {
checksumStr[0] = msg.charAt(starInd+1);
checksumStr[1] = msg.charAt(starInd+2);
checksumStr[2] = '\0';
} else {
return (false);
}
sscanf(checksumStr,"%x",&checksumTruth); // Convert ascii characters to hex value
// Compare checksum values
if (checksumTruth != checksumMsg) {
return (false);
}
// Parse message
int commaInd = 0;
for (int i = 0; i < nParts; i++) {
commaInd = msg.indexOf(',');
if (commaInd == -1 && i != nParts-1) {
return (false);
}
msgParts[i] = msg.substring(0,commaInd);
msg = msg.substring(commaInd+1);
}
// Make sure message has correct amount of fields
if (msg.indexOf(',') != -1) {
return (false);
} else {
return (true);
}
}
int NMEAGPS::stoi(const String &str) {
char charBuffer[str.length()+1];
str.toCharArray(charBuffer, str.length()+1);
return (atoi(charBuffer));
}
float NMEAGPS::stof(const String &str) {
char charBuffer[str.length()+1];
str.toCharArray(charBuffer, str.length()+1);
return (atof(charBuffer));
}
void NMEAGPS::parseGPGGA(String &msg) {
String msgParts[] = {
String("GPGGA"),
String("hhmmss.sss"),
String("ddmm.mmmm"),
String("N"),
String("dddmm.mmmm"),
String("E"),
String("0"),
String("00"),
String("0000.0"),
String("-000.0"),
String("M"),
String("-000.0"),
String("M"),
String(""),
String("0000*00")
};
// Parse msg
if (!parseMsg(msg, msgParts, 15)) { // Exit if not a valid message
_valid = false;
return;
} else {
_valid = true;
}
// Time
_hour = stoi(msgParts[1].substring(0,2));
_min = stoi(msgParts[1].substring(2,4));
_sec = stof(msgParts[1].substring(4));
// Lat
int latD = stoi(msgParts[2].substring(0,2));
float latM = stof(msgParts[2].substring(2));
int latS = msgParts[3] == "N" ? 1 : -1;
_lat = latS * (latD + latM/60);
// Lon
int lonD = stoi(msgParts[4].substring(0,3));
float lonM = stof(msgParts[4].substring(3));
int lonS = msgParts[5] == "E" ? 1 : -1;
_lon = lonS * (lonD + lonM/60);
// # Sats
_nSats = stoi(msgParts[7]);
// HDOP
_HDOP = stof(msgParts[8]);
// Alt
_alt = stof(msgParts[9]);
_altUnits = msgParts[10].charAt(0);
// Geoid
_geoid = stof(msgParts[11]);
_geoidUnits = msgParts[12].charAt(0);
}
void NMEAGPS::parseGPRMC(String &msg) {
String msgParts[] = {
String("GPRMC"),
String("hhmmss.sss"),
String("V"),
String("ddmm.mmmm"),
String("N"),
String("dddmm.mmmm"),
String("E"),
String("000.00"),
String("000.00"),
String("ddmmyy"),
String("00"),
String("E"),
String("A*00"),
};
// Parse msg
if (!parseMsg(msg, msgParts, 13)) { // Exit if not a valid message
_valid = false;
return;
} else {
_valid = true;
}
// Time
_hour = stoi(msgParts[1].substring(0,2));
_min = stoi(msgParts[1].substring(2,4));
_sec = stof(msgParts[1].substring(4));
// Status
_status = msgParts[2] == "A";
// Lat
int latD = stoi(msgParts[3].substring(0,2));
float latM = stof(msgParts[3].substring(2));
int latS = msgParts[4] == "N" ? 1 : -1;
_lat = latS * (latD + latM/60);
// Lon
int lonD = stoi(msgParts[5].substring(0,3));
float lonM = stof(msgParts[5].substring(3));
int lonS = (msgParts[6] == "E" ? 1 : -1);
_lon = lonS * (lonD + lonM/60);
// Speed
_speed = stof(msgParts[7]);
// Course
_course = stof(msgParts[8]);
// Date
_day = stoi(msgParts[9].substring(0,2));
_month = stoi(msgParts[9].substring(2,4));
_year = stoi(msgParts[9].substring(4,6));
// Magnetic variation
_magVar = msgParts[10].charAt(0);
// Mode
_mode = msgParts[12].charAt(0);
}