-
Notifications
You must be signed in to change notification settings - Fork 7
/
orientation.c
executable file
·1399 lines (1218 loc) · 45.1 KB
/
orientation.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 file contains functions designed to operate on, or compute, orientations.
// These may be in rotation matrix form, quaternion form, or Euler angles.
// It also includes functions designed to operate with specify reference frames
// (Android, Windows 8, NED).
//
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "config.h"
#include "types.h"
#include "orientation.h"
#include "matrix.h"
#include "approximations.h"
// compile time constants that are private to this file
#define SMALLQ0 0.01F // limit of quaternion scalar component requiring special algorithm
#define CORRUPTQUAT 0.001F // threshold for deciding rotation quaternion is corrupt
#define SMALLMODULUS 0.01F // limit where rounding errors may appear
// Aerospace NED accelerometer 3DOF tilt function computing rotation matrix fR
void f3DOFTiltNED(float fR[][3], float fGs[])
{
// the NED self-consistency twist occurs at 90 deg pitch
// local variables
int16 i; // counter
float fmodGxyz; // modulus of the x, y, z accelerometer readings
float fmodGyz; // modulus of the y, z accelerometer readings
float frecipmodGxyz; // reciprocal of modulus
float ftmp; // scratch variable
// compute the accelerometer squared magnitudes
fmodGyz = fGs[CHY] * fGs[CHY] + fGs[CHZ] * fGs[CHZ];
fmodGxyz = fmodGyz + fGs[CHX] * fGs[CHX];
// check for freefall special case where no solution is possible
if (fmodGxyz == 0.0F)
{
f3x3matrixAeqI(fR);
return;
}
// check for vertical up or down gimbal lock case
if (fmodGyz == 0.0F)
{
f3x3matrixAeqScalar(fR, 0.0F);
fR[CHY][CHY] = 1.0F;
if (fGs[CHX] >= 0.0F)
{
fR[CHX][CHZ] = 1.0F;
fR[CHZ][CHX] = -1.0F;
}
else
{
fR[CHX][CHZ] = -1.0F;
fR[CHZ][CHX] = 1.0F;
}
return;
}
// compute moduli for the general case
fmodGyz = sqrtf(fmodGyz);
fmodGxyz = sqrtf(fmodGxyz);
frecipmodGxyz = 1.0F / fmodGxyz;
ftmp = fmodGxyz / fmodGyz;
// normalize the accelerometer reading into the z column
for (i = CHX; i <= CHZ; i++)
{
fR[i][CHZ] = fGs[i] * frecipmodGxyz;
}
// construct x column of orientation matrix
fR[CHX][CHX] = fmodGyz * frecipmodGxyz;
fR[CHY][CHX] = -fR[CHX][CHZ] * fR[CHY][CHZ] * ftmp;
fR[CHZ][CHX] = -fR[CHX][CHZ] * fR[CHZ][CHZ] * ftmp;
// construct y column of orientation matrix
fR[CHX][CHY] = 0.0F;
fR[CHY][CHY] = fR[CHZ][CHZ] * ftmp;
fR[CHZ][CHY] = -fR[CHY][CHZ] * ftmp;
return;
}
// Android accelerometer 3DOF tilt function computing rotation matrix fR
void f3DOFTiltAndroid(float fR[][3], float fGs[])
{
// the Android tilt matrix is mathematically identical to the NED tilt matrix
// the Android self-consistency twist occurs at 90 deg roll
f3DOFTiltNED(fR, fGs);
return;
}
// Windows 8 accelerometer 3DOF tilt function computing rotation matrix fR
void f3DOFTiltWin8(float fR[][3], float fGs[])
{
// the Win8 self-consistency twist occurs at 90 deg roll
// local variables
float fmodGxyz; // modulus of the x, y, z accelerometer readings
float fmodGxz; // modulus of the x, z accelerometer readings
float frecipmodGxyz; // reciprocal of modulus
float ftmp; // scratch variable
int8 i; // counter
// compute the accelerometer squared magnitudes
fmodGxz = fGs[CHX] * fGs[CHX] + fGs[CHZ] * fGs[CHZ];
fmodGxyz = fmodGxz + fGs[CHY] * fGs[CHY];
// check for freefall special case where no solution is possible
if (fmodGxyz == 0.0F)
{
f3x3matrixAeqI(fR);
return;
}
// check for vertical up or down gimbal lock case
if (fmodGxz == 0.0F)
{
f3x3matrixAeqScalar(fR, 0.0F);
fR[CHX][CHX] = 1.0F;
if (fGs[CHY] >= 0.0F)
{
fR[CHY][CHZ] = -1.0F;
fR[CHZ][CHY] = 1.0F;
}
else
{
fR[CHY][CHZ] = 1.0F;
fR[CHZ][CHY] = -1.0F;
}
return;
}
// compute moduli for the general case
fmodGxz = sqrtf(fmodGxz);
fmodGxyz = sqrtf(fmodGxyz);
frecipmodGxyz = 1.0F / fmodGxyz;
ftmp = fmodGxyz / fmodGxz;
if (fGs[CHZ] < 0.0F)
{
ftmp = -ftmp;
}
// normalize the negated accelerometer reading into the z column
for (i = CHX; i <= CHZ; i++)
{
fR[i][CHZ] = -fGs[i] * frecipmodGxyz;
}
// construct x column of orientation matrix
fR[CHX][CHX] = -fR[CHZ][CHZ] * ftmp;
fR[CHY][CHX] = 0.0F;
fR[CHZ][CHX] = fR[CHX][CHZ] * ftmp;;
// // construct y column of orientation matrix
fR[CHX][CHY] = fR[CHX][CHZ] * fR[CHY][CHZ] * ftmp;
fR[CHY][CHY] = -fmodGxz * frecipmodGxyz;
if (fGs[CHZ] < 0.0F)
{
fR[CHY][CHY] = -fR[CHY][CHY];
}
fR[CHZ][CHY] = fR[CHY][CHZ] * fR[CHZ][CHZ] * ftmp;
return;
}
// Aerospace NED magnetometer 3DOF flat eCompass function computing rotation matrix fR
void f3DOFMagnetometerMatrixNED(float fR[][3], float fBc[])
{
// local variables
float fmodBxy; // modulus of the x, y magnetometer readings
// compute the magnitude of the horizontal (x and y) magnetometer reading
fmodBxy = sqrtf(fBc[CHX] * fBc[CHX] + fBc[CHY] * fBc[CHY]);
// check for zero field special case where no solution is possible
if (fmodBxy == 0.0F)
{
f3x3matrixAeqI(fR);
return;
}
// define the fixed entries in the z row and column
fR[CHZ][CHX] = fR[CHZ][CHY] = fR[CHX][CHZ] = fR[CHY][CHZ] = 0.0F;
fR[CHZ][CHZ] = 1.0F;
// define the remaining entries
fR[CHX][CHX] = fR[CHY][CHY] = fBc[CHX] / fmodBxy;
fR[CHY][CHX] = fBc[CHY] / fmodBxy;
fR[CHX][CHY] = -fR[CHY][CHX];
return;
}
// Android magnetometer 3DOF flat eCompass function computing rotation matrix fR
void f3DOFMagnetometerMatrixAndroid(float fR[][3], float fBc[])
{
// local variables
float fmodBxy; // modulus of the x, y magnetometer readings
// compute the magnitude of the horizontal (x and y) magnetometer reading
fmodBxy = sqrtf(fBc[CHX] * fBc[CHX] + fBc[CHY] * fBc[CHY]);
// check for zero field special case where no solution is possible
if (fmodBxy == 0.0F)
{
f3x3matrixAeqI(fR);
return;
}
// define the fixed entries in the z row and column
fR[CHZ][CHX] = fR[CHZ][CHY] = fR[CHX][CHZ] = fR[CHY][CHZ] = 0.0F;
fR[CHZ][CHZ] = 1.0F;
// define the remaining entries
fR[CHX][CHX] = fR[CHY][CHY] = fBc[CHY] / fmodBxy;
fR[CHX][CHY] = fBc[CHX] / fmodBxy;
fR[CHY][CHX] = -fR[CHX][CHY];
return;
}
// Windows 8 magnetometer 3DOF flat eCompass function computing rotation matrix fR
void f3DOFMagnetometerMatrixWin8(float fR[][3], float fBc[])
{
// call the Android function since it is identical to the Windows 8 matrix
f3DOFMagnetometerMatrixAndroid(fR, fBc);
return;
}
// NED: basic 6DOF e-Compass function computing rotation matrix fR and magnetic inclination angle fDelta
void feCompassNED(float fR[][3], float *pfDelta, float fBc[], float fGs[])
{
// local variables
float fmod[3]; // column moduli
float fmodBc; // modulus of Bc
float fGsdotBc; // dot product of vectors G.Bc
float ftmp; // scratch variable
int8 i, j; // loop counters
// set the inclination angle to zero in case it is not computed later
*pfDelta = 0.0F;
// place the un-normalized gravity and geomagnetic vectors into the rotation matrix z and x axes
for (i = CHX; i <= CHZ; i++)
{
fR[i][CHZ] = fGs[i];
fR[i][CHX] = fBc[i];
}
// set y vector to vector product of z and x vectors
fR[CHX][CHY] = fR[CHY][CHZ] * fR[CHZ][CHX] - fR[CHZ][CHZ] * fR[CHY][CHX];
fR[CHY][CHY] = fR[CHZ][CHZ] * fR[CHX][CHX] - fR[CHX][CHZ] * fR[CHZ][CHX];
fR[CHZ][CHY] = fR[CHX][CHZ] * fR[CHY][CHX] - fR[CHY][CHZ] * fR[CHX][CHX];
// set x vector to vector product of y and z vectors
fR[CHX][CHX] = fR[CHY][CHY] * fR[CHZ][CHZ] - fR[CHZ][CHY] * fR[CHY][CHZ];
fR[CHY][CHX] = fR[CHZ][CHY] * fR[CHX][CHZ] - fR[CHX][CHY] * fR[CHZ][CHZ];
fR[CHZ][CHX] = fR[CHX][CHY] * fR[CHY][CHZ] - fR[CHY][CHY] * fR[CHX][CHZ];
// calculate the rotation matrix column moduli
fmod[CHX] = sqrtf(fR[CHX][CHX] * fR[CHX][CHX] + fR[CHY][CHX] * fR[CHY][CHX] + fR[CHZ][CHX] * fR[CHZ][CHX]);
fmod[CHY] = sqrtf(fR[CHX][CHY] * fR[CHX][CHY] + fR[CHY][CHY] * fR[CHY][CHY] + fR[CHZ][CHY] * fR[CHZ][CHY]);
fmod[CHZ] = sqrtf(fR[CHX][CHZ] * fR[CHX][CHZ] + fR[CHY][CHZ] * fR[CHY][CHZ] + fR[CHZ][CHZ] * fR[CHZ][CHZ]);
// normalize the rotation matrix columns
if (!((fmod[CHX] == 0.0F) || (fmod[CHY] == 0.0F) || (fmod[CHZ] == 0.0F)))
{
// loop over columns j
for (j = CHX; j <= CHZ; j++)
{
ftmp = 1.0F / fmod[j];
// loop over rows i
for (i = CHX; i <= CHZ; i++)
{
// normalize by the column modulus
fR[i][j] *= ftmp;
}
}
}
else
{
// no solution is possible so set rotation to identity matrix
f3x3matrixAeqI(fR);
return;
}
// compute the geomagnetic inclination angle (deg)
fmodBc = sqrtf(fBc[CHX] * fBc[CHX] + fBc[CHY] * fBc[CHY] + fBc[CHZ] * fBc[CHZ]);
fGsdotBc = fGs[CHX] * fBc[CHX] + fGs[CHY] * fBc[CHY] + fGs[CHZ] * fBc[CHZ];
if (!((fmod[CHZ] == 0.0F) || (fmodBc == 0.0F)))
{
*pfDelta = fasin_deg(fGsdotBc / (fmod[CHZ] * fmodBc));
}
return;
}
// Android: basic 6DOF e-Compass function computing rotation matrix fR and magnetic inclination angle fDelta
void feCompassAndroid(float fR[][3], float *pfDelta, float fBc[], float fGs[])
{
// local variables
float fmod[3]; // column moduli
float fmodBc; // modulus of Bc
float fGsdotBc; // dot product of vectors G.Bc
float ftmp; // scratch variable
int8 i, j; // loop counters
// set the inclination angle to zero in case it is not computed later
*pfDelta = 0.0F;
// place the un-normalized gravity and geomagnetic vectors into the rotation matrix z and y axes
for (i = CHX; i <= CHZ; i++)
{
fR[i][CHZ] = fGs[i];
fR[i][CHY] = fBc[i];
}
// set x vector to vector product of y and z vectors
fR[CHX][CHX] = fR[CHY][CHY] * fR[CHZ][CHZ] - fR[CHZ][CHY] * fR[CHY][CHZ];
fR[CHY][CHX] = fR[CHZ][CHY] * fR[CHX][CHZ] - fR[CHX][CHY] * fR[CHZ][CHZ];
fR[CHZ][CHX] = fR[CHX][CHY] * fR[CHY][CHZ] - fR[CHY][CHY] * fR[CHX][CHZ];
// set y vector to vector product of z and x vectors
fR[CHX][CHY] = fR[CHY][CHZ] * fR[CHZ][CHX] - fR[CHZ][CHZ] * fR[CHY][CHX];
fR[CHY][CHY] = fR[CHZ][CHZ] * fR[CHX][CHX] - fR[CHX][CHZ] * fR[CHZ][CHX];
fR[CHZ][CHY] = fR[CHX][CHZ] * fR[CHY][CHX] - fR[CHY][CHZ] * fR[CHX][CHX];
// calculate the rotation matrix column moduli
fmod[CHX] = sqrtf(fR[CHX][CHX] * fR[CHX][CHX] + fR[CHY][CHX] * fR[CHY][CHX] + fR[CHZ][CHX] * fR[CHZ][CHX]);
fmod[CHY] = sqrtf(fR[CHX][CHY] * fR[CHX][CHY] + fR[CHY][CHY] * fR[CHY][CHY] + fR[CHZ][CHY] * fR[CHZ][CHY]);
fmod[CHZ] = sqrtf(fR[CHX][CHZ] * fR[CHX][CHZ] + fR[CHY][CHZ] * fR[CHY][CHZ] + fR[CHZ][CHZ] * fR[CHZ][CHZ]);
// normalize the rotation matrix columns
if (!((fmod[CHX] == 0.0F) || (fmod[CHY] == 0.0F) || (fmod[CHZ] == 0.0F)))
{
// loop over columns j
for (j = CHX; j <= CHZ; j++)
{
ftmp = 1.0F / fmod[j];
// loop over rows i
for (i = CHX; i <= CHZ; i++)
{
// normalize by the column modulus
fR[i][j] *= ftmp;
}
}
}
else
{
// no solution is possible so set rotation to identity matrix
f3x3matrixAeqI(fR);
return;
}
// compute the geomagnetic inclination angle (deg)
fmodBc = sqrtf(fBc[CHX] * fBc[CHX] + fBc[CHY] * fBc[CHY] + fBc[CHZ] * fBc[CHZ]);
fGsdotBc = fGs[CHX] * fBc[CHX] + fGs[CHY] * fBc[CHY] + fGs[CHZ] * fBc[CHZ];
if (!((fmod[CHZ] == 0.0F) || (fmodBc == 0.0F)))
{
*pfDelta = -fasin_deg(fGsdotBc / (fmod[CHZ] * fmodBc));
}
return;
}
// Win8: basic 6DOF e-Compass function computing rotation matrix fR and magnetic inclination angle fDelta
void feCompassWin8(float fR[][3], float *pfDelta, float fBc[], float fGs[])
{
// local variables
float fmod[3]; // column moduli
float fmodBc; // modulus of Bc
float fGsdotBc; // dot product of vectors G.Bc
float ftmp; // scratch variable
int8 i, j; // loop counters
// set the inclination angle to zero in case it is not computed later
*pfDelta = 0.0F;
// place the negated un-normalized gravity and un-normalized geomagnetic vectors into the rotation matrix z and y axes
for (i = CHX; i <= CHZ; i++)
{
fR[i][CHZ] = -fGs[i];
fR[i][CHY] = fBc[i];
}
// set x vector to vector product of y and z vectors
fR[CHX][CHX] = fR[CHY][CHY] * fR[CHZ][CHZ] - fR[CHZ][CHY] * fR[CHY][CHZ];
fR[CHY][CHX] = fR[CHZ][CHY] * fR[CHX][CHZ] - fR[CHX][CHY] * fR[CHZ][CHZ];
fR[CHZ][CHX] = fR[CHX][CHY] * fR[CHY][CHZ] - fR[CHY][CHY] * fR[CHX][CHZ];
// set y vector to vector product of z and x vectors
fR[CHX][CHY] = fR[CHY][CHZ] * fR[CHZ][CHX] - fR[CHZ][CHZ] * fR[CHY][CHX];
fR[CHY][CHY] = fR[CHZ][CHZ] * fR[CHX][CHX] - fR[CHX][CHZ] * fR[CHZ][CHX];
fR[CHZ][CHY] = fR[CHX][CHZ] * fR[CHY][CHX] - fR[CHY][CHZ] * fR[CHX][CHX];
// calculate the rotation matrix column moduli
fmod[CHX] = sqrtf(fR[CHX][CHX] * fR[CHX][CHX] + fR[CHY][CHX] * fR[CHY][CHX] + fR[CHZ][CHX] * fR[CHZ][CHX]);
fmod[CHY] = sqrtf(fR[CHX][CHY] * fR[CHX][CHY] + fR[CHY][CHY] * fR[CHY][CHY] + fR[CHZ][CHY] * fR[CHZ][CHY]);
fmod[CHZ] = sqrtf(fR[CHX][CHZ] * fR[CHX][CHZ] + fR[CHY][CHZ] * fR[CHY][CHZ] + fR[CHZ][CHZ] * fR[CHZ][CHZ]);
// normalize the rotation matrix columns
if (!((fmod[CHX] == 0.0F) || (fmod[CHY] == 0.0F) || (fmod[CHZ] == 0.0F)))
{
// loop over columns j
for (j = CHX; j <= CHZ; j++)
{
ftmp = 1.0F / fmod[j];
// loop over rows i
for (i = CHX; i <= CHZ; i++)
{
// normalize by the column modulus
fR[i][j] *= ftmp;
}
}
}
else
{
// no solution is possible so set rotation to identity matrix
f3x3matrixAeqI(fR);
return;
}
// compute the geomagnetic inclination angle (deg)
fmodBc = sqrtf(fBc[CHX] * fBc[CHX] + fBc[CHY] * fBc[CHY] + fBc[CHZ] * fBc[CHZ]);
fGsdotBc = fGs[CHX] * fBc[CHX] + fGs[CHY] * fBc[CHY] + fGs[CHZ] * fBc[CHZ];
if (!((fmod[CHZ] == 0.0F) || (fmodBc == 0.0F)))
{
*pfDelta = fasin_deg(fGsdotBc / (fmod[CHZ] * fmodBc));
}
return;
}
// NED: 6DOF e-Compass function computing least squares fit to orientation quaternion fq
// on the assumption that the geomagnetic field fB and magnetic inclination angle fDelta are known
void fLeastSquareseCompassNED(struct fquaternion *pfq, float fB, float fDelta, float fsinDelta, float fcosDelta,
float *pfDelta6DOF, float fBc[], float fGs[], float *pfQvBQd, float *pfQvGQa)
{
// local variables
float fK[4][4]; // K measurement matrix
float eigvec[4][4]; // matrix of eigenvectors of K
float eigval[4]; // vector of eigenvalues of K
float fmodGsSq; // modulus of fGs[] squared
float fmodBcSq; // modulus of fBc[] squared
float fmodGs; // modulus of fGs[]
float fmodBc; // modulus of fBc[]
float fGsdotBc; // scalar product of Gs and Bc
float fag, fam; // relative weightings
float fagOvermodGs; // a0 / |Gs|
float famOvermodBc; // a1 / |Bc|
float famOvermodBccosDelta; // a1 / |Bc| * cos(Delta)
float famOvermodBcsinDelta; // a1 / |Bc| * sin(Delta)
float ftmp; // scratch
int8 i; // loop counter
// calculate the measurement vector moduli and return with identity quaternion if either is null.
fmodGsSq = fGs[CHX] * fGs[CHX] + fGs[CHY] * fGs[CHY] + fGs[CHZ] * fGs[CHZ];
fmodBcSq = fBc[CHX] * fBc[CHX] + fBc[CHY] * fBc[CHY] + fBc[CHZ] * fBc[CHZ];
fmodGs = sqrtf(fmodGsSq);
fmodBc = sqrtf(fmodBcSq);
if ((fmodGs == 0.0F) || (fmodBc == 0.0F) || (fB == 0.0))
{
pfq->q0 = 1.0F;
pfq->q1 = pfq->q2 = pfq->q3 = 0.0F;
return;
}
// calculate the accelerometer and magnetometer noise covariances (units rad^2) and least squares weightings
*pfQvGQa = fabsf(fmodGsSq - 1.0F);
*pfQvBQd = fabsf(fmodBcSq - fB * fB);
fag = *pfQvBQd / (fB * fB * *pfQvGQa + *pfQvBQd);
fam = 1.0F - fag;
// compute useful ratios to reduce computation
fagOvermodGs = fag / fmodGs;
famOvermodBc = fam / fmodBc;
famOvermodBccosDelta = famOvermodBc * fcosDelta;
famOvermodBcsinDelta = famOvermodBc * fsinDelta;
// compute the scalar product Gs.Bc and 6DOF accelerometer plus magnetometer geomagnetic inclination angle (deg)
fGsdotBc = fGs[CHX] * fBc[CHX] + fGs[CHY] * fBc[CHY] + fGs[CHZ] * fBc[CHZ];
*pfDelta6DOF = fasin_deg((fGsdotBc) / (fmodGs * fmodBc));
// set the K matrix to the non-zero accelerometer components
fK[0][0] = fK[3][3] = fagOvermodGs * fGs[CHZ];
fK[1][1] = fK[2][2] = -fK[0][0];
fK[0][1] = fK[2][3] = fagOvermodGs * fGs[CHY];
fK[1][3] = fagOvermodGs * fGs[CHX];
fK[0][2] = -fK[1][3];
// update the K matrix with the magnetometer component
ftmp = famOvermodBcsinDelta * fBc[CHY];
fK[0][1] += ftmp;
fK[2][3] += ftmp;
fK[1][2] = famOvermodBccosDelta * fBc[CHY];
fK[0][3] = -fK[1][2];
ftmp = famOvermodBccosDelta * fBc[CHX];
fK[0][0] += ftmp;
fK[1][1] += ftmp;
fK[2][2] -= ftmp;
fK[3][3] -= ftmp;
ftmp = famOvermodBcsinDelta * fBc[CHZ];
fK[0][0] += ftmp;
fK[1][1] -= ftmp;
fK[2][2] -= ftmp;
fK[3][3] += ftmp;
ftmp = famOvermodBccosDelta * fBc[CHZ];
fK[0][2] += ftmp;
fK[1][3] += ftmp;
ftmp = famOvermodBcsinDelta * fBc[CHX];
fK[0][2] -= ftmp;
fK[1][3] += ftmp;
// copy above diagonal elements to below diagonal
fK[1][0] = fK[0][1];
fK[2][0] = fK[0][2];
fK[2][1] = fK[1][2];
fK[3][0] = fK[0][3];
fK[3][1] = fK[1][3];
fK[3][2] = fK[2][3];
// set eigval to the unsorted eigenvalues and eigvec to the unsorted normalized eigenvectors of fK
eigencompute4(fK, eigval, eigvec, 4);
// copy the largest eigenvector into the orientation quaternion fq
i = 0;
if (eigval[1] > eigval[i]) i = 1;
if (eigval[2] > eigval[i]) i = 2;
if (eigval[3] > eigval[i]) i = 3;
pfq->q0 = eigvec[0][i];
pfq->q1 = eigvec[1][i];
pfq->q2 = eigvec[2][i];
pfq->q3 = eigvec[3][i];
// force q0 to be non-negative
if (pfq->q0 < 0.0F)
{
pfq->q0 = -pfq->q0;
pfq->q1 = -pfq->q1;
pfq->q2 = -pfq->q2;
pfq->q3 = -pfq->q3;
}
return;
}
// Android: 6DOF e-Compass function computing least squares fit to orientation quaternion fq
// on the assumption that the geomagnetic field fB and magnetic inclination angle fDelta are known
void fLeastSquareseCompassAndroid(struct fquaternion *pfq, float fB, float fDelta, float fsinDelta, float fcosDelta,
float *pfDelta6DOF, float fBc[], float fGs[], float *pfQvBQd, float *pfQvGQa)
{
// local variables
float fK[4][4]; // K measurement matrix
float eigvec[4][4]; // matrix of eigenvectors of K
float eigval[4]; // vector of eigenvalues of K
float fmodGsSq; // modulus of fGs[] squared
float fmodBcSq; // modulus of fBc[] squared
float fmodGs; // modulus of fGs[]
float fmodBc; // modulus of fBc[]
float fGsdotBc; // scalar product of Gs and Bc
float fag, fam; // relative weightings
float fagOvermodGs; // a0 / |Gs|
float famOvermodBc; // a1 / |Bc|
float famOvermodBccosDelta; // a1 / |Bc| * cos(Delta)
float famOvermodBcsinDelta; // a1 / |Bc| * sin(Delta)
float ftmp; // scratch
int8 i; // loop counter
// calculate the measurement vector moduli and return with identity quaternion if either is null.
fmodGsSq = fGs[CHX] * fGs[CHX] + fGs[CHY] * fGs[CHY] + fGs[CHZ] * fGs[CHZ];
fmodBcSq = fBc[CHX] * fBc[CHX] + fBc[CHY] * fBc[CHY] + fBc[CHZ] * fBc[CHZ];
fmodGs = sqrtf(fmodGsSq);
fmodBc = sqrtf(fmodBcSq);
if ((fmodGs == 0.0F) || (fmodBc == 0.0F) || (fB == 0.0))
{
pfq->q0 = 1.0F;
pfq->q1 = pfq->q2 = pfq->q3 = 0.0F;
return;
}
// calculate the accelerometer and magnetometer noise covariances (units rad^2) and least squares weightings
*pfQvGQa = fabsf(fmodGsSq - 1.0F);
*pfQvBQd = fabsf(fmodBcSq - fB * fB);
fag = *pfQvBQd / (fB * fB * *pfQvGQa + *pfQvBQd);
fam = 1.0F - fag;
// compute useful ratios to reduce computation
fagOvermodGs = fag / fmodGs;
famOvermodBc = fam / fmodBc;
famOvermodBccosDelta = famOvermodBc * fcosDelta;
famOvermodBcsinDelta = famOvermodBc * fsinDelta;
// compute the scalar product Gs.Bc and 6DOF accelerometer plus magnetometer geomagnetic inclination angle (deg)
fGsdotBc = fGs[CHX] * fBc[CHX] + fGs[CHY] * fBc[CHY] + fGs[CHZ] * fBc[CHZ];
*pfDelta6DOF = -fasin_deg((fGsdotBc) / (fmodGs * fmodBc));
// set the K matrix to the non-zero accelerometer components
fK[0][0] = fK[3][3] = fagOvermodGs * fGs[CHZ];
fK[1][1] = fK[2][2] = -fK[0][0];
fK[0][1] = fK[2][3] = fagOvermodGs * fGs[CHY];
fK[1][3] = fagOvermodGs * fGs[CHX];
fK[0][2] = -fK[1][3];
// update the K matrix with the magnetometer component
ftmp = famOvermodBcsinDelta * fBc[CHX];
fK[0][2] += ftmp;
fK[1][3] -= ftmp;
fK[0][3] = fK[1][2] = famOvermodBccosDelta * fBc[CHX];
ftmp = famOvermodBccosDelta * fBc[CHY];
fK[0][0] += ftmp;
fK[1][1] -= ftmp;
fK[2][2] += ftmp;
fK[3][3] -= ftmp;
ftmp = famOvermodBcsinDelta * fBc[CHZ];
fK[0][0] -= ftmp;
fK[1][1] += ftmp;
fK[2][2] += ftmp;
fK[3][3] -= ftmp;
ftmp = famOvermodBccosDelta * fBc[CHZ];
fK[0][1] -= ftmp;
fK[2][3] += ftmp;
ftmp = famOvermodBcsinDelta * fBc[CHY];
fK[0][1] -= ftmp;
fK[2][3] -= ftmp;
// copy above diagonal elements to below diagonal
fK[1][0] = fK[0][1];
fK[2][0] = fK[0][2];
fK[2][1] = fK[1][2];
fK[3][0] = fK[0][3];
fK[3][1] = fK[1][3];
fK[3][2] = fK[2][3];
// set eigval to the unsorted eigenvalues and eigvec to the unsorted normalized eigenvectors of fK
eigencompute4(fK, eigval, eigvec, 4);
// copy the largest eigenvector into the orientation quaternion fq
i = 0;
if (eigval[1] > eigval[i]) i = 1;
if (eigval[2] > eigval[i]) i = 2;
if (eigval[3] > eigval[i]) i = 3;
pfq->q0 = eigvec[0][i];
pfq->q1 = eigvec[1][i];
pfq->q2 = eigvec[2][i];
pfq->q3 = eigvec[3][i];
// force q0 to be non-negative
if (pfq->q0 < 0.0F)
{
pfq->q0 = -pfq->q0;
pfq->q1 = -pfq->q1;
pfq->q2 = -pfq->q2;
pfq->q3 = -pfq->q3;
}
return;
}
// Win8: 6DOF e-Compass function computing least squares fit to orientation quaternion fq
// on the assumption that the geomagnetic field fB and magnetic inclination angle fDelta are known
void fLeastSquareseCompassWin8(struct fquaternion *pfq, float fB, float fDelta, float fsinDelta, float fcosDelta,
float *pfDelta6DOF, float fBc[], float fGs[], float *pfQvBQd, float *pfQvGQa)
{
// local variables
float fK[4][4]; // K measurement matrix
float eigvec[4][4]; // matrix of eigenvectors of K
float eigval[4]; // vector of eigenvalues of K
float fmodGsSq; // modulus of fGs[] squared
float fmodBcSq; // modulus of fBc[] squared
float fmodGs; // modulus of fGs[]
float fmodBc; // modulus of fBc[]
float fGsdotBc; // scalar product of Gs and Bc
float fag, fam; // relative weightings
float fagOvermodGs; // a0 / |Gs|
float famOvermodBc; // a1 / |Bc|
float famOvermodBccosDelta; // a1 / |Bc| * cos(Delta)
float famOvermodBcsinDelta; // a1 / |Bc| * sin(Delta)
float ftmp; // scratch
int8 i; // loop counter
// calculate the measurement vector moduli and return with identity quaternion if either is null.
fmodGsSq = fGs[CHX] * fGs[CHX] + fGs[CHY] * fGs[CHY] + fGs[CHZ] * fGs[CHZ];
fmodBcSq = fBc[CHX] * fBc[CHX] + fBc[CHY] * fBc[CHY] + fBc[CHZ] * fBc[CHZ];
fmodGs = sqrtf(fmodGsSq);
fmodBc = sqrtf(fmodBcSq);
if ((fmodGs == 0.0F) || (fmodBc == 0.0F) || (fB == 0.0))
{
pfq->q0 = 1.0F;
pfq->q1 = pfq->q2 = pfq->q3 = 0.0F;
return;
}
// calculate the accelerometer and magnetometer noise covariances (units rad^2) and least squares weightings
*pfQvGQa = fabsf(fmodGsSq - 1.0F);
*pfQvBQd = fabsf(fmodBcSq - fB * fB);
fag = *pfQvBQd / (fB * fB * *pfQvGQa + *pfQvBQd);
fam = 1.0F - fag;
// compute useful ratios to reduce computation
fagOvermodGs = fag / fmodGs;
famOvermodBc = fam / fmodBc;
famOvermodBccosDelta = famOvermodBc * fcosDelta;
famOvermodBcsinDelta = famOvermodBc * fsinDelta;
// compute the scalar product Gs.Bc and 6DOF accelerometer plus magnetometer geomagnetic inclination angle (deg)
fGsdotBc = fGs[CHX] * fBc[CHX] + fGs[CHY] * fBc[CHY] + fGs[CHZ] * fBc[CHZ];
*pfDelta6DOF = fasin_deg((fGsdotBc) / (fmodGs * fmodBc));
// set the K matrix to the non-zero accelerometer components
fK[0][0] = fK[3][3] = -fagOvermodGs * fGs[CHZ];
fK[1][1] = fK[2][2] = -fK[0][0];
fK[0][1] = fK[2][3] = -fagOvermodGs * fGs[CHY];
fK[1][3] = -fagOvermodGs * fGs[CHX];
fK[0][2] = -fK[1][3];
// update the K matrix with the magnetometer component
ftmp = famOvermodBcsinDelta * fBc[CHX];
fK[0][2] += ftmp;
fK[1][3] -= ftmp;
fK[0][3] = fK[1][2] = famOvermodBccosDelta * fBc[CHX];
ftmp = famOvermodBccosDelta * fBc[CHY];
fK[0][0] += ftmp;
fK[1][1] -= ftmp;
fK[2][2] += ftmp;
fK[3][3] -= ftmp;
ftmp = famOvermodBcsinDelta * fBc[CHZ];
fK[0][0] -= ftmp;
fK[1][1] += ftmp;
fK[2][2] += ftmp;
fK[3][3] -= ftmp;
ftmp = famOvermodBccosDelta * fBc[CHZ];
fK[0][1] -= ftmp;
fK[2][3] += ftmp;
ftmp = famOvermodBcsinDelta * fBc[CHY];
fK[0][1] -= ftmp;
fK[2][3] -= ftmp;
// copy above diagonal elements to below diagonal
fK[1][0] = fK[0][1];
fK[2][0] = fK[0][2];
fK[2][1] = fK[1][2];
fK[3][0] = fK[0][3];
fK[3][1] = fK[1][3];
fK[3][2] = fK[2][3];
// set eigval to the unsorted eigenvalues and eigvec to the unsorted normalized eigenvectors of fK
eigencompute4(fK, eigval, eigvec, 4);
// copy the largest eigenvector into the orientation quaternion fq
i = 0;
if (eigval[1] > eigval[i]) i = 1;
if (eigval[2] > eigval[i]) i = 2;
if (eigval[3] > eigval[i]) i = 3;
pfq->q0 = eigvec[0][i];
pfq->q1 = eigvec[1][i];
pfq->q2 = eigvec[2][i];
pfq->q3 = eigvec[3][i];
// force q0 to be non-negative
if (pfq->q0 < 0.0F)
{
pfq->q0 = -pfq->q0;
pfq->q1 = -pfq->q1;
pfq->q2 = -pfq->q2;
pfq->q3 = -pfq->q3;
}
return;
}
// extract the NED angles in degrees from the NED rotation matrix
void fNEDAnglesDegFromRotationMatrix(float R[][3], float *pfPhiDeg, float *pfTheDeg, float *pfPsiDeg,
float *pfRhoDeg, float *pfChiDeg)
{
// calculate the pitch angle -90.0 <= Theta <= 90.0 deg
*pfTheDeg = fasin_deg(-R[CHX][CHZ]);
// calculate the roll angle range -180.0 <= Phi < 180.0 deg
*pfPhiDeg = fatan2_deg(R[CHY][CHZ], R[CHZ][CHZ]);
// map +180 roll onto the functionally equivalent -180 deg roll
if (*pfPhiDeg == 180.0F)
{
*pfPhiDeg = -180.0F;
}
// calculate the yaw (compass) angle 0.0 <= Psi < 360.0 deg
if (*pfTheDeg == 90.0F)
{
// vertical upwards gimbal lock case
*pfPsiDeg = fatan2_deg(R[CHZ][CHY], R[CHY][CHY]) + *pfPhiDeg;
}
else if (*pfTheDeg == -90.0F)
{
// vertical downwards gimbal lock case
*pfPsiDeg = fatan2_deg(-R[CHZ][CHY], R[CHY][CHY]) - *pfPhiDeg;
}
else
{
// general case
*pfPsiDeg = fatan2_deg(R[CHX][CHY], R[CHX][CHX]);
}
// map yaw angle Psi onto range 0.0 <= Psi < 360.0 deg
if (*pfPsiDeg < 0.0F)
{
*pfPsiDeg += 360.0F;
}
// check for rounding errors mapping small negative angle to 360 deg
if (*pfPsiDeg >= 360.0F)
{
*pfPsiDeg = 0.0F;
}
// for NED, the compass heading Rho equals the yaw angle Psi
*pfRhoDeg = *pfPsiDeg;
// calculate the tilt angle from vertical Chi (0 <= Chi <= 180 deg)
*pfChiDeg = facos_deg(R[CHZ][CHZ]);
return;
}
// extract the Android angles in degrees from the Android rotation matrix
void fAndroidAnglesDegFromRotationMatrix(float R[][3], float *pfPhiDeg, float *pfTheDeg, float *pfPsiDeg,
float *pfRhoDeg, float *pfChiDeg)
{
// calculate the roll angle -90.0 <= Phi <= 90.0 deg
*pfPhiDeg = fasin_deg(R[CHX][CHZ]);
// calculate the pitch angle -180.0 <= The < 180.0 deg
*pfTheDeg = fatan2_deg(-R[CHY][CHZ], R[CHZ][CHZ]);
// map +180 pitch onto the functionally equivalent -180 deg pitch
if (*pfTheDeg == 180.0F)
{
*pfTheDeg = -180.0F;
}
// calculate the yaw (compass) angle 0.0 <= Psi < 360.0 deg
if (*pfPhiDeg == 90.0F)
{
// vertical downwards gimbal lock case
*pfPsiDeg = fatan2_deg(R[CHY][CHX], R[CHY][CHY]) - *pfTheDeg;
}
else if (*pfPhiDeg == -90.0F)
{
// vertical upwards gimbal lock case
*pfPsiDeg = fatan2_deg(R[CHY][CHX], R[CHY][CHY]) + *pfTheDeg;
}
else
{
// general case
*pfPsiDeg = fatan2_deg(-R[CHX][CHY], R[CHX][CHX]);
}
// map yaw angle Psi onto range 0.0 <= Psi < 360.0 deg
if (*pfPsiDeg < 0.0F)
{
*pfPsiDeg += 360.0F;
}
// check for rounding errors mapping small negative angle to 360 deg
if (*pfPsiDeg >= 360.0F)
{
*pfPsiDeg = 0.0F;
}
// the compass heading angle Rho equals the yaw angle Psi
// this definition is compliant with Motorola Xoom tablet behavior
*pfRhoDeg = *pfPsiDeg;
// calculate the tilt angle from vertical Chi (0 <= Chi <= 180 deg)
*pfChiDeg = facos_deg(R[CHZ][CHZ]);
return;
}
// extract the Windows 8 angles in degrees from the Windows 8 rotation matrix
void fWin8AnglesDegFromRotationMatrix(float R[][3], float *pfPhiDeg, float *pfTheDeg, float *pfPsiDeg,
float *pfRhoDeg, float *pfChiDeg)
{
// calculate the roll angle -90.0 <= Phi <= 90.0 deg
if (R[CHZ][CHZ] == 0.0F)
{
if (R[CHX][CHZ] >= 0.0F)
{
// tan(phi) is -infinity
*pfPhiDeg = -90.0F;
}
else
{
// tan(phi) is +infinity
*pfPhiDeg = 90.0F;
}
}
else
{
// general case
*pfPhiDeg = fatan_deg(-R[CHX][CHZ] / R[CHZ][CHZ]);
}
// first calculate the pitch angle The in the range -90.0 <= The <= 90.0 deg
*pfTheDeg = fasin_deg(R[CHY][CHZ]);
// use R[CHZ][CHZ]=cos(Phi)*cos(The) to correct the quadrant of The remembering
// cos(Phi) is non-negative so that cos(The) has the same sign as R[CHZ][CHZ].
if (R[CHZ][CHZ] < 0.0F)
{
// wrap The around +90 deg and -90 deg giving result 90 to 270 deg
*pfTheDeg = 180.0F - *pfTheDeg;
}
// map the pitch angle The to the range -180.0 <= The < 180.0 deg
if (*pfTheDeg >= 180.0F)
{
*pfTheDeg -= 360.0F;
}
// calculate the yaw angle Psi
if (*pfTheDeg == 90.0F)
{
// vertical upwards gimbal lock case: -270 <= Psi < 90 deg
*pfPsiDeg = fatan2_deg(R[CHX][CHY], R[CHX][CHX]) - *pfPhiDeg;
}
else if (*pfTheDeg == -90.0F)
{
// vertical downwards gimbal lock case: -270 <= Psi < 90 deg
*pfPsiDeg = fatan2_deg(R[CHX][CHY], R[CHX][CHX]) + *pfPhiDeg;
}
else
{
// general case: -180 <= Psi < 180 deg
*pfPsiDeg = fatan2_deg(-R[CHY][CHX], R[CHY][CHY]);
// correct the quadrant for Psi using the value of The (deg) to give -180 <= Psi < 380 deg
if (fabsf(*pfTheDeg) >= 90.0F)
{
*pfPsiDeg += 180.0F;
}
}
// map yaw angle Psi onto range 0.0 <= Psi < 360.0 deg
if (*pfPsiDeg < 0.0F)
{
*pfPsiDeg += 360.0F;
}
// check for any rounding error mapping small negative angle to 360 deg
if (*pfPsiDeg >= 360.0F)
{
*pfPsiDeg = 0.0F;
}
// compute the compass angle Rho = 360 - Psi
*pfRhoDeg = 360.0F - *pfPsiDeg;
// check for rounding errors mapping small negative angle to 360 deg and zero degree case
if (*pfRhoDeg >= 360.0F)
{
*pfRhoDeg = 0.0F;
}
// calculate the tilt angle from vertical Chi (0 <= Chi <= 180 deg)
*pfChiDeg = facos_deg(R[CHZ][CHZ]);
return;