annotate src/compass.c @ 625:5c2ca77ce2df

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