PID
来自Jack's Lab
(版本间的差异)
(→Overview) |
|||
| 第36行: | 第36行: | ||
return output; | return output; | ||
| + | } | ||
| + | </source> | ||
| + | |||
| + | |||
| + | 改进型 PI 控制(消除积分饱和): | ||
| + | <soure lang=cpp> | ||
| + | // 条件积分法 | ||
| + | if(!((output >= max_limit && error > 0) || | ||
| + | (output <= min_limit && error < 0))) { | ||
| + | integral += error; | ||
| + | } | ||
| + | |||
| + | // 积分分离法 | ||
| + | if(fabs(error) > threshold) { | ||
| + | // 只用比例控制 | ||
| + | output = Kp * error; | ||
| + | } else { | ||
| + | // 加入积分 | ||
| + | integral += error; | ||
| + | output = Kp * error + Ki * integral; | ||
} | } | ||
</source> | </source> | ||
2026年1月29日 (四) 11:23的版本
1 Overview
typedef struct {
float Kp; // 比例系数
float Ki; // 积分系数
float Kd; // 微分系数
float integral; // 积分项
float prev_error; // 上次误差
float setpoint; // 目标电流
} PID_Controller;
float PID_Calculate(PID_Controller *pid, float actual)
{
float error = pid->setpoint - actual;
// 比例项
float p_out = pid->Kp * error;
// 积分项(防饱和)
pid->integral += error;
if(pid->integral > 1000) pid->integral = 1000;
if(pid->integral < -1000) pid->integral = -1000;
float i_out = pid->Ki * pid->integral;
// 微分项
float d_out = pid->Kd * (error - pid->prev_error);
pid->prev_error = error;
// 输出
float output = p_out + i_out + d_out;
// 限幅
if(output > 0.95) output = 0.95;
if(output < 0.05) output = 0.05;
return output;
}
改进型 PI 控制(消除积分饱和):
<soure lang=cpp>
// 条件积分法
if(!((output >= max_limit && error > 0) ||
(output <= min_limit && error < 0))) {
integral += error;
}
// 积分分离法 if(fabs(error) > threshold) {
// 只用比例控制 output = Kp * error;
} else {
// 加入积分 integral += error; output = Kp * error + Ki * integral;
} </source>
2 Reference