Fusion.h 2.2 KB

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