pid.c 3.3 KB

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