-
Notifications
You must be signed in to change notification settings - Fork 7
/
fusion.c
executable file
·1355 lines (1231 loc) · 54.3 KB
/
fusion.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
// Copyright (c) 2014, 2015, Freescale Semiconductor, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Freescale Semiconductor, Inc. nor the
// names of its contributors may be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL FREESCALE SEMICONDUCTOR, INC. BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// This is the file that contains the fusion routines. It is STRONGLY RECOMMENDED
// that the casual developer NOT TOUCH THIS FILE. The mathematics behind this file
// is extremely complex, and it will be very easy (almost inevitable) that you screw
// it up.
//
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "config.h"
#include "types.h"
#include "fusion.h"
#include "orientation.h"
#include "magnetic.h"
#include "matrix.h"
#include "approximations.h"
//////////////////////////////////////////////////////////////////////////////////////////////////
// initialization functions for the sensor fusion algorithms
//////////////////////////////////////////////////////////////////////////////////////////////////
void fInit_1DOF_P_BASIC(struct SV_1DOF_P_BASIC *pthisSV, struct PressureSensor *pthisPressure, float flpftimesecs)
{
// set algorithm sampling interval (typically 25Hz) and low pass filter
// Note: the MPL3115 sensor only updates its output every 512ms and is therefore repeatedly oversampled at 25Hz
// but executing the exponenial filter at the 25Hz rate also performs an interpolation giving smoother output.
pthisSV->fdeltat = (float) OVERSAMPLE_RATIO / (float) SENSORFS;
pthisSV->flpf = pthisSV->fdeltat / flpftimesecs;
if (pthisSV->flpf > 1.0F)
{
pthisSV->flpf = 1.0F;
}
// initialize the filters
pthisSV->fLPH = pthisPressure->fH;
pthisSV->fLPT = pthisPressure->fT;
// clear the reset flag
pthisSV->resetflag = false;
return;
} // end fInit_1DOF_P_BASIC
void fInit_3DOF_G_BASIC(struct SV_3DOF_G_BASIC *pthisSV, struct AccelSensor *pthisAccel, float flpftimesecs)
{
// set algorithm sampling interval (typically 25Hz)
pthisSV->fdeltat = (float) OVERSAMPLE_RATIO / (float) SENSORFS;
// set low pass filter constant with maximum value 1.0 (all pass) decreasing to 0.0 (increasing low pass)
if (flpftimesecs > pthisSV->fdeltat)
pthisSV->flpf = pthisSV->fdeltat / flpftimesecs;
else
pthisSV->flpf = 1.0F;
// apply the tilt estimation algorithm to set the low pass orientation matrix and quaternion
switch (THISCOORDSYSTEM)
{
case NED:
f3DOFTiltNED(pthisSV->fLPR, pthisAccel->fGsAvg);
break;
case ANDROID:
f3DOFTiltAndroid(pthisSV->fLPR, pthisAccel->fGsAvg);
break;
case WIN8:
default:
f3DOFTiltWin8(pthisSV->fLPR, pthisAccel->fGsAvg);
break;
}
fQuaternionFromRotationMatrix(pthisSV->fLPR, &(pthisSV->fLPq));
// clear the reset flag
pthisSV->resetflag = false;
return;
} // end fInit_3DOF_G_BASIC
void fInit_3DOF_B_BASIC(struct SV_3DOF_B_BASIC *pthisSV, struct MagSensor *pthisMag, float flpftimesecs)
{
// set algorithm sampling interval (typically 25Hz)
pthisSV->fdeltat = (float) OVERSAMPLE_RATIO / (float) SENSORFS;
// set low pass filter constant with maximum value 1.0 (all pass) decreasing to 0.0 (increasing low pass)
if (flpftimesecs > pthisSV->fdeltat)
pthisSV->flpf = pthisSV->fdeltat / flpftimesecs;
else
pthisSV->flpf = 1.0F;
// initialize the low pass filtered magnetometer orientation matrix and quaternion using fBcAvg
switch (THISCOORDSYSTEM)
{
case NED:
f3DOFMagnetometerMatrixNED(pthisSV->fLPR, pthisMag->fBcAvg);
break;
case ANDROID:
f3DOFMagnetometerMatrixAndroid(pthisSV->fLPR, pthisMag->fBcAvg);
break;
case WIN8:
default:
f3DOFMagnetometerMatrixWin8(pthisSV->fLPR, pthisMag->fBcAvg);
break;
}
fQuaternionFromRotationMatrix(pthisSV->fLPR, &(pthisSV->fLPq));
// clear the reset flag
pthisSV->resetflag = false;
return;
} // end fInit_3DOF_B_BASIC
void fInit_3DOF_Y_BASIC(struct SV_3DOF_Y_BASIC *pthisSV)
{
// compute the sampling time intervals (secs)
pthisSV->fGyrodeltat = 1.0F / (float) SENSORFS;
pthisSV->fdeltat = (float) OVERSAMPLE_RATIO * pthisSV->fGyrodeltat;
// initialize orientation estimate to flat
f3x3matrixAeqI(pthisSV->fR);
fqAeq1(&(pthisSV->fq));
// clear the reset flag
pthisSV->resetflag = false;
return;
} // end fInit_3DOF_Y_BASIC
void fInit_6DOF_GB_BASIC(struct SV_6DOF_GB_BASIC *pthisSV, struct AccelSensor *pthisAccel, struct MagSensor *pthisMag, float flpftimesecs)
{
// set algorithm sampling interval (typically 25Hz)
pthisSV->fdeltat = (float) OVERSAMPLE_RATIO / (float) SENSORFS;
// set low pass filter constant with maximum value 1.0 (all pass) decreasing to 0.0 (increasing low pass)
if (flpftimesecs > pthisSV->fdeltat)
pthisSV->flpf = pthisSV->fdeltat / flpftimesecs;
else
pthisSV->flpf = 1.0F;
// initialize the instantaneous orientation matrix, inclination angle and quaternion
switch (THISCOORDSYSTEM)
{
case NED:
feCompassNED(pthisSV->fLPR, &(pthisSV->fLPDelta), pthisMag->fBcAvg, pthisAccel->fGsAvg);
break;
case ANDROID:
feCompassAndroid(pthisSV->fLPR, &(pthisSV->fLPDelta), pthisMag->fBcAvg, pthisAccel->fGsAvg);
break;
case WIN8:
default:
feCompassWin8(pthisSV->fLPR, &(pthisSV->fLPDelta), pthisMag->fBcAvg, pthisAccel->fGsAvg);
break;
}
fQuaternionFromRotationMatrix(pthisSV->fLPR, &(pthisSV->fLPq));
// clear the reset flag
pthisSV->resetflag = false;
return;
} // end fInit_6DOF_GB_BASIC
// function initalizes the 6DOF accel + gyro Kalman filter algorithm
void fInit_6DOF_GY_KALMAN(struct SV_6DOF_GY_KALMAN *pthisSV, struct AccelSensor *pthisAccel)
{
int8 i; // counter
// compute and store useful product terms to save floating point calculations later
pthisSV->fGyrodeltat = 1.0F / (float) SENSORFS;
pthisSV->fAlphaOver2 = 0.5F * FPIOVER180 * (float) OVERSAMPLE_RATIO / (float) SENSORFS;
pthisSV->fAlphaOver2Sq = pthisSV->fAlphaOver2 * pthisSV->fAlphaOver2;
pthisSV->fAlphaOver2Qwb = pthisSV->fAlphaOver2 * FQWB_6DOF_GY_KALMAN;
pthisSV->fAlphaOver2SqQvYQwb = pthisSV->fAlphaOver2Sq * (FQVY_6DOF_GY_KALMAN + FQWB_6DOF_GY_KALMAN);
// zero the a posteriori gyro offset and error vectors
for (i = CHX; i <= CHZ; i++)
{
pthisSV->fbPl[i] = 0.0F;
pthisSV->fqgErrPl[i] = 0.0F;
pthisSV->fbErrPl[i] = 0.0F;
}
// initialize the a posteriori orientation state vector to the tilt orientation
switch (THISCOORDSYSTEM)
{
case NED:
f3DOFTiltNED(pthisSV->fRPl, pthisAccel->fGsAvg);
break;
case ANDROID:
f3DOFTiltAndroid(pthisSV->fRPl, pthisAccel->fGsAvg);
break;
case WIN8:
default:
f3DOFTiltWin8(pthisSV->fRPl, pthisAccel->fGsAvg);
break;
}
fQuaternionFromRotationMatrix(pthisSV->fRPl, &(pthisSV->fqPl));
// clear the reset flag
pthisSV->resetflag = false;
return;
} // end fInit_6DOF_GY_KALMAN
// function initializes the 9DOF Kalman filter
void fInit_9DOF_GBY_KALMAN(struct SV_9DOF_GBY_KALMAN *pthisSV, struct AccelSensor *pthisAccel, struct MagSensor *pthisMag,
struct MagCalibration *pthisMagCal)
{
float fDelta6DOF; // eCompass estimate of inclination angle returned by least squares eCompass
float pfQvGQa; // accelerometer noise covariance to 1g sphere
float fQvBQd; // magnetometer noise covariances to geomagnetic sphere
int8 i; // counter
// compute and store useful product terms to save floating point calculations later
pthisSV->fGyrodeltat = 1.0F / (float) SENSORFS;
pthisSV->fKalmandeltat = (float) OVERSAMPLE_RATIO / (float) SENSORFS;
pthisSV->fgKalmandeltat = GTOMSEC2 * pthisSV->fKalmandeltat;
pthisSV->fAlphaOver2 = 0.5F * FPIOVER180 * (float) OVERSAMPLE_RATIO / (float) SENSORFS;
pthisSV->fAlphaOver2Sq = pthisSV->fAlphaOver2 * pthisSV->fAlphaOver2;
pthisSV->fAlphaOver2Qwb = pthisSV->fAlphaOver2 * FQWB_9DOF_GBY_KALMAN;
pthisSV->fAlphaOver2SqQvYQwb = pthisSV->fAlphaOver2Sq * (FQVY_9DOF_GBY_KALMAN + FQWB_9DOF_GBY_KALMAN);
// zero the a posteriori gyro offset and error vectors
for (i = CHX; i <= CHZ; i++)
{
pthisSV->fbPl[i] = 0.0F;
pthisSV->fqgErrPl[i] = 0.0F;
pthisSV->fqmErrPl[i] = 0.0F;
pthisSV->fbErrPl[i] = 0.0F;
}
pthisSV->fDeltaPl = 0.0F;
pthisSV->fDeltaErrPl = 0.0F;
// zero the inertial navigation velocity and displacement in the global frame
for (i = CHX; i <= CHZ; i++)
{
pthisSV->fVelGl[i] = 0.0F;
pthisSV->fDisGl[i] = 0.0F;
}
// initialize the posteriori orientation state vector to the instantaneous eCompass orientation
pthisSV->iFirstAccelMagLock = false;
switch (THISCOORDSYSTEM)
{
case NED:
fLeastSquareseCompassNED(&(pthisSV->fqPl), pthisMagCal->fB, pthisSV->fDeltaPl, pthisSV->fsinDeltaPl,
pthisSV->fcosDeltaPl, &fDelta6DOF, pthisMag->fBcAvg, pthisAccel->fGsAvg, &fQvBQd, &pfQvGQa);
break;
case ANDROID:
fLeastSquareseCompassAndroid(&(pthisSV->fqPl), pthisMagCal->fB, pthisSV->fDeltaPl, pthisSV->fsinDeltaPl,
pthisSV->fcosDeltaPl, &fDelta6DOF, pthisMag->fBcAvg, pthisAccel->fGsAvg, &fQvBQd, &pfQvGQa);
break;
case WIN8:
default:
fLeastSquareseCompassWin8(&(pthisSV->fqPl), pthisMagCal->fB, pthisSV->fDeltaPl, pthisSV->fsinDeltaPl,
pthisSV->fcosDeltaPl, &fDelta6DOF, pthisMag->fBcAvg, pthisAccel->fGsAvg, &fQvBQd, &pfQvGQa);
break;
}
fRotationMatrixFromQuaternion(pthisSV->fRPl, &(pthisSV->fqPl));
// initialize the geomagnetic inclination angle to the estimate from the eCompass call.
// this will be inaccurate because of the lack of a magnetic calibration but prevents a transient in
// the Kalman filter that will be tracking it even before the first magnetic calibration.
pthisSV->fDeltaPl = fDelta6DOF;
pthisSV->fsinDeltaPl = sinf(pthisSV->fDeltaPl * FPIOVER180);
pthisSV->fcosDeltaPl = sqrtf(1.0F - pthisSV->fsinDeltaPl * pthisSV->fsinDeltaPl);
// clear the reset flag
pthisSV->resetflag = false;
return;
} // end fInit_9DOF_GBY_KALMAN
//////////////////////////////////////////////////////////////////////////////////////////////////
// run time functions for the sensor fusion algorithms
//////////////////////////////////////////////////////////////////////////////////////////////////
// 1DOF pressure function
void fRun_1DOF_P_BASIC(struct SV_1DOF_P_BASIC *pthisSV, struct PressureSensor *pthisPressure)
{
// if requested, do a reset (with low pass filter time constant 1.5s) and return
if (pthisSV->resetflag)
{
fInit_1DOF_P_BASIC(pthisSV, pthisPressure, 1.5F);
return;
}
// exponentially low pass filter the pressure and temperature.
// when executed at (typically) 25Hz, this filter interpolates the raw signals which are
// updated every 512ms only.
pthisSV->fLPH += pthisSV->flpf * (pthisPressure->fH - pthisSV->fLPH);
pthisSV->fLPT += pthisSV->flpf * (pthisPressure->fT - pthisSV->fLPT);
return;
} // end fRun_1DOF_P_BASIC
// 3DOF orientation function which calls tilt functions and implements low pass filters
void fRun_3DOF_G_BASIC(struct SV_3DOF_G_BASIC *pthisSV, struct AccelSensor *pthisAccel)
{
// if requested, do a reset (with low pass filter time constant 0.5s) and return
if (pthisSV->resetflag)
{
fInit_3DOF_G_BASIC(pthisSV, pthisAccel, 0.5F);
return;
}
// apply the tilt estimation algorithm to get the instantaneous orientation matrix and quaternion
switch (THISCOORDSYSTEM)
{
case NED:
f3DOFTiltNED(pthisSV->fR, pthisAccel->fGsAvg);
break;
case ANDROID:
f3DOFTiltAndroid(pthisSV->fR, pthisAccel->fGsAvg);
break;
case WIN8:
default:
f3DOFTiltWin8(pthisSV->fR, pthisAccel->fGsAvg);
break;
}
fQuaternionFromRotationMatrix(pthisSV->fR, &(pthisSV->fq));
// low pass filter the orientation quaternion
fLPFOrientationQuaternion(&(pthisSV->fq), &(pthisSV->fLPq), pthisSV->flpf, pthisSV->fdeltat, pthisSV->fOmega);
// compute the low pass rotation matrix and rotation vector
fRotationMatrixFromQuaternion(pthisSV->fLPR, &(pthisSV->fLPq));
fRotationVectorDegFromQuaternion(&(pthisSV->fLPq), pthisSV->fLPRVec);
// calculate the Euler angles from the low pass orientation matrix
switch (THISCOORDSYSTEM)
{
case NED:
fNEDAnglesDegFromRotationMatrix(pthisSV->fLPR, &(pthisSV->fLPPhi), &(pthisSV->fLPThe), &(pthisSV->fLPPsi),
&(pthisSV->fLPRho), &(pthisSV->fLPChi));
break;
case ANDROID:
fAndroidAnglesDegFromRotationMatrix(pthisSV->fLPR, &(pthisSV->fLPPhi), &(pthisSV->fLPThe), &(pthisSV->fLPPsi),
&(pthisSV->fLPRho), &(pthisSV->fLPChi));
break;
case WIN8:
default:
fWin8AnglesDegFromRotationMatrix(pthisSV->fLPR, &(pthisSV->fLPPhi), &(pthisSV->fLPThe), &(pthisSV->fLPPsi),
&(pthisSV->fLPRho), &(pthisSV->fLPChi));
break;
}
// force the yaw and compass angles to zero
pthisSV->fLPPsi = pthisSV->fLPRho = 0.0F;
return;
} // end fRun_3DOF_G_BASIC
// 2D automobile eCompass
void fRun_3DOF_B_BASIC(struct SV_3DOF_B_BASIC *pthisSV, struct MagSensor *pthisMag)
{
// if requested, do a reset (with low pass filter time constant 1.0s) and return
if (pthisSV->resetflag)
{
fInit_3DOF_B_BASIC(pthisSV, pthisMag, 1.0F);
return;
}
// calculate the 3DOF magnetometer orientation matrix and quaternion from fBcAvg
switch (THISCOORDSYSTEM)
{
case NED:
f3DOFMagnetometerMatrixNED(pthisSV->fR, pthisMag->fBcAvg);
break;
case ANDROID:
f3DOFMagnetometerMatrixAndroid(pthisSV->fR, pthisMag->fBcAvg);
break;
case WIN8:
default:
f3DOFMagnetometerMatrixWin8(pthisSV->fR, pthisMag->fBcAvg);
break;
}
fQuaternionFromRotationMatrix(pthisSV->fR, &(pthisSV->fq));
// low pass filter the orientation quaternion and compute the low pass rotation matrix
fLPFOrientationQuaternion(&(pthisSV->fq), &(pthisSV->fLPq), pthisSV->flpf, pthisSV->fdeltat, pthisSV->fOmega);
fRotationMatrixFromQuaternion(pthisSV->fLPR, &(pthisSV->fLPq));
fRotationVectorDegFromQuaternion(&(pthisSV->fLPq), pthisSV->fLPRVec);
// calculate the Euler angles from the low pass orientation matrix
switch (THISCOORDSYSTEM)
{
case NED:
fNEDAnglesDegFromRotationMatrix(pthisSV->fLPR, &(pthisSV->fLPPhi), &(pthisSV->fLPThe), &(pthisSV->fLPPsi),
&(pthisSV->fLPRho), &(pthisSV->fLPChi));
break;
case ANDROID:
fAndroidAnglesDegFromRotationMatrix(pthisSV->fLPR, &(pthisSV->fLPPhi), &(pthisSV->fLPThe), &(pthisSV->fLPPsi),
&(pthisSV->fLPRho), &(pthisSV->fLPChi));
break;
case WIN8:
default:
fWin8AnglesDegFromRotationMatrix(pthisSV->fLPR, &(pthisSV->fLPPhi), &(pthisSV->fLPThe), &(pthisSV->fLPPsi),
&(pthisSV->fLPRho), &(pthisSV->fLPChi));
break;
}
return;
}
// basic gyro integration function
void fRun_3DOF_Y_BASIC(struct SV_3DOF_Y_BASIC *pthisSV, struct GyroSensor *pthisGyro)
{
struct fquaternion ftmpq; // scratch quaternion
float ftmpA3x1[3]; // rotation vector
int8 i, j; // loop counters
// if requested, do a reset and return
if (pthisSV->resetflag)
{
fInit_3DOF_Y_BASIC(pthisSV);
return;
}
// integrate the buffered high frequency (typically 200Hz) gyro readings
for (j = 0; j < OVERSAMPLE_RATIO; j++)
{
// compute the incremental fast (typically 200Hz) rotation vector rvec (deg)
for (i = CHX; i <= CHZ; i++)
{
pthisSV->fOmega[i] = (float)pthisGyro->iYsBuffer[j][i] * pthisGyro->fDegPerSecPerCount;
ftmpA3x1[i] = pthisSV->fOmega[i] * pthisSV->fGyrodeltat;
}
// compute the incremental quaternion fDeltaq from the rotation vector
fQuaternionFromRotationVectorDeg(&ftmpq, ftmpA3x1, 1.0F);
// incrementally rotate the orientation quaternion fq
qAeqAxB(&(pthisSV->fq), &ftmpq);
}
// re-normalize the orientation quaternion to stop error propagation
// the renormalization function ensures that the scalar component q0 is non-negative
fqAeqNormqA(&(pthisSV->fq));
// get the rotation matrix from the quaternion
fRotationMatrixFromQuaternion(pthisSV->fR, &(pthisSV->fq));
// compute the rotation vector from the a posteriori quaternion
fRotationVectorDegFromQuaternion(&(pthisSV->fq), pthisSV->fRVec);
// compute the Euler angles from the orientation matrix
switch (THISCOORDSYSTEM)
{
case NED:
fNEDAnglesDegFromRotationMatrix(pthisSV->fR, &(pthisSV->fPhi), &(pthisSV->fThe), &(pthisSV->fPsi),
&(pthisSV->fRho), &(pthisSV->fChi));
break;
case ANDROID:
fAndroidAnglesDegFromRotationMatrix(pthisSV->fR, &(pthisSV->fPhi), &(pthisSV->fThe), &(pthisSV->fPsi),
&(pthisSV->fRho), &(pthisSV->fChi));
break;
case WIN8:
default:
fWin8AnglesDegFromRotationMatrix(pthisSV->fR, &(pthisSV->fPhi), &(pthisSV->fThe), &(pthisSV->fPsi),
&(pthisSV->fRho), &(pthisSV->fChi));
break;
}
return;
} // end fRun_3DOF_Y_BASIC
// 6DOF eCompass orientation function
void fRun_6DOF_GB_BASIC(struct SV_6DOF_GB_BASIC *pthisSV, struct MagSensor *pthisMag, struct AccelSensor *pthisAccel)
{
// if requested, do a reset (with low pass filter time constant 1.0s) and return
if (pthisSV->resetflag)
{
fInit_6DOF_GB_BASIC(pthisSV, pthisAccel, pthisMag, 1.0F);
return;
}
// call the eCompass algorithm to get the instantaneous orientation matrix, inclination angle and quanternion
switch (THISCOORDSYSTEM)
{
case NED:
feCompassNED(pthisSV->fR, &(pthisSV->fDelta), pthisMag->fBcAvg, pthisAccel->fGsAvg);
break;
case ANDROID:
feCompassAndroid(pthisSV->fR, &(pthisSV->fDelta), pthisMag->fBcAvg, pthisAccel->fGsAvg);
break;
case WIN8:
default:
feCompassWin8(pthisSV->fR, &(pthisSV->fDelta), pthisMag->fBcAvg, pthisAccel->fGsAvg);
break;
}
fQuaternionFromRotationMatrix(pthisSV->fR, &(pthisSV->fq));
// low pass filter the orientation quaternion and compute the low pass rotation matrix and rotation vector
fLPFOrientationQuaternion(&(pthisSV->fq), &(pthisSV->fLPq), pthisSV->flpf, pthisSV->fdeltat, pthisSV->fOmega);
fRotationMatrixFromQuaternion(pthisSV->fLPR, &(pthisSV->fLPq));
fRotationVectorDegFromQuaternion(&(pthisSV->fLPq), pthisSV->fLPRVec);
// compute the low pass filtered Euler angles
switch (THISCOORDSYSTEM)
{
case NED:
fNEDAnglesDegFromRotationMatrix(pthisSV->fLPR, &(pthisSV->fLPPhi), &(pthisSV->fLPThe), &(pthisSV->fLPPsi),
&(pthisSV->fLPRho), &(pthisSV->fLPChi));
break;
case ANDROID:
fAndroidAnglesDegFromRotationMatrix(pthisSV->fLPR, &(pthisSV->fLPPhi), &(pthisSV->fLPThe), &(pthisSV->fLPPsi),
&(pthisSV->fLPRho), &(pthisSV->fLPChi));
break;
case WIN8:
default:
fWin8AnglesDegFromRotationMatrix(pthisSV->fLPR, &(pthisSV->fLPPhi), &(pthisSV->fLPThe), &(pthisSV->fLPPsi),
&(pthisSV->fLPRho), &(pthisSV->fLPChi));
break;
}
// low pass filter the geomagnetic inclination angle with a simple exponential filter
pthisSV->fLPDelta += pthisSV->flpf * (pthisSV->fDelta - pthisSV->fLPDelta);
return;
} // end fRun_6DOF_GB_BASIC
// 6DOF accelerometer+gyroscope orientation function implemented using indirect complementary Kalman filter
void fRun_6DOF_GY_KALMAN(struct SV_6DOF_GY_KALMAN *pthisSV, struct AccelSensor *pthisAccel, struct GyroSensor *pthisGyro)
{
// local scalars and arrays
float ftmpMi3x1[3]; // temporary vector used for a priori calculations
float ftmp3DOF3x1[3]; // temporary vector used for 3DOF calculations
float ftmpA3x3[3][3]; // scratch 3x3 matrix
float fC3x6ik; // element i, k of measurement matrix C
float fC3x6jk; // element j, k of measurement matrix C
float fmodSqGs; // modulus squared of accelerometer measurement
struct fquaternion fqMi; // a priori orientation quaternion
struct fquaternion ftmpq; // scratch quaternion
float ftmp; // scratch float
int8 ierror; // matrix inversion error flag
int8 i, j, k; // loop counters
// working arrays for 3x3 matrix inversion
float *pfRows[3];
int8 iColInd[3];
int8 iRowInd[3];
int8 iPivot[3];
// if requested, do a reset initialization with no further processing
if (pthisSV->resetflag)
{
fInit_6DOF_GY_KALMAN(pthisSV, pthisAccel);
return;
}
// compute the a priori orientation quaternion fqMi by integrating the buffered gyro measurements
fqMi = pthisSV->fqPl;
// loop over all the buffered gyroscope measurements
for (j = 0; j < OVERSAMPLE_RATIO; j++)
{
// calculate the instantaneous angular velocity (after subtracting the gyro offset) and rotation vector ftmpMi3x1
for (i = CHX; i <= CHZ; i++)
{
pthisSV->fOmega[i] = (float)pthisGyro->iYsBuffer[j][i] * pthisGyro->fDegPerSecPerCount - pthisSV->fbPl[i];
ftmpMi3x1[i] = pthisSV->fOmega[i] * pthisSV->fGyrodeltat;
}
// convert the rotation vector ftmpMi3x1 to a rotation quaternion ftmpq
fQuaternionFromRotationVectorDeg(&ftmpq, ftmpMi3x1, 1.0F);
// integrate the gyro sensor incremental quaternions into the a priori orientation quaternion fqMi
qAeqAxB(&fqMi, &ftmpq);
}
// set ftmp3DOF3x1 to the 3DOF gravity vector in the sensor frame
fmodSqGs = pthisAccel->fGsAvg[CHX] * pthisAccel->fGsAvg[CHX] + pthisAccel->fGsAvg[CHY] * pthisAccel->fGsAvg[CHY] +
pthisAccel->fGsAvg[CHZ] * pthisAccel->fGsAvg[CHZ];
if (fmodSqGs != 0.0F)
{
// normal non-freefall case
ftmp = 1.0F / sqrt(fabsf(fmodSqGs));
ftmp3DOF3x1[CHX] = pthisAccel->fGsAvg[CHX] * ftmp;
ftmp3DOF3x1[CHY] = pthisAccel->fGsAvg[CHY] * ftmp;
ftmp3DOF3x1[CHZ] = pthisAccel->fGsAvg[CHZ] * ftmp;
}
else
{
// use zero tilt in case of freefall
ftmp3DOF3x1[CHX] = 0.0F;
ftmp3DOF3x1[CHY] = 0.0F;
ftmp3DOF3x1[CHZ] = 1.0F;
}
// correct accelerometer gravity vector for different coordinate systems
switch (THISCOORDSYSTEM)
{
case NED:
// +1g in accelerometer z axis (z down) when PCB is flat so no correction needed
break;
case ANDROID:
// +1g in accelerometer z axis (z up) when PCB is flat so negate the vector to obtain gravity
ftmp3DOF3x1[CHX] = -ftmp3DOF3x1[CHX];
ftmp3DOF3x1[CHY] = -ftmp3DOF3x1[CHY];
ftmp3DOF3x1[CHZ] = -ftmp3DOF3x1[CHZ];
break;
case WIN8:
default:
// -1g in accelerometer z axis (z up) when PCB is flat so no correction needed
break;
}
// set ftmpMi3x1 to the a priori gravity vector in the sensor frame from the a priori quaternion
ftmpMi3x1[CHX] = 2.0F * (fqMi.q1 * fqMi.q3 - fqMi.q0 * fqMi.q2);
ftmpMi3x1[CHY] = 2.0F * (fqMi.q2 * fqMi.q3 + fqMi.q0 * fqMi.q1);
ftmpMi3x1[CHZ] = 2.0F * (fqMi.q0 * fqMi.q0 + fqMi.q3 * fqMi.q3) - 1.0F;
// correct a priori gravity vector for different coordinate systems
switch (THISCOORDSYSTEM)
{
case NED:
// z axis is down (NED) so no correction needed
break;
case ANDROID:
case WIN8:
default:
// z axis is up (ENU) so no negate the vector to obtain gravity
ftmpMi3x1[CHX] = -ftmpMi3x1[CHX];
ftmpMi3x1[CHY] = -ftmpMi3x1[CHY];
ftmpMi3x1[CHZ] = -ftmpMi3x1[CHZ];
break;
}
// calculate the rotation quaternion between 3DOF and a priori gravity vectors (ignored minus signs cancel here)
// and copy the quaternion vector components to the measurement error vector fZErr
fveqconjgquq(&ftmpq, ftmp3DOF3x1, ftmpMi3x1);
pthisSV->fZErr[CHX] = ftmpq.q1;
pthisSV->fZErr[CHY] = ftmpq.q2;
pthisSV->fZErr[CHZ] = ftmpq.q3;
// update Qw using the a posteriori error vectors from the previous iteration.
// as Qv increases or Qw decreases, K -> 0 and the Kalman filter is weighted towards the a priori prediction
// as Qv decreases or Qw increases, KC -> I and the Kalman filter is weighted towards the measurement.
// initialize Qw to all zeroes
for (i = 0; i < 6; i++)
for (j = 0; j < 6; j++)
pthisSV->fQw6x6[i][j] = 0.0F;
// partial diagonal gyro offset terms
pthisSV->fQw6x6[3][3] = pthisSV->fbErrPl[CHX] * pthisSV->fbErrPl[CHX];
pthisSV->fQw6x6[4][4] = pthisSV->fbErrPl[CHY] * pthisSV->fbErrPl[CHY];
pthisSV->fQw6x6[5][5] = pthisSV->fbErrPl[CHZ] * pthisSV->fbErrPl[CHZ];
// diagonal gravity vector components
pthisSV->fQw6x6[0][0] = pthisSV->fqgErrPl[CHX] * pthisSV->fqgErrPl[CHX] + pthisSV->fAlphaOver2SqQvYQwb +
pthisSV->fAlphaOver2Sq * pthisSV->fQw6x6[3][3];
pthisSV->fQw6x6[1][1] = pthisSV->fqgErrPl[CHY] * pthisSV->fqgErrPl[CHY] + pthisSV->fAlphaOver2SqQvYQwb +
pthisSV->fAlphaOver2Sq * pthisSV->fQw6x6[4][4];
pthisSV->fQw6x6[2][2] = pthisSV->fqgErrPl[CHZ] * pthisSV->fqgErrPl[CHZ] + pthisSV->fAlphaOver2SqQvYQwb +
pthisSV->fAlphaOver2Sq * pthisSV->fQw6x6[5][5];
// remaining diagonal gyro offset components
pthisSV->fQw6x6[3][3] += FQWB_6DOF_GY_KALMAN;
pthisSV->fQw6x6[4][4] += FQWB_6DOF_GY_KALMAN;
pthisSV->fQw6x6[5][5] += FQWB_6DOF_GY_KALMAN;
// off diagonal gravity and gyro offset components
pthisSV->fQw6x6[0][3] = pthisSV->fQw6x6[3][0] = pthisSV->fqgErrPl[CHX] * pthisSV->fbErrPl[CHX] - pthisSV->fAlphaOver2Qwb;
pthisSV->fQw6x6[1][4] = pthisSV->fQw6x6[4][1] = pthisSV->fqgErrPl[CHY] * pthisSV->fbErrPl[CHY] - pthisSV->fAlphaOver2Qwb;
pthisSV->fQw6x6[2][5] = pthisSV->fQw6x6[5][2] = pthisSV->fqgErrPl[CHZ] * pthisSV->fbErrPl[CHZ] - pthisSV->fAlphaOver2Qwb;
// calculate the vector fQv containing the diagonal elements of the measurement covariance matrix Qv
pthisSV->fQv = 0.25F * fabsf(fmodSqGs - 1.0F) + pthisSV->fAlphaOver2SqQvYQwb;
// calculate the 6x3 Kalman gain matrix K = Qw * C^T * inv(C * Qw * C^T + Qv)
// set fQwCT6x3 = Qw.C^T where Qw has size 6x6 and C^T has size 6x3
for (i = 0; i < 6; i++) // loop over rows
{
for (j = 0; j < 3; j++) // loop over columns
{
pthisSV->fQwCT6x3[i][j] = 0.0F;
// accumulate matrix sum
for (k = 0; k < 6; k++)
{
// determine fC3x6[j][k] since the matrix is highly sparse
fC3x6jk = 0.0F;
if (k == j) fC3x6jk = 1.0F;
if (k == (j + 3)) fC3x6jk = -pthisSV->fAlphaOver2;
// accumulate fQwCT6x3[i][j] += Qw6x6[i][k] * C[j][k]
if ((pthisSV->fQw6x6[i][k] != 0.0F) && (fC3x6jk != 0.0F))
{
if (fC3x6jk == 1.0F)
pthisSV->fQwCT6x3[i][j] += pthisSV->fQw6x6[i][k];
else
pthisSV->fQwCT6x3[i][j] += pthisSV->fQw6x6[i][k] * fC3x6jk;
}
}
}
}
// set symmetric ftmpA3x3 = C.(Qw.C^T) + Qv = C.fQwCT6x3 + Qv
for (i = 0; i < 3; i++) // loop over rows
{
for (j = i; j < 3; j++) // loop over on and above diagonal columns
{
// zero off diagonal and set diagonal to Qv
if (i == j)
ftmpA3x3[i][j] = pthisSV->fQv;
else
ftmpA3x3[i][j] = 0.0F;
// accumulate matrix sum
for (k = 0; k < 6; k++)
{
// determine fC3x6[i][k]
fC3x6ik = 0.0F;
if (k == i) fC3x6ik = 1.0F;
if (k == (i + 3)) fC3x6ik = -pthisSV->fAlphaOver2;
// accumulate ftmpA3x3[i][j] += C[i][k] & fQwCT6x3[k][j]
if ((fC3x6ik != 0.0F) && (pthisSV->fQwCT6x3[k][j] != 0.0F))
{
if (fC3x6ik == 1.0F)
ftmpA3x3[i][j] += pthisSV->fQwCT6x3[k][j];
else
ftmpA3x3[i][j] += fC3x6ik * pthisSV->fQwCT6x3[k][j];
}
}
}
}
// set ftmpA3x3 below diagonal elements to above diagonal elements
ftmpA3x3[1][0] = ftmpA3x3[0][1];
ftmpA3x3[2][0] = ftmpA3x3[0][2];
ftmpA3x3[2][1] = ftmpA3x3[1][2];
// invert ftmpA3x3 in situ to give ftmpA3x3 = inv(C * Qw * C^T + Qv) = inv(ftmpA3x3)
for (i = 0; i < 3; i++)
pfRows[i] = ftmpA3x3[i];
fmatrixAeqInvA(pfRows, iColInd, iRowInd, iPivot, 3, &ierror);
// on successful inversion set Kalman gain matrix fK6x3 = Qw * C^T * inv(C * Qw * C^T + Qv) = fQwCT6x3 * ftmpA3x3
if (!ierror)
{
// normal case
for (i = 0; i < 6; i++) // loop over rows
{
for (j = 0; j < 3; j++) // loop over columns
{
pthisSV->fK6x3[i][j] = 0.0F;
for (k = 0; k < 3; k++)
{
if ((pthisSV->fQwCT6x3[i][k] != 0.0F) && (ftmpA3x3[k][j] != 0.0F))
{
pthisSV->fK6x3[i][j] += pthisSV->fQwCT6x3[i][k] * ftmpA3x3[k][j];
}
}
}
}
}
else
{
// ftmpA3x3 was singular so set Kalman gain matrix fK6x3 to zero
for (i = 0; i < 6; i++) // loop over rows
{
for (j = 0; j < 3; j++) // loop over columns
{
pthisSV->fK6x3[i][j] = 0.0F;
}
}
}
// calculate the a posteriori gravity and geomagnetic tilt quaternion errors and gyro offset error vector
// from the Kalman matrix fK6x3 and from the measurement error vector fZErr.
for (i = CHX; i <= CHZ; i++)
{
// gravity tilt vector error component
if (pthisSV->fK6x3[i][CHX] != 0.0F)
pthisSV->fqgErrPl[i] = pthisSV->fK6x3[i][CHX] * pthisSV->fZErr[CHX];
else
pthisSV->fqgErrPl[i] = 0.0F;
if (pthisSV->fK6x3[i][CHY] != 0.0F) pthisSV->fqgErrPl[i] += pthisSV->fK6x3[i][CHY] * pthisSV->fZErr[CHY];
if (pthisSV->fK6x3[i][CHZ] != 0.0F) pthisSV->fqgErrPl[i] += pthisSV->fK6x3[i][CHZ] * pthisSV->fZErr[CHZ];
// gyro offset vector error component
if (pthisSV->fK6x3[i + 3][CHX] != 0.0F)
pthisSV->fbErrPl[i] = pthisSV->fK6x3[i + 3][CHX] * pthisSV->fZErr[CHX];
else
pthisSV->fbErrPl[i] = 0.0F;
if (pthisSV->fK6x3[i + 3][CHY] != 0.0F) pthisSV->fbErrPl[i] += pthisSV->fK6x3[i + 3][CHY] * pthisSV->fZErr[CHY];
if (pthisSV->fK6x3[i + 3][CHZ] != 0.0F) pthisSV->fbErrPl[i] += pthisSV->fK6x3[i + 3][CHZ] * pthisSV->fZErr[CHZ];
}
// set ftmpq to the gravity tilt correction (conjugate) quaternion
ftmpq.q1 = -pthisSV->fqgErrPl[CHX];
ftmpq.q2 = -pthisSV->fqgErrPl[CHY];
ftmpq.q3 = -pthisSV->fqgErrPl[CHZ];
ftmp = ftmpq.q1 * ftmpq.q1 + ftmpq.q2 * ftmpq.q2 + ftmpq.q3 * ftmpq.q3;
// determine the scalar component q0
if (ftmp <= 1.0F)
{
// normal case
ftmpq.q0 = sqrtf(fabsf(1.0F - ftmp));
}
else
{
// if vector component exceeds unity then rounding errors are present at 180 deg rotation
// so set scalar component to zero for 180 deg rotation about the rotation vector
ftmpq.q0 = 0.0F;
}
// apply the gravity tilt correction quaternion so fqPl = fqMi.(fqgErrPl)* = fqMi.ftmpq and normalize
qAeqBxC(&(pthisSV->fqPl), &fqMi, &ftmpq);
// normalize the a posteriori quaternion and compute the a posteriori rotation matrix and rotation vector
fqAeqNormqA(&(pthisSV->fqPl));
fRotationMatrixFromQuaternion(pthisSV->fRPl, &(pthisSV->fqPl));
fRotationVectorDegFromQuaternion(&(pthisSV->fqPl), pthisSV->fRVecPl);
// update the a posteriori gyro offset vector: b+[k] = b-[k] - be+[k] = b+[k] - be+[k] (deg/s)
for (i = CHX; i <= CHZ; i++)
pthisSV->fbPl[i] -= pthisSV->fbErrPl[i];
// compute the linear acceleration in the global frame
// de-rotate the accelerometer from the sensor to global frame using the transpose (inverse) of the orientation matrix
pthisSV->fAccGl[CHX] = pthisSV->fRPl[CHX][CHX] * pthisAccel->fGsAvg[CHX] + pthisSV->fRPl[CHY][CHX] * pthisAccel->fGsAvg[CHY] +
pthisSV->fRPl[CHZ][CHX] * pthisAccel->fGsAvg[CHZ];
pthisSV->fAccGl[CHY] = pthisSV->fRPl[CHX][CHY] * pthisAccel->fGsAvg[CHX] + pthisSV->fRPl[CHY][CHY] * pthisAccel->fGsAvg[CHY] +
pthisSV->fRPl[CHZ][CHY] * pthisAccel->fGsAvg[CHZ];
pthisSV->fAccGl[CHZ] = pthisSV->fRPl[CHX][CHZ] * pthisAccel->fGsAvg[CHX] + pthisSV->fRPl[CHY][CHZ] * pthisAccel->fGsAvg[CHY] +
pthisSV->fRPl[CHZ][CHZ] * pthisAccel->fGsAvg[CHZ];
// sutract the fixed gravity vector in the global frame leaving linear acceleration
switch (THISCOORDSYSTEM)
{
case NED:
// gravity positive NED
pthisSV->fAccGl[CHX] = -pthisSV->fAccGl[CHX];
pthisSV->fAccGl[CHY] = -pthisSV->fAccGl[CHY];
pthisSV->fAccGl[CHZ] = -(pthisSV->fAccGl[CHZ] - 1.0F);
break;
case ANDROID:
// acceleration positive ENU
pthisSV->fAccGl[CHZ] = pthisSV->fAccGl[CHZ] - 1.0F;
break;
case WIN8:
default:
// gravity positive ENU
pthisSV->fAccGl[CHX] = -pthisSV->fAccGl[CHX];
pthisSV->fAccGl[CHY] = -pthisSV->fAccGl[CHY];
pthisSV->fAccGl[CHZ] = -(pthisSV->fAccGl[CHZ] + 1.0F);
break;
}
// compute the a posteriori Euler angles from the a posteriori orientation matrix fRPl
switch (THISCOORDSYSTEM)
{
case NED:
fNEDAnglesDegFromRotationMatrix(pthisSV->fRPl, &(pthisSV->fPhiPl), &(pthisSV->fThePl), &(pthisSV->fPsiPl), &(pthisSV->fRhoPl), &(pthisSV->fChiPl));
break;
case ANDROID:
fAndroidAnglesDegFromRotationMatrix(pthisSV->fRPl, &(pthisSV->fPhiPl), &(pthisSV->fThePl), &(pthisSV->fPsiPl), &(pthisSV->fRhoPl), &(pthisSV->fChiPl));
break;
case WIN8:
default:
fWin8AnglesDegFromRotationMatrix(pthisSV->fRPl, &(pthisSV->fPhiPl), &(pthisSV->fThePl), &(pthisSV->fPsiPl), &(pthisSV->fRhoPl), &(pthisSV->fChiPl));
break;
}
return;
} // end fRun_6DOF_GY_KALMAN
// 9DOF accelerometer+magnetometer+gyroscope orientation function implemented using indirect complementary Kalman filter
void fRun_9DOF_GBY_KALMAN(struct SV_9DOF_GBY_KALMAN *pthisSV, struct AccelSensor *pthisAccel, struct MagSensor *pthisMag,
struct GyroSensor *pthisGyro, struct MagCalibration *pthisMagCal)
{
// local scalars and arrays
float ftmpA7x7[7][7]; // scratch 7x7 matrix
float fRMi[3][3]; // a priori orientation matrix
float fR6DOF[3][3]; // eCompass (6DOF accelerometer+magnetometer) orientation matrix
float ftmpMi3x1[3]; // temporary vector used for a priori calculations
float ftmp6DOF3x1[3]; // temporary vector used for 6DOF calculations
float ftmpA3x1[3]; // scratch vector
float fQvGQa; // accelerometer noise covariance to 1g sphere
float fQvBQd; // magnetometer noise covariance to geomagnetic sphere
float fC7x10ik; // element i, k of measurement matrix C
float fC7x10jk; // element j, k of measurement matrix C
struct fquaternion fqMi; // a priori orientation quaternion
struct fquaternion fq6DOF; // eCompass (6DOF accelerometer+magnetometer) orientation quaternion
struct fquaternion ftmpq; // scratch quaternion
float fDelta6DOF; // inclination angle from accelerometer and magnetometer
float ftmp; // scratch float
int8 ierror; // matrix inversion error flag
int8 i, j, k; // loop counters
// working arrays for 7x7 matrix inversion
float *pfRows[7];
int8 iColInd[7];
int8 iRowInd[7];
int8 iPivot[7];
// if requested, do a reset initialization with no further processing
if (pthisSV->resetflag)
{
fInit_9DOF_GBY_KALMAN(pthisSV, pthisAccel, pthisMag, pthisMagCal);
return;
}
// compute the a priori orientation quaternion fqMi by integrating the buffered gyro measurements
fqMi = pthisSV->fqPl;
// loop over all the buffered gyroscope measurements
for (j = 0; j < OVERSAMPLE_RATIO; j++)
{
// calculate the instantaneous angular velocity (after subtracting the gyro offset) and rotation vector ftmpMi3x1
for (i = CHX; i <= CHZ; i++)
{
pthisSV->fOmega[i] = (float)pthisGyro->iYsBuffer[j][i] * pthisGyro->fDegPerSecPerCount - pthisSV->fbPl[i];
ftmpMi3x1[i] = pthisSV->fOmega[i] * pthisSV->fGyrodeltat;
}
// convert the rotation vector ftmpMi3x1 to a rotation quaternion ftmpq
fQuaternionFromRotationVectorDeg(&ftmpq, ftmpMi3x1, 1.0F);
// integrate the gyro sensor incremental quaternions into the a priori orientation quaternion fqMi
qAeqAxB(&fqMi, &ftmpq);
}
// compute the a priori orientation matrix from the a priori quaternion.
fRotationMatrixFromQuaternion(fRMi, &fqMi);
// compute the 6DOF least squares orientation quaternion fq6DOF, matrix fR6DOF and inclination angle fDelta6DOF and
// the squared deviations of the accelerometer and magnetometer measurements from the 1g gravity and geomagnetic spheres.
switch (THISCOORDSYSTEM)
{
case NED:
fLeastSquareseCompassNED(&fq6DOF, pthisMagCal->fB, pthisSV->fDeltaPl, pthisSV->fsinDeltaPl, pthisSV->fcosDeltaPl,
&fDelta6DOF, pthisMag->fBcAvg, pthisAccel->fGsAvg, &fQvBQd, &fQvGQa);
break;
case ANDROID:
fLeastSquareseCompassAndroid(&fq6DOF, pthisMagCal->fB, pthisSV->fDeltaPl, pthisSV->fsinDeltaPl, pthisSV->fcosDeltaPl,
&fDelta6DOF, pthisMag->fBcAvg, pthisAccel->fGsAvg, &fQvBQd, &fQvGQa);
break;
case WIN8:
default:
fLeastSquareseCompassWin8(&fq6DOF, pthisMagCal->fB, pthisSV->fDeltaPl, pthisSV->fsinDeltaPl, pthisSV->fcosDeltaPl,
&fDelta6DOF, pthisMag->fBcAvg, pthisAccel->fGsAvg, &fQvBQd, &fQvGQa);
break;
}
// compute the 6DOF orientation matrix from the 6DOF quaternion.
fRotationMatrixFromQuaternion(fR6DOF, &fq6DOF);
// do a once-only orientation lock immediately after the first valid magnetic calibration by setting the
// a priori and a posteriori orientation to the 6DOF eCompass orientation.
// also initialize the geomagnetic inclination angle Delta now that a magnetic calibration has been achieved and a
// reasonable estimate is available for the first time.
if (pthisMagCal->iValidMagCal && !pthisSV->iFirstAccelMagLock)
{
fqMi = pthisSV->fqPl = fq6DOF;
f3x3matrixAeqB(fRMi, fR6DOF);
pthisSV->fDeltaPl = fDelta6DOF;
pthisSV->fsinDeltaPl = sinf(pthisSV->fDeltaPl * FPIOVER180);
pthisSV->fcosDeltaPl = sqrtf(1.0F - pthisSV->fsinDeltaPl * pthisSV->fsinDeltaPl);
pthisSV->iFirstAccelMagLock = true;
}
// set ftmp6DOF3x1 to the 6DOF gravity vector in the sensor frame (ignoring the minus sign for Android and Windows 8)
ftmp6DOF3x1[CHX] = fR6DOF[CHX][CHZ];
ftmp6DOF3x1[CHY] = fR6DOF[CHY][CHZ];
ftmp6DOF3x1[CHZ] = fR6DOF[CHZ][CHZ];
// set ftmpMi3x1 to the a priori gravity vector in the sensor frame (ignoring the minus sign for Android and Windows 8)
ftmpMi3x1[CHX] = fRMi[CHX][CHZ];
ftmpMi3x1[CHY] = fRMi[CHY][CHZ];
ftmpMi3x1[CHZ] = fRMi[CHZ][CHZ];
// set ftmpq to the quaternion that rotates the 6DOF gravity tilt vector to the a priori gravity tilt vector
// and copy the vector components to the measurement error vector fZErr. the ignored minus signs cancel here.
fveqconjgquq(&ftmpq, ftmp6DOF3x1, ftmpMi3x1);
pthisSV->fZErr[0] = ftmpq.q1;
pthisSV->fZErr[1] = ftmpq.q2;
pthisSV->fZErr[2] = ftmpq.q3;
// determine the normalized 6DOF and a priori geomagnetic vectors in the sensor frame.
switch (THISCOORDSYSTEM)
{
case NED:
// set ftmp6DOF3x1 to the normalized 6DOF geomagnetic vector in the sensor frame
ftmp6DOF3x1[CHX] = fR6DOF[CHX][CHX] * pthisSV->fcosDeltaPl + fR6DOF[CHX][CHZ] * pthisSV->fsinDeltaPl;
ftmp6DOF3x1[CHY] = fR6DOF[CHY][CHX] * pthisSV->fcosDeltaPl + fR6DOF[CHY][CHZ] * pthisSV->fsinDeltaPl;
ftmp6DOF3x1[CHZ] = fR6DOF[CHZ][CHX] * pthisSV->fcosDeltaPl + fR6DOF[CHZ][CHZ] * pthisSV->fsinDeltaPl;
// set ftmpMi3x1 to the normalized a priori geomagnetic vector in the sensor frame
ftmpMi3x1[CHX] = fRMi[CHX][CHX] * pthisSV->fcosDeltaPl + fRMi[CHX][CHZ] * pthisSV->fsinDeltaPl;
ftmpMi3x1[CHY] = fRMi[CHY][CHX] * pthisSV->fcosDeltaPl + fRMi[CHY][CHZ] * pthisSV->fsinDeltaPl;
ftmpMi3x1[CHZ] = fRMi[CHZ][CHX] * pthisSV->fcosDeltaPl + fRMi[CHZ][CHZ] * pthisSV->fsinDeltaPl;
break;
case ANDROID:
case WIN8:
default: