# Designing DSP-based motor control using fuzzy logic

Variable-speed drive (VSD) motors provide hope for greatly reducing energy consumption and reliance on foreign fuels. In one approach, digital signal processors (DSPs) are being used to create a new generation of VSD-based controllers for motors such as brushless direct current (BLDC) motors.

However, these motors present challenges. Controlling motor speed on a BLDC motor is complicated when using traditional proportional, integral, and differential (PID) controllers because they rely on a complex mathematical model and are computationally intensive. An alternative approach is to use fuzzy logic (FL) algorithms to eliminate the need for complex math formulas and provide an easy-to-understand solution. FL motor control also has a shorter development cycle compared to PID controllers, and thus a faster time-to-market. This article discusses the process of using FL algorithms to control BLDC motors using a Texas Instruments c28xx fixed-point family of DSPs.

**BLDC control model development**

Before constructing the FL engine, we must first develop a model to base the design on. FL controllers use heuristic knowledge and express the design using a linguistic description of the model. Rather than develop a model from scratch, we'll use the PID controller model as a starting point. Once developed and implemented, the FL controller is improved by adjusting its parameters.

In general, there are three design steps for developing a FL BLDC controller:

1. Define inputs, outputs, and the controller's range of operation.

2. Define fuzzy membership set functions and rules.

3. Tune the engine.

Figure 1 shows the block diagram of the BLDC controller model.

The first step is to define the relevant inputs and outputs of the model. The inputs are the error (E), which is the current error between the set speed (SS) and the current speed (CS). The other input is the change in error (CE), which is the difference between the current error, and the previously calculated error (PE). The output is the change in armature voltage (CV), which is the difference between the current armature voltage (CAV) and the stored value of the previous armature voltage (PAV). The resulting model equations are as follows:

*E* = *SS* – *CS*

*CE* = *E* – *PE*

*CV* = *CAV* – *PAV*

Motor speed units are in revolutions per minute (RPM), and E determines how close we are to the target speed. So for *E* > 0 motor speed is below set speed. Alternatively, *E* < 0 indicates that the motor is spinning faster than the set speed. CE determines controller direction to adjust. CE is positive if and only if (iff) the current speed is less than the set speed. Alternatively, CE is negative iff the current speed is greater than the set speed. When close to the set speed CE alternates between positive and negative values. CV is the energizing voltage applied to the armature. This voltage is expressed in implementation as a pulse width modulation (PWM) duty cycle.

The next step is to define the fuzzy membership set functions, variables and rules. In order to work, non-fuzzy (crisp) inputs and outputs must be converted into fuzzy ones. Conversion is performed by using linguistic variables to represent input and output ranges. These are also referred to as *fuzzy variables* . Fuzzy variables are used to partition a region of values for membership functions. For example, five variables are used to map the inputs and output. They are negative medium (NM), negative small (NS), zero (Z), positive small (PS), and positive medium (PM). The model's inputs and outputs are membership set functions that are described over the range of operation by the five fuzzy variables.

Instead of math formulas, an FL controller uses fuzzy rules to make a decision and generate an output; how cool is that? FL rules are in the form of IF-THEN statements. Fuzzy rules determine system behavior, rather than complex math equations. For example, IF the error (E) is equal to NM and change in error (CE) is equal to PS, then the change in the armature voltage (CV) is equal to NS. The number of rules used is based on the experience of the designer and the knowledge of the system. Thus, for our system the number of rules used is 25, which is based on our basic PID controller model using the PID's control surface.

In order to energize the armature, the CV fuzzy output must be converted back to a crisp output. This process is called *defuzzification* . A popular method of defuzzification called the *center of gravity* method is used; I'll discuss it in greater detail later.

The last step of design is to adjust the membership functions and rules. This stage is also referred to as *tuning* . Tuning is used to improve the performance of the FL controller. Once designed, the controller is ready to be implemented.

The FL controller implementation is made up of three modules. They are fuzzification, rule-base, and defuzzification. The following sections discuss the modules as related to the FL-BLDC implementation.

**Fuzzification** *Fuzzificaton* is the process of converting crisp value data into fuzzy data. The resulting fuzzy data conversion is based on the degree of fuzzy set membership of input variables. For this application, motor control input variables are rotational error (Error) and change in rotational error (Cerror), which are taken from the PID controller model discussed earlier. *Error* is the absolute error from one sample time to the next. Similarly, *Cerror* is the change in Error from one sample time to the next. The formulas for each are:

Error = SetSpeed – CurrentSpeed

Cerror = Error – PreviousError

