Transcript Document

Implementation of Fuzzy Controllers



The first hardware approaches that implemented fuzzy logic inference
engine were the digital circuits designed by Togai (1986, 1987) and
Watanabe (1990). They designed ASIC's implementing specific
architectures and specialized instructions (MIN & MAX) exhibiting
enhanced processing speed relatively to regular microprocessors that
time.
There was a trend in the market, ten years ago, to establish dedicated
have fuzzy/neural processor chips (NeuraLogix, Intel).
Such vanguard work has not flourished. The advent of powerful and
fast microcontrollers and the availability of advanced tools like
FuzzyTech, TILShell, O’Inca generating efficient software algorithms
for executing fuzzy logic faded away the emergence of dedicated
hardware.





Motorola, Intel, TI and Microchip have some sort of programming
environment for their microcontrollers, with ability of generating code,
or interfacing directly with fuzzy controllers.
Even PLCs (AEG, Siemens, Klockner-Moeller, OMRON, Foxboro,
Allen-Bradley) can have fuzzy controllers embedded on their system.
For sure, writing fuzzy algorithm code in C language is still one of the
the best ways to have it linked to real-time applications. A brief
discussion will be made on using C for such development
FUZZ-C generates C code that may be cross-compiled to the 6805,
Z8C and COP8C microprocessors using separate compilers. FUZZ-C
was reviewed in the March 1993 issue of AI Expert.
In order to have a didactic learning and integration into dynamical
systems we will take more time explaining the use of Matlab and
Simulink to design and evaluate fuzzy controllers.
IMPLENTING FUZZY CONTROL
IN C-LANGUAGE
Building Fuzzy Systems with C Language




