-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgnss.cpp
More file actions
5568 lines (5049 loc) · 202 KB
/
Copy pathgnss.cpp
File metadata and controls
5568 lines (5049 loc) · 202 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
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "gnss.h"
template <class T> Gnss<T, null>::Gnss(T & serial) : serial(serial) {
payloadLength = 0;
pollPayloadLength = 0;
offset = 0;
error = NO_ERROR;
payload = NULL;
pollPayload = NULL;
nmea = false;
nmeaChecksumNext = false;
nmeaValid = false;
nmeaPayload = "", nmeaChecksum = "";
endOfNavEpoch = false;
iTOW = 0;
ttp = false;
}
template <class T, class D> Gnss<T, D>::Gnss(T& serial, D& debug) : serial(serial), debug(debug) {
payloadLength = 0;
pollPayloadLength = 0;
offset = 0;
error = NO_ERROR;
payload = NULL;
pollPayload = NULL;
nmea = false;
nmeaChecksumNext = false;
nmeaValid = false;
nmeaPayload = "", nmeaChecksum = "";
endOfNavEpoch = false;
iTOW = 0;
ttp = false;
}
template <class T> void Gnss<T, null>::tp() {
pps = true;
if (ttp) { set_system_time(utcTime); ttp = false; }//use this with timTp message only
else system_tick(); //use this with normal time info such as navPvt, etc
}
template <class T, class D> void Gnss<T, D>::tp() {
pps = true;
if (ttp) { set_system_time(utcTime); ttp = false; }//use this with timTp message only
else system_tick(); //use this with normal time info such as navPvt, etc
}
template <class T> void Gnss<T, null>::begin(uint32_t gpsSpeed) {
serial.begin(gpsSpeed);
#ifdef DEBUG_LEVEL_UBX
InfoMsgMask mask = 0;
switch (DEBUG_LEVEL_UBX) {
case DEBUG_LEVEL_ERROR: mask.error = 1; break;
case DEBUG_LEVEL_WARNING: mask.warning = 1; mask.error = 1; break;
case DEBUG_LEVEL_NOTICE: mask.notice = 1; mask.warning = 1; mask.error = 1; break;
case DEBUG_LEVEL_TEST: mask.test = 1; mask.notice = 1; mask.warning = 1; mask.error = 1; break;
case DEBUG_LEVEL_DEBUG: mask.debug = 1; mask.test = 1; mask.notice = 1; mask.warning = 1; mask.error = 1; break;
}
setCfgInf(mask, UBX);
#endif
#ifdef DEBUG_LEVEL_NMEA
InfoMsgMask mask;
switch (DEBUG_LEVEL_NMEA) {
case DEBUG_LEVEL_ERROR: mask.error = 1; break;
case DEBUG_LEVEL_WARNING: mask.warning = 1; mask.error = 1; break;
case DEBUG_LEVEL_NOTICE: mask.notice = 1; mask.warning = 1; mask.error = 1; break;
case DEBUG_LEVEL_TEST: mask.test = 1; mask.notice = 1; mask.warning = 1; mask.error = 1; break;
case DEBUG_LEVEL_DEBUG: mask.debug = 1; mask.test = 1; mask.notice = 1; mask.warning = 1; mask.error = 1; break;
}
setCfgInf(mask, NMEA);
#endif
}
template <class T, class D> void Gnss<T, D>::begin(uint32_t gpsSpeed, uint32_t debugSpeed) {
serial.begin(gpsSpeed);
debug.begin(debugSpeed);
#ifdef DEBUG_LEVEL_UBX
InfoMsgMask mask = 0;
switch (DEBUG_LEVEL_UBX) {
case DEBUG_LEVEL_ERROR: mask.error = 1; break;
case DEBUG_LEVEL_WARNING: mask.warning = 1; mask.error = 1; break;
case DEBUG_LEVEL_NOTICE: mask.notice = 1; mask.warning = 1; mask.error = 1; break;
case DEBUG_LEVEL_TEST: mask.test = 1; mask.notice = 1; mask.warning = 1; mask.error = 1; break;
case DEBUG_LEVEL_DEBUG: mask.debug = 1; mask.test = 1; mask.notice = 1; mask.warning = 1; mask.error = 1; break;
}
setCfgInf(mask, UBX);
#endif
#ifdef DEBUG_LEVEL_NMEA
InfoMsgMask mask;
switch (DEBUG_LEVEL_NMEA) {
case DEBUG_LEVEL_ERROR: mask.error = 1; break;
case DEBUG_LEVEL_WARNING: mask.warning = 1; mask.error = 1; break;
case DEBUG_LEVEL_NOTICE: mask.notice = 1; mask.warning = 1; mask.error = 1; break;
case DEBUG_LEVEL_TEST: mask.test = 1; mask.notice = 1; mask.warning = 1; mask.error = 1; break;
case DEBUG_LEVEL_DEBUG: mask.debug = 1; mask.test = 1; mask.notice = 1; mask.warning = 1; mask.error = 1; break;
}
setCfgInf(mask, NMEA);
#endif
}
template <class T> void Gnss<T, null>::end() {
serial.end();
}
template <class T, class D> void Gnss<T, D>::end() {
serial.end();
debug.end();
}
template <class T> uint16_t Gnss<T, null>::available() {
return serial.available();
}
template <class T, class D> uint16_t Gnss<T, D>::available() {
return serial.available();
}
template <class T> uint8_t Gnss<T, null>::read() {
return serial.read();
}
template <class T, class D> uint8_t Gnss<T, D>::read() {
return serial.read();
}
template <class T> void Gnss<T, null>::calculateChecksum(uint8_t msgClass, uint8_t msgId, uint16_t len, uint8_t* pload) {
checksum[0] = 0;
checksum[1] = 0;
checksum[0] += msgClass;
checksum[1] += checksum[0];
checksum[0] += msgId;
checksum[1] += checksum[0];
checksum[0] += (uint8_t)(len & 0xFF);
checksum[1] += checksum[0];
checksum[0] += (uint8_t)(len >> 8);
checksum[1] += checksum[0];
if (pload != NULL) {
for (uint16_t i = 0; i < len; i++) {
checksum[0] += pload[i];
checksum[1] += checksum[0];
}
}
}
template <class T, class D> void Gnss<T, D>::calculateChecksum(uint8_t msgClass, uint8_t msgId, uint16_t len, uint8_t* pload) {
checksum[0] = 0;
checksum[1] = 0;
checksum[0] += msgClass;
checksum[1] += checksum[0];
checksum[0] += msgId;
checksum[1] += checksum[0];
checksum[0] += (uint8_t)(len & 0xFF);
checksum[1] += checksum[0];
checksum[0] += (uint8_t)(len >> 8);
checksum[1] += checksum[0];
if (pload != NULL) {
for (uint16_t i = 0; i < len; i++) {
checksum[0] += pload[i];
checksum[1] += checksum[0];
}
}
}
template <class T> void Gnss<T, null>::nmeaVerifyChecksum() {
uint8_t x = 0, hex, high_byte = 0, low_byte = 0;
uint16_t len = nmeaPayload.length();
const char* cString = nmeaPayload.c_str();
for (uint8_t i = 0; i < len; i++) {
x ^= cString[i];
}
if (isDigit(nmeaChecksum[0])) high_byte = (nmeaChecksum[0] - 48);
else if (isHexadecimalDigit(nmeaChecksum[0])) high_byte = (nmeaChecksum[0] - 55);
if (isDigit(nmeaChecksum[1])) low_byte = (nmeaChecksum[1] - 48);
else if (isHexadecimalDigit(nmeaChecksum[1])) low_byte = (nmeaChecksum[1] - 55);
//hex = high_byte * 16 + low_byte;
hex = (high_byte << 4) | low_byte;
if (hex == x) nmeaValid = true; else nmeaValid = false;
}
template <class T, class D> void Gnss<T, D>::nmeaVerifyChecksum() {
uint8_t x = 0, hex, high_byte = 0, low_byte = 0;
uint16_t len = nmeaPayload.length();
const char* cString = nmeaPayload.c_str();
for (uint8_t i = 0; i < len; i++) {
x ^= cString[i];
}
if (isDigit(nmeaChecksum[0])) high_byte = (nmeaChecksum[0] - 48);
else if (isHexadecimalDigit(nmeaChecksum[0])) high_byte = (nmeaChecksum[0] - 55);
if (isDigit(nmeaChecksum[1])) low_byte = (nmeaChecksum[1] - 48);
else if (isHexadecimalDigit(nmeaChecksum[1])) low_byte = (nmeaChecksum[1] - 55);
hex = (high_byte << 4) | low_byte;
if (hex == x) nmeaValid = true; else nmeaValid = false;
}
template <class T> bool Gnss<T, null>::ready() {
while (available()) {
uint8_t c = read();
/*if (!nmea) { //UBX message
if (offset < 6) {
switch (offset) {
case 0:
if (c == NMEA_START) { nmea = true; nmeaChecksumNext = false; nmeaPayload = ""; nmeaChecksum = ""; continue; }
if (c != SYNC_CHARS[0]) continue;
break;
case 1:
if (c != SYNC_CHARS[1])
{
offset = 0;
continue;
}
break;
case 2:
messageClass = c;
break;
case 3:
messageId = c;
break;
case 4: payloadLength = c; break;
case 5:
payloadLength |= (((uint16_t)c) << 8);
payload = (uint8_t*)realloc(payload, payloadLength);
if (payload == NULL && payloadLength > 0) { offset = 0; error = OUT_OF_MEMORY; return true; }
break;
}
offset++;
}
else { //offset >= 6
if ((offset < (payloadLength + 6))) {
payload[offset - 6] = c;
offset++;
}
else if (offset == payloadLength + 6) {
calculateChecksum(messageClass, messageId, payloadLength, payload);
if (c == checksum[0]) offset++;
else {
offset = 0;
error = CHECKSUM_ERROR; return true;
}
}
else if (offset == payloadLength + 7) {
offset = 0;
if (c == checksum[1]) {
error = NO_ERROR;
if (messageClass == UBX_NAV && messageId == NAV_EOE) { endOfNavEpoch = true; iTOW = *(uint32_t*)payload; }
else endOfNavEpoch = false;
return true;
}
else error = CHECKSUM_ERROR; return true;
}
}
}
else { //nmea message
String cls = "", msgId = "";
switch (c) {
case '*': nmeaChecksumNext = true; break;
case '\r': break;
case '\n':
nmea = false;
nmeaVerifyChecksum();
if (nmeaValid) {
cls = nmeaPayload.substring(2, 5);
if (cls == "RMC") { messageClass = UBX_NMEA; messageId = NMEA_RMC; }
else if (cls == "VTG") { messageClass = UBX_NMEA; messageId = NMEA_VTG; }
else if (cls == "GGA") { messageClass = UBX_NMEA; messageId = NMEA_GGA; }
else if (cls == "GSA") { messageClass = UBX_NMEA; messageId = NMEA_GSA; }
else if (cls == "GSV") { messageClass = UBX_NMEA; messageId = NMEA_GSV; }
else if (cls == "GLL") { messageClass = UBX_NMEA; messageId = NMEA_GLL; }
else if (cls == "GST") { messageClass = UBX_NMEA; messageId = NMEA_GST; }
else if (cls == "VLW") { messageClass = UBX_NMEA; messageId = NMEA_VLW; }
else if (cls == "GNS") { messageClass = UBX_NMEA; messageId = NMEA_GNS; }
else if (cls == "ZDA") { messageClass = UBX_NMEA; messageId = NMEA_ZDA; }
cls = nmeaPayload.substring(0, 4);
if (cls == "PUBX") {
messageClass = UBX_PUBX;
msgId = nmeaPayload.substring(5, 7);
if (msgId == "41") messageId = PUBX_CONFIG;
else if (msgId == "00") messageId = PUBX_POSITION;
else if (msgId == "40") messageId = PUBX_RATE;
else if (msgId == "03") messageId = PUBX_SVSTATUS;
else if (msgId == "04") messageId = PUBX_TIME;
}
return true;
}
else return false;
default:
if (!nmeaChecksumNext) nmeaPayload += char(c);
else nmeaChecksum += char(c);
break;
}
}*/
if (offset < 6) {
switch (offset) {
case 0:
if (c == NMEA_START) {
nmea = true; //possible NMEA message as '$' can appear in UBX messages as well
nmeaChecksumNext = false; nmeaPayload = ""; nmeaChecksum = "";
}
else if (c == SYNC_CHARS[0]) { //most likely UBX message as NMEA seems to be using ASCII chars only
offset = 1; nmea = false; endOfNavEpoch = false; continue;
}
break;
case 1:
if (c != SYNC_CHARS[1]) {
offset = 0;
}
else offset = 2;
break;
case 2:
messageClass = c;
offset = 3;
break;
case 3:
messageId = c;
offset = 4;
break;
case 4: payloadLength = c; offset = 5; break;
case 5:
payloadLength |= (((uint16_t)c) << 8);
payload = (uint8_t*)realloc(payload, payloadLength);
if (payload == NULL && payloadLength > 0) { offset = 0; error = OUT_OF_MEMORY; return true; }
offset = 6;
break;
}
}
else {
if ((offset < (payloadLength + 6))) {
payload[offset - 6] = c;
offset++;
}
else if (offset == payloadLength + 6) {
calculateChecksum(messageClass, messageId, payloadLength, payload);
if (c == checksum[0]) offset++;
else {
offset = 0;
error = CHECKSUM_ERROR;
return true;
}
}
else if (offset == payloadLength + 7) {
offset = 0;
if (c == checksum[1]) {
if (messageClass == UBX_NAV && messageId == NAV_EOE) {
endOfNavEpoch = true;
iTOW = *(uint32_t*)payload;
}
error = NO_ERROR;
return true;
}
else {
error = CHECKSUM_ERROR;
return true;
}
}
}
if (nmea) {
String cls = "", msgId = "";
switch (c) {
case '$': continue;
case '*': nmeaChecksumNext = true; break;
case '\r': break;
case '\n':
nmea = false;
nmeaVerifyChecksum();
if (nmeaValid) {
if (nmeaPayload.length() >= 5) {
cls = nmeaPayload.substring(2, 5);
if (cls == "RMC") { messageClass = UBX_NMEA; messageId = NMEA_RMC; }
else if (cls == "VTG") { messageClass = UBX_NMEA; messageId = NMEA_VTG; }
else if (cls == "GGA") { messageClass = UBX_NMEA; messageId = NMEA_GGA; }
else if (cls == "GSA") { messageClass = UBX_NMEA; messageId = NMEA_GSA; }
else if (cls == "GSV") { messageClass = UBX_NMEA; messageId = NMEA_GSV; }
else if (cls == "GLL") { messageClass = UBX_NMEA; messageId = NMEA_GLL; }
else if (cls == "GST") { messageClass = UBX_NMEA; messageId = NMEA_GST; }
else if (cls == "VLW") { messageClass = UBX_NMEA; messageId = NMEA_VLW; }
else if (cls == "GNS") { messageClass = UBX_NMEA; messageId = NMEA_GNS; }
else if (cls == "ZDA") { messageClass = UBX_NMEA; messageId = NMEA_ZDA; }
else if (cls == "GBS") { messageClass = UBX_NMEA; messageId = NMEA_GBS; }
else if (cls == "DTM") { messageClass = UBX_NMEA; messageId = NMEA_DTM; }
else if (cls == "GRS") { messageClass = UBX_NMEA; messageId = NMEA_GRS; }
else if (cls == "TXT") { messageClass = UBX_NMEA; messageId = NMEA_TXT; }
}
if (nmeaPayload.length() >= 4) {
cls = nmeaPayload.substring(0, 4);
if (cls == "PUBX") {
messageClass = UBX_PUBX;
if (nmeaPayload.length() >= 7) {
msgId = nmeaPayload.substring(5, 7);
if (msgId == "41") messageId = PUBX_CONFIG;
else if (msgId == "00") messageId = PUBX_POSITION;
else if (msgId == "40") messageId = PUBX_RATE;
else if (msgId == "03") messageId = PUBX_SVSTATUS;
else if (msgId == "04") messageId = PUBX_TIME;
}
}
}
return true;
}
else { printf("nmea checksum error: %s\n", nmeaPayload); return false; }
default:
if (!nmeaChecksumNext) nmeaPayload += char(c);
else {
nmeaChecksum += char(c);
}
break;
}
}
}
return false;
}
template <class T, class D> bool Gnss<T, D>::ready() {
while (available()) {
uint8_t c = read();
/* if (!nmea) { //UBX message
if (offset < 6) {
switch (offset) {
case 0:
//debug.print(c, HEX); debug.print(" ");
if (c == NMEA_START) { nmea = true; nmeaChecksumNext = false; nmeaPayload = ""; nmeaChecksum = ""; continue; }
if (c != SYNC_CHARS[0]) continue;
break;
case 1:
//debug.print(c, HEX);
if (c != SYNC_CHARS[1]) {
offset = 0;
continue;
}
break;
case 2:
//debug.print(" Receiving UBX... ");
//debug.print("msgCls "); debug.print(c, HEX);
messageClass = c;
break;
case 3:
//debug.print(", msgId "); debug.print(c, HEX);
messageId = c;
break;
case 4: payloadLength = c; break;
case 5:
payloadLength |= (((uint16_t)c) << 8);
//debug.print(", len "); debug.print(payloadLength); debug.print(", payload: ");
payload = (uint8_t*)realloc(payload, payloadLength);
if (payload == NULL && payloadLength > 0) { offset = 0; error = OUT_OF_MEMORY; return true; }
break;
}
offset++;
}
else { //offset >= 6
if ((offset < (payloadLength + 6))) {
payload[offset - 6] = c;
//debug.print(c, HEX); debug.print(" ");
offset++;
}
else if (offset == payloadLength + 6) {
//debug.println();
calculateChecksum(messageClass, messageId, payloadLength, payload);
//debug.print(" checksum0 "); debug.print(checksum[0], HEX); debug.print(" checksum1 "); debug.println(checksum[1],HEX);
if (c == checksum[0]) offset++;
else {
offset = 0;
//debug.print("checksum0 "); debug.print(checksum[0], HEX);
//debug.print(" - wrong checksum0: "); //debug.println(c, HEX);
//poll(pollMessageClass, pollMessageId, pollPayloadLength, pollPayload);
error = CHECKSUM_ERROR; return true;
}
//debug.print(" rec chksum0 "); debug.print(c, HEX);
}
else if (offset == payloadLength + 7) {
offset = 0;
//debug.print(", rec chksum1 "); debug.println(c, HEX);
if (c == checksum[1]) {
if (messageClass == UBX_NAV && messageId == NAV_EOE) { endOfNavEpoch = true; iTOW = *(uint32_t*)payload; }
else endOfNavEpoch = false;
error = NO_ERROR;
return true;
}
else {
//debug.print("checksum1 "); debug.print(checksum[1], HEX);
//debug.print(" - wrong checksum1: "); //debug.println(c, HEX);
//poll(pollMessageClass, pollMessageId, pollPayloadLength, pollPayload);
error = CHECKSUM_ERROR; return true;
}
}
}
}
else { //nmea message
String cls = "", msgId = "";
switch (c) {
case '*': nmeaChecksumNext = true; break;
case '\r': break;
case '\n':
nmea = false;
nmeaVerifyChecksum();
//debug.println(nmeaPayload);
if (nmeaValid) {
cls = nmeaPayload.substring(2, 5);
if (cls == "RMC") { messageClass = UBX_NMEA; messageId = NMEA_RMC; }
else if (cls == "VTG") { messageClass = UBX_NMEA; messageId = NMEA_VTG; }
else if (cls == "GGA") { messageClass = UBX_NMEA; messageId = NMEA_GGA; }
else if (cls == "GSA") { messageClass = UBX_NMEA; messageId = NMEA_GSA; }
else if (cls == "GSV") { messageClass = UBX_NMEA; messageId = NMEA_GSV; }
else if (cls == "GLL") { messageClass = UBX_NMEA; messageId = NMEA_GLL; }
else if (cls == "GST") { messageClass = UBX_NMEA; messageId = NMEA_GST; }
else if (cls == "VLW") { messageClass = UBX_NMEA; messageId = NMEA_VLW; }
else if (cls == "GNS") { messageClass = UBX_NMEA; messageId = NMEA_GNS; }
else if (cls == "ZDA") { messageClass = UBX_NMEA; messageId = NMEA_ZDA; }
cls = nmeaPayload.substring(0, 4);
if (cls == "PUBX") {
messageClass = UBX_PUBX;
msgId = nmeaPayload.substring(5, 7);
if (msgId == "41") messageId = PUBX_CONFIG;
else if (msgId == "00") messageId = PUBX_POSITION;
else if (msgId == "40") messageId = PUBX_RATE;
else if (msgId == "03") messageId = PUBX_SVSTATUS;
else if (msgId == "04") messageId = PUBX_TIME;
}
return true;
}
else { debug.println(String("nmea checksum error: ") + nmeaPayload); return false; }
default:
if (!nmeaChecksumNext) nmeaPayload += char(c);
else nmeaChecksum += char(c);
break;
}
}*/
if (offset < 6) {
switch (offset) {
case 0:
//if (nmea && (c != '\r')) debug.print(c); else debug.print(c, HEX);
if (c == NMEA_START) {
nmea = true; //possible NMEA message as '$' can appear in UBX messages as well
nmeaChecksumNext = false; nmeaPayload = ""; nmeaChecksum = "";
}
else if (c == SYNC_CHARS[0]) { //most likely UBX message as NMEA seems to be using ASCII chars only
offset = 1; nmea = false; endOfNavEpoch = false; continue;
}
break;
case 1:
//debug.print(c, HEX);
if (c != SYNC_CHARS[1]) {
offset = 0;
}
else offset = 2;
break;
case 2:
//debug.println(" Receiving UBX...");
//debug.print(" msgCls "); debug.print(c, HEX);
messageClass = c;
offset = 3;
break;
case 3:
//debug.print(", msgId "); debug.print(c, HEX);
messageId = c;
offset = 4;
break;
case 4: payloadLength = c; offset = 5; break;
case 5:
payloadLength |= (((uint16_t)c) << 8);
//debug.print(", len "); debug.print(payloadLength); debug.print(", payload: ");
payload = (uint8_t*)realloc(payload, payloadLength);
if (payload == NULL && payloadLength > 0) { offset = 0; error = OUT_OF_MEMORY; return true; }
offset = 6;
break;
}
}
else { //offset >= 6
if ((offset < (payloadLength + 6))) {
payload[offset - 6] = c;
//debug.print(c, HEX);
offset++;
}
else if (offset == payloadLength + 6) {
calculateChecksum(messageClass, messageId, payloadLength, payload);
//debug.print(" calculated checksum0 "); debug.print(checksum[0]); debug.print(", calculated checksum1 "); debug.print(checksum[1]);
if (c == checksum[0]) offset++;
else {
offset = 0;
//debug.print("wrong checksum0: "); debug.print(c, HEX);
error = CHECKSUM_ERROR;
return true;
}
//debug.print(" receieved chksum0 "); debug.print(c, HEX);
}
else if (offset == payloadLength + 7) {
offset = 0;
//debug.println(", chksum1"); debug.print(c, HEX);
if (c == checksum[1]) {
if (messageClass == UBX_NAV && messageId == NAV_EOE) {
//debug.println("end of nav epoch");
endOfNavEpoch = true;
iTOW = *(uint32_t*)payload;
}
error = NO_ERROR;
return true;
}
else {
//debug.print("wrong checksum1: "); debug.println(c, HEX);
error = CHECKSUM_ERROR;
return true;
}
}
}
if (nmea) {
String cls = "", msgId = "";
switch (c) {
case '$': continue;
case '*': nmeaChecksumNext = true; break;
case '\r': break;
case '\n':
nmea = false;
nmeaVerifyChecksum();
//debug.println(nmeaPayload); debug.print("*"); debug.println(nmeaChecksum);
if (nmeaValid) {
if (nmeaPayload.length() >= 5) {
cls = nmeaPayload.substring(2, 5);
if (cls == "RMC") { messageClass = UBX_NMEA; messageId = NMEA_RMC; }
else if (cls == "VTG") { messageClass = UBX_NMEA; messageId = NMEA_VTG; }
else if (cls == "GGA") { messageClass = UBX_NMEA; messageId = NMEA_GGA; }
else if (cls == "GSA") { messageClass = UBX_NMEA; messageId = NMEA_GSA; }
else if (cls == "GSV") { messageClass = UBX_NMEA; messageId = NMEA_GSV; }
else if (cls == "GLL") { messageClass = UBX_NMEA; messageId = NMEA_GLL; }
else if (cls == "GST") { messageClass = UBX_NMEA; messageId = NMEA_GST; }
else if (cls == "VLW") { messageClass = UBX_NMEA; messageId = NMEA_VLW; }
else if (cls == "GNS") { messageClass = UBX_NMEA; messageId = NMEA_GNS; }
else if (cls == "ZDA") { messageClass = UBX_NMEA; messageId = NMEA_ZDA; }
else if (cls == "GBS") { messageClass = UBX_NMEA; messageId = NMEA_GBS; }
else if (cls == "DTM") { messageClass = UBX_NMEA; messageId = NMEA_DTM; }
else if (cls == "GRS") { messageClass = UBX_NMEA; messageId = NMEA_GRS; }
else if (cls == "TXT") { messageClass = UBX_NMEA; messageId = NMEA_TXT; }
}
if (nmeaPayload.length() >= 4) {
cls = nmeaPayload.substring(0, 4);
if (cls == "PUBX") {
messageClass = UBX_PUBX;
if (nmeaPayload.length() >= 7) {
msgId = nmeaPayload.substring(5, 7);
if (msgId == "41") messageId = PUBX_CONFIG;
else if (msgId == "00") messageId = PUBX_POSITION;
else if (msgId == "40") messageId = PUBX_RATE;
else if (msgId == "03") messageId = PUBX_SVSTATUS;
else if (msgId == "04") messageId = PUBX_TIME;
}
}
}
return true;
}
else { debug.print("nmea checksum error: "); debug.println(nmeaPayload); return false; }
default:
if (!nmeaChecksumNext) nmeaPayload += char(c);
else {
nmeaChecksum += char(c);
}
break;
}
}
}
return false;
}
template <class T> void Gnss<T, null>::get() {
if (ready() && error == NO_ERROR) {
switch (messageClass) {
case UBX_NAV:
switch (messageId) {
case NAV_CLOCK: navClock(); break;
case NAV_DGPS: navDgps(); break;
case NAV_DOP: navDop(); break;
case NAV_GEOFENCE: navGeoFence(); break;
case NAV_ODO: navOdo(); break;
case NAV_ORB: navOrb(); break;
case NAV_POSLLH: navPosLlh(); break;
case NAV_PVT: navPvt(); break;
case NAV_SAT: navSat(); break;
case NAV_SBAS: navSbas(); break;
case NAV_SLAS: navSlas(); break;
case NAV_TIMEBDS: navTimeBds(); break;
case NAV_TIMEGAL: navTimeGal(); break;
case NAV_TIMEGLO: navTimeGlo(); break;
case NAV_TIMEGPS: navTimeGps(); break;
case NAV_TIMELS: navTimeLs(); break;
case NAV_TIMEUTC: navTimeUtc(); break;
} break;
case UBX_CFG:
switch (messageId) {
case CFG_GNSS: cfgGnss(); break;
case CFG_INF: cfgInf(); break;
case CFG_LOGFILTER: cfgLogFilter(); break;
case CFG_MSG: cfgMsg(); break;
case CFG_NAV5: cfgNav(); break;
case CFG_ODO: cfgOdo(); break;
case CFG_PM2: cfgPm(); break;
case CFG_PMS: cfgPms(); break;
case CFG_PRT: cfgPrt(); break;
case CFG_PWR: cfgPwr(); break;
case CFG_RATE: cfgRate(); break;
case CFG_RXM: cfgRxm(); break;
case CFG_SBAS: cfgSbas(); break;
case CFG_SLAS: cfgSlas(); break;
case CFG_TP5: cfgTp(); break;
case CFG_GEOFENCE: cfgGeoFence(); break;
case CFG_NMEA: cfgNmea(); break;
} break;
case UBX_MON:
switch (messageId) {
case MON_VER: monVer(MON_VER_EXTENSION_NUMBER); break;
case MON_GNSS: monGnss(); break;
case MON_PATCH: monPatch(); break;
} break;
case UBX_TIM:
switch (messageId) {
case TIM_TM2: timTm(); break;
case TIM_TP: timTp(); break;
} break;
case UBX_LOG:
switch (messageId) {
case LOG_INFO: logInfo(); break;
case LOG_RETRIEVEPOS: logRetrievePos(); break;
case LOG_RETRIEVEPOSEXTRA: logRetrievePosExtra(); break;
case LOG_RETRIEVESTRING: logRetrieveString(); break;
case LOG_FINDTIME: logFindTime(); break;
} break;
case UBX_INF:
char infLevel[8];
switch (messageId) {
case INF_DEBUG: sprintf(infLevel, String(F("debug")).c_str()); break;
case INF_ERROR: sprintf(infLevel, String(F("error")).c_str()); break;
case INF_NOTICE: sprintf(infLevel, String(F("notice")).c_str()); break;
case INF_TEST: sprintf(infLevel, String(F("test")).c_str()); break;
case INF_WARNING: sprintf(infLevel, String(F("warning")).c_str()); break;
default: sprintf(infLevel, String(F("unknown")).c_str()); break;
}
infMsg(infLevel);
break;
case UBX_ACK:
switch (messageId) {
case ACK_ACK: ackAck(); break;
case ACK_NAK: ackNak(); break;
} break;
case UBX_NMEA:
switch (messageId) {
case NMEA_RMC: nmeaRmc(); break;
case NMEA_VTG: nmeaVtg(); break;
case NMEA_GGA: nmeaGga(); break;
case NMEA_GSA: nmeaGsa(); break;
case NMEA_GSV: nmeaGsv(); break;
case NMEA_GLL: nmeaGll(); break;
case NMEA_GST: nmeaGst(); break;
case NMEA_VLW: nmeaVlw(); break;
case NMEA_GNS: nmeaGns(); break;
case NMEA_ZDA: nmeaZda(); break;
case NMEA_GBS: nmeaGbs(); break;
case NMEA_DTM: nmeaDtm(); break;
case NMEA_GRS: nmeaGrs(); break;
case NMEA_TXT: nmeaTxt(); break;
} break;
case UBX_PUBX:
switch (messageId) {
case PUBX_POSITION: pubxPosition(); break;
case PUBX_SVSTATUS: pubxSvStatus(); break;
case PUBX_TIME: pubxTime(); break;
} break;
}
}
}
template <class T, class D> void Gnss<T, D>::get() {
if (ready() && error == NO_ERROR) {
switch (messageClass) {
case UBX_NAV:
switch (messageId) {
case NAV_CLOCK: navClock(); break;
case NAV_DGPS: navDgps(); break;
case NAV_DOP: navDop(); break;
case NAV_GEOFENCE: navGeoFence(); break;
case NAV_ODO: navOdo(); break;
case NAV_ORB: navOrb(); break;
case NAV_POSLLH: navPosLlh(); break;
case NAV_PVT: navPvt(); break;
case NAV_SAT: navSat(); break;
case NAV_SBAS: navSbas(); break;
case NAV_SLAS: navSlas(); break;
case NAV_TIMEBDS: navTimeBds(); break;
case NAV_TIMEGAL: navTimeGal(); break;
case NAV_TIMEGLO: navTimeGlo(); break;
case NAV_TIMEGPS: navTimeGps(); break;
case NAV_TIMELS: navTimeLs(); break;
case NAV_TIMEUTC: navTimeUtc(); break;
} break;
case UBX_CFG:
switch (messageId) {
case CFG_GNSS: cfgGnss(); break;
case CFG_INF: cfgInf(); break;
case CFG_LOGFILTER: cfgLogFilter(); break;
case CFG_MSG: cfgMsg(); break;
case CFG_NAV5: cfgNav(); break;
case CFG_ODO: cfgOdo(); break;
case CFG_PM2: cfgPm(); break;
case CFG_PMS: cfgPms(); break;
case CFG_PRT: cfgPrt(); break;
case CFG_PWR: cfgPwr(); break;
case CFG_RATE: cfgRate(); break;
case CFG_RXM: cfgRxm(); break;
case CFG_SBAS: cfgSbas(); break;
case CFG_SLAS: cfgSlas(); break;
case CFG_TP5: cfgTp(); break;
case CFG_GEOFENCE: cfgGeoFence(); break;
case CFG_NMEA: cfgNmea(); break;
} break;
case UBX_MON:
switch (messageId) {
case MON_VER: monVer(MON_VER_EXTENSION_NUMBER); break;
case MON_GNSS: monGnss(); break;
case MON_PATCH: monPatch(); break;
} break;
case UBX_TIM:
switch (messageId) {
case TIM_TM2: timTm(); break;
case TIM_TP: timTp(); break;
} break;
case UBX_LOG:
switch (messageId) {
case LOG_INFO: logInfo(); break;
case LOG_RETRIEVEPOS: logRetrievePos(); break;
case LOG_RETRIEVEPOSEXTRA: logRetrievePosExtra(); break;
case LOG_RETRIEVESTRING: logRetrieveString(); break;
case LOG_FINDTIME: logFindTime(); break;
} break;
case UBX_INF:
char infLevel[8];
switch (messageId) {
case INF_DEBUG: sprintf(infLevel, String(F("debug")).c_str()); break;
case INF_ERROR: sprintf(infLevel, String(F("error")).c_str()); break;
case INF_NOTICE: sprintf(infLevel, String(F("notice")).c_str()); break;
case INF_TEST: sprintf(infLevel, String(F("test")).c_str()); break;
case INF_WARNING: sprintf(infLevel, String(F("warning")).c_str()); break;
default: sprintf(infLevel, String(F("unknown")).c_str()); break;
}
infMsg(infLevel);
break;
case UBX_ACK:
switch (messageId) {
case ACK_ACK: ackAck(); break;
case ACK_NAK: ackNak(); break;
} break;
case UBX_NMEA:
switch (messageId) {
case NMEA_RMC: nmeaRmc(); break;
case NMEA_VTG: nmeaVtg(); break;
case NMEA_GGA: nmeaGga(); break;
case NMEA_GSA: nmeaGsa(); break;
case NMEA_GSV: nmeaGsv(); break;
case NMEA_GLL: nmeaGll(); break;
case NMEA_GST: nmeaGst(); break;
case NMEA_VLW: nmeaVlw(); break;
case NMEA_GNS: nmeaGns(); break;
case NMEA_ZDA: nmeaZda(); break;
case NMEA_GBS: nmeaGbs(); break;
case NMEA_DTM: nmeaDtm(); break;
case NMEA_GRS: nmeaGrs(); break;
case NMEA_TXT: nmeaTxt(); break;
} break;
case UBX_PUBX:
switch (messageId) {
case PUBX_POSITION: pubxPosition(); break;
case PUBX_SVSTATUS: pubxSvStatus(); break;
case PUBX_TIME: pubxTime(); break;
} break;
}
}
}
template <class T> void Gnss<T, null>::ackAck() {
}
template <class T, class D> void Gnss<T, D>::ackAck() {
}
template <class T> void Gnss<T, null>::ackNak() {
}
template <class T, class D> void Gnss<T, D>::ackNak() {
}
template <class T> void Gnss<T, null>::poll(uint8_t msgClass, uint8_t msgId, uint16_t payload_length, uint8_t* pload) {
pollMessageClass = msgClass;
pollMessageId = msgId;
pollPayload = pload;
pollPayloadLength = payload_length;
calculateChecksum(pollMessageClass, pollMessageId, pollPayloadLength, pollPayload);
serial.write(SYNC_CHARS[0]);
serial.write(SYNC_CHARS[1]);
serial.write(pollMessageClass);
serial.write(pollMessageId);
serial.write(uint8_t(pollPayloadLength & 0xFF));
serial.write(uint8_t(pollPayloadLength >> 8));
if (pollPayload != NULL)
for (uint16_t i = 0; i < pollPayloadLength; i++) serial.write(pollPayload[i]);
serial.write(checksum[0]);
serial.write(checksum[1]);
}
template <class T, class D> void Gnss<T, D>::poll(uint8_t msgClass, uint8_t msgId, uint16_t payload_length, uint8_t* pload) {
pollMessageClass = msgClass;
pollMessageId = msgId;
pollPayload = pload;
pollPayloadLength = payload_length;
calculateChecksum(pollMessageClass, pollMessageId, pollPayloadLength, pollPayload);
serial.write(SYNC_CHARS[0]);
serial.write(SYNC_CHARS[1]);
serial.write(pollMessageClass);
//debug.print("Sending... msgCls "); debug.print(pollMessageClass, HEX);
serial.write(pollMessageId);
//debug.print(", msgID "); debug.print(pollMessageId, HEX);
serial.write(uint8_t(pollPayloadLength & 0xFF));
//debug.print(" len0 "); debug.print(uint8_t(payload_length & 0xFF), HEX);
serial.write(uint8_t(pollPayloadLength >> 8));
//debug.print(" len1 "); debug.print(uint8_t(payload_length >> 8), HEX);
//debug.print(", len "); debug.print(pollPayloadLength);
if (pollPayload != NULL)
{
//debug.print(", payload: ");
for (uint16_t i = 0; i < pollPayloadLength; i++)
{
serial.write(pollPayload[i]);
//debug.print(" "); debug.print(pollPayload[i], HEX);
}
}
//debug.println();
serial.write(checksum[0]);
//debug.print(" cksum0 "); debug.print(checksum[0], HEX);
serial.write(checksum[1]);
//debug.print(" cksum1 "); debug.println(checksum[1], HEX);
}
template <class T> bool Gnss<T, null>::pollNoError(uint32_t timeout) {
uint32_t starttime = millis();
while ((millis() - starttime) < timeout) {
if (ready() && messageClass == pollMessageClass && messageId == pollMessageId) {
switch (error) {
case NO_ERROR: return true;
case CHECKSUM_ERROR: poll(pollMessageClass, pollMessageId, pollPayloadLength, pollPayload); break;
case OUT_OF_MEMORY: return false;
}
}
}
return false;
}
template <class T, class D> bool Gnss<T, D>::pollNoError(uint32_t timeout) {
uint32_t starttime = millis();
while ((millis() - starttime) < timeout) {
if (ready() && messageClass == pollMessageClass && messageId == pollMessageId) {
switch (error) {
case NO_ERROR: return true;
case CHECKSUM_ERROR: poll(pollMessageClass, pollMessageId, pollPayloadLength, pollPayload); debug.println("chksum err"); break;
case OUT_OF_MEMORY: debug.println("out of memory"); return false;
}
}
}
debug.println("poll timeout");
return false;
}
template <class T> bool Gnss<T, null>::ackNoError(uint32_t timeout) {
uint32_t starttime = millis();
starttime = millis();
while ((millis() - starttime) < timeout) {
if (ready() && messageClass == UBX_ACK) {
UbxAck* ack = (UbxAck*)(payload);
switch (error) {
case NO_ERROR:
if (ack->classId == pollMessageClass && ack->messageId == pollMessageId) {
if (messageId == ACK_ACK) return true;
else return false;//ACK_NAK
}
else break;
case CHECKSUM_ERROR: poll(pollMessageClass, pollMessageId, pollPayloadLength, pollPayload); break;
case OUT_OF_MEMORY: return false;
}
}
}
return false;
}
template <class T, class D> bool Gnss<T, D>::ackNoError(uint32_t timeout) {
uint32_t starttime = millis();
starttime = millis();
while ((millis() - starttime) < timeout) {
if (ready() && messageClass == UBX_ACK) {
UbxAck* ack = (UbxAck*)(payload);