As mentioned during the design section, five membership sets are defined for variables Error and Cerror:

1. NM: Negative Medium

2. NS: Negative Small

3. ZE: Zero

4. PS: Positive Small

5. PM: Positive Medium

Figure 2 shows the membership sets for variables Error and Cerror. The membership sets are triangular-shaped and overlap to provide good response. Each set has a maximum value of 0x1AAA. This differs from typical fuzzy logic literature, which sets the maximum range equal to one. Using a maximum value of 0x1AAA for the range reduces computational complexity. Specifically, the multiplying operation is reduced to either a series of additions or subtractions rather than converting to and from a floating point number.

The resulting fuzzification of the input variables produces a vector with five components that correspond to the fuzzy membership sets NM, NS, ZE, PS, PM. The value (y-axis) of each component represents the degree of membership for that crisp input value. The vectors containing the fuzzified values of Error and Cerror are denoted by arrays X1[] and X2[] respectively. For example, with Error equal to 0x30 (x-axis), and Cerror equal to 0x10 (x-axis), the resulting fuzzified data are shown in Table 1.

Figures 3 and 4 graphically show the resulting fuzzified values for Error and Cerror.

**Fuzzy inference rules** *Fuzzy inference rules* operate on the fuzzified data to determine the system's behavior. Specifically, the fuzzified data is applied against the rule table. Linguistically, this is where the input data Error and Cerror are compared with the rule table. The rule table contains membership set components NM, NS, ZE, PS, and PM, depending on the control surface. The output is “inferred” from the valid or “fired” rules. The inference process is described by pseudo code in Listing 1.

Table 2 shows the initial rule base for the motor control surface.

In the earlier example, output vector Y[] is shown in Table 3.

**Defuzzification** *Defuzzification* is the process of converting fuzzy data back into crisp value data. For the purpose of this application the defuzzified value determines the duty cycle of the PWM signal used to drive the motor. The duty cycle is determined by using the modified centroid calculation function. The defuzzification approach used here takes the centroid function and multiplies it by a coefficient. The modified calculation is also known as the *centroid point calculation function* . This approach provides additional precision over the centroid calculation function.

The centroid point calculation is obtained by the center point of the function that is the result of the multiplication of the output membership function by the output vector Y[]. The formula for the centroid point calculation is:

where *Y* [*i* ] are the *i* -th elements of the output vector, and multCoeff[*i* ] are the multiplying coefficients of the output membership function. The index *i* has a range of *i* = 1 to *i* = 5.

Figure 5 shows the graphical representation of the output membership function used by this application with the coefficients of [-0x10, -0x8, 0x0, 0x8, 0x10].

Using the example output vector Y[] = [0x0, 0x13F, 0x196A, 0x0, 0x0], the following defuzzification output value is calculated:

**Defuz = 0 X (-16) + 319 X (-8) + 6506 X (0) + 0 X (8) + 0 X (16) / 0 + 319 + 6506 + 0 + 0 = -2552 / 6825 => -0.37391**

Figure 6 shows the graphical representation of the centroid point calculation for the output vector Y[].

**Hardware description**

The eZdsp2812 board is used in this motor control application. At the heart of the eZdsp board is the TMS320F2812 DSP. The TMS320F2812 is a 150-MHz device that uses timer T1 running at 20kHz for generating PWM1-6 signals, and timer T2 running at 40 kHz for executing interrupt service routines (ISRs). Additionally, the input capture pins CAP1-3 are used to collect speed data from the hall-effect sensors.

Other members of the 28xx family may be substituted for the TMS320F2812. For instance, the eZdsp2808 board may be used if the timers driving the PWM and ISRs are changed. Specifically on the eZdsp2808 board, EPWM1-3 is used for PWM generation, while CPU timer 0 is used for an ISR interrupt source. Similarly, ECAP1-3 capture signals from the hall-effect sensors.

The motor is driven with PWM signals generated by the DSP and translated to a 3-phase output. The six PWM signals are used to source the 3-phase power inverter. The power inverter converts the six signals to a 3-phase signal that directly powers the motor. The 3-phase power inverter function is handled by an auxiliary motor control board. Spectrum Digital provides two boards that provide this function: the DMC550 and the DMC1500. Either board plugs directly into the eZdsp28xx board.

Hall-effect sensors are used for feedback for the fuzzy logic control loop. The commutation instants for the 3-phase power inverter switches are determined by detecting edges from signals received from the hall-effect sensors. The signals are fed into the TMS320F2812's capture pins and are debounced to eliminate noise or false edges from motor oscillations. The actual motor speed is calculated by counting the edge-triggered signals from the Hall-effect sensors via a software module. Figure 7 shows the hardware block diagram for controlling a three phase BLDC motor.

