Real Time Stepper Motor Linear Ramping
Just by Addition and Multiplication

Aryeh Eiderman <leib@eiderman.com>



1. Kinematic basics

The linear acceleration (ramping) formulas are:

S  =  v0 . t + a . t2 / 2        [1],

v  =  v0 + a . t        [2]

where

        S  -  acceleration distance, in stepper motor case - number of steps,
        v0  -  initial velocity, base speed (steps per second),
        v  -  target velocity, slew speed (steps per second),
        a  -  acceleration (steps per second per second),
        t  -  acceleration time, ramping period (seconds).

By rearranging [2]

t  =  (v - v0) / a        [3]

and putting it in [1] we have

S  =  (v2 - v02) / (2 . a)        [4]

and

v  =  (v02 + 2 . a . S)1/2        [5]

that can be represented as a recursive form of speed calculation for one step:

vi  =  (vi-12 + 2 . a)1/2        [6]

where

        i  -  step number  (1 < i < S).


2. Control basics

To produce the speed profile for stepper motor we need to provide the real time delays between step pulses:

pi  =  F / vi        [7]

where

        pi  -  delay period for the i-th step (timer ticks),
        F  -  timer frequency (count of timer ticks per second),

so according to [6] the exact delay value will be:

pi  =  F / ((F / pi-1)2 + 2 . a)1/2        [8]

or

pi  =  pi-1 / (1 + pi-12 . 2 . a / F2)1/2        [9].


3. Approximation

Using Taylor series

1 / (1 + n)1/2  ~  1 - n / 2        [10]

when  -1 < n < 1  we can approximate [9] to

pi  =  pi-1 . (1 - pi-12 . a / F2)        [11].

Let's check the  -1 < n < 1  condition. Our  n  was

n  =  pi-12 . 2 . a / F2        [12]

or, by velocity,

n  =  2 . a / vi-12        [13].

The maximum  n  value will be at minimum speed, on the first calculated step, where  i = 2

nmax  =  2 . a / v12        [14].

Because the minimal  v0  is 0, from [6] we have

v1min  =  (2 . a)1/2        [15].

So  n  will be always less than or equal to 1. Because our calculations are forward-only we have no limitation in case of deceleration (negative acceleration) too.


4. Implementation

The given parameters are:

        v0  -  base speed,
        v  -  slew speed,
        a  -  acceleration,
        F  -  timer frequency

and the calculated parameters are:

        S  -  acceleration/deceleration distance

S  =  (v2 - v02) / (2 . a)        [4, 16],

        p1  -  delay period for the initial step

p1  =  F / (v02 + 2 . a)1/2        [17],

        pS  -  delay period for the slew speed steps

pS  =  F / v        [18],

        R  -  constant multiplier

R  =  a / F2        [19].

The variable delay period  p  (initially  p = p1)  that will be recalculated for each next step is:

p  =  p . (1 + m . p . p)        [20].

where

        m  -  variable multiplier that depends on the movement phase:

                m = -R  during acceleration phase,
                m = 0  between acceleration and deceleration phases,
                m = R  during deceleration phase.

For accuracy purpose let's set

        p = pS  if  p < pS  or between acceleration and deceleration phases,
        p = p1  if  p > p1 .


5. Optional enhancement

Using the higher order approximation of Taylor series

1 / (1 + n)1/2  ~  1 - n / 2 + 3 . n2 / 8        [21]

we can get more accurate results replacing [20] with

p  =  p . (1 + q + 1.5 . q . q)        [22]

where

        q  =  m . p . p .

By [22] we have excellent precision but with two extra multiplications and one extra addition vs [20]'s good precision way. Finally let's construct a very good compromise with just one extra multiplication and one extra addition:

p  =  p . (1 + q + q . q)        [23].

However I think that the good way ([20]) is not merely good but even good enough for most of stepper motor applications.
The enhancement is important for servo drivers with the step/direction control that ramping up from and down to zero speed.


6. Programming note

This algorithm was designed for floating point mathematics and in this form it works faster than in the integer form that requires division.




* This algorithm was developed by the author in 1994 for L.I.D. Ltd as a part of POEM Stepper Organizer software to control up to 4 axes through IBM PC's parallel port (LPT) and was ported to microcontroller platform in 2004.
The main field of usage since 1994 - laser diamond cutting.

** Special thanks to David Austin
<dave@slotech.fsnet.co.uk> who helped me with the enhancement part.