Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

A Fuzzy-Logic Torque Servo


March 1994/A Fuzzy-Logic Torque Servo

A Fuzzy-Logic Torque Servo

Jack J. McCauley


Since receiving a BSEE from UC Berkeley, Jack J. McCauley has been working as a Software Engineering Consultant. His specialty is real-time systems with an emphasis on servo controls and signal processing. He can be reached at (510) 531-1581.

Background

The developer of a control system desires a well-behaved system that is stable throughout the operating spectrum. In the process of designing and debugging the controller, operating regions that exhibit oscillatory or unstable operation are avoided. Nonlinearities are typically modeled using a series of discrete equations developed for the system.

A PID or Proportional Integral Differential controller is an example of a closed-loop system. Implemented discretely, it would compute at z intervals:

<I>T(z)</I> = <I>T(z-1)</I> + <I>K</I><SUB>k</SUB> * <I>(E(z)</I> - <I>E(z-1))</I>
   + <I>K</I><SUB>i</SUB> * <I>E(z)</I> + <I>K</I><SUB>d</SUB> * <I>E(z</I>-2<I>)</I> * <I>(1 </I>+<I> E(z-1))</I>
In this linear example, the z sampled periods for the controller represent discrete time samples and Kk, Ki, and Kd are constants. A nonlinear system designer might attempt to modify Kk, Ki, and Kd as a function of input E(z). This of course would require Kk(z), Ki(z), and Kd(z) (and possibly Kk(z-1, z-2, .... z-n) etc). The designer would compute stability and phase margin in a continuous system and convert these to the discrete world using a Discrete Transform.

After the analysis the designer would proceed to code T(z) into assembler or C and port it to a target. This article describes how to implement a simple fuzzy-logic based servo controller in the C programming language. C language portability allows the controller to run in either a micro-controller based environment or on a PC.

Escaping Brittleness

Brittleness is the bane of the control-system designer. A brittle system is one which "breaks" easily. For example, in the coding of T(z), we defined the output T(z) to be an eight-bit unsigned char that follows the eight-bit radix of our A/D converter. Suppose that several of the terms of T(z) were discreetly nine or ten bits. The result could be truncated and thus produce incorrect output when T(z) was cast to an unsigned char. We must check overflow after every operation of T(z) and perform corrective logical operations.

Besides the implementation issues, PID and PI suffer from some serious real-world problems. In controls, non-linearities are the rule. Factors such as friction and temperature affect the behavior of systems. What appears to be stable under one set of conditions yields poor performance under another.

There are techniques for dealing with changes. One possibility is to sliding-mode modify the PI gain parameters. Experience with this approach has shown that it works very well so long as the inputs are bounded properly. Current thinking, however, indicates that the amount of time invested in developing this approach will not be extracted in performance. What most people do to deal with non-linearities is to add a bunch of testing and branching code to deal with the caveats. In most PID, PI closed-loop systems, it ends up that the majority of the code is dedicated to dealing with these anomalies.

Fuzzy Logic

Fuzzy logic holds promise as a means of handling control-system non-linearities. It provides a unique way of looking at control problems. The control problem is described as a rule base, with a rule input matrix µ(T) and a corresponding output function for that rule. When few rules are to be evaluated, simple linguistic rules can be substituted instead.

One might imagine a fuzzy set µ(T) that describes the outside temperature as "very cold," "cold," "warm," and "hot." The four members of µ(T) are linguistic operators that represent the human inferred description of the space representation of each member of µ(T). The membership function is typically a trapezoid that represents a degree of membership. The degree of membership is a real number between 0.0 and 1.0 with a degree of 1.0 meaning full membership and a degree 0.0 indicating no membership. (See Figure 1. )

Suppose the outside temperature is --50 F. Then according to µ(T) the outside temperature will lie in the domain of "very cold." If the temperature is 12 F, the temperature might not be so much "very cold" as it is just "cold." Membership functions almost always overlap in the domain of µ(T). Fuzzy logic provides a means of dealing with overlapping domains and also domains of overlapping output sets. The math allows us to weigh the cumulative effect of all rules to generate a crisp output. A crisp output is also called a de-fuzzified output.

The typical fuzzy plant (controller) consists of one or more input fuzzy sets, a rule base, and an output fuzzy set. The input sets fuzzify using the rule base, and the output set de-fuzzifies from the inputs, rules, and the output de-fuzzifier function.

An Application

This two-input controller is a torque regulator on a DC motor. The controller topology could easily be extended to any two-input, single-output fuzzy controller.

We desire to regulate the torque applied to the shaft of a permanent-magnet DC brush motor. The torque Tmotor applied to the shaft is a linear function of armature current Imotor and (to a lesser extent which will be ignored here) motor temperature. Armature current is controlled by a Pulse Width Modulated (PWM) amplifier which varies the duty cycle DA from 0 to 100 per cent as a linear function of input voltage.

