float PID_DATA1(float speed,float snum)//speed 期望速度,snum 实测速度
{
float incrementSpeed; //当前值
pid.SetSpeed = speed;//设定的期望速度
pid.err = pid.SetSpeed - snum; //偏差值= 设定的速度-实测的速度
pid.s_error+=pid.err;//偏差积累
incrementSpeed = (pid.Kp + pid.Ki + pid.Kd)*pid.err - ( pid.Kp + 2* pid.Kd)*pid.err_next+pid.Kd*pid.err_last;//计算
pid.ActualSpeed += incrementSpeed;//实际输出值
pid.err_last = pid.err_next;
pid.err_next = pid.err;
if( pid.ActualSpeed>pid.SetSpeedMAX){ pid.ActualSpeed=pid.SetSpeedMAX;}
if( pid.ActualSpeed<-pid.SetSpeedMAX){ pid.ActualSpeed=-pid.SetSpeedMAX;}
return pid.
}
float PID_DATA2(float speed,float snum)//speed 期望位置,snum 实测位置
{
float incrementSpeed; //当前值
pid.SetSpeed = speed;//设定的期望位置
pid.err = pid.SetSpeed - snum; //偏差值= 设定的位置-实测的位置
pid.s_error+=pid.err;//偏差积累
incrementSpeed = (pid.Kp + pid.Ki + pid.Kd)*pid.err - ( pid.Kp + 2* pid.Kd)*pid.err_next+pid.Kd*pid.err_last;//计算
pid.ActualSpeed = incrementSpeed;//实际输出值
pid.err_last = pid.err_next;
pid.err_next = pid.err;
if( pid.ActualSpeed>pid.SetSpeedMAX){ pid.ActualSpeed=pid.SetSpeedMAX;}
if( pid.ActualSpeed<-pid.SetSpeedMAX){ pid.ActualSpeed=-pid.SetSpeedMAX;}
return pid.ActualSpeed;
}
|