**FUZZY LOGIC FOR "JUST PLAIN FOLKS " **

Chapter 3. Let's Build a Fuzzy Logic Control System

Building a System to Gain Understanding and Familiarity

The easiest and quickest way to understand fuzzy logic control is to build a fuzzy logic control system; following is one example:

This is a fuzzy logic speed control example, using the same techniques as used by Professor Mamdani, that you can build for yourself to get experience with fuzzy logic control. I recommend you do build some kind of system. I found I began more and more to understand what fuzzy logic was all about as I tried to make the system work. The following example system has been reduced in complexity to make it easier to understand, but the concepts are the same as those used by Mamdani.

If your application is more demanding than the following example, you add inputs and "rules"; you do not have to learn new things or change the approach. In considering this reduced complexity example, it may be observed that control could have been effected without going through the fuzzy control exercise we are about to go through. This would be correct, but only because we are working with a simple system, only one input and no discontinuities or aberrations requiring patching.

Following is a system diagram, Figure 3, for a "getting acquainted with fuzzy" project that provides speed control and regulation for a DC motor. The motor maintains "set point" speed, controlled by a stand-alone converter-controller, directed by a BASIC fuzzy logic control program in a personal computer.

Parts List

(1) IBM or compatible personal computer equipped to run Microsoft Quick BASIC. IBM is a registered trademark of IBM Corporation. Microsoft and Quick BASIC are registered trademarks of Microsoft, Inc.

(2) Controller (see below).

(3) Signal conditioner (transistor amplifier to adjust levels as needed).

(4) Transistor - 2N3053.

(5) DC motor, 1.5 V to 3.0 V, 100 ma., 1100 Rpm to 3300 Rpm, and compatible generator.

The above speed control system is low cost and suitable for learning at home where being rigorously, mathematically correct is not required. It is important to be aware that this speed controller is only an experimental controller to get familiar with the fuzzy logic concept. It is not what engineers call a rigorous, technically correct application of fuzzy logic. The difference is in the fact that this approach does not add triangles to compute center of mass as specified by Dr. Bart Kosko (Fuzzy Thinking, Chapter 10). Adding triangles can be done, but is difficult and time consuming, however that is the way a truly professional application would be designed. There are IC’s that do it all and commercially available fuzzy logic controllers that do everything correctly.

This fuzzy logic controller project was done under pressure of very limited money available, resulting in an inexpensive approach. What is needed is an analog to digital converter, which connects to a PC, and a digital to analog output device from the PC to the transistors and DC motor-generator being controlled. Often this is all in one plug-in card that goes inside the PC. Plug in the A to D and D to A converter in the PC and write a program to measure the input and control the output according to fuzzy logic principles. This approach can be somewhat expensive and was not used in this case.

For this experiment, the controller was a 40-8 controller manufactured by Prairie Digital Company. Click on the following Web page to see this controller. http://www.prairiedigital.com/Products/model40.htm (The author has no connection at all with Prairie Digital.) The 40-8 controller is external to the computer, connecting to the PC via a standard RS-232 serial port. The serial port connects to the 40-8 controller via a serial cable. A BASIC program is used to communicate with the 40-8 controller. The Prairie Digital instruction book has sample programs showing how to do this. Through a BASIC program, you can read the analog voltage level on one of the 40-8 analog input lines, then tell the 40-8 to output a pulse-width-modulated signal. By controlling the pulse width of the 40-8 output, the average value of the output is the equivalent of varying the level of the output in an analog fashion.

If not constrained by cost, a 12 bit, A to D unit should be used, rather than the 8 bit unit. This would provide improved control. This approach, using the 40-8 controller, is low cost, in the range of $100 to buy the 40-8. Purchasing the items Prairie Digital offers to accompany the controller, that is the connector, cables, software, etc., is recommended. It costs very little extra, but is well worth it.

With regard to the other hardware, only low cost transistors, resistors, capacitors, etc., were used for the signal conditioner providing input to the motor-generator. The DC motor and DC generator were small, low power units purchased from a surplus catalog. The motor output shaft was connected to the generator input shaft with a small section of shrink insulation tubing; cheap, simple and effective. The power supply for everything, including the 40-8, was a 12 Volt DC power supply removed from an old Apple computer.

