Mathias Agopian | 73e0bc8 | 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 | #ifndef ANDROID_FUSION_H |
| 18 | #define ANDROID_FUSION_H |
| 19 | |
| 20 | #include <utils/Errors.h> |
| 21 | |
Mathias Agopian | 6043e53 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 22 | #include "quat.h" |
Mathias Agopian | 73e0bc8 | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 23 | #include "mat.h" |
Mathias Agopian | 6043e53 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 24 | #include "vec.h" |
Mathias Agopian | 73e0bc8 | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | |
Mathias Agopian | 6043e53 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 28 | typedef mat<float, 3, 4> mat34_t; |
| 29 | |
Mathias Agopian | 73e0bc8 | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 30 | class Fusion { |
| 31 | /* |
| 32 | * the state vector is made of two sub-vector containing respectively: |
| 33 | * - modified Rodrigues parameters |
| 34 | * - the estimated gyro bias |
| 35 | */ |
Mathias Agopian | 6043e53 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 36 | quat_t x0; |
| 37 | vec3_t x1; |
Mathias Agopian | 73e0bc8 | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 38 | |
| 39 | /* |
Max Braun | 3d41ecd | 2011-08-17 18:22:52 -0700 | [diff] [blame] | 40 | * the predicated covariance matrix is made of 4 3x3 sub-matrices and it is |
Mathias Agopian | 73e0bc8 | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 41 | * semi-definite positive. |
| 42 | * |
| 43 | * P = | P00 P10 | = | P00 P10 | |
Mathias Agopian | 6043e53 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 44 | * | P01 P11 | | P10t P11 | |
Mathias Agopian | 73e0bc8 | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 45 | * |
| 46 | * Since P01 = transpose(P10), the code below never calculates or |
Mathias Agopian | 6043e53 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 47 | * stores P01. |
Mathias Agopian | 73e0bc8 | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 48 | */ |
| 49 | mat<mat33_t, 2, 2> P; |
| 50 | |
| 51 | /* |
Mathias Agopian | 6043e53 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 52 | * the process noise covariance matrix |
Mathias Agopian | 73e0bc8 | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 53 | */ |
Mathias Agopian | 6043e53 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 54 | mat<mat33_t, 2, 2> GQGt; |
Mathias Agopian | 73e0bc8 | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 55 | |
| 56 | public: |
| 57 | Fusion(); |
| 58 | void init(); |
| 59 | void handleGyro(const vec3_t& w, float dT); |
| 60 | status_t handleAcc(const vec3_t& a); |
| 61 | status_t handleMag(const vec3_t& m); |
Mathias Agopian | 6043e53 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 62 | vec4_t getAttitude() const; |
Mathias Agopian | 73e0bc8 | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 63 | vec3_t getBias() const; |
| 64 | mat33_t getRotationMatrix() const; |
| 65 | bool hasEstimate() const; |
| 66 | |
| 67 | private: |
Mathias Agopian | 6043e53 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 68 | mat<mat33_t, 2, 2> Phi; |
Mathias Agopian | 73e0bc8 | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 69 | vec3_t Ba, Bm; |
| 70 | uint32_t mInitState; |
Mathias Agopian | 6043e53 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 71 | float mGyroRate; |
Mathias Agopian | 73e0bc8 | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 72 | vec<vec3_t, 3> mData; |
| 73 | size_t mCount[3]; |
| 74 | enum { ACC=0x1, MAG=0x2, GYRO=0x4 }; |
Mathias Agopian | 6043e53 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 75 | bool checkInitComplete(int, const vec3_t& w, float d = 0); |
| 76 | void initFusion(const vec4_t& q0, float dT); |
Max Braun | 3d41ecd | 2011-08-17 18:22:52 -0700 | [diff] [blame] | 77 | void checkState(); |
Mathias Agopian | 6043e53 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 78 | void predict(const vec3_t& w, float dT); |
Mathias Agopian | 73e0bc8 | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 79 | void update(const vec3_t& z, const vec3_t& Bi, float sigma); |
Mathias Agopian | 6043e53 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 80 | static mat34_t getF(const vec4_t& p); |
Mathias Agopian | 73e0bc8 | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | }; // namespace android |
| 84 | |
| 85 | #endif // ANDROID_FUSION_H |