增量式PID控制算法Matlab仿真程序
时间:11-30 14:15 阅读:1662次
*温馨提示:点击图片可以放大观看高清大图
简介:设一被控对象G(s)=50/(0.125s^2+7s),用增量式PID控制算法编写仿真程序(输入分别为单位阶跃、正弦信号,采样时间为1ms,控制器输出限幅:[-5,5],仿真曲线包括系统输出及误差曲线,并加上注释、图例)。
clear all;
close all;
ts=0.001;
sys=tf(50,[0.125,7, 0]);
dsys=c2d(sys,ts,'z');
[num,den]=tfdata(dsys,'v');
u_1=0.0;u_2=0.0;
y_1=0.0;y_2=0.0;
x=[0,0,0]';
error_1=0;
error_2=0;
for k=1:1:1000
time(k)=k*ts;
S=2;
if S==1
kp=10;ki=0.1;kd=15;
rin(k)=1; %Step Signal
elseif S==2
kp=10;ki=0.1;kd=15; %Sine Signal
rin(k)=0.5*sin(2*pi*k*ts);
end
du(k)=kp*x(1)+kd*x(2)+ki*x(3); %PID Controller
u(k)=u_1+du(k);
%Restricting the output of controller
if u(k)>=5
u(k)=5;
end
if u(k)<=-5
u(k)=-5;
end
%Linear model
yout(k)=-den(2)*y_1-den(3)*y_2+num(2)*u_1+num(3)*u_2;
error(k)=rin(k)-yout(k);
%Return of parameters
u_2=u_1;u_1=u(k);
y_2=y_1;y_1=yout(k);
x(1)=error(k)-error_1; %Calculating P
x(2)=error(k)-2*error_1+error_2; %Calculating D
x(3)=error(k); %Calculating I
error_2=error_1;
error_1=error(k);
end
figure(1);
plot(time,rin,'b',time,yout,'r');
xlabel('time(s)'),ylabel('rin,yout');
figure(2);
plot(time,error,'r')
xlabel('time(s)');ylabel('error');