Mercurial > public > hwos_code
annotate src/compass.c @ 646:5b7fe7777425
3.16 release
author | heinrichs weikamp |
---|---|
date | Thu, 14 Oct 2021 12:03:24 +0200 |
parents | cd58f7fc86db |
children | bc214815deb2 |
rev | line source |
---|---|
282
7d9edd3b8c86
Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents:
214
diff
changeset
|
1 ////////////////////////////////////////////////////////////////////////////// |
7d9edd3b8c86
Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents:
214
diff
changeset
|
2 /// compass.c |
623 | 3 /// Compute north direction from magnetic/acceleration measures V3.02.1 |
604 | 4 /// |
282
7d9edd3b8c86
Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents:
214
diff
changeset
|
5 /// Copyright (c) 2012-2015, JD Gascuel, HeinrichsWeikamp, all right reserved. |
0 | 6 ////////////////////////////////////////////////////////////////////////////// |
7 // HISTORY | |
8 // 2012-12-01 [jDG] Creation | |
9 // 2012-12-23 [jDG] Added filtering. | |
10 // 2012-12-30 [jDG] Added calibration (spherical best fit). | |
282
7d9edd3b8c86
Make a more compact COMPASS calibration code (<7KB), and add more tests.
jDG
parents:
214
diff
changeset
|
11 // 2015-05-22 [jDG] Minor cleanups. Smaller calibration code. |
0 | 12 |
623 | 13 #include "configuration.inc" |
0 | 14 #include "compass.h" |
15 | |
623 | 16 |
17 #ifdef _compass | |
18 | |
19 | |
0 | 20 ////////////////////////////////////////////////////////////////////////////// |
604 | 21 // mH: crude work-around, needs to be fixed up |
0 | 22 #ifndef UNIX |
604 | 23 # pragma udata overlay bank8=0x800 |
24 static char C_STACK[256]; // overlay C-code data stack here | |
25 # define RESET_C_STACK \ | |
26 _asm \ | |
27 LFSR 1, 0x800 \ | |
28 LFSR 2, 0x800 \ | |
29 _endasm | |
30 # pragma udata overlay bank9_compass | |
31 # pragma code compass_run | |
0 | 32 #else |
604 | 33 # define RESET_C_STACK |
0 | 34 #endif |
35 | |
36 ////////////////////////////////////////////////////////////////////////////// | |
37 // fifth order of polynomial approximation of atan(), giving 0.05 deg max error | |
38 // | |
604 | 39 #define K1 (5701) // needs K1/2**16 |
40 #define K2 (1645) // needs K2/2**48 WAS NEGATIV | |
41 #define K3 ( 446) // needs K3/2**80 | |
42 | |
0 | 43 |
44 ////////////////////////////////////////////////////////////////////////////// | |
604 | 45 // Interface to assembler multiplies |
46 | |
0 | 47 Int16 umul(PARAMETER Int16 a, PARAMETER Int16 b) |
48 { | |
604 | 49 extern Int16 compass_umul(void); |
50 extern Int16 compass_a, compass_b; | |
51 compass_a = a; | |
52 compass_b = b; | |
53 return compass_umul(); | |
0 | 54 } |
55 | |
56 Int16 imul(PARAMETER Int16 a, PARAMETER Int16 b) | |
57 { | |
604 | 58 extern Int16 compass_imul(void); |
59 extern Int16 compass_a, compass_b; | |
60 compass_a = a; | |
61 compass_b = b; | |
62 return compass_imul(); | |
0 | 63 } |
64 | |
65 ////////////////////////////////////////////////////////////////////////////// | |
66 /// Returns a / b * 2**16 | |
67 /// | |
604 | 68 /// A 16/16 -> 16 bits divide, returning a scaled result. |
0 | 69 /// Used to multiply fractional numbers in the range 0..1, |
70 /// represented as 0..32767. | |
604 | 71 |
0 | 72 Int16 udiv(PARAMETER Int16 a, PARAMETER Int16 b) |
73 { | |
604 | 74 OVERLAY Int16 d, r; |
623 | 75 OVERLAY char failsafe=250; |
76 | |
604 | 77 //---- Pre-scale both numerator and denominator -------------------------- |
78 while( (((a>>8) | (b>>8)) & 0xC0) == 0 ) | |
79 { | |
623 | 80 failsafe--; |
81 if( failsafe == 0 ) break; | |
82 | |
604 | 83 a <<= 1; |
84 b <<= 1; | |
85 } | |
0 | 86 |
604 | 87 //---- Make division trials ---------------------------------------------- |
88 d = 0x4000; // start with 0.5, because 1.0 is sign bit | |
89 b >>= 1; // hence pre-shift b | |
90 r = 0; | |
91 do { | |
92 if( a >= b ) // a is big enough ? | |
93 { | |
94 a -= b; // then count d times b out of it | |
95 r |= d; // and accumulate that bit | |
96 } | |
97 b >>= 1; // then loop trying twice smaller | |
98 d >>= 1; | |
99 } while( b ); | |
100 return r; | |
0 | 101 } |
102 | |
103 ////////////////////////////////////////////////////////////////////////////// | |
104 /// Computes atan(y/x) in Angle, for x, y in range 0..32767 | |
105 /// | |
106 /// Results a single quadrant Angle, in the range 0 .. Q_PI/2 | |
604 | 107 |
0 | 108 Angle utan(PARAMETER Int16 y, PARAMETER Int16 x) |
109 { | |
604 | 110 OVERLAY Int16 ratio, angle, x2, x3; |
0 | 111 |
604 | 112 //---- Handle zero divisor ----------------------------------------------- |
113 if( x == 0 ) | |
114 return (y == 0) ? 0 : Q_PIO2; | |
0 | 115 |
604 | 116 //---- Make it half-quadrant : 0 .. 45 deg ------------------------------- |
117 ratio = (x > y) ? udiv(y, x) : udiv(x, y); | |
0 | 118 |
604 | 119 //---- Then apply the polynomial approximation --------------------------- |
120 angle = umul(K1, ratio); // r*K1 / 2**16 | |
121 x2 = umul(ratio, ratio); // r**2 / 2**16 | |
122 x3 = umul(x2, ratio); // r**3 / 2**32 | |
123 angle -= umul(x3, K2); // K2*r**3 / 2**48: NEGATIV. | |
0 | 124 |
604 | 125 x3 = umul(x3, x2); // r**5 / 2**64 |
126 angle += umul(x3, K3); // K3*r**5 / 2**80 | |
0 | 127 |
604 | 128 //---- Recover the full quadrant ----------------------------------------- |
129 return (x < y) ? (Angle)(Q_PIO2 - angle) | |
130 : (Angle)(angle); | |
0 | 131 } |
132 | |
133 ////////////////////////////////////////////////////////////////////////////// | |
134 /// Computes atan2(y/x) in Angle, for x, y in range -32768 to 32767 | |
135 /// | |
136 /// Results a four quadrant Angle, in the range -Q_PI .. +Q_PI | |
604 | 137 |
0 | 138 Angle itan(PARAMETER Int16 y, PARAMETER Int16 x) |
139 { | |
604 | 140 // Beware: -32768 is not properly handled (sign error) |
141 if( x == -32768 ) x = -32767; | |
142 if( y == -32768 ) y = -32767; | |
0 | 143 |
604 | 144 if( x >= 0 ) |
145 if( y >= 0 ) // first quadrant: 0..90 deg. | |
146 return utan(y,x); | |
147 else // fourth quadrant: 0..-90 deg | |
148 return -utan(-y,x); | |
149 else | |
150 if( y >= 0 ) // second quadrant: 90..180 deg | |
151 return Q_PI - utan(y, -x); | |
152 else // third quadrant: -90..-180 deg; | |
153 return -Q_PI + utan(-y, -x); | |
0 | 154 } |
155 | |
156 ////////////////////////////////////////////////////////////////////////////// | |
157 /// Computes cos(theta) = sqrtf(x2/h2), | |
158 /// when theta = atan(y/x) and h2=x*x+y*y | |
159 /// | |
604 | 160 |
0 | 161 Int16 cosxh(PARAMETER Int16 x2, PARAMETER Int16 h2) |
162 { | |
604 | 163 OVERLAY Int16 r = 0; |
164 OVERLAY Int16 d = 0x4000; | |
0 | 165 |
604 | 166 do { |
167 OVERLAY Int16 a = r + d; | |
168 a = umul(a, a); | |
169 a = umul(a, h2); | |
170 if( a <= x2 ) r += d; | |
171 d >>= 1; | |
172 } while( d ); | |
0 | 173 |
604 | 174 return r; |
0 | 175 } |
176 | |
177 ////////////////////////////////////////////////////////////////////////////// | |
178 /// Computes both sin and cos of angle y/x, | |
179 /// with h = sqrt(x**2+y**2). | |
180 /// | |
604 | 181 |
0 | 182 void sincos(PARAMETER Int16 x, PARAMETER Int16 y, Int16* sin, Int16* cos) |
183 { | |
604 | 184 OVERLAY Int16 x2, y2, h2; |
623 | 185 OVERLAY char failsafe = 250; |
186 | |
187 //---- Fold into one quadrant -------------------------------------------- | |
604 | 188 OVERLAY char neg = 0; |
189 if( x < 0 ) | |
190 { | |
191 neg |= 1; | |
192 x = -x; | |
193 } | |
194 if( y < 0 ) | |
195 { | |
196 neg |= 2; | |
197 y = -y; | |
198 } | |
623 | 199 |
200 //---- Pre-scale both numerator and denominator ---------------------- | |
604 | 201 while( (((x>>8) | (y>>8)) & 0xE0) == 0 ) |
202 { | |
623 | 203 failsafe--; |
204 if( failsafe == 0 ) break; | |
205 | |
604 | 206 x <<= 1; |
207 y <<= 1; | |
208 } | |
0 | 209 |
604 | 210 //---- Uses trig() to do the stuff one on quadrant ------------------- |
211 x2 = umul(x,x); | |
212 y2 = umul(y,y); | |
213 h2 = x2 + y2; | |
214 x2 = cosxh(x2, h2); | |
0 | 215 |
604 | 216 //---- Results back in four quadrants -------------------------------- |
217 *cos = (neg & 1) ? -x2 : x2; | |
218 y2 = cosxh(y2, h2); | |
219 *sin = (neg & 2) ? -y2 : y2; | |
0 | 220 } |
221 | |
222 ////////////////////////////////////////////////////////////////////////////// | |
223 // | |
224 | |
225 void compass(void) | |
226 { | |
604 | 227 OVERLAY Int16 sin, cos; |
228 OVERLAY Int16 iBfx, iBfy, Gz; | |
229 OVERLAY Int16 iBpx, iBpy, iBpz; | |
230 | |
231 RESET_C_STACK; | |
0 | 232 |
623 | 233 //---- Detect uncalibrated compass --------------------------------------- |
234 if( !compass_CX_f && !compass_CY_f && !compass_CZ_f ) | |
235 { | |
236 // no usable compass is signaled by bit 15 set to 1 | |
237 compass_heading_new = 32768; | |
238 return; | |
239 } | |
240 | |
604 | 241 //---- Make hard iron correction ----------------------------------------- |
242 // Measured magnetometer orientation, measured ok | |
243 // From matthias drawing: (X,Y,Z) --> (X,Y,Z) : no rotation | |
244 iBpx = compass_DX_f - compass_CX_f; // X | |
245 iBpy = compass_DY_f - compass_CY_f; // Y | |
246 iBpz = compass_DZ_f - compass_CZ_f; // Z | |
0 | 247 |
604 | 248 //---- Calculate sine and cosine of roll angle Phi ----------------------- |
249 sincos(accel_DZ_f, accel_DY_f, &sin, &cos); | |
628 | 250 |
604 | 251 //---- rotate by roll angle (-Phi) --------------------------------------- |
252 iBfy = imul(iBpy, cos) - imul(iBpz, sin); | |
253 iBpz = imul(iBpy, sin) + imul(iBpz, cos); | |
254 Gz = imul(accel_DY_f, sin) + imul(accel_DZ_f, cos); | |
0 | 255 |
604 | 256 //---- calculate sin and cosine of pitch angle Theta --------------------- |
257 sincos(Gz, -accel_DX_f, &sin, &cos); // NOTE: changed sin sign | |
628 | 258 |
604 | 259 /* correct cosine if pitch not in range -90 to 90 degrees */ |
260 if( cos < 0 ) cos = -cos; | |
0 | 261 |
604 | 262 ///---- de-rotate by pitch angle Theta ----------------------------------- |
263 iBfx = imul(iBpx, cos) + imul(iBpz, sin); | |
0 | 264 |
604 | 265 //---- calculate current yaw = e-compass angle Psi ----------------------- |
266 // Result in degree (no need of 0.01 deg precision... | |
623 | 267 compass_heading_new = itan(-iBfy, iBfx) / 100; |
0 | 268 |
604 | 269 // Result in 0..360 range: |
623 | 270 if( compass_heading_new < 0 ) |
271 compass_heading_new += 360; | |
0 | 272 } |
623 | 273 |
274 #endif // _compass |