%线性模型仿真 只对下水箱液位进行PID控制
clc
clear all
close all
u=zeros(2,1);
u(1)=0.0;%稳态初值为0
u(2)=0;
Kp=0.1;
Ti=10;
Td=0;
e=[0 0 0];
h2_set1=2.6; %下水箱液位设定值
h2_set=h2_set1-1.4;
h2=1.4;
h=zeros(1,2);
h(1,1)=0;
h(1,2)=0;
hStep = 10;
Hlevel = h;
nCounter = 250;
for t=0:hStep:(nCounter-1)*hStep
e(1)=h2_set-h2;
u(1)=u(1)+Kp*(e(1)-e(2))+(Kp*hStep)/Ti*e(1)+(Kp*Td/hStep)*(e(1)-2*e(2)+e(3));%增量PID
h = Linear_RK4(hStep,t,h,u);
h2=h(1,2);
Hlevel=[Hlevel;h];
e(3)=e(2);
e(2)=e(1);
end
Hlevel(:,1) = Hlevel(:,1)+1.5;
Hlevel(:,2) = Hlevel(:,2)+1.4;
figure(3)
plot([0:hStep:nCounter*hStep]',h2_set1,'r');hold on;
plot([0:hStep:nCounter*hStep]',Hlevel(:,2))
Grid