Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 157 additions & 8 deletions src/emc/tp/tp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 ==========

Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -3205,6 +3282,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;
}

Expand Down Expand Up @@ -4175,6 +4263,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
Expand Down Expand Up @@ -4203,6 +4343,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;

Expand Down Expand Up @@ -4321,14 +4465,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);
Expand Down