-
Notifications
You must be signed in to change notification settings - Fork 0
/
soarNMEA.c
1998 lines (1865 loc) · 68.5 KB
/
soarNMEA.c
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 <PalmOS.h> // all the system toolbox headers
#include <SerialMgr.h>
#include <MemoryMgr.h>
#include <DLServer.h>
#include "soaring.h"
#include "soarNMEA.h"
#include "soarUtil.h"
#include "soarForm.h"
#include "soarMap.h"
#include "soarUMap.h"
#include "soarWind.h"
#include "Mathlib.h"
#include "soarMath.h"
#include "soarRECO.h"
#include "soarIO.h"
#include "soarSUA.h"
#include "soarGPSInfo.h"
#include "AddIncs/GarminIncs/GPSLib68K.h"
#include "soarGEOMAG.h"
#include "soarComp.h"
#include "soarWay.h"
#define INVALID 0
#define GPRMC_VALID 1
#define GPRMB_VALID 2
#define GPGGA_VALID 3
#define PGRMZ_VALID 4
#define PGCS_VALID 5
#define PBB50_VALID 6
#define PCAIB_VALID 7
#define PCAID_VALID 8
#define PFSRA_VALID 9
#define LXWP0_VALID 10
#define PMGNST_VALID 11
#define W_VALID 12
#define RECOA_VALID 13
#define RECOB_VALID 14
#define GPGSA_VALID 15
#define PGRME_VALID 16
#define PTAS1_VALID 17
#define GPGSV_VALID 18
#define PILC_VALID 19
#define PFLAU_VALID 20
#define PFLAA_VALID 21
#define PZAN1_VALID 22
#define GPWIN_VALID 23
#define PWES0_VALID 24
#define PWES1_VALID 25
Boolean recv_data = false; // receiving GPS data
Boolean recv_palt = false; // receing pressure alititude data
GPSSatDataType *satData; // Arrays for GPS satellite data
GPSPVTDataType *pvtData;
Boolean updatewind = false;
Boolean updatetime = false;
UInt8 curgpsfixquality;
Int8 curgpsfixtype = -1;
extern Boolean updatemap;
extern RECOData *recodata;
extern GPSSatDataType *satDataFlarm;
extern Boolean draw_log;
extern UInt8 FlarmRX;
extern Int16 FlarmVSep;
extern Int16 FlarmIdx;
extern Boolean Flarmpresent;
extern Boolean FlarmAlarm;
extern UInt32 utcsecs;
extern Boolean ManualDistChg;
extern double MGCBattHours;
extern double MCCurVal;
void nmea_parser(Char* serinp, UInt32 length, Boolean reset)
{
Int16 state = INVALID;
static Int16 next = 0;
static Boolean gpsver2 = false;
static Boolean gpgsa = false;
static Boolean gpgga = false;
static Int16 revert;
static Char NMEAbuf[85];
static Boolean gpsephavail = false;
UInt32 cur = 0;
double true_track;
double magnetic_track;
double groundspeed;
double bearing_to_destination;
double distance_to_destination;
double gpsvar;
double ballast;
static Char gpsutc[10] = "000000000";
Char gpsdtg[7];
Char gpsstat[5];
Char gpslat[21];
Char gpslatdir[5];
Char gpslng[22];
Char gpslngdir[5];
Char gpsvardir[5];
Char gpsnumsats[5];
Char tempchar[20];
DateTimeType tempdt;
UInt32 tempsecs;
double tempalt;
Int8 j;
Int8 i;
Boolean readposdata = true;
static UInt16 DeviationCount=0;
static Int8 NumGSVMsgs=0;
static Int8 CurGSVMsg=0;
static Int8 ExpectedGSVMsg=1;
static double gpsalt;
static double gpsgeoidaldiff;
double tempelev, tempdist;
Int16 tempnum;
double RelN, RelE;
static Int16 FlarmMaxIdx;
static Int16 FlarmMaxDist;
char mychksumstr[10];
UInt16 mychksum = 0;
double true_bearing, vmg;
static UInt32 last_GPGGA = 0;
static UInt32 last_GPRMC = 0;
if (reset) {
// HostTraceOutputTL(appErrorClass, " resetting");
next = 0;
MemSet(&NMEAbuf,sizeof(NMEAbuf),0);
gpsver2 = false;
gpgsa = false;
return;
}
if (data.config.nmeaxfertype == USEIQUESER) {
readposdata = false;
}
// HostTraceOutputTL(appErrorClass, "Inside nmea_parser");
while (cur<length) {
state = INVALID;
while( (cur<length) && (next<85) && (serinp[cur] != '$') && (serinp[cur] != '!') ) {
NMEAbuf[next]=serinp[cur];
next++;
cur++;
}
// HostTraceOutputTL(appErrorClass, "NMEAbuf1-|%s|", NMEAbuf);
if (next >= 85) {
/* Parsing error start over */
next=0;
}
if ((cur<length) && (serinp[cur] == '$'||serinp[cur] == '!')) {
cur++;
// HostTraceOutputTL(appErrorClass, "NMEAbuf2-|%s|", NMEAbuf);
if (NMEAbuf[0]!='G' && NMEAbuf[0]!='P' && NMEAbuf[0]!='L' && NMEAbuf[0]!='w' && NMEAbuf[0]!='R'){
/* Parsing error start over*/
next=0;
} else {
NMEAbuf[next]=0;
if (NMEAbuf[0] == 'w') {
// HostTraceOutputTL(appErrorClass, " W");
if (gpsvalidate(NMEAbuf)) {
state = W_VALID;
} else {
state = INVALID;
// HostTraceOutputTL(appErrorClass, " Bad W|%s|", NMEAbuf);
}
} else {
switch (NMEAbuf[2]) {
case 'A':
if ((NMEAbuf[3]=='I') && (NMEAbuf[4]=='B')) {
// HostTraceOutputTL(appErrorClass, " PCAIB");
if (gpsvalidate(NMEAbuf)) {
state = PCAIB_VALID;
} else {
state = INVALID;
// HostTraceOutputTL(appErrorClass, " Bad PCAIB|%s|", NMEAbuf);
}
} else if ((NMEAbuf[3]=='I') && (NMEAbuf[4]=='D')) {
// HostTraceOutputTL(appErrorClass, " PCAID");
if (gpsvalidate(NMEAbuf)) {
state = PCAID_VALID;
} else {
state = INVALID;
// HostTraceOutputTL(appErrorClass, " Bad PCAID|%s|", NMEAbuf);
}
} else if ((NMEAbuf[3]=='S') && (NMEAbuf[4]=='1')) {
// HostTraceOutputTL(appErrorClass, " PTAS1");
if (gpsvalidate(NMEAbuf)) {
state = PTAS1_VALID;
} else {
state = INVALID;
// HostTraceOutputTL(appErrorClass, " Bad PTAS1|%s|", NMEAbuf);
}
} else if ((NMEAbuf[3]=='N') && (NMEAbuf[4]=='1')) {
// HostTraceOutputTL(appErrorClass, " PZAN1");
if (gpsvalidate(NMEAbuf)) {
state = PZAN1_VALID;
} else {
state = INVALID;
// HostTraceOutputTL(appErrorClass, " Bad PZAN1|%s|", NMEAbuf);
}
}
break;
case 'B':
if ((NMEAbuf[3]=='5') && (NMEAbuf[4]=='0')) {
// HostTraceOutputTL(appErrorClass, " PBB50");
if (gpsvalidate(NMEAbuf)) {
state = PBB50_VALID;
} else {
state = INVALID;
// HostTraceOutputTL(appErrorClass, " Bad PBB50|%s|", NMEAbuf);
}
}
break;
case 'R':
if ((NMEAbuf[3]=='M') && (NMEAbuf[4]=='Z')) {
// HostTraceOutputTL(appErrorClass, " PGRMZ");
if (gpsvalidate(NMEAbuf) && readposdata) {
state = PGRMZ_VALID;
} else {
state = INVALID;
// HostTraceOutputTL(appErrorClass, " Bad PGRMZ|%s|", NMEAbuf);
}
} else if ((NMEAbuf[3]=='M') && (NMEAbuf[4]=='C')) {
// HostTraceOutputTL(appErrorClass, " GPRMC found");
if (gpsvalidate(NMEAbuf) && readposdata) {
state = GPRMC_VALID;
if ((data.config.nmeaxfertype == USEBT) && data.config.echoNMEA && (utcsecs > last_GPRMC + data.config.slowlogint)) {
last_GPRMC = utcsecs;
// re-transmit GPRMC data to pass through port
StrCopy(tempchar, "$");
TxDataAlt(tempchar);
TxDataAlt(NMEAbuf);
// transmit GPRMB data to pass through port
if (data.input.destination_valid) {
// construct GPRMB data and to pass through port
StrCopy(NMEAbuf, "$GPRMB,");
// GPS fix status
StrCat(NMEAbuf, data.logger.gpsstat);
StrCat(NMEAbuf, ",");
// cross track error
StrCat(NMEAbuf, "0.00,L,");
// origin waypoint ID (current position)
StrCat(NMEAbuf, "POS,");
// destination waypoint ID
StrCat(NMEAbuf, Left(data.inuseWaypoint.name,3));
StrCat(NMEAbuf, ",");
// waypoint latitude
LLToStringDM(data.inuseWaypoint.lat, tempchar, true, false, true, 2);
StrCat(NMEAbuf, Left(tempchar, 7));
if (data.inuseWaypoint.lat > 0.0) {
StrCat(NMEAbuf, ",N,");
} else {
StrCat(NMEAbuf, ",S,");
}
// waypoint longitude
LLToStringDM(data.inuseWaypoint.lon, tempchar, false, false, true, 2);
StrCat(NMEAbuf, Left(tempchar,8));
if (data.inuseWaypoint.lon > 0.0) {
StrCat(NMEAbuf, ",E,");
} else {
StrCat(NMEAbuf, ",W,");
}
// range
if (data.inuseWaypoint.distance < 999.0) {
StrCat(NMEAbuf, leftpad(DblToStr(data.inuseWaypoint.distance, 1), '0', 3));
} else {
StrCat(NMEAbuf, "999.9");
}
StrCat(NMEAbuf, ",");
// bearing (true)
true_bearing = nice_brg(data.inuseWaypoint.bearing - data.input.deviation.value);
StrCat(NMEAbuf, leftpad(DblToStr(true_bearing, 1), '0', 3));
StrCat(NMEAbuf, ",");
// velocity towards destination waypoint (VMG)
if ((data.input.ground_speed.valid == VALID) && (data.input.true_track.valid == VALID)) {
vmg = Cos(DegreesToRadians(true_bearing - data.input.true_track.value)) * data.input.ground_speed.value;
if (vmg < 0.0) vmg = 0.0;
StrCat(NMEAbuf, leftpad(DblToStr(vmg, 1), '0', 3));
} else {
StrCat(NMEAbuf,"000.0");
}
StrCat(NMEAbuf, ",");
// arrival flag (not arrived)
StrCat(NMEAbuf, "V");
// checksum
mychksum = 0;
for (i=1; i < StrLen(NMEAbuf); i++) {
mychksum^=NMEAbuf[i];
}
StrIToH(tempchar, mychksum);
length = StrLen(tempchar);
mychksumstr[0] = tempchar[length-2];
mychksumstr[1] = tempchar[length-1];
mychksumstr[2] = '\0';
StrCat(NMEAbuf,"*");
StrCat(NMEAbuf,mychksumstr);
// end of line
StrCatEOL(NMEAbuf, USESER);
// confirm checksum
if (gpsvalidate(&NMEAbuf[1])) TxDataAlt(NMEAbuf);
}
}
} else {
state = INVALID;
// HostTraceOutputTL(appErrorClass, " Bad GPRMC|%s|", NMEAbuf);
}
} else if ((NMEAbuf[3]=='M') && (NMEAbuf[4]=='B') && !data.config.usepalmway) {
// HostTraceOutputTL(appErrorClass, " GPRMB");
if (gpsvalidate(NMEAbuf)) {
state = GPRMB_VALID;
} else {
state = INVALID;
// HostTraceOutputTL(appErrorClass, " Bad GPRMB|%s|", NMEAbuf);
}
} else if ((NMEAbuf[3]=='M') && (NMEAbuf[4]=='E')) {
// HostTraceOutputTL(appErrorClass, " GPRME");
if (gpsvalidate(NMEAbuf) && readposdata) {
state = PGRME_VALID;
} else {
state = INVALID;
// HostTraceOutputTL(appErrorClass, " Bad GPRME|%s|", NMEAbuf);
}
}
break;
case 'G':
if ((NMEAbuf[3]=='G') && (NMEAbuf[4]=='A')) {
// HostTraceOutputTL(appErrorClass, " GPGGA");
if (data.config.usechksums) {
if (gpsvalidate(NMEAbuf) && readposdata) {
state = GPGGA_VALID;
if ((data.config.nmeaxfertype == USEBT) && data.config.echoNMEA && (utcsecs > last_GPGGA + data.config.slowlogint)) {
last_GPGGA = utcsecs;
// re-transmit GPGGA data to pass through port
StrCopy(tempchar, "$");
TxDataAlt(tempchar);
TxDataAlt(NMEAbuf);
}
} else {
state = INVALID;
// HostTraceOutputTL(appErrorClass, " Bad GPGGA|%s|", NMEAbuf);
}
} else {
if (readposdata) {
state = GPGGA_VALID;
} else {
state = INVALID;
}
}
} else if ((NMEAbuf[3]=='S') && (NMEAbuf[4]=='A')) {
// HostTraceOutputTL(appErrorClass, " GPGSA");
if (gpsvalidate(NMEAbuf) && readposdata) {
state = GPGSA_VALID;
} else {
state = INVALID;
// HostTraceOutputTL(appErrorClass, " Bad GPGSA|%s|", NMEAbuf);
}
} else if ((NMEAbuf[3]=='S') && (NMEAbuf[4]=='V')) {
// HostTraceOutputTL(appErrorClass, " GPGSV");
if (gpsvalidate(NMEAbuf) && readposdata && (data.config.flightcomp != FLARMCOMP) &&
FrmGetActiveFormID() == form_sat_status) {
state = GPGSV_VALID;
} else {
state = INVALID;
// HostTraceOutputTL(appErrorClass, " Bad GPGSV|%s|", NMEAbuf);
}
} else if ((NMEAbuf[3]=='N') && (NMEAbuf[4]=='S')) {
// HostTraceOutputTL(appErrorClass, " PMGNST");
if (gpsvalidate(NMEAbuf) && (FrmGetActiveFormID() == form_sat_status)) {
state = PMGNST_VALID;
} else {
state = INVALID;
// HostTraceOutputTL(appErrorClass, " Bad PMGNST|%s|", NMEAbuf);
}
}
break;
case 'C':
if ((NMEAbuf[3]=='S') && (NMEAbuf[5]=='1')) {
// HostTraceOutputTL(appErrorClass, " PGCS");
if (gpsvalidate(NMEAbuf)) {
state = PGCS_VALID;
} else {
state = INVALID;
// HostTraceOutputTL(appErrorClass, " Bad PGCS|%s|", NMEAbuf);
}
} else if (NMEAbuf[3]=='O') {
if (NMEAbuf[4]=='A') {
// HostTraceOutputTL(appErrorClass, " RECOA");
if (gpsvalidate(NMEAbuf)) {
state = RECOA_VALID;
} else {
state = INVALID;
// HostTraceOutputTL(appErrorClass, " Bad RECOA|%s|", NMEAbuf);
}
} else if (NMEAbuf[4]=='B') {
// HostTraceOutputTL(appErrorClass, " RECOB");
if (gpsvalidate(NMEAbuf)) {
state = RECOB_VALID;
} else {
state = INVALID;
// HostTraceOutputTL(appErrorClass, " Bad RECOB|%s|", NMEAbuf);
}
}
}
break;
case 'S':
if ((NMEAbuf[3]=='R') && (NMEAbuf[4]=='A')) {
// HostTraceOutputTL(appErrorClass, " PFSRA");
if (gpsvalidate(NMEAbuf)) {
state = PFSRA_VALID;
} else {
state = INVALID;
// HostTraceOutputTL(appErrorClass, " Bad PFSRA|%s|", NMEAbuf);
}
}
break;
case 'W':
if ((NMEAbuf[3]=='P') && (NMEAbuf[4]=='0')) {
// HostTraceOutputTL(appErrorClass, " LXWP0");
if (gpsvalidate(NMEAbuf)) {
state = LXWP0_VALID;
} else {
state = INVALID;
// HostTraceOutputTL(appErrorClass, " Bad LXWP0|%s|", NMEAbuf);
}
} else if ((NMEAbuf[3]=='I') && (NMEAbuf[4]=='N')) {
// HostTraceOutputTL(appErrorClass, " GPWIN");
if (gpsvalidate(NMEAbuf)) {
state = GPWIN_VALID;
} else {
state = INVALID;
// HostTraceOutputTL(appErrorClass, " Bad GPWIN|%s|", NMEAbuf);
}
}
break;
case 'L':
if ((NMEAbuf[3]=='C')) {
// HostTraceOutputTL(appErrorClass, " PILC");
if (gpsvalidate(NMEAbuf)) {
state = PILC_VALID;
} else {
state = INVALID;
// HostTraceOutputTL(appErrorClass, " Bad PILC|%s|", NMEAbuf);
}
} else if ((NMEAbuf[3]=='A') && (NMEAbuf[4]=='U')) {
// HostTraceOutputTL(appErrorClass, " PFLAU");
if (gpsvalidate(NMEAbuf)) {
state = PFLAU_VALID;
} else {
state = INVALID;
// HostTraceOutputTL(appErrorClass, " Bad PFLAU|%s|", NMEAbuf);
}
} else if ((NMEAbuf[3]=='A') && (NMEAbuf[4]=='A')) {
// HostTraceOutputTL(appErrorClass, " PFLAA");
if (gpsvalidate(NMEAbuf)) {
state = PFLAA_VALID;
} else {
state = INVALID;
// HostTraceOutputTL(appErrorClass, " Bad PFLAA|%s|", NMEAbuf);
}
}
break;
case 'E':
if ((NMEAbuf[3]=='S') && (NMEAbuf[4]=='0')) {
// HostTraceOutputTL(appErrorClass, " PWES0");
if (gpsvalidate(NMEAbuf)) {
state = PWES0_VALID;
} else {
state = INVALID;
// HostTraceOutputTL(appErrorClass, " Bad PWES0|%s|", NMEAbuf);
}
} else if ((NMEAbuf[3]=='S') && (NMEAbuf[4]=='1')) {
// HostTraceOutputTL(appErrorClass, " PWES1");
if (gpsvalidate(NMEAbuf)) {
state = PWES1_VALID;
} else {
state = INVALID;
// HostTraceOutputTL(appErrorClass, " Bad PWES1|%s|", NMEAbuf);
}
}
break;
default:
break;
}
}
next=0;
}
}
// Parse Valid NMEA Sentences
switch (state) {
// NMEA183 Sentence to give distance & bearing to target waypoint
// Not Necessary if using internal waypoints
case GPRMB_VALID:
{
// HostTraceOutputTL(appErrorClass, " Parser RMB");
// MFH Getting waypoint id
GetField(NMEAbuf, 5, tempchar);
tempelev = 0.0;
StrNCopy(data.input.destination_name, tempchar, 16);
// decode altitude if in NNNXXX format with XXX = meters / 10
if ((data.config.wpformat == WPGPWPLALT) && (StrLen(tempchar) == 6)) {
if (StrChr("0123456789",tempchar[4]) && StrChr("0123456789",tempchar[5]) && StrChr("0123456789",tempchar[6])) {
tempelev = pround((double)StrAToI(Right(tempchar,3)) * 10 / ALTMETCONST, 0);
// HostTraceOutputTL(appErrorClass, "tempelev = %s", DblToStr(tempelev,0));
}
}
// PG getting destination lat
GetField(NMEAbuf, 6, tempchar);
StrNCopy(gpslat, tempchar, 20);
GetField(NMEAbuf, 7, tempchar);
StrCopy(gpslatdir, tempchar);
data.input.destination_lat = DegMinStringToLatLon(gpslat, gpslatdir[0]);
// HostTraceOutputTL(appErrorClass, "Destination Lat |%s|", DblToStr(data.input.destination_lat,2));
// PG getting destination lon
GetField(NMEAbuf, 8, tempchar);
StrNCopy(gpslng, tempchar, 21);
GetField(NMEAbuf, 9, tempchar);
StrCopy(gpslngdir, tempchar);
data.input.destination_lon = DegMinStringToLatLon(gpslng, gpslngdir[0]);
// HostTraceOutputTL(appErrorClass, "Destination Lon |%s|", DblToStr(data.input.destination_lon,2));
// MFH Getting distance to waypoint in nautical miles
GetField(NMEAbuf, 10, tempchar);
distance_to_destination=StrToDbl(tempchar);
// MFH Getting bearing to waypoint in true degrees then convert to mag
GetField(NMEAbuf, 11, tempchar);
bearing_to_destination=StrToDbl(tempchar)+data.input.deviation.value;
if (bearing_to_destination >= 360.0) {
bearing_to_destination -= 360.0;
}
if (bearing_to_destination < 0.0) {
bearing_to_destination = 360.0 + bearing_to_destination;
}
if (StrLen(tempchar) > 2) {
// valid goto waypoint from GPS
data.input.distance_to_destination.value = distance_to_destination;
data.input.distance_to_destination.valid = VALID;
data.input.bearing_to_destination.value = bearing_to_destination;
data.input.bearing_to_destination.valid = VALID;
data.input.destination_elev = tempelev;
} else if (!ManualDistChg) {
// no valid goto waypoint from GPS
data.input.destination_name[0] = 0;
data.input.destination_elev = 0.0;
data.input.distance_to_destination.valid = VALID;
data.input.bearing_to_destination.valid = NOT_VALID;
data.input.bearing_to_destination.value = 000.0;
} else {
// manual distance change
data.input.distance_to_destination.valid = VALID;
data.input.bearing_to_destination.valid = NOT_VALID;
data.input.bearing_to_destination.value = 000.0;
}
data.input.destination_valid = false;
data.application.changed = 1;
break;
} // End of GPRMB_VALID:
// NMEA183 Sentence to give course, speed and determine magnetic variation
// Also gives GPS Time, Status & LAT/LNG for logging
case GPRMC_VALID:
{
// HostTraceOutputTL(appErrorClass, " Parser RMC");
if (!gpsver2) {
GetField(NMEAbuf, 1, tempchar);
if (StrCompare(tempchar, "") == 0) {
// If the incoming data is blank, then use the previous gpsutc if not blank.
if (StrCompare(gpsutc, "") == 0) {
// If both the incoming data and gpsutc are blank, use system time adjusted for the timezone
SecondsToDateOrTimeString(TimGetSeconds(), gpsutc, 2, data.config.timezone*-1);
}
} else {
if (StrLen(tempchar) > 6) {
StrNCopy(gpsutc, tempchar, 6);
gpsutc[6] = '\0';
} else {
StrCopy(gpsutc, tempchar);
}
if (gpsutc[4] == '6') {
// This is to fix the time output by some GPS' which
// sometimes outputs a time of 144060 instead of 144100.
// Magellan GPS Companion seems to do this.
StringToDateOrTime(gpsutc, &tempdt, 1);
tempsecs=TimDateTimeToSeconds(&tempdt);
SecondsToDateOrTimeString(tempsecs, gpsutc, 2, 0);
}
}
GetField(NMEAbuf, 2, tempchar);
StrCopy(gpsstat, tempchar);
GetField(NMEAbuf, 3, tempchar);
StrNCopy(gpslat, tempchar, 20);
GetField(NMEAbuf, 4, tempchar);
StrCopy(gpslatdir, tempchar);
GetField(NMEAbuf, 5, tempchar);
StrNCopy(gpslng, tempchar, 21);
GetField(NMEAbuf, 6, tempchar);
StrCopy(gpslngdir, tempchar);
StrCopy(gpsnumsats, "XX");
}
GetField(NMEAbuf, 7, tempchar);
if (StrCompare(tempchar, "") == 0) {
groundspeed = 0.0;
} else {
groundspeed = StrToDbl(tempchar);
}
GetField(NMEAbuf, 8, tempchar);
if (StrCompare(tempchar, "") == 0) {
true_track = 0.0;
} else {
true_track = StrToDbl(tempchar);
}
GetField(NMEAbuf, 9, tempchar);
StrCopy(gpsdtg, tempchar);
GetField(NMEAbuf, 10, tempchar);
if (StrCompare(tempchar, "") == 0) {
gpsvar = 0.0;
} else {
gpsvar = StrToDbl(tempchar);
}
GetField(NMEAbuf, 11, tempchar);
if (StrCompare(tempchar, "") == 0) {
StrCopy(gpsvardir, "W");
} else {
StrCopy(gpsvardir, tempchar);
}
// HostTraceOutputTL(appErrorClass, "RMC:true_track:|%s|", DblToStr(true_track, 2));
if (true_track < 0.0 || true_track > 360.0) {
// HostTraceOutputTL(appErrorClass, "RMC:setting gpsstat to V");
// StrCopy(data.logger.gpsstat, "V");
// StrCopy(data.input.gpsnumsats, gpsnumsats);
// state = INVALID;
true_track=nice_brg(true_track);
// return;
}
if (true_track == 360.0) {
true_track = 0.0;
}
if ((data.config.flightcomp == RECOCOMP) || data.config.gpscalcdev) {
if (DeviationCount == 0) {
// Calculate the GPS Variation
data.input.deviation.value = GetDeviation();
// HostTraceOutputTL(appErrorClass, "gpsvar-|%s|", DblToStr(gpsvar, 1));
data.input.deviation.valid = VALID;
}
DeviationCount++;
if (DeviationCount == 120) {
DeviationCount = 0;
}
} else {
//Mag = True - Var(E) or + Var(W)
//so
//Magnetic = True + variation
//True = Magnetic - variation
if (StrCompare(gpsvardir, "E")==0) {
gpsvar *= -1.0;
}
data.input.deviation.value = gpsvar;
data.input.deviation.valid = VALID;
}
magnetic_track=nice_brg(true_track + data.input.deviation.value);
data.input.ground_speed.value=groundspeed;
data.input.ground_speed.valid=VALID;
if (groundspeed >= 1.0) {
data.input.magnetic_track.value=magnetic_track;
data.input.magnetic_track.valid=VALID;
data.input.true_track.value=true_track;
data.input.true_track.valid=VALID;
}
StrCopy(data.logger.gpsdtg, gpsdtg);
if (!gpsver2) {
// HostTraceOutputTL(appErrorClass, "RMC:setting gpsstat to gpsstat");
StrCopy(data.logger.gpsstat, gpsstat);
StrCopy(gpsnumsats, "XX");
StrCopy(data.input.gpsnumsats, gpsnumsats);
StrCopy(data.logger.gpsutc, gpsutc);
if ((StrCompare(data.logger.gpsstat, "A") == 0) && !draw_log) {
StrCopy(data.logger.gpslat, gpslat);
StrCopy(data.logger.gpslatdir, gpslatdir);
data.input.gpslatdbl = DegMinStringToLatLon(data.logger.gpslat, data.logger.gpslatdir[0]);
data.input.coslat = cos(DegreesToRadians(data.input.gpslatdbl));
StrCopy(data.logger.gpslng, gpslng);
StrCopy(data.logger.gpslngdir, gpslngdir);
data.input.gpslngdbl = DegMinStringToLatLon(data.logger.gpslng, data.logger.gpslngdir[0]);
}
// Calculate Lift Here RMC
if (data.config.usepalt) {
CalcLift(data.logger.pressalt, data.logger.gpsutc, -9999.9, NORESET);
// HostTraceOutputTL(appErrorClass, "Calc Lift GGA-GPSALT");
} else {
// } else if (data.config.flightcomp == NOCOMP ||
// data.config.flightcomp == B50COMP ||
// data.config.flightcomp == LXCOMP ||
// data.config.flightcomp == LXVARCOMP ||
// data.config.flightcomp == RECOCOMP ||
// data.config.flightcomp == B50VLKCOMP ||
// data.config.flightcomp == B50LXCOMP ||
// data.config.flightcomp == SN10COMP ||
// data.config.flightcomp == VOLKSCOMP ||
// data.config.flightcomp == GPSNAVCOMP ||
// data.config.flightcomp == FLARMCOMP ||
// data.config.flightcomp == C302ACOMP ||
// data.config.flightcomp == EWCOMP) {
// Might be able to use this instead of the above
// } else if (data.config.flightcomp != C302COMP) {
// Calculate using gps altitude because these types of computers do not output any alititude values
// or the operator has chosen not to use the pressure altitude info
CalcLift(data.logger.gpsalt, data.logger.gpsutc, -9999.9, NORESET);
// HostTraceOutputTL(appErrorClass, "Calc Lift RMC-NOCOMP||B50COMP||LXCOMP||LXVARCOMP||EWCOMP||B50VLKCOMP||B50LXCOMP||RECOCOMP");
}
if (FrmGetActiveFormID() == form_sat_status && curgpsfixtype == -1) {
if (StrCompare(data.logger.gpsstat, "V") == 0) {
curgpsfixquality = 0;
} else {
curgpsfixquality = 1;
}
SetFixStatus();
}
}
if ((StrCompare(data.logger.gpsstat, "V") == 0) || draw_log) {
updatetime = false;
} else {
updatemap = true;
updatetime = true;
// This is the only place that updatewind should be set otherwise it causes havoc with
// the thermal/cruise code
// HostTraceOutputTL(appErrorClass, "RMC:setting updatewind to true");
updatewind = true;
}
data.application.changed = 1;
break;
} //End of GPRMC_VALID
// NMEA183 V2 Sentence to give GPS Altitude in meters
// as well as Number of SATS tracked and Height above Ellipsoid
case GPGGA_VALID:
{
// HostTraceOutputTL(appErrorClass, " Parser GGA");
gpsver2 = true;
gpgga = true;
revert = 0;
GetField(NMEAbuf, 1, tempchar);
if (StrCompare(tempchar, "") == 0) {
// If the incoming data is blank, then use the previous gpsutc if not blank.
if (StrCompare(gpsutc, "") == 0) {
// If both the incoming data and gpsutc are blank, use system time adjusted for the timezone
SecondsToDateOrTimeString(TimGetSeconds(), gpsutc, 2, data.config.timezone*-1);
// HostTraceOutputTL(appErrorClass, " Setting gpsutc to system utc time");
}
} else {
if (StrLen(tempchar) > 6) {
StrNCopy(gpsutc, tempchar, 6);
gpsutc[6] = '\0';
} else {
StrCopy(gpsutc, tempchar);
}
if (gpsutc[4] == '6') {
// This is to fix the time output by some GPS' which
// sometimes outputs a time of 144060 instead of 144100.
// Magellan GPS Companion seems to do this.
StringToDateOrTime(gpsutc, &tempdt, 1);
tempsecs=TimDateTimeToSeconds(&tempdt);
SecondsToDateOrTimeString(tempsecs, gpsutc, 2, 0);
}
}
// HostTraceOutputTL(appErrorClass, " gpsutc=|%s|", gpsutc);
GetField(NMEAbuf, 2, tempchar);
StrNCopy(gpslat, tempchar, 20);
GetField(NMEAbuf, 3, tempchar);
StrCopy(gpslatdir, tempchar);
GetField(NMEAbuf, 4, tempchar);
StrNCopy(gpslng, tempchar, 21);
GetField(NMEAbuf, 5, tempchar);
StrCopy(gpslngdir, tempchar);
/* Fix Quality */
GetField(NMEAbuf, 6, tempchar);
curgpsfixquality = StrAToI(tempchar);
if (curgpsfixquality <= 0) {
// HostTraceOutputTL(appErrorClass, "GGA:setting gpsstat to V");
StrCopy(gpsstat, "V");
} else {
// HostTraceOutputTL(appErrorClass, "GGA:setting gpsstat to A");
StrCopy(gpsstat, "A");
}
/* Number of SATS Tracked */
GetField(NMEAbuf, 7, tempchar);
StrCopy(gpsnumsats, tempchar);
/* Horizontal Dillution of Precision (HDOP) in meters */
GetField(NMEAbuf, 8, tempchar);
// data.input.eph = StrToDbl(tempchar);
// Only want to use the calculated value if the Garmin
// eph value is not present
if (!gpsephavail) {
// Using this formula to get a rough 2-sigma ehp value
data.input.eph = StrToDbl(tempchar) * 5.1 * 2.0;
}
/* Altitude in meters above MSL */
GetField(NMEAbuf, 9, tempchar);
gpsalt = StrToDbl(tempchar);
/* Height above ellipsoid */
GetField(NMEAbuf, 11, tempchar);
gpsgeoidaldiff = StrToDbl(tempchar);
if (data.config.gpsaltref == GPSWGS84) {
data.input.gpsgeoidaldiff = gpsgeoidaldiff;
} else {
data.input.gpsgeoidaldiff = 0.0;
}
// HostTraceOutputTL(appErrorClass, "GGA:setting gpsstat to gpsstat");
StrCopy(data.logger.gpsstat, gpsstat);
StrCopy(data.input.gpsnumsats, gpsnumsats);
data.input.siu = (Int8)StrAToI(gpsnumsats);
/* simulate midnight in SeeYou
tempnum = StrToDbl(ToNumeric(Left(gpsutc,2)))+22;
StrCopy(tempchar, DblToStr(tempnum,0));
StrCat(tempchar, Right(gpsutc,4));
StrCopy(gpsutc, tempchar);
*/
StrCopy(data.logger.gpsutc, gpsutc);
if ((StrCompare(data.logger.gpsstat, "A") == 0) && !draw_log) {
StrCopy(data.logger.gpslat, gpslat);
StrCopy(data.logger.gpslatdir, gpslatdir);
data.input.gpslatdbl = DegMinStringToLatLon(data.logger.gpslat, data.logger.gpslatdir[0]);
data.input.coslat = cos(DegreesToRadians(data.input.gpslatdbl));
StrCopy(data.logger.gpslng, gpslng);
StrCopy(data.logger.gpslngdir, gpslngdir);
data.input.gpslngdbl = DegMinStringToLatLon(data.logger.gpslng, data.logger.gpslngdir[0]);
}
//Altitude in meters divided by ALTMETCONST to convert to feet.
//Normally the gpsgeoidaldiff would be added to convert the MSL value in field 9 to get
//HAE.
//however we aren't displaying HAE anymore. Instead this value is now subtracted to
//account those GPS' which put HAE in field 9 and the correction value in field 11. By
//subtracting the value we end up with an MSL value.
data.logger.gpsalt = (data.config.gpsmsladj*ALTMETCONST+gpsalt-data.input.gpsgeoidaldiff)/ALTMETCONST;
if (data.config.pressaltsrc == GPSALT) {
data.logger.pressalt = gpsalt/ALTMETCONST;
}
// Made the conversion to feet here so that it could be used above without conversion
data.input.gpsgeoidaldiff /= ALTMETCONST;
gpsgeoidaldiff /= ALTMETCONST; // Contains the info from the NMEA stream in feet
gpsalt /= ALTMETCONST; // Contains the info from the NMEA in feet
// Calculate Lift Here GGA
if (data.config.usepalt) {
CalcLift(data.logger.pressalt, data.logger.gpsutc, -9999.9, NORESET);
// HostTraceOutputTL(appErrorClass, "Calc Lift GGA-GPSALT");
} else {
// } else if (data.config.flightcomp == NOCOMP ||
// data.config.flightcomp == B50COMP ||
// data.config.flightcomp == LXCOMP ||
// data.config.flightcomp == LXVARCOMP ||
// data.config.flightcomp == RECOCOMP || //Not sure if this is needed
// data.config.flightcomp == B50VLKCOMP ||
// data.config.flightcomp == B50LXCOMP ||
// data.config.flightcomp == SN10COMP ||
// data.config.flightcomp == VOLKSCOMP ||
// data.config.flightcomp == GPSNAVCOMP ||
// data.config.flightcomp == FLARMCOMP ||
// data.config.flightcomp == C302ACOMP ||
// data.config.flightcomp == EWCOMP) {
// Might be able to use this instead of the above
// } else if (data.config.flightcomp != C302COMP) {
// Calculate using gps altitude because these types of computers do not output any alititude values
// or the operator has chosen not to use the pressure altitude info
CalcLift(data.logger.gpsalt, data.logger.gpsutc, -9999.9, NORESET);
// HostTraceOutputTL(appErrorClass, "Calc Lift GGA-NOCOMP||B50COMP||LXCOMP||LXVARCOMP||EWCOMP||B50VLKCOMP||B50LXCOMP||RECOCOMP");
}
if ((StrCompare(data.logger.gpsstat, "V") == 0) || draw_log) {
updatetime = false;
} else {
updatemap = true;
updatetime = true;
}
if (FrmGetActiveFormID() == form_sat_status && !gpgsa) {
// Have to fake the fixtype based on the number of satellites
if (data.input.siu <= 3) {
curgpsfixtype = 2;
} else {
curgpsfixtype = 3;
}
SetFixStatus();
}
data.application.changed = 1;
break;
} //End of GPGGA_VALID:
// NMEA183 V2 Sentence to give the list of SVN's being used in the
// current satellite fix.
case GPGSA_VALID:
{
// HostTraceOutputTL(appErrorClass, " Parser GSA");
gpgsa = true;
GetField(NMEAbuf, 2, tempchar);
curgpsfixtype = (Int8)StrAToI(tempchar);
for (i=0; i<12; i++) {
GetField(NMEAbuf, i+3, tempchar);
if(StrLen(tempchar) > 0) {
data.input.svns[i] = (Int8)StrAToI(tempchar);
} else {
data.input.svns[i] = -1;
}
}
// force iPhone fix type
if (device.StyleTapPDA == STIPHONE) {
if (gpgga) {
curgpsfixtype = 3;
} else {
curgpsfixtype = 2;
}
SetFixStatus();
}
data.application.changed = 1;
break;
} // End of GPGSA_VALID:
// NMEA183 V2 Sentence to get the stats for each satellite being used
case GPGSV_VALID:
{
// HostTraceOutputTL(appErrorClass, " Parser GSV");
GetField(NMEAbuf, 1, tempchar);
NumGSVMsgs = (Int8)StrAToI(tempchar);
GetField(NMEAbuf, 2, tempchar);
CurGSVMsg = (Int8)StrAToI(tempchar);
// HostTraceOutputTL(appErrorClass, "CurGSVMsg-|%hd|", (Int16)CurGSVMsg);
// HostTraceOutputTL(appErrorClass, "NumGSVMsgs-|%hd|", (Int16)NumGSVMsgs);
if (CurGSVMsg == ExpectedGSVMsg) {
// HostTraceOutputTL(appErrorClass, "ExpectedGSVMsg Found-|%hd|", (Int16)ExpectedGSVMsg);
if (ExpectedGSVMsg == NumGSVMsgs) {
ExpectedGSVMsg = 1;
} else {
ExpectedGSVMsg++;
}
j = (CurGSVMsg-1)*4;
for (i=0; i<4; i++) {
satData[j+i].status = 0;
satData[j+i].status |= satNoDif;
satData[j+i].status |= satNoEph;
GetField(NMEAbuf, i*4+4, tempchar);
if(StrLen(tempchar) > 1) {
satData[j+i].svid = (Int8)StrAToI(tempchar);
} else {
satData[j+i].svid = gpsInvalidSVID;
}
GetField(NMEAbuf, i*4+5, tempchar);
if(StrLen(tempchar) > 0) {
satData[j+i].elevation = (float)DegreesToRadians(StrToDbl(tempchar));
}
GetField(NMEAbuf, i*4+6, tempchar);
if(StrLen(tempchar) > 0) {
satData[j+i].azimuth = (float)DegreesToRadians(StrToDbl(tempchar));
}
GetField(NMEAbuf, i*4+7, tempchar);
if(StrLen(tempchar) > 0) {
if (StrLen(tempchar) > 1) {
satData[j+i].snr = (float)StrToDbl(tempchar) * 100.0;
if (satData[j+i].snr == 0.0) {
// Setting to -100 to indicate no signal
satData[j+i].snr = -100.0;
}
} else {
// Setting to -100 to indicate no signal
satData[j+i].snr = -100.0;
}
}
}
} else {
ExpectedGSVMsg = 1;
}
// Compare the Sats being tracked to the ones used for the fix to
// decide whether to set the ephemeris status
for (i=0; i<GPSMAXSATS; i++) {
if (satData[i].svid != gpsInvalidSVID) {
for (j=0; j<GPSMAXSATS; j++) {