| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- #include "pid.h"
- #include <QtMath>
- PID::PID()
- {
- //-----------------------------------------
- // PID参数
- //-----------------------------------------
- kp = 4.0;
- ki = 0.02;
- kd = 2.0;
- integral = 0;
- lastError = 0;
- // 积分最大值
- integralMax = 100;
- }
- //====================================================
- // 加热控制
- //====================================================
- double PID::calculate(double target, double current)
- {
- // PID执行周期
- const double dt = 0.5;
- double error = target - current;
- //-----------------------------------------
- // 1. 65℃以前
- //
- // 快速升温
- //-----------------------------------------
- if(current < 65.0)
- {
- integral = 0;
- lastError = error;
- return 100.0;
- }
- //-----------------------------------------
- // 2. 65~68℃
- //
- // 开始提前降低加热功率
- //
- // 65℃ -> 80%
- // 68℃ -> 40%
- //-----------------------------------------
- if(current < 68.0)
- {
- integral = 0;
- lastError = error;
- double duty = 80.0 - (current - 65.0) / 3.0 * 40.0;
- return qBound(0.0, duty, 100.0);
- }
- //-----------------------------------------
- // 3. 68℃以后
- //
- // 正式进入PID控温
- //-----------------------------------------
- //-----------------------------------------
- // 积分
- //-----------------------------------------
- integral += error * dt;
- integral = qBound(-integralMax,integral,integralMax);
- //-----------------------------------------
- // 微分
- //-----------------------------------------
- double derivative = (error - lastError) / dt;
- lastError = error;
- //-----------------------------------------
- // PID
- //-----------------------------------------
- double output =
- kp * error +
- ki * integral +
- kd * derivative;
- //-----------------------------------------
- // 4. 70℃附近
- //
- // 防止到达目标后继续大功率加热
- //-----------------------------------------
- if(current >= 70.0)
- {
- if(output > 25.0)
- output = 25.0;
- }
- //-----------------------------------------
- // 5. 71℃以后
- //
- // 进一步限制加热
- //-----------------------------------------
- if(current >= 71.0)
- {
- if(output > 10.0)
- output = 10.0;
- }
- //-----------------------------------------
- // 6. 72℃保护
- //
- // 超过控制范围,停止加热
- //-----------------------------------------
- if(current >= 72.0)
- {
- output = 0.0;
- // 清除积分,避免降温后仍然异常
- integral = 0;
- }
- //-----------------------------------------
- // 最终限幅
- //-----------------------------------------
- return qBound(0.0, output, 100.0);
- }
- //====================================================
- // 风扇控制
- //====================================================
- double PID::calculateFanDuty(double temp)
- {
- double duty;
- //-----------------------------------------
- // 68℃以前
- //
- // 固定10%
- //-----------------------------------------
- if(temp < 68.0)
- {
- duty = 10.0;
- }
- //-----------------------------------------
- // 68~72℃
- //
- // 10% -> 60%
- //-----------------------------------------
- else if(temp <= 72.0)
- {
- duty = 10.0 + (temp - 68.0) / 4.0 * 50.0;
- }
- //-----------------------------------------
- // 72~75℃
- //
- // 60% -> 100%
- //-----------------------------------------
- else if(temp <= 75.0)
- {
- duty = 60.0 + (temp - 72.0) / 3.0 * 40.0;
- }
- //-----------------------------------------
- // 75℃以上
- //
- // 全速
- //-----------------------------------------
- else
- {
- duty = 100.0;
- }
- return qBound(10.0, duty, 100.0);
- }
- //====================================================
- // 第二组风扇控制
- //====================================================
- double PID::calculateFanDuty1(double temp)
- {
- double duty;
- //-----------------------------------------
- // 30℃以下
- //-----------------------------------------
- if(temp <= 30.0)
- {
- duty = 10.0;
- }
- //-----------------------------------------
- // 30~40℃
- //
- // 0 -> 50%
- //-----------------------------------------
- else if(temp <= 40.0)
- {
- duty = (temp - 30.0) / 10.0 * 40.0;
- }
- //-----------------------------------------
- // 40~45℃
- //
- // 50 -> 100%
- //-----------------------------------------
- else if(temp <= 45.0)
- {
- duty = 50.0 + (temp - 40.0) / 5.0 * 50.0;
- }
- //-----------------------------------------
- // 45℃以上
- //-----------------------------------------
- else
- {
- duty = 100.0;
- }
- return qBound(0.0, duty, 100.0);
- }
|