First, fill in the transfer function of the continuous system, determine the sampling time interval, then convert it into the transfer function of the discrete system, and finally determine the PID tuning coefficients. Input a sine signal and observe the system’s output signal.
%% Example of PID tuning for discrete control system
%%%%%%%%%%%
clear all;close all;clc;
%%%%%%%%%%%%%
sampleTime1=0.01 ; %% Sampling time
sys1=tf(9000,[1, 70, 1000, 0]); %% Continuous system
sysD=c2d(sys1, sampleTime1, ‘z’) ;
[num1,den1]=tfdata(sysD, ‘v’);
disp(‘Discrete system transfer function:’);
printsys(num1,den1);
inX1=0; %% Input delay variable
inX2=0;
inX3=0;
outY1=0; %% Output delay variable
outY2=0;
outY3=0;
x1=[0; 0; 0];
err1=0; %% Output error
Len=3000;
time1=zeros(1,Len);
inX=zeros(1,Len);
outY=zeros(1,Len);
u1=zeros(1,Len);
errOut=zeros(1,Len);
Kp=5.0; %% PID coefficient
Ki=4.2; %% PID coefficient
Kd=0.1; %% PID coefficient
w1=0.5; %% Sine frequency
%%%%%%%%%%%%%
for k1=1:1:Len
time1(k1)=k1*sampleTime1 ;
inX(k1)=5.0*sin(2*pi*w1*k1*sampleTime1) ;
u1(k1)=Kp*x1(1)+Ki*x1(2)+Kd*x1(3) ; %% PID tuning
if u1(k1) >= 20 u1(k1) = 20 ; end
if u1(k1) <= -20 u1(k1) = -20 ; end
outY(k1)=-den1(2)*outY1-den1(3)*outY2-den1(4)*outY3+… num1(2)*inX1+num1(3)*inX2+num1(4)*inX3;
errOut(k1)=inX(k1)-outY(k1) ;
%%%%%%%%%%
inX3=inX2; %% Save input delay variable
inX2=inX1;
inX1=u1(k1); %% PID tuning
outY3=outY2 ; %% Save output delay variable
outY2=outY1 ;
outY1=outY(k1) ;
x1(1)=errOut(k1) ; %% PID tuning
x1(2)=x1(2)+errOut(k1)*sampleTime1 ;
x1(3)=(errOut(k1)-err1)/sampleTime1 ;
err1=errOut(k1);
end
figure(1);plot(time1,inX,’b’,time1,outY,’r’);title(‘Input and Output Curves.’);xlabel(‘t(s)’);ylabel(‘y’);legend(‘Input x’, ‘Output y’);
figure(2);plot(time1, outY-inX, ‘b’);title(‘Output and Input Error.’);xlabel(‘t(s)’);ylabel(‘y-x’);legend(‘y-x’);
disp(‘End.’);