Skip to content

tp: S-curve (PLANNER_TYPE 1) fixes for feed override changes - #4974

Open
MetalMusings wants to merge 3 commits into
LinuxCNC:masterfrom
MetalMusings:tp1-final-decel-fix
Open

MetalMusings wants to merge 3 commits into
LinuxCNC:masterfrom
MetalMusings:tp1-final-decel-fix

Conversation

@MetalMusings

Copy link
Copy Markdown

tp: S-curve (PLANNER_TYPE 1) fixes for feed override changes

With [TRAJ]PLANNER_TYPE = 1, changing the feed override while a program
runs can take the tool off the programmed path, by up to the length of the
next segment, and can give jerk spikes of up to 70× MAX_JERK. This PR
fixes three causes, one commit each. All changes are in src/emc/tp/tp.c
(+157 −8 lines) and only affect PLANNER_TYPE = 1. Without feed override
changes, the motion is the same as before.

  1. The final deceleration starts too early (b9377e5a11): the tool
    leaves the path.
  2. Segment changes within a servo cycle (ea28c2870c): one-cycle
    speed dips.
  3. Ruckig replans that fail (2ddbfca578): the old speed is kept,
    or the segment falls back to a trapezoidal profile.

Test setup

The machine used 200 mm/s, 2000 mm/s² and 50000 mm/s³ on X, Y and Z. The
servo period was 1 ms or 0.5 ms. The commanded axis positions were sampled
every servo cycle. They were compared with the programmed path, and with
the axis velocity, acceleration and jerk limits computed from position
differences. The feed override was stepped to random values while the
programs ran:

  • 20–180 % every 0.25 s
  • 10–200 % every 50 ms
  • 0–150 % every 0.1 s, including feed holds

There were 10 programs: lines, arcs, chords, a spiral of short segments, a
helix, rapids, exact stop and G95.


1. Final deceleration only near the end of the segment

Problem

Lowering the feed override in the middle of a segment can start the
parabolic blend into the next segment far too early. The next segment then
runs to its end while the current one still has most of its length to go.
For the rest of the current segment, the commanded path is offset by the
next segment's whole displacement.

Example: a 3-turn G3 helix followed by a 10 mm G1, with the feed override
changed while the helix runs:

G21 G90 G17 G64 P0.05
G0 X0 Y0 Z0
G1 X10 Y0 Z0 F3000
G3 X10 Y0 Z-10 I-10 J0 P4
G1 X0 Y0 Z-10
M2

About one turn into the helix, the commanded position leaves it. It then
follows the same helix shifted by −10 mm in X (centre at X−10 instead of
X0) down to Z−10, and ends at X0 Y0 Z−10. That is up to 10 mm off the
programmed path, while the reported velocity looks normal.

This happened in 2 of 5 random override sequences of 20–180 % every 0.25 s
at 1 ms. It needs the override to drop, and the speed to fall below the
blend velocity, while the segment is still slowing down.

Cause

tpCalculateSCurveAccel() returns TP_SCURVE_ACCEL_DECEL whenever Ruckig
slows down:

int res = (req_a < 0.0 || (req_a == 0.0 && req_v < tc->currentvel)) ?
          TP_SCURVE_ACCEL_DECEL : TP_SCURVE_ACCEL_ACCEL;

tpUpdateCycle() stores that as tc->on_final_decel. tcIsBlending()
starts the parabolic blend when on_final_decel is set and the speed is
below blend_vel, and keeps it set until the blend is complete. So any
deceleration counts as the final one, including one for a lower feed
override in the middle of a long segment.

The trapezoidal planner sets on_final_decel only when the distance left
is what slows the segment down, that is, near its end. The S-curve path has
no such condition.

Fix

Report DECEL only within the jerk-limited distance needed to stop. The
check uses stoppingDist() from sp_scurve, with 20 % and one cycle of
margin:

