舵机的PD控制

PID

舵机以及差速PD调节

struct PID
{
    uint8 SetPoint;  //设定目标 Desired Value
    uint16 SetSpeed;
    uint16 SumError; //误差累计
    float P;   //比例常数 Proportional Const
    float I;   //积分常数 Integral Const
    float D;   //微分常数 Derivative Const
    float LastError; //Error[-1]
    float PrevError; //Error[-2]
};
struct PID sPID;
struct PID *sptr = &sPID;

void steer_PIDInit(void)   //,常用pd控制
{
    //  sptr->SumError = 0;
    sptr->LastError = 0;    //Error[-1]
    sptr->PrevError = 0;    //Error[-2]
    sptr->P = 5;            //比例常数 Proportional Const
//  sptr->I = 0.5;          //积分常数 Integral Const
    sptr->D = 3;        //微分常数 Derivative Const
    sptr->SetPoint = 40;    //期望中心
}

int16 Steer_PIDCalc(int8 Steer_CenterLine)//位置式PID
{
    iError = sptr->SetPoint - Steer_CenterLine;//增量计算
    iIncpid =(int16)(sptr->P * iError+ sptr->D * (iError - sptr->LastError));
    //限幅
    if(iIncpid > MAX_STEER_DUTY)    iIncpid = MAX_STEER_DUTY;
    if(iIncpid < MIN_STEER_DUTY)    iIncpid = MIN_STEER_DUTY;
    sptr->LastError = iError;//存储误差,用于下次计算
    return (iIncpid);
}

可以加企鹅群 658476482 交流

你可能感兴趣的:(单片机)