Algorithmcore
Double-S velocity profile
Also called S-curve velocity profile, seven-segment jerk-limited profile.
A point-to-point motion with bounded jerk, acceleration and velocity, made of up to seven segments of constant jerk.
- Solves
- Trajectory generation
- Time
- Θ(n) worst
- Space
- Θ(n) extra
- Where
- n = the number of axes
- Model
- Real RAM
- Properties
- deterministicexactofflinenot randomized
- Evidence
- Proof sketchCode in 1 languages
A double-S velocity profile moves one axis over a distance from rest to rest with the velocity at most , the acceleration at most and the jerk, the rate of change of the acceleration, at most in size. The jerk is piecewise constant, taking the values , and , so the acceleration is piecewise linear and continuous, and the velocity plotted against time has two smooth S-shaped bends, one when speeding up and one when slowing down. This is the trapezoidal velocity profile with each jump of the acceleration replaced by a ramp of slope . Seven segments result: three to accelerate, one to cruise, three to decelerate. Some segments have zero length when a limit is not reached. The profile is the standard motion of servo drives and CNC machines, where feedrate scheduling uses it along the tool path, because the bounded jerk avoids the vibration that the trapezoid causes.
In plain words
The trapezoid profile switches the motor force on and off at once. A machine is a little elastic, so each sudden switch makes it ring like a struck bell. The double S profile turns the force up and down gradually, at a fixed maximum rate. So a move has seven parts: force rising, force held, force falling to zero, cruising, and the same three in reverse to stop. If the move is short, some parts vanish: the force may never reach its maximum, or the speed may never reach its maximum. The price is a little time. When the speed and force limits are both reached, the move is longer than the trapezoid by exactly the time it takes to ramp the force up once.
The problem
Input: a distance and limits . Output: a motion on with , , zero velocity and acceleration at both ends, , and , whose jerk is piecewise constant with values in . The deceleration phase is the mirror image of the acceleration phase. The general form, with nonzero end velocities and different limits for speeding up and slowing down, is in Biagiotti and Melchiorri, Section 3.4; this page covers the rest-to-rest case of their Section 3.4.3.
The algorithm
The acceleration phase has jerk for , jerk for and jerk for . It ends at the peak velocity and covers . The cruise lasts , and .
DOUBLE-S(D, V, A, J) ▷ D, V, A, J > 0- 1
if V·J ≥ A² then Tj ← A/J; Ta ← Tj + V/A ▷ A is reached - 2
else Tj ← √(V/J); Ta ← 2 Tj ▷ A is not reached - 3
if V·Ta ≤ D then ▷ V is reached - 4
Tv ← D/V - Ta; vp ← V else- 5
Tv ← 0 - 6
if D·J² ≥ 2A³ then Tj ← A/J; vp ← (-A²/J + √(A⁴/J² + 4AD))/2; Ta ← vp/A + Tj - 7
else vp ← (D² J / 4)^(1/3); Tj ← √(vp/J); Ta ← 2 Tj - 8
return (Tj, Ta, Tv, vp, 2 Ta + Tv) SAMPLE(pieces, t) ▷ pieces = the seven (duration, jerk) pairs- 9
for each piece (d, j): if t is inside it, integrate (s, v, a) over the rest of t exactly and return - 10
else integrate (s, v, a) over the whole piece: s += v d + a d²/2 + j d³/6, v += a d + j d²/2, a += j d
Lines L1 and L2 build the fastest acceleration phase that reaches : if is large compared with , the acceleration ramps up to , holds it and ramps down; if not, it ramps up and immediately down, and peaks at . Line L3 checks whether the two phases fit into . If they do, line L4 fills the rest with a cruise at . If not, lines L6 and L7 find the peak velocity for which the two phases cover exactly ; line L6 is the case where the acceleration still reaches (a quadratic in ), line L7 the case where it does not (a cubic). Lines L9 and L10 integrate the piecewise constant jerk exactly, so sampling has no integration error.
Example
All limits reached. m, m/s, m/s², m/s³. Line L1: , so s and s. Line L3: , so line L4 gives s and s. The seven pieces last , , , , , , s. The trapezoid with the same and takes s; the difference is s.
Shorter moves. With the same limits:
| (m) | case | (s) | (s) | (m/s) | (s) | trapezoid (s) |
|---|---|---|---|---|---|---|
| 1 | L1, L4 | 0.25 | 0.75 | 1 | 1.75 | 1.5 |
| 0.3 | L1, L6 | 0.25 | 0.53197 | 0.56394 | 1.06394 | 0.77460 |
| 0.2 | L1, L7 | 0.23208 | 0.46416 | 0.43089 | 0.92832 | 0.63246 |
For the acceleration peaks at , and the double S is s slower than the triangle profile, more than .
Low speed limit. , , , . Line L2: , so s and s, and the acceleration peaks at m/s². Line L4 gives s and s, s more than the trapezoid.
Machine tool numbers. The example of feedrate scheduling, mm, mm/s, mm/s², mm/s³, gives s, s, s and s. The code reproduces these values.
Correctness
Theorem: the returned times give a profile that is admissible and covers exactly . The proof first computes one acceleration phase: started at rest, it ends at the velocity and covers half of . It then checks that lines L1 and L2 reach exactly within the limit , that line L4 covers , and, without a cruise, that the distance of two phases is a strictly increasing function of the peak velocity, so the equation has one root. The threshold is the value of where the peak acceleration just reaches , which selects line L6 or line L7. The proof gives every step and shows that sampling is exact.
The rest-to-rest double S is often described as the minimum-time motion under the three limits. This page does not prove that claim.
Proof: The double-S profile covers the distance and respects the velocity, acceleration and jerk limitsfull
We use the notation of the page. One axis moves the distance from rest to rest with , and . Lines L1 to L8 return the jerk time , the acceleration time , the cruise time , the peak velocity and . The seven pieces have jerk and durations . This file proves that the profile is admissible and covers . It does not prove that the profile is the fastest one.
One acceleration phase
Lemma 1. Let and . The phase with jerk for , jerk for and jerk for , started at rest, has:
- acceleration between and ,
- a velocity that does not decrease, from to ,
- the distance , where is the final velocity, and a final acceleration of .
Proof. The acceleration is a trapezoid in time: it rises to , stays there and falls back to 0. This gives 1, the final acceleration 0, and, as it is not negative, the monotone velocity. The final velocity is the area of the acceleration trapezoid, , which is 2. The acceleration is symmetric about , so the velocity satisfies . Then , which gives .
The deceleration phase is the same phase with the opposite signs, run from down to 0, and it covers too. With a cruise of at in between, the whole move covers
it ends at rest with zero acceleration, its jerk is in , its acceleration is at most in size, and its speed is at most . So it remains to show, for each branch, that , , , and , with as in Lemma 1.
The phase that reaches V
Lemma 2. After line L1 or line L2, the phase of Lemma 1 reaches exactly and its acceleration stays within .
Proof. If : and , so , the peak acceleration is , and the final velocity is . If : and , so the peak acceleration is , and the final velocity is .
With a cruise
If (line L3), line L4 sets and . By (1) the move covers . If also , the total time is .
Without a cruise
Otherwise for the times of Lemma 2, and line L5 sets . The move must cover with a peak below . For a peak , let , where if and otherwise. These are the times of Lemma 2 with in place of . Both pieces of are increasing, they agree at where , so is continuous and strictly increasing, and .
Line L6, . Then the solution of has , and solves , that is . Its positive root is the of line L6, and line L6 sets and , so and the peak acceleration is .
Line L7, . Then the solution has and solves , that is , which is line L7. Line L7 sets and , and the peak acceleration is .
In both lines, because is increasing and , and the move covers by (1). In both lines Lemma 1 gives the final velocity , as a direct check shows: for line L6 and for line L7.
Sampling
Lines L9 and L10 integrate the pieces with the exact formulas for constant jerk over a time : , , . These are the Taylor expansions of a cubic, which have no remainder, so the samples are exact in exact arithmetic.
What is not proved here
The profile is the natural extension of the trapezoid. When and are both reached, its time exceeds the time of the trapezoid by exactly . In the other cases the difference can be smaller or larger than . For example, with , , and , the trapezoid is a triangle with time , and the double S needs , which is more, while . This file does not prove that no other jerk-limited motion is faster, so the page does not claim that as a theorem.
Complexity
In the real RAM with square and cube roots, lines L1 to L8 take time and space. Sampling takes per sample, since there are seven pieces. Synchronizing axes to the slowest one, as for the trapezoid, takes plus the cost of stretching each axis to the common time. That stretching has no simple closed form in general; Biagiotti and Melchiorri scale the limits instead.
Properties
- Bounded jerk. The jerk takes only the values , and . The acceleration is continuous, which the trapezoid does not give.
- Extra time over the trapezoid. A theorem: when both and are reached, , exactly more than the trapezoid. In the case with a cruise, the extra time is at most . Without a cruise it can be larger, as the example with shows.
- Seven segments at most. Pieces of zero duration appear when , or both are not reached, so a profile can have three, five or seven pieces of nonzero length.
- Symmetric. The rest-to-rest profile with equal limits for speeding up and slowing down is symmetric in time.
- Exact sampling. The position is a piecewise cubic in time, so exact integration of the pieces gives exact samples.
Variants and improvements
- Nonzero end velocities. Biagiotti and Melchiorri (Section 3.4) handle given start and end velocities, with checks for feasibility.
- Online computation. Starting from an arbitrary state with nonzero acceleration, the minimum-time jerk-limited motion needs many more cases; Kroger and Wahl solve it with decision trees (their Type IV). See online trajectory generation.
- Fifteen segments. A bound on the snap, the derivative of the jerk, gives a profile of up to fifteen pieces (Biagiotti and Melchiorri, Section 3.5).
- Smooth polynomial laws. A quintic or minimum-jerk profile is also smooth, but it reaches its limits only at single instants, so it is slower for the same limits.
- Along a path. Feedrate scheduling applies the double S to the arc length of a tool path, with extra limits from the curvature.
When to use it
Use the double S when the jumps of the acceleration of the trapezoid cause vibration, noise or wear: CNC machining, pick and place at high speed, precise positioning stages, elevators and any servo drive that offers an "S-curve" setting. It costs a few percent more time on long moves. Use the plain trapezoid if the drive is stiff and the cycle time matters most, and online trajectory generation if the target can change during the motion.
Pitfalls
- Missing cases. There are four cases (with or without , with or without cruise), and code that only handles the case where everything is reached gives wrong times for short moves.
- Wrong case test. The test of line L6 uses and only applies without a cruise; deciding from alone is wrong for short moves.
- Cube roots. Line L7 needs a real cube root of a positive number; in floating point, use a function that is accurate there, and check the result by the distance formula, as the tests do.
- Rounding near switch times. Accumulated sums of piece durations differ from by rounding; clamp the final sample to .
- Jerk limit too low. If is very small, the profile spends most of its time ramping the acceleration, and it can be much slower than the trapezoid.
Implementations
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/double-s-velocity-profile/. The authors are listed in prov/07FRKR.jsonl.
"""Double-S velocity profile: rest-to-rest motion of one axis with bounded jerk.
double_s(D, V, A, J) returns (Tj, Ta, Tv, vp, T) for the distance D > 0
with |velocity| <= V, |acceleration| <= A and |jerk| <= J. Tj is the time
of one jerk ramp, Ta the time of the whole acceleration phase, Tv the time
of the cruise, vp the peak velocity and T = 2 Ta + Tv the total time. The
deceleration phase is the mirror image of the acceleration phase.
jerk_segments(...) lists the seven (duration, jerk) pieces, and sample()
integrates them exactly, piece by piece.
"""
import math
def double_s(D, V, A, J):
"""Times of the seven-segment rest-to-rest profile."""
if min(D, V, A, J) <= 0:
raise ValueError("D, V, A and J must be positive")
if V * J >= A * A: # @line L1
Tj = A / J
Ta = Tj + V / A
else:
Tj = math.sqrt(V / J) # @line L2
Ta = 2 * Tj
if V * Ta <= D: # @line L3
Tv = D / V - Ta # @line L4
vp = V
else:
Tv = 0 * D # @line L5
if D * J * J >= 2 * A ** 3: # @line L6
Tj = A / J
vp = (-A * A / J + math.sqrt(A ** 4 / J ** 2 + 4 * A * D)) / 2
Ta = vp / A + Tj
else:
vp = (D * D * J / 4) ** (1 / 3) # @line L7
Tj = math.sqrt(vp / J)
Ta = 2 * Tj
return Tj, Ta, Tv, vp, 2 * Ta + Tv # @line L8
def jerk_segments(Tj, Ta, Tv, J):
"""The seven pieces (duration, jerk); some durations can be zero."""
Tc = Ta - 2 * Tj # time at constant acceleration
return [(Tj, J), (Tc, 0), (Tj, -J), (Tv, 0), (Tj, -J), (Tc, 0), (Tj, J)]
def _step(state, dt, j):
s, v, a = state
return (s + v * dt + a * dt * dt / 2 + j * dt ** 3 / 6, v + a * dt + j * dt * dt / 2, a + j * dt)
def sample(segments, t):
"""Return (s, v, a, j) at time t by exact integration of the pieces."""
state = (0 * t, 0 * t, 0 * t)
t0 = 0 * t
for dur, j in segments: # @line L9
if t <= t0 + dur:
s, v, a = _step(state, t - t0, j)
return s, v, a, j
state = _step(state, dur, j) # @line L10
t0 += dur
s, v, a = state
return s, v, a, 0
def peak_velocity_distance(vp, A, J):
"""Distance of a symmetric rest-to-rest profile with no cruise and peak velocity vp.
Used by the tests as an independent check of the closed forms of line L6
and line L7: the acceleration phase covers vp Ta / 2, so the move covers vp Ta.
"""
if vp * J >= A * A:
Ta = vp / A + A / J
else:
Ta = 2 * math.sqrt(vp / J)
return vp * Ta
test_double_s_velocity_profile.py 98 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/double-s-velocity-profile/. The authors are listed in prov/07FRKR.jsonl.
"""Tests: closed form times, exact end state, limits on a grid, and the peak velocity by bisection."""
import random
from fractions import Fraction as F
from double_s_velocity_profile import double_s, jerk_segments, peak_velocity_distance, sample
def end_state(D, V, A, J):
Tj, Ta, Tv, vp, T = double_s(D, V, A, J)
seg = jerk_segments(Tj, Ta, Tv, J)
return sample(seg, T), seg, (Tj, Ta, Tv, vp, T)
def test_long_move_exact():
# D = 1 m, V = 1 m/s, A = 2 m/s^2, J = 8 m/s^3: every phase is present.
(s, v, a, _), _, (Tj, Ta, Tv, vp, T) = end_state(F(1), F(1), F(2), F(8))
assert (Tj, Ta, Tv, vp, T) == (F(1, 4), F(3, 4), F(1, 4), 1, F(7, 4))
assert (s, v, a) == (1, 0, 0)
assert T == F(1) / 1 + F(1) / 2 + F(2) / 8 # D/V + V/A + A/J
def test_feedrate_example():
# 100 mm at 50 mm/s, 500 mm/s^2 and 10000 mm/s^3 takes 2.15 s.
T = double_s(F(100), F(50), F(500), F(10000))[4]
assert T == F(215, 100)
def test_branches():
# No cruise, A reached: D = 0.3 > 2 A^3 / J^2 = 0.25.
Tj, Ta, Tv, vp, T = double_s(0.3, 1.0, 2.0, 8.0)
assert Tv == 0 and abs(Tj - 0.25) < 1e-12 and vp < 1.0
# No cruise, A not reached: D = 0.2 < 0.25.
Tj, Ta, Tv, vp, T = double_s(0.2, 1.0, 2.0, 8.0)
assert Tv == 0 and abs(Ta - 2 * Tj) < 1e-12 and 8.0 * Tj < 2.0
assert abs(vp - 0.08 ** (1 / 3)) < 1e-12
# V reached but not A: V J < A^2.
Tj, Ta, Tv, vp, T = double_s(1.0, 0.25, 2.0, 8.0)
assert vp == 0.25 and abs(Tj - (0.25 / 8) ** 0.5) < 1e-12 and Tv > 0
def test_peak_velocity_by_bisection():
rng = random.Random(21)
for _ in range(300):
D, V, A, J = rng.uniform(0.01, 2), rng.uniform(0.1, 2), rng.uniform(0.1, 4), rng.uniform(0.5, 50)
Tj, Ta, Tv, vp, T = double_s(D, V, A, J)
if Tv > 0:
assert vp == V and abs(peak_velocity_distance(V, A, J) + V * Tv - D) < 1e-9
continue
lo, hi = 0.0, V
for _ in range(200):
mid = (lo + hi) / 2
if peak_velocity_distance(mid, A, J) < D:
lo = mid
else:
hi = mid
assert abs(vp - lo) < 1e-9
def test_limits_and_end_state():
rng = random.Random(22)
for _ in range(150):
D, V, A, J = rng.uniform(0.01, 2), rng.uniform(0.1, 2), rng.uniform(0.1, 4), rng.uniform(0.5, 50)
(s, v, a, _), seg, (Tj, Ta, Tv, vp, T) = end_state(D, V, A, J)
assert abs(s - D) < 1e-9 and abs(v) < 1e-9 and abs(a) < 1e-9
assert min(d for d, _ in seg) >= -1e-12
prev = 0.0
for k in range(301):
s, v, a, j = sample(seg, T * k / 300)
assert -1e-9 <= v <= V + 1e-9 and abs(a) <= A + 1e-9 and abs(j) <= J
assert s >= prev - 1e-12
prev = s
def test_compared_with_trapezoid():
# The jerk bound never makes the move faster. When V and A are both
# reached, it costs exactly A / J; it costs nothing in the limit J -> infinity.
rng = random.Random(23)
for _ in range(200):
D, V, A, J = rng.uniform(0.01, 2), rng.uniform(0.1, 2), rng.uniform(0.1, 4), rng.uniform(0.5, 50)
Tj, Ta, Tv, vp, T = double_s(D, V, A, J)
T_trap = D / V + V / A if D * A >= V * V else 2 * (D / A) ** 0.5
assert T >= T_trap - 1e-12
if Tv > 0 and V * J >= A * A:
assert abs(T - (T_trap + A / J)) < 1e-12
T = double_s(1.0, 1.0, 2.0, 1e9)[4]
assert abs(T - 1.5) < 1e-6
def test_bad_input():
try:
double_s(1, 1, 0, 1)
except ValueError:
pass
else:
raise AssertionError("A = 0 must fail")
History
Profiles with bounded jerk replaced the trapezoid in motion controllers when machines became faster and lighter and vibration started to limit their accuracy. Biagiotti and Melchiorri (2008) give the double S in a general form, with nonzero end velocities, the rest-to-rest case and an online version. Kroger and Wahl (2010) made the jerk-limited case work from arbitrary current states for several synchronized axes, the basis of online trajectory generation.
References
- Luigi Biagiotti and Claudio Melchiorri, Trajectory Planning for Automatic Machines and Robots, Springer, 2008, Section 3.4 (trajectory with double S velocity profile), in particular Section 3.4.3 for null initial and final velocities and Section 3.4.4 for online computation; Section 3.5 for the fifteen segments variant
- Torsten Kroger and Friedrich M. Wahl, Online trajectory generation, basic concepts for instantaneous reactions to unforeseen events, IEEE Transactions on Robotics 26(1), 2010, 94-111, the whole paper (jerk-limited motions from arbitrary states, Type IV)
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag 07FRKR Edit this page History Report an error Markdown