PID

来自Jack's Lab
(版本间的差异)
跳转到: 导航, 搜索
(Overview)
(Overview)
 
第41行: 第41行:
  
 
改进型 PI 控制(消除积分饱和):
 
改进型 PI 控制(消除积分饱和):
<soure lang=cpp>
+
<source lang=cpp>
 
// 条件积分法
 
// 条件积分法
 
if(!((output >= max_limit && error > 0) ||  
 
if(!((output >= max_limit && error > 0) ||  
第56行: 第56行:
 
     integral += error;
 
     integral += error;
 
     output = Kp * error + Ki * integral;
 
     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 控制(消除积分饱和):

// 条件积分法
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;



[编辑] 2 Reference







个人工具
名字空间

变换
操作
导航
工具箱