專注電子技術學習與研究
當前位置:單片機教程網 >> MCU設計實例 >> 瀏覽文章

外置式PID程序模板

作者:藍夢荒   來源:本站原創(chuàng)   點擊數:  更新時間:2013年12月08日   【字體:

 

外置式PID模板

#define MuBiaoCS 0  //目標常數
#define CHang_aCS  0  //比例常數
#define CHang_bCS  0  //積分常數
#define CHang_cCS  0  //微分常數
/*******************************************************************************************/
struct P_I_D  {
    int MuBiao; //設定目標 Desired Value
    double CHang_a; //比例常數 Proportional Const
    double CHang_b; //積分常數 Integral Const
    double CHang_c; //微分常數 Derivative Const
    int Error1; //Error[-1]
    int Error2; //Error[-2]
   }Pidn;
struct P_I_D *PID=&Pidn;
/********************************************************************************************/
void Pidinit(void)    //pid初始化
{
 PID->MuBiao=MuBiaoCS;
 PID->CHang_a=CHang_aCS;
 PID->CHang_b=CHang_bCS;
 PID->CHang_c=CHang_cCS;
 PID->Error1=0;
 PID->Error2=0;
}

int PID_WZ(int SRuu)       //位置式PID
{
 int Error0,SCuu;
 Error0 = PID->MuBiao - SRuu;
 PID->Error1+=Error0;
 SCuu = PID->CHang_a*Error0    //比例項
   + PID->CHang_b*PID->Error1  //積分項
   + PID->CHang_c*(Error0-PID->Error2);//微分項
 PID->Error2=Error0;
 return     SCuu;
}
void main(void)
{
 Pidinit();
}

增量式PID模板

#define MuBiaoCS 0  //目標常數
#define CHang_aCS  0  //比例常數
#define CHang_bCS  0  //積分常數
#define CHang_cCS  0  //微分常數
/*******************************************************************************************/
struct P_I_D  {
    int MuBiao; //設定目標 Desired Value
    double CHang_a; //比例常數 Proportional Const
    double CHang_b; //積分常數 Integral Const
    double CHang_c; //微分常數 Derivative Const
    int Error1; //Error[-1]
    int Error2; //Error[-2]
   }Pidn;
struct P_I_D *PID=&Pidn;
/********************************************************************************************/
void Pidinit(void)    //pid初始化
{
 PID->MuBiao=MuBiaoCS;
 PID->CHang_a=CHang_aCS;
 PID->CHang_b=CHang_bCS;
 PID->CHang_c=CHang_cCS;
 PID->Error1=0;
 PID->Error2=0;
}

int PID_WC(int SRuu)         //增量式PID
{
 int Error0,SCuu;
 Error0 = PID->MuBiao - SRuu;    //偏差
 SCuu = PID->CHang_a*Error0    //Error項
   - PID->CHang_b*PID->Error1  //Error1項
   + PID->CHang_c*PID->Error2;     //Error2項
 PID->Error2=PID->Error1;     //將上次偏差存PID->Error2
 PID->Error1=Error0;       //將這次偏差存PID->Error1
 return SCuu;
}

void main(void)
{
 Pidinit();
}
 

關閉窗口

相關文章