//*****************************************************************************
//!\brief The speed PID control logic
//!\param the set speed and the actual speed
//!\retval the offset value of the speed
//*****************************************************************************
LONG SpeedPID_Ctrl(LONG ref,LONG fdb)
{
SpeedPID.Ref = ref; // the set speed
SpeedPID.Fdb = fdb; // the actual speed
//-----Error-------------
SpeedPID.Err = SpeedPID.Ref - SpeedPID.Fdb;
//
SpeedPID.Uout = SpeedPID.UoutBak + SpeedPID.Kp * (SpeedPID.Err - SpeedPID.ErrBak) + SpeedPID.Ki * SpeedPID.Err;
//
SpeedPID.ErrBak = SpeedPID.Err;
//
if(SpeedPID.Uout > SpeedPID.OutMax)
{
SpeedPID.Uout = SpeedPID.OutMax; // set the maximum of Uout
}
else if(SpeedPID.Uout < SpeedPID.OutMin)
{
SpeedPID.Uout = SpeedPID.OutMin; // set the minimum of Uout
}
//
SpeedPID.UoutBak = SpeedPID.Uout;
//
SpeedPID.Uout >>= 17;