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