forked from gercunderscore4/WiGPS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WiGPS.cpp
454 lines (371 loc) · 11.3 KB
/
WiGPS.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
/*
Copyright 2013 Daniele Faugiana
This file is part of "WiGPS Arduino Library".
"WiGPS Arduino Library" 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 3 of the License, or
(at your option) any later version.
"WiGPS Arduino 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with "WiGPS Arduino Library". If not, see <http://www.gnu.org/licenses/>.
*/
/*
* MODIFIED BY: Geoffrey Card
* FILE: WiGPS.cpp
* PROGRAM: rover
* PURPOSE: collect and convert NMEA GPS data
* DATE: 2014-06-14 -
* NOTES: Arduino doubles are floats
* MODIFICATIONS: Converted to Arduino IDE
* Used m/s instead of km/h
* Fixed latitude and longitude seconds
* Added get functions
* Formatted to YYYY-MM-DD HH:mm:ss
* Added seconds as float for more precision
* Update loop exits on timeout
* Serial selection in constructor
*/
#include "WiGPS.h"
void WiGPS::parseGPRMC(GPRMC* str){
/*
* Save all data from the GPRMC string
* in a numeric format in memory.
*/
//Serial.println("GPS GPRMC parsing.");
hours = str->UTCtime().substring(0,2).toInt();
minutes = str->UTCtime().substring(2,4).toInt();
seconds = str->UTCtime().substring(4,6).toInt();
day = str->UTCdate().substring(0,2).toInt();
month = str->UTCdate().substring(2,4).toInt();
year = str->UTCdate().substring(4,6).toInt();
latitudeDeg = str->latitudeDeg().substring(0,2).toInt();
latitudeMin = str->latitudeDeg().substring(2,4).toInt();
latitudeSecf = (float) str->latitudeDeg().substring(5,10).toInt() * CONV_TO_SEC; // convert from .mm to sec
latitudeSec = (int) round(latitudeSecf);
latitudeRef = str->latitudeRef().charAt(0);
longitudeDeg = str->longitudeDeg().substring(0,3).toInt();
longitudeMin = str->longitudeDeg().substring(3,5).toInt();
longitudeSecf = (float) str->longitudeDeg().substring(6,11).toInt() * CONV_TO_SEC; // convert from .mm to sec
longitudeSec = (int) round(longitudeSecf);
longitudeRef = str->longitudeRef().charAt(0);
String speedString(str->speed());
int speedDot = speedString.indexOf('.');
Speed = (int) round(speedString.substring(0,speedDot).toInt() * KNOTS_TO_MS);
String courseString(str->course());
int courseDot = courseString.indexOf('.');
Course = courseString.substring(0,courseDot).toInt();
return;
}
/******************
* PUBLIC METHODS
******************/
WiGPS::WiGPS(HardwareSerial& serial) : _serial(serial) {
//Serial.println("GPS class initialization.");
// init all variables
powerPort = 0;
powerState = LOW;
hours = 0;
minutes = 0;
seconds = 0;
day = 0;
month = 0;
year = 0;
latitudeDeg = 0;
latitudeMin = 0;
latitudeSec = 0;
latitudeRef = '\0';
longitudeDeg = 0;
longitudeMin = 0;
longitudeSec = 0;
longitudeRef = '\0';
Speed = 0;
Course = 0;
dataReady = false;
return;
}
void WiGPS::init(int pw) {
//Serial.println("GPS initialization.");
dataReady = false;
_serial.begin(9600);
powerPort = pw;
pinMode(powerPort, OUTPUT);
digitalWrite(powerPort, LOW);
return;
}
int WiGPS::on(void){
/*
* Powers on the GPS which
* starts watching for satellites
* to retrieve data from them
*/
//Serial.println("GPS on.");
int counter = 0;
digitalWrite(powerPort, HIGH);
while(!_serial.available()){
if(counter++ < 3){
delay(1000);
}else{
return -1;
};
};
return 0;
}
int WiGPS::off(void){
/*
* Powers off the GPS which
* stops watching for satellites
* but keeps the last RTC data
* and RAM memory data for future
* exploring.
*/
//Serial.println("GPS off.");
int counter = 0;
digitalWrite(powerPort, LOW);
while(_serial.available()){
if(counter++ < 3){
delay(1000);
}else{
return -1;
};
};
return 0;
}
bool WiGPS::update(void){
return update(DEFAULT_UPDATE_TIMEOUT);
}
bool WiGPS::update(int timeout_sec){
/*
* The main function for fetching data
* from the GPS module.
* This function gets incoming Strings
* from the _serial port and store them
* in a buffer if they starts with the $ char
* After retrieving a "valid" String the
* parser is called.
*/
//Serial.println("GPS update.");
char buffer[BUFFER_LENGTH_2];
char *buf = buffer;
dataReady = false;
//int failed5 = 0;
unsigned long updateTimeout = millis() + timeout_sec*1000;
while(buf - buffer < BUFFER_LENGTH_2){
*(buf++) = '\0';
}
buf = buffer;
//Serial.print("GPS begin loop: ");
//Serial.print(updateTimeout - timeout_sec*1000);
//Serial.print(" < ");
//Serial.println(updateTimeout);
while(!dataReady){
// Wait for the first incoming header
while(_serial.read() != '$');
// Store the first 5 chars
for(int i = 0; i<5; i++){
while(!_serial.available());
*(buf++) = _serial.read();
}
//Serial.print("buffer:" );
//Serial.print(buffer);
if(strncmp(buffer, PROTOCOL, 5) == 0){
// This is the right String, go on
do {
// Fetch the rest of the GPRMC String
while(!_serial.available());
*buf = _serial.read();
} while(*(buf++) != '\n');
GPRMC str(buffer);
//Serial.println(str);
// 'A' for valid, 'V' for invalid
if(str.dataValid().equals("A")){
//Serial.println("GPS data is valid.");
// The string is ok, extract data
// TODO: ADD CHECKSUM CONTROL TO THE STRING
Serial.print(str);
parseGPRMC(&str);
//Now break the cycle
//Serial.println("GPS success.");
//Serial.println("GPS break loop.");
//Serial.print("\n");
dataReady = true;
return dataReady;
} else {
//Serial.println("GPS data invalid.");
}
}
//Serial.println("6- Data is not ready ");
//Serial.println("------");
//failed5++;
//if(failed5 <= 1500){
//Serial.println("7- Too many fails.. quitting ");
//return FALSE;
//}
//Serial.println("\n");
// if update timed out
//Serial.print("GPS time: ");
//Serial.print(millis());
//Serial.print(" < ");
//Serial.println(updateTimeout);
if (millis() >= updateTimeout) {
//Serial.println("GPS timeout.");
//Serial.println("GPS break loop.");
//Serial.print("\n");
dataReady = false;
return false;
}
buf = buffer;
};
//Serial.println("GPS loop end.");
//Serial.print("\n");
return true;
}
String WiGPS::time(void){
/*
* Get the time from the
* last updated data in the memory
* in the format of the String d
*/
//Serial.println("GPS time.");
String h(hours);
String m(minutes);
String s(seconds);
String f = h + String(':') + m + String(':') + s;
return f;
}
String WiGPS::date(void){
/*
* Get the date from the
* last updated data in the memory
* in the format of the String d
*/
//Serial.println("GPS date.");
String d(day);
String m(month);
String y(year);
String f = String('2') + String('0') + y + String('-') + m + String('-') + d;
return f;
}
String WiGPS::latitude(void){
/*
* Get the latitude from the
* last updated data in the memory
* in the format of the String d
*/
//Serial.println("GPS latitude.");
String d(latitudeDeg);
String m(latitudeMin);
String s(latitudeSec);
String r(latitudeRef);
char c = DEGREE_CHAR;
String f = d + String(c) + m + String('\'') + s + String('\"') + r;
return f;
}
String WiGPS::longitude(void){
/*
* Get the longitude from the
* last updated data in the memory
* in the format of the String d
*/
//Serial.println("GPS longitude.");
String d(longitudeDeg);
String m(longitudeMin);
String s(longitudeSec);
String r(longitudeRef);
char c = DEGREE_CHAR;
String f = d + String(c) + m + String('\'') + s + String('\"') + r;
return f;
}
String WiGPS::speed(void){
/*
* Get the speed from the
* last updated data in the memory
* in the format of the String d (m/s)
*/
//Serial.println("GPS speed.");
String s(Speed);
String f = s + String(" m/s");
return f;
}
String WiGPS::course(void){
/*
* Get the course from the
* last updated data in the memory
* in the format of the String d
*/
//Serial.println("GPS course.");
String s(Course);
char c = DEGREE_CHAR;
String f = s + String(c);
return s;
}
WiGPS::~WiGPS(){
/*
* This destroys the created
* SoftwareSerial object.
*/
//Serial.println("GPS de-initialization.");
//delete serialPort;
}
bool WiGPS::isReady(void){
//Serial.println("GPS readiness.");
return dataReady;
}
float WiGPS::getLatitude(void){
//Serial.println("GPS numerical latitude.");
float temp = (float) latitudeDeg;
temp += (float) latitudeMin/60;
temp += latitudeSecf/3600;
temp *= latitudeRef == 'N' ? 1.0 : -1.0;
return temp;
}
int WiGPS::getLatitudeDeg(void){
//Serial.println("GPS numerical latitude degrees.");
return latitudeDeg;
}
int WiGPS::getLatitudeMin(void){
//Serial.println("GPS numerical latitude minutes.");
return latitudeMin;
}
int WiGPS::getLatitudeSec(void){
//Serial.println("GPS numerical latitude seconds.");
return latitudeSec;
}
float WiGPS::getLatitudeSecf(void){
//Serial.println("GPS numerical (float) latitude seconds.");
return latitudeSecf;
}
char WiGPS::getLatitudeRef(void){
//Serial.println("GPS numerical latitude reference.");
return latitudeRef;
}
float WiGPS::getLongitude(void){
//Serial.println("GPS numerical longitude.");
float temp = (float) longitudeDeg;
temp += (float) longitudeMin/60;
temp += longitudeSecf/3600;
temp *= longitudeRef == 'N' ? 1.0 : -1.0;
return temp;
}
int WiGPS::getLongitudeDeg(void){
//Serial.println("GPS numerical longitude degrees.");
return longitudeDeg;
}
int WiGPS::getLongitudeMin(void){
//Serial.println("GPS numerical longitude minutes.");
return longitudeMin;
}
int WiGPS::getLongitudeSec(void){
//Serial.println("GPS numerical longitude seconds.");
return longitudeSec;
}
float WiGPS::getLongitudeSecf(void){
//Serial.println("GPS numerical (float) longitude seconds.");
return longitudeSecf;
}
char WiGPS::getLongitudeRef(void){
//Serial.println("GPS numerical longitude reference.");
return longitudeRef;
}