比较典型的PID处理程序

楼主
比较典型的PID处理程序
作者:晓奇
复制代码
  1. /*====================================================================  
  2. 这是一个比较典型的PID处理程序,在使用单片机作为控制cpu时,请稍作简化,具体的PID参数必须由具体对象通过实验确定。由于单片机的处理速度和ram资源的限制,一般不采用浮点数运算,而将所有参数全部用整数,运算  
  3. 到最后再除以一个2的N次方数据(相当于移位),作类似定点数运算,可大大提高运算速度,根据控制精度的不同要求,当精度要求很高时,注意保留移位引起的“余数”,做好余数补偿。这个程序只是一般常用pid算法的基本架构,没有包含输入输出处理部分。  
  4. ===========================================================================*/  
  5. #include  
  6. #include  
  7. /*==========================================================================  
  8. PID Function  
  9. The PID (比例、积分、微分) function is used in mainly  
  10. control applications. PIDCalc performs one iteration of the PID  
  11. algorithm.  
  12. While the PID function works, main is just a dummy program showing  
  13. a typical usage.  
  14. ============================================================================*/  
  15. typedef struct PID {  
  16. double SetPoint;   // 设定目标Desired value  
  17. double Proportion; // 比例常数Proportional Const  
  18. double Integral;   // 积分常数Integral Const  
  19. double Derivative; // 微分常数Derivative Const  
  20. double LastError;  // Error[-1]  
  21.  
  22. double PrevError;  // Error[-2]  
  23. double SumError;   // Sums of Errors  
  24. } PID;  
  25.  
  26. /*========================================================================== 
  27. PID计算部分  
  28. ============================================================================*/  
  29. double PIDCalc( PID *pp, double NextPoint )  
  30. {  
  31. double dError,  
  32. Error;  
  33. Error = pp->SetPoint - NextPoint;       // 偏差  
  34. pp->SumError += Error;                  // 积分  
  35. dError = pp->LastError - pp->PrevError; // 当前微分  
  36. pp->PrevError = pp->LastError;  
  37. pp->LastError = Error;  
  38. return (pp->Proportion * Error   // 比例项  
  39. + pp->Integral * pp->SumError    // 积分项  
  40. + pp->Derivative * dError       // 微分项  
  41. );  
  42. }  
  43.  
  44. /*=========================================================================  
  45. Initialize PID Structure  
  46. ===========================================================================*/  
  47. void PIDInit (PID *pp)  
  48. {  
  49. memset ( pp,0,sizeof(PID));  
  50. }  
  51.  
  52. /*===========================================================================  
  53. Main Program  
  54. =============================================================================*/  
  55. double sensor (void)              // Dummy Sensor Function  
  56. {  
  57. return 100.0;  
  58. }  
  59. void actuator(double rDelta)        // Dummy Actuator Function  
  60. {}  
  61. void main(void)  
  62. {  
  63. PID sPID;                          // PID Control Structure  
  64. double rOut;                       // PID Response (Output)  
  65. double rIn;                        // PID Feedback (Input)  
  66. PIDInit ( &sPID );                 // Initialize Structure  
  67. sPID.Proportion = 0.5;             // Set PID Coefficients  
  68. sPID.Integral = 0.5;  
  69. sPID.Derivative = 0.0;  
  70. sPID.SetPoint = 100.0;             // Set PID Setpoint  
  71. for (;;)  
  72. {                                  // Mock Up of PID Processing  
  73.   rIn = sensor ();                 // Read Input  
  74.   rOut = PIDCalc ( &sPID,rIn );    // Perform PID Interation  
  75.   actuator ( rOut );               // Effect Needed Changes  
  76. }  

电脑版 Page created in 0.8281 seconds width 5 queries.