pid.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * pid.c PID controller for testing cooling devices
  4. *
  5. * Copyright (C) 2012 Intel Corporation. All rights reserved.
  6. *
  7. * Author Name Jacob Pan <jacob.jun.pan@linux.intel.com>
  8. */
  9. #include <unistd.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <stdint.h>
  14. #include <sys/types.h>
  15. #include <dirent.h>
  16. #include <libintl.h>
  17. #include <ctype.h>
  18. #include <assert.h>
  19. #include <time.h>
  20. #include <limits.h>
  21. #include <math.h>
  22. #include <sys/stat.h>
  23. #include <syslog.h>
  24. #include "tmon.h"
  25. /**************************************************************************
  26. * PID (Proportional-Integral-Derivative) controller is commonly used in
  27. * linear control system, consider the process.
  28. * G(s) = U(s)/E(s)
  29. * kp = proportional gain
  30. * ki = integral gain
  31. * kd = derivative gain
  32. * Ts
  33. * We use type C Alan Bradley equation which takes set point off the
  34. * output dependency in P and D term.
  35. *
  36. * y[k] = y[k-1] - kp*(x[k] - x[k-1]) + Ki*Ts*e[k] - Kd*(x[k]
  37. * - 2*x[k-1]+x[k-2])/Ts
  38. *
  39. *
  40. ***********************************************************************/
  41. struct pid_params p_param;
  42. /* cached data from previous loop */
  43. static double xk_1, xk_2; /* input temperature x[k-#] */
  44. /*
  45. * TODO: make PID parameters tuned automatically,
  46. * 1. use CPU burn to produce open loop unit step response
  47. * 2. calculate PID based on Ziegler-Nichols rule
  48. *
  49. * add a flag for tuning PID
  50. */
  51. int init_thermal_controller(void)
  52. {
  53. /* init pid params */
  54. p_param.ts = ticktime;
  55. /* TODO: get it from TUI tuning tab */
  56. p_param.kp = .36;
  57. p_param.ki = 5.0;
  58. p_param.kd = 0.19;
  59. p_param.t_target = target_temp_user;
  60. return 0;
  61. }
  62. void controller_reset(void)
  63. {
  64. /* TODO: relax control data when not over thermal limit */
  65. syslog(LOG_DEBUG, "TC inactive, relax p-state\n");
  66. p_param.y_k = 0.0;
  67. xk_1 = 0.0;
  68. xk_2 = 0.0;
  69. set_ctrl_state(0);
  70. }
  71. /* To be called at time interval Ts. Type C PID controller.
  72. * y[k] = y[k-1] - kp*(x[k] - x[k-1]) + Ki*Ts*e[k] - Kd*(x[k]
  73. * - 2*x[k-1]+x[k-2])/Ts
  74. * TODO: add low pass filter for D term
  75. */
  76. #define GUARD_BAND (2)
  77. void controller_handler(const double xk, double *yk)
  78. {
  79. double ek;
  80. double p_term, i_term, d_term;
  81. ek = p_param.t_target - xk; /* error */
  82. if (ek >= 3.0) {
  83. syslog(LOG_DEBUG, "PID: %3.1f Below set point %3.1f, stop\n",
  84. xk, p_param.t_target);
  85. controller_reset();
  86. *yk = 0.0;
  87. return;
  88. }
  89. /* compute intermediate PID terms */
  90. p_term = -p_param.kp * (xk - xk_1);
  91. i_term = p_param.kp * p_param.ki * p_param.ts * ek;
  92. d_term = -p_param.kp * p_param.kd * (xk - 2 * xk_1 + xk_2) / p_param.ts;
  93. /* compute output */
  94. *yk += p_term + i_term + d_term;
  95. /* update sample data */
  96. xk_1 = xk;
  97. xk_2 = xk_1;
  98. /* clamp output adjustment range */
  99. if (*yk < -LIMIT_HIGH)
  100. *yk = -LIMIT_HIGH;
  101. else if (*yk > -LIMIT_LOW)
  102. *yk = -LIMIT_LOW;
  103. p_param.y_k = *yk;
  104. set_ctrl_state(lround(fabs(p_param.y_k)));
  105. }