C language is the straightforward choice of a language to implement a
fuzzy controller.
It is a very powerful language and the existing compilers allow the
generation of highly optimized code to most hardware systems, which
are very important features to real-time applications.
Following, it is shown an implementation of a fuzzy logic controller
using C language.
The first part of the code defines structures to support the fuzzy
inference process. After, the whole fuzzy inference process is depicted
into several steps, and a C function is implemented to perform each
one of these steps. Finally, a simple control loop function is presented,
which can regulate a physical or simulated process by means of the
implemented fuzzy controller.
Defining support structures
typedef struct
{
int Tnum, Point1, Point2;
double Value, Slope1, Slope2;
struct mf_type *Next;
} mf_type;
// Used to store fuzzy membership functions
// parameters, in a linked list fashion
typedef struct
{
int Vnum, Type, Atuation, Mode, RefConn, ConnR, ConnS, Resol;
double Gain, Value, LastV, DeltaV, LastDV, MinVal, MaxVal;
mf_type *MembershipFunction;
struct io_type *Next;
} io_type;
// Used to store information about the inputs
// and outputs of a fuzzy system, in a linked list fashion
typedef struct
{
double
*Value;
struct rule_element_type *Next;
} rule_element_type;
// Defines a fuzzy evaluation, present at IF and THEN
// parts of a fuzzy rule
typedef struct
{
rule_element_type *IfSide;
rule_element_type *ThenSide;
struct rule_type *Next;
} rule_type;
// Used to store rule base information (IF and THEN parts)
// in a linked list fashion
Defining support functions…
/*
***** Function used to Calculate fuzzy strengths of inputs into each membership
function
*/
void ComputeDegreeOfMembership(Mf, Input)
mf_type *Mf;
double Input;
{
double Delta1, Delta2;
/*
***** Begin ComputeDegreeOfMembership
*/
Delta1 = Input - (double) Mf->Point1;
Delta2 = (double) Mf->Point2 - Input;
if((Delta1 >= 0.0) && (Delta2 >= 0.0))
if( (Mf->Slope1 != 0.0) && (Mf->Slope2 != 0.0) )
Mf->Value = MIN( (Mf->Slope1 * Delta1), (Mf->Slope2 * Delta2) );
else
if(Mf->Slope1 == 0.0)
Mf->Value = MIN( UPPER_LIMIT, (Mf->Slope2 * Delta2) );
else
Mf->Value = MIN( (Mf->Slope1 * Delta1), UPPER_LIMIT );
else
Mf->Value = 0.0;
Mf->Value = MIN(Mf->Value, UPPER_LIMIT);
/*
***** End ComputeDegreeOfMembership
*/
}
Defining support functions…
/*
***** Function used to Calculate the area of a trapezoid object
*/
double TrapezoidArea(Mf)
mf_type *Mf;
{
double Run1 = 0, Run2 = 0, Base, Top, Area;
/*
***** Begin TrapezoidArea
*/
Base = (double) Mf->Point2 - Mf->Point1;
if(fabs(Mf->Slope1 - 0.0) > EPS)
Run1 = Mf->Value / Mf->Slope1;
if(fabs(Mf->Slope2 - 0.0) > EPS)
Run2 = Mf->Value / Mf->Slope2;
Top = Base - Run1 - Run2;
Area = Mf->Value * (Top + Base) / 2;
return( Area );
/*
***** End TrapezoidArea
*/
}
Defining support functions…
/*
***** Function used to Calculate the Center of Gravity of an object
*/
double CentroidPoint(Mf)
mf_type *Mf;
{
double a1, a2, a3, b, c, Point;
/*
***** Begin CentroidPoint
*/
if( fabs(Mf->Value - 0.0) > EPS )
{
if(Mf->Slope1 != 0.0)
b = (double)Mf->Point1 + Mf->Value / Mf ->Slope1;
else
b = (double)Mf->Point1;
if(Mf->Slope2 != 0.0)
c = (double)Mf->Point2 - Mf->Value / Mf ->Slope2;
else
c = (double)Mf->Point2;
a1 = (b - (double)Mf->Point1) * Mf->Value / 2;
a2 = (c - b) * Mf->Value;
a3 = ((double)Mf->Point2 - c) * Mf->Value / 2;
Point = ( a1 * ((double)Mf->Point1 + (b - (double)Mf->Point1) * 2/3) +
a2 * (b + (c - b) * 1/2) +
a3 * (c + ((double)Mf->Point2 - c) * 1/3) )
/ ( a1 + a2 + a3 );
return(Point);
}
else
return(0.0);}
/*
***** End CentroidPoint
*/
Defining fuzzy inference functions…
/*
***** Function used to transform crisp input values into fuzzy inputs
*/
void Fuzzification()
{
io_type *Si;
mf_type *Mf;
/*
***** Begin Fuzzification
*/
for(Si = SystemInput; Si != NULL; Si = Si->Next)
// For each input,
for(Mf = Si->MembershipFunction; Mf != NULL; Mf = Mf->Next)
ComputeDegreeOfMembership(Mf, Si->Value);
// Process Degree of
Membership
/*
***** End Fuzzification
*/
}
Defining fuzzy inference functions…
/*
***** Function used to evaluate a fuzzy rule base, given the fuzzy inputs
*/
void RuleEvaluation()
{
double Strength;
int i;
rule_type *Rule;
rule_element_type *IfPtr;
rule_element_type *ThenPtr;
io_type *So;
mf_type *Mf;
/*
***** Begin RuleEvaluation
*/
for(So = SystemOutput; So != NULL; So = So->Next)
for(Mf = So->MembershipFunction; Mf != NULL; Mf = Mf->Next)
Mf->Value = 0.0;
// for all outputs, clear "Value" fields
i = 0;
for(Rule = RuleBase; Rule != NULL; Rule = Rule->Next) // For each rule Rule,
{
i++;
Strength = UPPER_LIMIT;
/*
***** process IF side of the rule (MIN or PROD operations)
*/
for(IfPtr = Rule->IfSide; IfPtr != NULL; IfPtr = IfPtr->Next)
if(DefuzzyMaxMin == YES)
Strength = MIN( Strength , *(IfPtr->Value) );
else
Strength = Strength * *(IfPtr->Value);
/*
***** process THEN side of the rule (MAX operations)
*/
for(ThenPtr = Rule->ThenSide; ThenPtr != NULL; ThenPtr = ThenPtr->Next)
*(ThenPtr->Value) = MAX( Strength , *(ThenPtr->Value) );
}
/*
***** End RuleEvaluation
*/
}
Defining fuzzy inference functions
/*
***** Function used to transform fuzzy sets into crisp output values
*/
void Defuzzification()
{
io_type *So;
mf_type *Mf;
double SumProducts, SumAreas, Area, Centroid, Result;
/*
***** Begin Defuzzification
*/
for(So = SystemOutput; So != NULL; So = So->Next) // for each output,
{
SumProducts = 0.0;
// initialize variables
SumAreas = 0.0;
for(Mf = So->MembershipFunction; Mf != NULL; Mf = Mf->Next) // calculate fuzzy
output
{
Area = TrapezoidArea(Mf);
Centroid = CentroidPoint(Mf);
SumProducts += Area * Centroid;
SumAreas += Area;
}
if(fabs(SumAreas - 0.0) > EPS)
Result = SumProducts / SumAreas;
// calculate centroid area
else
Result = 0.0;
So->Value = Result;
// store on Value field
}
/*
***** End Defuzzification
*/
}
Defining fuzzy execution functions
/**********************************************************************************
**************************** Fuzzy Execution Functions ****************************
**********************************************************************************/
/*
***** Execution of the Fuzzy Controller
*/
void FuzzyController()
{
Fuzzification();
RuleEvaluation();
Defuzzification();
}
/*
***** Example of a Control Loop Routine
*/
void ControlLoop()
{
SystemInitialize();
while(Run == YES)
{
GetSystemInputs();
FuzzyController();
PutSystemOutputs();
}
SystemShutDown();
}
MATLAB, SIMULINK AND FIS
Constructing Fuzzy Systems
with Matlab/Simulink





FIS Editor permit building up rules and constructing
membership functions that will be part of a fuzzy inference
system.
There is a diagram that shows the names of each input variable
on the left, and those of each output variable on the right.
Below the diagram is the name of the system and the type of
inference used. The are two inference machines (1) Mamdanitype inference (rule based) and (2) Sugeno-type inference
(parametric based)
Pop-up menus allow you to modify the various pieces of the
inference process.
When you save your fuzzy structure, a file with extension “.fis”
is created which can be used inside your Simulink simulation.
You can start from scratch by typing at the Matlab prompt
>> fuzzy


A generic FIS Editor opens, with one input, labeled input1, and
one output, labeled output1.
4
3
5
6
2
7
1
8
1.
2.
3.
4.
5.
6.
7.
8.
This edit field is used to name and edit the names of the input
and output variable.
Double-click on the icon for the output variable to open the
Membership Function Editor.
Double-click on the system diagram to open the Rule Editor.
Double-click on an input variable icon to open the
Membership Function Editor.
These menu items allow you to save, open, or edit a fuzzy
system using any of the five basic GU I tools.
The name of the system is displayed here. It can be changed
using the Save… as menu option
These pop-up menus are used to adjust the fuzzy inference
functions, such as the defuzzification method.
This status line describes the most recent operation.
Example of a Project

Using the previous
project
constructed
inside Matlab, we will
determine
fuel
consumption with FIS.

The left block set-ups
the m.f. for velocity, the
right block the m.f. for
consumption,
the
middle block defines
the rules.

You can click on the
inflection points defined
in the membership
functions to change the
shape.

Typing Edit => Add you
can change the # of m.f.
or have customized
ones.

On the right you can see
the m.f. for input and
output of our system.




Double-click on the
Rule Editor.
You can select the rules
aggregation, the weight,
negation of rule
After you finish setting
up the system, save as a
*.fis
This example has been
saved as VelCons.fis




Inside Simulink you can
integrate your block to fit
your dynamical simulation.
Here, just for example, time
(clock) is being used to
generate the evaluation of
velocity (just to illustrate)
The Scope generates the
Consumption in terms of
Velocity
The button “Variable
Initialization” runs the
script file VelC_ini to load
the fuzzy structure into
memory.
The rules can be changed by using FIS
and saving a new *.fis structure to run in
Simulink
If Velocity is Low Then Consumption is High
If Vel. is Medium Then Consumption is Medium
If Velocity is High Then Consumption is High
If Velocity is Low Then Consumption is High
If Vel. is Medium Then Consumption is Medium
If Velocity is High Then Consumption is Medium
Activities
Play with the current system to understand how to
operate it
 Incorporate a second input, Power
 Rebuild your rule base, including rules containing the
new input variable.
 Observe the new optimum point after the inference
execution of your new fuzzy system.

INTERACTIVE TANKS SYSTEM




An interactive tank system is very instructive to model.
It has storage capacity limits, coupling and clamping effects.
Some non-linearities are found in the real implementation.
The dynamics are given by the interaction of Tank 02 and
Tank 03. Tank 01 is acting as a sump in the system,
providing continuous flow for the experimental set-up.
Tank 02
IN
23
Tank 01
Tank 03
OUT

Tank-02 and Tank-03 can be equated based on balance of
mass:
dm2
 win  w23
dt
dm3
 w23  wout
dt
where m2 e m3 are the mass of liquid in both tanks, and
IN, 23 and OUT are the mass flow. Considering both
tanks with same dimensions area (A) and height (h),
constant liquid density () :
m =Ah
=q

One obtains the flow rate determining the dynamics of the
height. The flow from Tank 02 to Tank 03 is determined by
Bernoulli law, relating the pressure difference and load loss
dh2 qin q 23


dt
A
A
dh3 q 23 q out


dt
A
A
q23  Cv . P2  P3

Patm is the local atmospheric pressure, g is the gravity
acceleration. Thus, Pn = Patm +  . g . hn , and :
q 23 
h2  h3
Rh
where
Rh 
1
Cv .  . g



