annotate src/compass.c @ 623:c40025d8e750

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