整理一些PID程序,欢迎大家指正.
这段时间搞PID,整理了一些程序:1.这个程序是本网站的,把PID部分复制到GCC可以编译通过,但是这个程序是位置式的,
=====================================================================================================*/
#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 {
doubleSetPoint; //设定目标 Desired Value
doubleProportion; //比例常数 Proportional Const
doubleIntegral; //积分常数 Integral Const
doubleDerivative; //微分常数 Derivative Const
doubleLastError; //Error[-1]
doublePrevError; //Error[-2]
doubleSumError; //Sums of Errors
} PID;
/*====================================================================================================
PID计算部分
=====================================================================================================*/
double PIDCalc( PID *pp, double NextPoint )
{
doubledError,
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
}
}
2.增量式的,参照这个程序:
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 (struct PID *pp,double NextPoint)
{
double dError,Error;
Error = pp->SetPoint - NextPoint; //偏差
pp->SumError += Error; //积分
dError =pp->SumError-2*pp->LastError + pp->PrevError;
//当前微分
pp->PrevError = pp->LastError;
pp->LastError = Error;
return (pp->Proportion * Error //比例项
+pp->Integral * pp->SumError //积分项
+pp->Derivative * dError // 微分项
);
其实一个是Uk,一个是Uk-Uk-1, MARK 赞一个 mark mark 现在发现高数TMD太重要了。以前的高数也不知道怎么考的,真有种马上冲出去买本高数的冲动。 Mark 学习了~ 谢谢 MARK一下。 MARK mark mark mark mark mark ark 先收藏,以后参考下啊。。。。 mark PIDo ^ dError =pp->SumError-2*pp->LastError + pp->PrevError在这里。 mark 温习温习 mark mark mark mark mark............. {:tongue:}留个标记。。
mark
页:
[1]