您的位置:首页 > 编程语言 > PHP开发

PID调节基础程序模版

2011-01-16 11:46 239 查看
 

Code:

#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;  
  
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));  
}  
  
/*==================================================================================================== 
主函数 
=====================================================================================================*/  
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  
}  
}  

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  input output