**Software description**

The motor control software is composed of DMC Library modules and the FL motor control routines. Seven of the DMClib modules are used in this application. They are:

• Datalog

• BLDC3PWM

• Hall3_Drv

• Mod6_Cnt

• Rmp2Cntl

• Ramp_Cntl

• Speed_PR

Additionally, the FL motor control is handled by a main **FuzzyCtl()** routine; this is **FuzzyBLDC()** for BLDC motors. When configured, these components demonstrate fuzzy logic control of a variable speed motor.

The software works by first performing configuration, then application-specific setup. Specifically, the GPIO pins are configured to act as PWMs and CAPture pins. Next the timers and module parameters are initialized, as well as ISR setup. After all peripherals are setup, interrupts are enabled, and the main control loop is entered. The main control loop calls the fuzzy controller once every 8.7 ms.

The error values are converted through fuzzification into fuzzy values and stored in X1[], and X2[]. Once converted, the fuzzified values applied to the fuzzy inference rules. The results from the inference module are stored in Y[]. Output from Y[] is then applied to the defuzzification module to convert the fuzzy value back to a crisp value. The resulting crisp value is a PWM offset that is added to the current PWM duty cycle; creating a closed loop system. The updated PWM value is checked to see if the new value is within bounds, and appropriate action is taken if it is not. Finally the fuzzy controller returns the updated PWM duty cycle count to the calling routine.

Figure 8 shows a debug session of the demo application.

Channels 1 &2 (the display window in the upper-right hand corner) displays the PWM counter, and the capture of the hall-effect sensors respectively. Channels 3 & 4 (the display window beneath channels 1 & 2) show the edge-triggered versions of the PWM counter and the hall-effect sensors. The watch window displays important variables. Most important are SetSpeed, and CurrentSpeed. These values are close enough so that the output of the fuzzy controller has a zero value. Also note the other values used in the controller process. This session shows the motor under no-load conditions. The behavior may be slightly different with a load. Moreover, if a finer granularity is desired it may be necessary to tune the controller.

**Byron Miller** is an independent firmware engineer specializing in the design of microprocessors, DSPs, hardware debug, porting, as well as the development of firmware for control, data acquisition, fuzzy logic, and Internet appliances. He has a BA in computer science and a masters in software engineering. You can reach him at .

**Further reading:**

Von Altrock, Constantin. *Fuzzy Logic and NeuroFuzzy Applications Explained* . Englewood Cliffs, NJ: Prentice Hall, 1995.

Miller, Byron. *The Design and Development of Fuzzy Logic Controllers.* Minneapolis, MN: Impatiens Publications, 1997

G. Klir, D. Schwartz, “Fuzzy logic flowers in Japan, *IEEE Spectrum* JULY 1992, p. 32.

Brubaker, David. “Fuzzy-logic basics: intuitive rules replace complex math,” *EDN* June 18, 1992, p. 111.

Brubaker, David. “Fuzzy-logic system solves control problem,” *EDN* June 18, 1992, p. 121.

Brubaker, David. “Design and simulate your own fuzzy setpoint controller,” *EDN* January 5, 1995, p. 167.

V. Donescu, D.O. Ncacsu, G. Griva, “Design of a Fuzzy Logic Speed Controller for Brushless DC Motor Drives,” *IEEE Spectrum* September 1996, p. 404.

M. Chow, Y. Tipsuwan, “Fuzzy Logic Microcontroller Implementation for DC Motor Speed Control,” *IEEE Spectrum* March 1999, p. 1271.

J. Lee, T. Im, H. Sung, Y. Kim, “A Low Cost Speed Control System of Brushless DC Motor Using Fuzzy Logic,” *IEEE Spectrum* April 1999, p. 433.

Miller, Byron. “A Top-Down Approach to Fuzzy Logic Design,” *Embedded Systems Programming* , July 1998, p. 52.

Simon, Dan. “Fuzzy Control,” *ESP* July, 2003, p. 55.

Miller, Byron. “Fuzzy Logic Does Real Time on the DSP,” *Dr. Dobbs Journal* , July 2004, p. 25.

*Fuzzy Logic: * *An Overview of the Latest Control Methodology.* (TI doc – SPRA028).

*Fuzzy Logic Motor Control with MSP430x14x* . (TI doc- SLAA235).

*TMS320F/C28xx Data Manual* . (TI doc – SPRS174M).