annotate src/compass_calib.c @ 644:1e695355dfc4

Merge
author heinrichsweikamp
date Mon, 24 May 2021 18:41:51 +0200
parents 4050675965ea
children bc214815deb2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
1 /////////////////////////////////////////////////////////////////////////////
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
2 //
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 628
diff changeset
3 // compass_calib.c next generation V3.09.4
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
4 //
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
5 // Calibrate hard-iron for magnetic compass measurements.
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
6 // Copyright (c) 2012-2019, JD Gascuel, HeinrichsWeikamp, all rights reserved.
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
7 //
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
8 //////////////////////////////////////////////////////////////////////////////
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
9
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
10 // 2015-05-22 [jDG] Make a smaller calibration code (15.6 --> 6.7 KB).
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
11 // 2019-05-14 [rl] make smaller again
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
12
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
13 #include "configuration.inc"
0
heinrichsweikamp
parents:
diff changeset
14 #include "compass.h"
heinrichsweikamp
parents:
diff changeset
15
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
16
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
17 #ifdef _compass
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
18
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
19
0
heinrichsweikamp
parents:
diff changeset
20 //////////////////////////////////////////////////////////////////////////////
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
21 //
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
22 // mH: Put compass data into bank 8 (stack) and bank 9 (variables)
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
23 // rl: could also be overlaid with p2_deco.c stack...
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
24 //
0
heinrichsweikamp
parents:
diff changeset
25 #ifndef UNIX
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
26 # pragma udata overlay bank8=0x800
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
27 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
28 # define RESET_C_STACK \
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
29 _asm \
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
30 LFSR 1, 0x800 \
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
31 LFSR 2, 0x800 \
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
32 _endasm
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
33 # pragma udata overlay bank9_compass
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 628
diff changeset
34 # pragma code compass_cal
0
heinrichsweikamp
parents:
diff changeset
35 #else
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
36 # define RESET_C_STACK
0
heinrichsweikamp
parents:
diff changeset
37 #endif
heinrichsweikamp
parents:
diff changeset
38
heinrichsweikamp
parents:
diff changeset
39 //////////////////////////////////////////////////////////////////////////////
heinrichsweikamp
parents:
diff changeset
40
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
41 static unsigned short int compass_N;
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
42
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
43 static float Su, Sv, Sw; // first order moments
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
44 static float Suu, Svv, Sww, Suv, Suw, Svw; // second order moments
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
45 static float Saa; // Suu + Svv + Sww
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
46 static float Saau; // Suuu + Svvu + Swwu third order moment
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
47 static float Saav; // Suuv + Svvv + Swwv third order moment
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
48 static float Saaw; // Suuw + Svvw + Swww third order moment
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
49
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
50 static float yu, yv, yw; // temp solution vector
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
51 static float uc, vc, wc; // temp sphere's center
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
52
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
53 static float yh,uh,S0,S1,S2,S3; // transfer vars for compass_solve_helper()
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
54
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
55 //////////////////////////////////////////////////////////////////////////////
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
56
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
57 void compass_reset_calibration()
0
heinrichsweikamp
parents:
diff changeset
58 {
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
59 RESET_C_STACK;
0
heinrichsweikamp
parents:
diff changeset
60
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
61 compass_N = 0;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
62 Su = Sv = Sw = 0.0;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
63 Suu = Svv = Sww = 0.0;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
64 Suv = Suw = Svw = 0.0;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
65 Saau = Saav = Saaw = 0.0;
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
66 compass_CX_f = compass_CY_f = compass_CZ_f = 0;
0
heinrichsweikamp
parents:
diff changeset
67 }
heinrichsweikamp
parents:
diff changeset
68
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
69 //////////////////////////////////////////////////////////////////////////////
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
70
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
71 void compass_add_calibration()
0
heinrichsweikamp
parents:
diff changeset
72 {
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
73 RESET_C_STACK;
0
heinrichsweikamp
parents:
diff changeset
74
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
75 // get filtered/calibrated magnetic direction
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
76 yu = (compass_DX_f - compass_CX_f) / 32768.0f;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
77 yv = (compass_DY_f - compass_CY_f) / 32768.0f;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
78 yw = (compass_DZ_f - compass_CZ_f) / 32768.0f;
0
heinrichsweikamp
parents:
diff changeset
79
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
80 // add to all moments
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
81 compass_N++;
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
82
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
83 Su += yu;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
84 Sv += yv;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
85 Sw += yw;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
86
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
87 Suu += yu*yu;
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
88 Suv += yu*yv;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
89 Suw += yu*yw;
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
90 Svv += yv*yv;
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
91 Svw += yv*yw;
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
92 Sww += yw*yw;
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
93
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
94 Saa = yu*yu + yv*yv + yw*yw;
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
95
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
96 Saau += yu * Saa;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
97 Saav += yv * Saa;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
98 Saaw += yw * Saa;
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
99 }
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
100
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
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
103 static float compass_discriminent(PARAMETER char column)
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
104 {
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
105 // basic symmetric matrix
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
106 OVERLAY float a = Suu, d = Suv, g = Suw;
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
107 OVERLAY float b = Suv, e = Svv, h = Svw;
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
108 OVERLAY float c = Suw, f = Svw, i = Sww;
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
109 OVERLAY float result;
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
110
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
111 // substitute a column, if asked to
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
112 if( column==1 ) { a = yu; b = yv; c = yw; }
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
113 if( column==2 ) { d = yu; e = yv; f = yw; }
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
114 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
115
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
116 // do the math in a couple of single terms to reduce
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
117 // the amount of required ".tmpdata" memory
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
118 result = a * (e * i - f * h);
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
119 result -= b * (d * i - f * g);
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
120 result += c * (d * h - e * g);
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
121
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
122 return result;
0
heinrichsweikamp
parents:
diff changeset
123 }
heinrichsweikamp
parents:
diff changeset
124
heinrichsweikamp
parents:
diff changeset
125 //////////////////////////////////////////////////////////////////////////////
heinrichsweikamp
parents:
diff changeset
126
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
127 static void compass_solve_helper(void)
0
heinrichsweikamp
parents:
diff changeset
128 {
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
129 // computes:
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
130 //
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
131 // yh = S0 - Saa*uh - compass_dotc(Su*uh + 2*S1, Sv*uh + 2*S2, Sw*uh + 2*S3);
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
132 //
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
133 // with compass_dotc(u,v,w) = u*uc + v*vc + w*wc the equation becomes:
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
134 //
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
135 // yh = S0 - Saa*uh - ( (Su*uh + 2*S1)*uc + (Sv*uh + 2*S2)*vc + (Sw*uh + 2*S3)*wc )
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
136 //
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
137 // this equation is now split into several single terms
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
138 // to reduce the amount of required ".tmpdata" memory:
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
139
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
140 yh = S0;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
141 yh -= Saa*uh;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
142 yh -= (Su*uh + 2*S1) * uc;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
143 yh -= (Sv*uh + 2*S2) * vc;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
144 yh -= (Sw*uh + 2*S3) * wc;
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
145
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
146 return;
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
147 }
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
148
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
149 //////////////////////////////////////////////////////////////////////////////
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
150
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
151 void compass_solve_calibration()
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
152 {
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
153 OVERLAY float delta;
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
154
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
155 RESET_C_STACK;
0
heinrichsweikamp
parents:
diff changeset
156
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
157 //---- Compute center of measured magnetic directions --------------------
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
158 uc = Su/compass_N;
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
159 vc = Sv/compass_N;
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
160 wc = Sw/compass_N;
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
161
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
162 //---- Normalize partial sums --------------------------------------------
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
163 //
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
164 // 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
165 // around the sphere center's (uc, vc, wc) as:
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
166 // uc = Su / N; The mean value
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
167 // x = u - uc; The difference to the mean.
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
168 //
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
169 // So:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
170 // 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
171 //
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
172 // We need the Sxx sum of 2nd orders:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
173 // 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
174 Suu -= Su*uc;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
175 Svv -= Sv*vc;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
176 Sww -= Sw*wc;
0
heinrichsweikamp
parents:
diff changeset
177
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
178 // (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
179 // Sxy = Suv - Su vc - Sv uc + N uc vc
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
180 // = Suv - Su vc - N vc uc + N uc vc
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
181 // = Suv - Su vc
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
182 Suv -= Su*vc;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
183 Suw -= Su*wc;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
184 Svw -= Sv*wc;
0
heinrichsweikamp
parents:
diff changeset
185
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
186 // (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
187 // 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
188 // Su = 0, um = Sx/N:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
189 // 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
190 // = Sxxx - 3 Sx*Suu/N - Sx**3/N**2
0
heinrichsweikamp
parents:
diff changeset
191
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
192 // (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
193 // 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
194 //
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
195 // Su = 0, Sv = 0, vm = Sy/N:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
196 // 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
197 //
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
198 // 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
199 // = 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
200 // = Sxxy - (Suu + Sx*Sx/N)*Sy/N - 2 Suv*Sx/N
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
201
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
202 Saa = Suu + Svv + Sww;
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
203
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
204 // compute yu = Saau - Saa*uc - compass_dotc(Su*uc + 2*Suu, Sv*uc + 2*Suv, Sw*uc + 2*Suw);
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
205 uh = uc; S0 = Saau; S1 = Suu; S2 = Suv; S3 = Suw;
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
206 compass_solve_helper();
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
207 yu = yh;
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
208
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
209 // compute yv = Saav - Saa*vc - compass_dotc(Su*vc + 2*Suv, Sv*vc + 2*Svv, Sw*vc + 2*Svw);
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
210 uh = vc; S0 = Saav; S1 = Suv; S2 = Svv; S3 = Svw;
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
211 compass_solve_helper();
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
212 yv = yh;
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
213
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
214 // compute yw = Saaw - Saa*wc - compass_dotc(Su*wc + 2*Suw, Sv*wc + 2*Svw, Sw*wc + 2*Sww);
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
215 uh = wc; S0 = Saaw; S1 = Suw; S2 = Svw; S3 = Sww;
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
216 compass_solve_helper();
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
217 yw = yh;
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
218
0
heinrichsweikamp
parents:
diff changeset
219
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
220 //---- Solve the system --------------------------------------------------
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
221 // 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
222 // 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
223 // uc Suw + vc Svw + wc Sww = (Suuw + Svvw + Swww) / 2
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
224 //
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
225 // Note this is symmetric, with a positive diagonal, hence discriminant is always not null
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
226
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
227 delta = 0.5f / compass_discriminent(0);
0
heinrichsweikamp
parents:
diff changeset
228
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
229 // computed new center, with offset values:
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
230 uc += compass_discriminent(1) * delta;
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
231 vc += compass_discriminent(2) * delta;
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
232 wc += compass_discriminent(3) * delta;
0
heinrichsweikamp
parents:
diff changeset
233
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
234 // add correction due to already applied calibration:
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
235 compass_CX_f += (short)(32768 * uc);
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
236 compass_CY_f += (short)(32768 * vc);
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
237 compass_CZ_f += (short)(32768 * wc);
0
heinrichsweikamp
parents:
diff changeset
238 }
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
239
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
240 #endif // _compass