ALGO_Kalman_ATY.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * @file ALGO_Kalman_ATY.h
  3. *
  4. * @param Project ALGO_Algorithm_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2023 MZ-ATY
  10. * - This code follows:
  11. * - MZ-ATY Various Contents Joint Statement -
  12. * <a href="https://mengze.top/MZ-ATY_VCJS">
  13. * https://mengze.top/MZ-ATY_VCJS</a>
  14. * - CC 4.0 BY-NC-SA -
  15. * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
  16. * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
  17. * - Your use will be deemed to have accepted the terms of this statement.
  18. *
  19. * @brief Familiar functions of Kalman
  20. *
  21. * @version
  22. * - 1_01_220605 > ATY
  23. * -# Preliminary version, first Release
  24. * - 1_02_230108 > ATY
  25. * -# Finish and test 2D
  26. ********************************************************************************
  27. */
  28. #ifndef __ALGO_Kalman_ATY_H
  29. #define __ALGO_Kalman_ATY_H
  30. #include "INCLUDE_ATY.h"
  31. /******************************* For user *************************************/
  32. // #define __DEBUG_ALGO_Kalman_ATY
  33. // #define Kalman_2D
  34. #define Kalman1D_TYPE 1
  35. /******************************************************************************/
  36. #if !Kalman1D_TYPE // use below instead
  37. typedef struct _ALGO_Kalman_S
  38. {
  39. float L_P; // estimate uncertainty (covariance) matrix of last state, not zero !
  40. float P; // predicted estimate uncertainty (covariance) matrix for this state
  41. float O; // kalman filter out
  42. float G; // kalman gain
  43. float Q; // process noise (covariance) matrix
  44. float R; // measurement noise covariance matrix (Measurement Uncertainty)
  45. }ALGO_Kalman1D_S;
  46. #else
  47. typedef struct _ALGO_Kalman1D_S
  48. {
  49. float X; // state
  50. float G; // kalman gain
  51. float A; // x(n)=A*x(n-1)+u(n),u(n)~N(0,q)
  52. float H; // z(n)=H*x(n)+w(n),w(n)~N(0,r)
  53. float P; // estimated error convariance
  54. float Q; // process(predict) noise convariance
  55. float R; // measure noise convariance
  56. } ALGO_Kalman1D_S;
  57. #endif /* 01 */
  58. float ALGO_KalmanFilter1D(ALGO_Kalman1D_S* kfp, float input);
  59. #ifdef Kalman_2D
  60. typedef struct _ALGO_Kalman2D_S
  61. {
  62. float X[2]; // state: [0]-angle [1]-diffrence of angle, 2x1
  63. float G[2]; // kalman gain, 2x1
  64. float A[2][2]; // X(n)=A*X(n-1)+U(n),U(n)~N(0,q), 2x2
  65. float H[2]; // Z(n)=H*X(n)+W(n),W(n)~N(0,r), 1x2
  66. float P[2][2]; // estimated error convariance,2x2 [p0 p1; p2 p3]
  67. float Q[2][2]; // process(predict) noise convariance,2x1 [q0,0; 0,q1]
  68. float R; // measure noise convariance
  69. } ALGO_Kalman2D_S;
  70. float ALGO_KalmanFilter2D(ALGO_Kalman2D_S* kfp, float input);
  71. #endif /* Kalman_2D */
  72. #ifdef __DEBUG_ALGO_Kalman_ATY
  73. extern ALGO_Kalman1D_S ALGO_kfp1D;
  74. extern ALGO_Kalman2D_S ALGO_kfp2D;
  75. extern float testSignalSigned[120];
  76. extern float testSignalUnsigned[120];
  77. extern const float code testSignalFloat[620];
  78. extern uint16_t code testSignalUint16[5000];
  79. void ALGO_Kalman_Test(void);
  80. #endif /* __DEBUG_ALGO_Kalman_ATY */
  81. #endif /* __ALGO_Kalman_ATY_H */
  82. /******************************** End Of File *********************************/