Skip to content

Commit 80f2861

Browse files
committed
feat(motion_profiles): add module with polynomial point-to-point profile
Deploy roadmap item 55 and create the motion_profiles module with the shared TrajectoryState / MotionLimits types. PolynomialProfile solves the closed-form cubic (position + velocity) or quintic (+ acceleration) coefficients once and samples position, velocity, acceleration and jerk by Horner evaluation, with time clamped to [0, tf]. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q53m7MZDmHwocmKbhhd2o5
1 parent 8d312d0 commit 80f2861

16 files changed

Lines changed: 405 additions & 218 deletions

File tree

‎AGENTS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ resource-constrained embedded systems. Real-time, deterministic, no heap.
5353
`analysis`, `windowing`, `control_analysis`, `controllers`, `estimators`,
5454
`filters` (active Kalman family — **not** `filters::active`), `filters::passive`, `math`,
5555
`optimization`, `regularization`, `solvers`, and new:
56-
`robust_control`, `nonlinear_control`.
56+
`robust_control`, `nonlinear_control`, `motion_profiles`.
5757

5858
## Math functions
5959

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Refer to the documentation to quickly integrate and utilize the library's signal
2424
| [Optimization](doc/optimization/README.md) | Gradient Descent, SGD / Adam Step Optimisers |
2525
| [Regularization](doc/regularization/README.md) | L1 (Lasso), L2 (Ridge) |
2626
| [Math](doc/math/README.md) | CORDIC, Quaternion, MatrixNorms, Step Response Metrics, MatrixExponential, Angle Utilities, Tridiagonal (Thomas) Solver |
27+
| [Motion Profiles](doc/motion_profiles/README.md) | Polynomial (Cubic/Quintic) Point-to-Point Profile |
2728
| [Solvers](doc/solvers/README.md) | Gaussian Elimination, Levinson-Durbin, Durand-Kerner, Cholesky, DARE, Runge-Kutta ODE Integrators (RK4 + Dormand-Prince), Spectral Radius & Discrete Stability Margin, QR Decomposition (Householder / Givens), LU Decomposition with Partial Pivoting, Singular Value Decomposition (Golub-Kahan), Symplectic Integrators (Semi-Implicit Euler, Velocity Verlet) |
2829
| [Nonlinear Control](doc/nonlinear_control/README.md) | Feedback Linearization, Backstepping Control, Model Reference Adaptive Control (MRAC) |
2930
| [Robust Control](doc/robust_control/README.md) | Active Disturbance Rejection Control (ADRC + ESO), Sliding Mode Control (SMC), Disturbance Observer (DOB), H∞ State-Feedback Control |

‎ROADMAP.md‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ Difficulty legend:
2626

2727
| # | Component | Target module | Difficulty |
2828
|----|------------------------------------------------------|-------------------------|------------|
29-
| 55 | Polynomial (cubic/quintic) point-to-point profile | `motion_profiles` (new) | ★☆☆☆☆ |
3029
| 56 | Trapezoidal (LSPB) profile + duration sync | `motion_profiles` (new) | ★☆☆☆☆ |
3130
| 53 | SE(3) transform, twists, wrenches, adjoint, exp/log | `math` | ★★☆☆☆ |
3231
| 60 | S-curve (jerk-limited) profile | `motion_profiles` (new) | ★★☆☆☆ |
@@ -47,14 +46,6 @@ the library does not yet expose. Detailed below under
4746

4847
## Tier 1 — Trivial primitives ★☆☆☆☆
4948