if (res == TP_SCURVE_ACCEL_DECEL
        && dx > 1.2 * stoppingDist(req_v, req_a, maxaccel, maxjerk) + req_v * tc->cycle_time) {
    res = TP_SCURVE_ACCEL_ACCEL;
}

The final deceleration towards the blend happens within that distance.
Slowing to the final velocity takes about as much distance as stopping, or
less, and the margin covers the difference in profile shape. Normal blends
therefore start as before. Only decelerations far from the end, which come
from the feed override, no longer start a blend.


2. Segment changes within a servo cycle, from the motion actually made

Problem

A segment that ends within a servo cycle hands the rest of that cycle to
the next segment. With override changes, this gives single-cycle dips or
bumps in the speed, for example 14.3 → 13.5 → 14.2 mm/s. Their jerk is far
above the limit: up to 30× MAX_JERK at 1 ms, and up to 70× at 0.5 ms on
the spiral of short segments.

Cause

Two parts of that hand-over assume the trapezoidal planner:

  • tpCheckEndCondition() estimates when in the cycle the segment ends. It
    assumes the speed goes straight to the final velocity. Under the S-curve
    planner the speed changes over many cycles. After an override change the
    segment ends at a speed far from the new final velocity. The estimated
    time is then wrong, and the position steps by the difference.
  • tpHandleSplitCycle() starts the next segment from the speed and
    acceleration at the start of the cycle. That loses the speed change up to
    the split time.

Fix

For PLANNER_TYPE = 1, a new tpCheckEndConditionSCurve() finds the split
time. It is the first time in the cycle where the current velocity,
acceleration and jerk reach the end of the segment, found by bisection. The
final velocity is the speed at that time. The next segment takes over that
speed and the acceleration the current jerk has reached by then.


3. Ruckig replans that fail after override changes

Problem

Ruckig position control finds no trajectory in two cases that override
changes create. When the replan fails, the segment either runs its old
trajectory on at the old speed to its end, or, right after velocity
control, falls back to a trapezoidal profile. That fallback has a step in
the acceleration, up to 53× MAX_JERK.

Cause and fix

  • Above the max velocity after the override was lowered. Ruckig brakes
    into the velocity limit at full jerk and arrives still decelerating. It
    then has to go below the limit and come back up. On a short segment that
    takes going backwards, which a min velocity of 0 forbids.
    • Velocity control now slows the motion to the max velocity, ending with
      zero acceleration, and position control takes over from there. This
      applies where there is still room to slow to the final velocity after
      that.
    • Where there is no such room, velocity control slows the motion to the
      final velocity as fast as it can.
    • Past the end of a velocity profile, the motion goes on at the speed it
      reached. Before, the clamped Ruckig time stopped the motion for the
      rest of the cycle.
  • Below a final velocity the segment has no room to reach, after the
    override was raised.
    Ruckig is now asked again with the highest final
    velocity there is room for, found by bisection. The next segment does
    the rest of the speed-up.

Testing

Highest axis jerk, as a multiple of MAX_JERK, over all programs and
override sequences:

before after
1 ms servo period 30–38× ≤ 1.19×
0.5 ms, non-realtime 41–70× ≤ 1.5×
0.5 ms, PREEMPT_RT 44–63× ≤ 1.46×
before after
Helix example, 5 random override sequences 10 mm off the path in 2 of 5 on the path in 5 of 5 (≤ 0.0005 mm from it)
All programs, no override changes — identical motion, timing and limits
Ruckig replan failures with override steps yes none

The remaining excess, up to about 1.5×, also occurs without override
changes and is not touched by this PR. It comes from stops that end within
a cycle, and from the curvature step where a G2 joins a G3.

At 0.5 ms on PREEMPT_RT, the longest cycles were 35 µs for
motion-controller and 16 µs for motion-command-handler.

On master c21c442 these tests pass: tests/motion, tests/tp, tests/trajectory-planner,
tests/abort, jog-speed-change, stale-motion-status and
spindle-sync-scurve.

Reproducing without the sampling setup

  1. Use a sim config with PLANNER_TYPE = 1 and MAX_LINEAR_JERK set.
  2. Run the helix program above. During the first turn of the helix, move
    the feed override slider down and up a few times.
  3. Before this PR, the preview shows the tool path shifted by −10 mm in X.

🤖 Generated with Claude Code

MetalMusings and others added 3 commits December 6, 2026 07:01
With PLANNER_TYPE = 1, tpCalculateSCurveAccel reported DECEL whenever
Ruckig slowed down, and that becomes tc->on_final_decel, which lets
tcIsBlending start the parabolic blend into the next segment. Slowing
down for a lower feed override in the middle of a segment then started
the blend there: the next segment ran while this one still had its
whole length to go, and the commanded path was offset by the next
segment's displacement for the rest of it.

Seen on a 3 turn G3 helix (P4) followed by a 10 mm G1, with the feed
override changed every 0.25 s: after about a turn the path left the
helix and ran a helix shifted by -10 mm in X to the end, 10 mm off the
programmed path. With 5 random override sequences this happened in 2.

DECEL is now only reported within the distance it takes to stop (with
20 % and one cycle to spare), as the trapezoidal planner's final
deceleration test does in effect. Without override changes the motion
is unchanged; with them the helix stays on its path in all 5.

Reproduce with tests/trajectory-bench of the scurve branch:
  run.py -p tp1 -g helix -s 0.25:0.2:1.8:1

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With PLANNER_TYPE = 1, a segment that ends within a servo cycle hands
the rest of the cycle to the next segment. Two things in that hand-over
assumed the trapezoidal planner:

- tpCheckEndCondition estimated when in the cycle the segment ends from
  a speed going straight to the final velocity. Under the S-curve the
  speed changes over many cycles; after a feed override change the
  segment ends at a speed far from the new final velocity, the split
  time is wrong, and the position steps by the difference.
- The next segment started from the speed and acceleration at the start
  of the cycle, losing the speed change of the split time.

Both show as a one cycle dip or bump in the speed, a jerk far above
the limit: up to 70x MAX_JERK at a 0.5 ms servo period on a spiral of
short segments with the feed override changed every 0.25 s.

The split time is now where the current velocity, acceleration and
jerk reach the end of the segment (bisection within the cycle), and the
next segment takes over the velocity and acceleration at that time.
Motion without override changes is unchanged in the trajectory bench.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With PLANNER_TYPE = 1, Ruckig position control could not replan in
two cases that feed override changes bring about. The replan failed,
and the segment either ran its old trajectory on to its end or, right
after a switch from velocity control, fell back to a trapezoidal
profile with a step in the acceleration (50x MAX_JERK).

- Above the max velocity after the override was lowered: Ruckig brakes
  into the velocity limit at full jerk and arrives still decelerating,
  so it has to undershoot and come back up, which on a short segment
  takes going backwards; min velocity 0 forbids that. Velocity control
  now slows to the max velocity with zero acceleration, where slowing
  to the final velocity fits in the distance left, and position control
  takes over from there; where it does not fit, velocity control slows
  to the final velocity as fast as it can. Past the end of a velocity
  profile the motion goes on at the speed reached, where the clamped
  Ruckig time used to stop it for the rest of the cycle.
- Below a final velocity the segment has no room to reach after the
  override was raised: plan again to the highest final velocity there
  is room for.

Checked in the trajectory bench at 0.5 and 1 ms servo periods with
random override steps (20-180 % every 0.25 s, 10-200 % every 50 ms,
0-150 % every 0.1 s): no replan fails, and the axis jerk stays within
1.5x MAX_JERK, the rest being from stops ending within a cycle and
curvature steps at arc joins, which are there without override changes
too. Without override changes the motion is unchanged. tests/motion,
tests/tp, tests/trajectory-planner, tests/abort pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant