Fuzzy Logic Controllers - Colorado School of...
Transcript of Fuzzy Logic Controllers - Colorado School of...
Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff
Fuzzy Logic Controllers
Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff
Control System
• Models the physical system
• Uses sensor feedback to generate a new system control signal
• Controller requires accurate mathematical model of system – Requires accurate, often
complex system of equations
– These equations are not always readily available
2
http://wwweng.uwyo.edu/electrical/faculty/barrett/68hc12/fuzzy_logic.pdf
Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff
Motivation for Fuzzy Logic
• “People do not require precise, numerical information input, and yet they are capable of highly adaptive control.”1
• Fuzzy logic deals with reasoning that is approximate rather than fixed and exact – Similar to neural networks
– Proponents claim fuzzy systems are much closer to human control processes
– Skeptics deride fuzzy logic for lack of mathematical rigor
• However, fuzzy logic controllers are in heavy use
• For example, HCS12 has dedicated machine instructions for fuzzy logic! It must have enough demand to warrant this kind of support.
3
1Professor Lotfi Zadeh, UC Berkeley, 1965
Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff
Fuzzy Logic
• Fuzzy logic is a form of many-valued logic or probabilistic logic
• Compared to traditional binary variables (true or false) fuzzy logic variables have a truth value that ranges in degree between 0 and 1
• Fuzzy logic has the concept of partial truth, where the truth value may range between completely true and completely false
4
“Strong Fever”
40.1°C
42°C
41.4°C
39.3°C
38.7°C
37.2°C
38°C
40.1°C
42°C
41.4°C
39.3°C
38.7°C
37.2°C
38°C
“Strong Fever”
Fuzzy Set Theory Conventional (Boolean) Set Theory
http://www.cse.lehigh.edu/~munoz/CSE335/classes/FuzzyLogic.ppt
Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff
Fuzzy Logic Controller Overview
• Rely on if-then rules rather than mathematical equations
• A simple form of an expert system – An expert system has rules to
map input signals to output system responses
• Uses confidence factors to assign a system output
5
From: http://wwweng.uwyo.edu/electrical/faculty/barrett/68hc12/fuzzy_logic.pdf
Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff
Advantages and Disadvantages
• Advantages
– Useful where system equations are difficult to develop
– Solutions can be developed quickly and intuitively • Mimics human control logic
• Uses imprecise language
– Other advantages • Inherently robust
• Fails safely
• Modified and tweaked easily
• Disadvantages
– Operator's experience required
– System complexity
6
Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff
Example Applications
• Sendai subway system train controller
• HVAC controllers
• Industrial automation for process control
• Videos
– Flexible robot control • http://www.youtube.com/watch?v=R7dhudG5ri4
– Anti-sway control of a crane • http://www.youtube.com/watch?v=fB1EfDZPoFY
– Mobile robot control • http://www.youtube.com/watch?v=OB6UMe9pyfI
7
Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff
Possible Instances to Use Fuzzy Logic
• When it is difficult to obtain a mathematical model of the process.
• If fuzzy logic has been previously applied to a similar application with success.
• If you have some knowledge about the process variables and their interdependencies.
• If you can easily do experiments to find good values for the fuzzy logic parameters.
8
Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff
Recall Boolean logic
• Any Boolean logic function can be implemented as an AND-OR network
• This network implements three functions of two inputs
• A control output signal is asserted if its “rule” (i.e., AND-OR expression) is satisfied
9
From: “Designing with Microcontrollers”, Tom Almy, http://www.almy.us
Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff
Fuzzy logic
• In fuzzy logic we don’t use True and False values, but allow a continuum of values from totally false (0) to totally true (1)
• The inputs and outputs can have multiple levels
• A process called fuzzification converts the input levels into fuzzy levels
• A “membership function” determines the fuzzy levels
10
Example: input voltages less than 1 are totally false and those greater than 4 are totally true; other values have intermediate truth
From: “Designing with Microcontrollers”, Tom Almy, http://www.almy.us
Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff
Fuzzy logic
• In Boolean logic – The output of an AND gate is false if
any input is false (i.e., the output is the minimum of the inputs)
– The output of an OR gate is the maximum of its inputs
• In fuzzy logic – the AND/OR logic network becomes
a minimum function followed by a maximum function
11
Instead of the AND logic operation, take the
minimum of the inputs’ fuzzy levels
Instead of the OR logic operation, take the maximum of the inputs from the first level
The result is that all of the output signals may be asserted to some extent!
Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff
Fuzzy logic control
• Steps – Map input values to fuzzy
input membership values (“fuzzification”)
– Evaluate rules to generate output membership values
– Convert output membership values to a digital value (“defuzzification”)
12
http://wwweng.uwyo.edu/electrical/faculty/barrett/68hc12/fuzzy_logic.pdf
Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff
Fuzzification
• Given an input datum, the controller must first assign a degree of membership, a value ranging from zero to one, to each fuzzy membership function
13
http://wwweng.uwyo.edu/electrical/faculty/barrett/68hc12/fuzzy_logic.pdf
Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff
Rule evaluation
• Apply expert rules to input membership functions – Rules should cover all possible combinations of the input variables
– Rules map combinations of inputs to output fuzzy membership functions
• Rule format: if (antecedent 1) AND (antecedent 2),
then (desired response)
• The minimum degree of membership among the input functions is assigned to the output membership function (this is the confidence factor)
14
Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff
Rule evaluation (continued)
• If there are multiple rules that assign an output function, take the maximum of the confidence factors as the degree of the membership for that output fuzzy function.
• Namely, use the most dominant rule to assign the output membership function.
• We still need to convert the output membership function values to a form suitable to control system (“defuzzification”).
15
Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff
Defuzzification
• Each output membership function has a nominal output value Si
• To combine them, average the output values of all the functions, but weighted by their fuzzy membership values
16
http://wwweng.uwyo.edu/electrical/faculty/barrett/68hc12/fuzzy_logic.pdf
Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff
Example - Fuzzy logic based robot controller
• Want robot to avoid obstacles
• Robot has two IR distance sensors (values 0..$FF)
• Output is the motor command, from 0 (hard left) to $FF (hard right)
– $80 means go straight
17
http://wwweng.uwyo.edu/electrical/faculty/barrett/68hc12/fuzzy_logic.pdf
Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff
Development steps
• Determine linguistic variables describing all inputs to the controller left sensor input: very weak, weak, medium, strong, very strong
right sensor input: very weak, weak, medium, strong, very strong
• Each linguistic variable must be defined with a set of input membership functions
18
For the HC12 a trapezoidal function must be used
http://wwweng.uwyo.edu/electrical/faculty/barrett/68hc12/fuzzy_logic.pdf
Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff
Development steps (continued)
• Input membership function
– Specify using: farthest left point, farthest right point, left slope, and right slope
– Full membership corresponds to $FF
19
very weak $00, $50, $00, $10 weak $40, $70, $10, $10 medium $60, $A0, $10, $10 strong $90, $C0, $10, $10 very strong $B0, $FF, $10, $00
0
50
100
150
200
250
300
0 50 100 150 200 250 300
very weak
weak
medium
strong
very strong
Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff
Development steps (continued)
• Determine fuzzy rules that map input to output membership functions
• Rules are intuitive based on considering all possible input sensor value scenarios
• Rule format: IF (antecedent 1) AND (antecedent 2),
THEN (desired output)
20
Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff
Development steps (continued)
• Rules in table form
21
http://wwweng.uwyo.edu/electrical/faculty/barrett/68hc12/fuzzy_logic.pdf
Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff
Development steps (continued)
• Output membership function design process
– Use singleton functions for the fuzzy output membership functions (“singleton”: each function is defined by a single value) • Medium Left ($40)
• Small Left ($60)
• Zero ($80)
• Small Right ($A0)
• Medium Right ($C0)
22
(Assumption: $80 keeps robot on straight path)
Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff
Development steps (continued)
• Define output values of all output fuzzy membership functions
– The controller will take a weighted average of the values, weighted by their fuzzy membership values
Output = Σ (Si Fi) / Σ (Fi) for i = i ..n
where: Si is the Singleton value
Fi is the output membership value for fuzzy logic output function i
23
Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff 24
// Define the tables for the input value membership functions.
// Specify using: farthest left pt, farthest right pt, left slope, right slope
int veryweakTable[] = { 0x00, 0x50, 0x00, 0x10 };
int weakTable[] = { 0x40, 0x70, 0x10, 0x10 };
int mediumTable[] = { 0x60, 0xa0, 0x10, 0x10 };
int strongTable[] = { 0x90, 0xc0, 0x10, 0x10 };
int verystrongTable[] = { 0xb0, 0xff, 0x10, 0x00 };
// Define the values for the output members.
#define mediumLeft 0x40 // hard left turn
#define smallLeft 0x60
#define zero 0x80 // go straight
#define smallRight 0xa0
#define mediumRight 0xc0 // hard right trun
// Utility functions for min, max.
int min(int a, int b) {
return (a<b ? a : b);
}
int max(int a, int b) {
return (a>b ? a : b);
}
int max(int a, int b, int c, int d) {
return max( max(a,b), max(c,d) ); // 4-input max function
}
int max(int a, int b, int c, int d, int e) {
return max( max(a,b,c,d), e ); // 5-input max function
}
int max(int a, int b, int c, int d, int e, int f) {
return max( max(a,b,c,d), max(e,f) ); // 6-input max function
}
int fuzzification(int x, int table[]) {
int x0 = table[0]; // Leftmost point
int x1 = table[1]; // Rightmost point
int s0 = table[2]; // Left slope
int s1 = table[3]; // Right slop
int y0 = (x0==0 ? 255 : 0); // Value at leftmost point
int y1 = (x1==255 ? 255 : 0); // Value at rightmost point
if (x < x0 || x > x1) return 0;
// Estimate value at x.
int y = min(y0 + s0*(x-x0), y1 - s1*(x-x1));
y = min(y, 255); // Clip to 255
return (int) y;
}
Mobile Robot Example Code
– C++
This is simple program that directly implements the robot controller. A better version would be able to handle an arbitrary number of variables and rules. See http://www.codeproject.com/Articles/316668/Cplusplus-Fuzzy-Logic-API-plus-Simple-DSL, for example
Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff 25
void main(void) {
EnableInterrupts;
for(;;) {
// Some code needed to read left and right sensor values.
int leftSensor;
int rightSensor;
// Perform fuzzification to determine input membership values.
int lvw = fuzzification(leftSensor, veryweakTable);
int lw = fuzzification(leftSensor, weakTable);
int lm = fuzzification(leftSensor, mediumTable);
int ls = fuzzification(leftSensor, strongTable);
int lvs = fuzzification(leftSensor, verystrongTable);
int rvw = fuzzification(rightSensor, veryweakTable);
int rw = fuzzification(rightSensor, weakTable);
int rm = fuzzification(rightSensor, mediumTable);
int rs = fuzzification(rightSensor, strongTable);
int rvs = fuzzification(rightSensor, verystrongTable);
// Perform rule evaluation to determine output membership values.
// Each rule is of the form IF (a AND b) OR (c AND d) THEN e
int goMediumLeft = max( min(lw,rvs), min(lw,rs), min(lvw,rvs), min(lvw,rs) );
int goSmallLeft = max( min(ls,rvs), min(lm,rvs), min(lm,rs), min(lw,rm), min(lvw,rm), min(lvw,rw));
int goZero = max( min(lvs,rvs), min(ls,rs), min(lm,rm), min(lw,rw), min(lvw,rvw) );
int goSmallRight = max( min(rs,lvs), min(rm,lvs), min(rm,ls), min(rw,lm), min(rvw,lm), min(rvw,lw));
int goMediumRight = max( min(rw,lvs), min(rw,ls), min(rvw,lvs), min(rvw,ls) );
// Perform defuzzification to determine a precise output value.
// The output is just the weighted sum of the members.
int outValue =
(goMediumLeft*mediumLeft + goSmallLeft*smallLeft + goZero*zero +
goSmallRight*smallRight + goMediumRight*mediumRight)/
(goMediumLeft + goSmallLeft + goZero + goSmallRight + goMediumRight);
// Some code needed to send the control output to the actuators.
} /* loop forever */
}
Mobile Robot Example Code
– C++
Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff
HCS12 Fuzzy Logic Instructions
• Four HCS12 instructions:
– MEM: grade of membership
– REV: MIN-MAX rule evaluation
– REVW: MIN-MAX rule evaluation with optional rule weighting
– WAV: performs weighted average calculations
• Supplementary instructions:
– ETBL, TBL, EDIV, EDIVS, EMACS, EMAX, EMAXM, EMIND, EMINM, EMUL, EMULS
26
See Section 9 in the CPU12 reference manual
Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff
Mobile Robot Example Code - Assembly
27
Line 1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Line 2 ;Fuzzy Logic Controller for A Mobile Robot
Line 3 ;Description: This program takes in two Infrared
Line 4 ;sensor values and computes a di
Line 5 ;rection control signal to avoid
Line 6 ;wall collisions. The two sensor
Line 7 ;values are read from memory
Line 8 ;locations $6000 and $6001 and
Line 9 ;the control output value is
Line 10 ;written to memory location $6002
Line 11 ;
Line 12 ; Authors: Daniel Pack and Steve Barrett
Line 13 ; Date: 8-21-2000
Line 14 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Line 15 ; Data Section
Line 16 O_R_VS EQU $00 ;Offset values input and output mem fns
Line 17 O_R_ST EQU $01 ; Right Sensor Strong
Line 18 O_R_ME EQU $02 ; Right Sensor Medium
Line 19 O_R_WE EQU $03 ; Right Sensor Weak
Line 20 O_R_VW EQU $04 ; Right Sensor Very Weak
Line 21 O_L_VS EQU $05 ; Left Sensor Very Strong
Line 22 O_L_ST EQU $06 ; Left Sensor Strong
Line 23 O_L_ME EQU $07 ; Left Sensor Medium
Line 24 O_L_WE EQU $08 ; Left Sensor Weak
Line 25 O_L_VW EQU $09 ; Left Sensor Very Weak
Line 26 O_ML EQU $0A ; Medium Left
Line 27 O_SL EQU $0B ; Small Left
Line 28 O_ZR EQU $0C ; Zero
Line 29 O_SR EQU $0D ; Small Right
Line 30 O_MR EQU $0E ; Medium Right
Line 31 MARKER EQU $FE ; rule separator
Line 32 ENDR EQU $FF ; end of rule marker
Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff 28
Line 33 ORG $6000
Line 34 RSENSOR RMB $01 ; Allocating
Line 35 LSENSOR RMB $01 ; memory locations
Line 36 CONTROLS RMB $01 ; for input/output variables
Line 37 ; Fuzzy Input Membership Function Definitions for Right Sensor
Line 38 R_Very_Strong FCB $B0, $FF, $10, $00
Line 39 R_Strong FCB $90, $C0, $10, $10
Line 40 R_Medium FCB $60, $A0, $10, $10
Line 41 R_Weak FCB $40, $70, $10, $10
Line 42 R_Very_Weak FCB $00, $50, $00, $10
Line 43 ; Fuzzy Input Membership Function Definitions for Left Sensor
Line 44 L_Very_Strong FCB $B0,$FF,$10,$00
Line 45 L_Strong FCB $90,$C0,$10,$10
Line 46 L_Medium FCB $60,$A0,$10,$10
Line 47 L_Weak FCB $40,$70,$10,$10
Line 48 L_Very_Weak FCB $00,$50,$00,$10
Line 49 ;Fuzzy Output Membership Function Definitions
Line 50 Medium_Left FCB $40
Line 51 Small_Left FCB $60
Line 52 Zero FCB $80
Line 53 Small_Right FCB $A0
Line 54 Medium_Right FCB $C0
Mobile Robot Example Code - Assembly
Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff 29
Line 55 ; Locations for fuzzy membership values
Line 56 R_VS RMB $01
Line 57 R_ST RMB $01
Line 58 R_ME RMB $01
Line 59 R_WE RMB $01
Line 60 R_VW RMB $01
Line 61 L_VS RMB $01
Line 62 L_ST RMB $01
Line 63 L_ME RMB $01
Line 64 L_WE RMB $01
Line 65 L_VW RMB $01
Line 66 ; Output Fuzzy Logic Membership Values - initialize to zero
Line 67 ML FCB $00
Line 68 SL FCB $00
Line 69 ZR FCB $00
Line 70 SR FCB $00
Line 71 MR FCB $00
Mobile Robot Example Code - Assembly
Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff 30
Line 72 ; Rule Definitions
Line 73 Rule_Start FCB O_R_VS,O_L_VS,MARKER,O_ZR,MARKER
Line 74 FCB O_R_VS,O_L_ST,MARKER,O_SL,MARKER
Line 75 FCB O_R_VS,O_L_MD,MARKER,O_SL,MARKER
Line 76 FCB O_R_VS,O_L_WE,MARKER,O_ML,MARKER
Line 77 FCB O_R_VS,O_L_VW,MARKER,O_ML,MARKER
Line 78 FCB O_R_ST,O_L_VS,MARKER,O_SR,MARKER
Line 79 FCB O_R_ST,O_L_ST,MARKER,O_ZR,MARKER
Line 80 FCB O_R_ST,O_L_MD,MARKER,O_SL,MARKER
Line 81 FCB O_R_ST,O_L_WE,MARKER,O_ML,MARKER
Line 82 FCB O_R_ST,O_L_VW,MARKER,O_ML,MARKER
Line 83 FCB O_R_MD,O_L_VS,MARKER,O_SR,MARKER
Line 84 FCB O_R_MD,O_L_ST,MARKER,O_SR,MARKER
Line 85 FCB O_R_MD,O_L_MD,MARKER,O_ZR,MARKER
Line 86 FCB O_R_MD,O_L_WE,MARKER,O_SL,MARKER
Line 87 FCB O_R_MD,O_L_VW,MARKER,O_SL,MARKER
Line 88 FCB O_R_WE,O_L_VS,MARKER,O_MR,MARKER
Line 89 FCB O_R_WE,O_L_ST,MARKER,O_MR,MARKER
Line 90 FCB O_R_WE,O_L_MD,MARKER,O_SR,MARKER
Line 91 FCB O_R_WE,O_L_WE,MARKER,O_ZR,MARKER
Line 92 FCB O_R_WE,O_L_VW,MARKER,O_SL,MARKER
Line 93 FCB O_R_VW,O_L_VS,MARKER,O_MR,MARKER
Line 94 FCB O_R_VW,O_L_ST,MARKER,O_MR,MARKER
Line 95 FCB O_R_VW,O_L_MD,MARKER,O_SR,MARKER
Line 96 FCB O_R_VW,O_L_WE,MARKER,O_SR,MARKER
Line 97 FCB O_R_VW,O_L_WE,MARKER,O_ZR,ENDR
• In HCS12, rules are stored as antecedent one, antecedent two, $FE, desired output, $FE
Mobile Robot Example Code -
Assembly
Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff 31
Line 98 ; Main Program
Line 99 ORG $4000
Line 100 ; Fuzzification----------------------------------------------------
; Fuzzification Procedure
; MEM function determines grade of membership.
;
; Registers must be set up as follows:
; A: must hold the current crisp value of system input variable
; X: must point to 4-byte data structure describing the trapezoidal
; membership function for a label of the system input
; Y: must point to the fuzzy input (RAM location) where the resulting
; grade of membership is to be stored
;------------------------------------------------------------------
; Perform fuzzification for right sensor
Line 101 LDX #R_Very_Strong ; Start of Input Mem func
Line 102 LDY #R_VS ; Start of Fuzzy Mem values
Line 103 LDAA RSENSOR ; Right Sensor Value
Line 104 LDAB #5 ; Number of iterations
Line 105 Loopr MEM ; Assign mem value
Line 106 DBNE B,Loopr ; Do all five iterations
; Perform fuzzification for left sensor
Line 107 LDAA LSENSOR ; Left Sensor Value
Line 108 LDAB #5 ; Number of iterations
Line 109 Loopl MEM ; Assign mem value
Line 110 DBNE B,Loopl ; Do all five iterations
Mobile Robot Example
Code - Assembly
All the preceding code is just setup and initialization. The bulk of the actual work is done here, by the “MEM” instruction.
Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff 32
; Fuzzy Logic Rule Evaluation-------------------------------------
; REV function performs an unweighted evaluation of a list of rules,
; using fuzzy input values to produce fuzzy outputs.
;
;Before executing REV, perform the following set up operations.
; X: must point to the first 8-bit element in the rule list.
; Y: must point to the base address for fuzzy inputs and outputs
; A: must contain the value of $FF, and the CCR V bit must = 0
; (LDAA #$FF places the correct value in A and clears V)
; Clear fuzzy outputs to zeros
;-----------------------------------------------------------------
Line 111 LDY #R_VS ; Process rules
Line 112 LDX #Rule_Start ; Point X to the start addr
Line 113 LDAA #$FF ; Initialize min and V bit
Line 114 REV ; Evaluate rules
Line 115 ; Defuzzification Process--------------------------------
; WAV function performs weighted average calculations on
; values stored in memory.
; X: used to reference one source operand list
; Y: used to reference second source operand list
; B: used a s counter to control number of elements to
; be included in the weighted average
;-------------------------------------------------------------
Line 116 LDX #Medium_Left ; Start of output mem func
Line 117 LDY #ML ; Start of mem values
Line 118 LDAB #$05 ; Five elements sum
Line 119 WAV ; Computing a crisp value
Line 120 EDIV ; Extended divide - 32 bit/16 bit
Line 121 TFR Y,D ; Store answer to D
Line 122 STAB CONTROLS ; Save the answer
Line 123 END
Mobile Robot Example Code -
Assembly
The REV and the WAV instructions do the bulk of the work here too.
Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff
Summary / Questions
• Fuzzy logic is a superset of conventional (Boolean) logic that has been extended to handle the concept of partial truth; i.e., truth values between "completely true" and "completely false".
• It has been used for control applications and recognition systems.
• What are the advantages and disadvantages of fuzzy logic?
33