Kalman.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. #ifndef _Kalman_h_
  15. #define _Kalman_h_
  16. class Kalman {
  17. public:
  18. Kalman();
  19. // The angle should be in degrees and the rate should be in degrees per second and the delta time in seconds
  20. float getAngle(float newAngle, float newRate, float dt);
  21. void setAngle(float angle); // Used to set angle, this should be set as the starting angle
  22. float getRate(); // Return the unbiased rate
  23. /* These are used to tune the Kalman filter */
  24. void setQangle(float Q_angle);
  25. /**
  26. * setQbias(float Q_bias)
  27. * Default value (0.003f) is in Kalman.cpp.
  28. * Raise this to follow input more closely,
  29. * lower this to smooth result of kalman filter.
  30. */
  31. void setQbias(float Q_bias);
  32. void setRmeasure(float R_measure);
  33. float getQangle();
  34. float getQbias();
  35. float getRmeasure();
  36. private:
  37. /* Kalman filter variables */
  38. float Q_angle; // Process noise variance for the accelerometer
  39. float Q_bias; // Process noise variance for the gyro bias
  40. float R_measure; // Measurement noise variance - this is actually the variance of the measurement noise
  41. float angle; // The angle calculated by the Kalman filter - part of the 2x1 state vector
  42. float bias; // The gyro bias calculated by the Kalman filter - part of the 2x1 state vector
  43. float rate; // Unbiased rate calculated from the rate and the calculated bias - you have to call getAngle to update the rate
  44. float P[2][2]; // Error covariance matrix - This is a 2x2 matrix
  45. };
  46. #endif