National Instruments, www.natinst.com, sells a fuzzy logic system where the fuzzy control action is accomplished by the software. National Instruments applications engineers recommend one of their several analog/digital in, digital/analog out converters for your application and provide a mathematically correct software program to produce fuzzy control action. Their system also provides attractive screen display color graphics. Needless to say, cost of the National Instruments system is considerably above the $100 range. One would use the National Instruments approach for a large, complex system where flexibility and changes down the road are involved, such as automating a processing plant.

Where using a personal computer is not practical because of space and weight limitations, fuzzy logic control is also available utilizing microchips manufactured by Motorola. These microchips are suitable for fuzzy control applications, www.mcu.motsps.com. One would use this approach if developing, for example, a fuzzy logic anti-lock braking system (see "Fuzzy Logic, Revolutionizing Automotive Engineering; Circuit Cellar INK magazine, November 1997; www.circuitcellar.com).

The steps in building our system are:

1. Determine the control system input. Examples: The temperature is the input for your home air conditioner control system. Speed of the car is the input for your cruise control.

In our case, input is the speed in Rpm of the DC motor, for which we are going to regulate the speed. See Figure 3 above. Speed error between the speed measured and the target speed of 2,420 Rpm is determined in the program. Speed error may be positive or negative. We measure the DC output voltage from the generator. This voltage is proportional to speed. This speed-proportional voltage is applied to an analog input channel of our fuzzy logic controller, where it is measured by the analog to digital converter and the pesonal computer, including appropriate software.

2. Determine the control system output. For a home air conditioner, the output is the opening and closing of the switch that turns the fan and compressor on and off. For a car's cruise control, the output is the adjustment of the throttle that causes the car to return to the target speed.

In our case, we have just one control output. This is the voltage connected to the input of the transistor controlling the motor. See Figure 3.

3. Determine the target set point value, for example 70 degrees F for your home temperature, or 60 Miles per hour for your car.

In our case, the target set point is 2,420 Rpm.

4. Choose word descriptions for the status of input and output.

For the steam engine project, Professor Mamdani used the following for input:

Positive Big

Positive Medium

Positive Small

Almost No
Error

Negative Small

Negative Medium

Negative Big

Our system is much less complicated, so let us select only three conditions for input:

Input Status Word Descriptions

Too slow

About right

Too fast

And, for output:

Output Action Word Descriptions

Speed up

Not much change needed

Slow down

RULES

Translate the above into plain English rules (called "linguistic" rules by Dr. Zadeh). These Rules will appear in the BASIC computer program as "If-Then" statements:

Rule 1: If the motor is running too slow, then speed it up.

Rule 2: If
motor speed is about right, then not much change is needed.

Rule 3: If motor
speed is to fast, then slow it down.

The next three steps use a charting technique which will lead to a computer program. The purpose of the computer program is to determine the voltage to send to the speed controlled motor. One function of the charting technique is to determine the "degree of membership" (see Ch. 1) of the Too slow, About right and Too fast triangles, for a given speed. Further, the charting technique helps make the continuous control feedback loop easier to visualize, program and fine tune.

5. Associate the above inputs and outputs as causes and effect with a Rules Chart, as in Figure 4, below. The chart is made with triangles, the use of which will be explained. Triangles are used, but other shapes, such as bell curves, could also be used. Triangles work just fine and are easy to work with. Width of the triangles can vary. Narrow triangles provide tight control when operating conditions are in their area. Wide triangles provide looser control. Narrow triangles are usually used in the center, at the set point (the target speed). For our example, there are three triangles, as can be seen in Figure 4 (three rules, hence three triangles).

6. Figure 4 (above) is derived from the previously discussed Rules and results in the following regarding voltage to the speed controller:

a. If speed is About right then Not much change needed in voltage to the speed controller.

b. If speed is Too slow then increase voltage to the speed controller to Speed up.

c. If speed is Too fast then decrease voltage to the speed controller to Slow down.

7. Determine the output, that is the voltage that will be sent from the controller/signal conditioner/transistor to the speed controlled motor. This calculation is time consuming when done by hand, as we will do below, but this calculation takes only thousandths of a second when done by a computer.

Assume something changes in the system causing the speed to increase from the target speed of 2,420 Rpm to 2,437.4 Rpm, 17.4 Rpm above the 'set point." Action is needed to "pull" the speed back to 2,420 Rpm. Intuitively we know we need to reduce the voltage to the motor a little. The "cause" chart and vertical speed line appear as follows, see Figure 5 below:

The vertical line intersects the About right triangle at .4 and the Too fast triangle at .3. This is determined by the ratio of sides of congruent triangles from Plane Geometry:

Intersect point / 1 = 11.6/29 = .4

Intersect point / 1 = 17.4/58 = .3

8. The next step is to draw "effect" (output determining) triangles with their height "h" determined by the values obtained in Step 7, above. The triangles to be drawn are determined by the rules in Step 6. Since the vertical 2,437.4 Rpm speed line does not intersect the Too slow triangle, we do not draw the Speed up triangle. We draw the Not much change and the Slow down triangles because the vertical speed line intersects the About right and Too fast triangles. These "effect" triangles will be used to determine controller output, that is the voltage to send to the speed control transistor. The result is affected by the widths we have given the triangles and will be calculated. See Figure 6, below. The Not much change triangle has a height of .4 and the Slow down triangle has a height of .3, because these were the intersect points for their matching "cause" triangles; see Figure 4, above.

The output, as seen in Figure 6 (above), is determined by calculating the point at which a fulcrum would balance the two triangles, as follows:

The Area of the Not much change triangle is: 1/2 X Base X Height = .5 X .04 X .4 = .008. Area of the Slow down triangle is .5 X .08 X .3 = .012.

Compute the controller output voltage by finding the point on the output voltage, Vdc, axis where the "weight" (area) of the triangles will balance. Assume all the weight of the Not much change triangle is at 2.40 Vdc and all the weight of the Slow down triangle is at 2.36 Vdc. We are looking for the balance point.

Find the position of the controller output voltage (the balance point) with the following calculation:

(Eq. 1) .008 X D1 = .012 X D2

(D1 is the fulcrum distance
from 2.4 V. D2 is the fulcrum distance from 2.36 V.)

(Eq. 2) D1 + D2 = .04 (from Figure 6)

D1 = .04 - D2

Solving the above by substituting (.04-D2) for D1 in Equation 1 gives D2 = .016 and D1 = .024, therefore the balance point is a voltage of 2.376 Vdc, and this is the voltage which we have determined should be applied to return speed to the target value. See Figure 6, above.

Keep in mind that we are only discussing one sample at one instant in time, with a resulting controller output voltage; the controller is sampling several times each second with a resulting "correction" output following each sample.

The above system was tested with changing loads on the rotating shaft, and returned the speed of the motor to within 2 % of the 2,420 Rpm set point in less than 1.5 seconds. The accuracy with which the set point speed can be maintained is determined by the resolution of the analog to digital and digital to analog conversion circuits in the fuzzy logic controller. Typical "low cost" resolution is "8 bit", 256 increments. Higher cost "12 bit" units provide 4,096 increments.

Please note: The above is a very effective, but much simplified, version of computer based fuzzy logic control systems actually in use commercially. If your application is of a more demanding, complex or commercial nature, we suggest you refer to Fuzzy Thinking, a book by Bart Kosko, Ph.D., Chapter 10, Hyperion, New York, 1993. Dr. Kosko is one of the world's leading proponents of fuzzy control and among the most knowledgeable regarding fuzzy control theory. In the Kosko method, the intersecting triangles are added, then the total area of the added triangles determined by integration. Fulcrum location is determined by computer integration of area "under the curve" to the point of one half the total area. This sounds complicated, but only requires a few thousandths of a second for a computer, once the program is set up.

For more complex systems with additional inputs (for example, using rate of change as an input in addition to speed error), the approach is as above, but there are two or more "sub-outputs" to be considered in arriving at one crisp output to control the system. This is handled by averaging these sub-outputs with a weighting determined by the system designer and inserted in the program. This weighting may be based on theoretical prediction, previous experience with a similar manual system and/or experimentation and "tuning" of the system, once it is assembled.

Patch It

For an individual control channel, fuzzy rules cover control requirements during a certain "range" of operation. In our example speed control system, one rule covered about right. There was an actual numerical upper limit and lower limit for about right. Our control rule for this range is sometimes referred to in fuzzy logic literature as a "patch." As you can see, the more patches we have over the control range, the better the control. Fortunately, most system control problems can be solved with relatively few patches. A patch, or rule, may be anything that solves the problem. If the system required it, you could even mix continuous feedback loop control and off-on control over a channel's control range, if that solved the problem.

The Program

The fuzzy logic program in the computer directs sending messages to and receiving messages from the controller, thereby directing the measurement and control operation and causing target and actual speed to be displayed. The fuzzy logic controller receives messages from the computer via BASIC language commands. Reply messages to the computer from the fuzzy logic controller are acquired via BASIC.

In this case, the computer was an IBM PC/XT. The programming language was Microsoft Quick BASIC. The program was compiled with Microsoft's compiler, but compiling is not essential. Compiling increases speed of execution and performance. Ideal computers for fuzzy logic control systems are often ancient IBM PC-XT computers, available in garage sales for $50. These computers are of no value for today's software, but work very adequately for fuzzy logic measurement and control applications. IBM is a trademark of IBM Corporation. Microsoft and Quick BASIC are trademarks of Microsoft, Inc.

The portion of the program for the above system system which examines the input and performs the "triangle" calculations to arrive at a crisp output follows:

910 IF MS = 2420 THEN MIV = 2.4 : GOTO 5000 'MS-MEASURED SPEED, MIV-MOTOR INPUT VOLTAGE

920 IF MS < 2420 THEN 2000 ELSE 1000

1000 ' LINES 1010-1110; GREATER THAN 2420 RPM, SLOW DOWN

1010 IF MS > 2449 THEN MIV = 2.36 : GOTO 5000

1020 ' COMPUTE INTERSECT POINT, IPA, FOR 'ABOUT RIGHT' TRIANGLE

1030 IPA = (2449-MS) / 29

1040 IF IPA =< 0 THEN IPA = .0001

1050 ' COMPUTE INTERSECT POINT, IPS, FOR 'SLOW DOWN' TRIANGLE

1060 IPS = (MS-2420) / 58

1070 ' COMPUTE MOTOR (TRANSISTOR) INPUT VOLTAGE (MIV)

1080 AAR = .5 * .04 * IPA 'AAR - AREA OF 'ABOUT RIGHT' TRIANGLE

1090 ASD = .5 * .08 * IPS 'ASD - AREA OF 'SLOW DOWN' TRIANGLE

1100 D1 = .04 * (ASD / (ASD+AAR))

1110 MIV = 2.4 - D1 : GOTO 5000

2000 ' LINES 2010-2110; LESS THAN 2420 RPM, SPEED UP

2010 IF MS < 2362 THEN MIV = 2.44 : GOTO 5000

2020 ' COMPUTE INTERSECT POINT, IPA, FOR 'ABOUT RIGHT 'TRIANGLE

2030 IPA = (MS-2391) / 29

2040 IF IPA =< 0 THEN IPA = .0001

2050 ' COMPUTE INTERSECT POINT, IPF, FOR 'SPEED UP' TRIANGLE

2060 IPF = (2420-MS) / 58

2070 ' COMPUTE MOTOR INPUT VOLTAGE (MIV)

2080 AAR = .5 * .04 * IPA 'AAR - AREA OF 'ABOUT RIGHT 'TRIANGLE

2090 ASU = .5 * .08 * IPF 'ASU - AREA OF 'SPEED UP' TRIANGLE

2100 D1 = .04 * (ASU / (ASU+AAR))

2110 MIV = 2.4 + D1

5000 '

The remainder of the program would be determined by the program requirements of the analog to digital/digital to analog controller in use. Program statements would be specific to the hardware selected. Almost any controller should be usable with the above BASIC statements, so long as the controller could be programmed in BASIC to measure inputs and send control output signals. Program execution would cycle in the sequence: 1. Measure input. 2. Analyze with the fuzzy logic program statements. 3. Send the output signal.

End Chapter 3.