增量式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; %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');
微分先行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)-error_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;
memset结构体初始化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);
%PID Controller with partly differential
ei=ei+error(k)*ts;
kc=0.30;
ki=0.0055;
TD=140;
kd=kc*TD/ts;
Tf=180;
Q=tf([1],[Tf,1]); %Low Freq Signal Filter
M=2;
if M==1 %Using PID with Partial differential加在简单PID后的不完全微分
alfa=Tf/(ts+Tf);
u(k)=alfa*u_1+(1-alfa)*(kc*error(k)+kd*(error(k)-error_1)+ki*ei);
u_1=u(k);
elseif M==2 %Using PID with Partial differential只加在微分环节上的不完全微分
alfa=Tf/(ts+Tf);
ud(k)=kd*(1-alfa)*(error(k)-error_1)+alfa*ud_1;
u(k)=kc*error(k)+ud(k)+ki*ei;
ud_1=ud(k);
elseif M==3 %Using Simple PID 简单的PID微分
u(k)=kc*error(k)+kd*(error(k)-error_1)+ki*ei;
end
%Restricting the output of controller
if u(k)>=10
u(k)=10;
end
if u(k)<=-10
u(k)=-10;
end
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_1=error(k);
end
figure(1);
plot(time,rin,'b',time,yout,'r');
xlabel('time(s)');ylabel('rin,yout');
figure(2);
plot(time,u,'r');
xlabel('time(s)');ylabel('u');
figure(3);
plot(time,rin-yout,'r');
xlabel('time(s)');ylabel('error');
figure(4);
bode(Q,'r');
dcgain(Q);
C语言PID演示程序
#include <string.h>
#include<stdio.h>
typedef struct PID{
double Command; //输入指令
double Proportion; //比例系数
double Integral; //积分系数
double Derivative; //微分系数
double preErr; //前一拍误差
double sumErr; //误差累积
}PID;
double PIDCale(PID *p,double feedback)
{
double dErr,Err;
Err=p->Command-feedback; //当前误差
p->sumErr+=Err; //误差累加
dErr=Err-p->preErr; //误差微分
p->preErr=Err;
return(p->Proportion*Err //比例项
+p->Derivative*dErr //微分项
+p->Integral*p->sumErr); //积分项
}
void PIDInit(PID *p)
{
memset(p,0,sizeof(PID)); //初始化
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论