永发信息网

求一下增量式和位置式PID的C语言程序

答案:2  悬赏:80  手机版
解决时间 2021-11-20 11:34
  • 提问者网友:情歌越听越心酸
  • 2021-11-20 01:17
求一下增量式和位置式PID的C语言程序
最佳答案
  • 五星知识达人网友:一叶十三刺
  • 2021-11-20 01:37
增量式PID:
typedef struct{  
    float scope;        //输出限幅量  
    float aim;       //目标输出量  
    float real_out;     //实际输出量   
    float Kp;     
    float Ki;     
    float Kd;     
    float e0;          //当前误差  
    float e1;          //上一次误差  
    float e2;          //上上次误差  
}PID_Type;  

#define min(a, b)           (a#define max(a, b)           (a>b? a:b)  
#define limiter(x, a, b)      (min(max(x, a), b))  
#define exchange(a, b, tmp) (tmp=a, a=b, b=tmp)  
#define myabs(x)            ((x<0)? -x:x)  
  
float pid_acc(PID_Type *pid)  
{  
    float out;  
    float ep, ei, ed;  
      
    pid->e0 = pid->aim - pid->real_out;  
    ep = pid->e0  - pid->e1;  
    ei = pid->e0;  
    ed = pid->e0 - 2*pid->e1 + pid->e2;  
    out = pid->Kp*ep + pid->Ki*ei + pid->Kd*ed;  
    out = limiter(out, -pid->scope, pid->scope);  
    pid->e2 = pid->e1;  
    pid->e1 = pid->e0;  
    return out;  
}位置式PID:
typedef struct{  
    float scope;    //输出限幅量  
    float aim;   //目标输出量  
    float real_out; //反馈输出量  
    float Kp;         
    float Ki;  
    float Kd;  
    float Sum;  
    float e0;       //当前误差  
    float e1;       //上一次误差  
}PID_Type;  
  
#define max(a, b)           (a>b? a:b)  
#define min(a, b)           (a#define limiter(x, a, b)      (min(max(x, a), b))  
  
float pid_pos(PID_Type *p)  
{  
    float pe, ie, de;  
    float out = 0;  
  
    p->e0 = p->aim - p->real_out;      //计算当前误差    
  
    p->Sum += p->e0;       //误差积分  
  
    de = p->e0 - p->e1;     //误差微分  
  
    pe = p->e0;  
    ie = p->Sum;  
  
    p->e1 = p->e0;  
  
    out = pe*(p->Kp) + ie*(p->Ki) + de*(p->Kd);  
  
    out = limiter(out, -p->scope, p->scope);       //输出限幅
    return out;  
}亲手移植到我的stm32小车上 调试3个参数后正常使用。
全部回答
  • 1楼网友:洒脱疯子
  • 2021-11-20 01:47
给我说,吧追问这两个有什么区别,什么时候用哪个
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