AC_HELI_PID.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /// @file AC_HELI_PID.cpp
  2. /// @brief Generic PID algorithm
  3. #include <AP_Math/AP_Math.h>
  4. #include "AC_HELI_PID.h"
  5. const AP_Param::GroupInfo AC_HELI_PID::var_info[] = {
  6. // @Param: P
  7. // @DisplayName: PID Proportional Gain
  8. // @Description: P Gain which produces an output value that is proportional to the current error value
  9. AP_GROUPINFO("P", 0, AC_HELI_PID, _kp, 0),
  10. // @Param: I
  11. // @DisplayName: PID Integral Gain
  12. // @Description: I Gain which produces an output that is proportional to both the magnitude and the duration of the error
  13. AP_GROUPINFO("I", 1, AC_HELI_PID, _ki, 0),
  14. // @Param: D
  15. // @DisplayName: PID Derivative Gain
  16. // @Description: D Gain which produces an output that is proportional to the rate of change of the error
  17. AP_GROUPINFO("D", 2, AC_HELI_PID, _kd, 0),
  18. // 3 was for uint16 IMAX
  19. // @Param: VFF
  20. // @DisplayName: Velocity FF FeedForward Gain
  21. // @Description: Velocity FF Gain which produces an output value that is proportional to the demanded input
  22. AP_GROUPINFO("VFF", 4, AC_HELI_PID, _kff, 0),
  23. // @Param: IMAX
  24. // @DisplayName: PID Integral Maximum
  25. // @Description: The maximum/minimum value that the I term can output
  26. AP_GROUPINFO("IMAX", 5, AC_HELI_PID, _kimax, 0),
  27. // 6 was for float FILT
  28. // @Param: ILMI
  29. // @DisplayName: I-term Leak Minimum
  30. // @Description: Point below which I-term will not leak down
  31. // @Range: 0 1
  32. // @User: Advanced
  33. AP_GROUPINFO("ILMI", 7, AC_HELI_PID, _leak_min, AC_PID_LEAK_MIN),
  34. // 8 was for float AFF
  35. // @Param: FLTT
  36. // @DisplayName: PID Target filter frequency in Hz
  37. // @Description: Target filter frequency in Hz
  38. // @Units: Hz
  39. AP_GROUPINFO("FLTT", 9, AC_HELI_PID, _filt_T_hz, AC_PID_TFILT_HZ_DEFAULT),
  40. // @Param: FLTE
  41. // @DisplayName: PID Error filter frequency in Hz
  42. // @Description: Error filter frequency in Hz
  43. // @Units: Hz
  44. AP_GROUPINFO("FLTE", 10, AC_HELI_PID, _filt_E_hz, AC_PID_EFILT_HZ_DEFAULT),
  45. // @Param: FLTD
  46. // @DisplayName: PID D term filter frequency in Hz
  47. // @Description: Derivative filter frequency in Hz
  48. // @Units: Hz
  49. AP_GROUPINFO("FLTD", 11, AC_HELI_PID, _filt_D_hz, AC_PID_DFILT_HZ_DEFAULT),
  50. AP_GROUPEND
  51. };
  52. /// Constructor for PID
  53. AC_HELI_PID::AC_HELI_PID(float initial_p, float initial_i, float initial_d, float initial_ff, float initial_imax, float initial_filt_T_hz, float initial_filt_E_hz, float initial_filt_D_hz, float dt) :
  54. AC_PID(initial_p, initial_i, initial_d, initial_ff, initial_imax, initial_filt_T_hz, initial_filt_E_hz, initial_filt_D_hz, dt)
  55. {
  56. _last_requested_rate = 0;
  57. }
  58. // This is an integrator which tends to decay to zero naturally
  59. // if the error is zero.
  60. void AC_HELI_PID::update_leaky_i(float leak_rate)
  61. {
  62. if(!is_zero(_ki) && !is_zero(_dt)){
  63. // integrator does not leak down below Leak Min
  64. if (_integrator > _leak_min){
  65. _integrator -= (float)(_integrator - _leak_min) * leak_rate;
  66. } else if (_integrator < -_leak_min) {
  67. _integrator -= (float)(_integrator + _leak_min) * leak_rate;
  68. }
  69. _pid_info.I = _integrator;
  70. }
  71. }