forked from rtklibexplorer/RTKLIB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.c
1779 lines (1631 loc) · 62.9 KB
/
solution.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
/*------------------------------------------------------------------------------
* solution.c : solution functions
*
* Copyright (C) 2007-2020 by T.TAKASU, All rights reserved.
*
* references :
* [1] National Marine Electronic Association and International Marine
* Electronics Association, NMEA 0183 version 4.10, August 1, 2012
* [2] NMEA 0183 Talker Identifier Mnemonics, March 3, 2019
* (https://www.nmea.org/content/STANDARDS/NMEA_0183_Standard)
*
* version : $Revision:$ $Date:$
* history : 2007/11/03 1.0 new
* 2009/01/05 1.1 add function outsols(), outsolheads(),
* setsolformat(), outsolexs, outsolex
* 2009/04/02 1.2 add dummy fields in NMEA mesassage
* fix bug to format lat/lon as deg-min-sec
* 2009/04/14 1.3 add age and ratio field to solution
* 2009/11/25 1.4 add function readsolstat()
* 2010/02/14 1.5 fix bug on output of gpstime at week boundary
* 2010/07/05 1.6 added api:
* initsolbuf(),freesolbuf(),addsol(),getsol(),
* inputsol(),outprcopts(),outprcopt()
* modified api:
* readsol(),readsolt(),readsolstat(),
* readsolstatt(),outsolheads(),outsols(),
* outsolexs(),outsolhead(),outsol(),outsolex(),
* outnmea_rmc(),outnmea_gga(),outnmea_gsa(),
* outnmea_gsv()
* deleted api:
* setsolopt(),setsolformat()
* 2010/08/14 1.7 fix bug on initialize solution buffer (2.4.0_p2)
* suppress enu-solution if base pos not available
* (2.4.0_p3)
* 2010/08/16 1.8 suppress null record if solution is not available
* (2.4.0_p4)
* 2011/01/23 1.9 fix bug on reading nmea solution data
* add api freesolstatbuf()
* 2012/02/05 1.10 fix bug on output nmea gpgsv
* 2013/02/18 1.11 support nmea GLGSA,GAGSA,GLCSV,GACSV sentence
* 2013/09/01 1.12 fix bug on presentation of nmea time tag
* 2015/02/11 1.13 fix bug on checksum of $GLGSA and $GAGSA
* fix bug on satellite id of $GAGSA
* 2016/01/17 1.14 support reading NMEA GxZDA
* ignore NMEA talker ID
* 2016/07/30 1.15 suppress output if std is over opt->maxsolstd
* 2017/06/13 1.16 support output/input of velocity solution
* 2018/10/10 1.17 support reading solution status file
* 2020/11/30 1.18 add NMEA talker ID GQ and GI (NMEA 0183 4.11)
* add NMEA GQ/GB/GI-GSA/GSV sentences
* change talker ID GP to GN for NMEA RMC/GGA
* change newline to "\r\n" in SOLF_LLH,XYZ,ENU
* add reading age information in NMEA GGA
* use integer types in stdint.h
* suppress warnings
*-----------------------------------------------------------------------------*/
#include <ctype.h>
#include "rtklib.h"
/* constants and macros ------------------------------------------------------*/
#define SQR(x) ((x)<0.0?-(x)*(x):(x)*(x))
#define SQRT(x) ((x)<0.0||(x)!=(x)?0.0:sqrt(x))
/* https://gpsd.gitlab.io/gpsd/NMEA.html#_talker_ids says ...
GA ~ Galileo Positioning System
GB ~ BeiDou(China)
GI ~ NavIC, IRNSS(India)
GL ~ GLONASS, according to IEIC 61162 - 1
GN ~ Combination of multiple satellite systems(NMEA 1083)
GP ~ Global Positioning System receiver
GQ ~ QZSS regional GPS augmentation system(Japan)
https://talk.newagtalk.com/forums/thread-view.asp?tid=877179&mid=7732209#M7732209
Says "The industry pretty much standardized on using GPxxx regardless of what
constellations are being used in the solution. To be completely NMEA compliant
the second letter should correspond to the constellations being used. Don't
forget Galileo and Beidou have their own unique letters too."
And rnx2rtkp GNxxx output is not plotted by RTKPLOT, whereas GPxxx output is
So, This NMEA Talker ID used to be "GN" but now its "GP" */
#define NMEA_TID "GP" /* NMEA talker ID for RMC and GGA sentences */
#define MAXFIELD 64 /* max number of fields in a record */
#define MAXNMEA 256 /* max length of nmea sentence */
#define KNOT2M 0.514444444 /* m/sec --> knot */
static const int nmea_sys[]={ /* NMEA systems */
SYS_GPS|SYS_SBS,SYS_GLO,SYS_GAL,SYS_CMP,SYS_QZS,SYS_IRN,0
};
static const char *nmea_tid[]={ /* NMEA talker IDs [2] */
"GP","GL","GA","GB","GQ","GI",""
};
static const int nmea_sid[]={ /* NMEA system IDs [1] table 21 */
1,2,3,4,5,6,0
};
static const int nmea_solq[]={ /* NMEA GPS quality indicator [1] */
/* 0=Fix not available or invalid */
/* 1=GPS SPS Mode, fix valid */
/* 2=Differential GPS, SPS Mode, fix valid */
/* 3=GPS PPS Mode, fix valid */
/* 4=Real Time Kinematic. System used in RTK mode with fixed integers */
/* 5=Float RTK. Satellite system used in RTK mode, floating integers */
/* 6=Estimated (dead reckoning) Mode */
/* 7=Manual Input Mode */
/* 8=Simulation Mode */
SOLQ_NONE ,SOLQ_SINGLE, SOLQ_DGPS, SOLQ_PPP , SOLQ_FIX,
SOLQ_FLOAT,SOLQ_DR , SOLQ_NONE, SOLQ_NONE, SOLQ_NONE
};
/* solution option to field separator ----------------------------------------*/
static const char *opt2sep(const solopt_t *opt)
{
if (!*opt->sep) return " ";
else if (!strcmp(opt->sep,"\\t")) return "\t";
return opt->sep;
}
/* separate fields -----------------------------------------------------------*/
static int tonum(char *buff, const char *sep, double *v)
{
int n,len=(int)strlen(sep);
char *p,*q;
for (p=buff,n=0;n<MAXFIELD;p=q+len) {
if ((q=strstr(p,sep))) *q='\0';
if (*p) v[n++]=atof(p);
if (!q) break;
}
return n;
}
/* sqrt of covariance --------------------------------------------------------*/
static double sqvar(double covar)
{
return covar<0.0?-sqrt(-covar):sqrt(covar);
}
/* convert ddmm.mm in nmea format to deg -------------------------------------*/
static double dmm2deg(double dmm)
{
return floor(dmm/100.0)+fmod(dmm,100.0)/60.0;
}
/* convert time in nmea format to time ---------------------------------------*/
static void septime(double t, double *t1, double *t2, double *t3)
{
*t1=floor(t/10000.0);
t-=*t1*10000.0;
*t2=floor(t/100.0);
*t3=t-*t2*100.0;
}
/* solution to covariance ----------------------------------------------------*/
static void soltocov(const sol_t *sol, double *P)
{
P[0] =sol->qr[0]; /* xx or ee */
P[4] =sol->qr[1]; /* yy or nn */
P[8] =sol->qr[2]; /* zz or uu */
P[1]=P[3]=sol->qr[3]; /* xy or en */
P[5]=P[7]=sol->qr[4]; /* yz or nu */
P[2]=P[6]=sol->qr[5]; /* zx or ue */
}
/* covariance to solution ----------------------------------------------------*/
static void covtosol(const double *P, sol_t *sol)
{
sol->qr[0]=(float)P[0]; /* xx or ee */
sol->qr[1]=(float)P[4]; /* yy or nn */
sol->qr[2]=(float)P[8]; /* zz or uu */
sol->qr[3]=(float)P[1]; /* xy or en */
sol->qr[4]=(float)P[5]; /* yz or nu */
sol->qr[5]=(float)P[2]; /* zx or ue */
}
/* solution to velocity covariance -------------------------------------------*/
static void soltocov_vel(const sol_t *sol, double *P)
{
P[0] =sol->qv[0]; /* xx */
P[4] =sol->qv[1]; /* yy */
P[8] =sol->qv[2]; /* zz */
P[1]=P[3]=sol->qv[3]; /* xy */
P[5]=P[7]=sol->qv[4]; /* yz */
P[2]=P[6]=sol->qv[5]; /* zx */
}
/* velocity covariance to solution -------------------------------------------*/
static void covtosol_vel(const double *P, sol_t *sol)
{
sol->qv[0]=(float)P[0]; /* xx */
sol->qv[1]=(float)P[4]; /* yy */
sol->qv[2]=(float)P[8]; /* zz */
sol->qv[3]=(float)P[1]; /* xy */
sol->qv[4]=(float)P[5]; /* yz */
sol->qv[5]=(float)P[2]; /* zx */
}
/* decode NMEA RMC (Recommended Minimum Specific GNSS Data) sentence ---------*/
static int decode_nmearmc(char **val, int n, sol_t *sol)
{
double tod=0.0,lat=0.0,lon=0.0,vel=0.0,dir=0.0,date=0.0,ang=0.0,ep[6];
double pos[3]={0};
char act=' ',ns='N',ew='E',mew='E',mode='A';
int i;
trace(4,"decode_nmearmc: n=%d\n",n);
for (i=0;i<n;i++) {
switch (i) {
case 0: tod =atof(val[i]); break; /* time in utc (hhmmss) */
case 1: act =*val[i]; break; /* A=active,V=void */
case 2: lat =atof(val[i]); break; /* latitude (ddmm.mmm) */
case 3: ns =*val[i]; break; /* N=north,S=south */
case 4: lon =atof(val[i]); break; /* longitude (dddmm.mmm) */
case 5: ew =*val[i]; break; /* E=east,W=west */
case 6: vel =atof(val[i]); break; /* speed (knots) */
case 7: dir =atof(val[i]); break; /* track angle (deg) */
case 8: date=atof(val[i]); break; /* date (ddmmyy) */
case 9: ang =atof(val[i]); break; /* magnetic variation */
case 10: mew =*val[i]; break; /* E=east,W=west */
case 11: mode=*val[i]; break; /* mode indicator (>nmea 2) */
/* A=autonomous,D=differential */
/* E=estimated,N=not valid,S=simulator */
}
}
if ((act!='A'&&act!='V')||(ns!='N'&&ns!='S')||(ew!='E'&&ew!='W')) {
trace(3,"invalid nmea rmc format\n");
return 0;
}
pos[0]=(ns=='S'?-1.0:1.0)*dmm2deg(lat)*D2R;
pos[1]=(ew=='W'?-1.0:1.0)*dmm2deg(lon)*D2R;
septime(date,ep+2,ep+1,ep);
septime(tod,ep+3,ep+4,ep+5);
ep[0]+=ep[0]<80.0?2000.0:1900.0;
sol->time=utc2gpst(epoch2time(ep));
pos2ecef(pos,sol->rr);
sol->stat=mode=='D'?SOLQ_DGPS:SOLQ_SINGLE;
sol->ns=0;
sol->type=0; /* position type = xyz */
trace(5,"decode_nmearmc: %s rr=%.3f %.3f %.3f stat=%d ns=%d vel=%.2f dir=%.0f ang=%.0f mew=%c mode=%c\n",
time_str(sol->time,0),sol->rr[0],sol->rr[1],sol->rr[2],sol->stat,sol->ns,
vel,dir,ang,mew,mode);
return 2; /* update time */
}
/* decode NMEA ZDA (Time and Date) sentence ----------------------------------*/
static int decode_nmeazda(char **val, int n, sol_t *sol)
{
double tod=0.0,ep[6]={0};
int i;
trace(4,"decode_nmeazda: n=%d\n",n);
for (i=0;i<n;i++) {
switch (i) {
case 0: tod =atof(val[i]); break; /* time in utc (hhmmss) */
case 1: ep[2]=atof(val[i]); break; /* day (0-31) */
case 2: ep[1]=atof(val[i]); break; /* mon (1-12) */
case 3: ep[0]=atof(val[i]); break; /* year */
}
}
septime(tod,ep+3,ep+4,ep+5);
sol->time=utc2gpst(epoch2time(ep));
sol->ns=0;
trace(5,"decode_nmeazda: %s\n",time_str(sol->time,0));
return 2; /* update time */
}
/* decode NMEA GGA (Global Positioning System Fix Data) sentence -------------*/
static int decode_nmeagga(char **val, int n, sol_t *sol)
{
gtime_t time;
double tod=0.0,lat=0.0,lon=0.0,hdop=0.0,alt=0.0,msl=0.0,ep[6],tt;
double pos[3]={0},age=0.0;
char ns='N',ew='E',ua=' ',um=' ';
int i,solq=0,nrcv=0;
trace(4,"decode_nmeagga: n=%d\n",n);
for (i=0;i<n;i++) {
switch (i) {
case 0: tod =atof(val[i]); break; /* UTC of position (hhmmss) */
case 1: lat =atof(val[i]); break; /* Latitude (ddmm.mmm) */
case 2: ns =*val[i]; break; /* N=north,S=south */
case 3: lon =atof(val[i]); break; /* Longitude (dddmm.mmm) */
case 4: ew =*val[i]; break; /* E=east,W=west */
case 5: solq=atoi(val[i]); break; /* GPS quality indicator */
case 6: nrcv=atoi(val[i]); break; /* # of satellites in use */
case 7: hdop=atof(val[i]); break; /* HDOP */
case 8: alt =atof(val[i]); break; /* Altitude MSL */
case 9: ua =*val[i]; break; /* unit (M) */
case 10: msl =atof(val[i]); break; /* Geoid separation */
case 11: um =*val[i]; break; /* unit (M) */
case 12: age =atof(val[i]); break; /* Age of differential */
}
}
if ((ns!='N'&&ns!='S')||(ew!='E'&&ew!='W')) {
trace(3,"invalid nmea gga format\n");
return 0;
}
if (sol->time.time==0) {
trace(3,"no date info for nmea gga\n");
return 0;
}
pos[0]=(ns=='N'?1.0:-1.0)*dmm2deg(lat)*D2R;
pos[1]=(ew=='E'?1.0:-1.0)*dmm2deg(lon)*D2R;
pos[2]=alt+msl;
time2epoch(sol->time,ep);
septime(tod,ep+3,ep+4,ep+5);
time=utc2gpst(epoch2time(ep));
tt=timediff(time,sol->time);
if (tt<-43200.0) sol->time=timeadd(time, 86400.0);
else if (tt> 43200.0) sol->time=timeadd(time,-86400.0);
else sol->time=time;
pos2ecef(pos,sol->rr);
sol->stat=0<=solq&&solq<=8?nmea_solq[solq]:SOLQ_NONE;
sol->ns=nrcv;
sol->age=(float)age;
sol->type=0; /* position type = xyz */
trace(5,"decode_nmeagga: %s rr=%.3f %.3f %.3f stat=%d ns=%d hdop=%.1f ua=%c um=%c\n",
time_str(sol->time,0),sol->rr[0],sol->rr[1],sol->rr[2],sol->stat,sol->ns,
hdop,ua,um);
return 1;
}
/* test NMEA sentence header -------------------------------------------------*/
static int test_nmea(const char *buff)
{
if (strlen(buff)<6||buff[0]!='$') return 0;
return !strncmp(buff+1,"GP",2)||!strncmp(buff+1,"GA",2)|| /* NMEA 4.10 [1] */
!strncmp(buff+1,"GL",2)||!strncmp(buff+1,"GN",2)||
!strncmp(buff+1,"GB",2)||!strncmp(buff+1,"GQ",2)|| /* NMEA 4.11 [2] */
!strncmp(buff+1,"GI",2)||
!strncmp(buff+1,"BD",2)||!strncmp(buff+1,"QZ",2); /* extension */
}
/* test solution status message header ---------------------------------------*/
static int test_solstat(const char *buff)
{
if (strlen(buff)<7||buff[0]!='$') return 0;
return !strncmp(buff+1,"POS" ,3)||!strncmp(buff+1,"VELACC",6)||
!strncmp(buff+1,"CLK" ,3)||!strncmp(buff+1,"ION" ,3)||
!strncmp(buff+1,"TROP",4)||!strncmp(buff+1,"HWBIAS",6)||
!strncmp(buff+1,"TRPG",4)||!strncmp(buff+1,"AMB" ,3)||
!strncmp(buff+1,"SAT" ,3);
}
/* decode NMEA sentence ------------------------------------------------------*/
static int decode_nmea(char *buff, sol_t *sol)
{
char *p,*q,*val[MAXFIELD]={0};
int n=0;
trace(4,"decode_nmea: buff=%s\n",buff);
/* parse fields */
for (p=buff;*p&&n<MAXFIELD;p=q+1) {
if ((q=strchr(p,','))||(q=strchr(p,'*'))) {
val[n++]=p; *q='\0';
}
else break;
}
if (n<1) {
return 0;
}
if (!strcmp(val[0]+3,"RMC")) { /* $xxRMC */
return decode_nmearmc(val+1,n-1,sol);
}
else if (!strcmp(val[0]+3,"ZDA")) { /* $xxZDA */
return decode_nmeazda(val+1,n-1,sol);
}
else if (!strcmp(val[0]+3,"GGA")) { /* $xxGGA */
return decode_nmeagga(val+1,n-1,sol);
}
return 0;
}
/* decode solution time ------------------------------------------------------*/
static char *decode_soltime(char *buff, const solopt_t *opt, gtime_t *time)
{
double v[MAXFIELD];
char *p,*q;
int n;
trace(4,"decode_soltime:\n");
if (opt->posf==SOLF_STAT) {
return buff;
}
const char *sep = opt2sep(opt);
size_t sep_len = strlen(sep);
if (opt->posf==SOLF_GSIF) {
if (sscanf(buff,"%lf %lf %lf %lf:%lf:%lf",v,v+1,v+2,v+3,v+4,v+5)<6) {
return NULL;
}
*time=timeadd(epoch2time(v),-12.0*3600.0);
if (!(p=strchr(buff,':'))||!(p=strchr(p+1,':'))) return NULL;
for (p++;isdigit((int)*p)||*p=='.';) p++;
p=strstr(p,sep);
if (p) return p+sep_len;
else return NULL;
}
/* yyyy/mm/dd hh:mm:ss or yyyy mm dd hh:mm:ss */
if (sscanf(buff,"%lf/%lf/%lf %lf:%lf:%lf",v,v+1,v+2,v+3,v+4,v+5)>=6) {
if (v[0]<100.0) {
v[0]+=v[0]<80.0?2000.0:1900.0;
}
*time=epoch2time(v);
if (opt->times==TIMES_UTC) {
*time=utc2gpst(*time);
}
else if (opt->times==TIMES_JST) {
*time=utc2gpst(timeadd(*time,-9*3600.0));
}
if (!(p=strchr(buff,':'))||!(p=strchr(p+1,':'))) return NULL;
for (p++;isdigit((int)*p)||*p=='.';) p++;
p=strstr(p,sep);
if (p) return p+sep_len;
else return NULL;
}
else { /* wwww ssss */
for (p=buff,n=0;n<2;p=q+sep_len) {
q=strstr(p,sep);
if (!q) return NULL;
*q='\0';
if (sscanf(p,"%lf",v+n)==1) n++;
}
if (n>=2&&0.0<=v[0]&&v[0]<=3000.0&&0.0<=v[1]&&v[1]<604800.0) {
*time=gpst2time((int)v[0],v[1]);
return p;
}
}
return NULL;
}
/* decode x/y/z-ecef ---------------------------------------------------------*/
static int decode_solxyz(char *buff, const solopt_t *opt, sol_t *sol)
{
double val[MAXFIELD],P[9]={0};
int i=0,j,n;
const char *sep=opt2sep(opt);
trace(4,"decode_solxyz:\n");
if ((n=tonum(buff,sep,val))<3) return 0;
for (j=0;j<3;j++) {
sol->rr[j]=val[i++]; /* xyz */
}
if (i<n) sol->stat=(uint8_t)val[i++];
if (i<n) sol->ns =(uint8_t)val[i++];
if (i+3<=n) {
P[0]=SQR(val[i]); i++; /* sdx */
P[4]=SQR(val[i]); i++; /* sdy */
P[8]=SQR(val[i]); i++; /* sdz */
if (i+3<=n) {
P[1]=P[3]=SQR(val[i]); i++; /* sdxy */
P[5]=P[7]=SQR(val[i]); i++; /* sdyz */
P[2]=P[6]=SQR(val[i]); i++; /* sdzx */
}
covtosol(P,sol);
}
if (i<n) sol->age =(float)val[i++];
if (i<n) sol->ratio=(float)val[i++];
if (i+3<=n) { /* velocity */
for (j=0;j<3;j++) {
sol->rr[j+3]=val[i++]; /* xyz */
}
}
if (i+3<=n) {
for (j=0;j<9;j++) P[j]=0.0;
P[0]=SQR(val[i]); i++; /* sdx */
P[4]=SQR(val[i]); i++; /* sdy */
P[8]=SQR(val[i]); i++; /* sdz */
if (i+3<n) {
P[1]=P[3]=SQR(val[i]); i++; /* sdxy */
P[5]=P[7]=SQR(val[i]); i++; /* sdyz */
P[2]=P[6]=SQR(val[i]); i++; /* sdzx */
}
covtosol_vel(P,sol);
}
sol->type=0; /* position type = xyz */
if (MAXSOLQ<sol->stat) sol->stat=SOLQ_NONE;
return 1;
}
/* decode lat/lon/height -----------------------------------------------------*/
static int decode_solllh(char *buff, const solopt_t *opt, sol_t *sol)
{
double val[MAXFIELD],pos[3],vel[3],Q[9]={0},P[9];
int i=0,j,n;
const char *sep=opt2sep(opt);
trace(4,"decode_solllh:\n");
n=tonum(buff,sep,val);
if (!opt->degf) {
if (n<3) return 0;
pos[0]=val[i++]*D2R; /* lat/lon/hgt (ddd.ddd) */
pos[1]=val[i++]*D2R;
pos[2]=val[i++];
}
else {
if (n<7) return 0;
pos[0]=dms2deg(val )*D2R; /* lat/lon/hgt (ddd mm ss) */
pos[1]=dms2deg(val+3)*D2R;
pos[2]=val[6];
i+=7;
}
pos2ecef(pos,sol->rr);
if (i<n) sol->stat=(uint8_t)val[i++];
if (i<n) sol->ns =(uint8_t)val[i++];
if (i+3<=n) {
Q[4]=SQR(val[i]); i++; /* sdn */
Q[0]=SQR(val[i]); i++; /* sde */
Q[8]=SQR(val[i]); i++; /* sdu */
if (i+3<n) {
Q[1]=Q[3]=SQR(val[i]); i++; /* sdne */
Q[2]=Q[6]=SQR(val[i]); i++; /* sdeu */
Q[5]=Q[7]=SQR(val[i]); i++; /* sdun */
}
covecef(pos,Q,P);
covtosol(P,sol);
}
if (i<n) sol->age =(float)val[i++];
if (i<n) sol->ratio=(float)val[i++];
if (i+3<=n) { /* velocity */
vel[1]=val[i++]; /* vel-n */
vel[0]=val[i++]; /* vel-e */
vel[2]=val[i++]; /* vel-u */
enu2ecef(pos,vel,sol->rr+3);
}
if (i+3<=n) {
for (j=0;j<9;j++) Q[j]=0.0;
Q[4]=SQR(val[i]); i++; /* sdn */
Q[0]=SQR(val[i]); i++; /* sde */
Q[8]=SQR(val[i]); i++; /* sdu */
if (i+3<=n) {
Q[1]=Q[3]=SQR(val[i]); i++; /* sdne */
Q[2]=Q[6]=SQR(val[i]); i++; /* sdeu */
Q[5]=Q[7]=SQR(val[i]); i++; /* sdun */
}
covecef(pos,Q,P);
covtosol_vel(P,sol);
}
sol->type=0; /* position type = xyz */
if (MAXSOLQ<sol->stat) sol->stat=SOLQ_NONE;
return 1;
}
/* decode e/n/u-baseline -----------------------------------------------------*/
static int decode_solenu(char *buff, const solopt_t *opt, sol_t *sol)
{
double val[MAXFIELD],Q[9]={0};
int i=0,j,n;
const char *sep=opt2sep(opt);
trace(4,"decode_solenu:\n");
if ((n=tonum(buff,sep,val))<3) return 0;
for (j=0;j<3;j++) {
sol->rr[j]=val[i++]; /* enu */
}
if (i<n) sol->stat=(uint8_t)val[i++];
if (i<n) sol->ns =(uint8_t)val[i++];
if (i+3<=n) {
Q[0]=SQR(val[i]); i++; /* sde */
Q[4]=SQR(val[i]); i++; /* sdn */
Q[8]=SQR(val[i]); i++; /* sdu */
if (i+3<=n) {
Q[1]=Q[3]=SQR(val[i]); i++; /* sden */
Q[5]=Q[7]=SQR(val[i]); i++; /* sdnu */
Q[2]=Q[6]=SQR(val[i]); i++; /* sdue */
}
covtosol(Q,sol);
}
if (i<n) sol->age =(float)val[i++];
if (i<n) sol->ratio=(float)val[i++];
if (i+3<=n) { /* velocity */
for (j=0;j<3;j++) {
sol->rr[j+3]=val[i++]; /* vel-enu */
}
}
if (i+3<=n) {
for (j=0;j<9;j++) Q[j]=0.0;
Q[0]=val[i]*val[i]; i++; /* sde */
Q[4]=val[i]*val[i]; i++; /* sdn */
Q[8]=val[i]*val[i]; i++; /* sdu */
if (i+3<=n) {
Q[1]=Q[3]=SQR(val[i]); i++; /* sden */
Q[5]=Q[7]=SQR(val[i]); i++; /* sdnu */
Q[2]=Q[6]=SQR(val[i]); i++; /* sdue */
}
covtosol_vel(Q,sol);
}
sol->type=1; /* position type = enu */
if (MAXSOLQ<sol->stat) sol->stat=SOLQ_NONE;
return 1;
}
/* decode solution status ----------------------------------------------------*/
static int decode_solsss(char *buff, sol_t *sol)
{
double tow,pos[3],std[3]={0};
int i,week,solq;
trace(4,"decode_solsss:\n");
if (sscanf(buff,"$POS,%d,%lf,%d,%lf,%lf,%lf,%lf,%lf,%lf",&week,&tow,&solq,
pos,pos+1,pos+2,std,std+1,std+2)<6) {
return 0;
}
if (week<=0||norm(pos,3)<=0.0||solq==SOLQ_NONE) {
return 0;
}
sol->time=gpst2time(week,tow);
for (i=0;i<6;i++) {
sol->rr[i]=i<3?pos[i]:0.0;
sol->qr[i]=i<3?(float)SQR(std[i]):0.0f;
sol->dtr[i]=0.0;
}
sol->ns=0;
sol->age=sol->ratio=sol->thres=0.0f;
sol->type=0; /* position type = xyz */
sol->stat=solq;
return 1;
}
/* decode GSI F solution -----------------------------------------------------*/
static int decode_solgsi(char *buff, const solopt_t *opt, sol_t *sol)
{
double val[MAXFIELD];
int i=0,j;
trace(4,"decode_solgsi:\n");
if (tonum(buff," ",val)<3) return 0;
for (j=0;j<3;j++) {
sol->rr[j]=val[i++]; /* xyz */
}
sol->stat=SOLQ_FIX;
return 1;
}
/* decode solution position --------------------------------------------------*/
static int decode_solpos(char *buff, const solopt_t *opt, sol_t *sol)
{
sol_t sol0={{0}};
char *p=buff;
trace(4,"decode_solpos: buff=%s\n",buff);
*sol=sol0;
/* decode solution time */
if (!(p=decode_soltime(p,opt,&sol->time))) {
return 0;
}
/* decode solution position */
switch (opt->posf) {
case SOLF_XYZ : return decode_solxyz(p,opt,sol);
case SOLF_LLH : return decode_solllh(p,opt,sol);
case SOLF_ENU : return decode_solenu(p,opt,sol);
case SOLF_GSIF: return decode_solgsi(p,opt,sol);
}
return 0;
}
/* decode reference position -------------------------------------------------*/
static void decode_refpos(char *buff, const solopt_t *opt, double *rb)
{
double val[MAXFIELD],pos[3];
int i,n;
const char *sep=opt2sep(opt);
trace(3,"decode_refpos: buff=%s\n",buff);
if ((n=tonum(buff,sep,val))<3) return;
if (opt->posf==SOLF_XYZ) { /* xyz */
for (i=0;i<3;i++) rb[i]=val[i];
}
else if (opt->degf==0) { /* lat/lon/hgt (ddd.ddd) */
pos[0]=val[0]*D2R;
pos[1]=val[1]*D2R;
pos[2]=val[2];
pos2ecef(pos,rb);
}
else if (opt->degf==1&&n>=7) { /* lat/lon/hgt (ddd mm ss) */
pos[0]=dms2deg(val )*D2R;
pos[1]=dms2deg(val+3)*D2R;
pos[2]=val[6];
pos2ecef(pos,rb);
}
}
/* decode solution -----------------------------------------------------------*/
static int decode_sol(char *buff, const solopt_t *opt, sol_t *sol, double *rb)
{
char *p;
trace(4,"decode_sol: buff=%s\n",buff);
if (test_nmea(buff)) { /* decode nmea */
return decode_nmea(buff,sol);
}
else if (test_solstat(buff)) { /* decode solution status */
return decode_solsss(buff,sol);
}
if (!strncmp(buff,COMMENTH,1)) { /* reference position */
if (!strstr(buff,"ref pos")&&!strstr(buff,"slave pos")) return 0;
if (!(p=strchr(buff,':'))) return 0;
decode_refpos(p+1,opt,rb);
return 0;
}
/* decode position record */
return decode_solpos(buff,opt,sol);
}
/* decode solution options ---------------------------------------------------*/
static void decode_solopt(char *buff, solopt_t *opt)
{
char *p;
trace(4,"decode_solhead: buff=%s\n",buff);
if (strncmp(buff,COMMENTH,1)&&strncmp(buff,"+",1)) return;
if (strstr(buff,"GPST")) opt->times=TIMES_GPST;
else if (strstr(buff,"UTC" )) opt->times=TIMES_UTC;
else if (strstr(buff,"JST" )) opt->times=TIMES_JST;
if ((p=strstr(buff,"x-ecef(m)"))) {
opt->posf=SOLF_XYZ;
opt->degf=0;
strncpy(opt->sep,p+9,1);
opt->sep[1]='\0';
}
else if ((p=strstr(buff,"latitude(d'\")"))) {
opt->posf=SOLF_LLH;
opt->degf=1;
strncpy(opt->sep,p+14,1);
opt->sep[1]='\0';
}
else if ((p=strstr(buff,"latitude(deg)"))) {
opt->posf=SOLF_LLH;
opt->degf=0;
strncpy(opt->sep,p+13,1);
opt->sep[1]='\0';
}
else if ((p=strstr(buff,"e-baseline(m)"))) {
opt->posf=SOLF_ENU;
opt->degf=0;
strncpy(opt->sep,p+13,1);
opt->sep[1]='\0';
}
else if (strstr(buff,"+SITE/INF")) { /* gsi f2/f3 solution */
opt->times=TIMES_GPST;
opt->posf=SOLF_GSIF;
opt->degf=0;
strcpy(opt->sep," ");
}
}
/* read solution option ------------------------------------------------------*/
static void readsolopt(FILE *fp, solopt_t *opt)
{
char buff[MAXSOLMSG+1];
int i;
trace(3,"readsolopt:\n");
for (i=0;fgets(buff,sizeof(buff),fp)&&i<100;i++) { /* only 100 lines */
/* decode solution options */
decode_solopt(buff,opt);
}
}
/* input solution data from stream ---------------------------------------------
* input solution data from stream
* args : uint8_t data I stream data
* gtime_t ts I start time (ts.time==0: from start)
* gtime_t te I end time (te.time==0: to end)
* double tint I time interval (0: all)
* int qflag I quality flag (0: all)
* solbuf_t *solbuf IO solution buffer
* return : status (1:solution received,0:no solution,-1:disconnect received)
*-----------------------------------------------------------------------------*/
extern int inputsol(uint8_t data, gtime_t ts, gtime_t te, double tint,
int qflag, const solopt_t *opt, solbuf_t *solbuf)
{
sol_t sol={{0}};
int stat;
trace(4,"inputsol: data=0x%02x\n",data);
if (data=='$'||(!isprint(data)&&data!='\r'&&data!='\n')) { /* sync header */
solbuf->nb=0;
}
if (data!='\r'&&data!='\n') {
solbuf->buff[solbuf->nb++]=data;
}
if (data!='\n'&&solbuf->nb<MAXSOLMSG) return 0; /* sync trailer */
solbuf->buff[solbuf->nb]='\0';
solbuf->nb=0;
/* check disconnect message */
if (!strncmp((char *)solbuf->buff,MSG_DISCONN,strlen(MSG_DISCONN)-2)) {
trace(3,"disconnect received\n");
return -1;
}
/* decode solution */
sol.time=solbuf->time;
if ((stat=decode_sol((char *)solbuf->buff,opt,&sol,solbuf->rb))>0) {
solbuf->time=sol.time; /* update current time */
if (stat!=1) return 0;
}
if (stat!=1||!screent(sol.time,ts,te,tint)||(qflag&&sol.stat!=qflag)) {
return 0;
}
/* add solution to solution buffer */
return addsol(solbuf,&sol);
}
/* read solution data --------------------------------------------------------*/
static int readsoldata(FILE *fp, gtime_t ts, gtime_t te, double tint, int qflag,
const solopt_t *opt, solbuf_t *solbuf)
{
int c;
trace(3,"readsoldata:\n");
while ((c=fgetc(fp))!=EOF) {
/* input solution */
inputsol((uint8_t)c,ts,te,tint,qflag,opt,solbuf);
}
return solbuf->n>0;
}
/* compare solution data -----------------------------------------------------*/
static int cmpsol(const void *p1, const void *p2)
{
sol_t *q1=(sol_t *)p1,*q2=(sol_t *)p2;
double tt=timediff(q1->time,q2->time);
return tt<-0.0?-1:(tt>0.0?1:0);
}
/* sort solution data --------------------------------------------------------*/
static int sort_solbuf(solbuf_t *solbuf)
{
sol_t *solbuf_data;
trace(4,"sort_solbuf: n=%d\n",solbuf->n);
if (solbuf->n<=0) return 0;
if (!(solbuf_data=(sol_t *)realloc(solbuf->data,sizeof(sol_t)*solbuf->n))) {
trace(1,"sort_solbuf: memory allocation error\n");
free(solbuf->data); solbuf->data=NULL; solbuf->n=solbuf->nmax=0;
return 0;
}
solbuf->data=solbuf_data;
qsort(solbuf->data,solbuf->n,sizeof(sol_t),cmpsol);
solbuf->nmax=solbuf->n;
solbuf->start=0;
solbuf->end=solbuf->n-1;
return 1;
}
/* read solutions data from solution files -------------------------------------
* read solution data from solution files
* args : char *files[] I solution files
* int nfile I number of files
* (gtime_t ts) I start time (ts.time==0: from start)
* (gtime_t te) I end time (te.time==0: to end)
* (double tint) I time interval (0: all)
* (int qflag) I quality flag (0: all)
* solbuf_t *solbuf O solution buffer
* return : status (1:ok,0:no data or error)
*-----------------------------------------------------------------------------*/
extern int readsolt(const char *files[], int nfile, gtime_t ts, gtime_t te,
double tint, int qflag, solbuf_t *solbuf)
{
FILE *fp;
solopt_t opt=solopt_default;
int i;
trace(3,"readsolt: nfile=%d\n",nfile);
initsolbuf(solbuf,0,0);
for (i=0;i<nfile;i++) {
if (!(fp=fopen(files[i],"rb"))) {
trace(2,"readsolt: file open error %s\n",files[i]);
continue;
}
/* read solution options in header */
readsolopt(fp,&opt);
rewind(fp);
/* read solution data */
if (!readsoldata(fp,ts,te,tint,qflag,&opt,solbuf)) {
trace(2,"readsolt: no solution in %s\n",files[i]);
}
fclose(fp);
}
return sort_solbuf(solbuf);
}
extern int readsol(const char *files[], int nfile, solbuf_t *sol)
{
gtime_t time={0};
trace(3,"readsol: nfile=%d\n",nfile);
return readsolt(files,nfile,time,time,0.0,0,sol);
}
/* add solution data to solution buffer ----------------------------------------
* add solution data to solution buffer
* args : solbuf_t *solbuf IO solution buffer
* sol_t *sol I solution data
* return : status (1:ok,0:error)
*-----------------------------------------------------------------------------*/
extern int addsol(solbuf_t *solbuf, const sol_t *sol)
{
sol_t *solbuf_data;
trace(4,"addsol:\n");
if (solbuf->cyclic) { /* ring buffer */
if (solbuf->nmax<=1) return 0;
solbuf->data[solbuf->end]=*sol;
if (++solbuf->end>=solbuf->nmax) solbuf->end=0;
if (solbuf->start==solbuf->end) {
if (++solbuf->start>=solbuf->nmax) solbuf->start=0;
}
else solbuf->n++;
return 1;
}
if (solbuf->n>=solbuf->nmax) {
solbuf->nmax=solbuf->nmax==0?8192:solbuf->nmax*2;
if (!(solbuf_data=(sol_t *)realloc(solbuf->data,sizeof(sol_t)*solbuf->nmax))) {
trace(1,"addsol: memory allocation error\n");
free(solbuf->data); solbuf->data=NULL; solbuf->n=solbuf->nmax=0;
return 0;
}
solbuf->data=solbuf_data;
}
solbuf->data[solbuf->n++]=*sol;
return 1;
}
/* get solution data from solution buffer --------------------------------------
* get solution data by index from solution buffer
* args : solbuf_t *solbuf I solution buffer
* int index I index of solution (0...)
* return : solution data pointer (NULL: no solution, out of range)
*-----------------------------------------------------------------------------*/
extern sol_t *getsol(solbuf_t *solbuf, int index)
{
trace(4,"getsol: index=%d\n",index);
if (index<0||solbuf->n<=index) return NULL;
if ((index=solbuf->start+index)>=solbuf->nmax) {
index-=solbuf->nmax;
}
return solbuf->data+index;
}
/* initialize solution buffer --------------------------------------------------
* initialize position solutions
* args : solbuf_t *solbuf I solution buffer
* int cyclic I solution data buffer type (0:linear,1:cyclic)
* int nmax I initial number of solution data
* return : status (1:ok,0:error)
*-----------------------------------------------------------------------------*/
extern void initsolbuf(solbuf_t *solbuf, int cyclic, int nmax)
{
#if 0
gtime_t time0={0};
#endif
int i;
trace(3,"initsolbuf: cyclic=%d nmax=%d\n",cyclic,nmax);
solbuf->n=solbuf->nmax=solbuf->start=solbuf->end=solbuf->nb=0;
solbuf->cyclic=cyclic;
#if 0
solbuf->time=time0;
#endif
solbuf->data=NULL;
for (i=0;i<3;i++) {
solbuf->rb[i]=0.0;
}
if (cyclic) {
if (nmax<=2) nmax=2;
if (!(solbuf->data=malloc(sizeof(sol_t)*nmax))) {
trace(1,"initsolbuf: memory allocation error\n");
return;
}
solbuf->nmax=nmax;
}
}
/* free solution ---------------------------------------------------------------
* free memory for solution buffer
* args : solbuf_t *solbuf I solution buffer
* return : none
*-----------------------------------------------------------------------------*/
extern void freesolbuf(solbuf_t *solbuf)