Algorithm Search⌘K

Algorithmintro

Quintic polynomial trajectory

Also called fifth-order polynomial time scaling.

Fits a quintic in time to given positions, velocities and accelerations at both ends, so acceleration is continuous.

Time
Θ(n m) worst
Space
Θ(n m) extra
Where
m = the number of segments between via points; n = the number of joints
Properties
deterministicexactofflinenot randomized
Origin
Tamar Flash, Neville Hogan, 1985

A quintic polynomial trajectory moves one coordinate, such as a robot joint angle, from a start state to an end state in a given time TT along a polynomial of degree 5 in time. A state here is a position, a velocity and an acceleration, so there are six end conditions, and a quintic has exactly six coefficients. This is the cubic polynomial trajectory with two more conditions: because the end accelerations can be set, the motion can start and stop with zero acceleration, and the acceleration is continuous when several moves are joined. The jerk, the derivative of the acceleration, is then finite everywhere. The rest-to-rest quintic 10τ3−15τ4+6τ510\tau^3 - 15\tau^4 + 6\tau^5 is also the minimum-jerk trajectory, and in cam design it is the 3-4-5 polynomial law of the cam follower motion laws.

In plain words

A cubic move sets where to start and end and how fast, but the push of the motor still jumps at the start and at the end. That jump shakes a robot arm and the load it carries. A quintic adds two more numbers to the curve, so it can also say how hard to push at both ends. Set those to zero and the motor starts pushing gently, pushes harder, then eases off, and the arm arrives with no push left. Six wishes, six numbers, so again there is exactly one curve and a short formula for it. The price is a higher peak speed than the cubic for the same move and time.

The problem

Input: a start state (q0,v0,c0)(q_0, v_0, c_0) and an end state (q1,v1,c1)(q_1, v_1, c_1) of position, velocity and acceleration, and a duration T>0T > 0. Output: the coefficients a0,…,a5a_0, \dots, a_5 of

q(t)=∑k=05aktk,0≤t≤T,q(t) = \sum_{k=0}^{5} a_k t^k, \qquad 0 \le t \le T,

with q(0)=q0q(0) = q_0, q′(0)=v0q'(0) = v_0, q′′(0)=c0q''(0) = c_0, q(T)=q1q(T) = q_1, q′(T)=v1q'(T) = v_1 and q′′(T)=c1q''(T) = c_1. For an arm with nn joints, each joint gets its own quintic with the same TT. With q0=0q_0 = 0, q1=1q_1 = 1 and all other end values zero, the result is a time scaling s(t)s(t) for a move along a fixed path.

The algorithm

Let h=q1−q0h = q_1 - q_0.

  1. QUINTIC(q0, v0, c0, q1, v1, c1, T) ▷ T > 0, h = q1 - q0
  2. 1a0 ← q0; a1 ← v0; a2 ← c0 / 2
  3. 2a3 ← (20h - (8 v1 + 12 v0) T - (3 c0 - c1) T²) / (2T³)
  4. 3a4 ← (-30h + (14 v1 + 16 v0) T + (3 c0 - 2 c1) T²) / (2T⁴)
  5. 4a5 ← (12h - 6 (v1 + v0) T + (c1 - c0) T²) / (2T⁵)
  6. 5return (a0, ..., a5)
  7. QUINTIC-EVAL(a, t, k)
  8. 6for each derivative order 0, 1, ..., k, evaluate it by Horner's rule
  9. REST-TO-REST(q0, q1, T, t)
  10. 7τ ← t / T; return q0 + (q1 - q0)(10τ³ - 15τ⁴ + 6τ⁵)

Line L1 reads the start state off the polynomial at t=0t = 0. Lines L2 to L4 are the solution of the remaining 3×33 \times 3 linear system for the end state at t=Tt = T, written out in closed form. Line L7 is the special case with zero end velocities and accelerations.

Example

The rest-to-rest profile. With T=1T = 1 and a unit move, lines L2 to L4 give a3=10a_3 = 10, a4=−15a_4 = -15, a5=6a_5 = 6:

s(τ)=10τ3−15τ4+6τ5,s′(τ)=30τ2(1−τ)2,s′′(τ)=60τ(1−τ)(1−2τ).s(\tau) = 10\tau^3 - 15\tau^4 + 6\tau^5, \qquad s'(\tau) = 30\tau^2(1 - \tau)^2, \qquad s''(\tau) = 60\tau(1 - \tau)(1 - 2\tau).

The peak velocity is s′(1/2)=15/8=1.875s'(1/2) = 15/8 = 1.875. The peak acceleration is 10/3≈5.77410/\sqrt 3 \approx 5.774 at τ=(3−3)/6≈0.2113\tau = (3 - \sqrt 3)/6 \approx 0.2113. The jerk is 6060 at both ends. Compare the cubic 3τ2−2τ33\tau^2 - 2\tau^3: its peak velocity is 1.51.5 and its acceleration is 66 at the ends, so the quintic is faster in the middle but starts with zero acceleration.

Nonzero start velocity. Take q0=0q_0 = 0 m, v0=1v_0 = 1 m/s, q1=2q_1 = 2 m, the other end values zero, and T=2T = 2 s, so h=2h = 2.

  • L1: a0=0a_0 = 0, a1=1a_1 = 1, a2=0a_2 = 0.
  • L2: a3=(40−12⋅2)/16=1a_3 = (40 - 12 \cdot 2)/16 = 1.
  • L3: a4=(−60+16⋅2)/32=−7/8a_4 = (-60 + 16 \cdot 2)/32 = -7/8.
  • L4: a5=(24−6⋅2)/64=3/16a_5 = (24 - 6 \cdot 2)/64 = 3/16.

So q(t)=t+t3−78t4+316t5q(t) = t + t^3 - \tfrac78 t^4 + \tfrac{3}{16} t^5. Check: q(2)=2+8−14+6=2q(2) = 2 + 8 - 14 + 6 = 2. The acceleration q′′(t)=6t−212t2+154t3=34t(5t−4)(t−2)q''(t) = 6t - \tfrac{21}{2}t^2 + \tfrac{15}{4}t^3 = \tfrac{3}{4} t (5t - 4)(t - 2) is zero at both ends, as required, and also at t=0.8t = 0.8 s, where the velocity has its peak 1.5121.512 m/s. The cubic with the same positions and velocities has the acceleration 11 m/s² at the start and −2-2 m/s² at the end.

Correctness

Putting t=0t = 0 and t=Tt = T into qq, q′q' and q′′q'' with the coefficients of lines L1 to L4 gives back the six end conditions, which is a direct check. No other polynomial of degree at most 5 meets them: the difference of two would have triple roots at 00 and TT, so t3(t−T)3t^3 (t - T)^3, of degree 6, would divide it, and it would be zero. The proof gives the coefficient check and derives the values of the rest-to-rest example. The same uniqueness argument with the cost ∫q′′′2\int q'''^2 shows that the quintic is the motion of least integrated squared jerk for its six end conditions; that is the theorem of the minimum-jerk trajectory page.

Proof: The quintic meets the six end conditions and is the only quintic that doesfull

We use the notation of the page. The input is the start state (q0,v0,c0)(q_0, v_0, c_0), the end state (q1,v1,c1)(q_1, v_1, c_1) of position, velocity and acceleration, and a duration T>0T > 0. Let h=q1−q0h = q_1 - q_0. Line L1 sets a0=q0a_0 = q_0, a1=v0a_1 = v_0 and a2=c0/2a_2 = c_0 / 2, and lines L2 to L4 set

a3=20h−(8v1+12v0)T−(3c0−c1)T22T3,a4=−30h+(14v1+16v0)T+(3c0−2c1)T22T4,a5=12h−6(v1+v0)T+(c1−c0)T22T5.a_3 = \frac{20h - (8v_1 + 12v_0)T - (3c_0 - c_1)T^2}{2T^3}, \quad a_4 = \frac{-30h + (14v_1 + 16v_0)T + (3c_0 - 2c_1)T^2}{2T^4}, \quad a_5 = \frac{12h - 6(v_1 + v_0)T + (c_1 - c_0)T^2}{2T^5}.

