《XS128PID控制》由會(huì)員分享,可在線閱讀,更多相關(guān)《XS128PID控制(5頁(yè)珍藏版)》請(qǐng)?jiān)谘b配圖網(wǎng)上搜索。
1、精品文檔,僅供學(xué)習(xí)與交流,如有侵權(quán)請(qǐng)聯(lián)系網(wǎng)站刪除
xs128-PID控制
采用增量式PID控制
//*****************************************************
//定義PID結(jié)構(gòu)體
//*****************************************************
typedef struct PID
{
int SetPoint; //設(shè)定目標(biāo) Desired Value
double Proportion; //比例常數(shù) Proportional Const
double Integra
2、l; //積分常數(shù) Integral Const
double Derivative; //微分常數(shù) Derivative Const
int LastError; //Error[-1]
int PrevError; //Error[-2]
} PID;
//*****************************************************
//定義相關(guān)宏
//*****************************************************
#define P_DATA 100
#define I_DATA 0.6
3、#define D_DATA 1
#define HAVE_NEW_VELOCITY 0X01
//*****************************************************
//聲明PID實(shí)體
//*****************************************************
static PID sPID;
static PID *sptr = &sPID;
//*****************************************************
//PID參數(shù)初始化
//*********
4、********************************************
void IncPIDInit(void)
{
sptr->LastError = 0; //Error[-1]
sptr->PrevError = 0; //Error[-2]
sptr->Proportion = P_DATA; //比例常數(shù) Proportional Const
sptr->Integral = I_DATA; //積分常數(shù)Integral Const
sptr->Derivative = D_DATA; //微分常數(shù) Derivative Const
s
5、ptr->SetPoint =100; //目標(biāo)是100
}
//*****************************************************
//增量式PID控制設(shè)計(jì)
//*****************************************************
int IncPIDCalc(int NextPoint)
{
int iError, iIncpid; //當(dāng)前誤差
iError = sptr->SetPoint - NextPoint; //增量計(jì)算
iIncpid = sptr->Proportion *
6、 iError //E[k]項(xiàng)
- sptr->Integral * sptr->LastError //E[k-1]項(xiàng)
+ sptr->Derivative * sptr->PrevError; //E[k-2]項(xiàng)
sptr->PrevError = sptr->LastError;
//存儲(chǔ)誤差,用于下次計(jì)算
sptr->LastError = iError;
return(iIncpid);
//返回增量值
}
Int g_CurrentVelocity;
Int g_Flag;
void main(void)
{
DisableInterr
7、upt
InitMCu();
IncPIDInit();
g_CurrentVelocity=0;
//全局變量也初始化
g_Flag=0;
//全局變量也初始化
EnableInterrupt;
While(1)
{
if (g_Flag& HAVE_NEW_VELOCITY)
PWMOUT+= IncPIDCalc(CurrentVelocity);
g_Flag&=~ HAVE_NEW_VELOCITY;
}
}
}
//****************************************
//采樣周期T
//****************************************
Interrrupt TIME void
{
CurrentVelocity =GetCurrentVelocity;
g_Flag|= HAVE_NEW_VELOCITY;
}
采樣周期后.時(shí)鐘中斷 獲取當(dāng)前速度.更新標(biāo)志g_Flag.調(diào)用IncPIDClac()計(jì)算增量.更新PWM.
【精品文檔】第 5 頁(yè)