Advertisement
Guest User

Kristian Lauszus, TKJ Electronics

a guest
Mar 19th, 2017
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. /* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved.
  2. This software may be distributed and modified under the terms of the GNU
  3. General Public License version 2 (GPL2) as published by the Free Software
  4. Foundation and appearing in the file GPL2.TXT included in the packaging of
  5. this file. Please note that GPL2 Section 2[b] requires that all works based
  6. on this software must also be made publicly available under the terms of
  7. the GPL2 ("Copyleft").
  8. Contact information
  9. -------------------
  10. Kristian Lauszus, TKJ Electronics
  11. Web : http://www.tkjelectronics.com
  12. */
  13.  
  14. #ifndef _Kalman_h
  15. #define _Kalman_h
  16.  
  17. class Kalman {
  18. public:
  19. Kalman() {
  20. /* We will set the variables like so, these can also be tuned by the user */
  21. Q_angle = 0.001;
  22. Q_bias = 0.003;
  23. R_measure = 0.03;
  24.  
  25. angle = 0; // Reset the angle
  26. bias = 0; // Reset bias
  27.  
  28. P[0][0] = 0; // Since we assume that the bias is 0 and we know the starting angle (use setAngle), the error covariance matrix is set like so - see: http://en.wikipedia.org/wiki/Kalman_filter#Example_application.2C_technical
  29. P[0][1] = 0;
  30. P[1][0] = 0;
  31. P[1][1] = 0;
  32. };
  33. // The angle should be in degrees and the rate should be in degrees per second and the delta time in seconds
  34. double getAngle(double newAngle, double newRate, double dt) {
  35. // KasBot V2 - Kalman filter module - http://www.x-firm.com/?page_id=145
  36. // Modified by Kristian Lauszus
  37. // See my blog post for more information: http://blog.tkjelectronics.dk/2012/09/a-practical-approach-to-kalman-filter-and-how-to-implement-it
  38.  
  39. // Discrete Kalman filter time update equations - Time Update ("Predict")
  40. // Update xhat - Project the state ahead
  41. /* Step 1 */
  42. rate = newRate - bias;
  43. angle += dt * rate;
  44.  
  45. // Update estimation error covariance - Project the error covariance ahead
  46. /* Step 2 */
  47. P[0][0] += dt * (dt*P[1][1] - P[0][1] - P[1][0] + Q_angle);
  48. P[0][1] -= dt * P[1][1];
  49. P[1][0] -= dt * P[1][1];
  50. P[1][1] += Q_bias * dt;
  51.  
  52. // Discrete Kalman filter measurement update equations - Measurement Update ("Correct")
  53. // Calculate Kalman gain - Compute the Kalman gain
  54. /* Step 4 */
  55. S = P[0][0] + R_measure;
  56. /* Step 5 */
  57. K[0] = P[0][0] / S;
  58. K[1] = P[1][0] / S;
  59.  
  60. // Calculate angle and bias - Update estimate with measurement zk (newAngle)
  61. /* Step 3 */
  62. y = newAngle - angle;
  63. /* Step 6 */
  64. angle += K[0] * y;
  65. bias += K[1] * y;
  66.  
  67. // Calculate estimation error covariance - Update the error covariance
  68. /* Step 7 */
  69. P[0][0] -= K[0] * P[0][0];
  70. P[0][1] -= K[0] * P[0][1];
  71. P[1][0] -= K[1] * P[0][0];
  72. P[1][1] -= K[1] * P[0][1];
  73.  
  74. return angle;
  75. };
  76. void setAngle(double newAngle) { angle = newAngle; }; // Used to set angle, this should be set as the starting angle
  77. double getRate() { return rate; }; // Return the unbiased rate
  78.  
  79. /* These are used to tune the Kalman filter */
  80. void setQangle(double newQ_angle) { Q_angle = newQ_angle; };
  81. void setQbias(double newQ_bias) { Q_bias = newQ_bias; };
  82. void setRmeasure(double newR_measure) { R_measure = newR_measure; };
  83.  
  84. double getQangle() { return Q_angle; };
  85. double getQbias() { return Q_bias; };
  86. double getRmeasure() { return R_measure; };
  87.  
  88. private:
  89. /* Kalman filter variables */
  90. double Q_angle; // Process noise variance for the accelerometer
  91. double Q_bias; // Process noise variance for the gyro bias
  92. double R_measure; // Measurement noise variance - this is actually the variance of the measurement noise
  93.  
  94. double angle; // The angle calculated by the Kalman filter - part of the 2x1 state vector
  95. double bias; // The gyro bias calculated by the Kalman filter - part of the 2x1 state vector
  96. double rate; // Unbiased rate calculated from the rate and the calculated bias - you have to call getAngle to update the rate
  97.  
  98. double P[2][2]; // Error covariance matrix - This is a 2x2 matrix
  99. double K[2]; // Kalman gain - This is a 2x1 vector
  100. double y; // Angle difference
  101. double S; // Estimate error
  102. };
  103.  
  104. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement