39
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.

Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

Embed Size (px)

Citation preview

Page 1: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

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.

Page 2: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

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.

Page 3: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

IMPLENTING FUZZY CONTROL IN C-LANGUAGE

Page 4: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

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.

Page 5: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

Defining support structurestypedef 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 fashiontypedef 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 fashiontypedef struct { double *Value; struct rule_element_type *Next; } rule_element_type; // Defines a fuzzy evaluation, present at IF and THEN // parts of a fuzzy ruletypedef 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

Page 6: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

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 */ }

Page 7: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

/*

***** 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…

Page 8: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

/****** 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 support functions…

Page 9: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

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

*/

}

Page 10: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

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 */ }

Page 11: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

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 */ }

Page 12: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

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(); }

Page 13: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

MATLAB, SIMULINK AND FIS

Page 14: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

Constructing Fuzzy Systemswith 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) Mamdani-type 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.

Page 15: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

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.

3

1

4

2 6

5

8

7

Page 16: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

1. This edit field is used to name and edit the names of the input and output variable.

2. Double-click on the icon for the output variable to open the Membership Function Editor.

3. Double-click on the system diagram to open the Rule Editor.

4. Double-click on an input variable icon to open the Membership Function Editor.

5. These menu items allow you to save, open, or edit a fuzzy system using any of the five basic GU I tools.

6. The name of the system is displayed here. It can be changed using the Save… as menu option

7. These pop-up menus are used to adjust the fuzzy inference functions, such as the defuzzification method.

8. This status line describes the most recent operation.

Page 17: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

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.

Page 18: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

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.

Page 19: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

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

Page 20: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

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.

Page 21: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

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

The rules can be changed by using FIS

and saving a new *.fis structure to run in

Simulink

Page 22: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

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.

Page 23: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

INTERACTIVE TANKS SYSTEM

Page 24: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

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.

Ta n k 0 2

Ta n k 0 1

Ta n k 0 3

IN

23

O U T

Page 25: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

Tank-02 and Tank-03 can be equated based on balance of mass:

dm

dtw win

223

dm

dtw wout

323

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 = A h

= q

Page 26: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

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

dh

dt

q

A

q

Ain2 23

dh

dt

q

A

q

Aout3 23

q C P Pv23 2 3 .

qh h

Rh23

2 3

RC g

h

v

1

. .where

Patm is the local atmospheric pressure, g is the gravity

acceleration. Thus, Pn = Patm + . g . hn , and :

Page 27: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

q Rfm ( )1

q Q Rin inMAXu . ( )

111

q Q Rout outMAXu . ( )

212

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

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.

Page 28: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

Dynamic System

xQ

AR

x x

A R

xQ

AR

x x

A R

inMAX u

h

outMAX u

h

1 11 1 2

2 21 1 2

1

2

.( )

.( )

..

..

y x

y Q RoutMAXu

1 2

2 212

. ( )

x1 and x2 are the tank levels

y2 is the output flow controlled by valve FCV-02

Page 29: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

A PI based closed system can control both loops

P I

P I

F lo w C o n tro l

Ta n k S y s te m

L e v e l C o n tro l

+

_

+_

O u tp u tF lo w

O u tp u tL e v e l

F lo wL e v e l

R e fe re n c eL e v e l

Page 30: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

A supervisory control system can manage the setting-points in order to decouple the tanks interaction

P I

P I

F lo w C o n tro l

Ta n k S y s te m

L e v e l C o n tro l

+

_

+_

O u tp u tF lo w

O u tp u tL e v e l

F lo wL e v e l

F u z zyS u p e rv iso ryC o n tro l

R e fe re n c eL e v e l

Page 31: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

Following is shown a Simulink diagram containing a model of the interactive tanks system previously described (non-linearities in orange).

2

Vaz_Tq3

1

Vol_Tq3

sqrt

sqrt (H2-H3)

ln

ln(R_v2)

ln

ln(R_v1)

1

0.1s+1

Valve_2 Delay

0.433

Tanks Area

20

R_valve2

13

R_valve1

eu

R_v1 ^ -u2

eu

R_v1 ^ (u1-1)

450

R_hidr

0.85e-3

Qo_Max

1.32e-3

Qi_Max

Product2

Product1

1

Const

s

1

Calculate H3

s

1

Calculate H2

... X u1

... X R_v2 ^ (u2-1)

... X R_v1 ^ u1 / A

... X -u2

2

u2

1

u1

Page 32: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

Now, the tanks system is regulated by two PID controllers and supervised by a Fuzzy System.

The goal of the supervisor system here is to modify the set-points of the regulated variables and minimize the existing interaction between them.

Vol_Tq3 X Set-Point

Vol_Tq3Set-Point

Vaz_Tq3 Advanced Set-Point

Vaz_Tq3Set-Point Vaz_Tq3

PID

PID ControllerVol_Tq3

PID

PID ControllerVaz_Tq3

Limiter PID2

Limiter PID1

u1

u2

Vol_Tq3

Vaz_Tq3

Interactive Tanks System

Fuzzy Logic Supervisor

Variable Initialization Edit Fuzzy System

500

Const

Clock

Page 33: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

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.

Page 34: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

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.

Page 35: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

FUZZY PI CONTROLLERFOR A DC-MACHINE

Page 36: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

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 ?

Page 37: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

Simulink based control of a dc-machine

Page 38: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

Implement the following fuzzy controller to replace your PI

Page 39: Implementation of Fuzzy Controllers u The first hardware approaches that implemented fuzzy logic inference engine were the digital circuits designed by

Use the following rules for your fuzzy controller

N V L

N L

N M

N S

Z E

P S

P M

P L

P V L

N V L N V L N L N M N S Z E N L N L N M N S Z E P S N M N L N M N S Z E P S P MN S N L N M N S Z E P S P M P L Z E N L N M N S Z E P S P M P L P S N L N M N S Z E P S P M P L P M N M N S Z E P S P M P L P L N S Z E P S P M P L

P V L Z E P S P M P L P V L

EEr