Pneumatic valves present exponential characteristic relating
their command to the flow.
One can neglect the valve dynamic response
But still needs to consider the resistive effects of a valve
normally open and a valve normally closed
q f  R ( m1)
qin  QinMAX . R1
( u1 1)
qout  QoutMAX . R2
( u2 1)
where q in and q out represent the input and output flow
Qin MAX and Qout MAX are the clamped values for input and output
R1 and R2 are the range of control (FCV-01 and FCV-02)
u1 and u2 are the input command for the valves.
Dynamic System
.

x1  x 2
QinMAX
( u1 1)
. R1

 x1 

A
A. Rh
 .
 x   QoutMAX . R ( u2 1)  x1  x 2
2
 2
A
A. Rh
x1 and x2 are the tank levels
y2 is the output flow controlled by valve FCV-02
 y1  x2

( u2 1)
 y2  QoutMAX . R2
A PI based closed system can control both loops
_
Reference +
Level
PI
Level Control
Output
Level
Tank System
Flow
Level
+
_
PI
Flow Control
Output
Flow
A supervisory control system can manage the
setting-points in order to decouple the tanks
interaction
Reference
_
Level
+
Fuzzy
Supervisory
Control
PI
Level Control
Output
Level
Tank System
+
Flow
Level
_
PI
Flow Control
Output
Flow

Following is shown a Simulink diagram containing a model of the
interactive tanks system previously described (non-linearities in
orange).
0.433
Tanks Area
1
Product1
450
R_hidr
s
Calculate H2
1.32e-3
sqrt
Qi_Max
... X R_v1 ^ u1 / A
13
ln
R_valve1
ln(R_v1)
u1
1
sqrt (H2-H3)
eu
... X u1
R_v1 ^ (u1-1)
1
Product2
1
0.85e-3
Const
Qo_Max
s
Calculate H3
1
Vol_Tq3
... X R_v2 ^ (u2-1)
u2
2
20
R_valve2
e
ln
ln(R_v2)
u
1
0.1s+1
... X -u2
R_v1 ^ -u2
Valve_2 Delay
2
Vaz_Tq3

Now, the tanks system is regulated by two PID controllers and
supervised by a Fuzzy System.
Variable Initialization
Edit Fuzzy System
PID
Vol_Tq3
Set-Point
Clock
500
Const
PID Controller Limiter PID1
Vol_Tq3
Vaz_Tq3
Advanced
Set-Point
Vol_Tq3 X Set-Point
Interactive Tanks
System
u1
Vol_Tq3
u2
Vaz_Tq3
Fuzzy Logic
Supervisor
PID
Vaz_Tq3
Set-Point

PID Controller Limiter PID2
Vaz_Tq3
The goal of the supervisor system here is to modify the setpoints of the regulated variables and minimize the existing
interaction between them.
Vaz_Tq3

The supervisor Fuzzy System uses 6 rules to optimize the
operation of the coupled system. These rules correspond to the
supervision surface shown on the bottom.

The time response for the system just described is shown below.



The first graphic shows
TQ3 level control and the
second one below shows
TQ3 outflow control.
Set-points are shown in
pink, actual variables in
green and control signals
in light blue.
The action of the fuzzy
supervisor is visible at the
instants of changing in the
set-point of output flow
from the tank 3, at times
3000s, 7000s and 11000s.
FUZZY PI CONTROLLER
FOR A DC-MACHINE
Design of a Fuzzy-PI Controller





You are given a dc-machine model in Simulink like the
block diagram below
The system is running with the provided PI. However, one
can see that the PI generates the armature voltage.
We know that there is an inner current loop control that
should be implemented to improve the transient response !
However, most of people try to avoid that. Thus, using a PI
controller like this actually does not optimize the transient
response…
Let us try to design a fuzzy controller to replace that PI ?
Simulink based control of a dc-machine
Implement the following fuzzy controller to replace your PI
Use the following rules for your fuzzy controller
E
NVL
Er
NVL
NL
NM
NS
ZE
PS
NL
PM
NM
PL
NS
PVL
ZE
NL
NL
NM
NS
ZE
PS
NM
NS
NL
NM
NS
ZE
PS
PM
NL
NM
NS
ZE
PS
PM
PL
ZE
NVL
NL
NM
NS
ZE
PS
PM
PL
PVL
PS
NL
NM
NS
ZE
PS
PM
PL
PM
NM
NS
ZE
PS
PM
PL
PL
NS
ZE
PS
PM
PL
PVL
ZE
PS
PM
PL