学生--PID控制MATLAB仿真实验

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

MATLAB2012-11-191PIDPIDPIDPID1-1r(t)-y(t)u(t)e(t)++1-1PIDPIDdttdeTdtteTtektuDtIp)()(1)()(0sTsTksEsUsGDIp11)()()(Ex1ss251332PID)2.0*2sin()(ttr3,1,60dipkkkODE4510sSimulinkPIDSimulinkExtrasPIDControllerex1_1.mdl1-21-2PIDSimulinkPID1-31-3PIDmex1_2.mdl20,01,1330,25010DCBA1-4mex1_2plot.mcloseall;plot(t,rin,'k',t,yout,'k');xlabel('time(s)');ylabel('r,y');PIDEx2JssKsG2)()2sin(300400),6sin(1020tKtJ)2sin(5.0tPD1,0,10dipkkkex2.mdl1-41-51-4ex2Simulink1-5Simulink3PIDPIDPIDPIDPIDPIDkTtPIDkjdipkjDIpTkekekTjekkekkekeTTjeTTkekku00)1()()()())1()(()()()(DpdIpiTkkTkk,ePIDuPIDPIDD/AA/DDSPPIDEx3BsJssG21)(J=0.0067,B=0.1)2sin(5.0tPD5.0,20dpkkODE45BsJssUsYsG21)()()(udtdyBdtydJ22yyyy2,1/J)*u((B/J)yyyy12221ex3f.mfunctiondy=ex3f(t,y,flag,para)u=para;J=0.0067;B=0.1;dy=zeros(2,1);dy(1)=y(2);dy(2)=-(B/J)*y(2)+(1/J)*u;ex3.mclearall;closeall;ts=0.001;%xk=zeros(2,1);%A/Dye_1=0;%e(k-1)4u_1=0;%u(k-1)fork=1:1:2000%ktime(k)=k*ts;%timerin(k)=0.50*sin(1*2*pi*k*ts);%para=u_1;%D/AtSpan=[0ts];[tt,xx]=ode45('ex3f',tSpan,xk,[],para);%ode45%xxttyttyxk=xx(end,:);%A/Dxxyyyout(k)=xk(1);%xk(1)y(k)e(k)=rin(k)-yout(k);%de(k)=(e(k)-e_1)/ts;%u(k)u(k)=20.0*e(k)+0.50*de(k);%u(k)%ifu(k)10.0u(k)=10.0;endifu(k)-10.0u(k)=-10.0;end%u(k-1)e(k-1)u_1=u(k);e_1=e(k);endfigure(1);plot(time,rin,'r',time,yout,'b');%xlabel('time(s)'),ylabel('rin,yout');figure(2);plot(time,rin-yout,'r');xlabel('time(s)'),ylabel('error');%Ex4sss1047035.8752350023SimulinkmODE45Simulinkm1msPID05.0,2,5.1dipkkkm5ex4.mdl2-1Simulinkex4f.mfunction[u]=ex4f(u1,u2)%u1Clocku22-1Sumepersistenterrorierror_1ifu1==0%Clock=0e(k)=e(k-1)=0errori=0error_1=0endts=0.001;kp=1.5;ki=2.0;kd=0.05;error=u2;errord=(error-error_1)/ts;%errori=errori+error*ts;%u=kp*error+kd*errord+ki*errori;%PIDu(k)error_1=error;%PIDEx5ssssG1047035.87523500)(231msPIDSS=1S=2S=3G(s))()()(zUzYzGY(z)U(z)z)3()4()2()3()1()2()3()4()2()3()1()2()(kunumkunumkunumkydenkydenkydenkyoutex5.m%PIDControllerclearall;6closeall;ts=0.001;%sys=tf(5.235e005,[1,87.35,1.047e004,0]);%dsys=c2d(sys,ts,'z');%z[num,den]=tfdata(dsys,'v');%zu_1=0.0;u_2=0.0;u_3=0.0;%u(k-1)u(k-2)u(k-3)y_1=0.0;y_2=0.0;y_3=0.0;%y(k-1)y(k-2)y(k-3)x=[0,0,0]';%error_1=0;%e(k-1)disp('S=1--step,S=2--sin,S=3--square')%S=1S=2S=3S=input('NumberofinputsignalS:')%fork=1:1:1500time(k)=k*ts;%ifS==1%kp=0.50;ki=0.001;kd=0.001;%PIDrin(k)=1;%elseifS==2kp=0.50;ki=0.001;kd=0.001;%PIDrin(k)=sign(sin(2*2*pi*k*ts));%elseifS==3kp=1.5;ki=1.0;kd=0.01;%PIDrin(k)=0.5*sin(2*2*pi*k*ts);%endu(k)=kp*x(1)+kd*x(2)+ki*x(3);%PIDu(k)%ifu(k)=10u(k)=10;endifu(k)=-10u(k)=-10;end%y(k)yout(k)=-den(2)*y_1-den(3)*y_2-den(4)*y_3+num(2)*u_1+num(3)*u_2+num(4)*u_3;error(k)=rin(k)-yout(k);%%u(k-1)u(k-2)u(k-3)y(k-1)y(k-2)y(k-3)u_3=u_2;u_2=u_1;u_1=u(k);7y_3=y_2;y_2=y_1;y_1=yout(k);x(1)=error(k);%x(2)=(error(k)-error_1)/ts;%x(3)=x(3)+error(k)*ts;%error_1=error(k);%e(k-1)endfigure(1);%plot(time,rin,'r',time,yout,'b');xlabel('time(s)'),ylabel('rin,yout');Ex6Ex5ex6.mS=1S=2S=3D=1pause%PIDControllerclearall;closeall;ts=0.001;sys=tf(5.235e005,[1,87.35,1.047e004,0]);dsys=c2d(sys,ts,'z');[num,den]=tfdata(dsys,'v');u_1=0.0;u_2=0.0;u_3=0.0;r_1=rand;y_1=0;y_2=0;y_3=0;x=[0,0,0]';error_1=0;disp('S=1--Triangle,S=2--Sawtooth,S=3--Random')%S=1S=2S=3S=input('NumberofinputsignalS:')%disp('D=1--Dynamicdisplay,D~=1--Directdisplay')%D=1D~=1D=input('D=')fork=1:1:3000time(k)=k*ts;kp=1.0;ki=2.0;kd=0.01;ifS==1%TriangleSignalifmod(time(k),2)1rin(k)=mod(time(k),1);8elserin(k)=1-mod(time(k),1);endrin(k)=rin(k)-0.5;endifS==2%SawtoothSignalrin(k)=mod(time(k),1.0);endifS==3%RandomSignalrin(k)=rand;vr(k)=(rin(k)-r_1)/ts;%Maxspeedis5.0whileabs(vr(k))=5.0rin(k)=rand;vr(k)=abs((rin(k)-r_1)/ts);endendu(k)=kp*x(1)+kd*x(2)+ki*x(3);%PIDController%Restrictingtheoutputofcontrollerifu(k)=10u(k)=10;endifu(k)=-10u(k)=-10;end%Linearmodelyout(k)=-den(2)*y_1-den(3)*y_2-den(4)*y_3+num(2)*u_1+num(3)*u_2+num(4)*u_3;error(k)=rin(k)-yout(k);r_1=rin(k);u_3=u_2;u_2=u_1;u_1=u(k);y_3=y_2;y_2=y_1;y_1=yout(k);x(1)=error(k);%CalculatingPx(2)=(error(k)-error_1)/ts;%CalculatingDx(3)=x(3)+error(k)*ts;%CalculatingIxi(k)=x(3);error_1=error(k);ifD==1%DynamicSimulationDisplay9plot(time,rin,'b',time,yout,'r');pause(0.000001);endendplot(time,rin,'r',time,yout,'b');xlabel('time(s)');ylabel('rin,yout');Ex7SimulinkPID2-2PID2-32-4ex7.mdl2-2PIDSimulink2-3PIDSimulink2-4PID10PIDe(k)u(k)u(k)ukPIDPIDPIDPID)2()1(2)()()1()()1()()(kekekekkekkekekkukukudipEx8sssG50400)(2PID10,1.0,8dipkkkex8.m%IncrementPIDControllerclearall;closeall;ts=0.001;sys=tf(400,[1,50,0]);dsys=c2d(sys,ts,'z');[num,den]=tfdata(dsys,'v');u_1=0.0;u_2=0.0;u_3=0.0;y_1=0;y_2=0;y_3=0;x=[0,0,0]';error_1=0;error_2=0;fork=1:1:1000time(k)=k*ts;rin(k)=1.0;kp=8;ki=0.10;kd=10;du(k)=kp*x(1)+kd*x(2)+ki*x(3);u(k)=u_1+du(k);ifu(k)=10u(k)=10;end11ifu(k)=-10u(k)=-10;endyout(k)=-den(2)*y_1-den(3)*y_2+num(2)*u_1+num(3)*u_2;error=rin(k)-yout(k);u_3=u_2;u_2=u_1;u_1=u(k);y_3=y_2;y_2=y_1;y_1=yout(k);x(1)=error-error_1;%CalculatingPx(2)=error-2*error_1+error_2;%CalculatingDx(3)=error;%CalculatingIerror_2=error_1;error_1=error;endplot(time,rin,'b',time,yout,'r');xla

1 / 27
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功