-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpony.c
2882 lines (2495 loc) · 92.2 KB
/
pony.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
// Aug-2022
// PONY core source code
#include <stdlib.h>
#include <math.h>
#include <limits.h>
#include "pony.h"
// core functions to be used in host application
// basic
char pony_add_plugin(void(*newplugin)(void)); // add plugin to the plugin execution list, input: pointer to plugin function, output: OK/not OK (1/0)
char pony_init (char* ); // initialize the bus, except for core, input: configuration string (see description), output: OK/not OK (1/0)
char pony_step (void ); // step through the plugin execution list, output: OK/not OK (1/0)
char pony_terminate (void ); // terminate operation, output: OK/not OK (1/0)
// advanced scheduling
char pony_remove_plugin (void(*plugin )(void) ); // remove all instances of the plugin from the plugin execution list, input: pointer to plugin function, output: OK/not OK (1/0)
char pony_replace_plugin (void(*oldplugin)(void), void(*newplugin)(void)); // replace all instances of the plugin by another one, input: pointers to old and new plugin functions, output: OK/not OK (1/0)
char pony_schedule_plugin (void(*newplugin)(void), int cycle, int shift ); // add scheduled plugin to the plugin execution list, input: pointer to plugin function, cycle, shift, output: OK/not OK (1/0)
char pony_reschedule_plugin(void(*plugin )(void), int cycle, int shift ); // reschedule all instances of the plugin in the plugin execution list, input: pointer to plugin function, new cycle, new shift, output: OK/not OK (1/0)
char pony_suspend_plugin (void(*plugin )(void) ); // suspend all instances of the plugin in the plugin execution list, input: pointer to plugin function, output: OK/not OK (1/0)
char pony_resume_plugin (void(*plugin )(void) ); // resume all instances of the plugin in the plugin execution list, input: pointer to plugin function, output: OK/not OK (1/0)
// bus instance
static pony_struct pony_bus = {
PONY_BUS_VERSION, // ver
pony_add_plugin, // add_plugin
pony_init, // init
pony_step, // step
pony_terminate, // terminate
pony_remove_plugin, // remove_plugin
pony_replace_plugin, // replace_plugin
pony_schedule_plugin, // schedule_plugin
pony_reschedule_plugin, // reschedule_plugin
pony_suspend_plugin, // suspend plugin
pony_resume_plugin, // resume plugin
{ NULL, 0, 0, UINT_MAX, 0 } // core.plugins, core.plugin_count, core.exit_plugin_id, core.host_termination
};
pony_struct* pony = &pony_bus;
// service subroutines
/*
allocate memory with size validation and NULL-assignment
input:
void** ptr --- pointer to a pointer to the desired memory block
const int specified size --- specified array size to be validated
size_t* actual_size --- pointer to store actual (validated and allocated) array size, provided non-NULL pointer
const size_t element_size --- size of each array element, e.g. sizeof(double)
const size_t default_size --- default array size if validation fails
const size_t max_size --- maximum allowed array size
return value:
1 if successful
0 otherwise (ptr==NULL or memory allocation failed)
*/
char pony_alloc_null(void** ptr, const int specified_size, size_t* actual_size, const size_t element_size, const size_t default_size, const size_t max_size)
{
size_t size;
char res;
// defaults
size = 0;
res = 0;
if (ptr != NULL) {
// validate size
if (specified_size > max_size)
size = max_size;
else if (specified_size >= 0 )
size = specified_size;
else
size = default_size;
// allocate memory
if (size > 0) // if nonzero size requested
*ptr = calloc(size, element_size);
else // if no elements required
*ptr = NULL;
// verify
if (size == 0 || *ptr != NULL)
res = 1;
}
// store size
if (actual_size != NULL)
*actual_size = size;
return res;
}
/*
free memory with pointer NULL-check and NULL-assignment
input:
void** ptr --- pointer to a pointer to the desired memory block
*/
void pony_free_null(void** ptr)
{
if (*ptr == NULL)
return;
free(*ptr);
*ptr = NULL;
}
/*
locate parameter group within a configuration string
input:
char* groupname --- pointer to a group identifier (see documentation) or empty string to locate a substring that is outside of any group
char* cfgstr --- pointer to a configuration string to parse
size_t cfglen --- number of characters in cfgstr to parse
output:
char** groupptr --- pointer to a pointer to the starting character of the group contents within a configuration string
int* grouplen --- pointer to a number of characters in the group contents
return value:
1 if the requested group found and grouplen > 0
0 otherwise
example:
groupname = "gnss:"
cfgstr = "{gnss: {gps: eph_in="gpsa.nav", obs_in="gpsa.obs"}}, out="sol.txt""
cfglen = 66
*/
char pony_locatecfggroup(const char* groupname, char* cfgstr, const size_t cfglen, char** groupptr, size_t* grouplen)
{
size_t i, j;
int group_layer;
char group_found = 0;
*groupptr = NULL;
*grouplen = 0;
if (cfgstr == NULL)
return 0;
// locate configuration substring that is outside of any group
if (groupname[0] == '\0') {
for (i = 0; i < cfglen && cfgstr[i]; i++) {
// skip all non-printable characters, blank spaces and commas between groups
for (; i < cfglen && cfgstr[i] && (cfgstr[i] <= ' ' || cfgstr[i] == ','); i++);
// if no group started at this point
if (cfgstr[i] != '{')
break;
// if a group started
else {
group_layer = 1;
while (i < cfglen && group_layer > 0 && cfgstr[i]) {
i++;
if (cfgstr[i] == '{')
group_layer++;
if (cfgstr[i] == '}')
group_layer--;
}
}
}
// skip all non-printable characters, blank spaces and commas between groups
for (; i < cfglen && cfgstr[i] && (cfgstr[i] <= ' ' || cfgstr[i] == ','); i++);
// start from this point
*groupptr = cfgstr + i;
// determine the length, counting until the end of the string or when a group started or ended
for (; (*groupptr)[*grouplen] && (*groupptr)[*grouplen] != '{' && (*groupptr)[*grouplen] != '}'; (*grouplen)++);
}
// locate configuration substring inside a requested group
else {
i = 0;
while (i < cfglen && cfgstr[i] && !group_found) {
// skip all non-printable characters, blank spaces and commas between groups
for (; i < cfglen && cfgstr[i] && (cfgstr[i] <= ' ' || cfgstr[i] == ','); i++);
// if a group started
if (cfgstr[i] == '{') {
group_layer = 1;
// skip all non-printable characters and blank spaces at the beginning of the group
for (i++; i < cfglen && cfgstr[i] && (cfgstr[i] <= ' '); i++);
// check if the group is the one that has been requested
group_found = 1;
for (j = 0; i < cfglen && cfgstr[i] && groupname[j]; i++, j++) {
if (cfgstr[i] != groupname[j]) {
group_found = 0;
break;
}
}
// if did not reach the last character of groupname
if (groupname[j])
group_found = 0;
if (group_found)
// start from this point
*groupptr = cfgstr + i;
// go through the rest of the group
while (i < cfglen && group_layer > 0 && cfgstr[i]) {
if (cfgstr[i] == '{')
group_layer++;
if (cfgstr[i] == '}')
group_layer--;
i++;
// count if inside the requested group, except for the last symbol
if (group_found && group_layer > 0)
(*grouplen)++;
}
}
// skip characters outside groups (common part for those groups)
else
i++;
}
}
return (*groupptr == NULL) ? 0 : 1;
}
/*
initialize epoch
input:
pony_time_epoch* epoch --- pointer to a time epoch structure
*/
void pony_init_epoch(pony_time_epoch* epoch)
{
epoch->Y = 0;
epoch->M = 0;
epoch->D = 0;
epoch->h = 0;
epoch->m = 0;
epoch->s = 0;
}
// solution data handling
/*
initialize navigation solution structure
input:
pony_sol* sol --- pointer to a navigation solution structure
char* settings --- pointer to a settings string
const size_t len --- length of the settings string
return value:
1 if successful
0 otherwise
example:
sol = &(pony->imu->sol)
settings = pony->imu->cfg
len = pony->imu->cfglength
*/
char pony_init_sol(pony_sol* sol, char* settings, const size_t len)
{
const size_t
metrics_count_default = 2, // default number of metrics in solution structures
metrics_count_limit = 255; // maximum number of metrics in solution structures
const char metrics_count_token[] = "metrics_count"; // token to look for in settings
size_t i;
int size;
char* cfgptr;
// validate
if (sol == NULL)
return 0;
// pos & vel
for (i = 0; i < 3; i++) {
sol-> x[i] = 0;
sol->llh[i] = 0;
sol-> v[i] = 0;
}
sol-> x_valid = 0;
sol->llh_valid = 0;
sol-> v_valid = 0;
sol-> x_std = -1; // stdev undefined
sol-> v_std = -1; // stdev undefined
// attitude
for (i = 0; i < 4; i++)
sol->q[i] = 0; // quaternion
sol->q_valid = 0;
for (i = 0; i < 9; i++)
sol->L[i] = 0; // attitude matrix
sol->L_valid = 0;
for (i = 0; i < 3; i++)
sol->rpy[i] = 0; // attitude angles
sol->rpy_valid = 0;
// clock
sol->dt = 0;
sol->dt_valid = 0;
// metrics
size = -1;
cfgptr = pony_locate_token(metrics_count_token, settings, len, '='); // try to find number of metrics in settings string
if (cfgptr != NULL) // if token found
size = atoi(cfgptr); // parse the number
return pony_alloc_null((void**)(&(sol->metrics)), size, &(sol->metrics_count), sizeof(double), metrics_count_default, metrics_count_limit);
}
/*
free solution data memory
input:
pony_sol *sol --- pointer to a navigation solution structure
*/
void pony_free_sol(pony_sol* sol)
{
// validate
if (sol == NULL)
return;
pony_free_null((void**)(&(sol->metrics)));
}
// IMU data handling subroutines
/*
initialize inertial navigation constants
*/
void pony_init_imu_const()
{
pony->imu_const.pi = 3.14159265358979323846264338327950288; // pi with maximum quad-precision floating point digits as in IEEE 754-2008 (binary128)
pony->imu_const.pi2 = pony->imu_const.pi*2; // pi x 2
pony->imu_const.pi_2 = pony->imu_const.pi/2; // pi / 2
pony->imu_const.rad2deg = 180/pony->imu_const.pi; // 180/pi
// Earth parameters as in Section 4 of GRS-80 by H. Moritz, Journal of Geodesy (2000) 74 (1): pp. 128-162
pony->imu_const.u = 7.292115e-5; // Earth's rotation rate, rad/s
pony->imu_const.a = 6378137.0; // Earth's ellipsoid semi-major axis, m
pony->imu_const.mu = 3986005e8; // Earth's geocentric gravitational constant (including the atmosphere)
pony->imu_const.J2 = 1.08263e-3; // Earth's dynamical form factor
pony->imu_const.e2 = 6.6943800229e-3; // Earth's ellipsoid first eccentricity squared
pony->imu_const.ge = 9.7803267715; // Earth's normal gravity at the equator, m/s^2
pony->imu_const.fg = 5.302440112e-3; // Earth's normal gravity flattening
}
/*
initialize IMU structure
input:
pony_imu* imu --- pointer to an IMU structure
return value:
1 if successful
0 otherwise
*/
char pony_init_imu(pony_imu* imu)
{
const size_t
temp_count_default = 3, // default number of additional temperature sensors
temp_count_limit = 255; // maximum number of additional temperature sensors
const char metrics_count_token[] = "temp_count"; // token to look for in settings
size_t i;
int size;
char* cfgptr;
// validate
if (imu == NULL)
return 0;
// validity flags
imu-> w_valid = 0;
imu-> f_valid = 0;
imu->Tw_valid = 0;
imu->Tf_valid = 0;
imu-> W_valid = 0;
imu-> g_valid = 0;
imu->t = 0;
// zero parameters
for (i = 0; i < 3; i++) {
imu->w [i] = 0; // gyroscopes
imu->f [i] = 0; // accelerometers
imu->Tw[i] = 0; // gyroscope temperature
imu->Tf[i] = 0; // accelerometer temperature
imu->W [i] = 0; // angular velocity of the local level reference frame
}
// default gravity acceleration vector
imu->g[0] = 0;
imu->g[1] = 0;
imu->g[2] = -pony->imu_const.ge*(1 + pony->imu_const.fg/2); // middle value
// init solution data
if (!pony_init_sol(&(imu->sol), imu->cfg, imu->cfglength))
return 0;
// init additional temperature sensors
size = -1;
cfgptr = pony_locate_token(metrics_count_token, imu->cfg, imu->cfglength, '='); // try to find number of additional temperature sensors in configuration string
if (cfgptr != NULL) // if token found
size = atoi(cfgptr); // parse the number
imu->T_valid = 0;
return pony_alloc_null((void**)(&(imu->T)), size, &(imu->T_count), sizeof(double), temp_count_default, temp_count_limit);
}
/*
free IMU memory
*/
void pony_free_imu(pony_imu* imu)
{
// validate
if (imu == NULL)
return;
// configuration
imu->cfg = NULL;
imu->cfglength = 0;
// solution
pony_free_sol(&(imu->sol));
// application-specific additional temperature sensors
pony_free_null((void**)(&(imu->T)));
}
// GNSS data handling subroutines
/*
initialize GNSS settings
input:
pony_gnss* gnss --- pointer to a GNSS structure
return value:
1
*/
void pony_init_gnss_settings(pony_gnss* gnss)
{
size_t i;
// locate settings within gnss group in configuration: outside all subgroups, either before or after all of them
pony_locatecfggroup("", gnss->cfg, gnss->cfglength, &(gnss->cfg_settings), &(gnss->settings_length));
// mask for the sine of GNSS satellite elevation angle
gnss->settings.sinEl_mask = 0;
// default stdev values
gnss->settings. code_sigma = 20;
gnss->settings. phase_sigma = 0.01;
gnss->settings.doppler_sigma = 0.5;
// antenna position
for (i = 0; i < 3; i++)
gnss->settings.ant_pos[i] = 0;
gnss->settings.ant_pos_tol = -1;
}
/*
initialize GNSS leap seconds from the configuration string
input:
pony_gnss* gnss --- pointer to a GNSS structure
*/
void pony_init_gnss_leap_sec(pony_gnss* gnss)
{
const char leap_sec_token[] = "leap_sec";
char* cfg_ptr;
if (gnss == NULL)
return;
// default values
gnss->leap_sec = 0;
gnss->leap_sec_valid = 0;
// check if the configuration is provided in GNSS data
if (gnss->cfg_settings == NULL || gnss->settings_length == 0)
return;
// look for leap seconds in configuration
cfg_ptr = pony_locate_token(leap_sec_token, gnss->cfg_settings, gnss->settings_length, '=');
if (cfg_ptr != NULL)
gnss->leap_sec = atoi(cfg_ptr);
else
return;
// check leap seconds
gnss->leap_sec_valid = (gnss->leap_sec >= 0) ? 1 : 0;
}
/*
initialize GNSS GPS constants
input:
pony_gps_const* gps_const --- pointer to a GPS system constants structure
*/
void pony_init_gnss_gps_const(pony_gps_const* gps_const)
{
gps_const->mu = 3.986005e14; // Earth gravity constant as in IS-GPS-200J (22 May 2018), m^3/s^2
gps_const->u = 7.2921151467e-5; // Earth rotation rate as in IS-GPS-200J (22 May 2018), rad/s
gps_const->a = 6378137.0; // Earth ellipsoid semi-major axis as in WGS-84(G1762) 2014-07-08, m
gps_const->e2 = 6.694379990141e-3; // Earth ellipsoid first eccentricity squared as in WGS-84(G1762) 2014-07-08
gps_const->F = -4.442807633e-10; // relativistic correction constant as in IS-GPS-200J (22 May 2018), s/sqrt(m)
gps_const->F1 = 1575.42e6; // nominal frequency for L1 signal as in IS-GPS-200J (22 May 2018)
gps_const->L1 = pony->gnss_const.c/gps_const->F1; // nominal wavelength for L1 signal
gps_const->F2 = 1227.60e6; // nominal frequency for L2 signal as in IS-GPS-200J (22 May 2018)
gps_const->L2 = pony->gnss_const.c/gps_const->F2; // nominal wavelength for L2 signal
}
/*
initialize satellite data
input:
pony_gnss_sat* sat --- pointer to a GNSS satellite data structure
*/
void pony_init_gnss_sat(pony_gnss_sat* sat)
{
sat->eph = NULL;
sat->eph_valid = 0;
sat->eph_counter = 0;
sat->obs = NULL;
sat->obs_valid = NULL;
sat->x_valid = 0;
sat->v_valid = 0;
sat->t_em_valid = 0;
sat->sinEl_valid = 0;
}
/*
free satellite data
input:
pony_gnss_sat** sat --- pointer to a GNSS satellite data structure
const size_t sat_count --- number of sattelites
*/
void pony_free_gnss_sat(pony_gnss_sat** sat, const size_t sat_count)
{
size_t s;
if (*sat == NULL)
return;
for (s = 0; s < sat_count; s++) {
// ephemeris
pony_free_null((void**)(&((*sat)[s].eph)));
// observations
pony_free_null((void**)(&((*sat)[s].obs)));
pony_free_null((void**)(&((*sat)[s].obs_valid)));
}
free((void*)(*sat));
*sat = NULL;
}
/*
initialize GNSS GPS structure
input:
pony_gnss_gps* gps --- pointer to a GPS constellation data structure
const size_t max_sat_count --- maximum number of sattelites
const size_t max_eph_count --- maximum number of ephemeris per satellite
return value:
1 if successful
0 otherwise
*/
char pony_init_gnss_gps(pony_gnss_gps* gps, const size_t max_sat_count, const size_t max_eph_count)
{
size_t i;
gps->sat = NULL;
gps->max_sat_count = 0;
gps->max_eph_count = 0;
// try to allocate memory for satellite data
gps->sat = (pony_gnss_sat*)calloc(max_sat_count, sizeof(pony_gnss_sat));
if (gps->sat == NULL)
return 0;
gps->max_sat_count = max_sat_count;
// initialize satellite data
for (i = 0; i < gps->max_sat_count; i++)
pony_init_gnss_sat(gps->sat + i);
// try to allocate memory for each satellite ephemeris
for (i = 0; i < gps->max_sat_count; i++) {
gps->sat[i].eph = (double*)calloc(max_eph_count, sizeof(double));
if (gps->sat[i].eph == NULL)
return 0;
}
gps->max_eph_count = max_eph_count;
// observation types
gps->obs_types = NULL;
gps->obs_count = 0;
// validity flags
gps->iono_valid = 0;
gps->clock_corr_valid = 0;
return 1;
}
/*
free GNSS GPS memory
input:
pony_gnss_gps* gps --- pointer to a GPS constellation data structure
*/
void pony_free_gnss_gps(pony_gnss_gps* gps)
{
if (gps == NULL)
return;
//configuration
gps->cfg = NULL;
gps->cfglength = 0;
// satellites
pony_free_gnss_sat(&(gps->sat), gps->max_sat_count);
gps->max_sat_count = 0;
// observation types
pony_free_null((void**)(&(gps->obs_types)));
gps->obs_count = 0;
// gnss_gps structure
free((void*)gps);
}
/*
initialize GNSS GLONASS constants
input:
pony_glo_const* glo_const --- pointer to a GLONASS system constants structure
*/
void pony_init_gnss_glo_const(pony_glo_const* glo_const)
{
glo_const->mu = 398600.4418e9; // Earth gravity constant as in PZ-90.11 (2014), m^3/s^2
glo_const->J02 = 1082.62575e-6; // second degree zonal harmonic coefficient of normal potential as in PZ-90.11 (2014)
glo_const->u = 7.292115e-5; // Earth rotation rate as in PZ-90.11 (2014), rad/s
glo_const->a = 6378136.0; // Earth ellipsoid semi-major axis as in PZ-90.11 (2014), m
glo_const->e2 = 0.0066943662; // Earth ellipsoid first eccentricity squared as in PZ-90.11 (2014)
glo_const->F01 = 1602e6; // nominal centre frequency for L1 signal as in ICD GLONASS Edition 5.1 2008, Hz
glo_const->dF1 = 562.5e3; // nominal channel separation for L1 signal as in ICD GLONASS Edition 5.1 2008, Hz
glo_const->F02 = 1246e6; // nominal centre frequency for L2 signal as in ICD GLONASS Edition 5.1 2008, Hz
glo_const->dF2 = 437.5e3; // nominal channel separation for L2 signal as in ICD GLONASS Edition 5.1 2008, Hz
}
/*
initialize GNSS GLONASS structure
input:
pony_gnss_glo* glo --- pointer to a GLONASS constellation data structure
const size_t max_sat_count --- maximum number of sattelites
const size_t max_eph_count --- maximum number of ephemeris per satellite
return value:
1 if successful
0 otherwise
*/
char pony_init_gnss_glo(pony_gnss_glo* glo, const size_t max_sat_count, const size_t max_eph_count)
{
size_t i;
glo->sat = NULL;
glo->freq_slot = NULL;
glo->max_sat_count = 0;
glo->max_eph_count = 0;
// try to allocate memory for satellite data
glo->sat = (pony_gnss_sat*)calloc(max_sat_count, sizeof(pony_gnss_sat));
if (glo->sat == NULL)
return 0;
glo->freq_slot = (int*)calloc(max_sat_count, sizeof(int));
glo->max_sat_count = max_sat_count;
// initialize satellite data
for (i = 0; i < glo->max_sat_count; i++)
pony_init_gnss_sat(glo->sat + i);
// try to allocate memory for each satellite ephemeris
for (i = 0; i < glo->max_sat_count; i++) {
glo->sat[i].eph = (double*)calloc(max_eph_count, sizeof(double));
if (glo->sat[i].eph == NULL)
return 0;
}
glo->max_eph_count = max_eph_count;
// observation types
glo->obs_types = NULL;
glo->obs_count = 0;
return 1;
}
/*
free GNSS GLONASS memory
input:
pony_gnss_glo* glo --- pointer to a GLONASS constellation data structure
*/
void pony_free_gnss_glo(pony_gnss_glo* glo)
{
if (glo == NULL)
return;
//configuration
glo->cfg = NULL;
glo->cfglength = 0;
// satellites
pony_free_gnss_sat(&(glo->sat), glo->max_sat_count);
glo->max_sat_count = 0;
// observation types
pony_free_null((void**)(&(glo->obs_types)));
glo->obs_count = 0;
// frequency slots
pony_free_null((void**)(&(glo->freq_slot)));
// gnss glonass structure
free((void*)glo);
}
/*
initialize GNSS Galileo constants
input:
pony_gal_const* gal_const --- pointer to a Galileo system constants structure
*/
void pony_init_gnss_gal_const(pony_gal_const* gal_const)
{
gal_const->mu = 3.986004418e14; // Earth gravity constant as in Galileo OS SIS ICD Issue 1.2 (November 2015), m^3/s^2
gal_const->u = 7.2921151467e-5; // Earth rotation rate as in Galileo OS SIS ICD Issue 1.2 (November 2015), rad/s
gal_const->a = 6378137.0; // Earth ellipsoid semi-major axis as in GRS-80 // JoG March 2000 vol. 74 issue 1, m
gal_const->e2 = 6.69438002290e-3; // Earth ellipsoid first eccentricity squared as in GRS-80 // JoG March 2000 vol. 74 issue 1
gal_const->F = -4.442807309e-10; // relativistic correction constant as in Galileo OS SIS ICD Issue 1.2 (November 2015), s/sqrt(m)
gal_const->F1 = 1575.42e6; // nominal frequency for E1 signal as in Galileo OS SIS ICD Issue 1.2 (November 2015)
gal_const->L1 = pony->gnss_const.c/gal_const->F1; // nominal wavelength for E1 signal
gal_const->F5a = 1176.45e6; // nominal frequency for E5a signal as in Galileo OS SIS ICD Issue 1.2 (November 2015)
gal_const->L5a = pony->gnss_const.c/gal_const->F5a; // nominal wavelength for E5a signal
gal_const->F5b = 1207.14e6; // nominal frequency for E5b signal as in Galileo OS SIS ICD Issue 1.2 (November 2015)
gal_const->L5b = pony->gnss_const.c/gal_const->F5b; // nominal wavelength for E5b signal
gal_const->F6 = 1278.75e6; // nominal frequency for E6 signal as in Galileo OS SIS ICD Issue 1.2 (November 2015)
gal_const->L6 = pony->gnss_const.c/gal_const->F6; // nominal wavelength for E6 signal
}
/*
initialize GNSS Galileo structure
input:
pony_gnss_gal* gal --- pointer to a Galileo constellation data structure
const size_t max_sat_count --- maximum number of sattelites
const size_t max_eph_count --- maximum number of ephemeris per satellite
return value:
1 if successful
0 otherwise
*/
char pony_init_gnss_gal(pony_gnss_gal* gal, const size_t max_sat_count, const size_t max_eph_count)
{
size_t i;
gal->sat = NULL;
gal->max_sat_count = 0;
gal->max_eph_count = 0;
// try to allocate memory for satellite data
gal->sat = (pony_gnss_sat*)calloc(max_sat_count, sizeof(pony_gnss_sat));
if (gal->sat == NULL)
return 0;
gal->max_sat_count = max_sat_count;
// initialize satellite data
for (i = 0; i < gal->max_sat_count; i++)
pony_init_gnss_sat(gal->sat + i);
// try to allocate memory for each satellite ephemeris
for (i = 0; i < gal->max_sat_count; i++) {
gal->sat[i].eph = (double*)calloc(max_eph_count, sizeof(double));
if (gal->sat[i].eph == NULL)
return 0;
}
gal->max_eph_count = max_eph_count;
// observation types
gal->obs_types = NULL;
gal->obs_count = 0;
// validity flags
gal->iono_valid = 0;
gal->clock_corr_valid = 0;
return 1;
}
/*
free GNSS Galileo memory
input:
pony_gnss_gal* gal --- pointer to a Galileo constellation data structure
*/
void pony_free_gnss_gal(pony_gnss_gal* gal)
{
if (gal == NULL)
return;
//configuration
gal->cfg = NULL;
gal->cfglength = 0;
// satellites
pony_free_gnss_sat(&(gal->sat), gal->max_sat_count);
gal->max_sat_count = 0;
// observation types
pony_free_null((void**)(&(gal->obs_types)));
gal->obs_count = 0;
// gnss galileo structure
free((void*)gal);
}
/*
initialize GNSS BeiDou constants
input:
pony_bds_const* bds_const --- pointer to a BeiDou system constants structure
*/
void pony_init_gnss_bds_const(pony_bds_const* bds_const)
{
bds_const->mu = 3.986004418e14; // Earth gravity constant as in BeiDou SIS ICD OSS Version 2.1 (November 2016), m^3/s^2
bds_const->u = 7.292115e-5; // Earth rotation rate as in BeiDou SIS ICD OSS Version 2.1 (November 2016), rad/s
bds_const->a = 6378137.0; // Earth ellipsoid semi-major axis as in BeiDou SIS ICD OSS Version 2.1 (November 2016), m
bds_const->e2 = 6.6943800229008e-3; // Earth ellipsoid first eccentricity squared derived from flattening as in BeiDou SIS ICD OSS Version 2.1 (November 2016)
bds_const->F = -4.442807309043977e-10; // relativistic correction constant derived from Earth gravity as in BeiDou SIS ICD OSS Version 2.1 (November 2016), s/sqrt(m)
bds_const->leap_sec = 14; // leap seconds between BeiDou time and GPS time as of 01-Jan-2006
bds_const->B1 = 1561.098e6; // nominal frequency for B1 signal as in BeiDou SIS ICD OSS Version 2.1 (November 2016)
bds_const->L1 = pony->gnss_const.c/bds_const->B1; // nominal wavelength for B1 signal
bds_const->B2 = 1207.140e6; // nominal frequency for B2 signal as in BeiDou SIS ICD OSS Version 2.1 (November 2016)
bds_const->L2 = pony->gnss_const.c/bds_const->B2; // nominal wavelength for B2 signal
}
/*
initialize GNSS BeiDou structure
input:
pony_gnss_bds* bds --- pointer to a BeiDou constellation data
const size_t max_sat_count --- maximum number of sattelites
const size_t max_eph_count --- maximum number of ephemeris per satellite
return value:
1 if successful
0 otherwise
*/
char pony_init_gnss_bds(pony_gnss_bds* bds, const size_t max_sat_count, const size_t max_eph_count)
{
size_t i;
bds->sat = NULL;
bds->max_sat_count = 0;
bds->max_eph_count = 0;
// try to allocate memory for satellite data
bds->sat = (pony_gnss_sat*)calloc(max_sat_count, sizeof(pony_gnss_sat));
if (bds->sat == NULL)
return 0;
bds->max_sat_count = max_sat_count;
// initialize satellite data
for (i = 0; i < bds->max_sat_count; i++)
pony_init_gnss_sat(bds->sat + i);
// try to allocate memory for each satellite ephemeris
for (i = 0; i < bds->max_sat_count; i++) {
bds->sat[i].eph = (double*)calloc(max_eph_count, sizeof(double));
if (bds->sat[i].eph == NULL)
return 0;
}
bds->max_eph_count = max_eph_count;
// observation types
bds->obs_types = NULL;
bds->obs_count = 0;
// validity flags
bds->iono_valid = 0;
bds->clock_corr_valid = 0;
return 1;
}
/*
free GNSS BeiDou memory
input:
pony_gnss_bds* bds --- pointer to a BeiDou constellation data structure
*/
void pony_free_gnss_bds(pony_gnss_bds* bds)
{
if (bds == NULL)
return;
//configuration
bds->cfg = NULL;
bds->cfglength = 0;
// satellites
pony_free_gnss_sat(&(bds->sat), bds->max_sat_count);
bds->max_sat_count = 0;
// observation types
pony_free_null((void**)(&(bds->obs_types)));
bds->obs_count = 0;
// gnss beidou structure
free((void*)bds);
}
/*
initialize GNSS constants
*/
void pony_init_gnss_const()
{
pony->gnss_const.pi = 3.1415926535898; // pi, circumference to diameter ratio, as in as in IS-GPS-200J, Galileo OS SIS ICD Issue 1.2 (November 2015), BeiDou SIS ICD OSS Version 2.1 (November 2016)
pony->gnss_const.c = 299792458; // speed of light as in IS-GPS-200J (22 May 2018), ICD GLONASS Edition 5.1 2008, Galileo OS SIS ICD Issue 1.2 (November 2015), BeiDou SIS ICD OSS Version 2.1 (November 2016), m/s
pony->gnss_const.sec_in_w = 604800; // seconds in a week
pony->gnss_const.sec_in_d = 86400; // seconds in a day
// constellation-specific constants
pony_init_gnss_gps_const(&(pony->gnss_const.gps));
pony_init_gnss_glo_const(&(pony->gnss_const.glo));
pony_init_gnss_gal_const(&(pony->gnss_const.gal));
pony_init_gnss_bds_const(&(pony->gnss_const.bds));
}
/*
initialize GNSS structure
input:
pony_gnss* gnss --- pointer to a GNSS data structure
return value:
1 if successful
0 otherwise
*/
char pony_init_gnss(pony_gnss* gnss)
{
// memory allocation limits
enum system_id {gps, glo, gal, bds};
const size_t max_sat_count[] = {36, 36, 36, 64 },
max_eph_count[] = {36, 24, 36, 36 };
size_t grouplen;
char* groupptr;
if (gnss == NULL)
return 0;
// gps
gnss->gps = NULL;
if (pony_locatecfggroup("gps:", gnss->cfg, gnss->cfglength, &groupptr, &grouplen)) {
gnss->gps = (pony_gnss_gps*)calloc(1, sizeof(pony_gnss_gps));
if (gnss->gps == NULL)
return 0;
gnss->gps->cfg = groupptr;
gnss->gps->cfglength = grouplen;
if (!pony_init_gnss_gps(gnss->gps, max_sat_count[gps], max_eph_count[gps]))
return 0;
}
// glonass
gnss->glo = NULL;
if (pony_locatecfggroup( "glo:", gnss->cfg, gnss->cfglength, &groupptr, &grouplen)) {
gnss->glo = (pony_gnss_glo*)calloc(1, sizeof(pony_gnss_glo));
if (gnss->glo == NULL)
return 0;
gnss->glo->cfg = groupptr;
gnss->glo->cfglength = grouplen;
if (!pony_init_gnss_glo(gnss->glo, max_sat_count[glo], max_eph_count[glo]))
return 0;
}
// galileo
gnss->gal = NULL;
if (pony_locatecfggroup("gal:", gnss->cfg, gnss->cfglength, &groupptr, &grouplen)) {
gnss->gal = (pony_gnss_gal*)calloc(1, sizeof(pony_gnss_gal));
if (gnss->gal == NULL)
return 0;
gnss->gal->cfg = groupptr;
gnss->gal->cfglength = grouplen;
if (!pony_init_gnss_gal(gnss->gal, max_sat_count[gal], max_eph_count[gal]))
return 0;
}
// beidou
gnss->bds = NULL;
if (pony_locatecfggroup("bds:", gnss->cfg, gnss->cfglength, &groupptr, &grouplen)) {
gnss->bds = (pony_gnss_bds*)calloc(1, sizeof(pony_gnss_bds));
if (gnss->bds == NULL)
return 0;
gnss->bds->cfg = groupptr;
gnss->bds->cfglength = grouplen;
if (!pony_init_gnss_bds(gnss->bds, max_sat_count[bds], max_eph_count[bds]))
return 0;
}
// gnss settings
pony_init_gnss_settings(gnss);
// gnss solution
if (!pony_init_sol(&(gnss->sol), gnss->cfg_settings, gnss->settings_length))
return 0;
// gnss epoch
pony_init_epoch(&(gnss->epoch));
// leap seconds
pony_init_gnss_leap_sec(gnss);