%基本成功uk0时等于0%fuzzytunningPIDcontrolclearall;clearall;a=newfis('fuzzpid');a=addvar(a,'input','e',[-0.15,0.15]);%parameterea=addmf(a,'input',1,'NB','zmf',[-0.15,-0.1]);a=addmf(a,'input',1,'NM','trimf',[-0.12,-0.1,0]);a=addmf(a,'input',1,'NS','trimf',[-0.06,-0.04,0]);a=addmf(a,'input',1,'Z','trimf',[-0.03,0,0.03]);a=addmf(a,'input',1,'PS','trimf',[0,0.04,0.06]);a=addmf(a,'input',1,'PM','trimf',[0,0.1,0.12]);a=addmf(a,'input',1,'PB','smf',[0.1,0.15]);a=addvar(a,'input','ec',[-0.003,0.003]);%parametereca=addmf(a,'input',2,'NB','zmf',[-0.003,-0.002]);a=addmf(a,'input',2,'NM','trimf',[-0.0025,-0.002,-0.001]);a=addmf(a,'input',2,'NS','trimf',[-0.0015,-0.001,-0.0005]);a=addmf(a,'input',2,'Z','trimf',[-0.001,0,0.001]);a=addmf(a,'input',2,'PS','trimf',[0.0005,0.001,0.0015]);a=addmf(a,'input',2,'PM','trimf',[0.001,0.002,0.0025]);a=addmf(a,'input',2,'PB','smf',[0.002,0.003]);a=addvar(a,'output','kp',[-0.33,0.33]);%parameterkpa=addmf(a,'output',1,'NB','zmf',[-0.33,-0.11]);a=addmf(a,'output',1,'NM','trimf',[-0.33,-0.22,0]);a=addmf(a,'output',1,'NS','trimf',[-0.33,-0.11,0.11]);a=addmf(a,'output',1,'Z','trimf',[-0.22,0,0.22]);a=addmf(a,'output',1,'PS','trimf',[-0.11,0.11,0.33]);a=addmf(a,'output',1,'PM','trimf',[0,0.22,0.33]);a=addmf(a,'output',1,'PB','smf',[0.11,0.33]);a=addvar(a,'output','ki',[-0.00000012,0.00000012]);%parameterkia=addmf(a,'output',2,'NB','zmf',[-0.00000012,-0.00000008]);a=addmf(a,'output',2,'NM','trimf',[-0.00000012,-0.00000008,0]);a=addmf(a,'output',2,'NS','trimf',[-0.00000006,-0.00000004,0.00000004]);a=addmf(a,'output',2,'Z','trimf',[-0.00000004,0,0.00000004]);a=addmf(a,'output',2,'PS','trimf',[-0.00000004,0.00000004,0.00000006]);a=addmf(a,'output',2,'PM','trimf',[0,0.00000008,0.00000012]);a=addmf(a,'output',2,'PB','smf',[0.00000004,0.00000012]);a=addvar(a,'output','kd',[-2.4,2.4]);%parameterkda=addmf(a,'output',3,'NB','zmf',[-2.4,-0.8]);a=addmf(a,'output',3,'NM','trimf',[-2.4,-1.6,0]);a=addmf(a,'output',3,'NS','trimf',[-2.4,-0.8,0]);a=addmf(a,'output',3,'Z','trimf',[-1.6,0,1.6]);a=addmf(a,'output',3,'PS','trimf',[0,0.8,2.4]);a=addmf(a,'output',3,'PM','trimf',[0,1.6,2.4]);a=addmf(a,'output',3,'PB','smf',[0.8,2.4]);rulelist=[1174711;1264611;1364711;1453411;1554111;1644411;1734411;2154711;2254511;2364711;2474711;2564611;2644411;2744511;3164511;3254511;3374711;3474711;3574711;3644311;3744311;4154611;4254411;4344411;4464411;4544411;4654411;4764611;5134611;5244511;5344411;5464411;5544611;5664611;5764611;6144411;6244411;6374411;6475411;6574611;6664611;6764611;7174711;7275711;7376711;7477711;7576711;7675711;7774711];a=addrule(a,rulelist);a=setfis(a,'DefuzzMethod','mom');%最大隶属度平均法解模糊;writefis(a,'fuzzpid');a=readfis('fuzzpid');%PIDcontrollerts=0.01;sys=tf([100],[31.25,1],'inputdelay',0.4);dsys=c2d(sys,ts,'zoh');%转化成dz函数[num,den]=tfdata(dsys,'v');u_1=0.0;u_2=0.0;u_3=0.0;u_4=0;u_5=0;u_6=0.0;u_7=0.0;u_8=0.0;u_9=0;u_10=0;u_11=0.0;u_12=0.0;u_13=0.0;u_14=0;u_15=0;u_16=0.0;u_17=0.0;u_18=0.0;u_19=0;u_20=0;u_21=0.0;u_22=0.0;u_23=0.0;u_24=0;u_25=0;u_26=0.0;u_27=0.0;u_28=0.0;u_29=0;u_30=0;u_31=0.0;u_32=0.0;u_33=0.0;u_34=0;u_35=0;u_36=0.0;u_37=0.0;u_38=0.0;u_39=0;u_40=0;u_41=0;y_1=0;x=[0,0,0]';error_1=0;e_1=0.0;ec_1=0.0;kp0=0.38;kd0=5.8;ki0=0;fork=1:1:6000time(k)=k*ts;ifk=3000rin(k)=40;elserin(k)=60;end%usingfuzzyinferencetotunningPIDifk=40yout(k)=20;elseyout(k)=-den(2)*y_1+num(2)*u_41;enderror(k)=rin(k)-yout(k);e_1=error(k);ife_1-1.5&&e_11.5k_pid=evalfis([e_1,ec_1],a);ki(k)=k_pid(2)+ki0;ki0=ki(k);kd(k)=k_pid(3)+kd0;kp(k)=kp0+k_pid(1);u(k)=kp(k)*x(1)+kd(k)*x(2)+ki(k)*x(3);endife_1=1.5u(k)=1;ki0=0;endife_1=-1.5u(k)=0;endifu(k)=1u(k)=1;endifu(k)=0u(k)=0;end%%%%%%%%%%%%%%%%%%%%%%%%%returnofpidparameters%%%%%%%%%%%%%%%%%%%%%%%%u_41=u_40;u_40=u_39;u_39=u_38;u_38=u_37;u_37=u_36;u_36=u_35;u_35=u_34;u_34=u_33;u_33=u_32;u_32=u_31;u_31=u_30;u_30=u_29;u_29=u_28;u_28=u_27;u_27=u_26;u_26=u_25;u_25=u_24;u_24=u_23;u_23=u_22;u_22=u_21;u_21=u_20;u_20=u_19;u_19=u_18;u_18=u_17;u_17=u_16;u_16=u_15;u_15=u_14;u_14=u_13;u_13=u_12;u_12=u_11;u_11=u_10;u_10=u_9;u_9=u_8;u_8=u_7;u_7=u_6;u_6=u_5;u_5=u_4;u_4=u_3;u_3=u_2;u_2=u_1;u_1=u(k);y_1=yout(k);x(1)=error(k);%calculatingPx(2)=error(k)-error_1;%calculatingDx(3)=x(3)+error(k);%calculatingDe_1=x(1);ec_1=x(2);ec(k)=x(2);error_2=error_1;error_1=error(k);endshowrule(a)figure(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');figure(3);plot(time,u,'r');xlabel('time(s)');ylabel('u');figure(4);plot(time,kp,'r');xlabel('time(s)');ylabel('kp');figure(5);plot(time,ki,'r');xlabel('time(s)');ylabel('ki');figure(6);plot(time,kd,'r');xlabel('time(s)');ylabel('kd');figure(7);plotmf(a,'input',1);figure(8);plotmf(a,'input',2);figure(9);plotmf(a,'output',1);figure(10);plotmf(a,'output',2);figure(11);plotmf(a,'output',3);