annotate src/compass_calib.c @ 618:7b3903536213

work on new battery menu
author heinrichsweikamp
date Mon, 04 Feb 2019 22:57:24 +0100
parents ca4556fb60b9
children c40025d8e750
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
1 //////////////////////////////////////////////////////////////////////////////
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
2 /// compass_calib.c
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
3 /// Calibrate hard-iron for magnetic compass measurements.
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
4 /// Copyright (c) 2012-2015, JD Gascuel, HeinrichsWeikamp, all right reserved.
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
5 //////////////////////////////////////////////////////////////////////////////
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
6 // 2015-05-22 [jDG] Make a smaller calibration code (15.6 --> 6.7 KB).
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
7
0
heinrichsweikamp
parents:
diff changeset
8 #include "compass.h"
heinrichsweikamp
parents:
diff changeset
9
heinrichsweikamp
parents:
diff changeset
10 //////////////////////////////////////////////////////////////////////////////
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
11 // mH: Put compass data into bank 8 (stack) and bank 9 (variables)
0
heinrichsweikamp
parents:
diff changeset
12 #ifndef UNIX
heinrichsweikamp
parents:
diff changeset
13 # pragma udata overlay bank8=0x800
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
14 static char C_STACK[256]; // overlay C-code data stack here
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
15 # define RESET_C_STACK \
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
16 _asm \
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
17 LFSR 1, 0x800 \
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
18 LFSR 2, 0x800 \
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
19 _endasm
0
heinrichsweikamp
parents:
diff changeset
20 # pragma udata overlay bank9_compass
heinrichsweikamp
parents:
diff changeset
21 #else
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
22 # define RESET_C_STACK
0
heinrichsweikamp
parents:
diff changeset
23 #endif
heinrichsweikamp
parents:
diff changeset
24
heinrichsweikamp
parents:
diff changeset
25 //////////////////////////////////////////////////////////////////////////////
heinrichsweikamp
parents:
diff changeset
26
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
27 static unsigned short int compass_N;
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
28
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
29 static float Su, Sv, Sw; // first order moments
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
30 static float Suu, Svv, Sww, Suv, Suw, Svw; // second order moments
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
31 static float Saa; // Suu + Svv + Sww
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
32 static float Saau; // Suuu + Svvu + Swwu // third order moment
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
33 static float Saav; // Suuv + Svvv + Swwv
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
34 static float Saaw; // Suuw + Svvw + Swww
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
35 static float yu, yv, yw; // temp solution vector
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
36 static float uc, vc, wc; // temp sphere's center
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
37
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
38 //////////////////////////////////////////////////////////////////////////////
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
39
0
heinrichsweikamp
parents:
diff changeset
40 void compass_reset_calibration()
heinrichsweikamp
parents:
diff changeset
41 {
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
42 RESET_C_STACK;
0
heinrichsweikamp
parents:
diff changeset
43
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
44 compass_N = 0;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
45 Su = Sv = Sw = 0.0;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
46 Suu = Svv = Sww = 0.0;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
47 Suv = Suw = Svw = 0.0;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
48 Saau = Saav = Saaw = 0.0;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
49 compass_CX_f = compass_CY_f = compass_CZ_f = 0;
0
heinrichsweikamp
parents:
diff changeset
50 }
heinrichsweikamp
parents:
diff changeset
51
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
52 //////////////////////////////////////////////////////////////////////////////
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
53
0
heinrichsweikamp
parents:
diff changeset
54 void compass_add_calibration()
heinrichsweikamp
parents:
diff changeset
55 {
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
56 RESET_C_STACK;
0
heinrichsweikamp
parents:
diff changeset
57
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
58 // get filtered/calibrated magnetic direction
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
59 yu = (compass_DX_f - compass_CX_f) / 32768.0f;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
60 yv = (compass_DY_f - compass_CY_f) / 32768.0f;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
61 yw = (compass_DZ_f - compass_CZ_f) / 32768.0f;
0
heinrichsweikamp
parents:
diff changeset
62
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
63 // add to all moments
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
64 compass_N++;
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
65
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
66 Su += yu;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
67 Sv += yv;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
68 Sw += yw;
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
69
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
70 Suu += yu*yu;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
71 Suv += yu*yv;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
72 Suw += yu*yw;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
73 Svv += yv*yv;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
74 Svw += yv*yw;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
75 Sww += yw*yw;
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
76
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
77 Saa = yu*yu + yv*yv + yw*yw;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
78 Saau += yu * Saa;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
79 Saav += yv * Saa;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
80 Saaw += yw * Saa;
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
81 }
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
82
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
83 //////////////////////////////////////////////////////////////////////////////
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
84
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
85 static float compass_discriminent(PARAMETER char column)
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
86 {
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
87 // basic symmetric matrix
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
88 OVERLAY float a = Suu, d = Suv, g = Suw;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
89 OVERLAY float b = Suv, e = Svv, h = Svw;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
90 OVERLAY float c = Suw, f = Svw, i = Sww;
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
91
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
92 // substitute a column, if asked to
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
93 if( column==1 ) { a = yu; b = yv; c = yw; }
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
94 if( column==2 ) { d = yu; e = yv; f = yw; }
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
95 if( column==3 ) { g = yu; h = yv; i = yw; }
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
96
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
97 // do the math
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
98 return a * (e * i - f * h)
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
99 - b * (d * i - f * g)
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
100 + c * (d * h - e * g);
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
101 }
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
102
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
103 //////////////////////////////////////////////////////////////////////////////
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
104
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
105 static float compass_dotc(PARAMETER float u, float v, float w)
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
106 {
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
107 return u*uc + v*vc + w*wc;
0
heinrichsweikamp
parents:
diff changeset
108 }
heinrichsweikamp
parents:
diff changeset
109
heinrichsweikamp
parents:
diff changeset
110 //////////////////////////////////////////////////////////////////////////////
heinrichsweikamp
parents:
diff changeset
111
heinrichsweikamp
parents:
diff changeset
112 void compass_solve_calibration()
heinrichsweikamp
parents:
diff changeset
113 {
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
114 OVERLAY float delta;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
115 RESET_C_STACK;
0
heinrichsweikamp
parents:
diff changeset
116
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
117 //---- Compute center of measured magnetic directions --------------------
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
118 uc = Su/compass_N;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
119 vc = Sv/compass_N;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
120 wc = Sw/compass_N;
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
121
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
122 //---- Normalize partial sums --------------------------------------------
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
123 //
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
124 // We measured the (u, v, w) values, and need the centered (x, y, z) ones
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
125 // around the sphere center's (uc, vc, wc) as:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
126 // uc = Su / N; The mean value
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
127 // x = u - uc; The differnce to the mean.
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
128 //
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
129 // So:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
130 // x**2 = (u - uc)**2 = u**2 - 2u*uc + uc**2
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
131 //
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
132 // We need the Sxx sum of 2nd orders:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
133 // Sxx = Suu - 2 uc Su + N*uc*(Su/N) = Suu - uc Su
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
134 Suu -= Su*uc;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
135 Svv -= Sv*vc;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
136 Sww -= Sw*wc;
0
heinrichsweikamp
parents:
diff changeset
137
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
138 // (u - uc)(v - vc) = uv - u vc - v uc + uc vc
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
139 // Sxy = Suv - Su vc - Sv uc + N uc vc
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
140 // = Suv - Su vc - N vc uc + N uc vc
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
141 // = Suv - Su vc
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
142 Suv -= Su*vc;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
143 Suw -= Su*wc;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
144 Svw -= Sv*wc;
0
heinrichsweikamp
parents:
diff changeset
145
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
146 // (u + um)**3 = u**3 + 3 u**2 um + 3 u um**2 + um**3
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
147 // Sxxx = Suuu + 3 um Suu + 3 um**2 Su + N.um**3
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
148 // Su = 0, um = Sx/N:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
149 // Suuu = Sxxx - 3 Sx*Suu/N - N.(Sx/N)**3
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
150 // = Sxxx - 3 Sx*Suu/N - Sx**3/N**2
0
heinrichsweikamp
parents:
diff changeset
151
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
152 // (u + um)**2 (v + vm) = (u**2 + 2 u um + um**2)(v + vm)
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
153 // Sxxy = Suuv + vm Suu + 2 um (Suv + vm Su) + um**2 (Sv + N.vm)
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
154 //
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
155 // Su = 0, Sv = 0, vm = Sy/N:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
156 // Sxxy = Suuv + vm Suu + 2 um Suv + N um**2 vm
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
157 //
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
158 // Suuv = Sxxy - (Sy/N) Suu - 2 (Sx/N) Suv - (Sx/N)**2 Sy
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
159 // = Sxxy - Suu*Sy/N - 2 Suv*Sx/N - Sx*Sx*Sy/N/N
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
160 // = Sxxy - (Suu + Sx*Sx/N)*Sy/N - 2 Suv*Sx/N
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
161 Saa = Suu + Svv + Sww;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
162 yu = Saau - Saa*uc - compass_dotc(Su*uc + 2*Suu, Sv*uc + 2*Suv, Sw*uc + 2*Suw);
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
163 yv = Saav - Saa*vc - compass_dotc(Su*vc + 2*Suv, Sv*vc + 2*Svv, Sw*vc + 2*Svw);
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
164 yw = Saaw - Saa*wc - compass_dotc(Su*wc + 2*Suw, Sv*wc + 2*Svw, Sw*wc + 2*Sww);
0
heinrichsweikamp
parents:
diff changeset
165
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
166 //---- Solve the system --------------------------------------------------
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
167 // uc Suu + vc Suv + wc Suw = (Suuu + Svvu + Swwu) / 2
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
168 // uc Suv + vc Svv + wc Svw = (Suuv + Svvv + Swwv) / 2
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
169 // uc Suw + vc Svw + wc Sww = (Suuw + Svvw + Swww) / 2
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
170 // Note this is symmetric, with a positive diagonal, hence
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
171 // discriminant is always not null.
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
172 delta = 0.5f / compass_discriminent(0);
0
heinrichsweikamp
parents:
diff changeset
173
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
174 // so computed new center, with offset values:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
175 uc += compass_discriminent(1) * delta;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
176 vc += compass_discriminent(2) * delta;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
177 wc += compass_discriminent(3) * delta;
0
heinrichsweikamp
parents:
diff changeset
178
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
179 // add correction due to already applied calibration:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
180 compass_CX_f += (short)(32768 * uc);
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
181 compass_CY_f += (short)(32768 * vc);
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
182 compass_CZ_f += (short)(32768 * wc);
0
heinrichsweikamp
parents:
diff changeset
183 }