![]()
APN08V20-1092
Introduction
System Design
Controller Design
Control Curve
Tuning
Implementation
Comments
Appendix A : FIU Source Code
Appendix B: MC68HC11 Assembly Code
So far, in the application note series, we have provided several examples showing how to create fuzzy controllers with FIDE. However, these examples do not provide topics on implementation of the designed system. In this application note, we use an example of an inverted pendulum to provide details on all aspects of fuzzy logic based system design.
We will begin with system design; analyzing control behavior of a two-stage inverted pendulum. We will then show how to design a fuzzy controller for the system. We will describe a control curve and how it differs from that of conventional controllers when using a fuzzy controller. Finally, we will discuss how to use this curve to define labels and membership functions for variables, as well as how to create rules for the controller.
Two stage inverted pendulum is illustrated in Figure 1. A servo motor produces a force f to move a cart in order to balance the two stage inverted pendulum on the cart, i.e., to keep q 1 and q 2to be zero. q 1 is the angle of the first stage pendulum from the vertical direction, while q 2 is the angle of the second stage pendulum from the vertical direction. These two angles are measured with two potentiometers*.
Figure 1 Two Stage Inverted Pendulum

Figure 2 shows the control system diagram. There are two control loops in this system. The inner loop is for controlling the first stage pendulum; the outer loop is for controlling the second stage. Without outer loop this becomes a single-stage inverted pendulum system. In that case, our control target is q 1 = 0. However when we have two stages, the control target for the first stage is no longer q 1 = 0. Instead, we have to adjust the first stage to a suitable angle in accordance with the angle of the second stage(see Figure 3). In other words, in a two stage inverted pendulum system, the purpose of the outer loop is to produce an angle for the first stage to follow.
We may summarize the control strategy for a two stage inverted pendulum as follows:
According to the angle of the second stage q 2, determine an angle q for the first stage. We set q = q 2.
Adjust the first stage to achieve q 1= q .
Because the target of the outer loop is q 2 = 0, the final target of the system is q 1 = q 2 = 0.
Figure 2 Two Stage Inverted Pendulum Control System


To create a fuzzy controller, let us start with analyzing control behaviors of the inverted pendulum system. For simplicity, we will use a single stage inverted pendulum shown in Figure 4.
Basically, a controller should reflect the relation between two variables q and f. In other words, if we know the relation between q and f , we can use it to write control rules. We know if q is large, f should be large; and if q is small, f should be small too.
Figure 4 Single Stage Inverted Pendulum

The relation between q and f can be denoted as a control curve (Figure 5). Using conventional PID controller, this control curve is a straight line (Figure 5a). However, the relation between q and f is nonlinear because force f should increase very fast when angle q is large in order to achieve q = 0. This improves response because the force is larger than that given by the linear curve when error is large. On the other hand, when angle q is very small, force f should be very small to reduce overshooting. A typical nonlinear control curve is shown in Figure 5b.
For the system we developed, the actual control curve is a little different from that in Figure 5b. Because of the physical characteristics, such as friction, force should be larger than a threshold value in order to move the cart. In addition, as a limitation of the output of a servo motor, force reaches its maximum value at a certain angle. The control curve for the actual inverted pendulum control system is illustrated in Figure 5c.
We can create a fuzzy controller from a control curve.
Figure 5 Control Curve: Linear Control and Nonlinear Control

Figure 6 Critical Points on a Control Curve

Figure 7 Define Labels & Membership Functions from Control Curve

