From b9377e5a1160f226cce9f78f4c8dd13d2c134959 Mon Sep 17 00:00:00 2001 From: MetalMusings Date: Thu, 3 Dec 2026 10:34:49 +0100 Subject: [PATCH 1/3] tp: S-curve final deceleration only near the end of the segment 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 --- src/emc/tp/tp.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/emc/tp/tp.c b/src/emc/tp/tp.c index 8c700ce339e..dc5e1cdda68 100644 --- a/src/emc/tp/tp.c +++ b/src/emc/tp/tp.c @@ -3205,6 +3205,17 @@ int tpCalculateSCurveAccel(TP_STRUCT const * const tp, TC_STRUCT * const tc, TC_ int res = (req_a < 0.0 || (req_a == 0.0 && req_v < tc->currentvel)) ? TP_SCURVE_ACCEL_DECEL : TP_SCURVE_ACCEL_ACCEL; + /* DECEL becomes tc->on_final_decel, which lets a parabolic blend into + * the next segment start (tcIsBlending). Slowing down for a lower feed + * override far from the end is not the final deceleration: a blend + * started there runs the next segment while this one still has its + * whole length to go, and the path is offset by the next segment. Only + * within the distance it takes to stop is it the final one. */ + 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; + } + return res; } From ea28c2870c2e564cdb24a677011026448b1cdb80 Mon Sep 17 00:00:00 2001 From: MetalMusings Date: Thu, 3 Dec 2026 11:29:31 +0100 Subject: [PATCH 2/3] tp: S-curve split cycle from the motion actually made 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 --- src/emc/tp/tp.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 4 deletions(-) diff --git a/src/emc/tp/tp.c b/src/emc/tp/tp.c index dc5e1cdda68..078c235863a 100644 --- a/src/emc/tp/tp.c +++ b/src/emc/tp/tp.c @@ -4186,6 +4186,58 @@ STATIC inline int tcSetSplitCycle(TC_STRUCT * const tc, double split_time, * then we flag the segment as "splitting", so that during the next cycle, * it handles the transition to the next segment. */ +/** + * Split time for the S-curve planner: when within the next cycle the + * segment reaches its end, from the motion it is actually making. + * + * The estimate below for the trapezoidal planner assumes the speed goes + * straight to the final velocity. Under the S-curve planner the speed + * changes over many cycles: after a feed override change the segment can + * end at a speed well away from the final velocity of the new override, + * and that estimate puts the end too early or too late in the cycle. The + * part of the cycle given to the next segment is then too short or too + * long, and the position steps by the difference, a one cycle dip or + * bump in the speed that is a jerk far above the limit. + * + * The motion keeps its current jerk for the rest of the cycle, which is + * what the profile does except where it changes phase within the cycle. + */ +STATIC int tpCheckEndConditionSCurve(TP_STRUCT const * const tp, TC_STRUCT * const tc, double dx) +{ + double v = tc->currentvel, a = tc->currentacc, j = tc->currentjerk; + double T = tp->cycleTime; +#define SC_DIST(t) ((t) * (v + (t) * (a / 2.0 + (t) * j / 6.0))) +#define SC_VEL(t) (v + (t) * (a + (t) * j / 2.0)) + if (SC_DIST(T) < dx) { + // not reaching the end in the next cycle (or slowing to a stop + // short of it, which the profile does not do) + return TP_ERR_NO_ACTION; + } + /* first time the distance reaches dx: on [0, T] it rises while the + * speed is positive, bisection to well below a nanosecond */ + double lo = 0.0, hi = T; + int i; + for (i = 0; i < 40; i++) { + double mid = 0.5 * (lo + hi); + if (SC_DIST(mid) < dx && SC_VEL(mid) > 0.0) { + lo = mid; + } else { + hi = mid; + } + } + double dt = hi; + double v_f = fmax(SC_VEL(dt), 0.0); +#undef SC_DIST +#undef SC_VEL + if (dt < TP_TIME_EPSILON) { + tc->progress = tcGetTarget(tc, tp->reverse_run); + tcSetSplitCycle(tc, 0.0, v_f); + } else { + tcSetSplitCycle(tc, dt, v_f); + } + return TP_ERR_OK; +} + STATIC int tpCheckEndCondition(TP_STRUCT const * const tp, TC_STRUCT * const tc, TC_STRUCT const * const nexttc, int in_overlap) { //Assume no split time unless we find otherwise @@ -4214,6 +4266,10 @@ STATIC int tpCheckEndCondition(TP_STRUCT const * const tp, TC_STRUCT * const tc, } + if (GET_TRAJ_PLANNER_TYPE() == 1) { + return tpCheckEndConditionSCurve(tp, tc, dx); + } + double v_f = tpGetRealFinalVel(tp, tc, nexttc); double v_avg = (tc->currentvel + v_f) / 2.0; @@ -4332,14 +4388,19 @@ STATIC int tpHandleSplitCycle(TP_STRUCT * const tp, TC_STRUCT * const tc, switch (tc->term_cond) { case TC_TERM_COND_TANGENT: nexttc->cycle_time = tp->cycleTime - tc->cycle_time; - // In S-curve mode, use actual current velocity instead of expected term_vel - // S-curve can't change velocity instantly, term_vel is just desired value + // In S-curve mode the next segment takes over the motion as it + // is where this one ends, split time into the cycle: the speed + // tpCheckEndConditionSCurve found there and the acceleration the + // current jerk has brought it to. (Taking the state at the start + // of the cycle loses the speed change of the split time, a one + // cycle step in the acceleration.) if (GET_TRAJ_PLANNER_TYPE() == 1) { - nexttc->currentvel = tc->currentvel; + nexttc->currentvel = tc->term_vel; // Inherit acceleration, but limit to nexttc's allowed range // Important for line-to-arc transitions where arc has lower tangential accel double maxacc_next = tcGetTangentialMaxAccel(nexttc); - nexttc->currentacc = saturate(tc->currentacc, maxacc_next); + nexttc->currentacc = saturate(tc->currentacc + tc->currentjerk * tc->cycle_time, + maxacc_next); nexttc->currentjerk = tc->currentjerk; tp_debug_print("Doing tangent split (S-curve): vel=%f, acc=%f (limited by %f), jerk=%f\n", nexttc->currentvel, nexttc->currentacc, maxacc_next, nexttc->currentjerk); From 2ddbfca57826919ab9367cdaa0ab5ee23fae7f52 Mon Sep 17 00:00:00 2001 From: MetalMusings Date: Thu, 3 Dec 2026 11:29:31 +0100 Subject: [PATCH 3/3] tp: S-curve follows feed override changes within a segment 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 --- src/emc/tp/tp.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 81 insertions(+), 4 deletions(-) diff --git a/src/emc/tp/tp.c b/src/emc/tp/tp.c index 078c235863a..79983ed9e3b 100644 --- a/src/emc/tp/tp.c +++ b/src/emc/tp/tp.c @@ -2887,6 +2887,19 @@ STATIC int tcUpdateDistFromSCurveAccel(TC_STRUCT *const tc, double acc, double j * acceleration limits. The formula has been tweaked slightly to allow a * non-zero velocity at the instant the target is reached. */ +/** + * Distance of a jerk limited change between two speeds, from and to zero + * acceleration. The profile is symmetric, so it is the mean speed times + * its time. + */ +static double scurveSpeedChangeDist(double v0, double v1, double maxaccel, double maxjerk) +{ + double dv = fabs(v1 - v0); + double t = dv * maxjerk <= maxaccel * maxaccel ? + 2.0 * sqrt(dv / maxjerk) : dv / maxaccel + maxaccel / maxjerk; + return 0.5 * (v0 + v1) * t; +} + int tpCalculateSCurveAccel(TP_STRUCT const * const tp, TC_STRUCT * const tc, TC_STRUCT const * const nexttc, double * const acc, double * const jerk, double * const vel_desired, double * const pos_error, int blend, double * const req_pos, int in_overlap) @@ -2937,6 +2950,38 @@ int tpCalculateSCurveAccel(TP_STRUCT const * const tp, TC_STRUCT * const tc, TC_ // Normal operation parameters double effective_max_vel = tc_target_vel; double effective_target_vel = tc_finalvel; + // speed the velocity control slows to: a stop, or the max velocity below + double velocity_control_target = 0.0; + + /* Above the max velocity, after the feed override was lowered: Ruckig + * position control first brakes into the velocity limit at full jerk, + * arriving there still decelerating, and then has to undershoot and + * come back up. Over the rest of a short segment that takes going + * backwards, which min velocity 0 forbids, so the planning fails and + * the old trajectory runs on at the old speed to the end of the + * segment. Velocity control slows to the max velocity with zero + * acceleration instead; position control takes over from there. + * + * That is where slowing down to the final velocity fits in most of the + * distance left, so that position control can still reach it after. + * Where it does not, slowing down to the final velocity as fast as + * possible is the best there is: position control would fail and run + * the old trajectory on (or, right after velocity control, there is + * none and the segment falls back to a trapezoidal profile). */ + if (!use_velocity_control && tc->currentvel > effective_max_vel + TP_VEL_EPSILON) { + double v_end = fmin(effective_target_vel, effective_max_vel); + // speeding up still: the speed and distance it takes to stop that + double a_up = fmax(tc->currentacc, 0.0); + double t_up = a_up / maxjerk; + double v_top = tc->currentvel + 0.5 * a_up * t_up; + double d_up = t_up * (tc->currentvel + a_up * t_up / 3.0); + use_velocity_control = true; + if (d_up + scurveSpeedChangeDist(v_top, v_end, maxaccel, maxjerk) <= 0.8 * dx) { + velocity_control_target = effective_max_vel; + } else { + velocity_control_target = v_end; + } + } // ========== Ruckig trajectory planning ========== @@ -2987,7 +3032,7 @@ int tpCalculateSCurveAccel(TP_STRUCT const * const tp, TC_STRUCT * const tc, TC_ // Check key parameters (velocity control mode: only check velocity/accel params) int param_changed = (fabs(tc->ruckig_last_maxaccel - maxaccel) > PARAM_EPSILON || fabs(tc->ruckig_last_maxjerk - maxjerk) > PARAM_EPSILON || - fabs(tc->ruckig_last_final_vel - 0.0) > PARAM_EPSILON); + fabs(tc->ruckig_last_final_vel - velocity_control_target) > PARAM_EPSILON); if (param_changed) { need_replan = 1; @@ -3002,8 +3047,8 @@ int tpCalculateSCurveAccel(TP_STRUCT const * const tp, TC_STRUCT * const tc, TC_ int plan_result = ruckig_plan_velocity(tc->ruckig_planner, replan_vel, // current velocity replan_acc, // current acceleration - 0.0, // target velocity (stop) - 0.0, // target acceleration (stop) + velocity_control_target, // target velocity + 0.0, // target acceleration 0.0, // min velocity (unidirectional) maxaccel, // max acceleration maxjerk); // max jerk @@ -3038,7 +3083,7 @@ int tpCalculateSCurveAccel(TP_STRUCT const * const tp, TC_STRUCT * const tc, TC_ tc->ruckig_last_maxaccel = maxaccel; tc->ruckig_last_maxjerk = maxjerk; tc->ruckig_last_target_vel = 0.0; - tc->ruckig_last_final_vel = 0.0; + tc->ruckig_last_final_vel = velocity_control_target; tc->ruckig_last_target_pos = 0.0; tc->ruckig_last_use_velocity_control = 1; tc->ruckig_last_req_pos = 0.0; @@ -3091,6 +3136,29 @@ int tpCalculateSCurveAccel(TP_STRUCT const * const tp, TC_STRUCT * const tc, TC_ maxaccel, // max acceleration maxjerk); // max jerk + /* After the feed override was raised the final velocity can be + * more than this segment has room to speed up to, and Ruckig + * finds no trajectory. Rather than going on at the old speed + * to the end of the segment, speed up to what there is room for + * and leave the rest to the next segment. */ + if (plan_result != 0 && effective_target_vel > replan_vel + && scurveSpeedChangeDist(replan_vel, effective_target_vel, maxaccel, maxjerk) > 0.9 * dx) { + double lo = replan_vel, hi = effective_target_vel; + int i; + for (i = 0; i < 30; i++) { + double mid = 0.5 * (lo + hi); + if (scurveSpeedChangeDist(replan_vel, mid, maxaccel, maxjerk) <= 0.9 * dx) { + lo = mid; + } else { + hi = mid; + } + } + plan_result = ruckig_plan_position(tc->ruckig_planner, + replan_pos, replan_vel, replan_acc, + target_pos, lo, 0.0, 0.0, + effective_max_vel, maxaccel, maxjerk); + } + if (plan_result != 0) { if (tc->ruckig_planned) { // Keep using previous trajectory @@ -3167,6 +3235,10 @@ int tpCalculateSCurveAccel(TP_STRUCT const * const tp, TC_STRUCT * const tc, TC_ return TP_SCURVE_ACCEL_ERROR; } + // end of the time this cycle queried; Ruckig holds the state at the end + // of its trajectory past that + double query_end = tc->ruckig_trajectory_time + tc->cycle_time; + // Update trajectory time tc->ruckig_trajectory_time += tc->cycle_time; @@ -3184,6 +3256,11 @@ int tpCalculateSCurveAccel(TP_STRUCT const * const tp, TC_STRUCT * const tc, TC_ // Velocity control: compute displacement delta from cumulative Ruckig position double ruckig_original_pos = req_pos_value; double displacement_delta = req_pos_value - tc->ruckig_last_req_pos; + // past its end the velocity profile goes on at the speed it reached + // (zero for a stop, the max velocity after slowing down to it) + if (duration > 0.0 && query_end > duration) { + displacement_delta += (query_end - duration) * req_v; + } double disp_sign = (tcGetTarget(tc, TC_DIR_FORWARD) > tcGetTarget(tc, TC_DIR_REVERSE)) ? 1 : -1; req_pos_value = tc->progress + disp_sign * displacement_delta; tc->ruckig_last_req_pos = ruckig_original_pos;