📄 ctrdemo.m
字号:
function ctrdemo
% CTRDEMO: To be called from FISDEMO.M.
% FISMAT: Fuzzy Inference Systems toolbox for MATLAB
% (c) A. Lotfi, University of Queensland (Email: lotfia@s1.elec.uq.oz.au)
% 13-10-93
% The program has been tested on MATLAB version 4.1, Sun workstation.
%#call mfplot
global sli_vol_t sli_vol_h tem_plot hum_plot A1 Tem A2 Hum B Speed speed_plot fan_plot
echo on
load ctrdemo.mat
clc;
% To illustrate application of approximate reasoning methods in close
% loop control systems, truck backer-upper control system , inverted
% pendulum system and Fan Speed control are taken as test_bed.
% Backing a truck to a loading dock is a nonlinear control problem which
% can involve extensive computation time to steer the truck to a
% prescribed loading zone. The dynamic of truck backer-upper is given
% in truck.m file. The input of this function are position
% of truck (x,y in yard), azimuth angle (Phi) of truck and the steering
% angle of truck (Theta). The function will return the next state of
% system.
% The range of variables for simulated truck and controllers are as follows;
X=[0:4:100];
Phi=[-90:10:270];
Theta=[-30:2:30];
pause % Press any key to continue
clc;
% Since we presuppose adequate clearance between the truck and the loading
% dock, state variable Y can be abandoned. Therefore the inputs of
% controller are X and Phi.
% The control goal is to steer the truck from any initial position to
% prespecified loading dock with a right azimuth angle (Phi_final=90) and
% coincided rear position. The steering angle Theta is the control action
% which is provided by the designed fuzzy controller.
% There are different control strategy for this control problem. In this
% demonstration we show that the truck control is possible only with
% maximum 9 rules.
[a1,b1,c1,a2,b2,c2,ahat,bhat,chat]=t_rule9;
[A1,A2,B]=rulebase(1,X,a1,b1,c1,1,Phi,a2,b2,c2,1,Theta,ahat,bhat,chat);
mfplot(A1,X,A2,Phi,B,Theta);
pause % Press any key to continue
clc;
% We can select any approximate reasoning methods explained earlier,
% the method we are going to use here is MAMDANI.
% The initial state of truck must be given for running simulation. We can
% assume;
truck_state_0=[20,20,90]' %[X_position, Y_position, Azimuth angle Phi]
%>> state=[];trst=truck_state_0;
%>> for i=1:100
%>> [fuzzy_decision]=mamdani(A1,trst(1),X,A2,trst(3),Phi,U2,B,1,1);
%>> steer_angle=defzfir(fuzzy_decision,Theta,1);
%>> trst=truck(trst,steer_angle);
%>> state=[state, trst];
%>> end
pause % Press any key to continue
clc
% The truck trajectory from three initial position has been shown.
% Moreover the fuzzy decision and its defuzzified value are shown on
% the smaller box. This tracking ability are related to Mamdani approach
% (Min-Max) and centroid defuzzification.
% Wait Please ........
echo off;
trst=truck_state_0;
state1=[];state2=[];state3=[];sangle1=[];sangle2=[];sangle3=[];
clf;set(gcf,'units','normal','position',[.44 .55 .55 .4])
truck_axes=axes('units','normal','position',[.3 .1 .6 .8]);
truck_plot=plot(0,0);
axis([0 100 0 100]);
set(gca,'xtick',[0 10 20 30 40 50 60 70 80 90 100])
xlabel('X position');ylabel('Y position');title('Loading dock');
set(truck_plot,'erasemode','none','linestyle','*','color','r')
deci_axes=axes('position',[.03,.1,.2,.2]);
deci_plot=plot(Theta,zeros(size(Theta)),0,0,'r*');
axis([-30 30 0 2]);set(deci_axes,'xtick',[-30 0 30],'ytick',[0 2]);
xlabel('Steering angle');title('Fuzzy Decision')
set(deci_plot,'erasemode','xor')
truck_state_0=[20,20,90]'; %[X_position, Y_position, Azimuth angle Phi]
text(20,20,'Initial Position');
trst=truck_state_0;state=[];
step=1;
while step < 300
state1=[state1,trst];
[fuzzy_decision]=mamdani(A1,trst(1),X,A2,trst(3),Phi,B,1,1);
steer_angle=defzfir(fuzzy_decision,Theta,1);
set(deci_plot(1),'ydata',fuzzy_decision)
set(deci_plot(2),'xdata',steer_angle)
trst=truck(trst,steer_angle);
if trst(2) >= 100
step=301;
end
set(truck_plot,'xdata',trst(1),'ydata',trst(2));
drawnow
step=step+1;
end
set(truck_plot,'color','y')
truck_state_0=[80,30,120]';
trst=truck_state_0;state=[];
step=1;
while step < 300
[fuzzy_decision]=mamdani(A1,trst(1),X,A2,trst(3),Phi,B,1,1);
steer_angle=defzfir(fuzzy_decision,Theta,1);
set(deci_plot(1),'ydata',fuzzy_decision)
set(deci_plot(2),'xdata',steer_angle)
trst=truck(trst,steer_angle);
if trst(2) >= 100
step=301;
end
set(truck_plot,'xdata',trst(1),'ydata',trst(2));
drawnow
step=step+1;
end
set(truck_plot,'color','g')
truck_state_0=[60,40,-90]';
trst=truck_state_0;state=[];
step=1;
while step < 300
[fuzzy_decision]=mamdani(A1,trst(1),X,A2,trst(3),Phi,B,1,1);
steer_angle=defzfir(fuzzy_decision,Theta,1);
set(deci_plot(1),'ydata',fuzzy_decision)
set(deci_plot(2),'xdata',steer_angle)
trst=truck(trst,steer_angle);
if trst(2) >= 100
step=301;
end
set(truck_plot,'xdata',trst(1),'ydata',trst(2));
drawnow
step=step+1;
end
echo on;
pause % Press any key to continue
clc;
% Calculated steering angle is a continuse variable in the universe of Theta.
% We may want to quantize this control action and then applying it to truck.
% The function that can quantize the variable with a quantum size Q.
%>> for i=1:100
%>> [fuzzy_decision]=mamdani(A1,trst(1),X,A2,trst(3),Phi,U2,B,1,1);
%>> steer_angle=defzfir(fuzzy_decision,Theta,1);
%>> steer_angle_q=quantize(steer_angle,2);
%>> trst=truck(trst,steer_angle_q);
%>> end
% Wait Please ........
echo off;
trst=truck_state_0;
state1=[];state2=[];state3=[];sangle1=[];sangle2=[];sangle3=[];
clf;set(gcf,'units','normal','position',[.44 .55 .55 .4])
truck_axes=axes('units','normal','position',[.3 .1 .6 .8]);
truck_plot=plot(0,0);
axis([0 100 0 100]);
set(gca,'xtick',[0 10 20 30 40 50 60 70 80 90 100])
xlabel('X position');ylabel('Y position');title('Loading dock');
set(truck_plot,'erasemode','none','linestyle','*','color','r')
deci_axes=axes('position',[.03,.1,.2,.2]);
deci_plot=plot(Theta,zeros(size(Theta)),0,0,'r*');
axis([-30 30 0 2]);set(deci_axes,'xtick',[-30 0 30],'ytick',[0 2]);
xlabel('Steering angle');title('Fuzzy Decision')
set(deci_plot,'erasemode','xor')
truck_state_0=[20,20,90]'; %[X_position, Y_position, Azimuth angle Phi]
text(20,20,'Initial Position');
trst=truck_state_0;state=[];
step=1;
while step < 300
state1=[state1,trst];
[fuzzy_decision]=mamdani(A1,trst(1),X,A2,trst(3),Phi,B,1,1);
steer_angle=defzfir(fuzzy_decision,Theta,1);
steer_angle_q=quantize(steer_angle,2);
set(deci_plot(1),'ydata',fuzzy_decision)
set(deci_plot(2),'xdata',steer_angle)
trst=truck(trst,steer_angle_q);
if trst(2) >= 100
step=301;
end
set(truck_plot,'xdata',trst(1),'ydata',trst(2));
drawnow
step=step+1;
end
set(truck_plot,'color','y')
truck_state_0=[80,30,120]';
trst=truck_state_0;state=[];
step=1;
while step < 300
[fuzzy_decision]=mamdani(A1,trst(1),X,A2,trst(3),Phi,B,1,1);
steer_angle=defzfir(fuzzy_decision,Theta,1);
steer_angle_q=quantize(steer_angle,2);
set(deci_plot(1),'ydata',fuzzy_decision)
set(deci_plot(2),'xdata',steer_angle)
trst=truck(trst,steer_angle_q);
if trst(2) >= 100
step=301;
end
set(truck_plot,'xdata',trst(1),'ydata',trst(2));
drawnow
step=step+1;
end
set(truck_plot,'color','g')
truck_state_0=[60,40,-90]';
trst=truck_state_0;state=[];
step=1;
while step < 300
[fuzzy_decision]=mamdani(A1,trst(1),X,A2,trst(3),Phi,B,1,1);
steer_angle=defzfir(fuzzy_decision,Theta,1);
steer_angle_q=quantize(steer_angle,2);
set(deci_plot(1),'ydata',fuzzy_decision)
set(deci_plot(2),'xdata',steer_angle)
trst=truck(trst,steer_angle_q);
if trst(2) >= 100
step=301;
end
set(truck_plot,'xdata',trst(1),'ydata',trst(2));
drawnow
step=step+1;
end
echo on;
pause % Press any key to continue
clc;
% To investigate the control strategy globally, we can construct the decision
% surface related to any fuzzy approximation method.
% Wait Please .......
%>> [S_truck9]=ctrsurf('mamdani',A1,X,A2,Phi,B,Theta,1,1);
clf;set(gcf,'units','normal','position',[.54 .55 .45 .4])
cont_surf=mesh(Phi,X,rot90(S_truck9));
xlabel('X Position');ylabel('Azimuth angle');zlabel('Steering angle');
title('Cnntrol Surface for Truck Backer-Upper Control 9 Rules')
pause % Press any key to continue
clc;
% Now we can compare the control performance with 9 rules and 36 rules
% as proposed by Kosko.
[a1,b1,c1,a2,b2,c2,ahat,bhat,chat]=t_rule36;
[A1,A2,B]=rulebase(1,X,a1,b1,c1,1,Phi,a2,b2,c2,1,Theta,ahat,bhat,chat);
% The approximate reasoning that we are going to use in this part of
% demonstration is LARSEN. (The initial positions are as before)
%>> state=[];trst=truck_state_0;
%>> for i=1:100
%>> [fuzzy_decision]=larsen(A1,trst(1),X,A2,trst(3),Phi,U2,B,1,1);
%>> steer_angle=defzfir(fuzzy_decision,Theta,1);
%>> trst=truck(trst,steer_angle);
%>> state=[state,trst];
%>> end
% Wait Please ........
echo off;
truck_state_0=[20,20,90]'; %[X_position, Y_position, Azimute angle Phi]
trst=truck_state_0;
state1=[];state2=[];state3=[];sangle1=[];sangle2=[];sangle3=[];
clf;set(gcf,'units','normal','position',[.44 .55 .55 .4])
truck_axes=axes('units','normal','position',[.3 .1 .6 .8]);
truck_plot=plot(0,0);
axis([0 100 0 100]);
set(gca,'xtick',[0 10 20 30 40 50 60 70 80 90 100])
xlabel('X position');ylabel('Y position');title('Loading dock');
set(truck_plot,'erasemode','none','linestyle','*','color','r')
deci_axes=axes('position',[.03,.1,.2,.2]);
deci_plot=plot(Theta,zeros(size(Theta)),0,0,'r*');
axis([-30 30 0 2]);set(deci_axes,'xtick',[-30 0 30],'ytick',[0 2]);
xlabel('Steering angle');title('Fuzzy Decision')
set(deci_plot,'erasemode','xor')
truck_state_0=[20,20,90]'; %[X_position, Y_position, Azimuth angle Phi]
text(20,20,'Initial Position');
trst=truck_state_0;state=[];
step=1;
while step < 300
[fuzzy_decision]=larsen(A1,trst(1),X,A2,trst(3),Phi,B,1,1);
steer_angle=defzfir(fuzzy_decision,Theta,1);
set(deci_plot(1),'ydata',fuzzy_decision)
set(deci_plot(2),'xdata',steer_angle)
trst=truck(trst,steer_angle);
if trst(2) >= 100
step=301;
end
set(truck_plot,'xdata',trst(1),'ydata',trst(2));
drawnow
step=step+1;
end
set(truck_plot,'color','y')
truck_state_0=[80,30,120]';
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -