#include <stdio.h>
#include <stdlib.h>
float point =200.0;
typedef struct _pid{
float setpoint; //设定值
float nowpoint; //当前值
float error; //设定值与当前值的误差
float error_before; //上一个误差
float error_last; //上上一个误差
float kp,ki,kd; //比例,积分,微分三个参数
}PID;
PID pid;
void PID_init(){ //pid参数初始化
pid.setpoint=0.0;
pid.nowpoint=0.0;
pid.error=0.0;
pid.error_before=0.0;
pid.error_last=0.0;
pid.kp=0.2; //三个参数可通过MATLAB调参调为最优
pid.ki=0.015;
pid.kd=0.2;
}
float PID_update(){ //PID算法更新
pid.setpoint=point;
pid.error=pid.setpoint-pid.nowpoint;
float SUM=pid.kp*(pid.error-pid.error_before)+pid.ki*pid.error+pid.kd*(pid.error-2*pid.error_before+pid.error_last); //增量式PID公式
pid.nowpoint+=SUM;
pid.error_last=pid.error_before; // 误差更新
pid.error_before=pid.error;
return pid.nowpoint;
}
int main()
{
int count=0;
PID_init();
while(count<1000) //范围可自行设定
{
float actually=PID_update();
printf("nowpoint is %f\n",actually);
printf("setpoint is %f\n",point);
count++;
}
return 0;
}
增量式PID示例算法
最新推荐文章于 2024-09-12 19:02:20 发布