链接:https://pan.baidu.com/s/1BNYtjg_hN-w5in9_xq4EUA
提取码:1234
#include "pid.h"
extern bit heatFlag; //加热启动标志
extern bit coolFlag; //制冷启动标志
#if 0
PID_State_Def PID_Iterate(PID_Calibration_Def calibration, PID_State_Def state)
{
float error;
float derivative;
// calculate difference between desired and actual values (the error)
error = state.target - state.actual;
// calculate and update integral
state.integral += (error);
/* 积分限幅 */
if (state.integral >= 1000)
{
state.integral = 1000;
}
// calculate derivative
derivative = (error - state.previous_error);
// calculate output value according to algorithm
state.output = (
(calibration.kp * error) + (calibration.ki * state.integral) + (calibration.kd * derivative)
);
if (state.output >= 100)
{
state.output = 100;
}
else if (state.output <= 0)
{
state.output = 0;
}
// update state.previous_error to the error value calculated on this iteration
state.previous_error = error;
// return the state struct reflecting the calculations
return state;
}
#else
PID_State_Def PID_Increament(PID_Calibration_Def calibration, PID_State_Def state)
{
float error;
// calculate difference between desired and actual values (the error)
if (heatFlag)
{
error = state.target - state.actual;
}
if (coolFlag)
{
error = state.actual - state.target;
}
// calculate output value according to algorithm
state.output += (
(calibration.kp * (error - state.last_error)) + (calibration.ki * error)\
+ calibration.kd * (error - 2 * state.last_error + state.previous_error)
);
if (state.output >= 100)
{
state.output = 100;
}
else if (state.output <= 0)
{
state.output = 0;
}
// update state.previous_error to the error value calculated on this iteration
state.previous_error = state.last_error;
state.last_error = error;
// return the state struct reflecting the calculations
return state;
}
#endif