pid.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #include "pid.h"
  2. #include <QtMath>
  3. PID::PID()
  4. {
  5. //-----------------------------------------
  6. // PID参数
  7. //-----------------------------------------
  8. kp = 4.0;
  9. ki = 0.02;
  10. kd = 2.0;
  11. integral = 0;
  12. lastError = 0;
  13. integralMax = 100;
  14. }
  15. //====================================================
  16. // 加热控制
  17. //====================================================
  18. double PID::calculate(double target, double current)
  19. {
  20. const double dt = 0.5;
  21. double error = target - current;
  22. //-----------------------------------------
  23. // 70℃以前
  24. //
  25. // 全功率快速升温
  26. //-----------------------------------------
  27. if(current <= 70.0)
  28. {
  29. integral = 0;
  30. lastError = error;
  31. return 100.0;
  32. }
  33. //-----------------------------------------
  34. // 70~77℃
  35. //
  36. // PID控温
  37. //-----------------------------------------
  38. if(current < 77.0)
  39. {
  40. //-----------------------------------------
  41. // 积分
  42. //-----------------------------------------
  43. integral += error * dt;
  44. integral = qBound(-integralMax,
  45. integral,
  46. integralMax);
  47. //-----------------------------------------
  48. // 微分
  49. //-----------------------------------------
  50. double derivative =
  51. (error - lastError) / dt;
  52. lastError = error;
  53. //-----------------------------------------
  54. // PID输出
  55. //-----------------------------------------
  56. double output =
  57. kp * error +
  58. ki * integral +
  59. kd * derivative;
  60. //-----------------------------------------
  61. // 71℃以后限制功率
  62. //-----------------------------------------
  63. if(current >= 72.0)
  64. {
  65. if(output > 50.0)
  66. output = 50.0;
  67. }
  68. //-----------------------------------------
  69. // 75℃以后进一步限制
  70. //-----------------------------------------
  71. if(current >= 75.0)
  72. {
  73. if(output > 20.0)
  74. output = 20.0;
  75. }
  76. return qBound(0.0,
  77. output,
  78. 100.0);
  79. }
  80. //-----------------------------------------
  81. // 77℃保护
  82. //-----------------------------------------
  83. integral = 0;
  84. lastError = error;
  85. return 0.0;
  86. }
  87. //====================================================
  88. // 第一组风扇控制
  89. //====================================================
  90. double PID::calculateFanDuty(double temp)
  91. {
  92. double duty;
  93. //-----------------------------------------
  94. // 70℃以前
  95. //
  96. // 小风量均温
  97. //-----------------------------------------
  98. if(temp <= 70.0)
  99. {
  100. duty = 20.0;
  101. }
  102. //-----------------------------------------
  103. // 70~72℃
  104. //
  105. // 20 -> 60%
  106. //-----------------------------------------
  107. else if(temp <= 72.0)
  108. {
  109. duty =
  110. 20.0 +
  111. (temp - 70.0) / 2.0 * 40.0;
  112. }
  113. //-----------------------------------------
  114. // 72~75℃
  115. //
  116. // 60 -> 100%
  117. //-----------------------------------------
  118. else if(temp <= 75.0)
  119. {
  120. duty =
  121. 60.0 +
  122. (temp - 72.0) / 3.0 * 40.0;
  123. }
  124. //-----------------------------------------
  125. // 75℃以上
  126. //-----------------------------------------
  127. else
  128. {
  129. duty = 100.0;
  130. }
  131. return qBound(20.0,
  132. duty,
  133. 100.0);
  134. }
  135. //====================================================
  136. // 第二组风扇控制
  137. // 保持原逻辑
  138. //====================================================
  139. double PID::calculateFanDuty1(double temp)
  140. {
  141. double duty;
  142. //-----------------------------------------
  143. // 30℃以下
  144. //-----------------------------------------
  145. if(temp <= 30.0)
  146. {
  147. duty = 10.0;
  148. }
  149. //-----------------------------------------
  150. // 30~40℃
  151. //-----------------------------------------
  152. else if(temp <= 40.0)
  153. {
  154. duty =
  155. (temp - 30.0) / 10.0 * 40.0;
  156. }
  157. //-----------------------------------------
  158. // 40~45℃
  159. //-----------------------------------------
  160. else if(temp <= 45.0)
  161. {
  162. duty =
  163. 50.0 +
  164. (temp - 40.0) / 5.0 * 50.0;
  165. }
  166. //-----------------------------------------
  167. // 45℃以上
  168. //-----------------------------------------
  169. else
  170. {
  171. duty = 100.0;
  172. }
  173. return qBound(0.0,
  174. duty,
  175. 100.0);
  176. }