The key question to be answered by the high-level control system for a stepper motor is, when should the next step be taken? While this almost always depends on the application, the similarities between different applications are sufficient to justify the development of fairly complex general purpose stepper motor controllers.

Stepper motor control may be based on open loop or closed loop models. We are primarily interested in open loop models, because this is where stepper motors excel, but we will treat closed loop models briefly because they are somewhat simpler. Figure 1 illustrates an extreme example:

In Figure 1, a quadrature shaft encoder is attached to the drive shaft of a permanent magnet or hybrid stepper motor, and the two phase output of this encoder is used to directly generate the control vector for the motor driver.

Rotary shaft encoders are typically rated in output pulses per channel per revolution; for this example to be useful, for a motor with n steps per revolution, the shaft encoder output must give n / 2 pulses per channel per revolution. If this is the case, the behavior of this system will depend on how the shaft encoder is rotated around the motor shaft relative to the motor.

If the shaft encoder is rotated into a position where the output of the shaft encoder translates to a control vector that holds the motor shaft in its initial position, the motor shaft will not rotate of itself, and if the motor shaft is rotated by force, it will stay wherever it is left. We will refer to this position of the shaft encoder relative to the motor as the neutral position.

If the shaft encoder is rotated one step clockwise (or counterclockwise) from the neutral position, the control vector output by the shaft encoder will pull the rotor clockwise (or counterclockwise). As the rotor turns, the shaft encoder will change the control vector so that the rotor is always trying to maintain a position one step clockwise (or counterclockwise) from where it is at the moment. The torque produced by this method will fall off with rotor speed, but this control system will always produce the maximum torque the motor is able to deliver at any speed.

In effect, with this one-step displacement, we have constructed a brushless DC motor from a stepper motor and a collection of off-the-shelf parts. In practice, this is rarely done, but there are numerous applications of stepper motors in closed-loop control systems that are based on this model, usually with a microprocessor included in the feedback loop between the shaft encoder and the motor controller.

In an open-loop control system, this feedback loop is broken, but at a high level, the basic principle remains quite similar, as illustrated in Figure 2:

In Figure 2, we replace the shaft encoder from Figure 1 with a simulation model of the response of the motor and load to the control vector. At any instant, the actual position of the rotor is unknown! Nonetheless, we can use the simulation model to predict, based on an assumed rotor position and velocity, how the motor will respond to the control vector, and we can construct this model so that its output is the control vector generated by a simulated shaft encoder.

So long as the model is sufficiently accurate, the behavior of the motor controlled by this model will be the same as the behavior of the motor controlled by a closed loop system!

## Model Variables

In the example given in Figure 1, the only control variable offered is the angle of the shaft encoder relative to the motor. In effect, this controls the extent to which the equilibrium point of the motor’s torque versus shaft angle curve leads or follows the current rotor position. In theory, any desired motor behavior can be elicited by adjusting this angle, but it is far more convenient to speak in terms of other variables:

Θ – The predicted shaft position (radians)

Θ_{target} – The target shaft position determined by the application

V = dΘ / dt – The predicted velocity (radians per second)

V_{target} – The target velocity determined by the application

A = dV / dt – The predicted acceleration (radians per second squared)

A_{target} – The target acceleration, may be determined by the application

The basic motor characteristics:

S – step or microstep angle, in radians

μ – moment of inertia of rotor and load

h – the holding torque of the motor

Note that here, the step angle S is not the physical step angle of the motor, but rather, the step angle offered by the mid-level motor interface; this may be a full step, a half step, or a microstep of some size!

## Models

The simplest model that will do the job is almost always the best. For some applications, this means the model is so simple that it is hard to identify it as a model! For example, consider the case where the application demands a constant motor velocity:

A_{target} = 0

V_{target} = Constant in this case, t_{step} = S / V_{target}

Where

t_{step} – time per step

This barely looks like a model; in part, this is because we have omitted the statement that, every t_{step} seconds, we advance the control vector one step:

repeat the following cycle forever:

wait t_{step} seconds and then

Θ = Θ + S

step ( 1 )

A more interesting model is required if we want to maintain a constant acceleration. Obviously, we can’t do this forever, but we’ll use this model as a component in more complex models that require changes of velocity or position. In this case, A_{target} = Constant

Where

A_{target} < h / μ

In developing a model, we begin with the observation that, for constant acceleration A and assuming a standing start at time 0, Θ = 1/2 A t^{2}

More generally, if the motor starts at position Θ and velocity V, after time t the new position Θ’ and velocity V’ will be: Θ’ = 1/^{2} A t^{2} + V t Θ V’ = A t + V

Setting Θ = 0 and Θ’ = S, we solve first for t, the time taken to move one step, as a function of V and A:

1/2 A t^{2} + V t – S = 0

t = (-V ± (V^{2} + 2 A S)^{0.5}) / A

Here, we have applied the quadratic formula, and for our situation, this gives two real roots! The additive root is the root we are concerned with; for this, we can use the resulting time to compute the velocity at the end of one step: t_{step} = (-V + (V^{2} + 2 A S)^{0.5}) / A

V’ = (V^{2} + 2 A S )^{0.5}

We can combine this model for acceleration with the model for constant speed running to make a motor controller that will seek V_{target}, assuming that an outside agent may change V_{target} at any time:

repeat the following cycle forever:

if V = V_{target} do the following:

wait S / V_{target} seconds and then

Θ = Θ + S

step( 1 )

otherwise, if V < V_{target}, accelerate as follows:

wait (-V + (V2 + 2 A_{accel} S)_{0.5} ) / A_{accel} seconds and then

Θ = Θ + S

V = (V^{2} + 2 A_{accel} S)^{0.5}

step ( 1 )

otherwise, V > V_{target}, decelerate as follows:

wait ( -V + (V2 + 2 A_{decel} S)^{0.5} ) / A_{decel} seconds and then

Θ =Θ + S

V = (V2 + 2 A_{decel} S)^{0.5}

step ( 1 )

This control system is not fully satisfactory for a number of reasons! First, it only allows the motor to operate in one direction and it fails utterly when V reaches zero; at that point, if a divide by zero operation is allowed to produce an infinite result, the program will wait infinitely and never again respond to change in the control input.

The second shortcoming of this program is simpler to correct: As written, there is an infinitesimal probability of the motor speed reaching the desired speed and staying there with V_{target} equal to V. Far more likely, what will happen is that V will oscillate around V_{target}, taking alternate accelerating and decelerating steps and never settling down at the desired running speed.

A quick and dirty solution to this latter problem is to add code to recognize when V passes V_{target} during acceleration or deceleration; when this occurs, V can be set to V_{target}. Formally, this is incorrect, but if the acceleration and deceleration rates are not too high and if there is sufficient damping in the system, this will work quite well.

In a frictionless system using sine-cosine microstepping at speeds below the cut of speed for the motor, the available torque is effectively constant and we can use the full torque to accelerate 0r decelerate the motor, so the above control algorithm will work with A_{accel} = A_{decel} = h / μ.

If there is significant static friction, we can take this into account as follows: A_{accel}= (h – f) / μ

A_{decel} = (h + f) / μ

Where

F – frictional torque If the motor is run using the maximum available acceleration and declaration, any unexpected increase in the load will cause the motor rotor to fall behind its predicted position, and the result will be a failure of the control system. As a result, open-loop stepper motor control systems are never run at the accelerations give above! In the case of fill or half-stepping, where there is no sine-cosine torque compensation, the available torque varies over a range of a factor of 2^{0.5}, so we typically adjust the accelerations given above by this amount: Ace=((h / 1.414) – f) / μ

A_{decel} = ((h / 1.414) + f) / μ

If we operate consistently near the edge of the performance envelope, and if we never request a velocity V_{target} near the resonant speed of the motor, we can safely accelerate through resonances without relying on damping. If, on the other hand, we select acceleration values that are significantly below the maximum that is possible, electrical or mechanical damping may be needed to avoid problems with resonance.

Note that it is not difficult to extend the above control model to account, at least approximately, for viscous fiction and for the drop off of torque as a function of speed. To do this, we merely modify the above formulas for A_{accel} and A_{decel} so that h and f are functions of V. Thus, instead of treating these as constants of the control algorithm, we must recomputed the available acceleration at each step.

If our goal is to turn the motor smoothly from one set position to another, we must first accelerate it, then perhaps coast at fixed speed for a while, then decelerate. The decision governing when to begin decelerating rests on a knowledge of the stopping distance from any particular velocity. Assuming that the available acceleration is constant over the relevant range of speeds, we can compute this from: V= A_{decel} t

Θ = 1/2 A_{decel} t^{2}

First we solve for the stopping time, t= V / Adecel and then we solve for the stopping angle Θ = 1/2 A_{decel} ( V / A_{decel})^{2} = V^{2} / ( 2 A_{decel})

Given this, we can outline a procedure for moving the motor from its current estimated position to a step just beyond some target position:

moveto (Θ_{target} )

– a function of one argument O

– no value is returned

while V < V_{target}

and while Θ < Θ_{target} – V^{2} / (2 A_{decel}) repeat the following to accelerate

step ( 1 )

wait (-V + (V^{2} + 2 A_{accel} S)^{0.5}) / A_{accel} seconds and then

Θ =Θ + S

V = (V^{2}+ 2 A_{accel} S)^{0.5}

V = V_{target}

While Θ <Θtarget – V^{2} / ( 2 A_{decel} ) repeat the following to coast

step ( 1 )

wait S / V_{target} seconds and then

Θ = Θ + S

while Θ < Θ_{target} repeat the following to decelerate

step ( 1 )

wait ( -V + ( V^{2} + 2 A_{decel} S )^{0.5} ) / Aaccel seconds and then

Θ = Θ + S

V = (V^{2} + 2 A_{accel} S )^{0.5}

V = 0

done, Θ and Θ_{target} are within a step of each other!

The control model only moves the motor one direction, it fails to plan in terms of the quantization of available stopping positions, and it doesn’t account for the cyclic nature of Θ. Nonetheless, it is a useful illustration. Note that we have used V target as a limiting velocity in this code, but that this will only be relevant during long moves; for short moves, the motor will never reach this speed.

With the above, code, so long as the acceleration and deceleration rates are high enough to avoid dwelling for too long at resonant speeds, and so long as V_{target} is not too close to a resonant speed, a plot of rotor position versus time will show fairly clean moves, as illustrated in Figure 3:

If the motor is to be accelerated at the maximum possible rate, the control model used above is not sufficient. In that case, during acceleration, the equilibrium position must be maintained between 0.5 and 1.5 steps ahead of the rotor position as the rotor moves, and during deceleration, the equilibrium position must be maintained the same distance behind the rotor position. This requires careful logic at the turnaround point, when the change is made from accelerating to decelerating modes. The above control model omits any such considerations, but it is adequate at accelerations sufficiently below the maximum available!