这两天看了模糊控制,初步了解了模糊控制的思想,下面附上一个Matlab上的模糊自适应PID程序
clc;
clear;
a = newfis('fuzzypid'); % 新建模糊推理系统 名称是fuzzypid
% 设置输入e的范围、隶属度函数;
a = addvar(a,'input','e',[-3 3]); % 变量的范围 类型
a = addmf(a,'input',1,'NB','zmf',[-3 -1]); % NB
a = addmf(a,'input',1,'NM','trimf',[-3 -2 0]); % NM
a = addmf(a,'input',1,'NS','trimf',[-3 -1 1]); % NS
a = addmf(a,'input',1,'Z','trimf',[-2 0 2]); % Z
a = addmf(a,'input',1,'PS','trimf',[-1 1 3]); % PS
a = addmf(a,'input',1,'PM','trimf',[0 2 3]); % PM
a = addmf(a,'input',1,'PB','smf',[1 3]); % PB
%figure(1);
% subplot(5,1,1);
% plotmf(a,'input',1); % 绘制给定变量的所有隶属的曲线
% 设置输入ec的范围、隶属度函数;
a = addvar(a,'input','ec',[-3 3]); % 变量的范围 类型
a = addmf(a,'input',2,'NB','zmf',[-3 -1]); % NB
a = addmf(a,'input',2,'NM','trimf',[-3 -2 0]); % NM
a = addmf(a,'input',2,'NS','trimf',[-3 -1 1]); % NS
a = addmf(a,'input',2,'Z','trimf',[-2 0 2]); % Z
a = addmf(a,'input',2,'PS','trimf',[-1 1 3]); % PS
a = addmf(a,'input',2,'PM','trimf',[0 2 3]); % PM
a = addmf(a,'input',2,'PB','smf',[1 3]); % PB
%figure(2);
% subplot(5,1,2);
% plotmf(a,'input',2); % 绘制给定变量的所有隶属的曲线
% 设置输出Kp的范围、隶属度函数;
a = addvar(a,'output','Kp',[-0.3 0.3]); % 变量的范围 类型
a = addmf(a,'output',1,'NB','zmf',[-0.3 -0.1]); % NB
a = addmf(a,'output',1,'NM','trimf',[-0.3 -0.2 0]); % NM
a = addmf(a,'output',1,'NS','trimf',[-0.3 -0.1 0.1]); % NS
a = addmf(a,'output',1,'Z','trimf',[-0.2 0 0.2]); % Z
a = addmf(a,'output',1,'PS','trimf',[-0.1 0.1 0.3]); % PS
a = addmf(a,'output',1,'PM','trimf',[0 0.2 0.3]); % PM
a = addmf(a,'output',1,'PB','smf',[0.1 0.3]); % PB
%figure(3);
% subplot(5,1,3);
% plotmf(a,'output',1); % 绘制给定变量的所有隶属的曲线
% 设置输出Ki的范围、隶属度函数;
a = addvar(a,'output','Ki',[-0.06 0.06]); % 变量的范围 类型
a = addmf(a,'output',2,'NB','zmf',[-0.06 -0.02]); % NB
a = addmf(a,'output',2,'NM','trimf',[-0.06 -0.04 0]); % NM
a = addmf(a,'output',2,'NS','trimf',[-0.06 -0.02 0.02]); % NS
a = addmf(a,'output',2,'Z','trimf',[-0.04 0 0.04]); % Z
a = addmf(a,'output',2,'PS','trimf',[-0.02 0.02 0.06]); % PS
a = addmf(a,'output',2,'PM','trimf',[0 0.04 0.06]); % PM
a = addmf(a,'output',2,'PB','smf',[0.02 0.06]); % PB
%figure(4);
% subplot(5,1,4);
% plotmf(a,'output',2); % 绘制给定变量的所有隶属的曲线
% 设置输出Kd的范围、隶属度函数;
a = addvar(a,'output','Kd',[-3 3]); % 变量的范围 类型
a = addmf(a,'output',3,'NB','zmf',[-3 -1]); % NB
a = addmf(a,'output',3,'NM','trimf',[-3 -2 0]); % NM
a = addmf(a,'output',3,'NS','trimf',[-3 -1 1]); % NS
a = addmf(a,'output',3,'Z','trimf',[-2 0 2]); % Z
a = addmf(a,'output',3,'PS','trimf',[-1 1 3]); % PS
a = addmf(a,'output',3,'PM','trimf',[0 2 3]); % PM
a = addmf(a,'output',3,'PB','smf',[1 3]); % PB
%figure(5);
% subplot(5,1,5);
% plotmf(a,'output',3); % 绘制给定变量的所有隶属的曲线
% 设置模糊规则
rulelist = [ % e ec Kp Ki Kd important and/or
1 1 7 1 5 1 1;
1 2 7 1 3 1 1;
1 3 6 2 1 1 1;
1 4 6 2 1 1 1;
1 5 5 3 1 1 1 ;
1 6 4 4 2 1 1;
1 7 4 4 5 1 1;
2 1 7 1 5 1 1;
2 2 7 1 5 1 1;
2 3 6 2 1 1 1;
2 4 5 3 2 1 1;
2 5 5 3 2 1 1;
2 6 4 4 3 1 1;
2 7 3 4 4 1 1;
3 1 6 1 4 1 1;
3 2 6 2 3 1 1;
3 3 6 3 2 1 1;
3 4 5 3 2 1 1;
3 5 4 4 3 1 1;
3 6 3 5 3 1 1;
3 7 3 5 4 1 1;
4 1 6 2 4 1 1;
4 2 6 2 3 1 1;
4 3 5 3 3 1 1;
4 4 4 4 3 1 1;
4 5 3 5 3 1 1;
4 6 2 6 3 1 1;
4 7 2 6 4 1 1;
5 1 5 2 4 1 1;
5 2 5 3 4 1 1;
5 3 4 4 4 1 1;
5 4 3 5 4 1 1;
5 5 3 5 4 1 1;
5 6 2 6 4 1 1;
5 7 2 7 4 1 1;
6 1 5 4 7 1 1;
6 2 4 4 5 1 1;
6 3 3 5 5 1 1;
6 4 2 5 5 1 1;
6 5 2 6 5 1 1;
6 6 2 7 5 1 1;
6 7 1 7 7 1 1;
7 1 4 4 7 1 1;
7 2 4 4 6 1 1;
7 3 2 5 6 1 1;
7 4 2 6 6 1 1;
7 5 2 6 5 1 1;
7 6 1 7 5 1 1;
7 7 1 7 7 1 1;
];
a = addrule(a, rulelist);
a = setfis(a, 'DefuzzMethod','mom'); % mom 平均最大隶属度法
writefis(a,'fuzzpid');
a = readfis('fuzzpid');
% PID控制器设定
ts = 0.001;
%sys = tf(5e5,[1,80,1.5e4,0]);
sys = tf(7e4,[1,30,1e5,0]);
dsys = c2d(sys,ts,'tustin'); % 变连续系统为离散系统 zoh:零阶保持器 foh:一阶保持器 tustin:双先行变换
[num,den]=tfdata(dsys,'v');
u_1=0.0; u_2=0.0; u_3=0.0;
y_1=0.0; y_2=0.0; y_3=0.0;
x=[0,0,0]';
error_1 = 0;
e_1=0.0; ec_1=0.0;
kp0=0.4; kd0=1.0; ki0=0.0;
for k=1:1:500
time(k)=k*ts;
rin(k)=1;
k_pid = evalfis([e_1,ec_1],a); % 模糊得到pid
kp(k) = kp0 + k_pid(1);
ki(k) = ki0 + k_pid(2);
kd(k) = kd0 + k_pid(3);
u(k) = kp(k)*x(1) + kd(k)*x(2) + ki(k)*x(3);
% 在t=0.35s处增加干扰(在无干扰时此处可省略)
if k==350
u(k)=u(k)+1.5;
end
if u(k)>=10
u(k)=10;
end
if u(k)<=-10
u(k)=10;
end
yout(k)=-den(2)*y_1-den(3)*y_2-den(4)*y_3+num(1)*u(k)+num(2)*u_1+num(3)*u_2+num(4)*u_3;
error(k)=rin(k)-yout(k);
% 返回PID参数
u_3 = u_2;
u_2 = u_1;
u_1 = u(k);
y_3 = y_2;
y_2 = y_1;
y_1 = yout(k);
x(1) = error(k);
x(2) = error(k)-error_1;
x(3) = x(3)+error(k);
e_1=x(1);
ec_1=x(2);
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);
xlabel('time(s)'); ylabel('error');
figure(3);
plot(time,kp);
xlabel('time(s)'); ylabel('kp');
figure(4);
plot(time,ki);
xlabel('time(s)'); ylabel('ki');
figure(5);
plot(time,kd);
xlabel('time(s)'); ylabel('kd');
|