The trajectory is q(t)=∑k=05aktkq(t) = \sum_{k=0}^{5} a_k t^k on [0,T][0, T]. The proof is in exact arithmetic.

The start conditions

q(0)=a0=q0q(0) = a_0 = q_0, q′(0)=a1=v0q'(0) = a_1 = v_0 and q′′(0)=2a2=c0q''(0) = 2a_2 = c_0, directly.

The end conditions

Let u=a3T3u = a_3 T^3, v=a4T4v = a_4 T^4 and w=a5T5w = a_5 T^5. Then

q(T)=q0+v0T+12c0T2+u+v+w,q(T) = q_0 + v_0 T + \tfrac12 c_0 T^2 + u + v + w,
Tq′(T)=v0T+c0T2+3u+4v+5w,T q'(T) = v_0 T + c_0 T^2 + 3u + 4v + 5w,
T2q′′(T)=c0T2+6u+12v+20w.T^2 q''(T) = c_0 T^2 + 6u + 12v + 20w.

Each of u,v,wu, v, w is half of a combination of hh, v1Tv_1 T, v0Tv_0 T, c0T2c_0 T^2 and c1T2c_1 T^2. The table gives the coefficients of these five terms in 2u2u, 2v2v, 2w2w and in the three sums.

hh v1Tv_1 T v0Tv_0 T c0T2c_0 T^2 c1T2c_1 T^2
2u2u 20 −8-8 −12-12 −3-3 1
2v2v −30-30 14 16 3 −2-2
2w2w 12 −6-6 −6-6 −1-1 1
2(u+v+w)2(u + v + w) 2 0 −2-2 −1-1 0
2(3u+4v+5w)2(3u + 4v + 5w) 0 2 −2-2 −2-2 0
2(6u+12v+20w)2(6u + 12v + 20w) 0 0 0 −2-2 2

So u+v+w=h−v0T−12c0T2u + v + w = h - v_0 T - \frac12 c_0 T^2, 3u+4v+5w=v1T−v0T−c0T23u + 4v + 5w = v_1 T - v_0 T - c_0 T^2, and 6u+12v+20w=c1T2−c0T26u + 12v + 20w = c_1 T^2 - c_0 T^2. Putting these into the three lines above gives q(T)=q0+h=q1q(T) = q_0 + h = q_1, Tq′(T)=v1TT q'(T) = v_1 T and T2q′′(T)=c1T2T^2 q''(T) = c_1 T^2. Since T>0T > 0, q′(T)=v1q'(T) = v_1 and q′′(T)=c1q''(T) = c_1. □\square

Uniqueness

Let pp be the difference of two polynomials of degree at most 5 that meet the six conditions. Then pp, p′p' and p′′p'' vanish at 00 and at TT, so 00 and TT are roots of multiplicity at least 3, and t3(t−T)3t^3 (t - T)^3 divides pp. That product has degree 6, so p=0p = 0. So the 6×66 \times 6 linear system of the six conditions is nonsingular for T>0T > 0, and the formulas above are its solution. □\square

The rest-to-rest profile

With h=1h = 1, T=1T = 1 and zero end velocities and accelerations, the formulas give a3=10a_3 = 10, a4=−15a_4 = -15, a5=6a_5 = 6, so s(τ)=10τ3−15τ4+6τ5s(\tau) = 10\tau^3 - 15\tau^4 + 6\tau^5, which is line L7. Its derivatives are

s′(τ)=30τ2(1−τ)2,s′′(τ)=60τ(1−τ)(1−2τ),s′′′(τ)=60(1−6τ+6τ2).s'(\tau) = 30\tau^2(1 - \tau)^2, \quad s''(\tau) = 60\tau(1 - \tau)(1 - 2\tau), \quad s'''(\tau) = 60(1 - 6\tau + 6\tau^2).

