h-move documentation
h-move plans jerk-limited, single-axis motion in portable C99. This page covers version 0.3.0.
Quick start
Download the source ZIP, extract
it into your project, and rename the extracted folder to h-move. Then add it
to your CMakeLists.txt:
cmake_minimum_required(VERSION 3.21)
project(motion_app LANGUAGES C)
add_subdirectory(h-move)
add_executable(motion_app main.c)
target_link_libraries(motion_app PRIVATE h_move::h_move)Save this as main.c. It plans a 100 mm move and samples it once per control
tick:
#include <stdio.h>
#include "h_move/trajectory.h"
int main(void) {
/* Move from rest at 0 mm to rest at 100 mm. */
const H_Move_Position_Request request = {
.initial = {.position = 0.0F, .velocity = 0.0F, .acceleration = 0.0F},
.target = {.position = 100.0F, .velocity = 0.0F, .acceleration = 0.0F},
.limits = {.velocity = 200.0F, .acceleration = 1000.0F, .jerk = 10000.0F}};
H_Move_Trajectory trajectory;
const H_Move_Status status = h_move_plan_position(&request, &trajectory);
if (status != k_h_move_ok) {
fprintf(stderr, "planning failed: %d\n", (int)status);
return 1;
}
/* Sample once per control tick until the move is complete. */
const float tick_seconds = 0.1F;
for (int tick = 0;; ++tick) {
float elapsed = (float)tick * tick_seconds;
if (elapsed > trajectory.duration) {
elapsed = trajectory.duration;
}
H_Move_Sample sample;
if (!h_move_sample(&trajectory, elapsed, &sample)) {
return 1;
}
const float position = request.initial.position + sample.displacement;
printf("t=%.3f s position=%7.3f mm velocity=%7.2f mm/s\n",
(double)elapsed, (double)position, (double)sample.velocity);
if (elapsed == trajectory.duration) {
break;
}
}
return 0;
}Build and run it:
cmake -S . -B build
cmake --build build
./build/motion_appt=0.000 s position= 0.000 mm velocity= 0.00 mm/s
t=0.100 s position= 1.667 mm velocity= 50.00 mm/s
t=0.200 s position= 11.667 mm velocity= 150.00 mm/s
t=0.300 s position= 30.000 mm velocity= 200.00 mm/s
t=0.400 s position= 50.000 mm velocity= 200.00 mm/s
t=0.500 s position= 70.000 mm velocity= 200.00 mm/s
t=0.600 s position= 88.333 mm velocity= 150.00 mm/s
t=0.700 s position= 98.333 mm velocity= 50.00 mm/s
t=0.800 s position=100.000 mm velocity= 0.00 mm/sConcepts
- Units. Pick one distance unit, such as millimetres, steps, or degrees, and use it for every field in a request. Velocity is that unit per second, acceleration per second squared, and jerk per second cubed. Time is always in seconds.
- Two planners. A position move travels from one full state (position, velocity, acceleration) to another. A velocity transition changes speed without a target position, for spindles, conveyors, and jogging.
- Plan once, sample often. Planning produces a trajectory. Your control loop then samples it at the elapsed time since the move started. Samples report displacement from the starting position, so add the initial position to get an absolute position.
- Your memory, your clock. Trajectories live in storage you provide and can be copied freely. h-move has no heap, clock, hardware access, or global state, so calls that write to separate trajectories are safe to make from different threads.
- One axis at a time. Each plan covers a single axis. Multi-axis coordination, motor drivers, and machine safety are up to your application.
API reference
Include h_move/trajectory.h and link h_move::h_move. C++ code
uses the same header.
Plan a position move
H_Move_Status h_move_plan_position(const H_Move_Position_Request *request,
H_Move_Trajectory *output);Plans from request->initial to request->target of an H_Move_Position_Request and writes the result to an H_Move_Trajectory. Endpoints may have nonzero velocity and acceleration, as long as each is within the limits.
Plan a velocity transition
H_Move_Status h_move_plan_velocity(const H_Move_Velocity_Request *request,
H_Move_Trajectory *output);Plans the change of velocity and acceleration described by an H_Move_Velocity_Request, with no target position.
/* Bring a conveyor from rest up to 200 mm/s. */
const H_Move_Velocity_Request request = {
.initial = {.velocity = 0.0F, .acceleration = 0.0F},
.target = {.velocity = 200.0F, .acceleration = 0.0F},
.limits = {.velocity = 250.0F, .acceleration = 1000.0F, .jerk = 10000.0F}};
H_Move_Trajectory trajectory;
if (h_move_plan_velocity(&request, &trajectory) == k_h_move_ok) {
/* Reaches 200 mm/s after 0.300 s, having travelled 30 mm. */
}Sample a trajectory
bool h_move_sample(const H_Move_Trajectory *trajectory, float elapsed_seconds,
H_Move_Sample *output);Writes an H_Move_Sample for elapsed_seconds. Valid times run from zero to trajectory->duration; clamp later times to the duration. Returns false for times outside that range.
Check limits
bool h_move_limits_make(float velocity, float acceleration, float jerk,
H_Move_Limits *output);Optional helper that fills an H_Move_Limits, returning false unless all three limits are positive and finite. The planners perform the same check, so you can also fill in limits directly.
Types
Every quantity is a float in your chosen distance unit U and seconds. Fields that
hold other structs have no unit of their own.
H_Move_State
A complete motion state: where the axis is, how fast it is moving, and how quickly that speed is changing.
typedef struct {
float position; /* U */
float velocity; /* U/s */
float acceleration; /* U/s^2 */
} H_Move_State;| Field | Type | Unit | Notes and limits |
|---|---|---|---|
position | float | U | Any finite value. |
velocity | float | U/s | Finite, with magnitude at most the velocity limit. |
acceleration | float | U/s² | Finite, with magnitude at most the acceleration limit. |
- Used as the
initialandtargetof anH_Move_Position_Request. - A
floatholds about seven significant digits, so keep positions in local coordinates near zero. At 1,000,000 mm, a 0.01 mm move rounds away entirely. - An endpoint accelerating toward the velocity limit must be able to bring its acceleration back to zero before reaching that limit. If it cannot, planning returns
k_h_move_no_solution.
H_Move_Velocity_State
Velocity and acceleration without a position.
typedef struct {
float velocity; /* U/s */
float acceleration; /* U/s^2 */
} H_Move_Velocity_State;| Field | Type | Unit | Notes and limits |
|---|---|---|---|
velocity | float | U/s | Finite, with magnitude at most the velocity limit. |
acceleration | float | U/s² | Finite, with magnitude at most the acceleration limit. |
- Used as the
initialandtargetof anH_Move_Velocity_Request, and as the starting derivatives stored in anH_Move_Trajectory. - The same endpoint rule as
H_Move_Stateapplies: an endpoint that cannot stop accelerating before the velocity limit returnsk_h_move_no_solution.
H_Move_Limits
Symmetric bounds the planned motion stays within. Velocity stays within ±velocity, acceleration within ±acceleration, and jerk within ±jerk.
typedef struct {
float velocity; /* U/s */
float acceleration; /* U/s^2 */
float jerk; /* U/s^3 */
} H_Move_Limits;| Field | Type | Unit | Notes and limits |
|---|---|---|---|
velocity | float | U/s | Positive and finite, from FLT_MIN to FLT_MAX / 524288 (about 6.5 × 10³²). |
acceleration | float | U/s² | Positive and finite, from FLT_MIN to FLT_MAX / 2. |
jerk | float | U/s³ | Positive and finite. Also sets the internal distance scale. |
h_move_limits_makechecks that all three are positive and finite. Both planners repeat that check, so you can also fill the struct directly.- The ratios between limits are bounded too. Let J₀ be
jerkrounded down to a power of two. Both planners needvelocity / J₀of at least 2⁻⁴⁸ s². - Position moves also need
acceleration / jerkfrom 2⁻¹⁶ s (about 15 µs) to 256 s, andvelocity / jerkof at most 4,096 s². The exact constants are inposition_domain.h. - Velocity transitions also need
velocity / J₀of at most 2³² s², andacceleration / J₀from 2⁻⁴⁸ s to 2³⁶ s. The exact constants are invelocity_domain.h. - Limits outside these ranges return
k_h_move_invalid_input.
H_Move_Position_Request
Input to h_move_plan_position: move from one full state to another.
typedef struct {
H_Move_State initial, target;
H_Move_Limits limits;
} H_Move_Position_Request;| Field | Type | Unit | Notes and limits |
|---|---|---|---|
initial | H_Move_State | — | Where the move starts. |
target | H_Move_State | — | Where the move ends. |
limits | H_Move_Limits | — | Bounds for the whole move, including both endpoints. |
- If
initialandtargetare exactly equal, planning succeeds at once with a zero-duration trajectory. - Otherwise the distance
target.position - initial.positionmust be at mostlimits.velocity× 65,536 s, and the planned move must last from 2⁻²⁰ s (about 1 µs) to 65,536 s. Requests outside this range returnk_h_move_invalid_input. - To chain moves, start each request from the sampled end of the previous trajectory.
H_Move_Velocity_Request
Input to h_move_plan_velocity: change velocity and acceleration with no position goal.
typedef struct {
H_Move_Velocity_State initial, target;
H_Move_Limits limits;
} H_Move_Velocity_Request;| Field | Type | Unit | Notes and limits |
|---|---|---|---|
initial | H_Move_Velocity_State | — | Velocity and acceleration at the start. |
target | H_Move_Velocity_State | — | Velocity and acceleration to reach. |
limits | H_Move_Limits | — | Bounds for the whole transition. |
- If
initialandtargetare exactly equal, planning succeeds at once with a zero-duration trajectory. - Otherwise the transition must last from 2⁻²⁰ s (about 1 µs) to 65,536 s.
- Sampling reports the distance travelled during the transition as
displacement.
H_Move_Trajectory
A planned motion, written by a successful planner call and read by h_move_sample. It is a sequence of constant-jerk segments.
enum { k_h_move_max_segments = 10 };
typedef struct {
H_Move_Velocity_State initial;
float duration;
H_Move_Segment segments[k_h_move_max_segments];
size_t segment_count;
int distance_exponent;
} H_Move_Trajectory;| Field | Type | Unit | Notes and limits |
|---|---|---|---|
initial | H_Move_Velocity_State | — | Velocity and acceleration at the start. |
duration | float | s | Total time. Valid sample times run from 0 to this value. |
segments | H_Move_Segment[] | — | The constant-jerk pieces, in order. The array holds k_h_move_max_segments entries. |
segment_count | size_t | — | How many entries of segments are used, at most k_h_move_max_segments. |
distance_exponent | int | — | Internal scale used when sampling. Do not change it. |
- You own the storage. It holds no pointers and needs no cleanup, so you can copy it by assignment and share it between threads for reading.
- Treat it as read-only. Only
durationis meant for your code; leave the other fields to h-move. - Its contents are only valid after the planner returns
k_h_move_ok. A failed call leaves it untouched. - Its size depends on the target. It is 112 bytes on x86-64; check
sizeof(H_Move_Trajectory)on your MCU.
H_Move_Segment
One piece of a trajectory, during which jerk is constant.
typedef struct {
float duration; /* s */
float jerk; /* U/s^3 */
} H_Move_Segment;| Field | Type | Unit | Notes and limits |
|---|---|---|---|
duration | float | s | Length of this piece. May be zero. |
jerk | float | U/s³ | Constant jerk during this piece. |
- Keep every segment as planned. Merging neighbouring segments with the same jerk can lose the precision needed to land exactly on the target.
H_Move_Sample
The motion at one moment, written by h_move_sample.
typedef struct {
float displacement; /* U */
float velocity; /* U/s */
float acceleration; /* U/s^2 */
} H_Move_Sample;| Field | Type | Unit | Notes and limits |
|---|---|---|---|
displacement | float | U | Distance from the starting position. |
velocity | float | U/s | Velocity at that moment. |
acceleration | float | U/s² | Acceleration at that moment. |
- For a position move, the absolute position is
initial.position + displacement. - At
duration, the sample is the planned endpoint as computed, not a copy of the requested target, so it may differ from the target by rounding.
Status codes
Both planners return an H_Move_Status:
typedef enum {
k_h_move_ok,
k_h_move_invalid_input,
k_h_move_no_solution,
k_h_move_numerical_failure
} H_Move_Status;| Status | Meaning | What to do |
|---|---|---|
k_h_move_ok | The trajectory is ready to sample. | Sample it. |
k_h_move_invalid_input | A value is not finite, a limit is not positive, an endpoint exceeds the limits, or the request is outside the supported numeric range. | Fix the request. |
k_h_move_no_solution | The motion is provably impossible within the limits. | Relax the limits or change the target. |
k_h_move_numerical_failure | No valid trajectory was found. The motion may still be possible. | Treat as a fault and bring the axis to a safe state. |
A failed call leaves the output trajectory untouched. Never sample it, and do not fall back to a previous trajectory without deciding that is safe for your machine.
Building
h-move needs CMake 3.21 or newer and a C99 compiler. It downloads nothing and needs no C++ compiler.
Installing
To install it instead of adding it as a subdirectory, run this in the h-move folder:
./build.sh
cmake --install build --prefix /path/to/prefixThen find it from your project, with CMAKE_PREFIX_PATH set to the install prefix:
find_package(h_move CONFIG REQUIRED)
target_link_libraries(motion_app PRIVATE h_move::h_move)Cross-compiling
Pass your toolchain file as usual, for example ./build.sh -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake. h-move links the m math library on GCC and Clang. Set H_MOVE_MATH_LIBRARY to your
target's math library, or to an empty string if your runtime provides math automatically.
Compiler requirements
h-move depends on exact IEEE 754 single-precision arithmetic. The supplied CMake target sets the right flags for GCC and Clang. With other compilers, make sure that:
floatis IEEE 754 binary32, evaluated at float precision.fmafis a true fused multiply-add. Hardware or software is fine.- Rounding is the default round-to-nearest mode.
- Fast-math, flush-to-zero, and automatic FMA contraction are disabled.
Performance
Planning time measured on a 360 MHz ESP32-P4 across 10 million fuzz-tested requests, half position and half velocity. Every call started from a cold instruction cache:
| Planner | Mean | 99th percentile | Maximum |
|---|---|---|---|
| Position | 1.968 ms | 3.719 ms | 3.969 ms |
| Velocity | 0.521 ms | 0.743 ms | 0.764 ms |
These are measured results, not a guaranteed worst case. Measure timing, stack use, and accuracy on your own hardware before relying on them.
License
h-move is MIT licensed, so you may use, modify, and redistribute it, including commercially, as long as you keep its copyright notice. The full license is in the download.