您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 经营企划 > PID算法Matlab仿真程序和C程序
1增量式PID控制算法Matlab仿真程序设一被控对象G(s)=50/(0.125s^2+7s),用增量式PID控制算法编写仿真程序(输入分别为单位阶跃、正弦信号,采样时间为1ms,控制器输出限幅:[-5,5],仿真曲线包括系统输出及误差曲线,并加上注释、图例)。程序如下clearall;closeall;ts=0.001;sys=tf(50,[0.125,7,0]);dsys=c2d(sys,ts,'z');[num,den]=tfdata(dsys,'v');u_1=0.0;u_2=0.0;y_1=0.0;y_2=0.0;x=[0,0,0]';error_1=0;error_2=0;fork=1:1:1000time(k)=k*ts;S=2;ifS==1kp=10;ki=0.1;kd=15;rin(k)=1;%StepSignalelseifS==2kp=10;ki=0.1;kd=15;%SineSignalrin(k)=0.5*sin(2*pi*k*ts);enddu(k)=kp*x(1)+kd*x(2)+ki*x(3);%PIDControlleru(k)=u_1+du(k);%Restrictingtheoutputofcontrollerifu(k)=5u(k)=5;endifu(k)=-5u(k)=-5;end%Linearmodelyout(k)=-den(2)*y_1-den(3)*y_2+num(2)*u_1+num(3)*u_2;error(k)=rin(k)-yout(k);%Returnofparametersu_2=u_1;u_1=u(k);y_2=y_1;y_1=yout(k);x(1)=error(k)-error_1;%CalculatingPx(2)=error(k)-2*error_1+error_2;%CalculatingDx(3)=error(k);%CalculatingIerror_2=error_1;error_1=error(k);endfigure(1);plot(time,rin,'b',time,yout,'r');xlabel('time(s)'),ylabel('rin,yout');figure(2);plot(time,error,'r')xlabel('time(s)');ylabel('error');微分先行PID算法Matlab仿真程序%PIDControlerwithdifferentialinadvanceclearall;closeall;ts=20;sys=tf([1],[60,1],'inputdelay',80);dsys=c2d(sys,ts,'zoh');[num,den]=tfdata(dsys,'v');u_1=0;u_2=0;u_3=0;u_4=0;u_5=0;ud_1=0;y_1=0;y_2=0;y_3=0;error_1=0;error_2=0;ei=0;fork=1:1:400time(k)=k*ts;2%Linearmodelyout(k)=-den(2)*y_1+num(2)*u_5;kp=0.36;kd=14;ki=0.0021;rin(k)=1.0*sign(sin(0.00025*2*pi*k*ts));error(k)=rin(k)-yout(k);ei=ei+error(k)*ts;gama=0.50;Td=kd/kp;Ti=0.5;c1=gama*Td/(gama*Td+ts);c2=(Td+ts)/(gama*Td+ts);c3=Td/(gama*Td+ts);M=1;ifM==1%PIDControlwithdifferentialinadvanceud(k)=c1*ud_1+c2*yout(k)-c3*y_1;u(k)=kp*error(k)+ud(k)+ki*ei;elseifM==2%SimplePIDControlu(k)=kp*error(k)+kd*(error(k)-error_1)/ts+ki*ei;endifu(k)=110u(k)=110;endifu(k)=-110u(k)=-110;end%Updateparametersu_5=u_4;u_4=u_3;u_3=u_2;u_2=u_1;u_1=u(k);y_3=y_2;y_2=y_1;y_1=yout(k);error_2=error_1;error_1=error(k);endfigure(1);plot(time,rin,'r',time,yout,'b');xlabel('time(s)');ylabel('rin,yout');figure(2);plot(time,u,'r');xlabel('time(s)');ylabel('u');不完全微分PID算法Matlab仿真程序%PIDControlerwithPartialdifferentialclearall;closeall;ts=20;sys=tf([1],[60,1],'inputdelay',80);dsys=c2d(sys,ts,'zoh');[num,den]=tfdata(dsys,'v');u_1=0;u_2=0;u_3=0;u_4=0;u_5=0;ud_1=0;y_1=0;y_2=0;y_3=0;error_1=0;ei=0;fork=1:1:100time(k)=k*ts;rin(k)=1.0;%Linearmodelyout(k)=-den(2)*y_1+num(2)*u_5;error(k)=rin(k)-yout(k);%PIDControllerwithpartlydifferentialei=ei+error(k)*ts;kc=0.30;ki=0.0055;TD=140;3kd=kc*TD/ts;Tf=180;Q=tf([1],[Tf,1]);%LowFreqSignalFilterM=2;ifM==1%UsingPIDwithPartialdifferential加在简单PID后的不完全微分alfa=Tf/(ts+Tf);u(k)=alfa*u_1+(1-alfa)*(kc*error(k)+kd*(error(k)-error_1)+ki*ei);u_1=u(k);elseifM==2%UsingPIDwithPartialdifferential只加在微分环节上的不完全微分alfa=Tf/(ts+Tf);ud(k)=kd*(1-alfa)*(error(k)-error_1)+alfa*ud_1;u(k)=kc*error(k)+ud(k)+ki*ei;ud_1=ud(k);elseifM==3%UsingSimplePID简单的PID微分u(k)=kc*error(k)+kd*(error(k)-error_1)+ki*ei;end%Restrictingtheoutputofcontrollerifu(k)=10u(k)=10;endifu(k)=-10u(k)=-10;endu_5=u_4;u_4=u_3;u_3=u_2;u_2=u_1;u_1=u(k);y_3=y_2;y_2=y_1;y_1=yout(k);error_1=error(k);endfigure(1);plot(time,rin,'b',time,yout,'r');xlabel('time(s)');ylabel('rin,yout');figure(2);plot(time,u,'r');xlabel('time(s)');ylabel('u');figure(3);plot(time,rin-yout,'r');xlabel('time(s)');ylabel('error');figure(4);bode(Q,'r');dcgain(Q);C语言PID演示程序#includestring.h#includestdio.htypedefstructPID{doubleCommand;//输入指令doubleProportion;//比例系数doubleIntegral;//积分系数doubleDerivative;//微分系数doublepreErr;//前一拍误差doublesumErr;//误差累积}PID;doublePIDCale(PID*p,doublefeedback){doubledErr,Err;Err=p-Command-feedback;//当前误差p-sumErr+=Err;//误差累加dErr=Err-p-preErr;//误差微分p-preErr=Err;return(p-Proportion*Err//比例项+p-Derivative*dErr//微分项+p-Integral*p-sumErr);//积分项}voidPIDInit(PID*p){memset(p,0,sizeof(PID));//初始化}typedefstructmotor{doublelastY;4doublepreY;doublelastU;doublepreU;}motor;voidmotorInit(motor*m){memset(m,0,sizeof(motor));}doublemotorCal(motor*m,doubleu){doubley=1.9753*m-lastY-0.9753*m-preY+0.00003284*u+0.00006568*m-lastU+0.00003284*m-preU;//二阶系统m-preY=m-lastY;m-lastY=y;m-preU=m-lastU;m-lastU=u;returny;}voidmain(){FILE*fp=fopen(data.txt,w+);PIDsPID;motorm_motor;intk=0;doubleu;doubley=0;PIDInit(&sPID);sPID.Proportion=2;sPID.Derivative=1;sPID.Integral=0.00001;sPID.Command=10;motorInit(&m_motor);while(k=1000){•fprintf(fp,%d设定值=%f被控量=%f偏差=%f控制量=%f\n,k,sPID.Command,y,sPID.Command-y,u);u=PIDCale(&sPID,y);y=motorCal(&m_motor,u);k++;}printf(%f\n,y);fclose(fp);}增量式PID控制C语言代码增量式PID控制C语言代码//////////////////////////////////////////////////////////////////定义PID参数结构体///////////////////////////////////////////////////////////////typedefstructPID{//结构体定义intSetPoint//设定值intProportion;//Proportion比例系数intIntegral;//Integral积分系数intDerivative;//Derivative微分系数intLastError;//Error[-1]前一拍误差intPreError;//Error[-2]前两拍误差}PID;main(){PIDvPID;//定义结构变量名PIDInit(&vPID);//InitializeStructure5vPID.Proportion=10;//SetPIDCoefficientsvPID.Integral=10;//SetPIDIntegralvPID.Derivative=10;//SetPIDDerivativevPID.SetPoint=//根据实际情况设定while(1){Verror=Measure();//得到AD的输出值Err
本文标题:PID算法Matlab仿真程序和C程序
链接地址:https://www.777doc.com/doc-1572881 .html