按模拟PID控制算法,
以一系列的采样时刻点kT代表连续时间t,以矩形法数值积分近似代替积分,以一阶后向查分近似代替微分,即
可得离散PID表达式
式中,T为采样周期,k为采样序号。控制系统图和程序框图如下所示。
根据实际情况,对控制器输出进行限幅。
电机模型,M函数,ODE45求微分方程。
chap1_6.m |
%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;
yd(k)=0.50*sin(1*2*pi*k*ts);
para=u_1; tSpan=[0 ts]; [tt,xx]=ode45('chap1_6plant',tSpan,xk,[],para); xk = xx(length(xx),:); y(k)=xk(1);
e(k)=yd(k)-y(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)<-10.0 u(k)=-10.0; end
u_1=u(k); e_1=e(k); end figure(1); plot(time,yd,'r',time,y,'k:','linewidth',2); xlabel('time(s)');ylabel('yd,y'); legend('Ideal position signal','Position tracking'); figure(2); plot(time,yd-y,'r','linewidth',2); xlabel('time(s)'),ylabel('error'); |
chap1_6plant |
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; |