ALGO_Kalman_ATY.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 - 2026 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 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 Kalman1D_TYPE 1
  33. // #define Kalman_2D
  34. /******************************************************************************/
  35. #if !Kalman1D_TYPE // use below instead
  36. typedef struct _ALGO_Kalman_S{
  37. float L_P; // estimate uncertainty (covariance) matrix of last state, not zero !
  38. float P; // predicted estimate uncertainty (covariance) matrix for this state
  39. float O; // kalman filter out
  40. float G; // kalman gain
  41. float Q; // process noise (covariance) matrix
  42. float R; // measurement noise covariance matrix (Measurement Uncertainty)
  43. }ALGO_Kalman1D_S;
  44. #else
  45. typedef struct _ALGO_Kalman1D_S{
  46. float X; // state / origin value
  47. float G; // kalman gain
  48. float A; // x(n)=A*x(n-1)+u(n),u(n)~N(0,q)
  49. float H; // z(n)=H*x(n)+w(n),w(n)~N(0,r)
  50. float P; // estimated error convariance
  51. float Q; // process(predict) noise convariance
  52. float R; // measure noise convariance
  53. } ALGO_Kalman1D_S;
  54. #endif /* 01 */
  55. float ALGO_KalmanFilter1D(ALGO_Kalman1D_S* kfp, float input);
  56. #ifdef Kalman_2D
  57. typedef struct _ALGO_Kalman2D_S{
  58. float X[2]; // state: [0]-angle [1]-diffrence of angle, 2x1
  59. float G[2]; // kalman gain, 2x1
  60. float A[2][2]; // X(n)=A*X(n-1)+U(n),U(n)~N(0,q), 2x2
  61. float H[2]; // Z(n)=H*X(n)+W(n),W(n)~N(0,r), 1x2
  62. float P[2][2]; // estimated error convariance,2x2 [p0 p1; p2 p3]
  63. float Q[2][2]; // process(predict) noise convariance,2x1 [q0,0; 0,q1]
  64. float R; // measure noise convariance
  65. } ALGO_Kalman2D_S;
  66. float ALGO_KalmanFilter2D(ALGO_Kalman2D_S* kfp, float input);
  67. #endif /* Kalman_2D */
  68. // #define ALGO_Kalman_ATY_Test_ATY
  69. #ifdef ALGO_Kalman_ATY_Test_ATY
  70. // add ALGO_KalmanData_ATY.c to add origin test data
  71. extern ALGO_Kalman1D_S ALGO_kfp1D;
  72. #ifdef Kalman_2D
  73. extern ALGO_Kalman2D_S ALGO_kfp2D;
  74. #endif /* Kalman_2D */
  75. extern float testSignalSigned[120];
  76. extern float testSignalUnsigned[120];
  77. extern uint16_t __CODE testSignalUint16[5000];
  78. void ALGO_Kalman_Test(void);
  79. #endif /* ALGO_Kalman_ATY_Test_ATY */
  80. #endif /* __ALGO_Kalman_ATY_H */
  81. /******************************** End Of File *********************************/