设计离散PID控制器,各信号的跟踪结果如图所示,其中S代表输入指令信号的类型。通过取余指令 mod实现三角波和锯齿波。当S=1时为三角波,S=2时为锯齿波,S=3时为随机信号。在仿真过程中,如果 D=1,则通过 pause命令实现动态演示仿真。在随机信号跟踪中,对随机信号的变化速率进行了限制。
仿真程序:
%PID Controller
clear all;
close all;
tS=0.001;
[num,den]=tfdata(dsys.,'v);
u_1=0.0;
u_2=0.0;
u_3=0.0;
yd_1=rand;
y_1=0;
y_2=0;
y_3=0;
x=[0,0.,0]';
error_ 1=0;
for k=1: 1:3000
time(k)=k*ts;
kp=1.0;ki=2.0;kd=0.01;
S=3;
if S==1 %Triangle Signal
if mod(time(k),2)<1
yd(k)=mod(time(k).1);
else
yd(k)=1-mod(time(k),1);
end
yd(k)=yd(k)-0.5;
end
if S==2 %Sawtooth Signal
yd(k)=mod(time(k),1.0);
end
if S==3 %Random Signal
yd(k)=rand;
dyd(k)=(yd(k)-yd_1)/ts; %Max speed is 5.0
while abs(dyd(k))>=5.0
yd(k)=rand;
dyd(k)-abs((yd(k)-yd_ 1Vts);
end
end
u(k)=kp*x(1)+kd*x(2)+ki*x(3); %PID Controller
%Restricting the output of
if u(k)>=10
u(k)=10;
end
if u(k)<=-10
u(k)=-10;
end
%Linear model
y(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)=yd(k)-y(k);
yd_1=yd(k);
u_3=u_2;u_2=u_l;u_I=u(k);
y_3=y_2:y_2=y_1:y_1=y(k):
x(1)=error(k); %Calculating P
x(2)(error(k)=error_1/ts; %Calculating D
x(3)=x(3)+error(k)*ts; %Calculating 1
xi(k)-x(3);
error_l=error(k);
D=0;
if D=-1 %Dynamic Simulation Display
plot(time,yd,'b' ,time,y,r);
pause(0.00000000000000000);
end
end
figure(1);
plot(time,yd.'r',time,y,'k:" 'linewidth' ,2);
xlabel('time(s));ylabel('yd,y);
legend('Ideal position signal', 'Position tracking');