PID控制算法的C语言实现六 抗积分饱和的PID控制算法C语言实现

 

   所谓的积分饱和现象是指如果系统存在一个方向的偏差,PID控制器的输出由于积分作用的不断累加而加大,从而导致执行机构达到极限位置,若控制器输出U(k)继续增大,执行器开度不可能再增大,此时计算机输出控制量超出了正常运行范围而进入饱和区。一旦系统出现反向偏差,u(k)逐渐从饱和区退出。进入饱和区越深则退出饱和区时间越长。在这段时间里,执行机构仍然停留在极限位置而不随偏差反向而立即做出相应的改变,这时系统就像失控一样,造成控制性能恶化,这种现象称为积分饱和现象或积分失控现象。

    防止积分饱和的方法之一就是抗积分饱和法,该方法的思路是在计算u(k)时,首先判断上一时刻的控制量u(k-1)是否已经超出了极限范围: 如果u(k-1)>umax,则只累加负偏差; 如果u(k-1)

struct _pid{
    float SetSpeed;            //定义设定值
    float ActualSpeed;        //定义实际值
    float err;                //定义偏差值
    float err_last;            //定义上一个偏差值
    float Kp,Ki,Kd;            //定义比例、积分、微分系数
    float voltage;            //定义电压值(控制执行器的变量)
    float integral;            //定义积分值
    float umax;
    float umin;

}pid;

void PID_init(){
    printf("PID_init begin \n");
    pid.SetSpeed=0.0;
    pid.ActualSpeed=0.0;
    pid.err=0.0;
    pid.err_last=0.0;
    pid.voltage=0.0;
    pid.integral=0.0;
    pid.Kp=0.2;
   pid.Ki=0.1;       //注意,和上几次相比,这里加大了积分环节的值
    pid.Kd=0.2;
    pid.umax=400;
    pid.umin=-200;
    printf("PID_init end \n");
}
float PID_realize(float speed){
    int index;
    pid.SetSpeed=speed;
    pid.err=pid.SetSpeed-pid.ActualSpeed;

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

       if(abs(pid.err)>200)      //蓝色标注为积分分离过程
        {
            index=0;
        }else{
            index=1;
            if(pid.err<0)
            {
              pid.integral+=pid.err;
            }
        }
    }else if(pid.ActualSpeed         if(abs(pid.err)>200)      //积分分离过程
        {
            index=0;
        }else{
            index=1;
            if(pid.err>0)
            {
            pid.integral+=pid.err;
            }
        }
    }else{
        if(abs(pid.err)>200)                    //积分分离过程
        {
            index=0;
        }else{
            index=1;
            pid.integral+=pid.err;
        }
    }

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

    pid.err_last=pid.err;
    pid.ActualSpeed=pid.voltage*1.0;
    return pid.ActualSpeed;
}


最终的测试程序运算结果如下,可以明显的看出系统的稳定时间相对前几次来讲缩短了不少。

 

100.000000
30.000000
95.000000
65.500000
103.750000
92.175003
115.237503
112.173752
126.794380
127.653938
137.468842
139.967911
146.934479
149.954224
155.144211
158.157745
162.174561
164.953079
168.149734
170.611786
173.205124
175.339691
177.470551
179.298065
181.063431
182.616440
184.086655
185.400513
186.628952
187.737457
188.766006
189.699692
190.561951
191.347580
192.071030
192.731674
193.338928
193.894257
194.404160
194.870834
195.299072
195.691193
196.050888
196.380341
196.682465
196.959244
197.213043
197.445572
197.658768
197.854111
198.033203
198.197311
198.347763
198.485626
198.612015
198.727829
198.834000
198.931290
199.020477
199.102219
199.177139
199.245804
199.308746
199.366425
199.419296
199.467758
199.512161
199.552872
199.590179
199.624390
199.655716
199.684464
199.710785
199.734924
199.757034
199.777298
199.795883
199.812912
199.828537
199.842834
199.855972
199.867981
199.879013
199.889099
199.898361
199.906845
199.914612
199.921753
199.928268
199.934280
199.939743
199.944794
199.949371
199.953629
199.957474
199.961029
199.964279
199.967270
199.969986
199.972504
199.974792
199.976898
199.978821
199.980591
199.982208
199.983688
199.985062
199.986298
199.987442
199.988495
199.989441
199.990326
199.991135
199.991867
199.992554
199.993179
199.993744
199.994263
199.994751
199.995178
199.995590
199.995941
199.996292
199.996582
199.996887
199.997116
199.997391
199.997574
199.997803
199.997971
199.998154
199.998291
199.998444
199.998581
199.998703
199.998810
199.998917
199.999008
199.999084
199.999176
199.999237
199.999298
199.999359
199.999405
199.999466
199.999496
199.999542
199.999588
199.999619
199.999649
199.999680
199.999710
199.999725
199.999756
199.999771
199.999786
199.999817
199.999817
199.999847
199.999847
199.999863
199.999863
199.999878
199.999893
199.999893
199.999908
199.999924
199.999924
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939
199.999939

你可能感兴趣的:(PID控制算法的C语言实现)