%PID Controller
clear all;
close all;
ts=0.001;
sys=tf([127.4 2817 11930],[1 1501 2021 11730]);
dsys=c2d(sys,ts,'z'); %连续离散化
[num,den]=tfdata(dsys,'v');
u_1=0.0;u_2=0.0;u_3=0.0;
y_1=0.0;y_2=0.0;y_3=0.0;
x=[0,0,0]';
error_1=0;
for k=1:1:1000
time(k)=k*ts;
kp=10;ki=3000;kd=0;
rin(k)=1; %Step Signal
u(k)=kp*x(1)+kd*x(2)+ki*x(3); %PID Controller
%Restricting the output of controller
if u(k)>=10
u(k)=10;
end
if u(k)<=-10
u(k)=-10;
end
%Linear model
yout(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)=rin(k)-yout(k);
u_3=u_2;u_2=u_1;u_1=u(k);
y_3=y_2;y_2=y_1;y_1=yout(k);
x(1)=error(k); %Calculating P
x(2)=(error(k)-error_1)/ts; %Calculating D
x(3)=x(3)+error(k)*ts; %Calculating I
error_1=error(k);
end
figure(1);
plot(time,rin,'b',time,yout,'r');
xlabel('time(s)'),ylabel('rin,yout');
title('单位阶跃响应曲线')
grid minor
评论
查看更多