pid.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * pid.h
  3. *
  4. * Created on: Jan 12, 2021
  5. * Author: David Original work by Jose (PTDreamer), 2017
  6. */
  7. #ifndef PID_H_
  8. #define PID_H_
  9. #include "main.h"
  10. typedef struct pid_values {
  11. uint16_t Kp;
  12. uint16_t Ki;
  13. uint16_t Kd;
  14. int16_t maxI;
  15. int16_t minI;
  16. } pid_values_t;
  17. typedef struct {
  18. uint32_t lastTime;
  19. int32_t lastMeasurement;
  20. int32_t lastSetpoint;
  21. /* Controller gains */
  22. float Kp;
  23. float Ki;
  24. float Kd;
  25. /* Derivative low-pass filter time constant */
  26. float tau;
  27. /* Output limits */
  28. float limMin;
  29. float limMax;
  30. /* Integrator limits */
  31. float limMinInt;
  32. float limMaxInt;
  33. /* Controller "memory" */
  34. float proportional;
  35. float integrator;
  36. float derivative;
  37. float prevError; /* Required for integrator */
  38. float prevMeasurement; /* Required for derivative */
  39. /* Controller output */
  40. float out;
  41. } PIDController_t;
  42. extern PIDController_t pid;
  43. void setupPID(pid_values_t* p);
  44. int32_t calculatePID(int32_t setpoint, int32_t measurement, int32_t baseCalc);
  45. void resetPID();
  46. float getPID_P();
  47. float getPID_I();
  48. float getPID_D();
  49. float getPID_Output();
  50. float getPID_Error();
  51. int32_t getPID_SetPoint();
  52. int32_t getPID_PresentValue();
  53. #endif /* PID_H_ */