The velocity s′s' is not negative on [0,1][0, 1], so ss rises from 0 to 1 without overshoot. Its largest value is at τ=1/2\tau = 1/2, where s′=30/16=15/8s' = 30/16 = 15/8. The acceleration s′′s'' has its extremes where s′′′=0s''' = 0, at τ=(3∓3)/6\tau = (3 \mp \sqrt3)/6. There τ(1−τ)=1/6\tau(1 - \tau) = 1/6 and 1−2τ=±3/31 - 2\tau = \pm\sqrt3/3, so ∣s′′∣=60⋅16⋅33=10/3|s''| = 60 \cdot \frac16 \cdot \frac{\sqrt3}{3} = 10/\sqrt3. The jerk at both ends is s′′′(0)=s′′′(1)=60s'''(0) = s'''(1) = 60. For a move of length DD in time TT the values scale by D/TD/T, D/T2D/T^2 and D/T3D/T^3.

Complexity

In the real RAM, one call of QUINTIC costs a fixed number of operations, about 40, so Θ(1)\Theta(1) time and space. Evaluating all derivatives up to order 3 costs about 20 multiplications. An arm with nn joints and mm segments needs Θ(nm)\Theta(nm) time and stores 6nm6nm coefficients.

Properties

  • Acceleration continuous. Joined quintics are C2C^2 at via points when each via point gets one velocity and one acceleration. The jerk is finite but jumps, from 0 to 60 D/T3D/T^3 at the start of a rest-to-rest move.
  • Minimum integrated squared jerk. A theorem (Flash and Hogan 1985): for its six end conditions, the quintic is the unique minimizer of ∫q′′′2\int q'''^2.
  • Peaks of the rest-to-rest move. For a distance DD in time TT: velocity 1.875 D/T1.875\, D/T, acceleration 5.774 D/T25.774\, D/T^2, jerk 60 D/T360\, D/T^3. These are exact values (theorems), from the example.
  • Linear in the data. For a fixed TT, the coefficients are linear in the six end values.
  • Overshoot. Large end velocities or accelerations compared with h/Th/T make the motion leave the interval between q0q_0 and q1q_1.

Variants and improvements

  • Higher degree. A polynomial of degree 7 also sets the end jerks; it is the single-segment minimum-snap trajectory. Each step up raises the peak velocity: 1.51.5, 1.8751.875 and 35/16=2.187535/16 = 2.1875 times D/TD/T for degrees 3, 5 and 7.
  • Via points. One quintic per segment with given velocities and accelerations at the via points. Choosing them to minimize a cost over all segments gives smoothing splines such as the minimum-snap method.
  • Jerk-limited profiles. If the move must be as fast as possible under limits on the velocity, the acceleration and the jerk, the double-S velocity profile is faster, because it can spend most of the time at the limits.
  • Choosing T. For a rest-to-rest move under ∣q′∣≤V|q'| \le V and ∣q′′∣≤A|q''| \le A, the smallest feasible time is T=max⁡(1.875 D/V,5.774 D/A)T = \max(1.875\, D/V, \sqrt{5.774\, D/A}).

When to use it

Use a quintic for point-to-point moves of a joint or as the time scaling of a path when the acceleration must be continuous, for example with an elastic arm, a heavy load, or liquid in a container. It is also the standard model of a human-like reaching motion, and a common choice for a short motion primitive that must join another motion at a given state. If the motion must be as fast as possible under fixed limits, use a trapezoidal or a double-S profile instead; a quintic reaches its limits only at one instant.

