annotate src/compass_calib.c @ 653:8bcd138ab744

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