Fusion.cpp revision eaf2d0bfe37415ba1e42a97608823e8dbef53220
1/* 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 27/* 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 */ 47static 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) 49 50static const float FREE_FALL_THRESHOLD = 0.981f; 51 52// ----------------------------------------------------------------------- 53 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 91 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() { 121 Phi[0][1] = 0; 122 Phi[1][1] = 1; 123 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() { 136 mInitState = 0; 137 mGyroRate = 0; 138 mCount[0] = 0; 139 mCount[1] = 0; 140 mCount[2] = 0; 141 mData = 0; 142} 143 144void Fusion::initFusion(const vec4_t& q, float dT) 145{ 146 // initial estimate: E{ x(t0) } 147 x0 = q; 148 x1 = 0; 149 150 // 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 // 159 160 // 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; 169 const float q01 = q10; 170 171 GQGt[0][0] = q00; // rad^2 172 GQGt[1][0] = -q10; 173 GQGt[0][1] = -q01; 174 GQGt[1][1] = q11; // (rad/s)^2 175 176 // initial covariance: Var{ x(t0) } 177 // TODO: initialize P correctly 178 P = 0; 179} 180 181bool Fusion::hasEstimate() const { 182 return (mInitState == (MAG|ACC|GYRO)); 183} 184 185bool Fusion::checkInitComplete(int what, const vec3_t& d, float dT) { 186 if (hasEstimate()) 187 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) { 198 mGyroRate = dT; 199 mData[2] += d*dT; 200 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; 222 const vec4_t q = matrixToQuat(R); 223 224 initFusion(q, mGyroRate); 225 } 226 227 return false; 228} 229 230void Fusion::handleGyro(const vec3_t& w, float dT) { 231 if (!checkInitComplete(GYRO, w, dT)) 232 return; 233 234 predict(w, dT); 235} 236 237status_t Fusion::handleAcc(const vec3_t& a) { 238 // ignore acceleration data if we're close to free-fall 239 if (length(a) < FREE_FALL_THRESHOLD) 240 return BAD_VALUE; 241 242 if (!checkInitComplete(ACC, a)) 243 return BAD_VALUE; 244 245 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 266 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; 274 x1 = 0; 275 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 285vec4_t Fusion::getAttitude() const { 286 return x0; 287} 288 289vec3_t Fusion::getBias() const { 290 return x1; 291} 292 293mat33_t Fusion::getRotationMatrix() const { 294 return quatToMatrix(x0); 295} 296 297mat34_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; 303 return F; 304} 305 306void 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); 312 313 // P(k+1) = F*P(k)*Ft + G*Q*Gt 314 315 // 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); 325 326 Phi[0][0] = I33 - wx*(k1*ilwe) + wx2*k0; 327 Phi[1][0] = wx*k0 - I33dT - wx2*(ilwe*ilwe*ilwe)*(lwedT-k1); 328 329 P = Phi*P*transpose(Phi) + GQGt; 330} 331 332void Fusion::update(const vec3_t& z, const vec3_t& Bi, float sigma) { 333 vec4_t q(x0); 334 // measured vector in body space: h(p) = A(p)*Bi 335 const mat33_t A(quatToMatrix(q)); 336 const vec3_t Bb(A*Bi); 337 338 // Sensitivity matrix H = dh(p)/dp 339 // H = [ L 0 ] 340 const mat33_t L(crossMatrix(Bb, 0)); 341 342 // gain... 343 // K = P*Ht / [H*P*Ht + R] 344 vec<mat33_t, 2> K; 345 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); 349 K[0] = P[0][0] * LtSi; 350 K[1] = transpose(P[1][0])*LtSi; 351 352 // update... 353 // 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]; 359 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; 368} 369 370// ----------------------------------------------------------------------- 371 372}; // namespace android 373 374