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:
[email protected] [email protected] [email protected]
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 Point α 1, where µ is limited to a maximum Segment 1 Point 2 value of 1, (c) in segment 2: slope is Segment 0 1 Segment 2 downward from left to right, Slope 2 therefore: µ = 1 - (Input value – 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)
Slope 2 = 1 / (12 – 9) = $FF / 3 = 255 / 3 = 85 = $55
(2)
and 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 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 Moderate Slow 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 0.2 application, the rate of temperature change can be simplified to become ($00) 0°C +3°C +5°C -5°C -3°C the difference between the current ($7F) ($CD) ($FF) ($32) ($00) and previous temperature reading. TE/dt As usual, the rate of temperature Figure 4. Membership function of Rate of Temperature change takes three linguistic terms Change (TE/dt) (Slow, Moderate and Fast) as its 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 0.6 Low, Low, Medium, High and Very µ High) are used to describe the heater 0.4 control variation. Figure 5 shows the 0.2 graphical representation of the singletons of these five variables. The ($00) 0.0
assignment to the output membership function in VHDL is simply declared as an 8-bit constant value, that is:
0.0 ($00)
20%
40%
AC
60%
80% 100% ($FF)
Figure 5. Membership Function of Air 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
Table 2. Rule Evaluation of the Air Conditioning System rule evaluations in this case, 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 4 Mild AND Slow Medium rule to be evaluated is to mimic 5 Warm AND Slow Low the actual behavior of human 6 Warm AND Moderate Low thinking. This will allow the 7 Hot AND Slow Very Low ability of a fuzzy control system 8 Hot AND Fast Very Low to approximate human thought 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/dt
TE Cold Cool Mild Warm Hot
Slow Moderate Fast Min
Rule 1 Rule 2 Rule 3 Rule 4
Cold Cold Cool Mild
Rule 5
Warm
Rule 6
Warm
0.0
AND
0.0
AND
0.0
AND
0.0
Rule 7
Hot
Rule 8
Hot
AND
0.8
AND
0.8
AND
0.2
AND
0.2
AND
Fast Slow Moderate Slow Slow Moderate
0.3
= 0.0
0.0
= 0.0
0.75 = 0.0 0.0
= 0.0
0.0
= 0.0
0.75 = 0.75
Slow Fast
0.0
= 0.0
0.3
= 0.2
1°C/s 75°C 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