                       Two Stage Inverted Pendulum

                       
     FIDE Application Note 008-921014       Aptronix Inc., 1992
     
     
     INTRODUCTION
     
     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.

     
     SYSTEM DESIGN
     
     Two stage inverted pendulum is illustrated in Figure 1. A
     servo motor produces a force  to move a cart in order to
     balance the two stage inverted pendulum on the cart, i.e., to
     keep Q1 and Q2 to be zero. Q1 is the angle of the first stage
     pendulum from the vertical direction, while Q2 is the angle of
     the second stage pendulum from the vertical direction. These
     two angles are measured with two potentiometers*.
     
     
     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 Q1 = 0. However when we have two stages, the
     control target for the first stage is no longer Q1 = 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 Q2, determine an
          angle Q for the first stage. We set Q = Q2.
          Adjust the first stage to achieve Q1 = Q.
     Because the target of the outer loop is Q2 = 0, the final target of
     the system is Q1 = Q2 = 0.
     
                                    
     CONTROLLER DESIGN
     
     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.
     
     
     Control Curve
     
     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.
     
     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 e equals zero. When e 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
                                    
                         Table 1  Control Rules
                                    
                                    
     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.
     
     
     TUNING
     
     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 function 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.
     
     
     IMPLEMENTATION
     
     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 L6 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 L1 and 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.
     
     COMMENTS
     
     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.
     
              (Weijing Zhang, Applications Engineer, Aptronix Inc.)
     
     
     NOTE:
     All information provided in this note is subject to change
     without notice and does not represent a commitment on the part
     of Aptronix Incorporated.
     
                    For Further Information Please Contact:
                                    
                          Aptronix Incorporated
                      2150 North First Street #300
                           San Jose, CA 95131
                           Tel  (408) 428-1888
                           Fax (408) 428-1884
                   FuzzyNet (408) 428-1883  data 8/N/1
     
                        Aptronix Company Overview
     
     Headquartered in San Jose, California, Aptronix develops and
     markets fuzzy logic-based software, systems and development
     tools for a complete range of commercial applications.  The
     company was founded in 1989 and has been responsible for a
     number of important innovations in fuzzy technology.
     
     Aptronix's product Fide (Fuzzy Inference Development
     Environment) -- is a complete environment for the development
     of fuzzy logic-based systems.  Fide provides system engineers
     with the most effective fuzzy tools in the industry and runs
     in MS-WindowsTM  on 386/486 hardware.  The price for Fide is
     $1495 and can be ordered from any authorized Motorola
     distributor.  For a list of authorized distributors or more
     information, please call Aptronix.  The software package comes
     with complete documentation on how to develop fuzzy logic
     based applications, free telephone support for 90 days and
     access to the Aptronix FuzzyNet information exchange.
     
     
     
     FIDE Application Notes Available:
     
      No.         Title                     Topics
      #1  Washing Machine       Decision Making, Determining
                                Wash Time
      #2  Automatic Focusing    Decision Making, Determining
          System                Focus
      #3  Servo Motor Force     Servo Control, Grasping
          Control               Object
      #4  Temperature           Process Control, Glass
          Control(1)            Melting Furnace
      #5  Temperature           Process Control, Air
          Control(2)            Conditioner
      #6  Temperature           Process Control, Reactor
          Control(3)
      #7  Automatic             Decision Making, Determining
          Transmission          Gear Shift
      #8  Two Stage Inverted    Servo Control, Details on
          Pendulum              System Design
     
     
     APPENDIX A :  FIU SOURCE CODE
     
          $ FILENAME: pdlm2.fil
          
          $ FIU HEADER
          
          fiu tvfi (min max);
          
          $ DEFINITION OF INPUT VARIABLE(S)
          
          >velocity " "   [
                PL (@0,255, @32,255, @80,0),
                PM (@32,0, @80, 255, @112,0),
                PS (@80,0, @112,255, @144,0),
                NS (@112,0, @144,255, @176,0),
                NM (@144,0, @176,255, @222,0),
                NL (@176,0, @222,255, @255,255)
              ];
          
          >angle " "  [
                NL (@0,255, @64,0),
                NM (@0,0, @64, 255, @128,0),
                ZR (@64,0, @128,255, @192,0),
                PM (@128,0, @192,255, @255,0),
                PL (@192,0, @255,255)
              ];
          
          $ DEFINITION OF OUTPUT VARIABLE(S)
          
          < voltage "volt" * (
                    NL = 0,
                    NM = 90,
                    NS = 106,
                    PS = 150,
                    PM = 166,
                    PL = 255
                  );
          
          $ RULES
          
          if angle is NL and velocity is NL  then voltage is NL;
          if angle is NL and velocity is NM  then voltage is NL;
          if angle is NL and velocity is ZR  then voltage is NL;
          if angle is NL and velocity is PM  then voltage is NM;
          if angle is NL and velocity is PL  then voltage is NS;
          
          if angle is NM and velocity is NL  then voltage is NL;
          if angle is NM and velocity is NM  then voltage is NL;
          if angle is NM and velocity is ZR  then voltage is NM;
          if angle is NM and velocity is PM  then voltage is NS;
          if angle is NM and velocity is PL  then voltage is PS;
          
          if angle is NS and velocity is NL  then voltage is NL;
          if angle is NS and velocity is NM  then voltage is NM;
          if angle is NS and velocity is ZR  then voltage is NS;
          if angle is NS and velocity is PM  then voltage is PS;
          if angle is NS and velocity is PL  then voltage is PM;
          
          if angle is PS and velocity is NL  then voltage is NM;
          if angle is PS and velocity is NM  then voltage is NS;
          if angle is PS and velocity is ZR  then voltage is PS;
          if angle is PS and velocity is PM  then voltage is PM;
          if angle is PS and velocity is PL  then voltage is PL;
          
          if angle is PM and velocity is NL  then voltage is NS;
          if angle is PM and velocity is NM  then voltage is PS;
          if angle is PM and velocity is ZR  then voltage is PM;
          if angle is PM and velocity is PM  then voltage is PL;
          if angle is PM and velocity is PL  then voltage is PL;
          
          if angle is PL and velocity is NL  then voltage is PS;
          if angle is PL and velocity is NM  then voltage is PM;
          if angle is PL and velocity is ZR  then voltage is PL;
          if angle is PL and velocity is PM  then voltage is PL;
          if angle is PL and velocity is PL  then voltage is PL
          
          end
     
     APPENDIX B:**   MC68HC11 ASSEMBLY CODE
     
        
     APPENDIX C** :  SCHEMATIC   SHEET 1
     
     APPENDIX C** :  SCHEMATIC   SHEET 2
     
     APPENDIX C** :  SCHEMATIC   SHEET 3
     
     APPENDIX C** :  SCHEMATIC   SHEET 4
     
     ** Schematics and assembly code available on request from Aptronix, Inc.
     _______________________________
     * Actually what we get from the second potentiometer is a
     measurement of Q2-Q1, not Q2.  An additional circuit is included in
     the system to obtain Q2 from Q1 and Q2-Q1. For details, see Appendix C.
          
     
     
     
     
     
     
