pid.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * pid.c
  3. *
  4. * Created on: Jan 12, 2021
  5. * Author: David Original work by Jose (PTDreamer), 2017
  6. */
  7. #include "pid.h"
  8. #include "tempsensors.h"
  9. #include "settings.h"
  10. PIDController_t pid;
  11. void setupPID(pid_values_t* p) {
  12. pid.Kp = (float)p->Kp/1000000;
  13. pid.Ki = (float)p->Ki/1000000;
  14. pid.Kd = (float)p->Kd/1000000;
  15. pid.limMinInt = (float)p->minI/100;
  16. pid.limMaxInt = (float)p->maxI/100;
  17. pid.limMin = (float)0;
  18. pid.limMax = (float)1;
  19. //pid.tau = (float)0.2; //TODO adjust this from menu? This is not used currently used (For New PID)
  20. }
  21. // New part from Phil: https://github.com/pms67/PID
  22. int32_t calculatePID(int32_t setpoint, int32_t measurement, int32_t baseCalc) {
  23. float dt = (float)(HAL_GetTick() - pid.lastTime)/1000;
  24. float error = setpoint - measurement;
  25. // Proportional term
  26. pid.proportional = pid.Kp * error;
  27. // Integral
  28. // pid.integrator = pid.integrator + 0.5f * pid.Ki * dt * (error + pid.prevError); // New
  29. pid.integrator = pid.integrator + (pid.Ki*(error*dt)); // Old
  30. // Integrator clamping
  31. if (pid.integrator > pid.limMaxInt) {
  32. pid.integrator = pid.limMaxInt;
  33. }
  34. else if (pid.integrator < pid.limMinInt) {
  35. pid.integrator = pid.limMinInt;
  36. }
  37. // Derivative term
  38. if(error==pid.prevError) {
  39. pid.derivative = 0;
  40. }
  41. else{
  42. /* // New
  43. pid.derivative = -(2.0f * pid.Kd * (measurement - pid.prevMeasurement) // Note: derivative on measurement,
  44. + (2.0f * pid.tau - dt) * pid.derivative) // therefore minus sign in front of equation!
  45. / (2.0f * pid.tau + dt);
  46. */
  47. pid.derivative = pid.Kd*((error-pid.prevError)/dt); // Old
  48. }
  49. // Compute output and apply limits
  50. pid.out = pid.proportional + pid.integrator + pid.derivative;
  51. if(pid.out > pid.limMax){
  52. pid.out = pid.limMax;
  53. } else if (pid.out < pid.limMin) {
  54. pid.out = pid.limMin;
  55. }
  56. // Store error and measurement for later use
  57. pid.prevMeasurement = measurement;
  58. pid.lastTime = HAL_GetTick();
  59. pid.prevError = error;
  60. return (pid.out*baseCalc);
  61. }
  62. void resetPID(void){
  63. pid.integrator = 0;
  64. pid.derivative = 0;
  65. pid.prevError = 0;
  66. pid.lastTime = HAL_GetTick();
  67. }
  68. float getPID_D() {
  69. return pid.derivative;
  70. }
  71. float getPID_P() {
  72. return pid.proportional;
  73. }
  74. float getPID_I() {
  75. return pid.integrator;
  76. }
  77. float getPID_Error() {
  78. return pid.prevError;
  79. }
  80. float getPID_Output() {
  81. return pid.out;
  82. }
  83. int32_t getPID_SetPoint() {
  84. return pid.lastSetpoint;
  85. }
  86. int32_t getPID_PresentValue() {
  87. return pid.lastMeasurement;
  88. }