|
#include<string.h>
#include<stdio.h>
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結構體變量初始化函數
=====================================================================================================*/
void PIDInit (PID *pp)
{
memset ( pp,0,sizeof(PID));
}
/*====================================================================================================
讀取輸入變量函數(在此設定為固定值100)
======================================================================================================*/
double sensor (void)
{
return 100.0;
}
/*====================================================================================================
輸出變量控制函數
======================================================================================================*/
void actuator(double rDelta)
{
}
//主函數
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 ); // PID計算函數(Perform PID Interation)
actuator ( rOut ); // 輸出變量控制函數(Effect Needed Changes)
}
}
|
-
-
PID算法代碼.zip
2017-4-19 15:24 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
20.2 KB, 下載次數: 598, 下載積分: 黑幣 -5
PID算法的代碼,輸出函數要根據項目來編寫
評分
-
查看全部評分
|