标题: 比较典型的PID处理程序 [打印本页]

作者: hey-yoo    时间: 2020-6-30 11:55
标题: 比较典型的PID处理程序
这是从网上找来的一个比较典型的PID处理程序,在使用单片机作为控制cpu时,请稍作简化,具体的PID
参数必须由具体对象通过实验确定。由于单片机的处理速度和ram资源的限制,一般不采用浮点数运算,
而将所有参数全部用整数,运算到最后再除以一个2的N次方数据(相当于移位),作类似定点数运算,可
大大提高运算速度,根据控制精度的不同要求,当精度要求很高时,注意保留移位引起的“余数”,做好余
数补偿。这个程序只是一般常用pid算法的基本架构,没有包含输入输出处理部分。


  1. #include <string.h>
  2. #include <stdio.h>
  3. /*====================================================================================================
  4. PID Function
  5. The PID (比例、积分、微分) function is used in mainly
  6. control applications. PIDCalc performs one iteration of the PID
  7. algorithm.
  8. While the PID function works, main is just a dummy program showing
  9. a typical usage.
  10. =====================================================================================================*/
  11. typedef struct PID {
  12. double SetPoint; // 设定目标 Desired Value
  13. double Proportion; // 比例常数 Proportional Const
  14. double Integral; // 积分常数 Integral Const
  15. double Derivative; // 微分常数 Derivative Const
  16. double LastError; // Error[-1]
  17. double PrevError; // Error[-2]
  18. double SumError; // Sums of Errors
  19. } PID;
  20. /*====================================================================================================
  21. PID计算部分
  22. =====================================================================================================*/
  23. double PIDCalc( PID *pp, double NextPoint )
  24. {
  25. double dError,
  26. Error;
  27. Error = pp->SetPoint - NextPoint; // 偏差
  28. pp->SumError += Error; // 积分
  29. dError = pp->LastError - pp->PrevError; // 当前微分
  30. pp->PrevError = pp->LastError;
  31. pp->LastError = Error;
  32. return (pp->Proportion * Error // 比例项
  33. + pp->Integral * pp->SumError // 积分项
  34. + pp->Derivative * dError // 微分项
  35. );
  36. }
  37. /*====================================================================================================
  38. Initialize PID Structure
  39. =====================================================================================================*/
  40. void PIDInit (PID *pp)
  41. {
  42. memset ( pp,0,sizeof(PID));
  43. }
  44. /*====================================================================================================
  45. Main Program
  46. =====================================================================================================*/
  47. double sensor (void) // Dummy Sensor Function
  48. {
  49. return 100.0;
  50. }
  51. void actuator(double rDelta) // Dummy Actuator Function
  52. {}
  53. void main(void)
  54. {
  55. PID sPID; // PID Control Structure
  56. double rOut; // PID Response (Output)


  57. double rIn; // PID Feedback (Input)
  58. PIDInit ( &sPID ); // Initialize Structure
  59. sPID.Proportion = 0.5; // Set PID Coefficients
  60. sPID.Integral = 0.5;
  61. sPID.Derivative = 0.0;
  62. sPID.SetPoint = 100.0; // Set PID Setpoint
  63. for (;;) { // Mock Up of PID Processing
  64. rIn = sensor (); // Read Input
  65. rOut = PIDCalc ( &sPID,rIn ); // Perform PID Interation
  66. actuator ( rOut ); // Effect Needed Changes
  67. }
  68. }





复制代码




作者: woyaodwn    时间: 2021-12-28 09:26
这个PID有用吗?
作者: wis98    时间: 2022-8-18 16:26

这个资料好,不过难度也不低呀!
作者: nanhaixiaodeng    时间: 2022-10-9 16:15
這個應該是位置式PID,如果換成增量式 數據計算就沒有那麼大




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1