# 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.

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

• The real world system may have range constraints on the position. There may also be safety considerations around potential energy (i.e. gravitational).
• $v_M$ is bound by the max RPM of the motor. Or more specifically the torque curve. It may also be limited by safety considerations, as it is directly related to the amount of kinetic energy in the system.
• $a_M$ is bound by the torque the motor can employ and the force the system can endure.
• $j_M$ is bound by the jerk the system can withstand.

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.

-> 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 + ε)}$$

or

$$\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$.

## References

https://en.wikipedia.org/wiki/File:Schematic_diagram_of_Jerk,_Acceleration,_and_Speed.svg

• P.J. Siripala, Y. Ahmet Sekercioglu (2013). "A generalised solution for generating stepper motor speed profiles in real time." Mechatronics 23 541-547.

• Josip Divić, Jurica Đurić & Koča Vrančić (2014). "Microcontroller implementation of dynamically adaptable control of stepper motor with continuous second derivative of speed curve." 37th International Convention on Information and Communication Technology, Electronics and Microelectronics (MIPRO). doi 10.1109/MIPRO.2014.6859550

• Ke Yu, Zhisheng Zhang, Zhiting Zhou & Min Dai (2020). "A Modified Bresenham Algorithm for Control System of FDM Three-Dimensional Printer". doi 10.1007/978-3-030-43703-9_11

• "Design of multi-step stepper motor coordinated control system based on Bresenham algorithm." Presented at 24th International Conference on Mechatronics and Machine Vision in Practice, Auckland, New Zealand.

• Study of Real-time Interpolation Algorithm with Varying Interpolation Period Based on Trapezoidal Velocity Profile

https://2π.com