forked from collabora/libsurvive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsurvive_kalman_tracker.c
More file actions
1754 lines (1457 loc) · 74.2 KB
/
survive_kalman_tracker.c
File metadata and controls
1754 lines (1457 loc) · 74.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "survive_kalman_tracker.h"
#include "generated/imu_model.gen.h"
#include "generated/kalman_kinematics.gen.h"
#include "linmath.h"
#include "math.h"
#include "survive_internal.h"
#include <cnkalman/kalman.h>
#include <assert.h>
#if !defined(__FreeBSD__) && !defined(__APPLE__)
#include <malloc.h>
#endif
#include <memory.h>
#include <stddef.h>
#include <survive_reproject.h>
#include <survive_reproject_gen2.h>
#include "generated/lighthouse_model.gen.h"
#include "generated/survive_reproject.aux.generated.h"
#include "survive_kalman_lighthouses.h"
#include "survive_recording.h"
#define SURVIVE_MODEL_MAX_STATE_CNT (sizeof(SurviveKalmanModel) / sizeof(FLT))
// clang-format off
STRUCT_CONFIG_SECTION(SurviveKalmanTracker)
STRUCT_CONFIG_ITEM("light-error-threshold", "Error limit to invalidate position",
-1., t->light_error_threshold)
STRUCT_CONFIG_ITEM("min-report-time",
"Minimum kalman report time in s (-1 defaults to 1. / imu_hz)", -1., t->min_report_time)
STRUCT_CONFIG_ITEM("report-covariance", "Report covariance matrix every n poses", -1, t->report_covariance_cnt);
STRUCT_CONFIG_ITEM("report-sampled-cloud", "Show sample cloud from covariance", false, t->report_sampled_cloud);
STRUCT_CONFIG_ITEM("report-ignore-start", "Number of reports to ignore at startup", 0, t->report_ignore_start)
STRUCT_CONFIG_ITEM("report-ignore-threshold",
"Minimum variance to report pose from the kalman filter", 1e-1, t->report_threshold_var)
STRUCT_CONFIG_ITEM("light-ignore-threshold",
"Minimum variance to allow light data into the kalman filter", 1., t->light_threshold_var)
STRUCT_CONFIG_ITEM("light-required-obs",
"Minimum observations to allow light data into the kalman filter", 16, t->light_required_obs)
STRUCT_CONFIG_ITEM("light-max-error", "Maximum error to integrate into lightcap", -1, t->lightcap_max_error)
STRUCT_CONFIG_ITEM("kalman-light-variance", "Variance of raw light sensor readings", 1e-2, t->light_var)
STRUCT_CONFIG_ITEM("obs-cov-scale", "Covariance matrix scaling for obs",
1, t->obs_cov_scale)
STRUCT_CONFIG_ITEM("kalman-obs-axisangle", "Process observation updates as axis angle poses", false, t->obs_axisangle_model)
STRUCT_CONFIG_ITEM("obs-pos-variance", "Variance of position integration from light capture",
1e-6, t->obs_pos_var)
STRUCT_CONFIG_ITEM("obs-rot-variance", "Variance of rotation integration from light capture",
1e-7, t->obs_rot_var)
STRUCT_CONFIG_ITEM("use-raw-obs", "If true; the raw position from the solver is used and no filtering is applied", 0, t->use_raw_obs)
STRUCT_CONFIG_ITEM("show-raw-obs", "Show position of raw poser output", 0, t->show_raw_obs)
STRUCT_CONFIG_ITEM("light-error-for-lh-confidence",
"Whether or not to invalidate LH positions based on kalman errors", 0, t->use_error_for_lh_pos)
STRUCT_CONFIG_ITEM("process-weight-jerk", "Jerk variance per second", 1874161, t->params.process_weight_jerk)
STRUCT_CONFIG_ITEM("process-weight-acc", "Acc variance per second", 0, t->params.process_weight_acc)
STRUCT_CONFIG_ITEM("process-weight-ang-vel", "Angular velocity variance per second", 60,
t->params.process_weight_ang_velocity)
STRUCT_CONFIG_ITEM("process-weight-vel", "Velocity variance per second", 0, t->params.process_weight_vel)
STRUCT_CONFIG_ITEM("process-weight-pos", "Position variance per second", 0, t->params.process_weight_pos)
STRUCT_CONFIG_ITEM("process-weight-rot", "Rotation variance per second", 0, t->params.process_weight_rotation)
STRUCT_CONFIG_ITEM("process-weight-acc-bias", "Acc bias variance per second", 1e-8, t->params.process_weight_acc_bias)
STRUCT_CONFIG_ITEM("process-weight-gyro-bias", "Gyro bias variance per second", 1e-8, t->params.process_weight_gyro_bias)
STRUCT_CONFIG_ITEM("kalman-minimize-state-space", "Minimize the state space", 1, t->minimize_state_space)
STRUCT_CONFIG_ITEM("kalman-use-error-space", "Model using error state", true, t->use_error_state)
STRUCT_CONFIG_ITEM("kalman-joint-model-lightcap", "Ratio of confidence of LH over tracked object to use joint filter", -1, t->joint_lightcap_ratio)
STRUCT_CONFIG_ITEM("kalman-joint-lightcap-minimum-sensors", "Minimum number of sensors for the joint model to run", 5, t->joint_min_sensor_cnt)
STRUCT_CONFIG_ITEM("kalman-lightcap-minimum-sensors", "Minimum number of sensors for the lightcap model to run", 5, t->lightcap_min_sensor_cnt)
STRUCT_CONFIG_ITEM("kalman-initial-imu-variance", "Initial variance in IMU frame", 0, t->params.initial_variance_imu_correction)
STRUCT_CONFIG_ITEM("kalman-initial-acc-scale-variance", "Initial variance in IMU frame", 1e-6, t->params.initial_acc_scale_variance)
STRUCT_CONFIG_ITEM("kalman-initial-acc-bias-variance", "Initial variance in IMU frame", 1e-6, t->params.initial_acc_bias_variance)
STRUCT_CONFIG_ITEM("kalman-initial-gyro-variance", "Initial variance in gyro", 1e-6, t->params.initial_gyro_variance)
STRUCT_CONFIG_ITEM("kalman-zvu-moving", "", -1, t->zvu_moving_var)
STRUCT_CONFIG_ITEM("kalman-zvu-stationary", "", 1e-5, t->zvu_stationary_var)
STRUCT_CONFIG_ITEM("kalman-zvu-no-light", "", 1e-4, t->zvu_no_light_var)
STRUCT_CONFIG_ITEM("kalman-zvu-no-light-time", "", .1, t->zvu_no_light_time)
STRUCT_CONFIG_ITEM("kalman-noise-model", "0 is jerk acceleration model, 1 is simple model", 0, t->noise_model)
STRUCT_CONFIG_ITEM("imu-acc-norm-penalty", "Penalty to IMU variance when reading high accel values", 0, t->acc_norm_penalty)
STRUCT_CONFIG_ITEM("imu-acc-variance", "Variance of accelerometer", 1e-3, t->acc_var)
STRUCT_CONFIG_ITEM("imu-gyro-variance", "Variance of gyroscope", 0.0000304617, t->gyro_var)
STRUCT_CONFIG_ITEM("light-batch-size", "", 32, t->light_batchsize)
END_STRUCT_CONFIG_SECTION(SurviveKalmanTracker)
// clang-format off
MEAS_MDL_CONFIG(obj, obs, 10, -1)
MEAS_MDL_CONFIG(obj, imu, 0, -1)
MEAS_MDL_CONFIG(obj, zvu, 0, 0)
MEAS_MDL_CONFIG(obj, lightcap, 10, .1)
MEAS_MDL_CONFIG(joint, lightcap, 10, .1)
static inline void integrate_variance_tracker(SurviveKalmanTracker *tracker, struct variance_tracker* vtracker, const FLT* v, size_t size) {
bool isStationary = SurviveSensorActivations_stationary_time(&tracker->so->activations) > 4800000;
if(!isStationary) {
variance_tracker_reset(vtracker);
} else {
variance_tracker_add(vtracker, v, size);
}
}
FLT pid_update(struct pid_t* pid, FLT err, FLT dt) {
FLT der = err - pid->err;
pid->integration += err;
FLT output = pid->Kp * err + (pid->Ki * pid->integration * dt) + (pid->Kd * der /dt);
pid->err = err;
return output;
}
static SurviveKalmanModel copy_model(const FLT *src, size_t state_size) {
SurviveKalmanModel rtn = {
.IMUBias = {
.IMUCorrection = { 1. },
.AccScale = { 1., 1, 1 }
}
};
assert(state_size >= 7 && state_size * sizeof(FLT) <= sizeof(SurviveKalmanModel));
memcpy(rtn.Pose.Pos, src, sizeof(FLT) * state_size);
quatnormalize(rtn.Pose.Rot, rtn.Pose.Rot);
quatnormalize(rtn.IMUBias.IMUCorrection, rtn.IMUBias.IMUCorrection);
return rtn;
}
static SurviveJointKalmanModel copy_joint_model(const FLT *src, size_t state_size) {
assert(state_size >= 14);
SurviveJointKalmanModel rtn = {
0
};
memcpy(&rtn, src, sizeof(FLT) * state_size);
quatnormalize(rtn.Lighthouse.Rot, rtn.Lighthouse.Rot);
return rtn;
}
static SurviveKalmanErrorModel copy_error_model(const CnMat* src) {
SurviveKalmanErrorModel rtn = {
0
};
assert(src->rows >= 7);
memcpy(rtn.Pose.Pos, src->data, sizeof(FLT) * src->rows);
return rtn;
}
static inline FLT survive_kalman_tracker_position_var2(SurviveKalmanTracker *tracker, FLT *var_diag, size_t cnt) {
FLT _var_diag[SURVIVE_MODEL_MAX_STATE_CNT] = {0};
if (var_diag == 0)
var_diag = _var_diag;
for (int i = 0; i < cnt; i++) {
var_diag[i] = cnMatrixGet(&tracker->model.P, i, i);
}
return normnd2(var_diag, cnt);
}
void kalman_model_normalize(void *user, struct CnMat* x) {
SurviveKalmanModel state = copy_model(x->data, x->rows);
quatnormalize(state.Pose.Rot, state.Pose.Rot);
quatnormalize(state.IMUBias.IMUCorrection, state.IMUBias.IMUCorrection);
memcpy(x->data, &state, sizeof(FLT) * x->rows);
}
static void normalize_model(SurviveKalmanTracker *pTracker) {
/*
FLT d = magnitude3d(pTracker->state.Pose.Rot + 1);
pTracker->state.Pose.Rot[0] = 0;
if(d < 1) {
pTracker->state.Pose.Rot[0] = FLT_SQRT(1 - d * d);
}
*/
quatnormalize(pTracker->state.Pose.Rot, pTracker->state.Pose.Rot);
quatnormalize(pTracker->state.IMUBias.IMUCorrection, pTracker->state.IMUBias.IMUCorrection);
for (int i = 0; i < 3; i++) {
pTracker->state.IMUBias.AccScale[i] = linmath_enforce_range(pTracker->state.IMUBias.AccScale[i], .95, 1.05);
pTracker->state.IMUBias.GyroBias[i] = linmath_enforce_range(pTracker->state.IMUBias.GyroBias[i], -1e-1, 1e-1);
pTracker->state.IMUBias.AccBias[i] = linmath_enforce_range(pTracker->state.IMUBias.AccBias[i], -1e-1, 1e-1);
}
for (int i = 0; i < 3; i++) {
assert(isfinite(pTracker->state.Pose.Pos[i]));
}
for (int i = 0; i < 4; i++) {
assert(isfinite(pTracker->state.Pose.Rot[i]));
}
}
struct map_light_data_ctx {
SurviveKalmanTracker *tracker;
};
typedef void (*SurviveJointKalmanModel_LightMeas_jac_x0_with_hx)(CnMat* Hx, CnMat* hx, const FLT dt, const SurviveJointKalmanModel * _x0, const FLT* sensor_pt, const BaseStationCal* bsc0);
typedef void (*SurviveJointKalmanErrorModel_LightMeas_jac_x0_with_hx)(CnMat* Hx, CnMat* hx, const FLT dt, const SurviveJointKalmanModel * _x0, const SurviveJointKalmanErrorModel* error_model, const FLT* sensor_pt);
const static SurviveJointKalmanErrorModel zero_error_joint_model = { 0 };
/*
SurviveJointKalmanModel_LightMeas_jac_x0_with_hx SurviveJointKalmanModel_LightMeas_jac_x0_with_hx_fns[2][2] = {
{SurviveJointKalmanModel_LightMeas_x_gen1_jac_x0_with_hx, SurviveJointKalmanModel_LightMeas_y_gen1_jac_x0_with_hx},
{SurviveJointKalmanModel_LightMeas_x_gen2_jac_x0_with_hx, SurviveJointKalmanModel_LightMeas_y_gen2_jac_x0_with_hx},
};
*/
SurviveJointKalmanErrorModel_LightMeas_jac_x0_with_hx SurviveJointKalmanErrorModel_LightMeas_jac_x0_with_hx_fns[2][2] = {
{SurviveJointKalmanErrorModel_LightMeas_x_gen1_jac_error_model_with_hx, SurviveJointKalmanErrorModel_LightMeas_y_gen1_jac_error_model_with_hx},
{SurviveJointKalmanErrorModel_LightMeas_x_gen2_jac_error_model_with_hx, SurviveJointKalmanErrorModel_LightMeas_y_gen2_jac_error_model_with_hx},
};
typedef void (*SurviveKalmanModel_LightMeas_jac_x0_with_hx)(CnMat* Hx, CnMat* hx, const FLT dt, const SurviveKalmanModel* _x0, const FLT* sensor_pt, const SurvivePose* lh_p, const BaseStationCal* bsc0);
typedef void (*SurviveKalmanErrorModel_LightMeas_jac_x0_with_hx)(CnMat* Hx, CnMat* hx, const FLT dt, const SurviveKalmanModel* _x0, const SurviveKalmanErrorModel* error_model, const FLT* sensor_pt, const SurvivePose* lh_p, const BaseStationCal* bsc0);
const static SurviveKalmanErrorModel zero_error_model = { 0 };
SurviveKalmanModel_LightMeas_jac_x0_with_hx SurviveKalmanModel_LightMeas_jac_x0_with_hx_fns[2][2] = {
{SurviveKalmanModel_LightMeas_x_gen1_jac_x0_with_hx, SurviveKalmanModel_LightMeas_y_gen1_jac_x0_with_hx},
{SurviveKalmanModel_LightMeas_x_gen2_jac_x0_with_hx, SurviveKalmanModel_LightMeas_y_gen2_jac_x0_with_hx},
};
SurviveKalmanErrorModel_LightMeas_jac_x0_with_hx SurviveKalmanErrorModel_LightMeas_jac_x0_with_hx_fns[2][2] = {
{SurviveKalmanErrorModel_LightMeas_x_gen1_jac_error_model_with_hx, SurviveKalmanErrorModel_LightMeas_y_gen1_jac_error_model_with_hx},
{SurviveKalmanErrorModel_LightMeas_x_gen2_jac_error_model_with_hx, SurviveKalmanErrorModel_LightMeas_y_gen2_jac_error_model_with_hx},
};
static bool map_joint_light_data(void *user, const struct CnMat *Z, const struct CnMat *x_t, struct CnMat *y,
struct CnMat *H_k) {
struct map_light_data_ctx *cbctx = (struct map_light_data_ctx *)user;
const SurviveKalmanTracker *tracker = cbctx->tracker;
SurviveObject *so = tracker->so;
struct SurviveContext *ctx = tracker->so->ctx;
struct SurviveJointKalmanModel s = copy_joint_model(cn_as_const_vector(x_t), x_t->rows);
if(H_k) {
cn_set_zero(H_k);
}
CN_CREATE_STACK_VEC(h_x, 1);
FLT *Y = cn_as_vector(y);
for (int i = 0; i < Z->rows; i++) {
const LightInfo *info = &tracker->savedLight[tracker->savedLight_idx + i];
int axis = info->axis;
assert(ctx->bsd[info->lh].PositionSet);
const FLT *pt = &so->sensor_locations[info->sensor_idx * 3];
SurvivePose imu2trackref = so->imu2trackref;
LinmathPoint3d ptInObj = { 0 };
gen_scale_sensor_pt(ptInObj, pt, &imu2trackref, so->sensor_scale);
FLT t = info->timecode / 48000000. - cbctx->tracker->model.t;
t = 0;
CnMat H_k_row = {0};
if(H_k) {
H_k_row = cnMatView(1, H_k->cols, H_k, i, 0);
}
if(cbctx->tracker->lightcap_model.error_state_model && cbctx->tracker->use_error_state) {
//assert(H_k == 0 || (cbctx->tracker->model.error_state_size + 6) == H_k->cols);
SurviveJointKalmanErrorModel_LightMeas_jac_x0_with_hx_fns[ctx->lh_version][info->axis](H_k ? &H_k_row : 0,
y ? &h_x : 0, t, &s,
&zero_error_joint_model,
ptInObj);
} else {
assert(false);
//assert(H_k == 0 || cbctx->tracker->model.state_cnt == H_k->cols);
//SurviveJointKalmanModel_LightMeas_jac_x0_with_hx_fns[ctx->lh_version][info->axis](H_k ? &H_k_row : 0,
// y ? &h_x : 0, t, &s, ptInObj, &world2lh, &ctx->bsd[info->lh].fcal[axis]);
}
if(y) {
Y[i] = cn_as_const_vector(Z)[i] - h_x.data[0];
if(tracker->lightcap_max_error > 0) {
Y[i] = linmath_enforce_range(Y[i], -tracker->lightcap_max_error, tracker->lightcap_max_error);
}
SV_DATA_LOG("h_light[%d][%d][%d]", h_x.data, 1, info->lh, info->axis, info->sensor_idx);
SV_DATA_LOG("Y_light[%d][%d][%d]", Y, 1, info->lh, info->axis, info->sensor_idx);
}
SV_DATA_LOG("Z_light[%d][%d][%d]", &info->value, 1, info->lh, info->axis, info->sensor_idx);
}
if (H_k && !cn_is_finite(H_k))
return false;
survive_recording_write_matrix(tracker->so->ctx->recptr, tracker->so, 100, "light-y", y);
return true;
}
/**
* This function reuses the reproject functions to estimate what it thinks the lightcap angle should be based on x_t,
* and uses that measurement to compare from the actual observed angle. These functions have jacobian functions that
* correspond to them; see @survive_reproject.c and @survive_reproject_gen2.c
*/
static bool map_light_data(void *user, const struct CnMat *Z, const struct CnMat *x_t, struct CnMat *y,
struct CnMat *H_k) {
struct map_light_data_ctx *cbctx = (struct map_light_data_ctx *)user;
SurviveKalmanModel s = copy_model(cn_as_const_vector(x_t), x_t->rows);
const SurviveKalmanTracker *tracker = cbctx->tracker;
SurviveObject *so = tracker->so;
struct SurviveContext *ctx = tracker->so->ctx;
const survive_reproject_model_t *mdl = survive_reproject_model(ctx);
if(H_k) {
cn_set_zero(H_k);
}
SurvivePose obj2world = *(SurvivePose *)cn_as_const_vector(x_t);
quatnormalize(obj2world.Rot, obj2world.Rot);
CN_CREATE_STACK_VEC(h_x, 1);
FLT *Y = cn_as_vector(y);
for (int i = 0; i < Z->rows; i++) {
const LightInfo *info = &tracker->savedLight[tracker->savedLight_idx + i];
int axis = info->axis;
assert(ctx->bsd[info->lh].PositionSet);
const SurvivePose world2lh = InvertPoseRtn(survive_get_lighthouse_position(ctx, info->lh));
const FLT *pt = &so->sensor_locations[info->sensor_idx * 3];
SurvivePose imu2trackref = so->imu2trackref;
LinmathPoint3d ptInObj;
gen_scale_sensor_pt(ptInObj, pt, &imu2trackref, so->sensor_scale);
FLT t = info->timecode / 48000000. - cbctx->tracker->model.t;
t = 0;
CnMat H_k_row = {0};
if(H_k) {
H_k_row = cnMatView(1, H_k->cols, H_k, i, 0);
}
if(cbctx->tracker->lightcap_model.error_state_model && cbctx->tracker->use_error_state) {
assert(H_k == 0 || cbctx->tracker->model.error_state_size == H_k->cols);
SurviveKalmanErrorModel_LightMeas_jac_x0_with_hx_fns[ctx->lh_version][info->axis](H_k ? &H_k_row : 0,
y ? &h_x : 0, t, &s, &zero_error_model, ptInObj, &world2lh,
survive_basestation_cal(ctx, info->lh, axis));
} else {
assert(H_k == 0 || cbctx->tracker->model.state_cnt == H_k->cols);
SurviveKalmanModel_LightMeas_jac_x0_with_hx_fns[ctx->lh_version][info->axis](H_k ? &H_k_row : 0,
y ? &h_x : 0, t, &s, ptInObj, &world2lh,
survive_basestation_cal(ctx, info->lh, axis));
}
if(y) {
Y[i] = cn_as_const_vector(Z)[i] - h_x.data[0];
if(tracker->lightcap_max_error > 0) {
Y[i] = linmath_enforce_range(Y[i], -tracker->lightcap_max_error, tracker->lightcap_max_error);
}
SV_DATA_LOG("h_light[%d][%d][%d]", h_x.data, 1, info->lh, info->axis, info->sensor_idx);
SV_DATA_LOG("Y_light[%d][%d][%d]", Y, 1, info->lh, info->axis, info->sensor_idx);
}
SV_DATA_LOG("Z_light[%d][%d][%d]", &info->value, 1, info->lh, info->axis, info->sensor_idx);
}
if (H_k && !cn_is_finite(H_k))
return false;
survive_recording_write_matrix(tracker->so->ctx->recptr, tracker->so, 100, "light-y", y);
return true;
}
int sort_by_lh_axis_sensor(const void * _info1, const void * _info2) {
const LightInfo* info1 = _info1;
const LightInfo* info2 = _info2;
if(info1->lh != info2->lh)
return (info1->lh > info2->lh) ? -1 : 1;
return 0;
}
void survive_kalman_tracker_integrate_saved_light(SurviveKalmanTracker *tracker, PoserData *pd) {
SurviveContext *ctx = tracker->so->ctx;
FLT time = pd->timecode / (FLT)tracker->so->timebase_hz;
if (tracker->use_raw_obs) {
return;
}
// A single light cap measurement has an infinite amount of solutions along a plane; so it only helps if we are
// already in a good place
if (tracker->light_threshold_var > 0 &&
survive_kalman_tracker_position_var2(tracker, 0, 7) > tracker->light_threshold_var) {
return;
}
if (tracker->light_required_obs > tracker->stats.obs_count) {
return;
}
if (tracker->light_var >= 0) {
for (int i = 0; i < tracker->savedLight_idx; i++) {
if (!ctx->bsd[tracker->savedLight[i].lh].PositionSet) {
tracker->savedLight[i] = tracker->savedLight[tracker->savedLight_idx - 1];
tracker->savedLight_idx--;
i--;
}
}
if (tracker->savedLight_idx == 0) {
return;
}
bool useJointModel = tracker->joint_lightcap_ratio >= 0;
if(useJointModel) {
qsort(tracker->savedLight, tracker->savedLight_idx, sizeof(tracker->savedLight[0]), sort_by_lh_axis_sensor);
}
FLT rtn = 0;
while(tracker->savedLight_idx > 0) {
int lh = tracker->savedLight[tracker->savedLight_idx-1].lh;
int cnt = 0;
if(useJointModel) {
while(tracker->savedLight_idx > 0) {
cnt++;
tracker->savedLight_idx--;
if(tracker->savedLight_idx > 0 && tracker->savedLight[tracker->savedLight_idx - 1].lh != lh)
break;
}
if (cnt < tracker->joint_min_sensor_cnt) {
cnt += tracker->savedLight_idx;
tracker->savedLight_idx = 0;
useJointModel = false;
tracker->stats.joint_model_dropped++;
}
} else {
cnt = tracker->savedLight_idx;
tracker->savedLight_idx = 0;
}
if(cnt < tracker->lightcap_min_sensor_cnt) {
tracker->stats.lightcap_model_dropped++;
return;
}
CN_CREATE_STACK_VEC(Z, cnt);
for (int i = tracker->savedLight_idx; i < cnt + tracker->savedLight_idx; i++) {
assert(useJointModel == false || tracker->savedLight[i].lh == lh);
cnMatrixSet(&Z, i - tracker->savedLight_idx, 0, tracker->savedLight[i].value);
}
struct map_light_data_ctx cbctx = {
.tracker = tracker,
};
SurviveObject *so = tracker->so;
FLT light_var = tracker->light_var;
SV_DATA_LOG("light_var", &light_var, 1);
FLT light_vars[32] = {0};
for (int i = 0; i < 32; i++)
light_vars[i] = light_var;
CnMat R = cnVec(Z.rows, light_vars);
tracker->datalog_tag = "light_data";
if(time < tracker->model.t)
time = tracker->model.t;
FLT obj_trace = cn_trace(&tracker->model.P);
FLT lh_trace = cn_trace(&ctx->bsd[lh].tracker->model.P);
FLT ratio = lh_trace / obj_trace;
tracker->last_light_time = time;
if(useJointModel && tracker->joint_lightcap_ratio < ratio) {
tracker->joint_model.ks[0] = &ctx->bsd[lh].tracker->model;
tracker->joint_model.ks[1] = &ctx->bsd[lh].tracker->bsd_model;
rtn += cnkalman_meas_model_predict_update(time, &tracker->joint_model, &cbctx, &Z, &R);
tracker->stats.joint_model_sensor_cnt_sum += cnt;
survive_kalman_lighthouse_report(ctx->bsd[lh].tracker);
} else {
rtn += cnkalman_meas_model_predict_update(time, &tracker->lightcap_model, &cbctx, &Z, &R);
tracker->stats.lightcap_model_sensor_cnt_sum += cnt;
}
}
tracker->datalog_tag = 0;
tracker->stats.lightcap_total_error += rtn;
tracker->light_residuals_all *= .9;
tracker->light_residuals_all += .1 * rtn;
SurviveObject * so = tracker->so;
SV_DATA_LOG("res_error_light_", &rtn, 1);
SV_DATA_LOG("res_error_light_avg", &tracker->light_residuals_all, 1);
tracker->stats.lightcap_count++;
survive_kalman_tracker_report_state(pd, tracker);
}
}
void survive_kalman_tracker_integrate_light(SurviveKalmanTracker *tracker, PoserDataLight *data) {
bool isSync = data->hdr.pt == POSERDATA_SYNC || data->hdr.pt == POSERDATA_SYNC_GEN2;
if (isSync) {
survive_kalman_tracker_integrate_saved_light(tracker, &data->hdr);
tracker->savedLight_idx = 0;
} else {
LightInfo *info = &tracker->savedLight[tracker->savedLight_idx++];
info->lh = data->lh;
info->value = data->angle;
info->axis = PoserDataLight_axis(data);
info->sensor_idx = data->sensor_id;
info->timecode = data->hdr.timecode;
integrate_variance_tracker(tracker, &tracker->light_variance[info->lh][info->sensor_idx][info->axis], &info->value, 1);
}
int batchtrigger = sizeof(tracker->savedLight) / sizeof(tracker->savedLight[0]);
if (tracker->light_batchsize >= 0) {
batchtrigger = tracker->light_batchsize;
}
if (tracker->savedLight_idx >= batchtrigger) {
survive_kalman_tracker_integrate_saved_light(tracker, &data->hdr);
tracker->savedLight_idx = 0;
}
}
struct map_imu_data_ctx {
bool use_gyro, use_accel;
SurviveKalmanTracker *tracker;
};
SURVIVE_EXPORT void survive_kalman_tracker_correct_imu(SurviveKalmanTracker *tracker, LinmathVec3d out, const LinmathVec3d accel) {
for(int i = 0;i < 3;i++) {
out[i] = accel[i] / tracker->state.IMUBias.AccScale[i] - tracker->state.IMUBias.AccBias[i];
}
}
/**
* The prediction for IMU given x_t is:
*
* [Position, Rotation, Velocity, Ang_Velocity, Acc, Gyro_Bias] = x_t
*
* acc_predict = Rotation^-1 * (Acc/9.80665 + [0, 0, 1])
* gyro_predict = Rotation^-1 * Ang_Velocity + Gyro_Bias
*
* The actual code for this is generated from tools/generate_math_functions/imu_functions.py. It isn't done in
* C natively to allow for the jacobian code to be generated using symengine
*/
bool survive_kalman_tracker_imu_measurement_model(void *user, const struct CnMat *Z, const struct CnMat *x_t, struct CnMat *y,
struct CnMat *H_k) {
struct map_imu_data_ctx *fn_ctx = user;
CN_CREATE_STACK_VEC(h_x, 6);
SurviveKalmanTracker *tracker = (SurviveKalmanTracker *)user;
SurviveKalmanModel s = copy_model(cn_as_const_vector(x_t), x_t->rows);
IMUMeasurementModel(&h_x, &s);
if(H_k) {
assert(H_k->rows * H_k->cols == H_k->cols * 6);
bool errorState = fn_ctx->tracker->imu_model.error_state_model;
if(errorState) {
IMUMeasurementErrorModel_jac_error_model(H_k, &s, &zero_error_model);
} else {
IMUMeasurementModel_jac_model(H_k, &s);
}
}
subnd(cn_as_vector(y), cn_as_const_vector(Z), cn_as_const_vector(&h_x), Z->rows);
if(fn_ctx) {
SurviveKalmanTracker * tracker = fn_ctx->tracker;
SurviveObject * so = fn_ctx->tracker->so;
SurviveContext *ctx = so->ctx;
survive_recording_write_matrix(tracker->so->ctx->recptr, tracker->so, 100, "imu-y", y);
SV_VERBOSE(600, "X " Point7_format, LINMATH_VEC7_EXPAND(cn_as_const_vector(x_t)))
SV_VERBOSE(600, "Z " Point6_format, LINMATH_VEC6_EXPAND(cn_as_const_vector(Z)))
if(so->ctx->datalogproc) {
SV_DATA_LOG("imu_prediction", h_x.data, 6);
LinmathVec3d up = {0, 0, 1};
FLT q[5];
LinmathVec3d imuWorld;
quatrotatevector(imuWorld, tracker->state.Pose.Rot, Z->data);
quatfrom2vectors(q, imuWorld, up);
q[4] = norm3d(q + 1);
quatrotateabout(q, q, tracker->state.Pose.Rot);
SV_DATA_LOG("perfect_q", q, 5);
LinmathVec3d perfect_acc;
quatrotatevector(perfect_acc, q, Z->data);
perfect_acc[2] -= 1;
SV_DATA_LOG("perfect_acc", perfect_acc, 3);
}
}
return true;
}
static void tracker_datalog(const cnkalman_state_t* state, const char *desc, const FLT *v, size_t length) {
SurviveKalmanTracker *tracker = state->datalog_user;
SurviveObject * so = tracker->so;
if(tracker->datalog_tag == 0)
tracker->datalog_tag = "unknown";
SV_DATA_LOG("%s_%s", v, length, desc, tracker->datalog_tag);
}
static SurviveIMUBiasModel copy_imu_bias_model(const struct CnMat *x0) {
SurviveIMUBiasModel state = { .IMUCorrection = { 1 }, .AccScale = {1., 1, 1} };
assert(x0->rows <= sizeof(SurviveIMUBiasModel) / sizeof(FLT));
memcpy(&state, cn_as_const_vector(x0), x0->rows * sizeof(FLT));
return state;
}
static void imu_error_state_fn(void *user, const struct CnMat *x0,
const struct CnMat *x1, struct CnMat *E,
struct CnMat *E_jac_x) {
SurviveIMUBiasModel state0 = copy_imu_bias_model(x0);
if(E_jac_x) {
SurviveIMUBiasModelToErrorModel_jac_x1(E_jac_x, &state0, &state0);
}
if(x1 && E) {
SurviveIMUBiasModel state1 = copy_imu_bias_model(x1);
SurviveIMUBiasErrorModel error_state = { 0 };
SurviveIMUBiasModelToErrorModel(&error_state, &state1, &state0);
memcpy(cn_as_vector(E), &error_state, sizeof(FLT) * E->rows);
}
}
static void error_state_fn(void *user, const struct CnMat *x0,
const struct CnMat *x1, struct CnMat *E,
struct CnMat *E_jac_x) {
SurviveKalmanModel state0 = copy_model(cn_as_const_vector(x0), x0->rows);
if(E_jac_x) {
SurviveKalmanModelToErrorModel_jac_x1(E_jac_x, &state0, &state0);
}
if(x1 && E) {
SurviveKalmanModel state1 = copy_model(cn_as_const_vector(x1), x1->rows);
SurviveKalmanErrorModel error_state = { 0 };
SurviveKalmanModelToErrorModel(&error_state, &state1, &state0);
memcpy(cn_as_vector(E), &error_state, sizeof(FLT) * E->rows);
}
}
static void imu_bias_update_fn(void *user, const struct CnMat *x0, const struct CnMat *E, struct CnMat *x1, struct CnMat* dX_wrt_error_state) {
SurviveIMUBiasModel state = copy_imu_bias_model(x0);
const FLT* x0v = cn_as_const_vector(x0);
if(x1){
SurviveIMUBiasModel _x1 = { 0 };
SurviveIMUBiasErrorModel error_state = { 0 };
memcpy(&error_state, cn_as_const_vector(E), E->rows * sizeof(FLT));
SurviveIMUBiasModelAddErrorModel(&_x1, &state, &error_state);
memcpy(cn_as_vector(x1), &_x1, sizeof(FLT) * x1->rows);
}
if(dX_wrt_error_state){
SurviveIMUBiasErrorModel error_model = { 0 };
SurviveIMUBiasModelAddErrorModel_jac_error_state(dX_wrt_error_state, &state, &error_model);
}
}
static void state_update_fn(void *user, const struct CnMat *x0, const struct CnMat *E, struct CnMat *x1, struct CnMat* dX_wrt_error_state) {
SurviveKalmanModel state = copy_model(cn_as_const_vector(x0), x0->rows);
const FLT* x0v = cn_as_const_vector(x0);
if(x1){
SurviveKalmanModel _x1 = { 0 };
SurviveKalmanErrorModel error_state = copy_error_model(E);
SurviveKalmanModelAddErrorModel(&_x1, &state, &error_state);
memcpy(cn_as_vector(x1), &_x1, sizeof(FLT) * x1->rows);
}
if(dX_wrt_error_state){
SurviveKalmanErrorModel error_model = { 0 };
SurviveKalmanModel state = copy_model(cn_as_const_vector(x0), x0->rows);
SurviveKalmanModelAddErrorModel_jac_error_state(dX_wrt_error_state, &state, &error_model);
}
}
/*
static void error_state_fn(void *user, const struct CnMat *x0, struct CnMat *H) {
cn_set_diag_val(H, 1);
}
static void state_update_fn(void *user, const struct CnMat *x0, struct CnMat *Ky, struct CnMat *x1) {
cn_elementwise_add(x1, x0, Ky);
}
*/
static bool map_obs_data(void *user, const struct CnMat *Z, const struct CnMat *x_t, struct CnMat *y,
struct CnMat *H_k) {
SurviveKalmanTracker *tracker = (SurviveKalmanTracker *)user;
if(y) {
subnd(cn_as_vector(y), cn_as_const_vector(Z), cn_as_const_vector(x_t), 7);
survive_recording_write_matrix(tracker->so->ctx->recptr, tracker->so, 100, "obs-y", y);
}
if(H_k) {
bool errorState = tracker->use_error_state && tracker->obs_model.error_state_model;
if(errorState) {
state_update_fn(user, x_t, 0, 0, H_k);
} else {
cn_set_zero(H_k);
cn_set_diag_val(H_k, 1);
}
}
return true;
}
static bool map_obs_data_axisangle(void *user, const struct CnMat *Z, const struct CnMat *x_t, struct CnMat *y,
struct CnMat *H_k) {
SurviveKalmanTracker *tracker = (SurviveKalmanTracker *)user;
const SurviveKalmanModel *x0 = (const SurviveKalmanModel *)cn_as_const_vector(x_t);
SurviveAxisAnglePose yp = { 0 };
SurviveAxisAnglePose predictedPose = *(const SurviveAxisAnglePose *)cn_as_const_vector(Z);
SurviveObsErrorModelNoFlip(&yp, x0, (const SurviveAxisAnglePose *)cn_as_const_vector(Z));
scalend((FLT *)&yp, (FLT *)&yp, -1, 6);
FLT mag = normnd2(yp.AxisAngleRot, 3);
bool hasFlip = mag > M_PI * M_PI;
if(hasFlip) {
mag = sqrt(mag);
scalend(yp.AxisAngleRot, yp.AxisAngleRot, (mag - 2.*M_PI) / mag, 3);
}
assert(norm3d(yp.AxisAngleRot) < M_PI);
if(y) {
memcpy(cn_as_vector(y), &yp, y->rows * sizeof(FLT));
}
if(H_k) {
bool errorState = tracker->use_error_state && tracker->obs_model.error_state_model;
if(errorState) {
SurviveKalmanErrorModel error_model = { 0 };
(!hasFlip ?
SurviveObsErrorStateErrorModelNoFlip_jac_err :
SurviveObsErrorStateErrorModelFlip_jac_err)(H_k, x0, &error_model, &predictedPose);
} else {
(!hasFlip ? SurviveObsErrorModelNoFlip_jac_x0 :
SurviveObsErrorModelFlip_jac_x0)(H_k, x0, &predictedPose);
}
}
return true;
}
static FLT integrate_pose(SurviveKalmanTracker *tracker, FLT time, const SurvivePose *pose, const struct CnMat *R_q) {
FLT rtn = 0;
size_t state_cnt = tracker->model.state_cnt;
size_t obs_cnt = tracker->obs_axisangle_model ? 6 : 7;
struct CnMat* Rp = (CnMat*)R_q;
CN_CREATE_STACK_MAT(R, obs_cnt, obs_cnt);
CN_CREATE_STACK_VEC(Z, obs_cnt);
if(tracker->obs_axisangle_model) {
LinmathAxisAnglePose poseAA = Pose2AAPose(pose);
if(Rp) {
survive_covariance_pose2poseAA(&R, pose, R_q);
}
memcpy(cn_as_vector(&Z), poseAA.Pos, obs_cnt * sizeof(FLT));
Rp = &R;
} else {
memcpy(cn_as_vector(&Z), pose->Pos, obs_cnt * sizeof(FLT));
}
tracker->datalog_tag = "pose_obs";
rtn = cnkalman_meas_model_predict_update(time, &tracker->obs_model, tracker, &Z, Rp);
tracker->datalog_tag = 0;
SurviveContext *ctx = tracker->so->ctx;
SV_VERBOSE(600, "Resultant state %f (pose) " Point16_format, time,
LINMATH_VEC16_EXPAND(cn_as_const_vector(&tracker->model.state)));
return rtn;
}
void survive_kalman_tracker_integrate_imu(SurviveKalmanTracker *tracker, PoserDataIMU *data) {
SurviveContext *ctx = tracker->so->ctx;
SurviveObject *so = tracker->so;
FLT time = data->hdr.timecode / (FLT)tracker->so->timebase_hz;
FLT time_diff = time - tracker->model.t;
FLT norm = norm3d(data->accel);
SV_DATA_LOG("acc_norm", &norm, 1);
bool isStationary = SurviveSensorActivations_stationary_time(&tracker->so->activations) > 4800000;
if (tracker->use_raw_obs) {
return;
}
// Wait til observation is in before reading IMU; gets rid of bad IMU data at the start
if (tracker->model.t == 0) {
return;
}
if (tracker->stats.obs_count < 16 && tracker->obs_pos_var > -1) {
return;
}
if (time_diff < -.01) {
// SV_WARN("Processing imu data from the past %fs", time - tracker->rot.t);
tracker->stats.late_imu_dropped++;
return;
}
if (time_diff > 0.5) {
SV_WARN("%s is probably dropping IMU packets; %f time reported between %" PRIu64, tracker->so->codename,
time_diff, data->hdr.timecode);
}
FLT rotation_variance[] = {1e5, 1e5, 1e5, 1e5, 1e5, 1e5};
bool no_light = (time - tracker->last_light_time) > tracker->zvu_no_light_time;
FLT zvu_var = tracker->zvu_moving_var;
if(isStationary && tracker->zvu_stationary_var >= 0) zvu_var = linmath_min(tracker->zvu_stationary_var, zvu_var < 0 ? INFINITY : zvu_var);
if(no_light && tracker->zvu_no_light_var >= 0) zvu_var = linmath_min(tracker->zvu_no_light_var, zvu_var < 0 ? INFINITY : zvu_var);
bool disable_ang_vel = no_light && !isStationary;
tracker->stats.no_light_imu_count += no_light;
if (zvu_var >= 0) {//time - tracker->last_light_time > .1) {//|| isStationary || fabs(1 - norm) < .001 ) {
// If we stop seeing light data; tank all velocity / acceleration measurements
size_t row_cnt = linmath_imin(9 - disable_ang_vel * 3, tracker->model.state_cnt - 7);
CN_CREATE_STACK_MAT(H, row_cnt, tracker->model.state_cnt + tracker->imu_bias_model.state_cnt);
cn_set_zero(&H);
int vel_idx = offsetof(SurviveKalmanModel, Velocity.Pos[0]) / sizeof(FLT);
int acc_idx = offsetof(SurviveKalmanModel, Acc) / sizeof(FLT);
int idx = 0;
for (idx = 0; idx < 3; idx++) {
cnMatrixSet(&H, idx, vel_idx + idx, 1);
}
if(!disable_ang_vel) {
for (int i = 0; i < 3; i++) {
cnMatrixSet(&H, idx + i, vel_idx + 3 + i, 1);
}
idx += 3;
}
for (int i = 0; i < 3; i++) {
cnMatrixSet(&H, idx + i, acc_idx + i, 1);
}
CN_CREATE_STACK_MAT(R, row_cnt, 1)
cn_set_constant(&R, zvu_var);
CN_CREATE_STACK_MAT(Z, row_cnt, 1);
cn_set_zero(&Z);
tracker->datalog_tag = "zvu";
tracker->stats.imu_total_error +=
cnkalman_meas_model_predict_update(time, &tracker->zvu_model, &H, &Z, &R);
tracker->datalog_tag = 0;
CN_FREE_STACK_MAT(Z);
CN_FREE_STACK_MAT(R);
CN_FREE_STACK_MAT(H);
}
struct map_imu_data_ctx fn_ctx = {.tracker = tracker};
if (tracker->acc_var >= 0) {
fn_ctx.use_accel = true;
for (int i = 0; i < 3; i++) {
rotation_variance[i] = tracker->acc_var;
if (tracker->acc_norm_penalty > 0) {
FLT ndiff = 1 - norm;
rotation_variance[i] += tracker->acc_norm_penalty * ndiff * ndiff;
}
}
}
if (tracker->gyro_var >= 0) {
fn_ctx.use_gyro = true;
for (int i = 0; i < 3; i++)
rotation_variance[3 + i] = tracker->gyro_var;
}
if (fn_ctx.use_gyro || fn_ctx.use_accel) {
int rows = 6;
int offset = 0;
FLT accelgyro[6] = { 0 };
copy3d(accelgyro, data->accel);
copy3d(accelgyro+3, data->gyro);
integrate_variance_tracker(tracker, &tracker->imu_variance, accelgyro, 6);
CnMat Z = cnMat(rows, 1, accelgyro + offset);
SV_VERBOSE(600, "Integrating IMU " Point6_format " with cov " Point6_format,
LINMATH_VEC6_EXPAND((FLT *)&accelgyro[0]), LINMATH_VEC6_EXPAND(rotation_variance));
tracker->datalog_tag = "imu_meas";
CnMat R = cnMat(6, tracker->imu_model.adaptive ? 6 : 1, tracker->imu_model.adaptive ? tracker->IMU_R : rotation_variance);
FLT err = cnkalman_meas_model_predict_update(time, &tracker->imu_model, &fn_ctx, &Z, &R);
tracker->datalog_tag = 0;
SV_DATA_LOG("res_err_imu", &err, 1);
tracker->stats.imu_total_error += err;
tracker->imu_residuals *= .9;
tracker->imu_residuals += .1 * err;
tracker->stats.acc_norm += norm3d(data->accel);
if(isStationary) {
tracker->stats.stationary_acc_norm += norm3d(data->accel);
tracker->stats.stationary_imu_count++;
}
tracker->stats.imu_count++;
if (tracker->first_imu_time == 0) {
tracker->first_imu_time = time;
}
tracker->last_imu_time = time;
SV_VERBOSE(600, "%s Resultant state %f (imu %e) " Point26_format, so->codename, time, tracker->imu_residuals,
LINMATH_VEC26_EXPAND(cn_as_const_vector(&tracker->model.state)));
}
survive_kalman_tracker_report_state(&data->hdr, tracker);
}
void survive_kalman_tracker_predict(const SurviveKalmanTracker *tracker, FLT t, SurvivePose *out) {
// if (tracker->model.info.P[0] > 100 || tracker->model.info.P[0] > 100 || tracker->model.t == 0)
// return;
if (tracker->model.t == 0)
return;
CnMat x1 = cnVec(7, out->Pos);
cnkalman_extrapolate_state(t, &tracker->model, &x1, 0);
quatnormalize(out->Rot, out->Rot);
struct SurviveContext *ctx = tracker->so->ctx;
SV_VERBOSE(300, "Predict pose %f %f " SurvivePose_format, t, t - tracker->model.t, SURVIVE_POSE_EXPAND(*out))
}
static void survive_kalman_tracker_process_noise_bounce(void *user, FLT t, const CnMat *x, struct CnMat *q_out) {
struct SurviveKalmanTracker_Params *params = (struct SurviveKalmanTracker_Params *)user;
survive_kalman_tracker_process_noise(params, false, t, x, q_out);
}
static void survive_kalman_tracker_error_process_noise_bounce(void *user, FLT t, const CnMat *x, struct CnMat *q_out) {
struct SurviveKalmanTracker_Params *params = (struct SurviveKalmanTracker_Params *)user;
survive_kalman_tracker_process_noise(params, true, t, x, q_out);
}
void survive_kalman_tracker_process_noise(const struct SurviveKalmanTracker_Params *params, bool errorState, FLT t, const CnMat *x, struct CnMat *q_out) {
/*
* Due to the rotational terms in the model, the process noise covariance is complicated. It mixes a XYZ third order
* positional model with a second order rotational model with tuning parameters
*/
/* dt is capped to prevent NaN/inf values */
if (t > 0.05) t = 0.05;
FLT t2 = t * t;
FLT t3 = t2 * t;
FLT t4 = t3 * t;
FLT t5 = t4 * t;
FLT t6 = t5 * t;
FLT t7 = t6 * t;
/* ================== Positional ============================== */
// Estimation with Applications to Tracking and Navigation: Theory Algorithms and Software Ch 6
// http://wiki.dmdevelopment.ru/wiki/Download/Books/Digitalimageprocessing/%D0%9D%D0%BE%D0%B2%D0%B0%D1%8F%20%D0%BF%D0%BE%D0%B4%D0%B1%D0%BE%D1%80%D0%BA%D0%B0%20%D0%BA%D0%BD%D0%B8%D0%B3%20%D0%BF%D0%BE%20%D1%86%D0%B8%D1%84%D1%80%D0%BE%D0%B2%D0%BE%D0%B9%20%D0%BE%D0%B1%D1%80%D0%B0%D0%B1%D0%BE%D1%82%D0%BA%D0%B5%20%D1%81%D0%B8%D0%B3%D0%BD%D0%B0%D0%BB%D0%BE%D0%B2/Estimation%20with%20Applications%20to%20Tracking%20and%20Navigation/booktext@id89013302placeboie.pdf
// We mix three order models here based on tuning variables.
FLT Q_jerk[] = {
t7 / 252.,
t6 / 72., t5 /20.,
t5 / 30, t4 / 8., t3 / 3.
};
FLT Q_acc[] = {
t5 / 20.,
t4 / 8., t3 / 3.,
t3 / 6., t2 / 2., t
};
FLT Q_vel[] = {
t3 / 3.,
t2 / 2., t,
};
FLT p_p = params->process_weight_jerk * Q_jerk[0] + params->process_weight_acc * Q_acc[0] + params->process_weight_vel * Q_vel[0] + params->process_weight_pos * t2;
FLT p_v = params->process_weight_jerk * Q_jerk[1] + params->process_weight_acc * Q_acc[1] + params->process_weight_vel * Q_vel[1];
FLT p_a = params->process_weight_jerk * Q_jerk[3] + params->process_weight_acc * Q_acc[3];
FLT v_v = params->process_weight_jerk * Q_jerk[2] + params->process_weight_acc * Q_acc[2] + params->process_weight_vel * Q_vel[2];
FLT v_a = params->process_weight_jerk * Q_jerk[4] + params->process_weight_acc * Q_acc[4];
FLT a_a = params->process_weight_jerk * Q_jerk[5] + params->process_weight_acc * Q_acc[5];
/* ================== Rotational ==============================
* https://www.ucalgary.ca/engo_webdocs/GL/96.20096.JSchleppe.pdf
* !!! NOTE: This document uses x,y,z,w quaternions !!!
This is a rework using the same methodology. Some helper output functions are in the tools/generate_math_functions
code.
*/
FLT s_w = params->process_weight_ang_velocity;
FLT rv = params->process_weight_ang_velocity * Q_vel[0] + params->process_weight_rotation * t;
FLT r_av = params->process_weight_ang_velocity * Q_vel[1];
/* The gyro bias is expected to change, but slowly through time */
FLT ga = params->process_weight_acc_bias * t;
FLT gb = params->process_weight_gyro_bias * t;
if(!errorState) {
FLT s_f = s_w / 12. * t3;