Skip to content

Commit 4b612f5

Browse files
committed
Split first move to planner for better chaining
1 parent cff886b commit 4b612f5

File tree

2 files changed

+112
-57
lines changed

2 files changed

+112
-57
lines changed

Marlin/src/module/planner.cpp

Lines changed: 100 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,7 @@ void Planner::calculate_volumetric_multipliers() {
580580
#if PLANNER_LEVELING
581581
/**
582582
* rx, ry, rz - Cartesian positions in mm
583+
* Leveled XYZ on completion
583584
*/
584585
void Planner::apply_leveling(float &rx, float &ry, float &rz) {
585586

@@ -622,7 +623,7 @@ void Planner::calculate_volumetric_multipliers() {
622623
#endif
623624

624625
rz += (
625-
#if ENABLED(AUTO_BED_LEVELING_UBL)
626+
#if ENABLED(AUTO_BED_LEVELING_UBL) // UBL_DELTA
626627
ubl.get_z_correction(rx, ry) * fade_scaling_factor
627628
#elif ENABLED(MESH_BED_LEVELING)
628629
mbl.get_z(rx, ry
@@ -698,69 +699,35 @@ void Planner::calculate_volumetric_multipliers() {
698699
#endif // PLANNER_LEVELING
699700

700701
/**
701-
* Planner::_buffer_line
702-
*
703-
* Add a new linear movement to the buffer in axis units.
702+
* Planner::_buffer_steps
704703
*
705-
* Leveling and kinematics should be applied ahead of calling this.
704+
* Add a new linear movement to the buffer (in terms of steps).
706705
*
707-
* a,b,c,e - target positions in mm and/or degrees
708-
* fr_mm_s - (target) speed of the move
709-
* extruder - target extruder
706+
* target - target position in steps units
707+
* fr_mm_s - (target) speed of the move
708+
* extruder - target extruder
710709
*/
711-
void Planner::_buffer_line(const float &a, const float &b, const float &c, const float &e, float fr_mm_s, const uint8_t extruder) {
712-
713-
// The target position of the tool in absolute steps
714-
// Calculate target position in absolute steps
715-
//this should be done after the wait, because otherwise a M92 code within the gcode disrupts this calculation somehow
716-
const long target[XYZE] = {
717-
LROUND(a * axis_steps_per_mm[X_AXIS]),
718-
LROUND(b * axis_steps_per_mm[Y_AXIS]),
719-
LROUND(c * axis_steps_per_mm[Z_AXIS]),
720-
LROUND(e * axis_steps_per_mm[E_AXIS_N])
721-
};
722-
723-
// When changing extruders recalculate steps corresponding to the E position
724-
#if ENABLED(DISTINCT_E_FACTORS)
725-
if (last_extruder != extruder && axis_steps_per_mm[E_AXIS_N] != axis_steps_per_mm[E_AXIS + last_extruder]) {
726-
position[E_AXIS] = LROUND(position[E_AXIS] * axis_steps_per_mm[E_AXIS_N] * steps_to_mm[E_AXIS + last_extruder]);
727-
last_extruder = extruder;
728-
}
729-
#endif
710+
void Planner::_buffer_steps(const int32_t (&target)[XYZE], float fr_mm_s, const uint8_t extruder) {
730711

731712
const int32_t da = target[X_AXIS] - position[X_AXIS],
732713
db = target[Y_AXIS] - position[Y_AXIS],
733714
dc = target[Z_AXIS] - position[Z_AXIS];
734715

735-
/*
736-
SERIAL_ECHOPAIR(" Planner FR:", fr_mm_s);
737-
SERIAL_CHAR(' ');
738-
#if IS_KINEMATIC
739-
SERIAL_ECHOPAIR("A:", a);
740-
SERIAL_ECHOPAIR(" (", da);
741-
SERIAL_ECHOPAIR(") B:", b);
742-
#else
743-
SERIAL_ECHOPAIR("X:", a);
716+
int32_t de = target[E_AXIS] - position[E_AXIS];
717+
718+
/* <-- add a slash to enable
719+
SERIAL_ECHOPAIR(" _buffer_steps FR:", fr_mm_s);
720+
SERIAL_ECHOPAIR(" A:", target[A_AXIS]);
744721
SERIAL_ECHOPAIR(" (", da);
745-
SERIAL_ECHOPAIR(") Y:", b);
746-
#endif
747-
SERIAL_ECHOPAIR(" (", db);
748-
#if ENABLED(DELTA)
749-
SERIAL_ECHOPAIR(") C:", c);
750-
#else
751-
SERIAL_ECHOPAIR(") Z:", c);
752-
#endif
753-
SERIAL_ECHOPAIR(" (", dc);
754-
SERIAL_CHAR(')');
755-
SERIAL_EOL();
722+
SERIAL_ECHOPAIR(" steps) B:", target[B_AXIS]);
723+
SERIAL_ECHOPAIR(" (", db);
724+
SERIAL_ECHOPAIR(" steps) C:", target[C_AXIS]);
725+
SERIAL_ECHOPAIR(" (", dc);
726+
SERIAL_ECHOPAIR(" steps) E:", target[E_AXIS]);
727+
SERIAL_ECHOPAIR(" (", de);
728+
SERIAL_ECHOLNPGM(" steps)");
756729
//*/
757730

758-
// DRYRUN ignores all temperature constraints and assures that the extruder is instantly satisfied
759-
if (DEBUGGING(DRYRUN))
760-
position[E_AXIS] = target[E_AXIS];
761-
762-
int32_t de = target[E_AXIS] - position[E_AXIS];
763-
764731
#if ENABLED(PREVENT_COLD_EXTRUSION) || ENABLED(PREVENT_LENGTHY_EXTRUDE)
765732
if (de) {
766733
#if ENABLED(PREVENT_COLD_EXTRUSION)
@@ -1067,6 +1034,7 @@ void Planner::_buffer_line(const float &a, const float &b, const float &c, const
10671034
// Segment time im micro seconds
10681035
uint32_t segment_time_us = LROUND(1000000.0 / inverse_secs);
10691036
#endif
1037+
10701038
#if ENABLED(SLOWDOWN)
10711039
if (WITHIN(moves_queued, 2, (BLOCK_BUFFER_SIZE) / 2 - 1)) {
10721040
if (segment_time_us < min_segment_time_us) {
@@ -1314,12 +1282,12 @@ void Planner::_buffer_line(const float &a, const float &b, const float &c, const
13141282
// Pick the smaller of the nominal speeds. Higher speed shall not be achieved at the junction during coasting.
13151283
vmax_junction = min(block->nominal_speed, previous_nominal_speed);
13161284

1317-
const float smaller_speed_factor = vmax_junction / previous_nominal_speed;
1318-
13191285
// Factor to multiply the previous / current nominal velocities to get componentwise limited velocities.
13201286
float v_factor = 1;
13211287
limited = 0;
1288+
13221289
// Now limit the jerk in all axes.
1290+
const float smaller_speed_factor = vmax_junction / previous_nominal_speed;
13231291
LOOP_XYZE(axis) {
13241292
// Limit an axis. We have to differentiate: coasting, reversal of an axis, full stop.
13251293
float v_exit = previous_speed[axis] * smaller_speed_factor,
@@ -1414,13 +1382,89 @@ void Planner::_buffer_line(const float &a, const float &b, const float &c, const
14141382
block_buffer_head = next_buffer_head;
14151383

14161384
// Update the position (only when a move was queued)
1385+
static_assert(COUNT(target) > 1, "array as function parameter should be declared as reference and with count");
14171386
COPY(position, target);
14181387

14191388
recalculate();
14201389

1390+
} // _buffer_steps()
1391+
1392+
/**
1393+
* Planner::_buffer_line
1394+
*
1395+
* Add a new linear movement to the buffer in axis units.
1396+
*
1397+
* Leveling and kinematics should be applied ahead of calling this.
1398+
*
1399+
* a,b,c,e - target positions in mm and/or degrees
1400+
* fr_mm_s - (target) speed of the move
1401+
* extruder - target extruder
1402+
*/
1403+
void Planner::_buffer_line(const float &a, const float &b, const float &c, const float &e, const float &fr_mm_s, const uint8_t extruder) {
1404+
// When changing extruders recalculate steps corresponding to the E position
1405+
#if ENABLED(DISTINCT_E_FACTORS)
1406+
if (last_extruder != extruder && axis_steps_per_mm[E_AXIS_N] != axis_steps_per_mm[E_AXIS + last_extruder]) {
1407+
position[E_AXIS] = LROUND(position[E_AXIS] * axis_steps_per_mm[E_AXIS_N] * steps_to_mm[E_AXIS + last_extruder]);
1408+
last_extruder = extruder;
1409+
}
1410+
#endif
1411+
1412+
// The target position of the tool in absolute steps
1413+
// Calculate target position in absolute steps
1414+
const int32_t target[XYZE] = {
1415+
LROUND(a * axis_steps_per_mm[X_AXIS]),
1416+
LROUND(b * axis_steps_per_mm[Y_AXIS]),
1417+
LROUND(c * axis_steps_per_mm[Z_AXIS]),
1418+
LROUND(e * axis_steps_per_mm[E_AXIS_N])
1419+
};
1420+
1421+
/* <-- add a slash to enable
1422+
SERIAL_ECHOPAIR(" _buffer_line FR:", fr_mm_s);
1423+
#if IS_KINEMATIC
1424+
SERIAL_ECHOPAIR(" A:", a);
1425+
SERIAL_ECHOPAIR(" (", position[A_AXIS]);
1426+
SERIAL_ECHOPAIR("->", target[A_AXIS]);
1427+
SERIAL_ECHOPAIR(") B:", b);
1428+
#else
1429+
SERIAL_ECHOPAIR(" X:", a);
1430+
SERIAL_ECHOPAIR(" (", position[X_AXIS]);
1431+
SERIAL_ECHOPAIR("->", target[X_AXIS]);
1432+
SERIAL_ECHOPAIR(") Y:", b);
1433+
#endif
1434+
SERIAL_ECHOPAIR(" (", position[Y_AXIS]);
1435+
SERIAL_ECHOPAIR("->", target[Y_AXIS]);
1436+
#if ENABLED(DELTA)
1437+
SERIAL_ECHOPAIR(") C:", c);
1438+
#else
1439+
SERIAL_ECHOPAIR(") Z:", c);
1440+
#endif
1441+
SERIAL_ECHOPAIR(" (", position[Z_AXIS]);
1442+
SERIAL_ECHOPAIR("->", target[Z_AXIS]);
1443+
SERIAL_ECHOPAIR(") E:", e);
1444+
SERIAL_ECHOPAIR(" (", position[E_AXIS]);
1445+
SERIAL_ECHOPAIR("->", target[E_AXIS]);
1446+
SERIAL_ECHOLNPGM(")");
1447+
//*/
1448+
1449+
// DRYRUN ignores all temperature constraints and assures that the extruder is instantly satisfied
1450+
if (DEBUGGING(DRYRUN))
1451+
position[E_AXIS] = target[E_AXIS];
1452+
1453+
// Always split the first move into one longer and one shorter move
1454+
if (!blocks_queued()) {
1455+
#define _BETWEEN(A) (position[A##_AXIS] + target[A##_AXIS]) >> 1
1456+
const int32_t between[XYZE] = { _BETWEEN(X), _BETWEEN(Y), _BETWEEN(Z), _BETWEEN(E) };
1457+
DISABLE_STEPPER_DRIVER_INTERRUPT();
1458+
_buffer_steps(between, fr_mm_s, extruder);
1459+
_buffer_steps(target, fr_mm_s, extruder);
1460+
ENABLE_STEPPER_DRIVER_INTERRUPT();
1461+
}
1462+
else
1463+
_buffer_steps(target, fr_mm_s, extruder);
1464+
14211465
stepper.wake_up();
14221466

1423-
} // buffer_line()
1467+
} // _buffer_line()
14241468

14251469
/**
14261470
* Directly set the planner XYZ position (and stepper positions)

Marlin/src/module/planner.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,17 @@ class Planner {
352352

353353
#endif
354354

355+
/**
356+
* Planner::_buffer_steps
357+
*
358+
* Add a new linear movement to the buffer (in terms of steps).
359+
*
360+
* target - target position in steps units
361+
* fr_mm_s - (target) speed of the move
362+
* extruder - target extruder
363+
*/
364+
static void _buffer_steps(const int32_t (&target)[XYZE], float fr_mm_s, const uint8_t extruder);
365+
355366
/**
356367
* Planner::_buffer_line
357368
*
@@ -363,7 +374,7 @@ class Planner {
363374
* fr_mm_s - (target) speed of the move
364375
* extruder - target extruder
365376
*/
366-
static void _buffer_line(const float &a, const float &b, const float &c, const float &e, float fr_mm_s, const uint8_t extruder);
377+
static void _buffer_line(const float &a, const float &b, const float &c, const float &e, const float &fr_mm_s, const uint8_t extruder);
367378

368379
static void _set_position_mm(const float &a, const float &b, const float &c, const float &e);
369380

0 commit comments

Comments
 (0)