歡迎您光臨本站 登入註冊首頁

典型的PID處理程序

admin @ 2014-03-26 , reply:0

概述

*===================================================================================================……

*============================ ========================================================================
這是一個比較典型的PID處理程序,在使用單片機作為控制cpu時,請稍作簡化,具體的PID參數必須由具體對象通過實驗確定。由於單片機的處理速度和 ram資源的限制,一般不採用浮點數運算,而將所有參數全部用整數,運算
到最後再除以一個2的N次方數據(相當於移位),作類似定點數運算,可大大提高運算速度,根據控制精度的不同要求,當精度要求很高時,注意保留移位引起的“餘數”,做好餘數補償。這個程序只是一般常用pid演算法的基本架構,沒有包含輸入輸出處理部分。
=====================================================================================================*/
#include <string.h>
#include <stdio.h>
/*====================================================================================================
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; // 設定目標Desired value
double Proportion; // 比例常數Proportional Const
double Integral; // 積分常數Integral Const
double Derivative; // 微分常數Derivative Const
double LastError; // Error[-1]

double PrevError; // Error[-2]
double SumError; // Sums of Errors
} PID;
/*====================================================================================================
PID計算部分
=====================================================================================================*/
double PIDCalc( PID *pp, double NextPoint )
{
double dError,
Error;
Error = pp->SetPoint - NextPoint; // 偏差
pp->SumError += Error; // 積分
dError = pp->LastError - pp->PrevError; // 當前微分
pp->PrevError = pp->LastError;
pp->LastError = Error;
return (pp->Proportion * Error // 比例項
+ pp->Integral * pp->SumError // 積分項
+ pp->Derivative * dError // 微分項
);
}
/*====================================================================================================
Initialize PID Structure
=====================================================================================================*/
void PIDInit (PID *pp)
{
memset ( pp,0,sizeof(PID));
}
/*====================================================================================================
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)
double rIn; // PID Feedback (Input)
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
}


[admin via 研發互助社區 ] 典型的PID處理程序已經有9374次圍觀

http://cocdig.com/docs/show-post-45164.html