Pitfalls

  • Same T for all joints. If each joint gets its own duration, the joints do not arrive together, and the path is not the intended one.
  • Check the limits after choosing T. The formulas accept any T>0T > 0; the peaks grow as 1/T1/T, 1/T21/T^2 and 1/T31/T^3.
  • Typing the closed form. The coefficients of lines L2 to L4 are easy to mistype. Test them with exact fractions against the six end conditions, as the tests of this page do.
  • Local time. Each segment uses its own local time t−tit - t_i; evaluate it there.
  • Floating point for small T. The divisions by T5T^5 amplify rounding when TT is small and the end values are large. Scale time to τ=t/T\tau = t/T in that case.

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.

quintic_polynomial_trajectory.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/quintic-polynomial-trajectory/. The authors are listed in prov/JZFXGC.jsonl.
"""Quintic polynomial trajectory of one joint.

quintic_coeffs(q0, v0, c0, q1, v1, c1, T) returns (a0, ..., a5) of
q(t) = a0 + a1 t + ... + a5 t^5 on [0, T], with position q, velocity v
and acceleration c given at t = 0 and t = T. The arithmetic is exact
when the inputs are fractions.Fraction values.
"""


def quintic_coeffs(q0, v0, c0, q1, v1, c1, T):
    """Coefficients of the quintic that meets the six end conditions."""
    if T <= 0:
        raise ValueError("T must be positive")
    h = q1 - q0
    a0 = q0  # @line L1
    a1 = v0
    a2 = c0 / 2
    a3 = (20 * h - (8 * v1 + 12 * v0) * T - (3 * c0 - c1) * T ** 2) / (2 * T ** 3)  # @line L2
    a4 = (-30 * h + (14 * v1 + 16 * v0) * T + (3 * c0 - 2 * c1) * T ** 2) / (2 * T ** 4)  # @line L3
    a5 = (12 * h - 6 * (v1 + v0) * T + (c1 - c0) * T ** 2) / (2 * T ** 5)  # @line L4
    return (a0, a1, a2, a3, a4, a5)  # @line L5


def quintic_eval(a, t, order=3):
    """Return the list [q, q', q'', ...] of the first `order` + 1 derivatives at t."""
    out = []
    coeffs = list(a)
    for _ in range(order + 1):  # @line L6
        val = 0
        for c in reversed(coeffs):  # Horner's rule
            val = val * t + c
        out.append(val)
        coeffs = [i * coeffs[i] for i in range(1, len(coeffs))] or [0]
    return out


def rest_to_rest(q0, q1, T, t):
    """The quintic with zero end velocities and accelerations: q0 + (q1 - q0) s(t / T)."""
    tau = t / T
    s = tau ** 3 * (10 - 15 * tau + 6 * tau ** 2)  # @line L7
    return q0 + (q1 - q0) * s
test_quintic_polynomial_trajectory.py 97 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/quintic-polynomial-trajectory/. The authors are listed in prov/JZFXGC.jsonl.
"""Tests: exact end conditions, a second method (linear solve), and the check values."""

import math
import random
from fractions import Fraction as F

from quintic_polynomial_trajectory import quintic_coeffs, quintic_eval, rest_to_rest


def solve(m, b):
    """Gauss-Jordan elimination in exact arithmetic (the second method)."""
    n = len(m)
    a = [[F(x) for x in m[i]] + [F(b[i])] for i in range(n)]
    for c in range(n):
        p = next(r for r in range(c, n) if a[r][c] != 0)
        a[c], a[p] = a[p], a[c]
        for r in range(n):
            if r != c and a[r][c] != 0:
                f = a[r][c] / a[c][c]
                a[r] = [x - f * y for x, y in zip(a[r], a[c])]
    return [a[i][n] / a[i][i] for i in range(n)]


def row(t, d):
    """Row of the d-th derivative of (1, t, ..., t^5) at t."""
    out = []
    for k in range(6):
        if k < d:
            out.append(F(0))
        else:
            out.append(F(math.perm(k, d)) * F(t) ** (k - d))
    return out


def rand_frac(rng):
    return F(rng.randint(-30, 30), rng.randint(1, 7))


