外置式PID程序模板
#define MuBiaoCS 0 //目標常數(shù)
#define CHang_aCS 0 //比例常數(shù)
#define CHang_bCS 0 //積分常數(shù)
#define CHang_cCS 0 //微分常數(shù)
/*******************************************************************************************/
struct P_I_D {
int MuBiao; //設(shè)定目標 Desired Value
double CHang_a; //比例常數(shù) Proportional Const
double CHang_b; //積分常數(shù) Integral Const
doubleCHang_c; //微分常數(shù) 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 //目標常數(shù)
#define CHang_aCS 0 //比例常數(shù)
#define CHang_bCS 0 //積分常數(shù)
#define CHang_cCS 0 //微分常數(shù)
/*******************************************************************************************/
struct P_I_D {
int MuBiao; //設(shè)定目標 Desired Value
double CHang_a; //比例常數(shù) Proportional Const
double CHang_b; //積分常數(shù) Integral Const
doubleCHang_c; //微分常數(shù) 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();
}
評論