blob: d706af598320c45cf17374d4cfb678f08874255c [file] [log] [blame]
Mathias Agopian984826c2011-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
Mathias Agopianeaf2d0b2011-06-13 16:00:49 -070027/*
28 * gyroVAR gives the measured variance of the gyro's output per
29 * Hz (or variance at 1 Hz). This is an "intrinsic" parameter of the gyro,
30 * which is independent of the sampling frequency.
31 *
32 * The variance of gyro's output at a given sampling period can be
33 * calculated as:
34 * variance(T) = gyroVAR / T
35 *
36 * The variance of the INTEGRATED OUTPUT at a given sampling period can be
37 * calculated as:
38 * variance_integrate_output(T) = gyroVAR * T
39 *
40 */
41static const float gyroVAR = 1e-7; // (rad/s)^2 / Hz
42static const float biasVAR = 1e-8; // (rad/s)^2 / s (guessed)
43
44/*
45 * Standard deviations of accelerometer and magnetometer
46 */
Mathias Agopian33015422011-05-27 18:18:13 -070047static const float accSTDEV = 0.05f; // m/s^2 (measured 0.08 / CDD 0.05)
48static const float magSTDEV = 0.5f; // uT (measured 0.7 / CDD 0.5)
Mathias Agopian984826c2011-05-17 22:54:42 -070049
Mathias Agopian33015422011-05-27 18:18:13 -070050static const float FREE_FALL_THRESHOLD = 0.981f;
51
52// -----------------------------------------------------------------------
Mathias Agopian984826c2011-05-17 22:54:42 -070053
54template <typename TYPE, size_t C, size_t R>
55static mat<TYPE, R, R> scaleCovariance(
56 const mat<TYPE, C, R>& A,
57 const mat<TYPE, C, C>& P) {
58 // A*P*transpose(A);
59 mat<TYPE, R, R> APAt;
60 for (size_t r=0 ; r<R ; r++) {
61 for (size_t j=r ; j<R ; j++) {
62 double apat(0);
63 for (size_t c=0 ; c<C ; c++) {
64 double v(A[c][r]*P[c][c]*0.5);
65 for (size_t k=c+1 ; k<C ; k++)
66 v += A[k][r] * P[c][k];
67 apat += 2 * v * A[c][j];
68 }
69 APAt[j][r] = apat;
70 APAt[r][j] = apat;
71 }
72 }
73 return APAt;
74}
75
76template <typename TYPE, typename OTHER_TYPE>
77static mat<TYPE, 3, 3> crossMatrix(const vec<TYPE, 3>& p, OTHER_TYPE diag) {
78 mat<TYPE, 3, 3> r;
79 r[0][0] = diag;
80 r[1][1] = diag;
81 r[2][2] = diag;
82 r[0][1] = p.z;
83 r[1][0] =-p.z;
84 r[0][2] =-p.y;
85 r[2][0] = p.y;
86 r[1][2] = p.x;
87 r[2][1] =-p.x;
88 return r;
89}
90
Mathias Agopian984826c2011-05-17 22:54:42 -070091
92template<typename TYPE, size_t SIZE>
93class Covariance {
94 mat<TYPE, SIZE, SIZE> mSumXX;
95 vec<TYPE, SIZE> mSumX;
96 size_t mN;
97public:
98 Covariance() : mSumXX(0.0f), mSumX(0.0f), mN(0) { }
99 void update(const vec<TYPE, SIZE>& x) {
100 mSumXX += x*transpose(x);
101 mSumX += x;
102 mN++;
103 }
104 mat<TYPE, SIZE, SIZE> operator()() const {
105 const float N = 1.0f / mN;
106 return mSumXX*N - (mSumX*transpose(mSumX))*(N*N);
107 }
108 void reset() {
109 mN = 0;
110 mSumXX = 0;
111 mSumX = 0;
112 }
113 size_t getCount() const {
114 return mN;
115 }
116};
117
118// -----------------------------------------------------------------------
119
120Fusion::Fusion() {
Mathias Agopian33015422011-05-27 18:18:13 -0700121 Phi[0][1] = 0;
122 Phi[1][1] = 1;
Mathias Agopian984826c2011-05-17 22:54:42 -0700123
124 Ba.x = 0;
125 Ba.y = 0;
126 Ba.z = 1;
127
128 Bm.x = 0;
129 Bm.y = 1;
130 Bm.z = 0;
131
132 init();
133}
134
135void Fusion::init() {
Mathias Agopian984826c2011-05-17 22:54:42 -0700136 mInitState = 0;
Mathias Agopian33015422011-05-27 18:18:13 -0700137 mGyroRate = 0;
Mathias Agopian984826c2011-05-17 22:54:42 -0700138 mCount[0] = 0;
139 mCount[1] = 0;
140 mCount[2] = 0;
141 mData = 0;
142}
143
Mathias Agopian33015422011-05-27 18:18:13 -0700144void Fusion::initFusion(const vec4_t& q, float dT)
145{
146 // initial estimate: E{ x(t0) }
147 x0 = q;
148 x1 = 0;
149
Mathias Agopianeaf2d0b2011-06-13 16:00:49 -0700150 // process noise covariance matrix: G.Q.Gt, with
151 //
152 // G = | -1 0 | Q = | q00 q10 |
153 // | 0 1 | | q01 q11 |
154 //
155 // q00 = sv^2.dt + 1/3.su^2.dt^3
156 // q10 = q01 = 1/2.su^2.dt^2
157 // q11 = su^2.dt
158 //
Mathias Agopian33015422011-05-27 18:18:13 -0700159
Mathias Agopianeaf2d0b2011-06-13 16:00:49 -0700160 // variance of integrated output at 1/dT Hz
161 // (random drift)
162 const float q00 = gyroVAR * dT;
163
164 // variance of drift rate ramp
165 const float q11 = biasVAR * dT;
166
167 const float u = q11 / dT;
168 const float q10 = 0.5f*u*dT*dT;
Mathias Agopian33015422011-05-27 18:18:13 -0700169 const float q01 = q10;
Mathias Agopianeaf2d0b2011-06-13 16:00:49 -0700170
171 GQGt[0][0] = q00; // rad^2
Mathias Agopian33015422011-05-27 18:18:13 -0700172 GQGt[1][0] = -q10;
173 GQGt[0][1] = -q01;
Mathias Agopianeaf2d0b2011-06-13 16:00:49 -0700174 GQGt[1][1] = q11; // (rad/s)^2
Mathias Agopian33015422011-05-27 18:18:13 -0700175
176 // initial covariance: Var{ x(t0) }
Mathias Agopianeaf2d0b2011-06-13 16:00:49 -0700177 // TODO: initialize P correctly
Mathias Agopian33015422011-05-27 18:18:13 -0700178 P = 0;
179}
180
Mathias Agopian984826c2011-05-17 22:54:42 -0700181bool Fusion::hasEstimate() const {
182 return (mInitState == (MAG|ACC|GYRO));
183}
184
Mathias Agopian33015422011-05-27 18:18:13 -0700185bool Fusion::checkInitComplete(int what, const vec3_t& d, float dT) {
186 if (hasEstimate())
Mathias Agopian984826c2011-05-17 22:54:42 -0700187 return true;
188
189 if (what == ACC) {
190 mData[0] += d * (1/length(d));
191 mCount[0]++;
192 mInitState |= ACC;
193 } else if (what == MAG) {
194 mData[1] += d * (1/length(d));
195 mCount[1]++;
196 mInitState |= MAG;
197 } else if (what == GYRO) {
Mathias Agopian33015422011-05-27 18:18:13 -0700198 mGyroRate = dT;
199 mData[2] += d*dT;
Mathias Agopian984826c2011-05-17 22:54:42 -0700200 mCount[2]++;
201 if (mCount[2] == 64) {
202 // 64 samples is good enough to estimate the gyro drift and
203 // doesn't take too much time.
204 mInitState |= GYRO;
205 }
206 }
207
208 if (mInitState == (MAG|ACC|GYRO)) {
209 // Average all the values we collected so far
210 mData[0] *= 1.0f/mCount[0];
211 mData[1] *= 1.0f/mCount[1];
212 mData[2] *= 1.0f/mCount[2];
213
214 // calculate the MRPs from the data collection, this gives us
215 // a rough estimate of our initial state
216 mat33_t R;
217 vec3_t up(mData[0]);
218 vec3_t east(cross_product(mData[1], up));
219 east *= 1/length(east);
220 vec3_t north(cross_product(up, east));
221 R << east << north << up;
Mathias Agopian33015422011-05-27 18:18:13 -0700222 const vec4_t q = matrixToQuat(R);
Mathias Agopian984826c2011-05-17 22:54:42 -0700223
Mathias Agopian33015422011-05-27 18:18:13 -0700224 initFusion(q, mGyroRate);
Mathias Agopian984826c2011-05-17 22:54:42 -0700225 }
226
227 return false;
228}
229
230void Fusion::handleGyro(const vec3_t& w, float dT) {
Mathias Agopian33015422011-05-27 18:18:13 -0700231 if (!checkInitComplete(GYRO, w, dT))
Mathias Agopian984826c2011-05-17 22:54:42 -0700232 return;
233
Mathias Agopian33015422011-05-27 18:18:13 -0700234 predict(w, dT);
Mathias Agopian984826c2011-05-17 22:54:42 -0700235}
236
237status_t Fusion::handleAcc(const vec3_t& a) {
Mathias Agopian33015422011-05-27 18:18:13 -0700238 // ignore acceleration data if we're close to free-fall
239 if (length(a) < FREE_FALL_THRESHOLD)
Mathias Agopian984826c2011-05-17 22:54:42 -0700240 return BAD_VALUE;
241
242 if (!checkInitComplete(ACC, a))
243 return BAD_VALUE;
244
Mathias Agopian984826c2011-05-17 22:54:42 -0700245 const float l = 1/length(a);
246 update(a*l, Ba, accSTDEV*l);
247 return NO_ERROR;
248}
249
250status_t Fusion::handleMag(const vec3_t& m) {
251 // the geomagnetic-field should be between 30uT and 60uT
252 // reject obviously wrong magnetic-fields
253 if (length(m) > 100)
254 return BAD_VALUE;
255
256 if (!checkInitComplete(MAG, m))
257 return BAD_VALUE;
258
259 const vec3_t up( getRotationMatrix() * Ba );
260 const vec3_t east( cross_product(m, up) );
261 vec3_t north( cross_product(up, east) );
262
263 const float l = 1 / length(north);
264 north *= l;
265
Mathias Agopian984826c2011-05-17 22:54:42 -0700266 update(north, Bm, magSTDEV*l);
267 return NO_ERROR;
268}
269
270bool Fusion::checkState(const vec3_t& v) {
271 if (isnanf(length(v))) {
272 LOGW("9-axis fusion diverged. reseting state.");
273 P = 0;
Mathias Agopian33015422011-05-27 18:18:13 -0700274 x1 = 0;
Mathias Agopian984826c2011-05-17 22:54:42 -0700275 mInitState = 0;
276 mCount[0] = 0;
277 mCount[1] = 0;
278 mCount[2] = 0;
279 mData = 0;
280 return false;
281 }
282 return true;
283}
284
Mathias Agopian33015422011-05-27 18:18:13 -0700285vec4_t Fusion::getAttitude() const {
286 return x0;
Mathias Agopian984826c2011-05-17 22:54:42 -0700287}
288
289vec3_t Fusion::getBias() const {
Mathias Agopian33015422011-05-27 18:18:13 -0700290 return x1;
Mathias Agopian984826c2011-05-17 22:54:42 -0700291}
292
293mat33_t Fusion::getRotationMatrix() const {
Mathias Agopian33015422011-05-27 18:18:13 -0700294 return quatToMatrix(x0);
Mathias Agopian984826c2011-05-17 22:54:42 -0700295}
296
Mathias Agopian33015422011-05-27 18:18:13 -0700297mat34_t Fusion::getF(const vec4_t& q) {
298 mat34_t F;
299 F[0].x = q.w; F[1].x =-q.z; F[2].x = q.y;
300 F[0].y = q.z; F[1].y = q.w; F[2].y =-q.x;
301 F[0].z =-q.y; F[1].z = q.x; F[2].z = q.w;
302 F[0].w =-q.x; F[1].w =-q.y; F[2].w =-q.z;
Mathias Agopian984826c2011-05-17 22:54:42 -0700303 return F;
304}
305
Mathias Agopian33015422011-05-27 18:18:13 -0700306void Fusion::predict(const vec3_t& w, float dT) {
307 const vec4_t q = x0;
308 const vec3_t b = x1;
309 const vec3_t we = w - b;
310 const vec4_t dq = getF(q)*((0.5f*dT)*we);
311 x0 = normalize_quat(q + dq);
Mathias Agopian984826c2011-05-17 22:54:42 -0700312
Mathias Agopian33015422011-05-27 18:18:13 -0700313 // P(k+1) = F*P(k)*Ft + G*Q*Gt
Mathias Agopian984826c2011-05-17 22:54:42 -0700314
Mathias Agopian33015422011-05-27 18:18:13 -0700315 // Phi = | Phi00 Phi10 |
316 // | 0 1 |
317 const mat33_t I33(1);
318 const mat33_t I33dT(dT);
319 const mat33_t wx(crossMatrix(we, 0));
320 const mat33_t wx2(wx*wx);
321 const float lwedT = length(we)*dT;
322 const float ilwe = 1/length(we);
323 const float k0 = (1-cosf(lwedT))*(ilwe*ilwe);
324 const float k1 = sinf(lwedT);
Mathias Agopian984826c2011-05-17 22:54:42 -0700325
Mathias Agopian33015422011-05-27 18:18:13 -0700326 Phi[0][0] = I33 - wx*(k1*ilwe) + wx2*k0;
327 Phi[1][0] = wx*k0 - I33dT - wx2*(ilwe*ilwe*ilwe)*(lwedT-k1);
Mathias Agopian984826c2011-05-17 22:54:42 -0700328
Mathias Agopian33015422011-05-27 18:18:13 -0700329 P = Phi*P*transpose(Phi) + GQGt;
Mathias Agopian984826c2011-05-17 22:54:42 -0700330}
331
332void Fusion::update(const vec3_t& z, const vec3_t& Bi, float sigma) {
Mathias Agopian33015422011-05-27 18:18:13 -0700333 vec4_t q(x0);
Mathias Agopian984826c2011-05-17 22:54:42 -0700334 // measured vector in body space: h(p) = A(p)*Bi
Mathias Agopian33015422011-05-27 18:18:13 -0700335 const mat33_t A(quatToMatrix(q));
Mathias Agopian984826c2011-05-17 22:54:42 -0700336 const vec3_t Bb(A*Bi);
337
338 // Sensitivity matrix H = dh(p)/dp
339 // H = [ L 0 ]
Mathias Agopian33015422011-05-27 18:18:13 -0700340 const mat33_t L(crossMatrix(Bb, 0));
Mathias Agopian984826c2011-05-17 22:54:42 -0700341
Mathias Agopian33015422011-05-27 18:18:13 -0700342 // gain...
343 // K = P*Ht / [H*P*Ht + R]
344 vec<mat33_t, 2> K;
Mathias Agopian984826c2011-05-17 22:54:42 -0700345 const mat33_t R(sigma*sigma);
346 const mat33_t S(scaleCovariance(L, P[0][0]) + R);
347 const mat33_t Si(invert(S));
348 const mat33_t LtSi(transpose(L)*Si);
Mathias Agopian984826c2011-05-17 22:54:42 -0700349 K[0] = P[0][0] * LtSi;
350 K[1] = transpose(P[1][0])*LtSi;
351
Mathias Agopian33015422011-05-27 18:18:13 -0700352 // update...
Mathias Agopian984826c2011-05-17 22:54:42 -0700353 // P -= K*H*P;
354 const mat33_t K0L(K[0] * L);
355 const mat33_t K1L(K[1] * L);
356 P[0][0] -= K0L*P[0][0];
357 P[1][1] -= K1L*P[1][0];
358 P[1][0] -= K0L*P[1][0];
Mathias Agopian33015422011-05-27 18:18:13 -0700359 P[0][1] = transpose(P[1][0]);
360
361 const vec3_t e(z - Bb);
362 const vec3_t dq(K[0]*e);
363 const vec3_t db(K[1]*e);
364
365 q += getF(q)*(0.5f*dq);
366 x0 = normalize_quat(q);
367 x1 += db;
Mathias Agopian984826c2011-05-17 22:54:42 -0700368}
369
370// -----------------------------------------------------------------------
371
372}; // namespace android
373