pid.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * PID controller
  3. *
  4. * Copyright (c) 2015-2018 Michael Buesch <m@bues.ch>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include "pid.h"
  21. #include "util.h"
  22. #include "debug_uart.h"
  23. #include <string.h>
  24. void pid_reset(struct pid *pid)
  25. {
  26. pid->prev_e = int_to_fixpt(0);
  27. pid->integr = int_to_fixpt(0);
  28. }
  29. void pid_set_factors(struct pid *pid, const struct pid_k_set *k)
  30. {
  31. pid->k = *k;
  32. pid_reset(pid);
  33. }
  34. fixpt_t pid_run(struct pid *pid, fixpt_t dt, fixpt_t r)
  35. {
  36. fixpt_t e, de;
  37. fixpt_t kp, ki, kd;
  38. fixpt_t p, i, d;
  39. fixpt_t pid_result;
  40. /* Calculate the deviation. */
  41. e = fixpt_sub(pid->setpoint, r);
  42. #if CONF_DEBUG
  43. debug_report_fixpt(DEBUG_PREFIX2(pid->name, PSTR("e")),
  44. &pid->debug_old_e, e);
  45. #endif
  46. /* P term */
  47. kp = pid->k.kp;
  48. p = fixpt_mul(kp, e);
  49. pid_result = p;
  50. #if CONF_DEBUG
  51. debug_report_fixpt(DEBUG_PREFIX2(pid->name, PSTR("p")),
  52. &pid->debug_old_p, p);
  53. #endif
  54. /* I term */
  55. ki = pid->k.ki;
  56. i = fixpt_add(pid->integr, fixpt_mul(fixpt_mul(ki, e), dt));
  57. i = clamp(i, pid->i_neglim, pid->i_poslim);
  58. pid->integr = i;
  59. pid_result = fixpt_add(pid_result, i);
  60. #if CONF_DEBUG
  61. debug_report_fixpt(DEBUG_PREFIX2(pid->name, PSTR("i")),
  62. &pid->debug_old_i, i);
  63. #endif
  64. /* D term */
  65. kd = pid->k.kd;
  66. de = fixpt_sub(e, pid->prev_e);
  67. d = fixpt_mul_div(de, kd, dt);
  68. pid->prev_e = fixpt_div(e, pid->k.d_decay_div);
  69. pid_result = fixpt_add(pid_result, d);
  70. #if CONF_DEBUG
  71. debug_report_fixpt(DEBUG_PREFIX2(pid->name, PSTR("d")),
  72. &pid->debug_old_d, d);
  73. debug_report_fixpt(DEBUG_PREFIX2(pid->name, PSTR("pe")),
  74. &pid->debug_old_pe, pid->prev_e);
  75. #endif
  76. pid_result = clamp(pid_result, pid->y_neglim, pid->y_poslim);
  77. return pid_result;
  78. }
  79. void pid_init(struct pid *pid,
  80. #if CONF_DEBUG
  81. const char __flash *name,
  82. #endif
  83. const struct pid_k_set *k,
  84. fixpt_t i_neglim, fixpt_t i_poslim,
  85. fixpt_t y_neglim, fixpt_t y_poslim)
  86. {
  87. #if CONF_DEBUG
  88. pid->name = name;
  89. #endif
  90. pid->i_neglim = i_neglim;
  91. pid->i_poslim = i_poslim;
  92. pid->y_neglim = y_neglim;
  93. pid->y_poslim = y_poslim;
  94. pid_set_factors(pid, k);
  95. }