PID增量控制
#include"Pid_Init.h"
#include"main.h"
extern u16 out;
extern u16 CurSpeed;
extern u16 SpeedSet;
extern u16 PWMTime;
int error=0; //当前偏差
int error1=0; //上次偏差
int error2=0; //上上次偏差
struct Pid_Struct{
float kp;
float ki;
float kd; //pid控制系数
float uk; //当前增量输出
float uk1; //上次增量输出
float uk2; //上上次增量输出
};
struct Pid_Struct PID;
//PID参数初始化
void PID_Init(){
PID.kp=15;
PID.ki=10;
PID.kd=3;
PID.uk=0;
PID.uk1=0;
PID.uk2=0;
}
//PID增量算法控制转速
void PID_Control(){
error=SpeedSet-CurSpeed;//
PID.uk2=(PID.kp*(error-error1) \
+PID.ki*error \
+PID.kd*(error-2*error1+error2))/50;
/*增量PID算法*/
PID.uk=PID.uk1+PID.uk2;
out=(int)PID.uk;
if(out>1000)
{
out=1000;
}
else if(out<0)
{
out=0;
}
PID.uk1=PID.uk;
error2=error1;
error1=error;
PWMTime=out;
}
盗了一个你 发布了48 篇原创文章 · 获赞 3 · 访问量 1万+ 私信 关注