C语言算法之PID算法

//增量式PID
typedef struct{
    int SetPoint;        //目标值
    long SumError;        //误差累计
    
    float P;            //比例常数
    float I;            //积分常数
    float D;            //微分常数
    
    int LastError;        //上次偏差值
    int PrevError;        //上上次偏差值
}PID;
 
/*************************************************
 *函数名称:void PID_Arg_Init(PID* sptr)                              
 *功    能:参数初始化                                     
 *参    数:PID* sptr                                     
 *返 回 值:void                                       
 *************************************************/
void PID_Arg_Init(PID* sptr)
{
    sptr->SumError = 0;         //误差累计 
    sptr->LastError = 0;        //上次偏差值
    sptr->PrevError = 0;        //上上次偏差值
                                
    sptr->P = 0;                //比例常数
    sptr->I = 0;                //积分常数
    sptr->D = 0;                //微分常数
                                
    sptr->SetPoint = 0;            //目标值
}
/*************************************************
 *函数名称:int PID_Contrl(PID* sptr,int NextPoint)                             
 *功    能:PID控制                                   
 *参    数:PID* sptr:上次参数  int NextPoint:当前实际值                                   
 *返 回 值:void                                       
 *************************************************/
int PID_Contrl(PID* sptr,int NextPoint)
{
    register int iError,iIncPid;
    
    iError = sptr->SetPoint - NextPoint;    //当前误差  设定的目标值和实际值的偏差
    
    iIncPid = sptr->P * iError                //增量计算
              - sptr->I * sptr->LastError
              + sptr->D * sptr->PrevError;
    
    /*存储误差  用于下次计算*/
    sptr->PrevError = sptr->LastError;
    sptr->LastError = iError;
    
    return iIncPid;                            //返回增量值
}
 
int main()
{
    int temp = 0;
    int add = 0;
 
    PID PID_Temp;
    PID_Arg_Init(&PID_Temp);
    for(int i = 0;i < 10;i++)
    {
        add = PID_Contrl(&PID_Temp,temp);
        temp += add;
 
        printf("add:%d\n",add);
        printf("temp:%d\n",temp);
        printf("PID.tmp:%d\n",PID_Temp.SetPoint);
        printf("PID.error:%d\n\n",PID_Temp.LastError);
        
    }
   return 0;
}

//版权声明:本文为CSDN博主「出奇」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
//原文链接:https://blog.csdn.net/qq_41673920/java/article/details/86493655

 

posted @ 2020-05-09 09:40  淘气开发者  阅读(943)  评论(0)    收藏  举报