位置式PID控制演算法

來源:互聯網
上載者:User
剛好前不久搞過PID,部分程式如下,僅供參考

/*==============================================================================
在使用單片機作為控制cpu時,請稍作簡化,具體的PID參數必須由具體對象通過實驗確定。
由於單片機的處理速度和ram資源的限制,一般不採用浮點數運算,而將所有參數全部用整數,
運算到最後再除以一個2的N次方資料(相當於移位),作類似定點數運算,可大大提高運算速度,
根據控制精度的不同要求,當精度要求很高時,注意保留移位引起的“餘數”,做好餘數補償。
這個程式只是一般常用pid演算法的基本架構,沒有包含輸入輸出處理部分。
==============================================================================*/

#include <string.h>
#include <stdio.h>


/*===============================================================================
PID Function
The PID function is used in mainly
control applications. PID Calc performs one iteration of the PID
algorithm.
While the PID function works, main is just a dummy program showing
a typical usage.

PID功能
在PID功能主要用於控制應用。 PID 計算機執行一個PID的迭代演算法。雖然PID功能的工程,
主要只是一個虛擬程式顯示一個典型的使用。
================================================================================*/
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 // 微分項
);
}

/*======================= 初始化的PID結構 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控制結構 PID Control Structure
double rOut; // PID響應(輸出) PID Response (Output)
double rIn; // PID反饋(輸入) PID Feedback (Input)
PIDInit ( &sPID ); // 初始化結構 Initialize Structure
sPID.Proportion = 0.5; // 設定PID係數 Set PID Coefficients
sPID.Integral = 0.5;
sPID.Derivative = 0.0;
sPID.SetPoint = 100.0; // 設定PID設定 Set PID Setpoint
for (;;)
{ // 類比最多的PID處理 Mock Up of PID Processing
rIn = sensor (); // 讀取輸入 Read Input
rOut = PIDCalc ( &sPID,rIn ); // 執行的PID迭代 Perform PID Interation
actuator ( rOut ); // 所需的更改的影響 Effect Needed Changes

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.