找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2945|回复: 2
打印 上一主题 下一主题
收起左侧

51单片机PID抗饱和积分程序+Proteus仿真

[复制链接]
跳转到指定楼层
楼主
这是一个C51的PID调节的源程序。

仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)


单片机源程序如下:
  1. #include <reg51.h>
  2. #include <intrins.h>
  3. #include <stdio.h>
  4. #include <math.h>

  5. #ifndef  uchar   
  6. #define  uchar         unsigned char
  7. #endif

  8. #ifndef  uint   
  9. #define  uint         unsigned int
  10. #endif

  11. #ifndef  ulong   
  12. #define  ulong         unsigned long
  13. #endif

  14. /*
  15. struct _pid{
  16. float         SetSpeed;                                //定义设定值
  17. float         ActualSpeed;                        //定义实际值
  18. float         err;                                        //定义偏差值
  19. float         err_last;                                //定义上一个偏差值
  20. float         Kp,Ki,Kd;                                //定义比例、积分、微分系数
  21. float         voltage;                                //定义电压值(控制执行器的变量)
  22. float         integral;                                //定义积分值
  23. float         umax;
  24. float         umin;
  25. }pid;

  26. void PID_init(){
  27. printf("PID_init begin \n");
  28. pid.SetSpeed=0.0;
  29. pid.ActualSpeed=0.0;
  30. pid.err=0.0;
  31. pid.err_last=0.0;
  32. pid.voltage=0.0;
  33. pid.integral=0.0;
  34. pid.Kp=0.4;
  35. pid.Ki=0.2;                                //注意,和上几次相比,这里加大了积分环节的值
  36. pid.Kd=0.2;
  37. pid.umax=400;
  38. pid.umin=-200;
  39. printf("PID_init end \n");
  40. }

  41. float PID_realize(float speed){
  42. int index;
  43. pid.SetSpeed=speed;
  44. pid.err=pid.SetSpeed-pid.ActualSpeed;

  45. if(pid.ActualSpeed>pid.umax)                //灰色底色表示抗积分饱和的实现
  46. {

  47.                 if(abs(pid.err)>200)                //蓝色标注为积分分离过程
  48.                 {        index=0;  }
  49.                 else{
  50.                         if(abs(pid.err)>180)        {index=(200-abs(err))/20;}       
  51.                         else{index=1;}

  52.                                 if(pid.err<0)
  53.                                 {
  54.                                         pid.integral+=pid.err;
  55.                                 }
  56.                         }
  57. }
  58. else
  59.         if(pid.ActualSpeed<pid.umin)
  60.         {
  61.                 if(abs(pid.err)>200)                        //积分分离过程
  62.                 {        index=0;        }
  63.                 else{
  64.                         if(abs(pid.err)>180)        {index=(200-abs(err))/20;}       
  65.                         else{index=1;}
  66.                                 if(pid.err>0)
  67.                                 {
  68.                                         pid.integral+=pid.err;
  69.                                 }
  70.                         }
  71.         }
  72.         else
  73.         {
  74.                 if(abs(pid.err)>200)                        //积分分离过程
  75.                 {        index=0;        }
  76.                 else{
  77.                         if(abs(pid.err)>180)        {index=(200-abs(err))/20;}       
  78.                         else{index=1;}

  79.                                 pid.integral+=pid.err;
  80.                         }
  81.         }

  82. //pid.voltage=pid.Kp*pid.err+index*pid.Ki*pid.integral+pid.Kd*(pid.err-pid.err_last);

  83. //pid.voltage=pid.Kp*pid.err+index*pid.Ki*pid.integral/2+pid.Kd*(pid.err-pid.err_last);        //梯形积分

  84.   pid.voltage=pid.Kp*pid.err+index*pid.Ki*pid.integral+pid.Kd*(pid.err-pid.err_last);           //
  85. pid.err_last=pid.err;
  86. pid.ActualSpeed=pid.voltage*1.0;
  87. return pid.ActualSpeed;
  88. }

  89. */





  90. void main()
  91. {
  92. uint idata        count=0;
  93. int idata        speed1,speed3=0;
  94. // uchar         speed2;
  95. SCON=0x50;
  96. TMOD=0x20;
  97. TCON=0x40;
  98. TH1=0xe8;
  99. TL1=0xe8;
  100. TI=1;
  101. TR1=1;

  102. printf("System begin \n");

  103. PID_init();
  104. while(1)
  105. {
  106.         if(count<100)
  107.         {       
  108.         speed1 = PID_contral(250, speed3 );

  109.         // printf("%f\n",speed);         
  110.          printf("%d\n",speed1);       
  111.          printf("%5d\n",speed3);
  112. //         if(speed3<151){speed3 +=20;}
  113. //         if((speed3>150)&&(speed3<181)){speed3 += 10;}
  114. //         if((speed3>180)&&(speed3<191)){speed3 += 2;}
  115.          if(speed3<250){speed3 += 10;}

  116.           }


  117.          if((count>99)&&(count<150))
  118.          {
  119.                   speed1=PID_contral(200,speed3);
  120.                  printf("%d\n",speed1);         
  121.                 printf("%5d\n",speed3);
  122.                 if(count<110) {speed3 -= 2;}
  123.                 if((count>119)&&(count<130)) {speed3 -= 1;}
  124. //                else speed3 =202;
  125.         }
  126.         count++;
  127.        
  128.          if(count>150){break;}       
  129.           
  130. }


  131. }
复制代码

所有资料51hei提供下载:
pid抗饱和变积分.rar (103.99 KB, 下载次数: 32)


评分

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

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏3 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:136947 发表于 2019-9-27 16:02 | 只看该作者
keil编译不通过,提供的仿真一直发送“览”,你告诉我你干啥呢?
回复

使用道具 举报

板凳
ID:606251 发表于 2019-9-28 07:38 | 只看该作者
这里高手真多,学习了,谢谢楼主!
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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