智能车制作

 找回密码
 注册

扫一扫,访问微社区

查看: 2128|回复: 1
打印 上一主题 下一主题

自己收集整理的资料和代码v1.1

[复制链接]

15

主题

135

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
1870

优秀会员奖章活跃会员奖章

QQ
威望
396
贡献
1426
兑换币
0
注册时间
2010-12-2
在线时间
24 小时
跳转到指定楼层
1#
发表于 2011-4-23 01:22:37 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式


  1. /*====================================================================================================
  2. 这是从网上找来的一个比较典型的PID处理程序免费分享了,在使用单片机作为控制cpu时,请稍作简化,具体的PID
  3. 参数必须由具体对象通过实验确定。由于单片机的处理速度和ram资源的限制,一般不采用浮点数运算,
  4. 而将所有参数全部用整数,运算到最后再除以一个2的N次方数据(相当于移位),作类似定点数运算,可
  5. 大大提高运算速度,根据控制精度的不同要求,当精度要求很高时,注意保留移位引起的“余数”,做好余
  6. 数补偿。这个程序只是一般常用pid算法的基本架构,没有包含输入输出处理部分。
  7. =====================================================================================================*/
  8. #include <string.h>
  9. #include <stdio.h>
  10. /*====================================================================================================
  11. PID Function

  12. The PID (比例、积分、微分) function is used in mainly
  13. control applications. PIDCalc performs one iteration of the PID
  14. algorithm.

  15. While the PID function works, main is just a dummy program showing
  16. a typical usage.
  17. =====================================================================================================*/

  18. typedef struct PID {

  19. double SetPoint; // 设定目标 Desired Value

  20. double Proportion; // 比例常数 Proportional Const
  21. double Integral; // 积分常数 Integral Const
  22. double Derivative; // 微分常数 Derivative Const

  23. double LastError; // Error[-1]
  24. double PrevError; // Error[-2]
  25. double SumError; // Sums of Errors

  26. } PID;

  27. /*====================================================================================================
  28. PID计算部分
  29. =====================================================================================================*/

  30. double PIDCalc( PID *pp, double NextPoint )
  31. {
  32. double dError,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. Initialize PID Structure
  45. =====================================================================================================*/

  46. void PIDInit (PID *pp)
  47. {
  48. memset ( pp,0,sizeof(PID));
  49. }

  50. /*====================================================================================================
  51. Main Program
  52. =====================================================================================================*/

  53. double sensor (void) // Dummy Sensor Function虚拟传感器函数
  54. {
  55. return 100.0;
  56. }

  57. void actuator(double rDelta) // Dummy Actuator Function虚拟激励函数
  58. {
  59. ; //省略...
  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 (;;) { // Mock Up of PID Processing模拟的PID处理

  72. rIn = sensor (); // Read Input
  73. rOut = PIDCalc ( &sPID,rIn ); // Perform PID Interation
  74. actuator ( rOut ); // Effect Needed Changes
  75. }
  76. }



复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

15

主题

135

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
1870

优秀会员奖章活跃会员奖章

QQ
威望
396
贡献
1426
兑换币
0
注册时间
2010-12-2
在线时间
24 小时
2#
 楼主| 发表于 2011-4-23 01:24:34 | 只看该作者

PS:看清楚是否需要在买!

回复 支持 反对

使用道具 举报

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

本版积分规则

关于我们|联系我们|小黑屋|智能车制作 ( 黑ICP备2022002344号

GMT+8, 2024-6-16 09:19 , Processed in 0.045345 second(s), 32 queries , Gzip On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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