C语言理解代码:
//创建变量结构体
struct pid_type {
float Kp; //PI调节的比例常数
float Ti; //PI调节的积分常数
float T; //采样周期
float Ki;
float ek; //偏差e[k]
float ek1; //偏差e[k-1]
float ek2; //偏差e[k-2]
float uk; //u[k]
signed int uk1; //对u[k]四舍五入取整
signed int adjust; //调节器输出调整量
} pid;
//变量初始
void Pid_Init(void)
{
pid.Kp=4;
pid.Ti=0.005;
pid.T=0.001;
pid.Ki=0.6; //微分系数Kd=KpTd/T。根据实际调节
pid.ek=0;
pid.ek1=0;
pid.ek2=0;
pid.uk=0;
pid.uk1=0;
pid.adjust=0;
}
int PIDadjust(float ek) //PI调节算法
{
if( gabs(ek)<0.1 )
{
pid.adjust=0;
}
else
{
pid.uk=pid.Kp*(pid.ek-pid.ek1)+pid.Ki*pid.ek; //计算控制增量
pid.ek1=pid.ek;
pid.uk1=(signed int)pid.uk;
if(http://pid.uk>0)
{
if(piduk-piduk1>=0.5)
{
pi.uk1=pid.uk1+1;
}
}
if(piduk<0)
{
if(http://pid.uk1-pid.uk>=0.5)
{
pid.uk1=pid.uk1-1;
}
}
adjust=pid.uk1;
}
return adjust;
}
|