50-
### 55. Polynomial point-to-point profile
51-
- **What:** Cubic (position + velocity) or quintic (+ acceleration) time law between two states over a
52-
fixed duration; defines the shared `TrajectoryState` / `MotionLimits` types of `motion_profiles`.
53-
- **Embedded value:** Coefficients solved once in closed form; each servo tick is a Horner evaluation.
54-
- **Algorithm / paper:** M. Spong, S. Hutchinson, M. Vidyasagar, *Robot Modeling and Control*, Ch. 5.
55-
- **Reuses:** Scalar templates.
56-
- **Spec:** [roadmap/motion_profiles/PolynomialProfile](roadmap/motion_profiles/PolynomialProfile/implementation.md).
57-
5849
### 56. Trapezoidal (LSPB) profile
5950
- **What:** Minimum-time velocity/acceleration-limited rest-to-rest move, triangular for short moves, plus
6051
a fixed-duration plan for multi-axis synchronization.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# Polynomial Point-to-Point Profile
2+
3+
## Overview & Motivation
4+
5+
A polynomial profile is a closed-form time law that drives one axis from a start state to a goal state over a fixed duration $T$. A **cubic** matches endpoint position and velocity; a **quintic** additionally matches endpoint acceleration, so the motion has no acceleration step at either end.
6+
7+
Point-to-point moves are the bread and butter of servo motion: robot joints, CNC and pick-and-place axes, motor position loops. The coefficients come from a handful of algebraic expressions, solved once. Each servo tick is then a fixed number of multiply-adds, with no iteration, no lookup table and a deterministic cycle count.
8+
9+
## Mathematical Theory
10+
11+
### Boundary Conditions
12+
13+
Let $t \in [0, T]$, $\Delta q = q_f - q_0$, and let $\alpha_0, \alpha_f$ be the start and end accelerations. A polynomial of degree $n$ has $n+1$ coefficients, so a cubic satisfies four conditions and a quintic six:
14+
15+
$$q(0) = q_0,\quad q(T) = q_f,\quad \dot{q}(0) = v_0,\quad \dot{q}(T) = v_f \qquad \text{(quintic also: } \ddot{q}(0) = \alpha_0,\ \ddot{q}(T) = \alpha_f\text{)}$$
16+
17+
The conditions are linear in the coefficients $a_k$ of $q(t) = \sum_k a_k t^k$ and the time basis is fixed, so the solution is a plug-in formula rather than a runtime matrix solve.
18+
19+
### Cubic
20+
21+
The start conditions give $a_0 = q_0$ and $a_1 = v_0$. The end conditions leave a $2\times 2$ system with the closed-form solution
22+
23+
$$a_2 = \frac{3\,\Delta q - (2 v_0 + v_f)\,T}{T^2}, \qquad a_3 = \frac{-2\,\Delta q + (v_0 + v_f)\,T}{T^3}$$
24+
25+
The acceleration is linear in $t$ and the jerk is the constant $6 a_3$.
26+
27+
### Quintic
28+
29+
The start conditions give $a_0 = q_0$, $a_1 = v_0$, $a_2 = \alpha_0 / 2$. The end conditions give
30+
31+
$$a_3 = \frac{20\,\Delta q - (8 v_f + 12 v_0)\,T - (3\alpha_0 - \alpha_f)\,T^2}{2\,T^3}$$
32+
33+
$$a_4 = \frac{-30\,\Delta q + (14 v_f + 16 v_0)\,T + (3\alpha_0 - 2\alpha_f)\,T^2}{2\,T^4}$$
34+
35+
$$a_5 = \frac{12\,\Delta q - 6 (v_f + v_0)\,T - (\alpha_0 - \alpha_f)\,T^2}{2\,T^5}$$
36+
37+
The acceleration is a cubic matching $\alpha_0$ and $\alpha_f$, hence continuous; the jerk is a quadratic.
38+
39+
### Sampling
40+
41+
Position, velocity, acceleration and jerk are four Horner evaluations of the polynomial and its three derivatives:
42+
43+
$$q = a_0 + t\big(a_1 + t(a_2 + t(a_3 + t(a_4 + t\,a_5)))\big), \qquad \dddot{q} = 6 a_3 + t\,(24 a_4 + 60 a_5\, t)$$
44+
45+
The cubic is the same evaluation with $a_4 = a_5 = 0$. The time is first clamped to $[0, T]$.
46+
47+
### Rest-to-Rest Peaks
48+
49+
With zero end velocities (and accelerations, for the quintic) the cubic peaks at $1.5\,\Delta q / T$ in velocity and $6\,\Delta q / T^2$ in acceleration; the quintic at $1.875\,\Delta q / T$ and $\tfrac{10}{\sqrt{3}}\,\Delta q / T^2 \approx 5.77\,\Delta q / T^2$.
50+
51+
## Complexity Analysis
52+
53+
| Operation | Time | Space | Notes |
54+
|-----------|------|-------|-------|
55+
| Coefficient solve | $O(1)$ | $O(1)$ | Closed-form expressions, once at construction or reset |
56+
| Sample | $O(\text{degree})$ | $O(1)$ | Four Horner evaluations, at most 14 multiply-adds; no state change, so deterministic |
57+
| Stored state | n/a | $O(1)$ | Six coefficients, the duration and the degree; stack only, no heap |
58+
59+
## Step-by-Step Walkthrough
60+
61+
Rest-to-rest cubic with $q_0 = 0$, $q_f = 1$, $T = 2$, $v_0 = v_f = 0$, so $\Delta q = 1$ and $a_0 = a_1 = 0$.
62+
63+
| Step | Computation | Result |
64+
|------|-------------|--------|
65+
| $a_2$ | $(3\cdot 1 - 0)/2^2$ | $0.75$ |
66+
| $a_3$ | $(-2\cdot 1 + 0)/2^3$ | $-0.25$ |
67+
| Position | $0.75\,t^2 - 0.25\,t^3 = 3(t/2)^2 - 2(t/2)^3$ | $q(1) = 0.5$, $q(2) = 1$ |
68+
| Velocity | $1.5\,t - 0.75\,t^2$ | $v(1) = 0.75 = 1.5\,\Delta q / T$ (the peak) |
69+
| Acceleration | $1.5 - 1.5\,t$ | $+1.5$ at $t=0$, $-1.5$ at $t=2$ |
70+
| Jerk | $6 a_3$ | $-1.5$, constant |
71+
72+
The position is the smoothstep of $t/2$, symmetric about the midpoint, where the velocity peaks. The acceleration jumps from $0$ at rest to $1.5$ at the first instant and from $-1.5$ back to $0$ at the end.
73+
74+
## Pitfalls & Edge Cases
75+
76+
- **Duration must be positive.** The coefficients contain $1/T^k$ terms; a zero or negative duration is rejected at construction and reset.
77+
- **Short durations in single precision.** Higher-order coefficients grow like $1/T^5$ for the quintic, and terms of opposite sign cancel near the end of the move, losing digits.
78+
- **Clamped time holds the endpoint state.** Outside $[0, T]$ the sample is the state at $0$ or $T$, so it reports the end velocity and acceleration, not zero, when they are nonzero.
79+
- **Acceleration steps in the cubic.** Acceleration is not matched, so at rest endpoints it jumps from or to zero, which excites flexible loads. The quintic's jerk is finite but generally nonzero at the ends.
80+
- **No limit enforcement.** Peaks follow from $\Delta q$, $T$ and the boundary values; inconsistent boundary velocities can overshoot $q_f$ before returning.
81+
82+
## Variants & Generalizations
83+
84+
| Variant | Key difference |
85+
|---------|----------------|
86+
| **Cubic (this)** | Position and velocity matched; constant jerk; acceleration discontinuous at the ends |
87+
| **Quintic (this)** | Adds acceleration matching; continuous acceleration at about $25\%$ higher peak velocity for rest-to-rest |
88+
| **Path-parameter time law** | A 0 to 1 instance maps time to a normalized path coordinate; with $v_0 = v_f = 1$, $T = 1$ the cubic collapses to $s = t$ |
89+
90+
## Applications
91+
92+
- **Joint-space moves** — robot joints and servo axes between taught points; multi-axis moves use one instance per axis with a common $T$ so all axes arrive together.
93+
- **Pick-and-place and CNC positioning** — fixed-time axis moves where smoothness matters more than minimum time.
94+
- **Path timing** — the scalar time law along a geometric path such as an orientation interpolation.
95+
96+
## Connections to Other Algorithms
97+
98+
| Algorithm | Relationship |
99+
|-----------|--------------|
100+
| Trapezoidal velocity profile | Respects velocity and acceleration limits with a free duration; polynomials fix the duration and let the peaks follow |
101+
| S-curve profile | Bounds jerk explicitly under limits; the quintic only smooths the endpoints |
102+
| Cubic spline interpolation | Handles many via points with continuous acceleration; for two knots it reduces to this cubic |
103+
104+
## References & Further Reading
105+
106+
- Craig, J. J., "Introduction to Robotics: Mechanics and Control," 3rd ed., Pearson, 2005, Chapter 7 (trajectory generation).
107+
- Biagiotti, L. and Melchiorri, C., "Trajectory Planning for Automatic Machines and Robots," Springer, 2008, §2.1.

