在《先进PID控制MATLAB仿真》中有这样一个例子:
被控对象为一电动机模型的传递函数: ,式中,J=0.0067,B=0.10。输入指令信号为
,
利用ODE45求解连续对象方程,采用PID控制方法设计控制器,其中
控制主程序如下:
%Discrete PID control for continuous plant
clear all;
close all;
ts=0.001; %Sampling time
xk=zeros(2,1);
e_1=0;
u_1=0;
for k=1:1:2000
time(k) = k*ts;
rin(k)=0.50*sin(1*2*pi*k*ts);
para=u_1; % D/A
tSpan=[0 ts];
[tt,xx]=ode45('chap1_6f',tSpan,xk,[],para);
xk =
xx(length(xx),:); % A/D
yout(k)=xk(1);
e(k)=rin(k)-yout(k);
de(k)=(e(k)-e_1)/ts;
u(k)=20.0*e(k)+0.50*de(k);
%Control limit
if u(k)>10.0
u(k)=10.0;
end
if u(k)
u(k)=-10.0;
end
u_1=u(k);
e_1=e(k);
end
figure(1);
plot(time,rin,'r',time,yout,'b');
xlabel('time(s)'),ylabel('rin,yout');
figure(2);
plot(time,rin-yout,'r');
xlabel('time(s)'),ylabel('error');
连续对象子函数:chap1_6f.m
function dy = PlantModel(t,y,flag,para)
u=para;
J=0.0067;B=0.1;
dy=zeros(2,1);
dy(1) = y(2);
dy(2) = -(B/J)*y(2) + (1/J)*u;
根据函数句柄和匿名函数的定义,程序可修改为如下形式:
%Discrete PID control for continuous plant
clear all;
close all;
ts=0.001; %Sampling time
xk=zeros(2,1);
e_1=0;
u_1=0;
for k=1:1:2000
time(k) = k*ts;
rin(k)=0.50*sin(1*2*pi*k*ts);
para=u_1; % D/A
tSpan=[0 ts];
J=0.0067;B=0.1;
PlantModel =@(t,xk,para)([ xk(2); -(B/J)*xk(2) + (1/J)*u_1])
;
[tt,xx]=ode45(PlantModel,tSpan,xk, [],para);
xk =
xx(length(xx),:); % A/D
yout(k)=xk(1);
e(k)=rin(k)-yout(k);
de(k)=(e(k)-e_1)/ts;
u(k)=20.0*e(k)+0.50*de(k);
%Control limit
if u(k)>10.0
u(k)=10.0;
end
if u(k)
u(k)=-10.0;
end
u_1=u(k);
e_1=e(k);
end
figure(1);
plot(time,rin,'r',time,yout,'b');
xlabel('time(s)'),ylabel('rin,yout');
figure(2);
plot(time,rin-yout,'r');
xlabel('time(s)'),ylabel('error');
figure1
figure2