%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Fuzzy Control System for a Tanker Ship
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% By: Kevin Passino
% Version: 4/15/99
%
% Notes: This program has evolved over time and uses programming
% ideas of Andrew Kwong, Scott Brown, and Brian Klinehoffer.
%
% This program simulates a fuzzy control system for a tanker
% ship. It has a fuzzy controller with two inputs, the error
% in the ship heading (e) and the change in that error (c). The output
% of the fuzzy controller is the rudder input (delta). We want the
% tanker ship heading (psi) to track the reference input heading
% (psi_r). We simulate the tanker as a continuous time system
% that is controlled by a fuzzy controller that is implemented on
% a digital computer with a sampling interval of T.
%
% This program can be used to illustrate:
% - How to code a fuzzy controller (for two inputs and one output,
% illustrating some approaches to simplify the computations, for
% triangular membership functions, and either center-of-gravity or
% center-average defuzzification).
% - How to tune the input and output gains of a fuzzy controller.
% - How changes in plant conditions ("ballast" and "full")
% can affect performance.
% - How sensor noise (heading sensor noise), plant disturbances
% (wind hitting the side of the ship), and plant operating
% conditions (ship speed) can affect performance.
% - How improper choice of the scaling gains can result in
% oscillations (limit cycles).
% - How an improper choice of the scaling gains (or rule base) can
% result in an unstable system.
% - The shape of the nonlinearity implemented by the fuzzy controller
% by plotting the input-output map of the fuzzy controller.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear % Clear all variables in memory
% Initialize ship parameters
% (can test two conditions, "ballast" or "full"):
ell=350; % Length of the ship (in meters)
u=5; % Nominal speed (in meters/sec)
%u=3; % A lower speed where the ship is more difficult to control
abar=1; % Parameters for nonlinearity
bbar=1;
% The parameters for the tanker under "ballast" conditions
% (a heavy ship) are:
K_0=5.88;
tau_10=-16.91;
tau_20=0.45;
tau_30=1.43;
% The parameters for the tanker under "full" conditions (a ship
% that weighs less than one under "ballast" conditions) are:
%K_0=0.83;
%tau_10=-2.88;
%tau_20=0.38;
%tau_30=1.07;
% Some other parameters are:
K=K_0*(u/ell);
tau_1=tau_10*(ell/u);
tau_2=tau_20*(ell/u);
tau_3=tau_30*(ell/u);
% Initialize parameters for the fuzzy controller
nume=11; % Number of input membership functions for the e
% universe of discourse (can change this but must also
% change some variables below if you make such a change)
numc=11; % Number of input membership functions for the c
% universe of discourse (can change this but must also
% change some variables below if you make such a change)
% Next, we define the scaling gains for tuning membership functions for
% universes of discourse for e, change in e (what we call c) and
% delta. These are g1, g2, and g0, respectively
% These can be tuned to try to improve the performance.
% First guess:
g1=1/pi;,g2=100;,g0=8*pi/18; % Chosen since:
% g1: The heading error is at most 180 deg (pi rad)
% g2: Just a guess - that ship heading will change at most
% by 0.01 rad/sec (0.57 deg/sec)
% g0: Since the rudder is constrained to move between +-80 deg
% Tuning:
g1=1/pi;,g2=200;,g0=8*pi/18; % Try to reduce the overshoot
g1=2/pi;,g2=250;,g0=8*pi/18; % Try to speed up the response a bit but to do this
% have to raise g2 a bit to avoid overshoot. Take these
% as "good" tuned values.
%g1=2/pi;,g2=0.000001;,g0=2000*pi/18; % Values tuned to get an oscillation (limit
% cycle) for COG, ballast,
% and nominal speed with no sensor
% noise or rudder disturbance):
% g1: Leave as before
% g2: Essentially turn off the derivative gain
% since this help induce an oscillation
% g0: Make this big to force the limit cycle
% In this case simulate for 16,000 sec.
%g1=2/pi;,g2=250;,g0=-8*pi/18; % Values tuned to get an instability
% g0: Make this negative so that when there
% is an error the rudder will drive the
% heading in the direction to increase the error
% Next, define some parameters for the membership functions
we=0.2*(1/g1);
% we is half the width of the triangular input membership
% function bases (note that if you change g0, the base width
% will correspondingly change so that we always end
% up with uniformly distributed input membership functions)
% Note that if you change nume you will need to adjust the
% "0.2" factor if you want membership functions that
% overlap in the same way.
wc=0.2*(1/g2);
% Similar to we but for the c universe of discourse
base=0.4*g0;
% Base width of output membership fuctions of the fuzzy
% controller
% Place centers of membership functions of the fuzzy controller:
% Centers of input membership functions for the e universe of
% discourse of fuzzy controller (a vector of centers)
ce=[-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1]*(1/g1);
% Centers of input membership functions for the c universe of
% discourse of fuzzy controller (a vector of centers)
cc=[-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1]*(1/g2);
% This next matrix specifies the rules of the fuzzy controller.
% The entries are the centers of the output membership functions.
% This choice represents just one guess on how to synthesize
% the fuzzy controller. Notice the regularity
% of the pattern of rules. Notice that it is scaled by g0, the
% output scaling factor, since it is a normalized rule base.
% The rule base can be tuned to try to improve performance.
rules=[1 1 1 1 1 1 0.8 0.6 0.3 0.1 0;
1 1 1 1 1 0.8 0.6 0.3 0.1 0 -0.1;
1 1 1 1 0.8 0.6 0.3 0.1 0 -0.1 -0.3;
1 1 1 0.8 0.6 0.3 0.1 0 -0.1 -0.3 -0.6;
1 1 0.8 0.6 0.3 0.1 0 -0.1 -0.3 -0.6 -0.8;
1 0.8 0.6 0.3 0.1 0 -0.1 -0.3 -0.6 -0.8 -1;
0.8 0.6 0.3 0.1 0 -0.1 -0.3 -0.6 -0.8 -1 -1;
0.6 0.3 0.1 0 -0.1 -0.3 -0.6 -0.8 -1 -1 -1;
0.3 0.1 0 -0.1 -0.3 -0.6 -0.8 -1 -1 -1 -1;
0.1 0 -0.1 -0.3 -0.6 -0.8 -1 -1 -1 -1 -1;
0 -0.1 -0.3 -0.6 -0.8 -1 -1 -1 -1 -1 -1]*g0;
% Now, you can proceed to do the simulation or simply view the nonlinear
% surface generated by the fuzzy controller.
flag1=input('\n Do you want to simulate the \n fuzzy control system \n for the tanker? \n (type 1 for yes and 0 for no) ');
if flag1==1,
% Next, we initialize the simulation:
t=0; % Reset time to zero
index=1; % This is time's index (not time, its index).
tstop=4000; % Stopping time for the simulation (in seconds)
step=1; % Integration step size
T=10; % The controller is implemented in discrete time and
% this is the sampling time for the controller.
% Note that the integration step size and the sampling
% time are not the same. In this way we seek to simulate
% the continuous time system via the Runge-Kutta method and
% the discrete time fuzzy controller as if it were
% implemented by a digital computer. Hence, we sample
% the plant output every T seconds and at that time
% output a new value of the controller output.
counter=10; % This counter will be used to count the number of integration
% steps that have been taken in the current sampling interval.
% Set it to 10 to begin so that it will compute a fuzzy controller
% output at the first step.