If a positive voltage is applied to the motor terminals, a positive torque will be applied to the motor shaft. If a negative voltage is applied, a negative torque will be applied to the motor shaft. The job of the servo is to regulate the torque on the motor shaft under varying load conditions. The servo applies an adjustment to DA based on the commanded torque input, Tcmd(z), and the feedback torque input:

<I>T</I><SUB>arm</SUB> = <I>I</I><SUB>motor</SUB><I>(z)</I> * <I>K</I><SUB>motor</SUB><I>(z)</I>
sensed on the motor shaft. (See Figure 2. )

The eight-bit PWM applies a positive voltage to the terminals if the PWM value is between 128 and 255. A negative voltage is applied if the value is between 0 and 126. An eight-bit A/D converter and pre-amp sense the feedback torque. The granularity of the feedback torque is determined by the radix of an eight-bit A/D converter. An eight-bit D/A (PWM) assures coherency between conversion radixes on the input. (See Figure 3. )

Fuzzy Controller for Torque

The fuzzy controller is implemented in C on an inexpensive high-integration micro-controller. A simulation program executes on a PC-AT, to assist debugging of the fuzzy servo prior to porting it to the micro-controller.

In this system the fuzzy plant is a two-input, single-output controller. Three fuzzy sets exist with the input sets occupying two dimensions of the rule base and the output set occupying the action to be performed on each rule, which is the combination pointed to by the input fuzzy set membership functions.

The first input fuzzy variable Terror is the error between the setpoint or command torque at time t, and the feedback torque or:

<I>T</I><SUB>error</SUB><I>(z) = T</I><SUB>cmd</SUB><I>(z)- T</I><SUB>arm</SUB><I>(z)</I>
The second input dTerror/dt is the "rate of change of error" or how quickly the feedback torque at time t is changing with respect to the previous sample at time t--1:

<I>dT</I><SUB>err</SUB><I>(z)/dt = T</I><SUB>arm</SUB><I>(z) - T</I><SUB>arm</SUB><I>(z-1)</I>
The output function µ(DA) determines the action to be performed upon evaluation of the rules. For example, if the instantaneous feedback torque is at the setpoint:

<I>T</I><SUB>cmd</SUB><I>(z)</I> ~ <I>T</I><SUB>arm</SUB><I>(z)</I>
but the feedback torque derivative is non-zero:

<I>T</I><SUB>arm</SUB><I>(z)</I> > <I>T</I><SUB>arm</SUB><I>(z-1)</I>
we might wish to apply a slight adjustment to DA so that Terror(z) and dTerr(z)/dt remain zero. The amount of adjustment applied to DA is determined by the inputs and also the output control fuzzy set µ(DA). Which membership function of µ(DA) to apply is determined by the input membership function space and the operator-inferred description of the control problem in the rule-base table. The idea is to maintain zero error and zero error derivative:

