找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2837|回复: 3
收起左侧

比较典型的PID处理程序

  [复制链接]
ID:793610 发表于 2020-6-30 11:55 | 显示全部楼层 |阅读模式
这是从网上找来的一个比较典型的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. }





复制代码



评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:462629 发表于 2021-12-28 09:26 | 显示全部楼层
这个PID有用吗?
回复

使用道具 举报

ID:33544 发表于 2022-8-18 16:26 | 显示全部楼层

这个资料好,不过难度也不低呀!
回复

使用道具 举报

ID:1046594 发表于 2022-10-9 16:15 | 显示全部楼层
這個應該是位置式PID,如果換成增量式 數據計算就沒有那麼大
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表