blob: 602779f5390e8b7dc8be3da0a3ce8f78b09cec4c [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#ifndef ANDROID_FUSION_H
18#define ANDROID_FUSION_H
19
20#include <utils/Errors.h>
21
Mathias Agopian33015422011-05-27 18:18:13 -070022#include "quat.h"
Mathias Agopian984826c2011-05-17 22:54:42 -070023#include "mat.h"
Mathias Agopian33015422011-05-27 18:18:13 -070024#include "vec.h"
Mathias Agopian984826c2011-05-17 22:54:42 -070025
26namespace android {
27
Mathias Agopian33015422011-05-27 18:18:13 -070028typedef mat<float, 3, 4> mat34_t;
29
Peng Xuf66684a2015-07-23 11:41:53 -070030enum FUSION_MODE{
31 FUSION_9AXIS, // use accel gyro mag
32 FUSION_NOMAG, // use accel gyro (game rotation, gravity)
33 FUSION_NOGYRO, // use accel mag (geomag rotation)
34 NUM_FUSION_MODE
35};
36
Mathias Agopian984826c2011-05-17 22:54:42 -070037class Fusion {
38 /*
39 * the state vector is made of two sub-vector containing respectively:
40 * - modified Rodrigues parameters
41 * - the estimated gyro bias
42 */
Mathias Agopian33015422011-05-27 18:18:13 -070043 quat_t x0;
44 vec3_t x1;
Mathias Agopian984826c2011-05-17 22:54:42 -070045
46 /*
Max Brauna01b4e22011-08-17 18:22:52 -070047 * the predicated covariance matrix is made of 4 3x3 sub-matrices and it is
Mathias Agopian984826c2011-05-17 22:54:42 -070048 * semi-definite positive.
49 *
50 * P = | P00 P10 | = | P00 P10 |
Mathias Agopian33015422011-05-27 18:18:13 -070051 * | P01 P11 | | P10t P11 |
Mathias Agopian984826c2011-05-17 22:54:42 -070052 *
53 * Since P01 = transpose(P10), the code below never calculates or
Mathias Agopian33015422011-05-27 18:18:13 -070054 * stores P01.
Mathias Agopian984826c2011-05-17 22:54:42 -070055 */
56 mat<mat33_t, 2, 2> P;
57
58 /*
Mathias Agopian33015422011-05-27 18:18:13 -070059 * the process noise covariance matrix
Mathias Agopian984826c2011-05-17 22:54:42 -070060 */
Mathias Agopian33015422011-05-27 18:18:13 -070061 mat<mat33_t, 2, 2> GQGt;
Mathias Agopian984826c2011-05-17 22:54:42 -070062
63public:
64 Fusion();
Peng Xuf66684a2015-07-23 11:41:53 -070065 void init(int mode = FUSION_9AXIS);
Mathias Agopian984826c2011-05-17 22:54:42 -070066 void handleGyro(const vec3_t& w, float dT);
Peng Xuf66684a2015-07-23 11:41:53 -070067 status_t handleAcc(const vec3_t& a, float dT);
Mathias Agopian984826c2011-05-17 22:54:42 -070068 status_t handleMag(const vec3_t& m);
Mathias Agopian33015422011-05-27 18:18:13 -070069 vec4_t getAttitude() const;
Mathias Agopian984826c2011-05-17 22:54:42 -070070 vec3_t getBias() const;
71 mat33_t getRotationMatrix() const;
72 bool hasEstimate() const;
73
74private:
Peng Xuf66684a2015-07-23 11:41:53 -070075 struct Parameter {
76 float gyroVar;
77 float gyroBiasVar;
78 float accStdev;
79 float magStdev;
80 } mParam;
81
Mathias Agopian33015422011-05-27 18:18:13 -070082 mat<mat33_t, 2, 2> Phi;
Mathias Agopian984826c2011-05-17 22:54:42 -070083 vec3_t Ba, Bm;
84 uint32_t mInitState;
Mathias Agopian33015422011-05-27 18:18:13 -070085 float mGyroRate;
Mathias Agopian984826c2011-05-17 22:54:42 -070086 vec<vec3_t, 3> mData;
87 size_t mCount[3];
Peng Xuf66684a2015-07-23 11:41:53 -070088 int mMode;
89
Mathias Agopian984826c2011-05-17 22:54:42 -070090 enum { ACC=0x1, MAG=0x2, GYRO=0x4 };
Mathias Agopian33015422011-05-27 18:18:13 -070091 bool checkInitComplete(int, const vec3_t& w, float d = 0);
92 void initFusion(const vec4_t& q0, float dT);
Max Brauna01b4e22011-08-17 18:22:52 -070093 void checkState();
Mathias Agopian33015422011-05-27 18:18:13 -070094 void predict(const vec3_t& w, float dT);
Mathias Agopian984826c2011-05-17 22:54:42 -070095 void update(const vec3_t& z, const vec3_t& Bi, float sigma);
Mathias Agopian33015422011-05-27 18:18:13 -070096 static mat34_t getF(const vec4_t& p);
Peng Xuf66684a2015-07-23 11:41:53 -070097 static vec3_t getOrthogonal(const vec3_t &v);
Mathias Agopian984826c2011-05-17 22:54:42 -070098};
99
100}; // namespace android
101
102#endif // ANDROID_FUSION_H