<I>dT</I><SUB>err</SUB><I>(z)/dt = T</I><SUB>error</SUB><I>(z) = 0</I>
The number of inputs and the dimension of each determines the rule base matrix. For example, we have two input fuzzy sets, m(T(Verr) and m(dTerr/dt), with five membership functions each. This yields a 5x5 or 25 rule-base controller. The corresponding action to be performed by the evaluation of each input rule determines the output controlling membership function m(DA). For example, rule (3, 3) is:

IF <I>T</I><SUB>error</SUB> == <I>M</I>
   AND <I>dT</I><SUB>err</SUB><I>/dt</I> == <I>-M</I>
     THEN <I>DA</I> == <I>Z</I>
and rule (3, 2) is:

IF <I>T</I><SUB>error</SUB> == <I>M</I>
   AND <I>dT</I><SUB>err</SUB><I>/dt</I> == <I>Z</I>
     THEN <I>DA</I> == <I>-M</I>
A literal evaluation of rule (3,2) would read, "If the feedback torque is less than the setpoint, and the feedback torque is approaching the setpoint at medium rate, then apply zero bias to the PWM." The key to fuzzy logic control is that the degrees of membership incorporate the amount of bias to apply and the degree of truth to each rule. (See Figure 4. )

When a Rule Fires

A rule fires when a nonzero result is returned upon evaluation of the rule. The degree to which the rule is true is incorporated in the membership functions. If the input values lie within the membership function trapezoid space, then the input is valid. A rule fires when both inputs to the rule are non-zero. A rule is evaluated using ternary operators similar in concept to Boolean operators. The operators logically follow the AND, OR, and NOT constructs.

Rules and rule bases can be built using the operators exactly like those in standard Boolean logic, but because membership functions deal with degrees of truth we must have a mechanism for evaluating the operators:

AND == MINIMUM(Input x, Input y)
OR == MAXIMUM(Input x, Input y)
NOT == 1.0 -Input x
The AND operator returns the minimum of the result of a rule that has fired. The OR operator returns the maximum of the result of a rule that has fired. The NOT operator returns the complement of its input x, which is roughly the analog of ~x in C.

In this application the AND operator is used exclusively to operate on the rule table inputs, and the normalized 0.0 -> 1.0 values are actually normalized eight bit values from 0 to 255.

(let dTerr(z)/dt = -11 and Terr(z) = 90)
In evaluating the fired rules (3, 3) and (3, 2) shown above, we first determined if the inputs lie within the domain of µ(Terror) and µ(dTerror/dt) by examining membership functions for µ(Terror) and µ(dTerror/dt). If the rule fires, we take the a cut for each of the input membership functions M and Z. The a cut graphically slices the top of the output controlling membership function. The AND operator returns the lesser of the two a cuts for the inputs µ(dTerror/dt, L) and µ(Terror, M) for use in the slicing operation. Because of the overlapping domain of the membership functions, more than one rule will typically fire. When this occurs, fuzzy logic provides a mathematical method for dealing with multiple rule findings. The output of each rule is an output membership functions that has been a cut and is then graphically ORed with the accumulated preceding output membership functions. The resultant resembels a "shadow" of each fired rule with the accumulated output overlapping each preceding fired rule. Mathematically, the accumulation is the fuzzy OR operator (MAXIMUM) applied to the current rule that fires and the accumulated fired rules. (See figure 5. )

De-fuzzification

The accumulated result of all firing rules occupies a two-dimensional space which is a fuzzy set with one membership function. The resultant must be converted to real world crisp result which takes into account all applied rules. To do this, a spatial average taken which computes the center of area (COA) of the function. COA is among several methods for computing the average. It is the method used in this application because it executes quickly.

<I>COA </I>=<I> (25</I>*<I>18 </I>+<I> 25</I>*<I>31 </I>+<I> 128</I>*<I>100</I> + <I>128</I>*<I>127 </I>+<I> 128</I>*<I>140)</I> <I>/(25 </I>+<I> 25+ 128 + 128 + 128) 
    = 74</I>
So the DA value written to the PWM/D/Ais 74.

C Language Implementation

The membership functions use table look up to follow the example:

µ(<I>dT</I><SUB>error</SUB>/<I>dt</I>) (in oz-in):
-<I>L</I> WHEN <I>dT</I><SUB>err</SUB><I>(z)</I>/<I>dt</I><=-<I>35</I>
-<I>M</I> WHEN -<I>50 </I><= <I>dT</I><SUB>err</SUB>(<I>z</I>)/<I>dt </I>=< <I>0</I>
<I>ZM</I> WHEN -<I>10 </I>> = <I>dT</I><SUB>err</SUB><I>(z)</I>/<I>dt </I><= <I>7</I>
<I>M</I> WHEN <I>0</I> <= <I>dT</I><SUB>err</SUB><I>(z)</I>/<I>dt</I> <= <I>50</I>
<I>L</I> WHEN <I>dT</I><SUB>err</SUB><I>(z)</I>/<I>dt</I> >= <I>10</I>
All 25 rules evaluated and the rules that fire result in a output according to the COA output de-fuzzifier function. The output array stores the result of the rule firing and the history of predicate rules. Conflicts are resolved using the fuzzy OR MAXIMUM) operator. The resultant outputs contribute to a weighted sum to yield the COA output. This strategy allows for the quick computation of a crisp value using the eight-bit multiples.

For a micro-controller application, It was determined by experimentation that a loop repetition rate of at least 1 KHz would be required for satisfactory performance. Command set-points were entered through a serial port and an oscilloscope measured the response of the loop. Figure 6 shows the measured step response, which is quite adequate.

Conclusion

Fuzzy logic control seems to hold promise as an alternative to standard PID control. Rigid modeling is not required because the servo tuning is done via the fuzzy set membership functions and the rules. The rules incorporate Boolean statements and operator-inferred control rather than strict theoretical modeling. The membership functions allow tuning of the control system by altering the space that they occupy, and the placements of the output membership functions provide the control action to be performed by the rule. The rules use Boolean-like operators that infer a control action depending upon the degree of truth of the rule firing. A rule fires if the fuzzified inputs return a non-zero degree of truth, referred to as the a cut for each input.

Fuzzy logic is not as brittle as standard PID control because of the natural language approach to the control problem. Second-order effects such as temperature can be incorporated directly in the rule by logically appending another fuzzy set for temperature onto the rule. Suppose that we know that an increase in temperature causes the torque to change much more rapidly. By creating a rule base that incorporates temperature, we compensate for the change, as in:

IF <I>T</I><SUB>error</SUB> == <I>M</I>
   AND <I>dT</I><SUB>err</SUB>/<I>dt</I> == -M
   AND <I>Temperature</I> == L
     THEN <I>DA</I> = <I>M</I>
In PID control second-order variables such as temperatures are not so easily incorporated into the model.

The fuzzy controller is also less prone to radix overflow and underflow problems. In this application, the controller was implemented using eight-bit LUTs. All mathematical operations were cast to 32-bit long, thus preventing overflow. Because of the averaging nature of COA, it is inherently safe from overflow.

Listing 1: C declaration code for rule table and membership functions


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.