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