?? 比較典型的pid算法控制程序源代碼.txt
字號(hào):
比較典型的PID處理程序
來(lái)源:21ICbbs 作者:lookuper
/*====================================================================================================這是一個(gè)比較典型的PID處理程序,在使用單片機(jī)作為控制cpu時(shí),請(qǐng)稍作簡(jiǎn)化,具體的PID參數(shù)必須由具體對(duì)象通過實(shí)驗(yàn)確定。由于單片機(jī)的處理速度和ram資源的限制,一般不采用浮點(diǎn)數(shù)運(yùn)算,而將所有參數(shù)全部用整數(shù),運(yùn)算
到最后再除以一個(gè)2的N次方數(shù)據(jù)(相當(dāng)于移位),作類似定點(diǎn)數(shù)運(yùn)算,可大大提高運(yùn)算速度,根據(jù)控制精度的不同要求,當(dāng)精度要求很高時(shí),注意保留移位引起的“余數(shù)”,做好余數(shù)補(bǔ)償。這個(gè)程序只是一般常用pid算法的基本架構(gòu),沒有包含輸入輸出處理部分。
=====================================================================================================*/
#include
#include
/*====================================================================================================
PID Function
The PID (比例、積分、微分) function is used in mainly
control applications. PIDCalc performs one iteration of the PID
algorithm.
While the PID function works, main is just a dummy program showing
a typical usage.
=====================================================================================================*/
typedef struct PID {
double SetPoint; // 設(shè)定目標(biāo)Desired value
double Proportion; // 比例常數(shù)Proportional Const
double Integral; // 積分常數(shù)Integral Const
double Derivative; // 微分常數(shù)Derivative Const
double LastError; // Error[-1]
double PrevError; // Error[-2]
double SumError; // Sums of Errors
} PID;
/*====================================================================================================
PID計(jì)算部分
=====================================================================================================*/
double PIDCalc( PID *pp, double NextPoint )//PID算法的核心,積分項(xiàng),微分項(xiàng)和比例項(xiàng)
{
double dError,
Error;
Error = pp->SetPoint - NextPoint; // 偏差
pp->SumError += Error; // 積分
dError = pp->LastError - pp->PrevError; // 當(dāng)前微分
pp->PrevError = pp->LastError;
pp->LastError = Error;
return (pp->Proportion * Error // 比例項(xiàng)
+ pp->Integral * pp->SumError // 積分項(xiàng)
+ pp->Derivative * dError // 微分項(xiàng)
);
}
/*====================================================================================================
Initialize PID Structure
=====================================================================================================*/
void PIDInit (PID *pp)
{
memset ( pp,0,sizeof(PID));//用C庫(kù)函數(shù)對(duì)PID結(jié)構(gòu)體進(jìn)行賦值
}
/*====================================================================================================
Main Program
=====================================================================================================*
double sensor (void) // Dummy Sensor Function
{
return 100.0;
}
void actuator(double rDelta) // Dummy Actuator Function
{}
void main(void)
{
PID sPID; // PID Control Structure
double rOut; // PID Response (Output),PID的輸出
double rIn; // PID Feedback (Input),PID的輸入反饋
PIDInit ( &sPID ); // Initialize Structure
sPID.Proportion = 0.5; // Set PID Coefficients
sPID.Integral = 0.5;
sPID.Derivative = 0.0;
sPID.SetPoint = 100.0; // Set PID Setpoint
for (;;) { // Mock Up of PID Processing
rIn = sensor (); // Read Input
rOut = PIDCalc ( &sPID,rIn ); // Perform PID Interation
actuator ( rOut ); // Effect Needed Changes
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -