annotate src/compass_calib.c @ 623:c40025d8e750

3.03 beta released
author heinrichsweikamp
date Mon, 03 Jun 2019 14:01:48 +0200
parents ca4556fb60b9
children cd58f7fc86db
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 //
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
3 // compass_calib.c next generation V3.03.4
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).
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
11 // 2019-05-14 [rl] make it even smaller again by another 2.000 byte
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
0
heinrichsweikamp
parents:
diff changeset
34 #else
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
35 # define RESET_C_STACK
0
heinrichsweikamp
parents:
diff changeset
36 #endif
heinrichsweikamp
parents:
diff changeset
37
heinrichsweikamp
parents:
diff changeset
38 //////////////////////////////////////////////////////////////////////////////
heinrichsweikamp
parents:
diff changeset
39
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
40 static unsigned short int compass_N;
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
41
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
42 static float Su, Sv, Sw; // first order moments
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
43 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
44 static float Saa; // Suu + Svv + Sww
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
45 static float Saau; // Suuu + Svvu + Swwu third order moment
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
46 static float Saav; // Suuv + Svvv + Swwv third order moment
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
47 static float Saaw; // Suuw + Svvw + Swww third order moment
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
48
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
49 static float yu, yv, yw; // temp solution vector
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
50 static float uc, vc, wc; // temp sphere's center
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
51
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
52 static float yh, uh, S0, S1, S2, S3; // transfer vars for compass_solve_helper()
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
53 static float discriminant, delta; // transfer vars for calc_discriminant()
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
54
282
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 //////////////////////////////////////////////////////////////////////////////
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
57
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
58 #ifndef UNIX
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
59 # pragma code compass_calib
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
60 #endif
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
61
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
62 //////////////////////////////////////////////////////////////////////////////
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
63
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
64 void compass_reset_calibration() // 202 byte
0
heinrichsweikamp
parents:
diff changeset
65 {
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
66 RESET_C_STACK;
0
heinrichsweikamp
parents:
diff changeset
67
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
68 compass_N = 0;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
69 Su = Sv = Sw = 0.0;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
70 Suu = Svv = Sww = 0.0;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
71 Suv = Suw = Svw = 0.0;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
72 Saau = Saav = Saaw = 0.0;
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
73 compass_CX_f = compass_CY_f = compass_CZ_f = 0; // int16
0
heinrichsweikamp
parents:
diff changeset
74 }
heinrichsweikamp
parents:
diff changeset
75
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
76 //////////////////////////////////////////////////////////////////////////////
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
77
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
78 void compass_add_calibration() // 1488 byte
0
heinrichsweikamp
parents:
diff changeset
79 {
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
80 overlay float SQR_yu, SQR_yv, SQR_yw; // squared values;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
81
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
82 RESET_C_STACK;
0
heinrichsweikamp
parents:
diff changeset
83
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
84 // get filtered/calibrated magnetic direction // 276 byte
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
85 yu = (compass_DX_f - compass_CX_f) / 32768.0f;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
86 yv = (compass_DY_f - compass_CY_f) / 32768.0f;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
87 yw = (compass_DZ_f - compass_CZ_f) / 32768.0f;
0
heinrichsweikamp
parents:
diff changeset
88
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
89 // compute squared values // 156 byte
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
90 SQR_yu = yu*yu;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
91 SQR_yv = yv*yv;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
92 SQR_yw = yw*yw;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
93
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
94 // increment count
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
95 compass_N++;
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
96
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
97 // add to all moments // 156 byte
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
98 Su += yu;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
99 Sv += yv;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
100 Sw += yw;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
101
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
102 // // 156 byte
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
103 Suu += SQR_yu;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
104 Svv += SQR_yv;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
105 Sww += SQR_yw;
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
106
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
107 // // 312 byte
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
108 Suv += yu*yv;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
109 Suw += yu*yw;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
110 Svw += yv*yw;
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
111
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
112 // // 104 byte
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
113 Saa = SQR_yu + SQR_yv + SQR_yw;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
114
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
115
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
116 // // 312 byte
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
117 Saau += yu * Saa;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
118 Saav += yv * Saa;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
119 Saaw += yw * Saa;
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
120 }
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
121
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
122 //////////////////////////////////////////////////////////////////////////////
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
123
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
124 static void calc_discriminant(PARAMETER char column)
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
125 {
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
126 // basic symmetric matrix
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
127 overlay float a = Suu, d = Suv, g = Suw;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
128 overlay float b = Suv, e = Svv, h = Svw;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
129 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
130
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
131 // substitute a column, if asked to
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
132 if( column==1 ) { a = yu; b = yv; c = yw; }
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
133 if( column==2 ) { d = yu; e = yv; f = yw; }
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
134 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
135
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
136 // do the math in a couple of single terms to reduce
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
137 // the amount of required ".tmpdata" memory
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
138 discriminant = a * (e * i - f * h);
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
139 discriminant -= b * (d * i - f * g);
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
140 discriminant += c * (d * h - e * g);
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
141
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
142 // apply delta if column = 1/2/3
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
143 if( column ) discriminant *= delta;
0
heinrichsweikamp
parents:
diff changeset
144 }
heinrichsweikamp
parents:
diff changeset
145
heinrichsweikamp
parents:
diff changeset
146 //////////////////////////////////////////////////////////////////////////////
heinrichsweikamp
parents:
diff changeset
147
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
148 static void compass_solve_helper(void) // 338 byte
0
heinrichsweikamp
parents:
diff changeset
149 {
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
150 // computes:
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
151 //
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
152 // 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
153 //
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
154 // 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
155 //
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
156 // 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
157 //
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
158 // this equation is now split into several single terms
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
159 // to reduce the amount of required ".tmpdata" memory:
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
160
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
161 yh = S0;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
162 yh -= Saa*uh;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
163 yh -= (Su*uh + 2*S1) * uc;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
164 yh -= (Sv*uh + 2*S2) * vc;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
165 yh -= (Sw*uh + 2*S3) * wc;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
166 }
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
167
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
168 //////////////////////////////////////////////////////////////////////////////
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
169
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
170 void compass_solve_calibration() // 1738 byte
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
171 {
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
172 overlay float inv_compass_N;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
173
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
174 RESET_C_STACK;
0
heinrichsweikamp
parents:
diff changeset
175
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
176 // compute center of measured magnetic directions // 200 byte
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
177 inv_compass_N = 1 / compass_N;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
178 uc = Su * inv_compass_N;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
179 vc = Sv * inv_compass_N;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
180 wc = Sw * inv_compass_N;
282
7d9edd3b8c86 Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents: 96
diff changeset
181
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
182 // normalize partial sums
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
183 //
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
184 // 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
185 // around the sphere center's (uc, vc, wc) as:
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
186 // uc = Su / N; The mean value
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
187 // x = u - uc; The difference to the mean.
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
188 //
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
189 // So:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
190 // 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
191 //
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
192 // We need the Sxx sum of 2nd orders:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
193 // 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
194 Suu -= Su*uc;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
195 Svv -= Sv*vc;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
196 Sww -= Sw*wc;
0
heinrichsweikamp
parents:
diff changeset
197
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
198 // (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
199 // Sxy = Suv - Su vc - Sv uc + N uc vc
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
200 // = Suv - Su vc - N vc uc + N uc vc
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
201 // = Suv - Su vc
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
202 Suv -= Su*vc;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
203 Suw -= Su*wc;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
204 Svw -= Sv*wc;
0
heinrichsweikamp
parents:
diff changeset
205
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
206 // (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
207 // 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
208 // Su = 0, um = Sx/N:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
209 // 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
210 // = Sxxx - 3 Sx*Suu/N - Sx**3/N**2
0
heinrichsweikamp
parents:
diff changeset
211
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
212 // (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
213 // 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
214 //
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
215 // Su = 0, Sv = 0, vm = Sy/N:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
216 // 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
217 //
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
218 // 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
219 // = 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
220 // = Sxxy - (Suu + Sx*Sx/N)*Sy/N - 2 Suv*Sx/N
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
221
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
222 Saa = Suu + Svv + Sww;
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
223
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
224 // compute yu = Saau - Saa*uc - compass_dotc(Su*uc + 2*Suu, Sv*uc + 2*Suv, Sw*uc + 2*Suw);
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
225 //
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
226 uh = uc; S0 = Saau; S1 = Suu; S2 = Suv; S3 = Suw; compass_solve_helper(); yu = yh;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
227
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
228 // compute yv = Saav - Saa*vc - compass_dotc(Su*vc + 2*Suv, Sv*vc + 2*Svv, Sw*vc + 2*Svw);
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
229 //
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
230 uh = vc; S0 = Saav; S1 = Suv; S2 = Svv; S3 = Svw; compass_solve_helper(); yv = yh;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
231
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
232 // compute yw = Saaw - Saa*wc - compass_dotc(Su*wc + 2*Suw, Sv*wc + 2*Svw, Sw*wc + 2*Sww);
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
233 //
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
234 uh = wc; S0 = Saaw; S1 = Suw; S2 = Svw; S3 = Sww; compass_solve_helper(); yw = yh;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
235
0
heinrichsweikamp
parents:
diff changeset
236
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
237 //---- Solve the system --------------------------------------------------
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
238 // 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
239 // 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
240 // uc Suw + vc Svw + wc Sww = (Suuw + Svvw + Swww) / 2
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
241 //
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
242 // Note this is symmetric, with a positive diagonal, hence discriminant is always not null
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
243
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
244 //compute delta value
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
245 calc_discriminant(0); delta = 0.5f / discriminant;
0
heinrichsweikamp
parents:
diff changeset
246
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
247 // compute new center, with offset values
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
248 calc_discriminant(1); uc += discriminant;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
249 calc_discriminant(2); vc += discriminant;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
250 calc_discriminant(3); wc += discriminant;
0
heinrichsweikamp
parents:
diff changeset
251
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
252 // add correction due to already applied calibration
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
253 compass_CX_f += (short)(32768 * uc);
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
254 compass_CY_f += (short)(32768 * vc);
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 282
diff changeset
255 compass_CZ_f += (short)(32768 * wc);
0
heinrichsweikamp
parents:
diff changeset
256 }
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
257
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
258 #endif // _compass