You are on page 1of 8

VHDL Implementation For a Fuzzy Logic Controller

Philip T. Vuong, Asad M. Madni and Jim B. Vuong

BEI Technologies, Inc.


13100 Telfair Avenue, Sylmar, California 91342 U. S. A.
Tel: (818) 364-7215 Fax: (818) 362-1836
E-Mail: pvuong@bei-tech.com bei1madni@aol.com jvuong@bei-tech.com

ABSTRACT
This paper describes the implementation for a basic fuzzy logic controller in Very High
speed integrated-circuit Hardware-Description Language (VHDL). It is not intended as
an introduction to fuzzy logic control methodology; instead, we try to demonstrate the
implementation of a fuzzy logic controller through the use of the VHDL code. Use of the
hardware description language (HDL) in the application is suitable for being
implemented into an Application Specific Integrated Circuit (ASIC) and Field
Programmable Gate Array (FPGA). The main advantages of using the HDL approach are
rapid prototyping, and allowing usage of powerful synthesis tools such as Xilinx ISE,
Synosys, Mentor Graphic, or Cadence to be targeted easily and efficiently.

KEYWORDS: VHDL, Fuzzy Logic Controller, Fuzzzification, Rule Evaluation,


Defuzzification, Application Specific Integrated Circuit (ASIC).

1. INTRODUCTION
The motivation behind the implementation of a fuzzy controller in VHDL was driven by the
need for an inexpensive hardware implementation of a generic fuzzy controller for use in
industrial and commercial applications. A very simple fuzzy controller is used to demonstrate
this implementation. In the controller, an external device’s information, such as that from a
sensor, etc., is converted into an output control signal to drive a device(s) such as motors,
actuators etc., via the process of fuzzification, rule evaluation and defuzzification [1,2,3]. These
processes are all based on a set of membership functions and the details of this process can be
found in numerous publications [4, 5, 6, 7].

2. FUZZIFICATION
There are numerous different types of membership functions. Among them, the two most
commonly used in practice are the triangular and trapezoidal membership functions (triangular
membership function being a special case of the trapezoidal function). For this application we use
a trapezodial function.
Each trapezoidal membership function is defined by two points and two slope values. The
entire membership function can be divided into three segments: 0, 1 and 2 as shown in Figure 1.
The Y axis shows the degree of membership (µ) as a value between 0 and 1. The X axis shows
the universe of discourse and is divided into three segments. The degree of membership depends
on the location of the input value with reference to these three segments. Figure 1 shows how
trapezoidal input membership functions are formed in the fuzzification process [8, 9, 10].
The calculation of the degree of membership ((µ) can be categorized into three different
segments: (a) in segment 0: µ = 0, (b) in segment 1: slope is upward from left to right, therefore:
µ = (Input value – point 1) * slope Y
1, where µ is limited to a maximum Point α Segment 1 Point 2
value of 1, (c) in segment 2: slope is
1 Segment 0 Segment 2
downward from left to right,
therefore: µ = 1 - (Input value – Slope 2
µ Slope 1
point 2) * slope 2, where µ is
limited to a minimum value of 0. Point β
Point 1
As an example, let’s use the
input value of 10 to calculate the
0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15X
degree of membership function.
Using an 8-bit resolution
Figure 1. Trapezoidal Type Membership Function
computation, µ = 1 equals to $FF or
255 in decimal (the “$” sign indicates hexadecimal number representation). The values of Point
1 and Point 2 are $04 and $09, respectively, and the two slopes can be calculated as follows:

Slope 1 = 1 / (6 – 4) = $FF / 2 = 255 / 2 = 127 = $7F (1)


and
Slope 2 = 1 / (12 – 9) = $FF / 3 = 255 / 3 = 85 = $55 (2)

Since the input value of 10 ($0A) is greater than Point 2 and lies in segment 2, therefore:

µ = $FF – (Input value – point 2) * slope 2 = $FF – ($0A - $09)*$55 = $AA (3)

In VHDL, each membership function is defined by four 8-bit values, two points and two
slope values using record type declaration as follow [11]:

type membership is (term, none);


type mfs is record
linguistic: membership;
point1: std_logic_vector(7 downto 0);
slope1: std_logic_vector(7 downto 0);
point2: std_logic_vector(7 downto 0);
slope2: std_logic_vector(7 downto 0); end record;
type membership_functions is array(natural range <>) mfs;
constant linguistic_name : membership_functions :=((linguistic => term, point1 => x"04",
slope1 => x"7F", point2 => x"09", slope2 => "55"), linguistic => none, point1 => x"FF",
slope1 => x"FF", point2 => x"FF", slope2 => x"FF"));