There are some critical points on a control curve that can be used for controller design. Control curve in Figure 6 shows six points of this kind.
Rules can also be obtained from the control curve. There are two input variables for our controller. Suppose the velocity of the angle change is zero, we have a control curve for angle q as shown in Figure 7. From this curve, we obtain following rules,
IF q is NL THEN f is NL
IF q is NM THEN f is NM
IF q is NS THEN f is NS
IF q is PS THEN f is PS
IF q is PM THEN f is PM
IF q is PL THEN f is PL
Note that these rules are obtained when the velocity of the angle change q& equals zero. When q& is not zero, the control curve should be shifted. Velocity is given five labels and get the rules obtained listed in Table 1 and shown in Figure 8. From this figure, we see that the curves are not simply obtained by shifting. If curves were obtained by shifting, more labels for angle q would be necessary. Here we use as few labels as possible to keep the controller simple.
| Velocity | ||||||
| NL | NM | ZR | PM | PL | ||
| NL | NL | NL | NL | NM | NS | |
| NM | NL | NL | NM | NS | PS | |
| Angles | NS | NL | NM | NS | PS | PM |
| PS | NM | NS | PS | PM | PL | |
| PM | NS | PS | PM | PL | PL | |
| PL | PS | PM | PL | PL | PL |
Figure 8 Control Curves When Velocity Is Not Zero

In actuality, the output of the FIU(Fuzzy Inference Unit) is a voltage sent to the motor, used to produce the force. However, the design principle is the same.
FIDE's tools (such as Analyzer, MF-editor, Composer, etc.) are used to modify our design, as well as to tune membership functions and rules, because the actual control curve for a control system is essentially unknown. The tuning of membership functions will adjust the control curve (see Figure 7).
After tuning, our final design of the controller is obtained. The FIU source code created in FIDE for the two stage inverted pendulum can be found in Appendix A. The labels and membership functions for the variables of our controller are shown in Figures 9, 10, and 11. Figures 12 and 13 depict the control curves, and the I/O response surface of the controller is displayed in Figure 14.
Figure 9 Labels & Membership Functions of Variable Angle

Figure 10 Labels & Membership Functions of Variable Velocity

Figure 11 Labels & Membership Functions of Variable Voltage

Figure 12 Control Curve : Voltage(Angle)

Figure 13 Control Curve : Voltage(Velocity)

Figure 14 Input/Output Control Surface

When we are satisfied with our design, we then generate the machine code for a specific microprocessor.
For our two-stage inverted pendulum, a MC68HC11 MCU is used for implementation. Utilizing the RTC command provided in FIDE's menu, we can convert the object output of a designed fuzzy controller into Motorola MCU's assembly code (FIDE V1.0 supports three Motorola chips: MC6805, MC68HC05 and MC68HC11). For details on how to use the RTC command, please refer to the FIDE User's Manual, Chapter 5.
The process of tuning parameters shown in Figure 2 is as follows:
1. Determine l5 to get a suitable level of motor power. If l6 is large, the force applied to the cart will be large.
2. Determine l4 and l5 to keep first stage stable. These two parameters are very similar to those in a PD controller. A larger l4 produces a response with smaller overshoot and less oscillations than a smaller one while a smaller l4 produces a response that is closer to the set point. A larger l5 would decrease overshoot and oscillations and cause a long rising time. These two parameters should be adjusted together.
3. Determine l1, l2, l3 to keep the whole system stable. Choose l3 to provide a suitable setpoint for the first stage loop. This parameter should be adjusted with l1and l2. Adjusting l1 and l2 is similar to that of l4 and l5.
Appendix B provides the assembly code generated from the source code as shown in Appendix A. The generated code uses a memory of 870 bytes on a MC68HC11 MCU. One single control circle takes 4.48 ms. FIU is called two times in a control circle.
Control engineers are aware of the difficulty in designing a controller for a two-stage inverted pendulum using conventional approaches. We have shown in this application note, how easily a system is designed with fuzzy logic using FIDE. It is surprising that only an 8-bit MCU is required. Fuzzy system development environment provided by FIDE can lead not only to a faster development circle, but also lower costs.
For Further Information Please Contact:
FuzzyNet http://www.aptronix.com/fuzzynet
Email: fuzzynet@aptronix.com
Weijing Zhang,
Applications Engineer.
Copyright © 1992 by Aptronix
Inc.
Revised: October 21, 1996.