PID控制算法位置型公式u(k)=Kp((err(k))+T/Ti∑err(j)+Td/T(err(k)-err(k-1)))T/Ti即为KiTd/T即为Kd#includeiostream#includestdio.husingnamespacestd;struct{floatsetnum;floatactualnum;floaterr;floatlasterr;floatkp,ki,kd;floatcontrolmachine;floatsumerr;}pid;voidPID(){pid.setnum=0.0;pid.actualnum=0.0;pid.err=0.0;pid.lasterr=0.0;pid.controlmachine=0.0;pid.sumerr=0.0;pid.kp=0.2;pid.ki=0.015;pid.kd=0.2;}floatrealizepid(floatnum){pid.setnum=num;pid.err=pid.setnum-pid.actualnum;pid.sumerr+=pid.err;pid.controlmachine=pid.kp*pid.err+pid.ki*pid.sumerr+pid.kd*(pid.err-pid.lasterr);pid.lasterr=pid.err;pid.actualnum=pid.controlmachine*1.0;returnpid.actualnum;}intmain(){PID();intcount=0;while(count1000){floatnum;num=realizepid(200.0);printf(%f\n,num);count++;}return0;}增量型公式:Δu(k)=Kp(err(k)-err(k-1))+Kierr(k)+Kd(err(k)-2err(k-1)+err(k-2))#includeiostream#includestdio.h#includestdlib.husingnamespacestd;struct{floatsetnum;floatactualnum;floaterr;floatnexterr;floatlasterr;floatkp,ki,kd;floatcontrolmachine;floatsumerr;}pid;voidPID(){pid.setnum=0.0;pid.actualnum=0.0;pid.err=0.0;pid.nexterr=0.0;pid.lasterr=0.0;pid.controlmachine=0.0;pid.sumerr=0.0;pid.kp=0.2;pid.ki=0.015;pid.kd=0.2;}floatrealizepid(floatnum){pid.setnum=num;pid.err=pid.setnum-pid.actualnum;pid.sumerr+=pid.err;floatincrementnum=pid.kp*(pid.err-pid.nexterr)+pid.ki*pid.err+pid.kd*(pid.err-2*pid.nexterr+pid.lasterr);pid.actualnum+=incrementnum;pid.lasterr=pid.nexterr;pid.nexterr=pid.err;returnpid.actualnum;}intmain(){PID();intcount=0;while(count1000){floatnum;num=realizepid(200.0);printf(%f\n,num);count++;}return0;}积分分离:消除静差,减少偏差积累#includeiostream#includestdio.h#includestdlib.h#includemath.husingnamespacestd;struct{floatsetnum;floatactualnum;floaterr;floatnexterr;floatlasterr;floatkp,ki,kd;floatcontrolmachine;floatsumerr;}pid;voidPID(){pid.setnum=0.0;pid.actualnum=0.0;pid.err=0.0;pid.nexterr=0.0;pid.lasterr=0.0;pid.controlmachine=0.0;pid.sumerr=0.0;pid.kp=0.2;pid.ki=0.04;pid.kd=0.2;}floatrealizepid(floatnum){intindex;pid.setnum=num;pid.err=pid.setnum-pid.actualnum;if(abs(pid.err)200){index=0;}else{index=1;pid.sumerr+=pid.err;}pid.controlmachine=pid.kp*pid.err+index*pid.ki*pid.sumerr+pid.kd*(pid.err-pid.lasterr);pid.lasterr=pid.err;pid.actualnum=pid.controlmachine*1.0;returnpid.actualnum;}intmain(){PID();intcount=0;while(count1000){floatnum;num=realizepid(200.0);printf(%f\n,num);count++;}return0;}抗击分饱和算法:#includeiostream#includestdio.h#includestdlib.h#includemath.husingnamespacestd;struct{floatsetnum;floatactualnum;floaterr;floatnexterr;floatlasterr;floatkp,ki,kd;floatcontrolmachine;floatsumerr;floatumax;floatumin;}pid;voidPID(){pid.setnum=0.0;pid.actualnum=0.0;pid.err=0.0;pid.nexterr=0.0;pid.lasterr=0.0;pid.controlmachine=0.0;pid.sumerr=0.0;pid.kp=0.2;pid.ki=0.1;pid.kd=0.2;pid.umax=400;pid.umin=-200;}floatrealizepid(floatnum){intindex;pid.setnum=num;pid.err=pid.setnum-pid.actualnum;if(pid.actualnumpid.umax)//积分分离过程{if(abs(pid.err)200){index=0;}else{index=1;if(pid.err0)//u(k-1)umax只累加负积分{pid.sumerr+=pid.err;}}}elseif(pid.actualnumpid.umin)//u(k-1)umin只累加正积分{if(abs(pid.err)200){index=0;}else{index=1;if(pid.err0){pid.sumerr+=pid.err;}}}else{if(abs(pid.err)200){index=0;}else{index=1;pid.sumerr+=pid.err;}}pid.controlmachine=pid.kp*pid.err+index*pid.ki*pid.sumerr+pid.kd*(pid.err-pid.lasterr);pid.lasterr=pid.err;pid.actualnum=pid.controlmachine*1.0;returnpid.actualnum;}intmain(){PID();intcount=0;while(count1000){floatnum;num=realizepid(200.0);printf(%f\n,num);count++;}return0;}梯形积分算法:积分项的作用是消除余差,为了减小余差,提高运算精度,可将矩形积分改成梯形积分,实现语句如下:pid.controlmachine=pid.kp*pid.err+index*pid.ki*pid.sumerr/2+pid.kd*(pid.err-pid.lasterr)变积分的PID算法:改变积分项的累加速度,偏差越大,积分越慢,偏差越小,积分越快#includeiostream#includestdio.h#includestdlib.h#includemath.husingnamespacestd;struct{floatsetnum;floatactualnum;floaterr;floatnexterr;floatlasterr;floatkp,ki,kd;floatcontrolmachine;floatsumerr;floatumax;floatumin;}pid;voidPID(){pid.setnum=0.0;pid.actualnum=0.0;pid.err=0.0;pid.nexterr=0.0;pid.lasterr=0.0;pid.controlmachine=0.0;pid.sumerr=0.0;pid.kp=0.4;pid.ki=0.2;pid.kd=0.2;}floatrealizepid(floatnum){floatindex;pid.setnum=num;pid.err=pid.setnum-pid.actualnum;if(abs(pid.err)200)//变积分过程{index=0.0;}elseif(abs(pid.err)180){index=1.0;pid.sumerr+=pid.err;}else{index=(200-abs(pid.err))/20;pid.sumerr+=pid.err;}pid.controlmachine=pid.kp*pid.err+index*pid.ki*pid.sumerr+pid.kd*(pid.err-pid.lasterr);pid.lasterr=pid.err;pid.actualnum=pid.controlmachine*1.0;returnpid.actualnum;}intmain(){PID();intcount=0;while(count1000){floatnum;num=realizepid(200.0);printf(%f\n,num);count++;}return0;}