3. RULE EVALUATION

A rule usually takes the form of IF-THEN statement as follow:

IF x is A AND y is B THEN z is C (4)

“AND” is a fuzzy operator which is the minimum operation between the two antecedents. In
VHDL, the following “minimum” function is used to obtain the result of each rule evaluation
between two variables:

function minimum(a, b: std_logic_vector) return std_logic_vector is


variable min: std_logic_vector(7 downto 0) := (others => '0');begin
if a < b then min := a; else min := b; end if; return min; end minimum;

There is also an implied “OR” operation between successive rules when more than one rule is
involved with the same output. The linguistic rule, IF (x1 is A1 AND y1 is B1) OR (x2 is A2 AND
y2 is B2) THEN z is C , can be implemented by taking the maximum value of all the results with
the same consequent block. In VHDL code, a function of taking the maximum of two input
variables to determine the final result of all rules with the same output can be written as:

function maximum(a, b: std_logic_vector) return std_logic_vector is


variable max: std_logic_vector(7 downto 0) := (others => '0'); begin
if a > b then max := a; else max := b; end if; return max; end maximum;

By combining the “minimum” and “maximum’ functions, the truth value for each rule can be
obtained as:

C <= maximum( minimum(A1, B1), minimum(A2, B2));

4. DEFUZZIFICATION
During the defuzzification process, each fuzzy output is multiplied by its corresponding
singleton position. The sum of this product is divided by the sum of all fuzzy output to obtain the
result of the final output. In VHDL, this is implemented as:

For i = 1 to n do begin product = (s(i) × f(i)) + product; sum = f[i] + sum; end for loop;
output = product / sum;

Although several types of membership functions, such as singleton, trapezoidal etc., could be
used to describe the output using VHDL, the defuzzification calculation are not the same, some
are more complicated than others. If trapezoidal shapes are used, finding the center of gravity
and overall area of the membership functions are considerably more complicated than the
calculation on singletons, and the results are not necessary effective or better [8].

5. FUZZY CONTROLLER FOR AIR CONDITIONING CONTROL SYSTEM


In the following discussion, an example of a
classical temperature fuzzy control system is TE Fuzzy
AC
Controller
given to illustrate the implementation of a fuzzy TE/dt
logic controller using the VHDL code. Figure 2
depicts a basic model of a simple fuzzy Figure 2. Fuzzy Controller for Air
Conditioning System
controller air conditioning system.
There are two control input variables to adjust the temperature of an air conditioning (AC)
room: the temperature error, TE, which is defined as the difference between the current
temperature and the target set temperature, and TE/dt, the rate of the temperature error change
with time:
TE = Tset-point - Tpresent (5)
and
dTE/dt = [TEn – TEn-1] / [tn – tn-1] (6)

where: TE = Temperature Error, t = time, n = integer

5.1. Define Temperature Membership Functions:


5.1.1. Temperature Error (TE)
The membership functions for TE consists of seven fuzzy logic ranges that can be defined
using the linguistic terms as Cold, Cool, Mild, Warm, and Hot. Table 1 shows the partitions for
the variable temperature. In this case, the ranges are subjectively chosen for demonstrating the
fuzzification process in VHDL.
The graphical representation of the member-ship function of TE is depicted in Figure 3. The
triangular and trapezoidal shapes of membership function will be used throughout this application
because of their simplicity and efficiency of implementation in VHDL code.
The values of the x-axis of the membership function have been scaled as an 8-bit
hexadecimal value representing the actual reading from the temperature sensor. Similarly, the y-
axis for the grade of the membership function starts from $00 (false) to $FF (completely true).

Table 1. Range of the Cold Cool Mild Warm Hot


