PID算法Matlab仿真程序和C程序
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
增量式PID控制算法Matlab仿真程序设一被控对象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; %Sin e 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+nu m(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; %C alculating 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');
微分先行PID算法Matlab仿真程序%PID Controler with differential in advance
clear all;
close all;
ts=20;
sys=tf([1],[60,1],'inputdelay',80); dsys=c2d(sys,ts,'zoh');
[num,den]=tfdata(dsys,'v');
u_1=0;u_2=0;u_3=0;u_4=0;u_5=0;
ud_1=0;
y_1=0;y_2=0;y_3=0;
error_1=0;error_2=0;
ei=0;
for k=1:1:400
time(k)=k*ts;
%Linear model
yout(k)=-den(2)*y_1+num(2)*u_5; kp=0.36;kd=14;ki=0.0021;
rin(k)=1.0*sign(sin(0.00025*2*pi*k*ts ));
error(k)=rin(k)-yout(k);
ei=ei+error(k)*ts;
gama=0.50;
Td=kd/kp;
Ti=0.5;
c1=gama*Td/(gama*Td+ts);
c2=(Td+ts)/(gama*Td+ts);
c3=Td/(gama*Td+ts);
M=1;
if M==1 %PID Control with differential in advance
ud(k)=c1*ud_1+c2*yout(k)-c3*y_ 1;
u(k)=kp*error(k)+ud(k)+ki*ei; elseif M==2 %Simple PID Control u(k)=kp*error(k)+kd*(error(k)-er ror_1)/ts+ki*ei;
end
if u(k)>=110
u(k)=110;
end
if u(k)<=-110
u(k)=-110;
end
%Update parameters u_5=u_4;u_4=u_3;u_3=u_2;u_2=u_1; u_1=u(k);
y_3=y_2;y_2=y_1;y_1=yout(k);
error_2=error_1;
error_1=error(k);
end
figure(1);
plot(time,rin,'r',time,yout,'b'); xlabel('time(s)');ylabel('rin,yout'); figure(2);
plot(time,u,'r');
xlabel('time(s)');ylabel('u');
不完全微分PID算法Matlab仿真程序%PID Controler with Partial differential
clear all;
close all;
ts=20;
sys=tf([1],[60,1],'inputdelay',80); dsys=c2d(sys,ts,'zoh');
[num,den]=tfdata(dsys,'v');
u_1=0;u_2=0;u_3=0;u_4=0;u_5=0; ud_1=0;
y_1=0;y_2=0;y_3=0;
error_1=0;
ei=0;
for k=1:1:100
time(k)=k*ts;
rin(k)=1.0;
%Linear model
yout(k)=-den(2)*y_1+num(2)*u_5; error(k)=rin(k)-yout(k);