0001 function bodePIDcompare(num,den,tau)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025 global PIDPARAMETERS
0026
0027
0028 P = tf(num,den,'IODelay',tau);
0029 [m,f,w] = bode(P);
0030 w = logspace(log10(w(1)),log10(w(end)),512);
0031 s = j*w;
0032
0033
0034 [y,t] = step(P);
0035 Ts = min(real(abs(pole(P))))/50;
0036 [y,t] = step(P,0:Ts:t(end));
0037 Tf = t(end);
0038
0039
0040 modelFOPDT = idareas(y,1,Ts);
0041
0042
0043 [Gm,Pm,Wcg,Wcp] = margin(P);
0044 modelPI.A = 2/Gm;
0045 modelPI.T = 2*pi/Wcg;
0046
0047
0048 P = squeeze(freqresp(P,w)).';
0049
0050
0051 Lfig = figure(...
0052 'Name', 'Bode diagram - Open loop function',...
0053 'NumberTitle', 'off',...
0054 'MenuBar', 'figure');
0055 Ffig = figure(...
0056 'Name', 'Bode diagram - Complementary sensitivity',...
0057 'NumberTitle', 'off',...
0058 'MenuBar', 'figure');
0059 Sfig = figure(...
0060 'Name', 'Bode diagram - Sensitivity',...
0061 'NumberTitle', 'off',...
0062 'MenuBar', 'figure');
0063
0064
0065
0066 [K,Ti,Td,N,b] = pid_tuning(modelFOPDT,'ZN (OL)',[],'PID');
0067 Rff = K*(b+1./(Ti*s));
0068 Rfb = K*(1+1./(Ti*s)+(Td*s)./(1+Td*s/N));
0069 L = Rfb.*P;
0070 F = (Rff.*P)./(ones(1,length(P))+(Rfb.*P));
0071 S = 1./(ones(1,length(P))+(Rfb.*P));
0072 figure(Lfig)
0073 subplot(211), semilogx(w,20*log10(abs(L)),'b')
0074 subplot(212), semilogx(w,unwrap(angle(L))/pi*180,'b')
0075 figure(Ffig)
0076 subplot(211), semilogx(w,20*log10(abs(F)),'b')
0077 subplot(212), semilogx(w,unwrap(angle(F))/pi*180,'b')
0078 figure(Sfig)
0079 subplot(211), semilogx(w,20*log10(abs(S)),'b')
0080 subplot(212), semilogx(w,unwrap(angle(S))/pi*180,'b')
0081
0082
0083
0084 [K,Ti,Td,N,b] = pid_tuning(modelFOPDT,'KT',2,'PID');
0085 Rff = K*(b+1./(Ti*s));
0086 Rfb = K*(1+1./(Ti*s)+(Td*s)./(1+Td*s/N));
0087 L = Rfb.*P;
0088 F = (Rff.*P)./(ones(1,length(P))+(Rfb.*P));
0089 S = 1./(ones(1,length(P))+(Rfb.*P));
0090 figure(Lfig)
0091 subplot(211), hold on, semilogx(w,20*log10(abs(L)),'r')
0092 subplot(212), hold on, semilogx(w,unwrap(angle(L))/pi*180,'r')
0093 figure(Ffig)
0094 subplot(211), hold on, semilogx(w,20*log10(abs(F)),'r')
0095 subplot(212), hold on, semilogx(w,unwrap(angle(F))/pi*180,'r')
0096 figure(Sfig)
0097 subplot(211), hold on, semilogx(w,20*log10(abs(S)),'r')
0098 subplot(212), hold on, semilogx(w,unwrap(angle(S))/pi*180,'r')
0099
0100
0101
0102
0103 [K,Ti,Td,N,b] = pid_tuning(modelFOPDT,'IMC',Tf/5,'PID');
0104 Rff = K*(b+1./(Ti*s));
0105 Rfb = K*(1+1./(Ti*s)+(Td*s)./(1+Td*s/N));
0106 L = Rfb.*P;
0107 F = (Rff.*P)./(ones(1,length(P))+(Rfb.*P));
0108 S = 1./(ones(1,length(P))+(Rfb.*P));
0109 figure(Lfig)
0110 subplot(211), hold on, semilogx(w,20*log10(abs(L)),'g')
0111 subplot(212), hold on, semilogx(w,unwrap(angle(L))/pi*180,'g')
0112 figure(Ffig)
0113 subplot(211), hold on, semilogx(w,20*log10(abs(F)),'g')
0114 subplot(212), hold on, semilogx(w,unwrap(angle(F))/pi*180,'g')
0115 figure(Sfig)
0116 subplot(211), hold on, semilogx(w,20*log10(abs(S)),'g')
0117 subplot(212), hold on, semilogx(w,unwrap(angle(S))/pi*180,'g')
0118
0119
0120 [K,Ti,Td,N,b] = pid_tuning(modelPI,'ZN (CL)',[],'PID');
0121 Rff = K*(b+1./(Ti*s));
0122 Rfb = K*(1+1./(Ti*s)+(Td*s)./(1+Td*s/N));
0123 L = Rfb.*P;
0124 F = (Rff.*P)./(ones(1,length(P))+(Rfb.*P));
0125 S = 1./(ones(1,length(P))+(Rfb.*P));
0126
0127 figure(Lfig)
0128 subplot(211), semilogx(w,20*log10(abs(L)),'m')
0129 set(gca,'Position', [0.1514 0.4858 0.7536 0.3916],...
0130 'XColor',[0.4 0.4 0.4],'YColor',[0.4 0.4 0.4],...
0131 'FontSize',8,...
0132 'XTickLabel',[])
0133 title('Bode Diagram - Open loop function',...
0134 'Color',[0 0 0],'FontSize',8)
0135 ylabel('Magnitude (dB)',...
0136 'Color',[0 0 0],'FontSize',8)
0137 legend('STEP + ZN(OL)','STEP + KT','STEP + IMC','RELAY + ZN(CL)',4)
0138 subplot(212), semilogx(w,unwrap(angle(L))/pi*180,'m')
0139 set(gca,'Position',[0.1514 0.1100 0.7536 0.3472],...
0140 'XColor',[0.4 0.4 0.4],'YColor',[0.4 0.4 0.4],...
0141 'FontSize',8)
0142 ylabel('Phase (deg)',...
0143 'Color',[0 0 0],'FontSize',8)
0144 xlabel('Frequency (rad/sec)',...
0145 'Color',[0 0 0],'FontSize',8)
0146
0147 figure(Ffig)
0148 subplot(211), semilogx(w,20*log10(abs(F)),'m')
0149 set(gca,'Position', [0.1514 0.4858 0.7536 0.3916],...
0150 'XColor',[0.4 0.4 0.4],'YColor',[0.4 0.4 0.4],...
0151 'FontSize',8,...
0152 'XTickLabel',[])
0153 title('Bode Diagram - Complementary sensitivity function',...
0154 'Color',[0 0 0],'FontSize',8)
0155 ylabel('Magnitude (dB)',...
0156 'Color',[0 0 0],'FontSize',8)
0157 legend('STEP + ZN(OL)','STEP + KT','STEP + IMC','RELAY + ZN(CL)',4)
0158 subplot(212), semilogx(w,unwrap(angle(F))/pi*180,'m')
0159 set(gca,'Position',[0.1514 0.1100 0.7536 0.3472],...
0160 'XColor',[0.4 0.4 0.4],'YColor',[0.4 0.4 0.4],...
0161 'FontSize',8)
0162 ylabel('Phase (deg)',...
0163 'Color',[0 0 0],'FontSize',8)
0164 xlabel('Frequency (rad/sec)',...
0165 'Color',[0 0 0],'FontSize',8)
0166
0167 figure(Sfig)
0168 subplot(211), semilogx(w,20*log10(abs(S)),'m')
0169 set(gca,'Position', [0.1514 0.4858 0.7536 0.3916],...
0170 'XColor',[0.4 0.4 0.4],'YColor',[0.4 0.4 0.4],...
0171 'FontSize',8,...
0172 'XTickLabel',[])
0173 title('Bode Diagram - Sensitivity function',...
0174 'Color',[0 0 0],'FontSize',8)
0175 ylabel('Magnitude (dB)',...
0176 'Color',[0 0 0],'FontSize',8)
0177 legend('STEP + ZN(OL)','STEP + KT','STEP + IMC','RELAY + ZN(CL)',4)
0178 subplot(212), semilogx(w,unwrap(angle(S))/pi*180,'m')
0179 set(gca,'Position',[0.1514 0.1100 0.7536 0.3472],...
0180 'XColor',[0.4 0.4 0.4],'YColor',[0.4 0.4 0.4],...
0181 'FontSize',8)
0182 ylabel('Phase (deg)',...
0183 'Color',[0 0 0],'FontSize',8)
0184 xlabel('Frequency (rad/sec)',...
0185 'Color',[0 0 0],'FontSize',8)