发表于:2005-12-15 10:50:00
楼主
***************************************************************************\
PID Function
This program has been written by the Technical Support Staff at Z-World in
response to several customer requests. As such, it has NOT had the testing and
validation procedures which our "standard" software products have. It is being
made available as a sample. There is no warranty, implied or otherwise.
The PID (Proportional Integral Derivative) 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;
/*=========================================================================*\
Perform One PID Iteration
\*=========================================================================*/
double PIDCalc ( PID *pp, double NextPoint )
{ double dError, Error;
pp->SumError += (Error = pp->SetPoint - NextPoint);
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 )
{&