‎doc/motion_profiles/README.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Motion Profiles
2+
3+
Time laws for point-to-point motion of a single axis: position, velocity, acceleration and jerk as functions of time, planned once and sampled every control tick.
4+
5+
## Algorithms
6+
7+
| Algorithm | Description |
8+
|-------------------------------------------------|---------------------------------------------------------------------------------------------|
9+
| [Polynomial Profile](PolynomialProfile.md) | Cubic (position + velocity) or quintic (+ acceleration) boundary-matched time law over a fixed duration |

‎numerical/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ add_subdirectory(controllers)
44
add_subdirectory(estimators)
55
add_subdirectory(filters)
66
add_subdirectory(math)
7+
add_subdirectory(motion_profiles)
78
add_subdirectory(optimization)
89
add_subdirectory(regularization)
910
add_subdirectory(nonlinear_control)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
numerical_add_header_library(numerical.motion_profiles)
2+
3+
target_include_directories(numerical.motion_profiles ${NUMERICAL_VISIBILITY}
4+
"$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/../../>"
5+
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
6+
)
7+
8+
target_link_libraries(numerical.motion_profiles ${NUMERICAL_VISIBILITY}
9+
infra.util
10+
numerical.math
11+
)
12+
13+
target_sources(numerical.motion_profiles PRIVATE
14+
MotionProfileTypes.hpp
15+
PolynomialProfile.hpp
16+
)
17+
18+
numerical_add_coverage_sources(numerical.motion_profiles
19+
PolynomialProfile.cpp
20+
)
21+
22+
add_subdirectory(test)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#pragma once
2+
3+
#include <type_traits>
4+
5+
namespace motion_profiles
6+
{
7+
template<typename T>
8+
struct TrajectoryState
9+
{
10+
static_assert(std::is_floating_point_v<T>, "TrajectoryState supports floating-point types only");
11+
12+
T position{};
13+
T velocity{};
14+
T acceleration{};
15+
T jerk{};
16+
17+
bool operator==(const TrajectoryState& other) const = default;
18+
};
19+
20+
template<typename T>
21+
struct MotionLimits
22+
{
23+
static_assert(std::is_floating_point_v<T>, "MotionLimits supports floating-point types only");
24+
25+
T vMax{};
26+
T aMax{};
27+
T jMax{};
28+
};
29+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "numerical/motion_profiles/PolynomialProfile.hpp"
2+
3+
namespace motion_profiles
4+
{
5+
template class PolynomialProfile<float>;
6+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#pragma once
2+
3+
#include "infra/util/ReallyAssert.hpp"
4+
#include "numerical/math/CompilerOptimizations.hpp"
5+
#include "numerical/motion_profiles/MotionProfileTypes.hpp"
6+
#include <algorithm>
7+
#include <array>
8+
#include <cstdint>
9+
#include <type_traits>
10+
11+
namespace motion_profiles
12+
{
13+
enum class Degree : std::uint8_t
14+
{
15+
Cubic = 3,
16+
Quintic = 5
17+
};
18+
19+
template<typename T>
20+
struct BoundaryConditions
21+
{
22+
static_assert(std::is_floating_point_v<T>, "BoundaryConditions supports floating-point types only");
23+
24+
T q0{};
25+
T qf{};
26+
T v0{};
27+
T vf{};
28+
T a0{};
29+
T af{};
30+
};
31+
32+
template<typename T>
33+
class PolynomialProfile
34+
{
35+
static_assert(std::is_floating_point_v<T>, "PolynomialProfile supports floating-point types only");
36+
37+
public:
38+
PolynomialProfile(const BoundaryConditions<T>& bc, T tf, Degree degree);
39+
40+
OPTIMIZE_FOR_SPEED TrajectoryState<T> Sample(T t) const;
41+
T Duration() const;
42+
void Reset(const BoundaryConditions<T>& bc, T tf);
43+
44+
private:
45+
void SolveCubic(const BoundaryConditions<T>& bc);
46+
void SolveQuintic(const BoundaryConditions<T>& bc);
47+
48+
std::array<T, 6> coeff{};
49+
T duration{};
50+
Degree degree;
51+
};
52+
53+
template<typename T>
54+
PolynomialProfile<T>::PolynomialProfile(const BoundaryConditions<T>& bc, T tf, Degree degree)
55+
: degree{ degree }
56+
{
57+
Reset(bc, tf);
58+
}
59+
60+
template<typename T>
61+
void PolynomialProfile<T>::Reset(const BoundaryConditions<T>& bc, T tf)
62+
{
63+
really_assert(tf > T{});
64+
65+
duration = tf;
66+
if (degree == Degree::Cubic)
67+
SolveCubic(bc);
68+
else
69+
SolveQuintic(bc);
70+
}
71+
72+
template<typename T>
73+
OPTIMIZE_FOR_SPEED TrajectoryState<T> PolynomialProfile<T>::Sample(T t) const
74+
{
75+
t = std::min(std::max(t, T{}), duration);
76+
77+
const auto& a{ coeff };
78+
return {
79+
a[0] + t * (a[1] + t * (a[2] + t * (a[3] + t * (a[4] + t * a[5])))),
80+
a[1] + t * (T{ 2 } * a[2] + t * (T{ 3 } * a[3] + t * (T{ 4 } * a[4] + t * T{ 5 } * a[5]))),
81+
T{ 2 } * a[2] + t * (T{ 6 } * a[3] + t * (T{ 12 } * a[4] + t * T{ 20 } * a[5])),
82+
T{ 6 } * a[3] + t * (T{ 24 } * a[4] + t * T{ 60 } * a[5])
83+
};
84+
}
85+
86+
template<typename T>
87+
T PolynomialProfile<T>::Duration() const
88+
{
89+
return duration;
90+
}
91+
92+
template<typename T>
93+
void PolynomialProfile<T>::SolveCubic(const BoundaryConditions<T>& bc)
94+
{
95+
const T tf{ duration };
96+
const T distance{ bc.qf - bc.q0 };
97+
98+
coeff = {
99+
bc.q0,
100+
bc.v0,
101+
(T{ 3 } * distance - (T{ 2 } * bc.v0 + bc.vf) * tf) / (tf * tf),
102+
(T{ -2 } * distance + (bc.v0 + bc.vf) * tf) / (tf * tf * tf),
103+
T{},
104+
T{}
105+
};
106+
}
107+
108+
template<typename T>
109+
void PolynomialProfile<T>::SolveQuintic(const BoundaryConditions<T>& bc)
110+
{
111+
const T tf{ duration };
112+
const T tf2{ tf * tf };
113+
const T tf3{ tf2 * tf };
114+
const T distance{ bc.qf - bc.q0 };
115+
116+
coeff = {
117+
bc.q0,
118+
bc.v0,
119+
bc.a0 / T{ 2 },
120+
(T{ 20 } * distance - (T{ 8 } * bc.vf + T{ 12 } * bc.v0) * tf - (T{ 3 } * bc.a0 - bc.af) * tf2) / (T{ 2 } * tf3),
121+
(T{ -30 } * distance + (T{ 14 } * bc.vf + T{ 16 } * bc.v0) * tf + (T{ 3 } * bc.a0 - T{ 2 } * bc.af) * tf2) / (T{ 2 } * tf3 * tf),
122+
(T{ 12 } * distance - T{ 6 } * (bc.vf + bc.v0) * tf - (bc.a0 - bc.af) * tf2) / (T{ 2 } * tf3 * tf2)
123+
};
124+
}
125+
126+
#ifdef NUMERICAL_TOOLBOX_COVERAGE_BUILD
127+
extern template class PolynomialProfile<float>;
128+
#endif
129+
}

0 commit comments

Comments
 (0)