📄 pid.txt
字号:
***************************************************************************\ PID FunctionThis program has been written by the Technical Support Staff at Z-World inresponse to several customer requests. As such, it has NOT had the testing andvalidation procedures which our "standard" software products have. It is beingmade 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 ){ 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 }}这个PID的算法只能满足一般控制的场合,如果您要用于实际工程的话,还需要根据实际需求进行修改。
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -