Realtime stepper motor control

Abstract. Ideal stepper motor control requires generating step and direction signals for jerk-limited trajectories. For embedded realtime systems an efficient method is required to generate such signals.

Admissible trajectories

A motor trajectory is a position over time $x(t)$. For simplicity assume time $t$ is in seconds and position $x$ is in steps. Of relevance are its first three derivatives, velocity $v(t)$, acceleration $a(t)$ and jerk $j(t)$:

$$ \begin{aligned} v(t) &= \frac{\d}{\d t} x(t) & a(t) &= \frac{\d}{\d t} v(t) & j(t) &= \frac{\d}{\d t} a(t) & \end{aligned} $$

The jerk function can have discontinuous jumps, so the position function is of differentiability class $C^2$.

These constraints have causes

Note. These being constants is just an approximation, in practice the maximum torque depends on the motor RPM. The required torque may be a function of the position, etc. Handling the cases with dynamic limits is left for future work.

For practical reasons, there are limits to the range of all trajectory functions.

$$ \begin{aligned} \vert v(0) \vert &≤ v_M & \vert a(0) \vert &≤ a_M & \vert j(0) \vert &≤ j_M & \end{aligned} $$

Consequentially, $x$, $v$ and $a$ are Lipschitz continuous with constants $K = v_M$, $a_M$ and $j_M$ respectively.

-> Admissible positions

-> Example showing the importance of velocity (and more subtly also acceleration).

  1. Constant $x$.
  2. Constant $v$.
  3. Constant $a$.
  4. Constant $j$.

Maximum jerk trajectories

The fastest admissible trajectory from a state $(x_0, v_0, a_0)$ to $(x_1, v_1, a_1)$ is an s-curve and has maximum jerk:

$$ \begin{aligned} x''(t) & ∈ \{-1, 0, 1\} ⋅ M_{\text{jerk}} \end{aligned} $$

When the initial and final velocity and acceleration are zero, this results in the seven step S-curve.

Step and direction signals

Stepper motors operate in discrete position steps, in our units normalized to $1$. The control signal $\hat x(t)$ will thus have discrete steps $\hat x ∈ \Z$. The ideal control signal minimizes the discrepancy $\abs{\hat x(t) - x(t)}$, and is accomplished simply by rounding the position to the nearest step:

$$ \hat x(t) = \floor{ x(t) + \frac 12 } $$

This results in a step function that can be written as a sum of discrete step intervals

$$ \hat x(t) = \sum_i x_i 1_{[t_i, t_{i+1})}(t) $$

where $1_A$ is the indicator function, $x_i ∈ \Z$ is the position during step $i$ and $t_i$ is the starting time of step $i$. We can take the (distributional) derivative of $\hat x(t)$ to get a $\hat v(t)$, which consists of a sequence of positive or negative unit impulses, also known as Dirac delta functions:

$$ \hat v(t) = \frac{\d}{\d t} \hat x(t) = \sum_i s_i ⋅δ(t - t_i) $$

where $s_i ∈ \{-1, 1\} = x_{i+1} - x_i$ is the direction of the pulse.

The stepper motor controller takes a step and direction signal, which are the the absolute value $\abs{\hat v(t)}$ and sign $\sign \hat v(t)$, convolved with a small pulse $1_{[0, ε]}$ to make it physically realizable.

$$ \mathrm{step} = \abs{\hat v} * 1_{[0, ε]} $$

Interestingly, the solution to this is

$$ \mathrm{step}(t) = \abs{\hat x(t) - \hat x(t + ε)} $$


$$ \mathrm{step}(t) = \abs{\hat x(t) - \hat x(t - ε)} $$

To practically generate this signal all we need are the $t_i$ and $s_i$.

Step duration


Remco Bloemen
Math & Engineering