Variable Temperature ($FF) 1.0
Fuzzy Set Range (C°)
$A5 0.8
Cold -55 to +5
Cool -25 to +35 µ 0.6
Mild +5 to 65 $5A 0.4
Warm +35 to +95
0.2
Hot +65 to +125
($00) 0.0 5° 5° 65° 95° 125°
-55° -25°
To illustrate how the input ($00 ($2A) ($55 ($7F) ($AA) ($DA) ($FF)
membership functions are used, let’s TE 75°
look at the temperature 75°C as Figure 3. Membership function of Temperature
indicated by the vertical reference Error (TE)
line in Figure 3. The membership
functions of the five linguistic terms (Cold, Cool, Mild, Warm, and Hot) can be constructed as
followed:

¾ Cold : Point1 = -55° ($00), Slope1 = 255, Point2 = -25° ($2A), Slope2 = 6
¾ Cool : Point1 = -25° ($2A), Slope1= 6, Point2 = 5° ($55), Slope2 = 6
¾ Mild : Point1 = 5° ($55), Slope1 = 6, Point2 = 35° ($7F), Slope2 = 6
¾ Warm : Point1 = 35° ($7F), Slope1 = 6, Point2 = 65° ($AA), Slope2 = 6
¾ Hot : Point1 = 65° ($AA), Slope1= 6, Point2 = 95° ($FF), Slope2 = 255

Since the 75°C vertical line intersects two membership functions, warm and hot respectively,
the degrees of these two membership function intersections can be calculated using the three
segment information as described above to obtain the grade of each membership intersection.
The following calculation shows how the grade of the membership is calculated for the
temperature value of 75°C:

The vertical line is in segment 2 of the warm label, therefore:

Grade = $FF – ((Input value – point 2) * slope 2)


= $FF – ((75° – 65°) * 6) = 255 – (10*6) = 195 = $C3 (7)

The vertical line is in segment 1 of the hot label, therefore:

Grade = (Input value – point 1) * slope 1 = ((75° – 65°) * 6) = 60 = $3C (8)


The following declaration defines the data structure description of the TE membership
function in VHDL:

type te_type is (cold, cool, mild, warm, hot, none); type te_ membership is record term: te_type;
point1: std_logic_vector(7 downto 0); slope1: std_logic_vector(7 downto 0);
point2: std_logic_vector(7 downto 0); slope2: std_logic_vector(7 downto 0);end record;
type te_membership_functions is array(natural range <>) te_membership;
constant te_mfs: te_membership_functions :=
((term => cold, point1 => x"00", slope1 => x"FF", point2 => x"2A", slope2 => x"06"), (term
=> cool, point1 => x"2A", slope1 => x"06", point2 => x"55", slope2 => x"06"), (term =>
mild, point1 => x"55", slope1 => x"06", point2 => x"7F", slope2 => x"06"),
(term => warm, point1 => x"7F", slope1 => x"06", point2 => x"AA", slope2 => x"06"),
(term => hot, point1 => x"AA", slope1 => x"06", point2 => x"D5", slope2 => x"FF"),
(term => none, point1 => x"FF", slope1 => x"FF", point2 => x"FF", slope2 => x"FF"));

5.1.2. Rate of Temperature Change (TE/dt)


The second input variable to the temperature fuzzy controller is the rate of change of
temperature with respect to time. This calculation results in the rate of temperature change per
time interval as fined below:

(present temperature reading) – (previous temperature reading)


rate = (9)
time between temperature reading

The purpose of implementing the Slow Moderate Fast


rate of temperature change in fuzzy ($FF) 1.0
control is to determine the velocity at 0.8
which the process should react. Since 0.6
time between temperature reading is µ 0.4
triggered by every clock cycle in this
application, the rate of temperature 0.2
change can be simplified to become ($00)
-5°C -3°C 0°C +3°C +5°C
the difference between the current
($00) ($32) ($7F) ($CD) ($FF)
and previous temperature reading.
TE/dt
As usual, the rate of temperature
change takes three linguistic terms Figure 4. Membership function of Rate of Temperature
(Slow, Moderate and Fast) as its Change (TE/dt)
membership function. The graphical representation of this membership function is shown in
Figure 4. The data structure of the TE/dt membership function in VHDL is basically the same as
TE. It is defined as:

type tr_type is (slow, moderate, fast); type tr_ membership is record term: tr_type;
point1: std_logic_vector(7 downto 0); slope1: std_logic_vector(7 downto 0);
point2: std_logic_vector(7 downto 0); slope2: std_logic_vector(7 downto 0); end record;
type tre_membership_functions is array(natural range <>) tr_membership;
constant tr_mfs: tr_membership_functions :=
((term => slow, point1 => x"00", slope1 => x"00", point2 => x"32", slope2 => x"03"),
(term => moderate, point1 => x"32", slope1 => x"03", point2 => x"7E", slope2 => x"03"),
(term => fast, point1 => x"7E", slope1 => x"03", point2 => x"FF", slope2 => x"FF"),
(term => none, point1 => x"FF", slope1 => x"FF", point2 => x"FF", slope2 => x"FF"));

6. AIR CONDITIONING CONTROL OUTPUT (AC)


The output membership functions
Very Low Low Medium High Very High
of the fuzzy temperature control are ($FF) 1.0
singletons, Five different linguistic 0.8
terms of the singleton output (Very
Low, Low, Medium, High and Very 0.6
High) are used to describe the heater
µ
0.4
control variation. Figure 5 shows the
graphical representation of the 0.2
singletons of these five variables. The ($00) 0.0
assignment to the output member- 0.0 20% 40% 60% 80% 100%
($00) ($FF)
ship function in VHDL is simply AC
declared as an 8-bit constant value, Figure 5. Membership Function of Air
that is: Conditioning Control (AC)

constant very_low : std_logic_vector := x"2A"; constant low : std_logic_vector := x"55";


constant medium : std_logic_vector := x"7F"; constant high : std_logic_vector := x"A8";
constant very_high : std_logic_vector := x"D2";
type singletons is array (0 to 4) of std_logic_vector(7 downto 0);
signal ac : singletons := (very_low, low, medium, high, very_high);

7. FUZZY INFERENCE MODULE


In this step, each input value is applied to its membership function to find the fuzzy input
value. In an application of two inputs, one having five membership functions and the other having
three, there are totally eight degrees of membership functions needed to be calculated. Since a
specific input value only intersects at most two membership functions to create the degree of
membership function for the corresponding input, so most of them will be zero. As described
previously, two points and two slopes are used to define the structure of a membership function.
The following pseudo-code illustrates the procedure of this process:

Fuzzification():

Set n = number of membership function; Set µ = array of degree of membership function;


Set membership = array of membership function; For i = 1 to n do begin
if input value < membership[i].point 1 then µ[i] = 0;
else if input value < membership[i].point 2 then
µ[i] = (input value – membership[i].point 1) × membership[i].slope 1;
else µ[i] = 255 - (input value – membership[i].point 2) × membership[i].slope 2;
end if; end for loop;

8. RULE INFERENCE MODULE


After the degrees of membership function have been determined in the fuzzification stage, the
next step is to use linguistic rules to decide what action is needed to be taken in response to a
given set of degree of membership function. A technique called min-max inference [8,9,10] is
used to calculate numerical results of the linguistic rules based on the system input values. The
numerical results from this calculation are called fuzzy outputs, since different rules that can be
true for different degrees of membership function variation, thus introducing composite results.
A system with two inputs, one having five linguistic terms and the other having three, and
one output with five linguistic terms has a total of 5×3×5 = 75 different rules can be used to
describe the complete fuzzy control strategy. Although there are totally 75 possible rule
rule evaluations in this case, Table 2. Rule Evaluation of the Air Conditioning System
only eight of them are enough to IF THEN
be used in describing how the Rule Temperature Logical Rate Air
fuzzy control system should Error (TE) Operator Change Conditioning
be operated and the remaining (TE/dt) (AC)
rule combinations can be 1 Cold AND Fast Very High
discarded. The reason for 2 Cold AND Slow High
choosing only the important 3 Cool AND Moderate Medium
rule to be evaluated is to mimic 4 Mild AND Slow Medium
the actual behavior of human 5 Warm AND Slow Low
thinking. This will allow the 6 Warm AND Moderate Low
ability of a fuzzy control system 7 Hot AND Slow Very Low
to approximate human thought 8 Hot AND Fast Very Low
using a simple description of the system behavior. Table 2 summarizes these eight important rules
of evaluation for this application.
The IF part of the rule gets the degree of the input membership function and the THEN part
has the degree of membership function that is determined by the result of the rule evaluation.
Since the input grades are connected by a logical “AND” operator, one input is dependent upon
the other, selecting the minimum value will satisfy both conditions. Therefore, taking the
minimum of the two variables will be used as the output grade of membership function in each
rule.
One way to illustrate how the fuzzy control evaluates rules is to use a specific example.
Suppose the room temperature set point is 75°C and any change greater than 1°C per second is
defined as a fast rate of temperature change inside the room. A graphical representation of the
rule evaluation process is presented in Figure 6.
TE TE/dt
Cold Cool Mild Warm Hot Slow Moderate Fast
Min
Cold 0.0 AND Fast
Rule 1
0.3 = 0.0
Cold AND Slow
Rule 2 0.0 0.0 = 0.0
Cool Moderate
Rule 3 0.0 AND 0.75 = 0.0
Mild 0.0 AND Slow
Rule 4 0.0 = 0.0
Slow
Rule 5 Warm 0.8 AND 0.0 = 0.0
Warm 0.8 AND Moderate
Rule 6 0.75 = 0.75
Slow
Rule 7 Hot 0.2 AND 0.0 = 0.0
Fast
Rule 8 Hot 0.2 AND 0.3 = 0.2

75°C 1°C/s
Figure 6. Rule Evaluation Example

Understanding the evaluation of these rules in the fuzzy inference stage, the set of rule
evaluation in VHDL can be described as follow:

Rule 1: very_high <= minimum(cold, fast);


Rule 2: high <= minimum(cold, slow);
Rule 3 and Rule 4: medium <= maximum( minimum(cool, moderate), minimum(mild, slow));
Rule 5 and Rule 6: low <= maximum( minimum(warm, slow), minimum(warm, moderate));
Rule 7 and Rule 8: very_ low <= maximum( minimum(hot, slow), minimum(hot, fast));

9. DEFUZZIFICATION MODULE
After the output grade of each rule has been determined, the next step is to combine all the
output grades into a single value that can be used to control the heater operation. In the
defuzzifier process, Center-of-Gravity (COG) [8, 9, 10] deffuzifiction technique will be used to
obtain the final system output. In this application, singleton variables are used as the output
membership functions. Defuzzification involves taking the weighted average of all the fuzzy
outputs. Each fuzzy output is multiplied by its corresponding singleton value, then the sum of
these products divided by the sum of all the fuzzy outputs to obtain the final single output
variable. The following pseudo-code illustrates the procedure of this defuzzification process:

Defuzzification():

Set n = number of output membership function; Set s = array of singleton of output


membership function; Set f = array of result of all rule evaluations; Set sum = 0
For i = 1 to n do begin product = (s(i) × f(i)) + product; sum = f[i] + sum; end for loop;
output = product / sum;

CONCLUSION
The design of a typical fuzzy logic controller using VHDL is presented in this paper. Once
the basic design of the fuzzy logic control system has been defined, the implementation of the
fuzzy logic controller is very straight forward by coding each component of the fuzzy inference
system in VHDL according to the design specifications. The availability of different synthesis
tools for the programmable logic devices such as FPGA and CPLD have made it easier for the
designers to experiment their design capabilities. By simply changing some parameters in the
codes and design constraint on the specific synthesis tool, one can experiment with different
design circuitry to get the best result in order to satisfy the system requirement.

REFERENCES
[1] Madni Asad M, Wan L. A, Hansen, Robert K, Vuong Jim B, “Adaptive Fuzzy Logic Based Control
System for Rifle Stabilization”, Proceedings, 1998 World Automation Congress (WAC ’98),
Anchorage, Alaska, May 10-14, 1998, pp.103-112.
[2] Madni Asad M, Wan L.A, Vuong J. and Vuong P, “Fuzzy Logic Based Smart Controller for a
Cryogenic Cooler”, Proceedings 1996 World Automation Congress (WAC ’96), International
Symposium of Soft Computing in Industry (ISSCI), May 27-30, 1996, Volume 5, pp.481-488.
[3] Madni Asad M, Wan L. A, Kuo D and Vuong J, “Sensor Based Microcontroller Unit with Built In
Fuzzy Inference for an Endometrium Ablator”, Proceedings 1995, 3rd European Congress on
Intelligent Techniques and Soft Computing (EUFIT ’95), August 28-31, 1995, pp. 1621-1624.
[4] Zadeh, L.A., "Fuzzy Sets," Information and Control, 8, 338-353, 1965.
[5] Brubaker, David I., "Fuzzy Logic Basics: Intuitive Rules Replace Complex Math," EDN, June 18,
1992, pp.111.
[6] Glenn A, “Fundamentals of Fuzzy Logic Part I & II”, SENSORS, April 1993.
[7] Earl Cox, The Fuzzy Systems Handbook (1994), ISBN 0-12-194270-8
[8] Sibigtroth J, “Implementing Fuzzy Expert Rules in Hardware”, AI Expert, April 1992.
[9] Sibigtroth J, “Creating Fuzzy Micros”, Embedded Systems Programming, Vol. 4, December 1991.
[10] Sibigtroth J, “Fuzzy Logic for Embedded Microcontrollers”, Circuit Cellar, March 1995.
[11] Kevin Skahill, “VHDL for PROGRAMMABLE LOGIC”, Cypress Semiconductor, Addison-Wesley
Publishing., Inc. 1996.

You might also like