Kalman.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. e-mail : kristianl@tkjelectronics.com
  13. */
  14. #include "Kalman.h"
  15. Kalman::Kalman() {
  16. /* We will set the variables like so, these can also be tuned by the user */
  17. Q_angle = 0.001f;
  18. Q_bias = 0.003f;
  19. R_measure = 0.03f;
  20. angle = 0.0f; // Reset the angle
  21. bias = 0.0f; // Reset bias
  22. P[0][0] = 0.0f; // 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
  23. P[0][1] = 0.0f;
  24. P[1][0] = 0.0f;
  25. P[1][1] = 0.0f;
  26. };
  27. // The angle should be in degrees and the rate should be in degrees per second and the delta time in seconds
  28. float Kalman::getAngle(float newAngle, float newRate, float dt) {
  29. // KasBot V2 - Kalman filter module - http://www.x-firm.com/?page_id=145
  30. // Modified by Kristian Lauszus
  31. // See my blog post for more information: http://blog.tkjelectronics.dk/2012/09/a-practical-approach-to-kalman-filter-and-how-to-implement-it
  32. // Discrete Kalman filter time update equations - Time Update ("Predict")
  33. // Update xhat - Project the state ahead
  34. /* Step 1 */
  35. rate = newRate - bias;
  36. angle += dt * rate;
  37. // Update estimation error covariance - Project the error covariance ahead
  38. /* Step 2 */
  39. P[0][0] += dt * (dt*P[1][1] - P[0][1] - P[1][0] + Q_angle);
  40. P[0][1] -= dt * P[1][1];
  41. P[1][0] -= dt * P[1][1];
  42. P[1][1] += Q_bias * dt;
  43. // Discrete Kalman filter measurement update equations - Measurement Update ("Correct")
  44. // Calculate Kalman gain - Compute the Kalman gain
  45. /* Step 4 */
  46. float S = P[0][0] + R_measure; // Estimate error
  47. /* Step 5 */
  48. float K[2]; // Kalman gain - This is a 2x1 vector
  49. K[0] = P[0][0] / S;
  50. K[1] = P[1][0] / S;
  51. // Calculate angle and bias - Update estimate with measurement zk (newAngle)
  52. /* Step 3 */
  53. float y = newAngle - angle; // Angle difference
  54. /* Step 6 */
  55. angle += K[0] * y;
  56. bias += K[1] * y;
  57. // Calculate estimation error covariance - Update the error covariance
  58. /* Step 7 */
  59. float P00_temp = P[0][0];
  60. float P01_temp = P[0][1];
  61. P[0][0] -= K[0] * P00_temp;
  62. P[0][1] -= K[0] * P01_temp;
  63. P[1][0] -= K[1] * P00_temp;
  64. P[1][1] -= K[1] * P01_temp;
  65. return angle;
  66. };
  67. void Kalman::setAngle(float angle) { this->angle = angle; }; // Used to set angle, this should be set as the starting angle
  68. float Kalman::getRate() { return this->rate; }; // Return the unbiased rate
  69. /* These are used to tune the Kalman filter */
  70. void Kalman::setQangle(float Q_angle) { this->Q_angle = Q_angle; };
  71. void Kalman::setQbias(float Q_bias) { this->Q_bias = Q_bias; };
  72. void Kalman::setRmeasure(float R_measure) { this->R_measure = R_measure; };
  73. float Kalman::getQangle() { return this->Q_angle; };
  74. float Kalman::getQbias() { return this->Q_bias; };
  75. float Kalman::getRmeasure() { return this->R_measure; };