Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 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 | |
| 23 | namespace android { |
| 24 | |
| 25 | // ----------------------------------------------------------------------- |
| 26 | |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 27 | /*==================== BEGIN FUSION SENSOR PARAMETER =========================*/ |
| 28 | |
| 29 | /* Note: |
| 30 | * If a platform uses software fusion, it is necessary to tune the following |
| 31 | * parameters to fit the hardware sensors prior to release. |
| 32 | * |
| 33 | * The DEFAULT_ parameters will be used in FUSION_9AXIS and FUSION_NOMAG mode. |
| 34 | * The GEOMAG_ parameters will be used in FUSION_NOGYRO mode. |
| 35 | */ |
| 36 | |
Mathias Agopian | eaf2d0b | 2011-06-13 16:00:49 -0700 | [diff] [blame] | 37 | /* |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 38 | * GYRO_VAR gives the measured variance of the gyro's output per |
Mathias Agopian | eaf2d0b | 2011-06-13 16:00:49 -0700 | [diff] [blame] | 39 | * Hz (or variance at 1 Hz). This is an "intrinsic" parameter of the gyro, |
| 40 | * which is independent of the sampling frequency. |
| 41 | * |
| 42 | * The variance of gyro's output at a given sampling period can be |
| 43 | * calculated as: |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 44 | * variance(T) = GYRO_VAR / T |
Mathias Agopian | eaf2d0b | 2011-06-13 16:00:49 -0700 | [diff] [blame] | 45 | * |
| 46 | * The variance of the INTEGRATED OUTPUT at a given sampling period can be |
| 47 | * calculated as: |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 48 | * variance_integrate_output(T) = GYRO_VAR * T |
Mathias Agopian | eaf2d0b | 2011-06-13 16:00:49 -0700 | [diff] [blame] | 49 | */ |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 50 | static const float DEFAULT_GYRO_VAR = 1e-7; // (rad/s)^2 / Hz |
| 51 | static const float DEFAULT_GYRO_BIAS_VAR = 1e-12; // (rad/s)^2 / s (guessed) |
| 52 | static const float GEOMAG_GYRO_VAR = 1e-4; // (rad/s)^2 / Hz |
| 53 | static const float GEOMAG_GYRO_BIAS_VAR = 1e-8; // (rad/s)^2 / s (guessed) |
Mathias Agopian | eaf2d0b | 2011-06-13 16:00:49 -0700 | [diff] [blame] | 54 | |
| 55 | /* |
| 56 | * Standard deviations of accelerometer and magnetometer |
| 57 | */ |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 58 | static const float DEFAULT_ACC_STDEV = 0.015f; // m/s^2 (measured 0.08 / CDD 0.05) |
| 59 | static const float DEFAULT_MAG_STDEV = 0.1f; // uT (measured 0.7 / CDD 0.5) |
| 60 | static const float GEOMAG_ACC_STDEV = 0.05f; // m/s^2 (measured 0.08 / CDD 0.05) |
| 61 | static const float GEOMAG_MAG_STDEV = 0.1f; // uT (measured 0.7 / CDD 0.5) |
| 62 | |
| 63 | |
| 64 | /* ====================== END FUSION SENSOR PARAMETER ========================*/ |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 65 | |
Max Braun | a01b4e2 | 2011-08-17 18:22:52 -0700 | [diff] [blame] | 66 | static const float SYMMETRY_TOLERANCE = 1e-10f; |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 67 | |
Michael Johnson | 3e87d8d | 2011-08-19 11:47:08 -0700 | [diff] [blame] | 68 | /* |
Mathias Agopian | a83f45c | 2011-08-24 18:40:33 -0700 | [diff] [blame] | 69 | * Accelerometer updates will not be performed near free fall to avoid |
| 70 | * ill-conditioning and div by zeros. |
Michael Johnson | 3e87d8d | 2011-08-19 11:47:08 -0700 | [diff] [blame] | 71 | * Threshhold: 10% of g, in m/s^2 |
| 72 | */ |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 73 | static const float NOMINAL_GRAVITY = 9.81f; |
| 74 | static const float FREE_FALL_THRESHOLD = 0.1f * (NOMINAL_GRAVITY); |
Michael Johnson | 3e87d8d | 2011-08-19 11:47:08 -0700 | [diff] [blame] | 75 | |
| 76 | /* |
| 77 | * The geomagnetic-field should be between 30uT and 60uT. |
Mathias Agopian | a83f45c | 2011-08-24 18:40:33 -0700 | [diff] [blame] | 78 | * Fields strengths greater than this likely indicate a local magnetic |
| 79 | * disturbance which we do not want to update into the fused frame. |
Michael Johnson | 3e87d8d | 2011-08-19 11:47:08 -0700 | [diff] [blame] | 80 | */ |
| 81 | static const float MAX_VALID_MAGNETIC_FIELD = 100; // uT |
Mathias Agopian | a83f45c | 2011-08-24 18:40:33 -0700 | [diff] [blame] | 82 | static const float MAX_VALID_MAGNETIC_FIELD_SQ = |
| 83 | MAX_VALID_MAGNETIC_FIELD*MAX_VALID_MAGNETIC_FIELD; |
Michael Johnson | 3e87d8d | 2011-08-19 11:47:08 -0700 | [diff] [blame] | 84 | |
| 85 | /* |
Mathias Agopian | a83f45c | 2011-08-24 18:40:33 -0700 | [diff] [blame] | 86 | * Values of the field smaller than this should be ignored in fusion to avoid |
| 87 | * ill-conditioning. This state can happen with anomalous local magnetic |
| 88 | * disturbances canceling the Earth field. |
Michael Johnson | 3e87d8d | 2011-08-19 11:47:08 -0700 | [diff] [blame] | 89 | */ |
| 90 | static const float MIN_VALID_MAGNETIC_FIELD = 10; // uT |
Mathias Agopian | a83f45c | 2011-08-24 18:40:33 -0700 | [diff] [blame] | 91 | static const float MIN_VALID_MAGNETIC_FIELD_SQ = |
| 92 | MIN_VALID_MAGNETIC_FIELD*MIN_VALID_MAGNETIC_FIELD; |
Michael Johnson | 3e87d8d | 2011-08-19 11:47:08 -0700 | [diff] [blame] | 93 | |
| 94 | /* |
Mathias Agopian | a83f45c | 2011-08-24 18:40:33 -0700 | [diff] [blame] | 95 | * If the cross product of two vectors has magnitude squared less than this, |
| 96 | * we reject it as invalid due to alignment of the vectors. |
| 97 | * This threshold is used to check for the case where the magnetic field sample |
| 98 | * is parallel to the gravity field, which can happen in certain places due |
| 99 | * to magnetic field disturbances. |
Michael Johnson | 3e87d8d | 2011-08-19 11:47:08 -0700 | [diff] [blame] | 100 | */ |
| 101 | static const float MIN_VALID_CROSS_PRODUCT_MAG = 1.0e-3; |
| 102 | static const float MIN_VALID_CROSS_PRODUCT_MAG_SQ = |
| 103 | MIN_VALID_CROSS_PRODUCT_MAG*MIN_VALID_CROSS_PRODUCT_MAG; |
| 104 | |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 105 | static const float SQRT_3 = 1.732f; |
| 106 | static const float WVEC_EPS = 1e-4f/SQRT_3; |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 107 | // ----------------------------------------------------------------------- |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 108 | |
| 109 | template <typename TYPE, size_t C, size_t R> |
| 110 | static mat<TYPE, R, R> scaleCovariance( |
| 111 | const mat<TYPE, C, R>& A, |
| 112 | const mat<TYPE, C, C>& P) { |
| 113 | // A*P*transpose(A); |
| 114 | mat<TYPE, R, R> APAt; |
| 115 | for (size_t r=0 ; r<R ; r++) { |
| 116 | for (size_t j=r ; j<R ; j++) { |
| 117 | double apat(0); |
| 118 | for (size_t c=0 ; c<C ; c++) { |
| 119 | double v(A[c][r]*P[c][c]*0.5); |
| 120 | for (size_t k=c+1 ; k<C ; k++) |
| 121 | v += A[k][r] * P[c][k]; |
| 122 | apat += 2 * v * A[c][j]; |
| 123 | } |
| 124 | APAt[j][r] = apat; |
| 125 | APAt[r][j] = apat; |
| 126 | } |
| 127 | } |
| 128 | return APAt; |
| 129 | } |
| 130 | |
| 131 | template <typename TYPE, typename OTHER_TYPE> |
| 132 | static mat<TYPE, 3, 3> crossMatrix(const vec<TYPE, 3>& p, OTHER_TYPE diag) { |
| 133 | mat<TYPE, 3, 3> r; |
| 134 | r[0][0] = diag; |
| 135 | r[1][1] = diag; |
| 136 | r[2][2] = diag; |
| 137 | r[0][1] = p.z; |
| 138 | r[1][0] =-p.z; |
| 139 | r[0][2] =-p.y; |
| 140 | r[2][0] = p.y; |
| 141 | r[1][2] = p.x; |
| 142 | r[2][1] =-p.x; |
| 143 | return r; |
| 144 | } |
| 145 | |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 146 | |
| 147 | template<typename TYPE, size_t SIZE> |
| 148 | class Covariance { |
| 149 | mat<TYPE, SIZE, SIZE> mSumXX; |
| 150 | vec<TYPE, SIZE> mSumX; |
| 151 | size_t mN; |
| 152 | public: |
| 153 | Covariance() : mSumXX(0.0f), mSumX(0.0f), mN(0) { } |
| 154 | void update(const vec<TYPE, SIZE>& x) { |
| 155 | mSumXX += x*transpose(x); |
| 156 | mSumX += x; |
| 157 | mN++; |
| 158 | } |
| 159 | mat<TYPE, SIZE, SIZE> operator()() const { |
| 160 | const float N = 1.0f / mN; |
| 161 | return mSumXX*N - (mSumX*transpose(mSumX))*(N*N); |
| 162 | } |
| 163 | void reset() { |
| 164 | mN = 0; |
| 165 | mSumXX = 0; |
| 166 | mSumX = 0; |
| 167 | } |
| 168 | size_t getCount() const { |
| 169 | return mN; |
| 170 | } |
| 171 | }; |
| 172 | |
| 173 | // ----------------------------------------------------------------------- |
| 174 | |
| 175 | Fusion::Fusion() { |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 176 | Phi[0][1] = 0; |
| 177 | Phi[1][1] = 1; |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 178 | |
| 179 | Ba.x = 0; |
| 180 | Ba.y = 0; |
| 181 | Ba.z = 1; |
| 182 | |
| 183 | Bm.x = 0; |
| 184 | Bm.y = 1; |
| 185 | Bm.z = 0; |
| 186 | |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 187 | x0 = 0; |
| 188 | x1 = 0; |
| 189 | |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 190 | init(); |
| 191 | } |
| 192 | |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 193 | void Fusion::init(int mode) { |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 194 | mInitState = 0; |
Max Braun | a01b4e2 | 2011-08-17 18:22:52 -0700 | [diff] [blame] | 195 | |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 196 | mGyroRate = 0; |
Max Braun | a01b4e2 | 2011-08-17 18:22:52 -0700 | [diff] [blame] | 197 | |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 198 | mCount[0] = 0; |
| 199 | mCount[1] = 0; |
| 200 | mCount[2] = 0; |
Max Braun | a01b4e2 | 2011-08-17 18:22:52 -0700 | [diff] [blame] | 201 | |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 202 | mData = 0; |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 203 | mMode = mode; |
| 204 | |
| 205 | if (mMode != FUSION_NOGYRO) { //normal or game rotation |
| 206 | mParam.gyroVar = DEFAULT_GYRO_VAR; |
| 207 | mParam.gyroBiasVar = DEFAULT_GYRO_BIAS_VAR; |
| 208 | mParam.accStdev = DEFAULT_ACC_STDEV; |
| 209 | mParam.magStdev = DEFAULT_MAG_STDEV; |
| 210 | } else { |
| 211 | mParam.gyroVar = GEOMAG_GYRO_VAR; |
| 212 | mParam.gyroBiasVar = GEOMAG_GYRO_BIAS_VAR; |
| 213 | mParam.accStdev = GEOMAG_ACC_STDEV; |
| 214 | mParam.magStdev = GEOMAG_MAG_STDEV; |
| 215 | } |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 218 | void Fusion::initFusion(const vec4_t& q, float dT) |
| 219 | { |
| 220 | // initial estimate: E{ x(t0) } |
| 221 | x0 = q; |
| 222 | x1 = 0; |
| 223 | |
Mathias Agopian | eaf2d0b | 2011-06-13 16:00:49 -0700 | [diff] [blame] | 224 | // process noise covariance matrix: G.Q.Gt, with |
| 225 | // |
| 226 | // G = | -1 0 | Q = | q00 q10 | |
| 227 | // | 0 1 | | q01 q11 | |
| 228 | // |
| 229 | // q00 = sv^2.dt + 1/3.su^2.dt^3 |
| 230 | // q10 = q01 = 1/2.su^2.dt^2 |
| 231 | // q11 = su^2.dt |
| 232 | // |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 233 | |
Mathias Agopian | dc5b63e | 2012-06-18 18:49:08 -0700 | [diff] [blame] | 234 | const float dT2 = dT*dT; |
| 235 | const float dT3 = dT2*dT; |
| 236 | |
| 237 | // variance of integrated output at 1/dT Hz (random drift) |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 238 | const float q00 = mParam.gyroVar * dT + 0.33333f * mParam.gyroBiasVar * dT3; |
Mathias Agopian | eaf2d0b | 2011-06-13 16:00:49 -0700 | [diff] [blame] | 239 | |
| 240 | // variance of drift rate ramp |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 241 | const float q11 = mParam.gyroBiasVar * dT; |
| 242 | const float q10 = 0.5f * mParam.gyroBiasVar * dT2; |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 243 | const float q01 = q10; |
Mathias Agopian | eaf2d0b | 2011-06-13 16:00:49 -0700 | [diff] [blame] | 244 | |
| 245 | GQGt[0][0] = q00; // rad^2 |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 246 | GQGt[1][0] = -q10; |
| 247 | GQGt[0][1] = -q01; |
Mathias Agopian | eaf2d0b | 2011-06-13 16:00:49 -0700 | [diff] [blame] | 248 | GQGt[1][1] = q11; // (rad/s)^2 |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 249 | |
| 250 | // initial covariance: Var{ x(t0) } |
Mathias Agopian | eaf2d0b | 2011-06-13 16:00:49 -0700 | [diff] [blame] | 251 | // TODO: initialize P correctly |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 252 | P = 0; |
| 253 | } |
| 254 | |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 255 | bool Fusion::hasEstimate() const { |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 256 | return ((mInitState & MAG) || (mMode == FUSION_NOMAG)) && |
| 257 | ((mInitState & GYRO) || (mMode == FUSION_NOGYRO)) && |
| 258 | (mInitState & ACC); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 261 | bool Fusion::checkInitComplete(int what, const vec3_t& d, float dT) { |
| 262 | if (hasEstimate()) |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 263 | return true; |
| 264 | |
| 265 | if (what == ACC) { |
| 266 | mData[0] += d * (1/length(d)); |
| 267 | mCount[0]++; |
| 268 | mInitState |= ACC; |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 269 | if (mMode == FUSION_NOGYRO ) { |
| 270 | mGyroRate = dT; |
| 271 | } |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 272 | } else if (what == MAG) { |
| 273 | mData[1] += d * (1/length(d)); |
| 274 | mCount[1]++; |
| 275 | mInitState |= MAG; |
| 276 | } else if (what == GYRO) { |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 277 | mGyroRate = dT; |
| 278 | mData[2] += d*dT; |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 279 | mCount[2]++; |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 280 | mInitState |= GYRO; |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 283 | if (hasEstimate()) { |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 284 | // Average all the values we collected so far |
| 285 | mData[0] *= 1.0f/mCount[0]; |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 286 | if (mMode != FUSION_NOMAG) { |
| 287 | mData[1] *= 1.0f/mCount[1]; |
| 288 | } |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 289 | mData[2] *= 1.0f/mCount[2]; |
| 290 | |
| 291 | // calculate the MRPs from the data collection, this gives us |
| 292 | // a rough estimate of our initial state |
| 293 | mat33_t R; |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 294 | vec3_t up(mData[0]); |
| 295 | vec3_t east; |
| 296 | |
| 297 | if (mMode != FUSION_NOMAG) { |
| 298 | east = normalize(cross_product(mData[1], up)); |
| 299 | } else { |
| 300 | east = getOrthogonal(up); |
| 301 | } |
| 302 | |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 303 | vec3_t north(cross_product(up, east)); |
| 304 | R << east << north << up; |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 305 | const vec4_t q = matrixToQuat(R); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 306 | |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 307 | initFusion(q, mGyroRate); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | return false; |
| 311 | } |
| 312 | |
| 313 | void Fusion::handleGyro(const vec3_t& w, float dT) { |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 314 | if (!checkInitComplete(GYRO, w, dT)) |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 315 | return; |
| 316 | |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 317 | predict(w, dT); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 320 | status_t Fusion::handleAcc(const vec3_t& a, float dT) { |
| 321 | if (!checkInitComplete(ACC, a, dT)) |
| 322 | return BAD_VALUE; |
| 323 | |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 324 | // ignore acceleration data if we're close to free-fall |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 325 | const float l = length(a); |
| 326 | if (l < FREE_FALL_THRESHOLD) { |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 327 | return BAD_VALUE; |
Michael Johnson | 3e87d8d | 2011-08-19 11:47:08 -0700 | [diff] [blame] | 328 | } |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 329 | |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 330 | const float l_inv = 1.0f/l; |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 331 | |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 332 | if ( mMode == FUSION_NOGYRO ) { |
| 333 | //geo mag |
| 334 | vec3_t w_dummy; |
| 335 | w_dummy = x1; //bias |
| 336 | predict(w_dummy, dT); |
| 337 | } |
| 338 | |
| 339 | if ( mMode == FUSION_NOMAG) { |
| 340 | vec3_t m; |
| 341 | m = getRotationMatrix()*Bm; |
| 342 | update(m, Bm, mParam.magStdev); |
| 343 | } |
| 344 | |
| 345 | vec3_t unityA = a * l_inv; |
| 346 | const float d = sqrtf(fabsf(l- NOMINAL_GRAVITY)); |
| 347 | const float p = l_inv * mParam.accStdev*expf(d); |
| 348 | |
| 349 | update(unityA, Ba, p); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 350 | return NO_ERROR; |
| 351 | } |
| 352 | |
| 353 | status_t Fusion::handleMag(const vec3_t& m) { |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 354 | if (!checkInitComplete(MAG, m)) |
| 355 | return BAD_VALUE; |
| 356 | |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 357 | // the geomagnetic-field should be between 30uT and 60uT |
Michael Johnson | 3e87d8d | 2011-08-19 11:47:08 -0700 | [diff] [blame] | 358 | // reject if too large to avoid spurious magnetic sources |
| 359 | const float magFieldSq = length_squared(m); |
| 360 | if (magFieldSq > MAX_VALID_MAGNETIC_FIELD_SQ) { |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 361 | return BAD_VALUE; |
Michael Johnson | 3e87d8d | 2011-08-19 11:47:08 -0700 | [diff] [blame] | 362 | } else if (magFieldSq < MIN_VALID_MAGNETIC_FIELD_SQ) { |
Mathias Agopian | a83f45c | 2011-08-24 18:40:33 -0700 | [diff] [blame] | 363 | // Also reject if too small since we will get ill-defined (zero mag) |
| 364 | // cross-products below |
Michael Johnson | 3e87d8d | 2011-08-19 11:47:08 -0700 | [diff] [blame] | 365 | return BAD_VALUE; |
| 366 | } |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 367 | |
Mathias Agopian | a83f45c | 2011-08-24 18:40:33 -0700 | [diff] [blame] | 368 | // Orthogonalize the magnetic field to the gravity field, mapping it into |
| 369 | // tangent to Earth. |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 370 | const vec3_t up( getRotationMatrix() * Ba ); |
| 371 | const vec3_t east( cross_product(m, up) ); |
Michael Johnson | 3e87d8d | 2011-08-19 11:47:08 -0700 | [diff] [blame] | 372 | |
Mathias Agopian | a83f45c | 2011-08-24 18:40:33 -0700 | [diff] [blame] | 373 | // If the m and up vectors align, the cross product magnitude will |
| 374 | // approach 0. |
| 375 | // Reject this case as well to avoid div by zero problems and |
| 376 | // ill-conditioning below. |
Michael Johnson | 3e87d8d | 2011-08-19 11:47:08 -0700 | [diff] [blame] | 377 | if (length_squared(east) < MIN_VALID_CROSS_PRODUCT_MAG_SQ) { |
Michael Johnson | 3e87d8d | 2011-08-19 11:47:08 -0700 | [diff] [blame] | 378 | return BAD_VALUE; |
| 379 | } |
| 380 | |
Mathias Agopian | a83f45c | 2011-08-24 18:40:33 -0700 | [diff] [blame] | 381 | // If we have created an orthogonal magnetic field successfully, |
| 382 | // then pass it in as the update. |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 383 | vec3_t north( cross_product(up, east) ); |
| 384 | |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 385 | const float l_inv = 1 / length(north); |
| 386 | north *= l_inv; |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 387 | |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 388 | update(north, Bm, mParam.magStdev*l_inv); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 389 | return NO_ERROR; |
| 390 | } |
| 391 | |
Max Braun | a01b4e2 | 2011-08-17 18:22:52 -0700 | [diff] [blame] | 392 | void Fusion::checkState() { |
| 393 | // P needs to stay positive semidefinite or the fusion diverges. When we |
| 394 | // detect divergence, we reset the fusion. |
| 395 | // TODO(braun): Instead, find the reason for the divergence and fix it. |
| 396 | |
| 397 | if (!isPositiveSemidefinite(P[0][0], SYMMETRY_TOLERANCE) || |
| 398 | !isPositiveSemidefinite(P[1][1], SYMMETRY_TOLERANCE)) { |
Steve Block | 3c20fbe | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 399 | ALOGW("Sensor fusion diverged; resetting state."); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 400 | P = 0; |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 401 | } |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 404 | vec4_t Fusion::getAttitude() const { |
| 405 | return x0; |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | vec3_t Fusion::getBias() const { |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 409 | return x1; |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | mat33_t Fusion::getRotationMatrix() const { |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 413 | return quatToMatrix(x0); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 416 | mat34_t Fusion::getF(const vec4_t& q) { |
| 417 | mat34_t F; |
Mathias Agopian | dc5b63e | 2012-06-18 18:49:08 -0700 | [diff] [blame] | 418 | |
| 419 | // This is used to compute the derivative of q |
| 420 | // F = | [q.xyz]x | |
| 421 | // | -q.xyz | |
| 422 | |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 423 | F[0].x = q.w; F[1].x =-q.z; F[2].x = q.y; |
| 424 | F[0].y = q.z; F[1].y = q.w; F[2].y =-q.x; |
| 425 | F[0].z =-q.y; F[1].z = q.x; F[2].z = q.w; |
| 426 | F[0].w =-q.x; F[1].w =-q.y; F[2].w =-q.z; |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 427 | return F; |
| 428 | } |
| 429 | |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 430 | void Fusion::predict(const vec3_t& w, float dT) { |
| 431 | const vec4_t q = x0; |
| 432 | const vec3_t b = x1; |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 433 | vec3_t we = w - b; |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 434 | |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 435 | if (length(we) < WVEC_EPS) { |
| 436 | we = (we[0]>0.f)?WVEC_EPS:-WVEC_EPS; |
| 437 | } |
Mathias Agopian | bdf2773 | 2012-06-28 18:21:43 -0700 | [diff] [blame] | 438 | // q(k+1) = O(we)*q(k) |
| 439 | // -------------------- |
| 440 | // |
| 441 | // O(w) = | cos(0.5*||w||*dT)*I33 - [psi]x psi | |
| 442 | // | -psi' cos(0.5*||w||*dT) | |
| 443 | // |
| 444 | // psi = sin(0.5*||w||*dT)*w / ||w|| |
| 445 | // |
| 446 | // |
Mathias Agopian | 8f11b24 | 2012-06-27 18:51:43 -0700 | [diff] [blame] | 447 | // P(k+1) = Phi(k)*P(k)*Phi(k)' + G*Q(k)*G' |
Mathias Agopian | bdf2773 | 2012-06-28 18:21:43 -0700 | [diff] [blame] | 448 | // ---------------------------------------- |
Mathias Agopian | 8f11b24 | 2012-06-27 18:51:43 -0700 | [diff] [blame] | 449 | // |
| 450 | // G = | -I33 0 | |
| 451 | // | 0 I33 | |
| 452 | // |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 453 | // Phi = | Phi00 Phi10 | |
| 454 | // | 0 1 | |
Mathias Agopian | 8f11b24 | 2012-06-27 18:51:43 -0700 | [diff] [blame] | 455 | // |
| 456 | // Phi00 = I33 |
| 457 | // - [w]x * sin(||w||*dt)/||w|| |
| 458 | // + [w]x^2 * (1-cos(||w||*dT))/||w||^2 |
| 459 | // |
| 460 | // Phi10 = [w]x * (1 - cos(||w||*dt))/||w||^2 |
| 461 | // - [w]x^2 * (||w||*dT - sin(||w||*dt))/||w||^3 |
| 462 | // - I33*dT |
| 463 | |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 464 | const mat33_t I33(1); |
| 465 | const mat33_t I33dT(dT); |
| 466 | const mat33_t wx(crossMatrix(we, 0)); |
| 467 | const mat33_t wx2(wx*wx); |
| 468 | const float lwedT = length(we)*dT; |
Mathias Agopian | bdf2773 | 2012-06-28 18:21:43 -0700 | [diff] [blame] | 469 | const float hlwedT = 0.5f*lwedT; |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 470 | const float ilwe = 1.f/length(we); |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 471 | const float k0 = (1-cosf(lwedT))*(ilwe*ilwe); |
| 472 | const float k1 = sinf(lwedT); |
Mathias Agopian | bdf2773 | 2012-06-28 18:21:43 -0700 | [diff] [blame] | 473 | const float k2 = cosf(hlwedT); |
| 474 | const vec3_t psi(sinf(hlwedT)*ilwe*we); |
| 475 | const mat33_t O33(crossMatrix(-psi, k2)); |
| 476 | mat44_t O; |
| 477 | O[0].xyz = O33[0]; O[0].w = -psi.x; |
| 478 | O[1].xyz = O33[1]; O[1].w = -psi.y; |
| 479 | O[2].xyz = O33[2]; O[2].w = -psi.z; |
| 480 | O[3].xyz = psi; O[3].w = k2; |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 481 | |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 482 | Phi[0][0] = I33 - wx*(k1*ilwe) + wx2*k0; |
| 483 | Phi[1][0] = wx*k0 - I33dT - wx2*(ilwe*ilwe*ilwe)*(lwedT-k1); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 484 | |
Mathias Agopian | bdf2773 | 2012-06-28 18:21:43 -0700 | [diff] [blame] | 485 | x0 = O*q; |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 486 | |
Mathias Agopian | bdf2773 | 2012-06-28 18:21:43 -0700 | [diff] [blame] | 487 | if (x0.w < 0) |
| 488 | x0 = -x0; |
| 489 | |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 490 | P = Phi*P*transpose(Phi) + GQGt; |
Max Braun | a01b4e2 | 2011-08-17 18:22:52 -0700 | [diff] [blame] | 491 | |
| 492 | checkState(); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | void Fusion::update(const vec3_t& z, const vec3_t& Bi, float sigma) { |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 496 | vec4_t q(x0); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 497 | // measured vector in body space: h(p) = A(p)*Bi |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 498 | const mat33_t A(quatToMatrix(q)); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 499 | const vec3_t Bb(A*Bi); |
| 500 | |
| 501 | // Sensitivity matrix H = dh(p)/dp |
| 502 | // H = [ L 0 ] |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 503 | const mat33_t L(crossMatrix(Bb, 0)); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 504 | |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 505 | // gain... |
| 506 | // K = P*Ht / [H*P*Ht + R] |
| 507 | vec<mat33_t, 2> K; |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 508 | const mat33_t R(sigma*sigma); |
| 509 | const mat33_t S(scaleCovariance(L, P[0][0]) + R); |
| 510 | const mat33_t Si(invert(S)); |
| 511 | const mat33_t LtSi(transpose(L)*Si); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 512 | K[0] = P[0][0] * LtSi; |
| 513 | K[1] = transpose(P[1][0])*LtSi; |
| 514 | |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 515 | // update... |
Mathias Agopian | dc5b63e | 2012-06-18 18:49:08 -0700 | [diff] [blame] | 516 | // P = (I-K*H) * P |
| 517 | // P -= K*H*P |
| 518 | // | K0 | * | L 0 | * P = | K0*L 0 | * | P00 P10 | = | K0*L*P00 K0*L*P10 | |
| 519 | // | K1 | | K1*L 0 | | P01 P11 | | K1*L*P00 K1*L*P10 | |
| 520 | // Note: the Joseph form is numerically more stable and given by: |
| 521 | // P = (I-KH) * P * (I-KH)' + K*R*R' |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 522 | const mat33_t K0L(K[0] * L); |
| 523 | const mat33_t K1L(K[1] * L); |
| 524 | P[0][0] -= K0L*P[0][0]; |
| 525 | P[1][1] -= K1L*P[1][0]; |
| 526 | P[1][0] -= K0L*P[1][0]; |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 527 | P[0][1] = transpose(P[1][0]); |
| 528 | |
| 529 | const vec3_t e(z - Bb); |
| 530 | const vec3_t dq(K[0]*e); |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 531 | |
| 532 | q += getF(q)*(0.5f*dq); |
| 533 | x0 = normalize_quat(q); |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 534 | |
| 535 | if (mMode != FUSION_NOMAG) { |
| 536 | const vec3_t db(K[1]*e); |
| 537 | x1 += db; |
| 538 | } |
Max Braun | a01b4e2 | 2011-08-17 18:22:52 -0700 | [diff] [blame] | 539 | |
| 540 | checkState(); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 541 | } |
| 542 | |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 543 | vec3_t Fusion::getOrthogonal(const vec3_t &v) { |
| 544 | vec3_t w; |
| 545 | if (fabsf(v[0])<= fabsf(v[1]) && fabsf(v[0]) <= fabsf(v[2])) { |
| 546 | w[0]=0.f; |
| 547 | w[1] = v[2]; |
| 548 | w[2] = -v[1]; |
| 549 | } else if (fabsf(v[1]) <= fabsf(v[2])) { |
| 550 | w[0] = v[2]; |
| 551 | w[1] = 0.f; |
| 552 | w[2] = -v[0]; |
| 553 | }else { |
| 554 | w[0] = v[1]; |
| 555 | w[1] = -v[0]; |
| 556 | w[2] = 0.f; |
| 557 | } |
| 558 | return normalize(w); |
| 559 | } |
| 560 | |
| 561 | |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 562 | // ----------------------------------------------------------------------- |
| 563 | |
| 564 | }; // namespace android |
| 565 | |