AC_PID.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /// @file AC_PID.cpp
  2. /// @brief Generic PID algorithm
  3. #include <AP_Math/AP_Math.h>
  4. #include "AC_PID.h"
  5. const AP_Param::GroupInfo AC_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_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_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_PID, _kd, 0),
  18. // 3 was for uint16 IMAX
  19. // @Param: FF
  20. // @DisplayName: FF FeedForward Gain
  21. // @Description: FF Gain which produces an output value that is proportional to the demanded input
  22. AP_GROUPINFO("FF", 4, AC_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_PID, _kimax, 0),
  27. // 6 was for float FILT
  28. // 7 is for float ILMI and FF
  29. // index 8 was for AFF
  30. // @Param: FLTT
  31. // @DisplayName: PID Target filter frequency in Hz
  32. // @Description: Target filter frequency in Hz
  33. // @Units: Hz
  34. AP_GROUPINFO("FLTT", 9, AC_PID, _filt_T_hz, AC_PID_TFILT_HZ_DEFAULT),
  35. // @Param: FLTE
  36. // @DisplayName: PID Error filter frequency in Hz
  37. // @Description: Error filter frequency in Hz
  38. // @Units: Hz
  39. AP_GROUPINFO("FLTE", 10, AC_PID, _filt_E_hz, AC_PID_EFILT_HZ_DEFAULT),
  40. // @Param: FLTD
  41. // @DisplayName: PID Derivative term filter frequency in Hz
  42. // @Description: Derivative filter frequency in Hz
  43. // @Units: Hz
  44. AP_GROUPINFO("FLTD", 11, AC_PID, _filt_D_hz, AC_PID_DFILT_HZ_DEFAULT),
  45. AP_GROUPEND
  46. };
  47. // Constructor
  48. AC_PID::AC_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) :
  49. _dt(dt),
  50. _integrator(0.0f),
  51. _error(0.0f),
  52. _derivative(0.0f)
  53. {
  54. // load parameter values from eeprom
  55. AP_Param::setup_object_defaults(this, var_info);
  56. _kp = initial_p;
  57. _ki = initial_i;
  58. _kd = initial_d;
  59. _kff = initial_ff;
  60. _kimax = fabsf(initial_imax);
  61. filt_T_hz(initial_filt_T_hz);
  62. filt_E_hz(initial_filt_E_hz);
  63. filt_D_hz(initial_filt_D_hz);
  64. // reset input filter to first value received
  65. _flags._reset_filter = true;
  66. memset(&_pid_info, 0, sizeof(_pid_info));
  67. }
  68. // set_dt - set time step in seconds
  69. void AC_PID::set_dt(float dt)
  70. {
  71. // set dt and calculate the input filter alpha
  72. _dt = dt;
  73. }
  74. // filt_T_hz - set target filter hz
  75. void AC_PID::filt_T_hz(float hz)
  76. {
  77. _filt_T_hz.set(fabsf(hz));
  78. }
  79. // filt_E_hz - set error filter hz
  80. void AC_PID::filt_E_hz(float hz)
  81. {
  82. _filt_E_hz.set(fabsf(hz));
  83. }
  84. // filt_D_hz - set derivative filter hz
  85. void AC_PID::filt_D_hz(float hz)
  86. {
  87. _filt_D_hz.set(fabsf(hz));
  88. }
  89. // update_all - set target and measured inputs to PID controller and calculate outputs
  90. // target and error are filtered
  91. // the derivative is then calculated and filtered
  92. // the integral is then updated based on the setting of the limit flag
  93. float AC_PID::update_all(float target, float measurement, bool limit)
  94. {
  95. // don't process inf or NaN
  96. if (!isfinite(target) || !isfinite(measurement)) {
  97. return 0.0f;
  98. }
  99. // reset input filter to value received
  100. if (_flags._reset_filter) {
  101. _flags._reset_filter = false;
  102. _target = target;
  103. _error = _target - measurement;
  104. _derivative = 0.0f;
  105. } else {
  106. float error_last = _error;
  107. _target += get_filt_T_alpha() * (target - _target);
  108. _error += get_filt_E_alpha() * ((_target - measurement) - _error);
  109. // calculate and filter derivative
  110. if (_dt > 0.0f) {
  111. float derivative = (_error - error_last) / _dt;
  112. _derivative += get_filt_D_alpha() * (derivative - _derivative);
  113. }
  114. }
  115. // update I term
  116. update_i(limit);
  117. float P_out = (_error * _kp);
  118. float D_out = (_derivative * _kd);
  119. _pid_info.target = _target;
  120. _pid_info.actual = measurement;
  121. _pid_info.error = _error;
  122. _pid_info.P = P_out;
  123. _pid_info.D = D_out;
  124. return P_out + _integrator + D_out;
  125. }
  126. // update_error - set error input to PID controller and calculate outputs
  127. // target is set to zero and error is set and filtered
  128. // the derivative then is calculated and filtered
  129. // the integral is then updated based on the setting of the limit flag
  130. // Target and Measured must be set manually for logging purposes.
  131. // todo: remove function when it is no longer used.
  132. float AC_PID::update_error(float error, bool limit)
  133. {
  134. // don't process inf or NaN
  135. if (!isfinite(error)) {
  136. return 0.0f;
  137. }
  138. _target = 0.0f;
  139. // reset input filter to value received
  140. if (_flags._reset_filter) {
  141. _flags._reset_filter = false;
  142. _error = error;
  143. _derivative = 0.0f;
  144. } else {
  145. float error_last = _error;
  146. _error += get_filt_E_alpha() * (error - _error);
  147. // calculate and filter derivative
  148. if (_dt > 0.0f) {
  149. float derivative = (_error - error_last) / _dt;
  150. _derivative += get_filt_D_alpha() * (derivative - _derivative);
  151. }
  152. }
  153. // update I term
  154. update_i(limit);
  155. float P_out = (_error * _kp);
  156. float D_out = (_derivative * _kd);
  157. _pid_info.target = 0.0f;
  158. _pid_info.actual = 0.0f;
  159. _pid_info.error = _error;
  160. _pid_info.P = P_out;
  161. _pid_info.D = D_out;
  162. return P_out + _integrator + D_out;
  163. }
  164. // update_i - update the integral
  165. // If the limit flag is set the integral is only allowed to shrink
  166. void AC_PID::update_i(bool limit)
  167. {
  168. if (!is_zero(_ki) && is_positive(_dt)) {
  169. // Ensure that integrator can only be reduced if the output is saturated
  170. if (!limit || ((is_positive(_integrator) && is_negative(_error)) || (is_negative(_integrator) && is_positive(_error)))) {
  171. _integrator += ((float)_error * _ki) * _dt;
  172. _integrator = constrain_float(_integrator, -_kimax, _kimax);
  173. }
  174. } else {
  175. _integrator = 0.0f;
  176. }
  177. _pid_info.I = _integrator;
  178. }
  179. float AC_PID::get_p() const
  180. {
  181. return _error * _kp;
  182. }
  183. float AC_PID::get_i() const
  184. {
  185. return _integrator;
  186. }
  187. float AC_PID::get_d() const
  188. {
  189. return _kd * _derivative;
  190. }
  191. float AC_PID::get_ff()
  192. {
  193. _pid_info.FF = _target * _kff;
  194. return _target * _kff;
  195. }
  196. // todo: remove function when it is no longer used.
  197. float AC_PID::get_ff(float target)
  198. {
  199. float FF_out = (target * _kff);
  200. _pid_info.FF = FF_out;
  201. return FF_out;
  202. }
  203. void AC_PID::reset_I()
  204. {
  205. _integrator = 0;
  206. }
  207. void AC_PID::load_gains()
  208. {
  209. _kp.load();
  210. _ki.load();
  211. _kd.load();
  212. _kff.load();
  213. _kimax.load();
  214. _kimax = fabsf(_kimax);
  215. _filt_T_hz.load();
  216. _filt_E_hz.load();
  217. _filt_D_hz.load();
  218. }
  219. // save_gains - save gains to eeprom
  220. void AC_PID::save_gains()
  221. {
  222. _kp.save();
  223. _ki.save();
  224. _kd.save();
  225. _kff.save();
  226. _kimax.save();
  227. _filt_T_hz.save();
  228. _filt_E_hz.save();
  229. _filt_D_hz.save();
  230. }
  231. /// Overload the function call operator to permit easy initialisation
  232. void AC_PID::operator()(float p_val, float i_val, float d_val, float ff_val, float imax_val, float input_filt_T_hz, float input_filt_E_hz, float input_filt_D_hz, float dt)
  233. {
  234. _kp = p_val;
  235. _ki = i_val;
  236. _kd = d_val;
  237. _kff = ff_val;
  238. _kimax = fabsf(imax_val);
  239. _filt_T_hz = input_filt_T_hz;
  240. _filt_E_hz = input_filt_E_hz;
  241. _filt_D_hz = input_filt_D_hz;
  242. _dt = dt;
  243. }
  244. // get_filt_T_alpha - get the target filter alpha
  245. float AC_PID::get_filt_T_alpha() const
  246. {
  247. return get_filt_alpha(_filt_T_hz);
  248. }
  249. // get_filt_E_alpha - get the error filter alpha
  250. float AC_PID::get_filt_E_alpha() const
  251. {
  252. return get_filt_alpha(_filt_E_hz);
  253. }
  254. // get_filt_D_alpha - get the derivative filter alpha
  255. float AC_PID::get_filt_D_alpha() const
  256. {
  257. return get_filt_alpha(_filt_D_hz);
  258. }
  259. // get_filt_alpha - calculate a filter alpha
  260. float AC_PID::get_filt_alpha(float filt_hz) const
  261. {
  262. if (is_zero(filt_hz)) {
  263. return 1.0f;
  264. }
  265. // calculate alpha
  266. float rc = 1 / (M_2PI * filt_hz);
  267. return _dt / (_dt + rc);
  268. }
  269. void AC_PID::set_integrator(float target, float measurement, float i)
  270. {
  271. set_integrator(target - measurement, i);
  272. }
  273. void AC_PID::set_integrator(float error, float i)
  274. {
  275. _integrator = constrain_float(i - error * _kp, -_kimax, _kimax);
  276. }