First, fill in the transfer function of the device to be controlled, then determine the parameters for PID control, followed by setting the simulation time and sampling interval. Next, use the anti-integral saturation calculation method to determine the integral result of the PID, and then use the PID output to control the forward function, observing the output signal curve of the entire system’s closed-loop function.
G(s)=(s+3)/(s^3+7*s^2+8*s+1.2);
clear all;close all;clc;
% Parameter settings
timeSample1=0.01; % Sampling interval
num1=200.0;
den1=[1, 35, 160, 0];
sys1=tf(num1,den1);
% Discrete system
sysD1=c2d(sys1, timeSample1, 'z');
% Continuous system
[num2,den2]=tfdata(sysD1,'v');
inU1=0; % Delay time 1
inU2=0; % Delay time 2
inU3=0; % Delay time 3
outY1=0; % Delay time 1
outY2=0; % Delay time 2
outY3=0; % Delay time 3
x1=[0; 0; 0];
err1=0; % Delay time 1
uMax=10; % Maximum PID output
Kp=8.2; % PID parameter
Ki=6.0; % PID parameter
Kd=0.1; % PID parameter
inUsr=36;
Len=2000;
time1=zeros(1, Len);
pidU=zeros(1, Len);
outVecY=zeros(1, Len);
VecErr=zeros(1, Len);
integ1=zeros(1, Len);
for k1=1:1:Len
time1(k1)=k1*timeSample1;
pidU(k1)=Kp*x1(1)+Ki*x1(2)+Kd*x1(3);
if pidU(k1) > uMax
pidU(k1) = uMax;
end
if pidU(k1) < -uMax
pidU(k1) = -uMax;
end
outVecY(k1)=-den2(2)*outY1-den2(3)*outY2-den2(4)*outY3+...
num2(2)*inU1+num2(3)*inU2+num2(4)*inU3;
VecErr(k1)=inUsr-outVecY(k1);
if pidU(k1) >= uMax
if VecErr(k1) > 0
coef1=0;
else
coef1=1;
end
elseif pidU(k1) <= -uMax
if VecErr(k1) > 0
coef1=1;
else
coef1=0;
end
else
coef1=1;
end
inU3=inU2;
inU2=inU1;
inU1=pidU(k1);
outY3=outY2;
outY2=outY1;
outY1=outVecY(k1);
x1(1)=VecErr(k1);
x1(2)=x1(2)+coef1*VecErr(k1)*timeSample1;
x1(3)=(VecErr(k1)-err1)/timeSample1;
err1=VecErr(k1);
integ1(k1)=x1(2);
end
% Plotting results
figure(1);plot(time1,inUsr*ones(1,Len),'b',time1,outVecY,'r');title('Input and Output');
figure(2);plot(time1,pidU,'b');title('PID Output');
figure(3);plot(time1,integ1,'b');title('PID Integral Output');
disp('Calculation complete');