def test_end_conditions_exact():
    rng = random.Random(11)
    for _ in range(200):
        q0, v0, c0, q1, v1, c1 = (rand_frac(rng) for _ in range(6))
        T = F(rng.randint(1, 30), rng.randint(1, 7))
        a = quintic_coeffs(q0, v0, c0, q1, v1, c1, T)
        assert quintic_eval(a, F(0), 2) == [q0, v0, c0]
        assert quintic_eval(a, T, 2) == [q1, v1, c1]


def test_against_linear_solve():
    rng = random.Random(12)
    for _ in range(60):
        vals = [rand_frac(rng) for _ in range(6)]
        T = F(rng.randint(1, 9), rng.randint(1, 3))
        m = [row(0, 0), row(0, 1), row(0, 2), row(T, 0), row(T, 1), row(T, 2)]
        assert list(quintic_coeffs(*vals, T)) == solve(m, vals)


def test_check_values():
    a = quintic_coeffs(F(0), F(0), F(0), F(1), F(0), F(0), F(1))
    assert a == (0, 0, 0, 10, -15, 6)
    assert quintic_eval(a, F(1, 2), 1)[1] == F(15, 8)
    assert quintic_eval(a, F(0), 3)[3] == 60
    assert quintic_eval(a, F(1), 3)[3] == 60
    t_star = (3 - math.sqrt(3)) / 6
    af = tuple(float(x) for x in a)
    assert abs(t_star - 0.2113) < 1e-4
    assert abs(quintic_eval(af, t_star, 2)[2] - 10 / math.sqrt(3)) < 1e-12
    assert abs(10 / math.sqrt(3) - 5.7735) < 1e-4
    grid = [quintic_eval(af, k / 10000, 2) for k in range(10001)]
    assert max(g[1] for g in grid) <= 1.875 + 1e-12
    assert max(g[2] for g in grid) <= 10 / math.sqrt(3) + 1e-12
    assert min(g[2] for g in grid) >= -10 / math.sqrt(3) - 1e-12


def test_rest_to_rest_matches():
    q0, q1, T = F(2), F(-3), F(5, 2)
    a = quintic_coeffs(q0, F(0), F(0), q1, F(0), F(0), T)
    for k in range(11):
        t = T * k / 10
        assert quintic_eval(a, t, 0)[0] == rest_to_rest(q0, q1, T, t)


def test_float_inputs():
    a = quintic_coeffs(0.5, 1.0, -2.0, 1.5, 0.0, 0.0, 1.5)
    got = quintic_eval(a, 1.5, 2)
    assert max(abs(x - y) for x, y in zip(got, [1.5, 0.0, 0.0])) < 1e-12


def test_bad_input():
    try:
        quintic_coeffs(0, 0, 0, 1, 0, 0, 0)
    except ValueError:
        pass
    else:
        raise AssertionError("T = 0 must fail")

History

Polynomials in time were the standard way to plan joint trajectories in the first robot controllers, and the quintic was used where the cubic gave unacceptable acceleration jumps; its first use is not traced here. Flash and Hogan (1985) proposed that human arm movements minimize the integrated squared jerk, showed that for a rest-to-rest move the optimum is the quintic 10τ3−15τ4+6τ510\tau^3 - 15\tau^4 + 6\tau^5, and compared it with measured planar arm movements. Lynch and Park (2017) present the quintic as the fifth-order polynomial time scaling.

References

  1. Kevin M. Lynch and Frank C. Park, Modern Robotics, Mechanics, Planning, and Control, Cambridge University Press, 2017, Chapter 9, Section 9.2.2 (time scaling a straight-line path)
  2. Luigi Biagiotti and Claudio Melchiorri, Trajectory Planning for Automatic Machines and Robots, Springer, 2008, Chapter 2 (analytic expressions of elementary trajectories, polynomial trajectories)
  3. Tamar Flash and Neville Hogan, The coordination of arm movements, an experimentally confirmed mathematical model, Journal of Neuroscience 5(7), 1985, 1688-1703, the whole paper (the minimum-jerk model and its fifth-order polynomial solution)

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.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.worst
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a scene preset for the example