blob: 56ac9f9319894367e7664b34feab9a20e3d4c77b [file] [log] [blame]
Mathias Agopian73e0bc82011-05-17 22:54:42 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdio.h>
18
19#include <utils/Log.h>
20
21#include "Fusion.h"
22
23namespace android {
24
25// -----------------------------------------------------------------------
26
27template <typename TYPE>
28static inline TYPE sqr(TYPE x) {
29 return x*x;
30}
31
32template <typename T>
33static inline T clamp(T v) {
34 return v < 0 ? 0 : v;
35}
36
37template <typename TYPE, size_t C, size_t R>
38static mat<TYPE, R, R> scaleCovariance(
39 const mat<TYPE, C, R>& A,
40 const mat<TYPE, C, C>& P) {
41 // A*P*transpose(A);
42 mat<TYPE, R, R> APAt;
43 for (size_t r=0 ; r<R ; r++) {
44 for (size_t j=r ; j<R ; j++) {
45 double apat(0);
46 for (size_t c=0 ; c<C ; c++) {
47 double v(A[c][r]*P[c][c]*0.5);
48 for (size_t k=c+1 ; k<C ; k++)
49 v += A[k][r] * P[c][k];
50 apat += 2 * v * A[c][j];
51 }
52 APAt[j][r] = apat;
53 APAt[r][j] = apat;
54 }
55 }
56 return APAt;
57}
58
59template <typename TYPE, typename OTHER_TYPE>
60static mat<TYPE, 3, 3> crossMatrix(const vec<TYPE, 3>& p, OTHER_TYPE diag) {
61 mat<TYPE, 3, 3> r;
62 r[0][0] = diag;
63 r[1][1] = diag;
64 r[2][2] = diag;
65 r[0][1] = p.z;
66 r[1][0] =-p.z;
67 r[0][2] =-p.y;
68 r[2][0] = p.y;
69 r[1][2] = p.x;
70 r[2][1] =-p.x;
71 return r;
72}
73
74template <typename TYPE>
75static mat<TYPE, 3, 3> MRPsToMatrix(const vec<TYPE, 3>& p) {
76 mat<TYPE, 3, 3> res(1);
77 const mat<TYPE, 3, 3> px(crossMatrix(p, 0));
78 const TYPE ptp(dot_product(p,p));
79 const TYPE t = 4/sqr(1+ptp);
80 res -= t * (1-ptp) * px;
81 res += t * 2 * sqr(px);
82 return res;
83}
84
85template <typename TYPE>
86vec<TYPE, 3> matrixToMRPs(const mat<TYPE, 3, 3>& R) {
87 // matrix to MRPs
88 vec<TYPE, 3> q;
89 const float Hx = R[0].x;
90 const float My = R[1].y;
91 const float Az = R[2].z;
92 const float w = 1 / (1 + sqrtf( clamp( Hx + My + Az + 1) * 0.25f ));
93 q.x = sqrtf( clamp( Hx - My - Az + 1) * 0.25f ) * w;
94 q.y = sqrtf( clamp(-Hx + My - Az + 1) * 0.25f ) * w;
95 q.z = sqrtf( clamp(-Hx - My + Az + 1) * 0.25f ) * w;
96 q.x = copysignf(q.x, R[2].y - R[1].z);
97 q.y = copysignf(q.y, R[0].z - R[2].x);
98 q.z = copysignf(q.z, R[1].x - R[0].y);
99 return q;
100}
101
102template<typename TYPE, size_t SIZE>
103class Covariance {
104 mat<TYPE, SIZE, SIZE> mSumXX;
105 vec<TYPE, SIZE> mSumX;
106 size_t mN;
107public:
108 Covariance() : mSumXX(0.0f), mSumX(0.0f), mN(0) { }
109 void update(const vec<TYPE, SIZE>& x) {
110 mSumXX += x*transpose(x);
111 mSumX += x;
112 mN++;
113 }
114 mat<TYPE, SIZE, SIZE> operator()() const {
115 const float N = 1.0f / mN;
116 return mSumXX*N - (mSumX*transpose(mSumX))*(N*N);
117 }
118 void reset() {
119 mN = 0;
120 mSumXX = 0;
121 mSumX = 0;
122 }
123 size_t getCount() const {
124 return mN;
125 }
126};
127
128// -----------------------------------------------------------------------
129
130Fusion::Fusion() {
131 // process noise covariance matrix
132 const float w1 = gyroSTDEV;
133 const float w2 = biasSTDEV;
134 Q[0] = w1*w1;
135 Q[1] = w2*w2;
136
137 Ba.x = 0;
138 Ba.y = 0;
139 Ba.z = 1;
140
141 Bm.x = 0;
142 Bm.y = 1;
143 Bm.z = 0;
144
145 init();
146}
147
148void Fusion::init() {
149 // initial estimate: E{ x(t0) }
150 x = 0;
151
152 // initial covariance: Var{ x(t0) }
153 P = 0;
154
155 mInitState = 0;
156 mCount[0] = 0;
157 mCount[1] = 0;
158 mCount[2] = 0;
159 mData = 0;
160}
161
162bool Fusion::hasEstimate() const {
163 return (mInitState == (MAG|ACC|GYRO));
164}
165
166bool Fusion::checkInitComplete(int what, const vec3_t& d) {
167 if (mInitState == (MAG|ACC|GYRO))
168 return true;
169
170 if (what == ACC) {
171 mData[0] += d * (1/length(d));
172 mCount[0]++;
173 mInitState |= ACC;
174 } else if (what == MAG) {
175 mData[1] += d * (1/length(d));
176 mCount[1]++;
177 mInitState |= MAG;
178 } else if (what == GYRO) {
179 mData[2] += d;
180 mCount[2]++;
181 if (mCount[2] == 64) {
182 // 64 samples is good enough to estimate the gyro drift and
183 // doesn't take too much time.
184 mInitState |= GYRO;
185 }
186 }
187
188 if (mInitState == (MAG|ACC|GYRO)) {
189 // Average all the values we collected so far
190 mData[0] *= 1.0f/mCount[0];
191 mData[1] *= 1.0f/mCount[1];
192 mData[2] *= 1.0f/mCount[2];
193
194 // calculate the MRPs from the data collection, this gives us
195 // a rough estimate of our initial state
196 mat33_t R;
197 vec3_t up(mData[0]);
198 vec3_t east(cross_product(mData[1], up));
199 east *= 1/length(east);
200 vec3_t north(cross_product(up, east));
201 R << east << north << up;
202 x[0] = matrixToMRPs(R);
203
204 // NOTE: we could try to use the average of the gyro data
205 // to estimate the initial bias, but this only works if
206 // the device is not moving. For now, we don't use that value
207 // and start with a bias of 0.
208 x[1] = 0;
209
210 // initial covariance
211 P = 0;
212 }
213
214 return false;
215}
216
217void Fusion::handleGyro(const vec3_t& w, float dT) {
218 const vec3_t wdT(w * dT); // rad/s * s -> rad
219 if (!checkInitComplete(GYRO, wdT))
220 return;
221
222 predict(wdT);
223}
224
225status_t Fusion::handleAcc(const vec3_t& a) {
226 if (length(a) < 0.981f)
227 return BAD_VALUE;
228
229 if (!checkInitComplete(ACC, a))
230 return BAD_VALUE;
231
232 // ignore acceleration data if we're close to free-fall
233 const float l = 1/length(a);
234 update(a*l, Ba, accSTDEV*l);
235 return NO_ERROR;
236}
237
238status_t Fusion::handleMag(const vec3_t& m) {
239 // the geomagnetic-field should be between 30uT and 60uT
240 // reject obviously wrong magnetic-fields
241 if (length(m) > 100)
242 return BAD_VALUE;
243
244 if (!checkInitComplete(MAG, m))
245 return BAD_VALUE;
246
247 const vec3_t up( getRotationMatrix() * Ba );
248 const vec3_t east( cross_product(m, up) );
249 vec3_t north( cross_product(up, east) );
250
251 const float l = 1 / length(north);
252 north *= l;
253
254#if 0
255 // in practice the magnetic-field sensor is so wrong
256 // that there is no point trying to use it to constantly
257 // correct the gyro. instead, we use the mag-sensor only when
258 // the device points north (just to give us a reference).
259 // We're hoping that it'll actually point north, if it doesn't
260 // we'll be offset, but at least the instantaneous posture
261 // of the device will be correct.
262
263 const float cos_30 = 0.8660254f;
264 if (dot_product(north, Bm) < cos_30)
265 return BAD_VALUE;
266#endif
267
268 update(north, Bm, magSTDEV*l);
269 return NO_ERROR;
270}
271
272bool Fusion::checkState(const vec3_t& v) {
273 if (isnanf(length(v))) {
274 LOGW("9-axis fusion diverged. reseting state.");
275 P = 0;
276 x[1] = 0;
277 mInitState = 0;
278 mCount[0] = 0;
279 mCount[1] = 0;
280 mCount[2] = 0;
281 mData = 0;
282 return false;
283 }
284 return true;
285}
286
287vec3_t Fusion::getAttitude() const {
288 return x[0];
289}
290
291vec3_t Fusion::getBias() const {
292 return x[1];
293}
294
295mat33_t Fusion::getRotationMatrix() const {
296 return MRPsToMatrix(x[0]);
297}
298
299mat33_t Fusion::getF(const vec3_t& p) {
300 const float p0 = p.x;
301 const float p1 = p.y;
302 const float p2 = p.z;
303
304 // f(p, w)
305 const float p0p1 = p0*p1;
306 const float p0p2 = p0*p2;
307 const float p1p2 = p1*p2;
308 const float p0p0 = p0*p0;
309 const float p1p1 = p1*p1;
310 const float p2p2 = p2*p2;
311 const float pp = 0.5f * (1 - (p0p0 + p1p1 + p2p2));
312
313 mat33_t F;
314 F[0][0] = 0.5f*(p0p0 + pp);
315 F[0][1] = 0.5f*(p0p1 + p2);
316 F[0][2] = 0.5f*(p0p2 - p1);
317 F[1][0] = 0.5f*(p0p1 - p2);
318 F[1][1] = 0.5f*(p1p1 + pp);
319 F[1][2] = 0.5f*(p1p2 + p0);
320 F[2][0] = 0.5f*(p0p2 + p1);
321 F[2][1] = 0.5f*(p1p2 - p0);
322 F[2][2] = 0.5f*(p2p2 + pp);
323 return F;
324}
325
326mat33_t Fusion::getdFdp(const vec3_t& p, const vec3_t& we) {
327
328 // dF = | A = df/dp -F |
329 // | 0 0 |
330
331 mat33_t A;
332 A[0][0] = A[1][1] = A[2][2] = 0.5f * (p.x*we.x + p.y*we.y + p.z*we.z);
333 A[0][1] = 0.5f * (p.y*we.x - p.x*we.y - we.z);
334 A[0][2] = 0.5f * (p.z*we.x - p.x*we.z + we.y);
335 A[1][2] = 0.5f * (p.z*we.y - p.y*we.z - we.x);
336 A[1][0] = -A[0][1];
337 A[2][0] = -A[0][2];
338 A[2][1] = -A[1][2];
339 return A;
340}
341
342void Fusion::predict(const vec3_t& w) {
343 // f(p, w)
344 vec3_t& p(x[0]);
345
346 // There is a discontinuity at 2.pi, to avoid it we need to switch to
347 // the shadow of p when pT.p gets too big.
348 const float ptp(dot_product(p,p));
349 if (ptp >= 2.0f) {
350 p = -p * (1/ptp);
351 }
352
353 const mat33_t F(getF(p));
354
355 // compute w with the bias correction:
356 // w_estimated = w - b_estimated
357 const vec3_t& b(x[1]);
358 const vec3_t we(w - b);
359
360 // prediction
361 const vec3_t dX(F*we);
362
363 if (!checkState(dX))
364 return;
365
366 p += dX;
367
368 const mat33_t A(getdFdp(p, we));
369
370 // G = | G0 0 | = | -F 0 |
371 // | 0 1 | | 0 1 |
372
373 // P += A*P + P*At + F*Q*Ft
374 const mat33_t AP(A*transpose(P[0][0]));
375 const mat33_t PAt(P[0][0]*transpose(A));
376 const mat33_t FPSt(F*transpose(P[1][0]));
377 const mat33_t PSFt(P[1][0]*transpose(F));
378 const mat33_t FQFt(scaleCovariance(F, Q[0]));
379 P[0][0] += AP + PAt - FPSt - PSFt + FQFt;
380 P[1][0] += A*P[1][0] - F*P[1][1];
381 P[1][1] += Q[1];
382}
383
384void Fusion::update(const vec3_t& z, const vec3_t& Bi, float sigma) {
385 const vec3_t p(x[0]);
386 // measured vector in body space: h(p) = A(p)*Bi
387 const mat33_t A(MRPsToMatrix(p));
388 const vec3_t Bb(A*Bi);
389
390 // Sensitivity matrix H = dh(p)/dp
391 // H = [ L 0 ]
392 const float ptp(dot_product(p,p));
393 const mat33_t px(crossMatrix(p, 0.5f*(ptp-1)));
394 const mat33_t ppt(p*transpose(p));
395 const mat33_t L((8 / sqr(1+ptp))*crossMatrix(Bb, 0)*(ppt-px));
396
397 // update...
398 const mat33_t R(sigma*sigma);
399 const mat33_t S(scaleCovariance(L, P[0][0]) + R);
400 const mat33_t Si(invert(S));
401 const mat33_t LtSi(transpose(L)*Si);
402
403 vec<mat33_t, 2> K;
404 K[0] = P[0][0] * LtSi;
405 K[1] = transpose(P[1][0])*LtSi;
406
407 const vec3_t e(z - Bb);
408 const vec3_t K0e(K[0]*e);
409 const vec3_t K1e(K[1]*e);
410
411 if (!checkState(K0e))
412 return;
413
414 if (!checkState(K1e))
415 return;
416
417 x[0] += K0e;
418 x[1] += K1e;
419
420 // P -= K*H*P;
421 const mat33_t K0L(K[0] * L);
422 const mat33_t K1L(K[1] * L);
423 P[0][0] -= K0L*P[0][0];
424 P[1][1] -= K1L*P[1][0];
425 P[1][0] -= K0L*P[1][0];
426}
427
428// -----------------------------------------------------------------------
429
430}; // namespace android
431