blob: e92b23dab41cbf4f5f52e9057e85c21bd6f248ef [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
Peng Xuf66684a2015-07-23 11:41:53 -070027/*==================== 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 Agopianeaf2d0b2011-06-13 16:00:49 -070037/*
Peng Xuf66684a2015-07-23 11:41:53 -070038 * GYRO_VAR gives the measured variance of the gyro's output per
Mathias Agopianeaf2d0b2011-06-13 16:00:49 -070039 * 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 Xuf66684a2015-07-23 11:41:53 -070044 * variance(T) = GYRO_VAR / T
Mathias Agopianeaf2d0b2011-06-13 16:00:49 -070045 *
46 * The variance of the INTEGRATED OUTPUT at a given sampling period can be
47 * calculated as:
Peng Xuf66684a2015-07-23 11:41:53 -070048 * variance_integrate_output(T) = GYRO_VAR * T
Mathias Agopianeaf2d0b2011-06-13 16:00:49 -070049 */
Peng Xuf66684a2015-07-23 11:41:53 -070050static const float DEFAULT_GYRO_VAR = 1e-7; // (rad/s)^2 / Hz
51static const float DEFAULT_GYRO_BIAS_VAR = 1e-12; // (rad/s)^2 / s (guessed)
52static const float GEOMAG_GYRO_VAR = 1e-4; // (rad/s)^2 / Hz
53static const float GEOMAG_GYRO_BIAS_VAR = 1e-8; // (rad/s)^2 / s (guessed)
Mathias Agopianeaf2d0b2011-06-13 16:00:49 -070054
55/*
56 * Standard deviations of accelerometer and magnetometer
57 */
Peng Xuf66684a2015-07-23 11:41:53 -070058static const float DEFAULT_ACC_STDEV = 0.015f; // m/s^2 (measured 0.08 / CDD 0.05)
59static const float DEFAULT_MAG_STDEV = 0.1f; // uT (measured 0.7 / CDD 0.5)
60static const float GEOMAG_ACC_STDEV = 0.05f; // m/s^2 (measured 0.08 / CDD 0.05)
61static const float GEOMAG_MAG_STDEV = 0.1f; // uT (measured 0.7 / CDD 0.5)
62
63
64/* ====================== END FUSION SENSOR PARAMETER ========================*/
Mathias Agopian984826c2011-05-17 22:54:42 -070065
Max Brauna01b4e22011-08-17 18:22:52 -070066static const float SYMMETRY_TOLERANCE = 1e-10f;
Mathias Agopian33015422011-05-27 18:18:13 -070067
Michael Johnson3e87d8d2011-08-19 11:47:08 -070068/*
Mathias Agopiana83f45c2011-08-24 18:40:33 -070069 * Accelerometer updates will not be performed near free fall to avoid
70 * ill-conditioning and div by zeros.
Michael Johnson3e87d8d2011-08-19 11:47:08 -070071 * Threshhold: 10% of g, in m/s^2
72 */
Peng Xuf66684a2015-07-23 11:41:53 -070073static const float NOMINAL_GRAVITY = 9.81f;
74static const float FREE_FALL_THRESHOLD = 0.1f * (NOMINAL_GRAVITY);
Michael Johnson3e87d8d2011-08-19 11:47:08 -070075
76/*
77 * The geomagnetic-field should be between 30uT and 60uT.
Mathias Agopiana83f45c2011-08-24 18:40:33 -070078 * Fields strengths greater than this likely indicate a local magnetic
79 * disturbance which we do not want to update into the fused frame.
Michael Johnson3e87d8d2011-08-19 11:47:08 -070080 */
81static const float MAX_VALID_MAGNETIC_FIELD = 100; // uT
Mathias Agopiana83f45c2011-08-24 18:40:33 -070082static const float MAX_VALID_MAGNETIC_FIELD_SQ =
83 MAX_VALID_MAGNETIC_FIELD*MAX_VALID_MAGNETIC_FIELD;
Michael Johnson3e87d8d2011-08-19 11:47:08 -070084
85/*
Mathias Agopiana83f45c2011-08-24 18:40:33 -070086 * 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 Johnson3e87d8d2011-08-19 11:47:08 -070089 */
90static const float MIN_VALID_MAGNETIC_FIELD = 10; // uT
Mathias Agopiana83f45c2011-08-24 18:40:33 -070091static const float MIN_VALID_MAGNETIC_FIELD_SQ =
92 MIN_VALID_MAGNETIC_FIELD*MIN_VALID_MAGNETIC_FIELD;
Michael Johnson3e87d8d2011-08-19 11:47:08 -070093
94/*
Mathias Agopiana83f45c2011-08-24 18:40:33 -070095 * 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 Johnson3e87d8d2011-08-19 11:47:08 -0700100 */
101static const float MIN_VALID_CROSS_PRODUCT_MAG = 1.0e-3;
102static const float MIN_VALID_CROSS_PRODUCT_MAG_SQ =
103 MIN_VALID_CROSS_PRODUCT_MAG*MIN_VALID_CROSS_PRODUCT_MAG;
104
Peng Xuf66684a2015-07-23 11:41:53 -0700105static const float SQRT_3 = 1.732f;
106static const float WVEC_EPS = 1e-4f/SQRT_3;
Mathias Agopian33015422011-05-27 18:18:13 -0700107// -----------------------------------------------------------------------
Mathias Agopian984826c2011-05-17 22:54:42 -0700108
109template <typename TYPE, size_t C, size_t R>
110static 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
131template <typename TYPE, typename OTHER_TYPE>
132static 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 Agopian984826c2011-05-17 22:54:42 -0700146
147template<typename TYPE, size_t SIZE>
148class Covariance {
149 mat<TYPE, SIZE, SIZE> mSumXX;
150 vec<TYPE, SIZE> mSumX;
151 size_t mN;
152public:
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
175Fusion::Fusion() {
Mathias Agopian33015422011-05-27 18:18:13 -0700176 Phi[0][1] = 0;
177 Phi[1][1] = 1;
Mathias Agopian984826c2011-05-17 22:54:42 -0700178
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 Agopian667102f2011-09-14 16:43:34 -0700187 x0 = 0;
188 x1 = 0;
189
Mathias Agopian984826c2011-05-17 22:54:42 -0700190 init();
191}
192
Peng Xuf66684a2015-07-23 11:41:53 -0700193void Fusion::init(int mode) {
Mathias Agopian984826c2011-05-17 22:54:42 -0700194 mInitState = 0;
Max Brauna01b4e22011-08-17 18:22:52 -0700195
Mathias Agopian33015422011-05-27 18:18:13 -0700196 mGyroRate = 0;
Max Brauna01b4e22011-08-17 18:22:52 -0700197
Mathias Agopian984826c2011-05-17 22:54:42 -0700198 mCount[0] = 0;
199 mCount[1] = 0;
200 mCount[2] = 0;
Max Brauna01b4e22011-08-17 18:22:52 -0700201
Mathias Agopian984826c2011-05-17 22:54:42 -0700202 mData = 0;
Peng Xuf66684a2015-07-23 11:41:53 -0700203 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 Agopian984826c2011-05-17 22:54:42 -0700216}
217
Mathias Agopian33015422011-05-27 18:18:13 -0700218void Fusion::initFusion(const vec4_t& q, float dT)
219{
220 // initial estimate: E{ x(t0) }
221 x0 = q;
222 x1 = 0;
223
Mathias Agopianeaf2d0b2011-06-13 16:00:49 -0700224 // 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 Agopian33015422011-05-27 18:18:13 -0700233
Mathias Agopiandc5b63e2012-06-18 18:49:08 -0700234 const float dT2 = dT*dT;
235 const float dT3 = dT2*dT;
236
237 // variance of integrated output at 1/dT Hz (random drift)
Peng Xuf66684a2015-07-23 11:41:53 -0700238 const float q00 = mParam.gyroVar * dT + 0.33333f * mParam.gyroBiasVar * dT3;
Mathias Agopianeaf2d0b2011-06-13 16:00:49 -0700239
240 // variance of drift rate ramp
Peng Xuf66684a2015-07-23 11:41:53 -0700241 const float q11 = mParam.gyroBiasVar * dT;
242 const float q10 = 0.5f * mParam.gyroBiasVar * dT2;
Mathias Agopian33015422011-05-27 18:18:13 -0700243 const float q01 = q10;
Mathias Agopianeaf2d0b2011-06-13 16:00:49 -0700244
245 GQGt[0][0] = q00; // rad^2
Mathias Agopian33015422011-05-27 18:18:13 -0700246 GQGt[1][0] = -q10;
247 GQGt[0][1] = -q01;
Mathias Agopianeaf2d0b2011-06-13 16:00:49 -0700248 GQGt[1][1] = q11; // (rad/s)^2
Mathias Agopian33015422011-05-27 18:18:13 -0700249
250 // initial covariance: Var{ x(t0) }
Mathias Agopianeaf2d0b2011-06-13 16:00:49 -0700251 // TODO: initialize P correctly
Mathias Agopian33015422011-05-27 18:18:13 -0700252 P = 0;
253}
254
Mathias Agopian984826c2011-05-17 22:54:42 -0700255bool Fusion::hasEstimate() const {
Peng Xuf66684a2015-07-23 11:41:53 -0700256 return ((mInitState & MAG) || (mMode == FUSION_NOMAG)) &&
257 ((mInitState & GYRO) || (mMode == FUSION_NOGYRO)) &&
258 (mInitState & ACC);
Mathias Agopian984826c2011-05-17 22:54:42 -0700259}
260
Mathias Agopian33015422011-05-27 18:18:13 -0700261bool Fusion::checkInitComplete(int what, const vec3_t& d, float dT) {
262 if (hasEstimate())
Mathias Agopian984826c2011-05-17 22:54:42 -0700263 return true;
264
265 if (what == ACC) {
266 mData[0] += d * (1/length(d));
267 mCount[0]++;
268 mInitState |= ACC;
Peng Xuf66684a2015-07-23 11:41:53 -0700269 if (mMode == FUSION_NOGYRO ) {
270 mGyroRate = dT;
271 }
Mathias Agopian984826c2011-05-17 22:54:42 -0700272 } else if (what == MAG) {
273 mData[1] += d * (1/length(d));
274 mCount[1]++;
275 mInitState |= MAG;
276 } else if (what == GYRO) {
Mathias Agopian33015422011-05-27 18:18:13 -0700277 mGyroRate = dT;
278 mData[2] += d*dT;
Mathias Agopian984826c2011-05-17 22:54:42 -0700279 mCount[2]++;
Peng Xuf66684a2015-07-23 11:41:53 -0700280 mInitState |= GYRO;
Mathias Agopian984826c2011-05-17 22:54:42 -0700281 }
282
Peng Xuf66684a2015-07-23 11:41:53 -0700283 if (hasEstimate()) {
Mathias Agopian984826c2011-05-17 22:54:42 -0700284 // Average all the values we collected so far
285 mData[0] *= 1.0f/mCount[0];
Peng Xuf66684a2015-07-23 11:41:53 -0700286 if (mMode != FUSION_NOMAG) {
287 mData[1] *= 1.0f/mCount[1];
288 }
Mathias Agopian984826c2011-05-17 22:54:42 -0700289 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 Xuf66684a2015-07-23 11:41:53 -0700294 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 Agopian984826c2011-05-17 22:54:42 -0700303 vec3_t north(cross_product(up, east));
304 R << east << north << up;
Mathias Agopian33015422011-05-27 18:18:13 -0700305 const vec4_t q = matrixToQuat(R);
Mathias Agopian984826c2011-05-17 22:54:42 -0700306
Mathias Agopian33015422011-05-27 18:18:13 -0700307 initFusion(q, mGyroRate);
Mathias Agopian984826c2011-05-17 22:54:42 -0700308 }
309
310 return false;
311}
312
313void Fusion::handleGyro(const vec3_t& w, float dT) {
Mathias Agopian33015422011-05-27 18:18:13 -0700314 if (!checkInitComplete(GYRO, w, dT))
Mathias Agopian984826c2011-05-17 22:54:42 -0700315 return;
316
Mathias Agopian33015422011-05-27 18:18:13 -0700317 predict(w, dT);
Mathias Agopian984826c2011-05-17 22:54:42 -0700318}
319
Peng Xuf66684a2015-07-23 11:41:53 -0700320status_t Fusion::handleAcc(const vec3_t& a, float dT) {
321 if (!checkInitComplete(ACC, a, dT))
322 return BAD_VALUE;
323
Mathias Agopian33015422011-05-27 18:18:13 -0700324 // ignore acceleration data if we're close to free-fall
Peng Xuf66684a2015-07-23 11:41:53 -0700325 const float l = length(a);
326 if (l < FREE_FALL_THRESHOLD) {
Mathias Agopian984826c2011-05-17 22:54:42 -0700327 return BAD_VALUE;
Michael Johnson3e87d8d2011-08-19 11:47:08 -0700328 }
Mathias Agopian984826c2011-05-17 22:54:42 -0700329
Peng Xuf66684a2015-07-23 11:41:53 -0700330 const float l_inv = 1.0f/l;
Mathias Agopian984826c2011-05-17 22:54:42 -0700331
Peng Xuf66684a2015-07-23 11:41:53 -0700332 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 Agopian984826c2011-05-17 22:54:42 -0700350 return NO_ERROR;
351}
352
353status_t Fusion::handleMag(const vec3_t& m) {
Peng Xuf66684a2015-07-23 11:41:53 -0700354 if (!checkInitComplete(MAG, m))
355 return BAD_VALUE;
356
Mathias Agopian984826c2011-05-17 22:54:42 -0700357 // the geomagnetic-field should be between 30uT and 60uT
Michael Johnson3e87d8d2011-08-19 11:47:08 -0700358 // 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 Agopian984826c2011-05-17 22:54:42 -0700361 return BAD_VALUE;
Michael Johnson3e87d8d2011-08-19 11:47:08 -0700362 } else if (magFieldSq < MIN_VALID_MAGNETIC_FIELD_SQ) {
Mathias Agopiana83f45c2011-08-24 18:40:33 -0700363 // Also reject if too small since we will get ill-defined (zero mag)
364 // cross-products below
Michael Johnson3e87d8d2011-08-19 11:47:08 -0700365 return BAD_VALUE;
366 }
Mathias Agopian984826c2011-05-17 22:54:42 -0700367
Mathias Agopiana83f45c2011-08-24 18:40:33 -0700368 // Orthogonalize the magnetic field to the gravity field, mapping it into
369 // tangent to Earth.
Mathias Agopian984826c2011-05-17 22:54:42 -0700370 const vec3_t up( getRotationMatrix() * Ba );
371 const vec3_t east( cross_product(m, up) );
Michael Johnson3e87d8d2011-08-19 11:47:08 -0700372
Mathias Agopiana83f45c2011-08-24 18:40:33 -0700373 // 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 Johnson3e87d8d2011-08-19 11:47:08 -0700377 if (length_squared(east) < MIN_VALID_CROSS_PRODUCT_MAG_SQ) {
Michael Johnson3e87d8d2011-08-19 11:47:08 -0700378 return BAD_VALUE;
379 }
380
Mathias Agopiana83f45c2011-08-24 18:40:33 -0700381 // If we have created an orthogonal magnetic field successfully,
382 // then pass it in as the update.
Mathias Agopian984826c2011-05-17 22:54:42 -0700383 vec3_t north( cross_product(up, east) );
384
Peng Xuf66684a2015-07-23 11:41:53 -0700385 const float l_inv = 1 / length(north);
386 north *= l_inv;
Mathias Agopian984826c2011-05-17 22:54:42 -0700387
Peng Xuf66684a2015-07-23 11:41:53 -0700388 update(north, Bm, mParam.magStdev*l_inv);
Mathias Agopian984826c2011-05-17 22:54:42 -0700389 return NO_ERROR;
390}
391
Max Brauna01b4e22011-08-17 18:22:52 -0700392void 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 Block3c20fbe2012-01-05 23:22:43 +0000399 ALOGW("Sensor fusion diverged; resetting state.");
Mathias Agopian984826c2011-05-17 22:54:42 -0700400 P = 0;
Mathias Agopian984826c2011-05-17 22:54:42 -0700401 }
Mathias Agopian984826c2011-05-17 22:54:42 -0700402}
403
Mathias Agopian33015422011-05-27 18:18:13 -0700404vec4_t Fusion::getAttitude() const {
405 return x0;
Mathias Agopian984826c2011-05-17 22:54:42 -0700406}
407
408vec3_t Fusion::getBias() const {
Mathias Agopian33015422011-05-27 18:18:13 -0700409 return x1;
Mathias Agopian984826c2011-05-17 22:54:42 -0700410}
411
412mat33_t Fusion::getRotationMatrix() const {
Mathias Agopian33015422011-05-27 18:18:13 -0700413 return quatToMatrix(x0);
Mathias Agopian984826c2011-05-17 22:54:42 -0700414}
415
Mathias Agopian33015422011-05-27 18:18:13 -0700416mat34_t Fusion::getF(const vec4_t& q) {
417 mat34_t F;
Mathias Agopiandc5b63e2012-06-18 18:49:08 -0700418
419 // This is used to compute the derivative of q
420 // F = | [q.xyz]x |
421 // | -q.xyz |
422
Mathias Agopian33015422011-05-27 18:18:13 -0700423 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 Agopian984826c2011-05-17 22:54:42 -0700427 return F;
428}
429
Mathias Agopian33015422011-05-27 18:18:13 -0700430void Fusion::predict(const vec3_t& w, float dT) {
431 const vec4_t q = x0;
432 const vec3_t b = x1;
Peng Xuf66684a2015-07-23 11:41:53 -0700433 vec3_t we = w - b;
Mathias Agopian984826c2011-05-17 22:54:42 -0700434
Peng Xuf66684a2015-07-23 11:41:53 -0700435 if (length(we) < WVEC_EPS) {
436 we = (we[0]>0.f)?WVEC_EPS:-WVEC_EPS;
437 }
Mathias Agopianbdf27732012-06-28 18:21:43 -0700438 // 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 Agopian8f11b242012-06-27 18:51:43 -0700447 // P(k+1) = Phi(k)*P(k)*Phi(k)' + G*Q(k)*G'
Mathias Agopianbdf27732012-06-28 18:21:43 -0700448 // ----------------------------------------
Mathias Agopian8f11b242012-06-27 18:51:43 -0700449 //
450 // G = | -I33 0 |
451 // | 0 I33 |
452 //
Mathias Agopian33015422011-05-27 18:18:13 -0700453 // Phi = | Phi00 Phi10 |
454 // | 0 1 |
Mathias Agopian8f11b242012-06-27 18:51:43 -0700455 //
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 Agopian33015422011-05-27 18:18:13 -0700464 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 Agopianbdf27732012-06-28 18:21:43 -0700469 const float hlwedT = 0.5f*lwedT;
Peng Xuf66684a2015-07-23 11:41:53 -0700470 const float ilwe = 1.f/length(we);
Mathias Agopian33015422011-05-27 18:18:13 -0700471 const float k0 = (1-cosf(lwedT))*(ilwe*ilwe);
472 const float k1 = sinf(lwedT);
Mathias Agopianbdf27732012-06-28 18:21:43 -0700473 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 Agopian984826c2011-05-17 22:54:42 -0700481
Mathias Agopian33015422011-05-27 18:18:13 -0700482 Phi[0][0] = I33 - wx*(k1*ilwe) + wx2*k0;
483 Phi[1][0] = wx*k0 - I33dT - wx2*(ilwe*ilwe*ilwe)*(lwedT-k1);
Mathias Agopian984826c2011-05-17 22:54:42 -0700484
Mathias Agopianbdf27732012-06-28 18:21:43 -0700485 x0 = O*q;
Peng Xuf66684a2015-07-23 11:41:53 -0700486
Mathias Agopianbdf27732012-06-28 18:21:43 -0700487 if (x0.w < 0)
488 x0 = -x0;
489
Mathias Agopian33015422011-05-27 18:18:13 -0700490 P = Phi*P*transpose(Phi) + GQGt;
Max Brauna01b4e22011-08-17 18:22:52 -0700491
492 checkState();
Mathias Agopian984826c2011-05-17 22:54:42 -0700493}
494
495void Fusion::update(const vec3_t& z, const vec3_t& Bi, float sigma) {
Mathias Agopian33015422011-05-27 18:18:13 -0700496 vec4_t q(x0);
Mathias Agopian984826c2011-05-17 22:54:42 -0700497 // measured vector in body space: h(p) = A(p)*Bi
Mathias Agopian33015422011-05-27 18:18:13 -0700498 const mat33_t A(quatToMatrix(q));
Mathias Agopian984826c2011-05-17 22:54:42 -0700499 const vec3_t Bb(A*Bi);
500
501 // Sensitivity matrix H = dh(p)/dp
502 // H = [ L 0 ]
Mathias Agopian33015422011-05-27 18:18:13 -0700503 const mat33_t L(crossMatrix(Bb, 0));
Mathias Agopian984826c2011-05-17 22:54:42 -0700504
Mathias Agopian33015422011-05-27 18:18:13 -0700505 // gain...
506 // K = P*Ht / [H*P*Ht + R]
507 vec<mat33_t, 2> K;
Mathias Agopian984826c2011-05-17 22:54:42 -0700508 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 Agopian984826c2011-05-17 22:54:42 -0700512 K[0] = P[0][0] * LtSi;
513 K[1] = transpose(P[1][0])*LtSi;
514
Mathias Agopian33015422011-05-27 18:18:13 -0700515 // update...
Mathias Agopiandc5b63e2012-06-18 18:49:08 -0700516 // 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 Agopian984826c2011-05-17 22:54:42 -0700522 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 Agopian33015422011-05-27 18:18:13 -0700527 P[0][1] = transpose(P[1][0]);
528
529 const vec3_t e(z - Bb);
530 const vec3_t dq(K[0]*e);
Mathias Agopian33015422011-05-27 18:18:13 -0700531
532 q += getF(q)*(0.5f*dq);
533 x0 = normalize_quat(q);
Peng Xuf66684a2015-07-23 11:41:53 -0700534
535 if (mMode != FUSION_NOMAG) {
536 const vec3_t db(K[1]*e);
537 x1 += db;
538 }
Max Brauna01b4e22011-08-17 18:22:52 -0700539
540 checkState();
Mathias Agopian984826c2011-05-17 22:54:42 -0700541}
542
Peng Xuf66684a2015-07-23 11:41:53 -0700543vec3_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 Agopian984826c2011-05-17 22:54:42 -0700562// -----------------------------------------------------------------------
563
564}; // namespace android
565