pid.h 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #ifndef PID_H_
  2. #define PID_H_
  3. #include "fixpt.h"
  4. struct pid_k_set {
  5. fixpt_t kp;
  6. fixpt_t ki;
  7. fixpt_t kd;
  8. fixpt_t d_decay_div;
  9. };
  10. struct pid {
  11. struct pid_k_set k;
  12. fixpt_t setpoint;
  13. fixpt_t i_neglim;
  14. fixpt_t i_poslim;
  15. fixpt_t y_neglim;
  16. fixpt_t y_poslim;
  17. fixpt_t prev_e;
  18. fixpt_t integr;
  19. #if CONF_DEBUG
  20. const char __flash *name;
  21. fixpt_t debug_old_e;
  22. fixpt_t debug_old_p;
  23. fixpt_t debug_old_i;
  24. fixpt_t debug_old_d;
  25. fixpt_t debug_old_pe;
  26. #endif
  27. };
  28. void pid_reset(struct pid *pid);
  29. void pid_set_factors(struct pid *pid, const struct pid_k_set *k);
  30. void pid_init(struct pid *pid,
  31. #if CONF_DEBUG
  32. const char __flash *name,
  33. #endif
  34. const struct pid_k_set *k,
  35. fixpt_t i_neglim, fixpt_t i_poslim,
  36. fixpt_t y_neglim, fixpt_t y_poslim);
  37. static inline void pid_set_setpoint(struct pid *pid, fixpt_t setpoint)
  38. {
  39. pid->setpoint = setpoint;
  40. }
  41. static inline fixpt_t pid_get_setpoint(struct pid *pid)
  42. {
  43. return pid->setpoint;
  44. }
  45. fixpt_t pid_run(struct pid *pid, fixpt_t dt, fixpt_t r);
  46. #endif /* PID_H_ */