AN1307 Stepper Motor Control With dsPIC DSCs DS00001307B

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

AN1307

Stepper Motor Control with dsPIC® DSCs

Author: Sorin Manea and Srisairam Gaarlapati To learn more details about the hardware tool used,
Microchip Technology Inc. refer to the “dsPICDEM™ MCSM Development Board
User’s Guide” (DS70000610).

INTRODUCTION The dsPIC DSC is used to achieve high-speed micro-


stepping in closed-loop current control. For this task,
This application note describes how to drive a stepper voltages higher than the motor rated voltage are
motor using a Microchip Digital Signal Controller needed to force the current quickly through the motor
(dsPIC® DSC). windings. These high voltages require a high PWM
The eight PWM channels are used to control a stepper frequency with a synchronized ADC for fast and
motor in all possible ways, whether it is bipolar or accurate measurement, and control of currents. The
unipolar, using full step or microstepping, open or characteristic features of the dsPIC® DSC fast timers
closed-loop, without the need for external jumpers or and high processing power are necessary since one
complicated logic circuitry. microstep can be as short as one PWM period.

The application firmware is developed for demonstration The dsPICDEM MCSM Development Board was
using the dsPICDEM™ MCSM Development Board designed to work with drive voltages of up to 80V, and
(DM330022-1), which includes the dsPIC33CK64MP105 therefore, accommodate a wide range of stepper motors
External Op Amp Motor Control PIM (Part# MA330050-1), and driving algorithms. Since high voltages are used
along with a stepper motor (Leadshine Part relative to the stepper motor rated voltage, a very fast
Number 42HS03), which is supplied separately from reacting controller is needed. A PWM frequency of
Microchip as Part Number AC300024. The 24-volt power 40 kHz was chosen to have the smallest possible reac-
supply for the board is also supplied separately by tion time. For example, having a stepper motor with
Microchip as Part Number AC002013. As shown in 2.3 ohm and 4 mH per phase, driven at 80V, requires
Figure 1, the hardware consists of the dsPIC® DSC, the 70 µs to reach the current magnitude of 1.4A at 100%
drivers and two H-bridges. Each MOSFET in the dual duty cycle, whereas at 24V, 250 µs are needed to reach
H-bridge is controlled by one PWM signal. The powerful the current magnitude of 1.4A.
PWM module of the dsPIC® DSC features independent
or complementary control over each of the four PWM
pairs, plus an additional override function on each pin,
which gives more flexibility in controlling the power
MOSFETs.

 2009-2019 Microchip Technology Inc. DS00001307B-page 1


AN1307
FIGURE 1: dsPICDEM™ MCSM DEVELOPMENT BOARD BLOCK DIAGRAM
J6
24 VDC
J5
BP1
Regulator Regulator BP2 J7
DC_BUS
3.3V 15V

3.3V PWM1H1
PWM1L1 J8
POT
PWM1H2
PWM1L2
Drivers M1
PWM1H3 M1 M3
S1 PWM1L3 M2
M2 M4
PWM2H1 M
PWM2L1 M3
DC_BUS M4
Plug In Module
(PIM) IMOTOR1

Amplifier

IMOTOR2
J1/J2
Programmer
Amplifier

FAULT
J4
UART
USB to Safe Current
USB Comparator Level

OVERVIEW OF CONTROL Different Decay modes are implemented and the


available Decay modes are:
TOPOLOGIES
• Fixed Decay mode, which is configurable to either
This application note discusses the following control slow or fast decay
methods for stepper motor control:
• Alternating Decay mode, which combines both
• Open Loop – Fixed Voltage slow and fast decay
• Open Loop – Fixed Current These Decay modes can be combined with any control
• Closed-Loop Current Control method and with any Step modes (full, half or
Each of these methods can be operated with a different microstepping).
granularity of voltage steps fed to the motor windings. Decay modes are described in detail in upcoming
The different step-size options available are: sections of this application note.
• Full Step Mode (1/1 Step)
• Half Step Mode (1/2 Step)
• Microstepping:
- 1/4 Step
- 1/8 Step
- 1/16 Step
- 1/32 Step
- 1/64 Step

DS00001307B-page 2  2009-2019 Microchip Technology Inc.


AN1307
FULL STEP, HALF STEP AND voltage play a key role here. Since motor inductance
cannot be reduced, increasing the drive voltage will
MICROSTEP
give a better resolution to smaller microsteps.
In applications where high positional accuracy and low A microstep table, consisting of desired current or
vibrations and noise are needed, the ideal waveform voltage levels, is generated starting from a cosine, as
for driving a stepper motor winding is a sine wave. A shown in Figure 2. The x-axis is divided into evenly
two-phase stepper motor is driven by two sine waves, spaced intervals based on the desired microstep
shifted 90 degrees apart, driving each of the motor size. Considering an application with a resolution of
windings. 1/64 microsteps, there will be 256 points per period.
All Stepping modes are derived from the Sinusoidal However, in the software implementation, one cosine
mode by adjusting the granularity of the driving sine period is divided into 1024 points. This allows the
wave. A full step is the largest step and it consists of microstep resolution to be easily increased, up to 1/256
90 degrees of one sine wave period. A half step rep- if needed. The values of the cosine at each of these
resents half of that and so on. Microstepping is used time intervals is stored in a look-up table that will later
to increase the rotor position resolution and to reduce be used to reconstruct the original cosine at any
vibration and noise in motor operation. With typical desired resolution. The properties of the cosine
motors, a microstepping value of 1/32 is more than function allows us to store only the first quadrant of
enough to achieve the best performance. Going over the function in the look-up table (256 values, one-
this point will not usually bring significant improve- fourth of a period), while the other three quadrants are
ments to positional accuracy, although running noise reconstructed from this first one.
may decrease. The motor inductance and drive

FIGURE 2: MICROSTEP GENERATION

Quarter Step Half Step Full Step Full Step


1.5 Wave Drive Two-Phase ON

0.5

-0.5

-1

-1.5
1+ ¼ ½¼ 1 ¼½¼ 1+ ½ 1 ½ 1+ 1 1+ 1+

The values represented in the microstep table repre- values in the microstep table represent current refer-
sent different things depending on the operating control ences. In both cases, the table is scaled with the max-
mode. If the control mode is Open-Loop Voltage/Open- imum allowed voltage or current, as appropriate.
Loop Current Control, then this table represents the Figure 3 shows the current waveforms for full step
desired voltages to be applied to each winding. If the generation in closed-loop current control.
operating mode is Closed-Loop Current Control, the

FIGURE 3: FULL STEP MODE PHASE CURRENT

 2009-2019 Microchip Technology Inc. DS00001307B-page 3


AN1307
FIGURE 4: MICROSTEPPING WITH 1/4 STEP-SIZE

Users can change microstep granularity in software. Note that when the value of step-size increases, the
Table 1 summarizes steps per electrical cycle for number of microsteps also increases.
various Step modes. When operating in Full Step mode, two options are
available to drive the motor:
TABLE 1: STEP MODES
• Full Step Wave Drive mode: In this mode, only
Step Mode Steps per Electric Cycle one phase is on at any moment in time.
Full Step 4 • Full Step Two-Phase On mode: In this mode, two
Half Step 8 phases are always on, but the polarity changes
every two steps.
1/4 Step 16
Note: Figure 5 shows the current waveforms
1/8 Step 32
during Wave Drive mode and Figure 6
1/16 Step 64 shows the current waveforms during
1/32 Step 128 Two-Phase On mode.
1/64 Step 256

FIGURE 5: CURRENT – FULL STEP WAVE DRIVE MODE

FIGURE 6: CURRENT – FULL STEP TWO-PHASE ON MODE

DS00001307B-page 4  2009-2019 Microchip Technology Inc.


AN1307
OPEN-LOOP CONTROL METHODS Stepper motors are designed to run reliably at the rated
current as instructed by the manufacturer. The rated
There are two open-loop control methods discussed in motor voltage is based on that current and the winding
this application note. One is fixed voltage control, which resistance. However, the voltage across the motor can
is an open-loop control and does not adjust PWM duty be higher than that, as long as the current is kept at all
cycles according to feedback. The second control times at the rated value or lower. As shown in Figure 1,
method is fixed current control. In this method, the duty the motor is connected to two H-bridges powered at
cycle is corrected every four full steps (one sine wave 24V and driven by PWM signals. By carefully choosing
period) in order to reach a desired current amplitude the PWM duty cycle, the appropriate average voltage
set point. Both methods are described in the following for driving the motor at the rated current is generated,
two sections. as shown in Figure 7.

Fixed Voltage Control


In classic voltage control, the rated motor voltage is
applied to the windings. When a higher power supply is
used, such as 24V, the motor rated voltage is achieved
with the use of a chopper, which is implemented with
the Pulse-Width Modulation (PWM) module.

FIGURE 7: MOTOR CURRENT AND PWM

 2009-2019 Microchip Technology Inc. DS00001307B-page 5


AN1307
The fixed voltage control is implemented by generating current, which reduces the generated torque. The
the desired voltage levels with the appropriate PWM speed at which this starts to occur depends on the
duty cycles. If a particular application requires very low motor inductance and applied voltage.
noise operation, open-loop voltage control with As the motor speeds up, the step time gets smaller and
microstepping would be the best choice. the current amplitude reduces until the rotor eventually
Figure 8 shows the practical results of open-loop volt- stalls. To overcome this problem, the solution is to
age control. As shown by the red line in the graph, the increase the drive voltage as the motor speeds up in
magnitude of current varies in proportion to the voltage; order to have a maximum current amplitude equal to
however, since there is no current control, the resulting the rated motor current and extend the maximum
current waveform is distorted. torque versus speed range.
Figure 9 shows the voltage and current for fixed volt-
Fixed Current Control age control. The voltage level is low and the measured
current is rising slowly until the voltage drops. The
When using fixed voltage control, the motor is driven
desired level is far away and the motor torque is low.
with the rated voltage, which allows the current to rise
from zero to the rated current value in a fixed amount
of time. At higher speeds, the current may not rise fast
enough through the motor coil to reach the rated

FIGURE 8: OPEN-LOOP VOLTAGE CONTROL WITH HALF STEP

Note: The voltage reference is scaled to make it visible.

FIGURE 9: FIXED VOLTAGE CONTROL AT 120 RPM WITH ALTERNATE DECAY

Note: The voltage reference is scaled to make it visible.

DS00001307B-page 6  2009-2019 Microchip Technology Inc.


AN1307
Figure 10, on the other hand, shows how the current
amplitude is controlled to a higher value by applying a
higher voltage. Only the current amplitude is controlled
in this mode, not the shape or phase.

FIGURE 10: FIXED CURRENT CONTROL AT 120 RPM WITH ALTERNATE DECAY

Note: The voltage reference is scaled to make it visible.

A simple control loop is used for controlling the current Fast Decay
amplitude. The maximum amplitude of the current in
both motor windings is sampled during one complete In fast decay, when the current is flowing through a
sine wave. If the maximum current amplitude is lower motor’s winding and all MOSFETs are switched off, the
than the desired value, the drive voltage is increased voltage on that winding will be equal to the negative of
gradually by adjusting the PWM duty cycle until the the supply voltage plus the drop voltage on two free-
desired current amplitude is reached. If the current is wheeling diodes, as shown in Figure 11. The decay
too high, the duty cycle is decreased, but not less than rate can be adjusted slightly by shorting one or two
the initial value corresponding to the rated motor diodes in the circuit with their corresponding
voltage. MOSFETs. However, the reverse voltage applied to the
As long as the drive voltage is higher than the motor coil will not change significantly since the voltage drop
rated voltage, this method provides an extended speed across a diode (1V) is much smaller than the supply
range over the classic open-loop (Fixed Voltage Con- voltage (24V). Still, the advantage of using this method
trol) approach. Another advantage to using this algo- is that the decaying current is flowing through the
rithm is that there is no need to retune for different MOSFET body diodes only briefly, until the MOSFET
motors. As long as the starting voltage produces a turns on. The MOSFET has a lower on resistance, and
lower current than desired, the algorithm will increase thus, the dissipated power will be much lower, which
this voltage until the desired current level amplitude is presents an advantage to the overall system power
reached. dissipation.
Another advantage of fast decay is the simplicity of the
DECAY MODES current feedback circuit, since motor current can be
read from the simple shunt resistor at all times. When
When a motor winding is turned off by the PWM, such the winding is driven, the current is positive. While the
as in a chopping circuit, the current through that current is dropping during fast decay, the current will be
winding starts to decay until it reaches zero or until the negative since the voltage is reversed across the wind-
winding is energized again. The rate at which the ing. Therefore, current is available on the shunt resistor
current decays depends on the configuration of the at all times.
H-bridge at that specific moment. The different current
decay methods are referred to as Decay modes in this
document.

 2009-2019 Microchip Technology Inc. DS00001307B-page 7


AN1307
FIGURE 11: FAST DECAY CURRENT FIGURE 13: FAST DECAY (REVERSE)
FLOW CURRENT FLOW
Drive Decay Drive Decay
DC_BUS DC_BUS DC_BUS DC_BUS

Q1A Q2A Q1A Q2A Q1A Q2A Q1A Q2A

Q1B Q2B Q1B Q2B Q1B Q2B Q1B Q2B

FIGURE 12: FAST DECAY PWM TIMING FIGURE 14: FAST DECAY (REVERSE)
DIAGRAM PWM TIMING DIAGRAM

Drive Decay Drive Decay

PWM1H1 PWM1H1

PWM1L1 PWM1L1

PWM1H2 PWM1H2

PWM1L2 PWM1L2

TABLE 2: FAST DECAY DRIVE SIGNALS TABLE 3: FAST DECAY (REVERSE)


MOSFET Driving Signal Value DRIVE SIGNALS

Q1A PWM1H1 PWM MOSFET Driving Signal Value

Q1B PWM1L1 0 Q1A PWM1H1 PWM


Q2B PWM1H2 PWM Q1B PWM1L1 PWM
Q2A PWM1L2 0 Q2B PWM1H2 PWM
Q2A PWM1L2 PWM
With a slight variation on the drive signals, we have
another variant of this Decay mode which is referred to Fast Decay and Reverse Decay modes can be set in
as Reverse Decay mode. Reverse decay behaves like software (refer to Table 4).
fast decay until the current reaches zero, at which point
Fast decay is not recommended as a base decay since
it forces the current in the opposite direction. For short
the current may drop faster during fast decay than it is
decay times though, until the current reaches zero, this
actually rising when the supply voltage is applied to the
is not an issue. If reverse decay is continued after the
winding.
current has dropped to zero, then negative current will
be generated when a positive current is desired and vice
versa. Reverse decay generates the lowest possible
dissipated power in the fast decay configuration.

TABLE 4: FAST DECAY MODES


Name Active Components During Decay
Fast Decay Mode Low-Side Diode + Opposite High-Side Diode
Reverse Decay Mode Low-Side MOSFET + Opposite High-Side MOSFET

DS00001307B-page 8  2009-2019 Microchip Technology Inc.


AN1307
Slow Decay FIGURE 16: SLOW DECAY LOW-SIDE
MOSFET PWM TIMING
Slow decay is entered by shorting the motor winding
DIAGRAM
when it is not driven by the supply voltage. This is
achieved by keeping one of the drive MOSFETs open
Drive Decay
at all times (see the Q1A or Q2B MOSFETs in
Figure 15). The current recirculates through the motor PWM1H1
winding, driving the MOSFET and the opposite
MOSFET or its body diode. If two MOSFETs are on PWM1L1
(lower ones or upper ones), the diodes are shorted,
PWM1H2
allowing less power dissipation and less current drop
during slow decay. PWM1L2

FIGURE 15: SLOW DECAY LOW-SIDE


MOSFET CURRENT FLOW TABLE 5: SLOW DECAY LOW-SIDE
Drive Decay MOSFET DRIVE SIGNALS
DC_BUS DC_BUS
MOSFET Driving Signal Value

Q1A Q2A Q1A Q2A Q1A PWM1H1 PWM


Q1B PWM1L1 PWM
Q2B PWM1H2 1
Q2A PWM1L2 0

Depending on which MOSFET remains on during


Q1B Q2B Q1B Q2B
decay, there are several Slow Decay modes that can
be used. When a bootstrap topology is used for driving
high-side MOSFETs, the Slow Decay mode, called the
Low-Side MOSFET Recirculation mode, is recom-
mended as it helps the bootstrap capacitors to charge.
If the bootstrap capacitors discharge, the upper
MOSFETs cannot be turned on. Appendix B: “Decay
Modes” lists all Slow Decay modes, including the cur-
rent flow path, timing diagrams and drive signals.
Table 6 summarizes all Slow Decay modes.

TABLE 6: SLOW DECAY MODES


Name Active Components During Decay
Low-Side Diode Recirculation Mode Low-Side Diode + Opposite Low-Side MOSFET
High-Side Diode Recirculation Mode High-Side MOSFET + Opposite High-Side Diode
Low-Side MOSFET Recirculation Mode Low-Side MOSFET + Opposite Low-Side MOSFET
High-Side MOSFET Recirculation Mode High-Side MOSFET + Opposite High-Side MOSFET

 2009-2019 Microchip Technology Inc. DS00001307B-page 9


AN1307
Due to the shunt resistor circuit used for current sens- upper signal. The peaks of the bottom signal represent
ing, current measurement is not possible in Slow the shunt resistor current and the peaks match with the
Decay modes. This is because in Slow Decay modes, on time of the PWM. The shunt resistor current is pos-
current does not flow through the shunt resistor since it itive when the winding is driven, which is during the on
recirculates through the motor and MOSFETs or phase of the PWM (Q1A and Q2B switches are on) and
diodes. negative in fast decay. The signal in the middle of the
Figure 17 shows how the current measurement signal plot represents the actual motor current using a current
changes when the Decay mode changes from slow probe. It can be observed that during slow decay (when
(low MOSFET recirculation) to fast. This transition, the top signal is low), the current is zero when the
from slow to fast, happens during the high level of the winding is not driven.

FIGURE 17: CURRENT SIGNALS FOR FAST AND SLOW DECAY MODES

COMBINING DECAY MODES low speeds. As the motor speed increases and the
desired current declines at a faster rate, the winding
In this application note, there are two ways that the current can no longer follow this curve using slow
Decay modes can be used. The first one is Fixed decay. Although operation in fast decay is noisier, it
Decay mode, in which either fast or slow decay is used, allows greater control of the current descent rate.
and this can be selected by the user in application
The two plots in Figure 18 show the difference between
code. The second option is Alternate Decay mode,
Fixed Decay mode, using Slow Decay and Alternate
where two Decay modes are combined while driving
Decay modes, using fast and slow decay. In Alternate
the stepper motor. Table 7 shows two Decay Operation
Decay mode (right plot), fast decay is only used when
modes.
the current is decreasing and only for a limited time
until the current reaches the desired level.
Fixed Decay
The advantage of using the Alternate Decay mode can
As mentioned earlier, in Fixed Decay mode, there is be seen at high speeds, where slow decay cannot pro-
only one Decay mode used during motor operation. vide a fast current drop rate as demanded by the
The recommended Decay mode is Slow Decay mode switching pattern. Also, the BEMF of the motor pre-
in the low MOSFET recirculation configuration. vents the current from decreasing fast enough. Fast
decay can be used to bring the current down faster to
Alternate Decay the desired level. Where fast decay is too aggressive or
needs to be used for a very short time, slow decay with
With all of the available Decay modes, the question diode recirculation can be used for a longer period as it
arises of which one to use and when. Slow decay pro- forces the current to decay faster than in the MOSFET
vides quieter motor operation and is good at relative Recirculation mode.

DS00001307B-page 10  2009-2019 Microchip Technology Inc.


AN1307
For each step, a different current drop is required, so a alternate decay) periods must be proportional to the
smaller or larger ratio of fast to slow decay is needed current amplitude drop. Since the motor back-EMF
based on the step amplitude change. If fast decay is induces current in the windings, it is recommended to
not used long enough, the current decreases too slowly keep the winding in fast decay whenever the desired
and does not follow the desired shape. If it is used for current level is zero. This is an efficient and fast method
too long, the current drops too much and will have to of controlling the current to zero.
rise back up. This is why the number of fast decay (or

TABLE 7: DECAY MODE COMBINATIONS


Decay Modes Description
Fixed Decay Mode Only one Decay mode is used: Base Decay.
Alternate Decay Mode Alternates between two Decay modes: Base Decay and Alternate Decay.

FIGURE 18: COMPARISON BETWEEN FIXED AND ALTERNATE DECAY

 2009-2019 Microchip Technology Inc. DS00001307B-page 11


AN1307
CURRENT MEASUREMENT fast decay, the shunt current will be negative. In all
Slow Decay modes, there is no current flowing through
Current measurement in the full-bridge configuration the shunt resistor.
brings up some challenges. First of all, the measuring
Figure 19 shows a typical shunt resistor waveform
shunt resistor is located between the ground and the
during motor operation in full step wave drive with fast
low-side MOSFETs, which means that no current will
and slow decay. For simplicity, we will assume first that
be visible unless there is a path opened between the
there is no PWM driving the motor and that only DC
DC bus and ground. The path can either be one high-
voltages are applied to the winding.
side MOSFET plus the opposite low-side MOSFET, or
the body diodes of the same MOSFETs when they are The challenge is to reconstruct the real motor current
turned off. based on the available measured data from the shunt
resistor. As DC voltage is replaced by the PWM, the
When the motor winding is energized, the shunt current
step pattern shown in Figure 19 is reproduced on a
will always be positive, regardless of the current direc-
much smaller scale, a number of times inside each of
tion in the motor winding. Whenever the winding is in
those steps, as shown in Figure 20.

FIGURE 19: WINDING CURRENT VERSUS SHUNT CURRENT IN DC MODE

Fast Fast Slow Slow


Drive
Decay Decay Decay Decay

Motor Current Shunt Resistor Current

DS00001307B-page 12  2009-2019 Microchip Technology Inc.


AN1307
In the Closed-Loop Control mode, the PI controller Whenever PWM1H1 is high, the entire supply DC
switches from slow to fast decay often and at small time voltage is applied to the winding and its current
intervals, as shown in Figure 20 and Figure 21. In this increases. The shunt resistor only sees this current
scenario, PWM1H1 and PWM1H2 are driving the wind- when the PWM signal is high. When PWM1L2 is high,
ing current in the positive direction. PWM1L1 and the same DC voltage is applied to the winding, but in
PWM1L2 are driving the winding current in the negative reverse polarity. This puts the winding in fast decay and
direction, but as long as the winding current is positive, forces the shunt resistor current to negative values, but
this is identical to fast decay. The PWM1H1 and equal in amplitude with the real winding current. When
PWM1L2 signals are controlling the high MOSFETs of both of these PWMs are low, their complementary
the H-bridge. Since the slow decay with Low MOSFET PWM pins driving the H-bridge low MOSFETs are high;
Recirculation mode is used, the PWM1L1 and therefore, the winding is in the Slow Decay MOSFET
PWM1H2 signals are complementary to PWM1H1 and Recirculation mode and no current flows through the
PWM1L2, respectively. shunt resistor.

FIGURE 20: SHUNT CURRENT IN CLOSED-LOOP CURRENT CONTROL

FIGURE 21: RECONSTRUCTION OF WINDING CURRENT FROM ADC READINGS

 2009-2019 Microchip Technology Inc. DS00001307B-page 13


AN1307
The ADC reads the shunt current twice every PWM EQUATION 3: CONTINUOUS PI
cycle, once on the active pulse (PWMxHx is high) and CONTROLLER
once on the inactive pulse (PWMxLx is high). By
L/R  s + 1
properly connecting all of the high amplitude peaks, the H PI  s  = K  -------------------------
s
real winding current is reconstructed in software. By
monitoring in which direction the winding is driven, the Legend:
reconstructed current variable is only updated at the s = Laplace Variable
right time. The current value is updated on the active K = Continuous Controller Gain
pulse if the driving direction is positive and on the
inactive pulse when the driving direction is negative. By applying Tustin discretization to Equation 3, the
formula shown in Equation 4 is obtained:
At small duty cycles, the winding current does not have
enough time to energize the shunt resistor, filter capac- EQUATION 4: DISCRETE PI CONTROLLER
itors and amplification circuits, and therefore, it is not
uz p1  z – p 2
read properly by the ADC. The minimum PWM pulse ---------- = H PI  z  = G  -----------------------
width that still allows reliable ADC readings is approxi- z z–1
mately 1.75 µs. At 40 kHz PWM frequency, this results
p 1 = L + R  T/2
in a duty cycle of 7%. All duty cycles below this value
are set to 7%. Current levels that normally require p 2 = L – R  T/2
lower duty cycles in open loop are still achieved by the
T = 25 µs
PI controller by properly controlling the Decay mode.
G = K/R
CLOSED-LOOP PI CURRENT
Legend:
CONTROL  = Current Error
Two Proportional Integral (PI) controllers are used to u = Voltage Output
control the current, one for each of the two motor wind- z = z-Transform Variable
ings. For the best possible results, a theoretical p1, p2 = Discrete Controller Parameters
approach is used that allows easy tuning for any motor T = Controller Sample Time
configuration. As long as the motor parameters are G = Controller Discrete Gain
known, such as resistance, inductance and rated cur-
rent, setting up the system to run with different motors The closed-loop transfer function of the entire system
poses no real challenge in terms of tuning. is shown in Equation 5. As step response rise time of
the first order system is approximately three times the
PI Controller time constant, by choosing the response time control-
ler, gains can be calculated. Controller gains can be
In order to compensate the motor transfer function pole
calculated using Equation 6. For the discrete gain, a
and achieve a zero steady-state error, a classic PI
multiplication factor of 4 is used in order to get more
controller is chosen. See Equation 1 and Equation 2.
resolution from fixed point calculations by avoiding
underflows.
EQUATION 1: MOTOR WINDING
EQUATION
EQUATION 5: CLOSED-LOOP TRANSFER
di FUNCTION
V u  R i  L
dt
Legend:
L = Motor Resistance
R = Motor Inductance
V = DC Voltage EQUATION 6: DISCRETE GAIN USED IN
i = Instantaneous Motor Current THE FIXED POINT
u = PWM Duty Cycle Percentage IMPLEMENTATION
R
EQUATION 2: MOTOR TRANSFER
3  70 s
K V
FUNCTION
G0  4  K R
i( s) V V R
 H M (s)   Legend:
u ( s) L  s  R L R  s 1 G0 = Scaled Discrete Gain

DS00001307B-page 14  2009-2019 Microchip Technology Inc.


AN1307
FIGURE 22: CLOSED-LOOP CONTROLLER BLOCK DIAGRAM

iref  u i
HPI HM
+

In this application, a value of 70 µs was chosen as the equals 75 µs, which is very close to the desired time. In
desired rise time. The step response of the closed-loop all other cases, when the output is limited, the rise time
system is shown in Figure 23. We can only measure the will be longer because higher voltages than the DC bus
correct rise time as set by the PI parameters during the would be needed to achieve the set rise time.
last pulse, where the voltage output is not limited. We
can count six periods, which at 12.5 µs for one period

FIGURE 23: CLOSED-LOOP PI CURRENT CONTROL

 2009-2019 Microchip Technology Inc. DS00001307B-page 15


AN1307
Anti-Windup EQUATION 7: DISCRETE PI CONTROLLER
IMPLEMENTATION
Limiting the controller output leads to a problem called,
accumulator wind-up. The output is saturated, but the acc k + 1 = G 0  p 1  k + 1 – G 0  p 2   k + acc k
PI integrator accumulator keeps counting and grows
until it eventually saturates. When the error is returning u k + 1 = – 24V  acc k + 1  24V
from the saturation area, the accumulator value is Legend:
much higher than normal for that specific error value
acc = Integral Accumulator
and, as a result, the system response slows. To prevent
u = Output Voltage
this effect, the accumulator also has to be compen-
 = Current Error
sated. To do this, another gain is added in Equation 8, p1, p2 = Discrete Controller Parameters
which is called the anti-windup gain. The difference
acck, k = Values from Previous Cycle
between the actual (saturated) output and the accumu-
acck +1, k + 1 = Values from Present Cycle
lator is multiplied with this gain, and then subtracted in
the next accumulator calculation cycle.

EQUATION 8: ANTI-WINDUP PI CONTROLLER

acck + 1 = G 0  P1   k + 1 – G 0  P 2   k + acc k – G w  acc k – u k 


Legend:
Gw = Anti-Windup Gain

Phase Advance Figure 24 and Figure 25 are taken with one-fourth


microstep resolution at a motor speed of 840 RPM.
By changing the value of the anti-windup gain, different With a low anti-windup gain, Figure 24 shows that the
controller behaviors are achieved. For low speeds, it is output voltage and the desired current are in phase.
good to have a small gain so that the current tracks the However, because of the high speed at which the motor
reference as precise as possible. At higher speeds, is running, the winding current is not able to follow the
when the DC bus voltage is not strong enough to bring reference. At some point, the current will be rising too
the current to the reference value and the fast decay late and brake the rotor instead of accelerating it, since
rate is not sufficient to bring the current down in the the stator poles will be energized too late, after the rotor
allocated time frame for one step, the anti-windup gain has passed them.
helps to change the phase of the current, thereby
allowing transition to higher speeds, which otherwise
could not be reached. Keep anti-windup sufficiently
high to prevent motor Stall at higher speeds.

FIGURE 24: 1/4 STEP AT 840 RPM WITH A LOW ANTI-WINDUP GAIN

DS00001307B-page 16  2009-2019 Microchip Technology Inc.


AN1307
FIGURE 25: 1/4 STEP AT 840 RPM WITH A HIGH ANTI-WINDUP GAIN

In Figure 25, the high anti-windup gain forces the The current waveform reference plays an important
controller output voltage to exit saturation sooner, and role here. If it is closer to a sine wave, the current will
therefore, changes the phase of the winding current rel- follow it better and the motor will have better torque. At
ative to the desired current. With this phase advance, high speeds, it is best to use the smallest possible
the current has enough time to rise into the winding microsteps in order to obtain the best motor torque.
before the rotor pole reaches the energized stator pole. However, at high speeds, the microstep changing rate
Further increasing the speed, the current amplitude becomes faster than the output frequency of 40 kHz.
keeps dropping until it eventually changes phase The dsPIC DSC device might also run out of time to
forced by the back-EMF. At this point, the current ampli- execute all of the step changes if they are very fast. For
tude will begin to rise again and the phase advance and these reasons, a value of approximately 20 µs for one
motor back-EMF work together to keep the motor run- microstep is implemented as the lower limit for one
ning, as shown in Figure 26. The motor torque at microstep time, regardless of the microstepping
2400 RPM is strong enough to operate the motor under resolution used. This means the top speed is higher for
a light load. As a comparison, the maximum speed Low-Resolution modes, such as full, half or quarter
achieved in the Open-Loop Control modes with the step, and lower for high-resolution microstepping.
same motor is around 200 RPM.

FIGURE 26: 1/4 STEP WITH PHASE ADVANCE

 2009-2019 Microchip Technology Inc. DS00001307B-page 17


AN1307
POSITION CONTROL deceleration at the right point and a fast stop is
obtained. Compared with a fixed gain solution, this
Open-loop position control is the main reason why controller output exits saturation later.
stepper motors are used. In combination with the
closed-loop current control, open-loop position control
is more accurate and reliable up to considerable CONCLUSION
speeds. To reach high speeds though, the motor has to This application note presented three methods to
be accelerated gradually to prevent the rotor from stall- control a stepper motor: Fixed Voltage mode, Fixed
ing. Stopping the motor must be done in the same way, Current mode and PI Closed-Loop Control mode.
so maximum acceleration and deceleration rates for
the motor speed are used. A method of generating up to 1024 points per cycle was
also discussed in this application note using micro-
A classic Proportional (P) position controller with a vari- stepping. Different decay methods were also presented,
able gain and a maximum speed limit is implemented allowing the controller to operate the stepper in a variety
based on Equation 9. The fixed gain is chosen in such of ways depending on system requirements.
a way that the deceleration rate is slightly smaller than
the maximum allowed deceleration rate. For software implementation, code is available for
download from the Microchip website at:
When the desired position is reached, the motor should www.microchip.com/mcsm.
stop immediately to avoid position oscillations. Since a
fixed deceleration rate is imposed to the motor, the To learn more details about the hardware tool used,
position controller must take it into account and begin refer to the “dsPICDEM™ MCSM Development Board
the deceleration at the right time, before it is too late. A User’s Guide” (DS70000610).
variable gain is used to ensure that the motor starts

FIGURE 27: POSITION CONTROL WITH VARIABLE GAIN – EIGHT MOTOR ROTATIONS ARE
PERFORMED AT QUARTER STEP RESOLUTION

EQUATION 9: PROPORTIONAL (P) POSITION CONTROLLER WITH VARIABLE GAIN AND


MAXIMUM SPEED LIMIT

posFixed_gain  decelerationRate
speed Ref = posGain  pos Err = ---------------------------------------------------------------------------------  pos Err
speedOut
speedRef  speedMax

DS00001307B-page 18  2009-2019 Microchip Technology Inc.


AN1307
APPENDIX A: SOFTWARE FLOW CHARTS

FIGURE A-1: MAIN STATE MACHINE

Start

Peripherals
Initialization

YES Process
Control Mode = NO
State = RUN DMCI
Button Control
Commands

YES
NO

Button NO
Pressed

YES

Step Sizes = NO
Increment Step-Size
1/64 Step

YES

State = RUN Command Wait

YES Process
State = RUN Control Mode = NO
DMCI
Command Wait Button Control
Commands

YES
NO

Button NO
Pressed

YES

State = RUN

YES
State = INIT Initialize Control Algorithm State = RUN Command Wait

NO

 2009-2019 Microchip Technology Inc. DS00001307B-page 19


AN1307
FIGURE A-2: ADC INTERRUPT

ADC Interrupt

NO YES PWM NO
State = RUN Counting
Up

YES

Read Currents Read Currents


Read POT Read POT

Calculate Max YES CurrentControl CurrentControl YES


Current Amplitude Loop == OFF Loop == OFF

Process Alternate
Decay for Both NO NO
Windings

Set PWM Duty Winding1 PI Loop Winding2 PI Loop Set PWM Duty
Cycles Update Decay Mode Update Decay Mode Cycles

Generate ADC Trigger Generate ADC Trigger


to PWM Counting Down to PWM Counting Up

Exit

DS00001307B-page 20  2009-2019 Microchip Technology Inc.


AN1307
FIGURE A-3: TIMER INTERRUPTS

32-Bit Timer
Interrupt
Timer1
Interrupt

Update Timer
Period
Call Speed and
Position Controllers
Increment
Microstep Counter

Clear IRQ Flag

Sinewave YES Reset Microstep


Period Exit
Counter
Complete

Process Fixed
Current Mode

NO

Calculate Next Step


Amplitude Reference

Calculate Position

Clear IRQ Flag

Exit

 2009-2019 Microchip Technology Inc. DS00001307B-page 21


AN1307
APPENDIX B: DECAY MODES

FIGURE B-1: FAST DECAY

Drive Decay Drive Decay


DC_BUS DC_BUS
PWM1H1

Q1A Q2A Q1A Q2A PWM1L1

PWM1H2

PWM1L2

Q1B Q2B Q1B Q2B

MOSFET Driving Signal Value


Q1A PWM1H1 PWM
Q1B PWM1L1 0
Q2B PWM1H2 PWM
Q2A PWM1L2 0

FIGURE B-2: SLOW DECAY LOW DIODE RECIRCULATION

Drive Decay Drive Decay


DC_BUS DC_BUS
PWM1H1

Q1A Q2A Q1A Q2A PWM1L1

PWM1H2

PWM1L2

Q1B Q2B Q1B Q2B

MOSFET Driving Signal Value


Q1A PWM1H1 PWM
Q1B PWM1L1 0
Q2B PWM1H2 1
Q2A PWM1L2 0

DS00001307B-page 22  2009-2019 Microchip Technology Inc.


AN1307
FIGURE B-3: SLOW DECAY HIGH DIODE RECIRCULATION

Drive Decay
DC_BUS
Drive Decay
DC_BUS

PWM1H1
Q1A Q2A Q1A Q2A
PWM1L1

PWM1H2

PWM1L2
Q1B Q2B Q1B Q2B

MOSFET Driving Signal Value


Q1A PWM1H1 1
Q1B PWM1L1 0
Q2B PWM1H2 PWM
Q2A PWM1L2 0

FIGURE B-4: SLOW DECAY LOW MOSFET RECIRCULATION

Drive Decay
DC_BUS DC_BUS Drive Decay

PWM1H1
Q1A Q2A Q1A Q2A
PWM1L1

PWM1H2

PWM1L2
Q1B Q2B Q1B Q2B

MOSFET Driving Signal Value


Q1A PWM1H1 PWM
Q1B PWM1L1 PWM
Q2B PWM1H2 1
Q2A PWM1L2 0

 2009-2019 Microchip Technology Inc. DS00001307B-page 23


AN1307
FIGURE B-5: SLOW DECAY HIGH MOSFET RECIRCULATION

Drive Decay
Drive Decay
DC_BUS DC_BUS

PWM1H1
Q1A Q2A Q1A Q2A
PWM1L1

PWM1H2

PWM1L2
Q1B Q2B Q1B Q2B

MOSFET Driving Signal Value


Q1A PWM1H1 1
Q1B PWM1L1 0
Q2B PWM1H2 PWM
Q2A PWM1L2 PWM

FIGURE B-6: REVERSE DECAY

Drive Decay Drive Decay


DC_BUS DC_BUS
PWM1H1
Q1A Q2A Q1A Q2A
PWM1L1

PWM1H2

PWM1L2

Q1B Q2B Q1B Q2B

MOSFET Driving Signal Value


Q1A PWM1H1 PWM
Q1B PWM1L1 PWM
Q2B PWM1H2 PWM
Q2A PWM1L2 PWM

DS00001307B-page 24  2009-2019 Microchip Technology Inc.


Note the following details of the code protection feature on Microchip devices:
• Microchip products meet the specification contained in their particular Microchip Data Sheet.

• Microchip believes that its family of products is one of the most secure families of its kind on the market today, when used in the
intended manner and under normal conditions.

• There are dishonest and possibly illegal methods used to breach the code protection feature. All of these methods, to our
knowledge, require using the Microchip products in a manner outside the operating specifications contained in Microchip’s Data
Sheets. Most likely, the person doing so is engaged in theft of intellectual property.

• Microchip is willing to work with the customer who is concerned about the integrity of their code.

• Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their code. Code protection does not
mean that we are guaranteeing the product as “unbreakable.”

Code protection is constantly evolving. We at Microchip are committed to continuously improving the code protection features of our
products. Attempts to break Microchip’s code protection feature may be a violation of the Digital Millennium Copyright Act. If such acts
allow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that Act.

Information contained in this publication regarding device Trademarks


applications and the like is provided only for your convenience The Microchip name and logo, the Microchip logo, Adaptec,
and may be superseded by updates. It is your responsibility to AnyRate, AVR, AVR logo, AVR Freaks, BesTime, BitCloud, chipKIT,
ensure that your application meets with your specifications. chipKIT logo, CryptoMemory, CryptoRF, dsPIC, FlashFlex,
flexPWR, HELDO, IGLOO, JukeBlox, KeeLoq, Kleer, LANCheck,
MICROCHIP MAKES NO REPRESENTATIONS OR LinkMD, maXStylus, maXTouch, MediaLB, megaAVR, Microsemi,
WARRANTIES OF ANY KIND WHETHER EXPRESS OR Microsemi logo, MOST, MOST logo, MPLAB, OptoLyzer,
IMPLIED, WRITTEN OR ORAL, STATUTORY OR PackeTime, PIC, picoPower, PICSTART, PIC32 logo, PolarFire,
OTHERWISE, RELATED TO THE INFORMATION, Prochip Designer, QTouch, SAM-BA, SenGenuity, SpyNIC, SST,
INCLUDING BUT NOT LIMITED TO ITS CONDITION, SST Logo, SuperFlash, Symmetricom, SyncServer, Tachyon,
TempTrackr, TimeSource, tinyAVR, UNI/O, Vectron, and XMEGA
QUALITY, PERFORMANCE, MERCHANTABILITY OR are registered trademarks of Microchip Technology Incorporated in
FITNESS FOR PURPOSE. Microchip disclaims all liability the U.S.A. and other countries.
arising from this information and its use. Use of Microchip
devices in life support and/or safety applications is entirely at APT, ClockWorks, The Embedded Control Solutions Company,
the buyer’s risk, and the buyer agrees to defend, indemnify and EtherSynch, FlashTec, Hyper Speed Control, HyperLight Load,
IntelliMOS, Libero, motorBench, mTouch, Powermite 3, Precision
hold harmless Microchip from any and all damages, claims, Edge, ProASIC, ProASIC Plus, ProASIC Plus logo, Quiet-Wire,
suits, or expenses resulting from such use. No licenses are SmartFusion, SyncWorld, Temux, TimeCesium, TimeHub,
conveyed, implicitly or otherwise, under any Microchip TimePictra, TimeProvider, Vite, WinPath, and ZL are registered
intellectual property rights unless otherwise stated. trademarks of Microchip Technology Incorporated in the U.S.A.

Adjacent Key Suppression, AKS, Analog-for-the-Digital Age, Any


Capacitor, AnyIn, AnyOut, BlueSky, BodyCom, CodeGuard,
CryptoAuthentication, CryptoAutomotive, CryptoCompanion,
CryptoController, dsPICDEM, dsPICDEM.net, Dynamic Average
Matching, DAM, ECAN, EtherGREEN, In-Circuit Serial
Programming, ICSP, INICnet, Inter-Chip Connectivity, JitterBlocker,
KleerNet, KleerNet logo, memBrain, Mindi, MiWi, MPASM, MPF,
MPLAB Certified logo, MPLIB, MPLINK, MultiTRAK, NetDetach,
Omniscient Code Generation, PICDEM, PICDEM.net, PICkit,
PICtail, PowerSmart, PureSilicon, QMatrix, REAL ICE, Ripple
Blocker, SAM-ICE, Serial Quad I/O, SMART-I.S., SQI,
SuperSwitcher, SuperSwitcher II, Total Endurance, TSHARC,
USBCheck, VariSense, ViewSpan, WiperLock, Wireless DNA, and
ZENA are trademarks of Microchip Technology Incorporated in the
U.S.A. and other countries.

SQTP is a service mark of Microchip Technology Incorporated in


the U.S.A.
The Adaptec logo, Frequency on Demand, Silicon Storage
Technology, and Symmcom are registered trademarks of Microchip
Technology Inc. in other countries.
GestIC is a registered trademark of Microchip Technology Germany
II GmbH & Co. KG, a subsidiary of Microchip Technology Inc., in
other countries.
All other trademarks mentioned herein are property of their
respective companies.

© 2009-2019, Microchip Technology Incorporated, All Rights


Reserved.
For information regarding Microchip’s Quality Management Systems,
please visit www.microchip.com/quality. ISBN: 978-1-5224-4625-5

 2009-2019 Microchip Technology Inc. DS00001307B-page 25


Worldwide Sales and Service
AMERICAS ASIA/PACIFIC ASIA/PACIFIC EUROPE
Corporate Office Australia - Sydney India - Bangalore Austria - Wels
2355 West Chandler Blvd. Tel: 61-2-9868-6733 Tel: 91-80-3090-4444 Tel: 43-7242-2244-39
Chandler, AZ 85224-6199 China - Beijing India - New Delhi Fax: 43-7242-2244-393
Tel: 480-792-7200 Tel: 86-10-8569-7000 Tel: 91-11-4160-8631 Denmark - Copenhagen
Fax: 480-792-7277 Tel: 45-4450-2828
China - Chengdu India - Pune
Technical Support: Fax: 45-4485-2829
Tel: 86-28-8665-5511 Tel: 91-20-4121-0141
http://www.microchip.com/
China - Chongqing Japan - Osaka Finland - Espoo
support Tel: 358-9-4520-820
Tel: 86-23-8980-9588 Tel: 81-6-6152-7160
Web Address:
www.microchip.com China - Dongguan Japan - Tokyo France - Paris
Tel: 86-769-8702-9880 Tel: 81-3-6880- 3770 Tel: 33-1-69-53-63-20
Atlanta Fax: 33-1-69-30-90-79
Duluth, GA China - Guangzhou Korea - Daegu
Tel: 678-957-9614 Tel: 86-20-8755-8029 Tel: 82-53-744-4301 Germany - Garching
Tel: 49-8931-9700
Fax: 678-957-1455 China - Hangzhou Korea - Seoul
Austin, TX Tel: 86-571-8792-8115 Tel: 82-2-554-7200 Germany - Haan
Tel: 49-2129-3766400
Tel: 512-257-3370 China - Hong Kong SAR Malaysia - Kuala Lumpur
Tel: 852-2943-5100 Tel: 60-3-7651-7906 Germany - Heilbronn
Boston
Tel: 49-7131-72400
Westborough, MA China - Nanjing Malaysia - Penang
Tel: 774-760-0087 Tel: 86-25-8473-2460 Tel: 60-4-227-8870 Germany - Karlsruhe
Fax: 774-760-0088 Tel: 49-721-625370
China - Qingdao Philippines - Manila
Chicago Tel: 86-532-8502-7355 Tel: 63-2-634-9065 Germany - Munich
Itasca, IL Tel: 49-89-627-144-0
China - Shanghai Singapore
Tel: 630-285-0071 Fax: 49-89-627-144-44
Tel: 86-21-3326-8000 Tel: 65-6334-8870
Fax: 630-285-0075 Germany - Rosenheim
China - Shenyang Taiwan - Hsin Chu
Dallas Tel: 86-24-2334-2829 Tel: 886-3-577-8366 Tel: 49-8031-354-560
Addison, TX Israel - Ra’anana
China - Shenzhen Taiwan - Kaohsiung
Tel: 972-818-7423 Tel: 86-755-8864-2200 Tel: 886-7-213-7830
Tel: 972-9-744-7705
Fax: 972-818-2924 Italy - Milan
China - Suzhou Taiwan - Taipei
Detroit Tel: 39-0331-742611
Tel: 86-186-6233-1526 Tel: 886-2-2508-8600
Novi, MI Fax: 39-0331-466781
Tel: 248-848-4000 China - Wuhan Thailand - Bangkok
Tel: 86-27-5980-5300 Tel: 66-2-694-1351 Italy - Padova
Houston, TX Tel: 39-049-7625286
Tel: 281-894-5983 China - Xian Vietnam - Ho Chi Minh
Tel: 86-29-8833-7252 Tel: 84-28-5448-2100 Netherlands - Drunen
Indianapolis Tel: 31-416-690399
Noblesville, IN China - Xiamen Fax: 31-416-690340
Tel: 86-592-2388138
Tel: 317-773-8323 Norway - Trondheim
Fax: 317-773-5453 China - Zhuhai Tel: 47-7288-4388
Tel: 317-536-2380 Tel: 86-756-3210040
Poland - Warsaw
Los Angeles Tel: 48-22-3325737
Mission Viejo, CA
Romania - Bucharest
Tel: 949-462-9523
Tel: 40-21-407-87-50
Fax: 949-462-9608
Tel: 951-273-7800 Spain - Madrid
Tel: 34-91-708-08-90
Raleigh, NC Fax: 34-91-708-08-91
Tel: 919-844-7510
Sweden - Gothenberg
New York, NY Tel: 46-31-704-60-40
Tel: 631-435-6000
Sweden - Stockholm
San Jose, CA Tel: 46-8-5090-4654
Tel: 408-735-9110
Tel: 408-436-4270 UK - Wokingham
Tel: 44-118-921-5800
Canada - Toronto Fax: 44-118-921-5820
Tel: 905-695-1980
Fax: 905-695-2078

DS00001307B-page 26  2009-2019 Microchip Technology Inc.


05/14/19

You might also like