Skip to content

Commit

Permalink
Update - planner
Browse files Browse the repository at this point in the history
  • Loading branch information
classicrocker883 committed Oct 26, 2024
1 parent d2fa313 commit f17d1a9
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 94 deletions.
95 changes: 51 additions & 44 deletions Marlin/src/module/planner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ uint8_t Planner::delay_before_delivering; // Delay block delivery so initi
#if ENABLED(EDITABLE_STEPS_PER_UNIT)
float Planner::mm_per_step[DISTINCT_AXES]; // (mm) Millimeters per step
#else
constexpr float PlannerSettings::axis_steps_per_mm[DISTINCT_AXES];
constexpr float Planner::mm_per_step[DISTINCT_AXES];
constexpr float PlannerSettings::axis_steps_per_mm[DISTINCT_AXES],
Planner::mm_per_step[DISTINCT_AXES];
#endif
planner_settings_t Planner::settings; // Initialized by settings.load

Expand Down Expand Up @@ -785,9 +785,9 @@ block_t* Planner::get_current_block() {
* sqrt(block->acceleration_steps_per_s2 / 2). This is ensured through
* minimum_planner_speed_sqr / min_entry_speed_sqr though note there's one
* exception in recalculate_trapezoids().
**
*
* ############ VERY IMPORTANT ############
* NOTE that the PRECONDITION to call this function is that the block is
* NOTE: The PRECONDITION to call this function is that the block is
* NOT BUSY and it is marked as RECALCULATE. That WARRANTIES the Stepper ISR
* is not and will not use the block while we modify it.
*/
Expand Down Expand Up @@ -1545,7 +1545,7 @@ void Planner::check_axes_activity() {
else if (z_full_fade >= z_fade_height) // Above the fade height?
raw.z = z_full_fade; // Nothing more to unapply.
else // Within the fade zone?
raw.z = z_no_fade / (1.0f - z_correction * inverse_z_fade_height); // Unapply the faded Z offset
raw.z = z_no_fade / (1.0f - z_correction * inverse_z_fade_height); // Unapply the faded Z offset
#else
raw.z = z_no_fade;
#endif
Expand Down Expand Up @@ -1573,12 +1573,14 @@ void Planner::check_axes_activity() {

void Planner::quick_stop() {

// Remove all the queued blocks.
/// NOTE: this function is NOT called from the Stepper ISR,
// so we must consider tail as readonly!
// That is why we set head to tail - But there is a race condition that
// must be handled: The tail could change between the read and the assignment
// so this must be enclosed in a critical section
/**
* Remove all the queued blocks.
* NOTE: This function is NOT called from the Stepper ISR,
* so we must consider tail as readonly!
* That is why we set head to tail - But there is a race condition that
* must be handled: The tail could change between the read and the assignment
* so this must be enclosed in a critical section
*/

const bool was_enabled = stepper.suspend();

Expand Down Expand Up @@ -1707,9 +1709,9 @@ void Planner::synchronize() { while (busy()) idle(); }
* @param target Target position in steps units
* @param target_float Target position in direct (mm, degrees) units.
* @param cart_dist_mm The pre-calculated move lengths for all axes, in mm
* @param fr_mm_s (target) speed of the move
* @param extruder target extruder
* @param hints parameters to aid planner calculations
* @param fr_mm_s (Target) speed of the move
* @param extruder Target extruder
* @param hints Parameters to aid planner calculations
*
* @return true if movement was properly queued, false otherwise (if cleaning)
*/
Expand Down Expand Up @@ -1769,7 +1771,7 @@ bool Planner::_buffer_steps(const xyze_long_t &target
}

/**
* @brief Populate a block in preparation for insertion
* @brief Populate a block in preparation for insertion.
* @details Populate the fields of a new linear movement block
* that will be added to the queue and processed soon
* by the Stepper ISR.
Expand All @@ -1778,9 +1780,9 @@ bool Planner::_buffer_steps(const xyze_long_t &target
* @param target Target position in steps units
* @param target_float Target position in native mm
* @param cart_dist_mm The pre-calculated move lengths for all axes, in mm
* @param fr_mm_s (target) speed of the move
* @param extruder target extruder
* @param hints parameters to aid planner calculations
* @param fr_mm_s (Target) speed of the move
* @param extruder Target extruder
* @param hints Parameters to aid planner calculations
*
* @return true if movement is acceptable, false otherwise
*/
Expand Down Expand Up @@ -2197,11 +2199,13 @@ bool Planner::_populate_block(
else
NOLESS(fr_mm_s, settings.min_travel_feedrate_mm_s);

const float inverse_millimeters = 1.0f / block->millimeters; // Inverse millimeters to remove multiple divides
const float inverse_millimeters = 1.0f / block->millimeters; // Inverse millimeters to remove multiple divides

// Calculate inverse time for this move. No divide by zero due to previous checks.
/// EXAMPLE: At 120mm/s a 60mm move involving XYZ axes takes 0.5s. So this will give 2.0.
/// EXAMPLE: At 120°/s a 60° move involving only rotational axes takes 0.5s. So this will give 2.0.
/**
* Calculate inverse time for this move. No divide by zero due to previous checks.
* EXAMPLE: At 120mm/s a 60mm move involving XYZ axes takes 0.5s. So this will give 2.0.
* EXAMPLE: At 120°/s a 60° move involving only rotational axes takes 0.5s. So this will give 2.0.
*/
float inverse_secs = inverse_millimeters * (
#if ALL(HAS_ROTATIONAL_AXES, INCH_MODE_SUPPORT)
/**
Expand Down Expand Up @@ -2770,10 +2774,12 @@ bool Planner::_populate_block(
} // _populate_block()

/**
* @brief Add a block to the buffer that just updates the position
* Supports LASER_SYNCHRONOUS_M106_M107 and LASER_POWER_SYNC power sync block buffer queueing.
* @brief Add a block to the buffer that just updates the position.
* @details Supports LASER_SYNCHRONOUS_M106_M107 and LASER_POWER_SYNC power sync block buffer queueing.
*
* @param sync_flag The sync flag to set, determining the type of sync the block will do
* Sets a condition bit to process additional items such as sync fan pwm
* or sync M3/M4 laser power into a queued block
*/
void Planner::buffer_sync_block(const BlockFlagBit sync_flag/*=BLOCK_BIT_SYNC_POSITION*/) {

Expand Down Expand Up @@ -2817,18 +2823,17 @@ void Planner::buffer_sync_block(const BlockFlagBit sync_flag/*=BLOCK_BIT_SYNC_PO
} // buffer_sync_block()

/**
* @brief Add a single linear movement
*
* @description Add a new linear movement to the buffer in axis units.
* Leveling and kinematics should be applied before calling this.
* @brief Add a single linear movement.
* @details Add a new linear movement to the buffer in axis units.
* Leveling and kinematics should be applied before calling this.
*
* @param abce Target position in mm and/or degrees
* @param cart_dist_mm The pre-calculated move lengths for all axes, in mm
* @param fr_mm_s (target) speed of the move
* @param extruder optional target extruder (otherwise active_extruder)
* @param hints optional parameters to aid planner calculations
* @param fr_mm_s (Target) speed of the move
* @param extruder Optional target extruder (otherwise active_extruder)
* @param hints Optional parameters to aid planner calculations
*
* @return false if no segment was queued due to cleaning, cold extrusion, full queue, etc.
* @return false if no segment was queued due to cleaning, cold extrusion, full queue, etc...
*/
bool Planner::buffer_segment(const abce_pos_t &abce
OPTARG(HAS_DIST_MM_ARG, const xyze_float_t &cart_dist_mm),
Expand Down Expand Up @@ -2948,14 +2953,16 @@ bool Planner::buffer_segment(const abce_pos_t &abce
} // buffer_segment()

/**
* Add a new linear movement to the buffer.
* The target is cartesian. It's translated to
* delta/scara if needed.
* @brief Add a new linear movement to the buffer.
* @details The target is cartesian. It's translated to
* delta/scara if needed.
*
* @param cart Target position in mm or degrees
* @param fr_mm_s (Target) speed of the move (mm/s)
* @param extruder Optional target extruder (otherwise active_extruder)
* @param hints Optional parameters to aid planner calculations
*
* cart - target position in mm or degrees
* fr_mm_s - (target) speed of the move (mm/s)
* extruder - optional target extruder (otherwise active_extruder)
* hints - optional parameters to aid planner calculations
* @return false if no segment was queued due to cleaning, cold extrusion, full queue, etc...
*/
bool Planner::buffer_line(const xyze_pos_t &cart, const_feedRate_t fr_mm_s,
const uint8_t extruder/*=active_extruder*/,
Expand Down Expand Up @@ -3237,7 +3244,7 @@ inline void limit_and_warn(float &val, const AxisEnum axis, FSTR_P const setting
}

/**
* For the specified 'axis' set the Maximum Acceleration to the given value (mm/s^2)
* For the specified 'axis' set the Maximum Acceleration to the given value (mm/s^2).
* The value may be limited with warning feedback, if configured.
* Calls refresh_acceleration_rates to precalculate planner terms in steps.
*
Expand All @@ -3261,7 +3268,7 @@ void Planner::set_max_acceleration(const AxisEnum axis, float inMaxAccelMMS2) {
}

/**
* For the specified 'axis' set the Maximum Feedrate to the given value (mm/s)
* For the specified 'axis' set the Maximum Feedrate to the given value (mm/s).
* The value may be limited with warning feedback, if configured.
*
* This hard limit is applied as a block is being added to the planner queue.
Expand All @@ -3283,7 +3290,7 @@ void Planner::set_max_feedrate(const AxisEnum axis, float inMaxFeedrateMMS) {
#if ENABLED(CLASSIC_JERK)

/**
* For the specified 'axis' set the Maximum Jerk (instant change) to the given value (mm/s)
* For the specified 'axis' set the Maximum Jerk (instant change) to the given value (mm/s).
* The value may be limited with warning feedback, if configured.
*
* This hard limit is applied (to the block start speed) as the block is being added to the planner queue.
Expand Down Expand Up @@ -3314,7 +3321,7 @@ void Planner::set_max_feedrate(const AxisEnum axis, float inMaxFeedrateMMS) {
uint16_t Planner::block_buffer_runtime() {
#ifdef __AVR__
// Protect the access to the variable. Only required for AVR, as
// any 32bit CPU offers atomic access to 32bit variables
// any 32bit CPU offers atomic access to 32bit variables
const bool was_enabled = stepper.suspend();
#endif

Expand All @@ -3336,7 +3343,7 @@ void Planner::set_max_feedrate(const AxisEnum axis, float inMaxFeedrateMMS) {
void Planner::clear_block_buffer_runtime() {
#ifdef __AVR__
// Protect the access to the variable. Only required for AVR, as
// any 32bit CPU offers atomic access to 32bit variables
// any 32bit CPU offers atomic access to 32bit variables
const bool was_enabled = stepper.suspend();
#endif

Expand Down
111 changes: 61 additions & 50 deletions Marlin/src/module/planner.h
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,12 @@ typedef struct PlannerSettings {
#undef _DLIM
#endif

feedRate_t max_feedrate_mm_s[DISTINCT_AXES]; // (mm/s) M203 XYZE - Max speeds
float acceleration, // (mm/s^2) M204 S - Normal acceleration. DEFAULT ACCELERATION for all printing moves.
retract_acceleration, // (mm/s^2) M204 R - Retract acceleration. Filament pull-back and push-forward while standing still in the other axes
travel_acceleration; // (mm/s^2) M204 T - Travel acceleration. DEFAULT ACCELERATION for all NON printing moves.
feedRate_t min_feedrate_mm_s, // (mm/s) M205 S - Minimum linear feedrate
min_travel_feedrate_mm_s; // (mm/s) M205 T - Minimum travel feedrate
feedRate_t max_feedrate_mm_s[DISTINCT_AXES]; // (mm/s) M203 XYZE - Max speeds
float acceleration, // (mm/s^2) M204 S - Normal acceleration. DEFAULT ACCELERATION for all printing moves.
retract_acceleration, // (mm/s^2) M204 R - Retract acceleration. Filament pull-back and push-forward while standing still in the other axes
travel_acceleration; // (mm/s^2) M204 T - Travel acceleration. DEFAULT ACCELERATION for all NON printing moves.
feedRate_t min_feedrate_mm_s, // (mm/s) M205 S - Minimum linear feedrate
min_travel_feedrate_mm_s; // (mm/s) M205 T - Minimum travel feedrate
} planner_settings_t;

#if ENABLED(IMPROVE_HOMING_RELIABILITY)
Expand Down Expand Up @@ -812,14 +812,14 @@ class Planner {
FORCE_INLINE static uint8_t moves_free() { return (BLOCK_BUFFER_SIZE) - 1 - movesplanned(); }

/**
* Planner::get_next_free_block
* @fn Planner::get_next_free_block
*
* - Get the next head indices (passed by reference)
* - Wait for the number of spaces to open up in the planner
* - Return the first head block
* @details Get the next head indices (passed by reference)
* Wait for the number of spaces to open up in the planner
*
* @return The first head block
*/
FORCE_INLINE static block_t* get_next_free_block(uint8_t &next_buffer_head, const uint8_t count=1) {

// Wait until there are enough slots free
while (moves_free() < count) { idle(); }

Expand All @@ -829,16 +829,18 @@ class Planner {
}

/**
* Planner::_buffer_steps
* @fn Planner::_buffer_steps
*
* Add a new linear movement to the buffer (in terms of steps).
* @brief Add a new linear movement to the planner queue (in terms of steps).
*
* target - target position in steps units
* fr_mm_s - (target) speed of the move
* extruder - target extruder
* hints - parameters to aid planner calculations
* @param target Target position in steps units
* @param target_float Target position in direct (mm, degrees) units.
* @param cart_dist_mm The pre-calculated move lengths for all axes, in mm
* @param fr_mm_s (Target) speed of the move
* @param extruder Target extruder
* @param hints Parameters to aid planner calculations
*
* Returns true if movement was buffered, false otherwise
* @return true if movement was properly queued, false otherwise (if cleaning)
*/
static bool _buffer_steps(const xyze_long_t &target
OPTARG(HAS_POSITION_FLOAT, const xyze_pos_t &target_float)
Expand All @@ -847,7 +849,9 @@ class Planner {
);

/**
* @brief Populate a block in preparation for insertion
* @fn Planner::_populate_block
*
* @brief Populate a block in preparation for insertion.
* @details Populate the fields of a new linear movement block
* that will be added to the queue and processed soon
* by the Stepper ISR.
Expand All @@ -856,9 +860,9 @@ class Planner {
* @param target Target position in steps units
* @param target_float Target position in native mm
* @param cart_dist_mm The pre-calculated move lengths for all axes, in mm
* @param fr_mm_s (target) speed of the move
* @param extruder target extruder
* @param hints parameters to aid planner calculations
* @param fr_mm_s (Target) speed of the move
* @param extruder Target extruder
* @param hints Parameters to aid planner calculations
*
* @return true if movement is acceptable, false otherwise
*/
Expand All @@ -870,31 +874,31 @@ class Planner {
);

/**
* Planner::buffer_sync_block
* Add a block to the buffer that just updates the position
* @param sync_flag sets a condition bit to process additional items
* such as sync fan pwm or sync M3/M4 laser power into a queued block
* @fn Planner::buffer_sync_block
*
* @brief Add a block to the buffer that just updates the position.
* @details Supports LASER_SYNCHRONOUS_M106_M107 and LASER_POWER_SYNC power sync block buffer queueing.
*
* @param sync_flag The sync flag to set, determining the type of sync the block will do
* Sets a condition bit to process additional items such as sync fan pwm
* or sync M3/M4 laser power into a queued block
*/
static void buffer_sync_block(const BlockFlagBit flag=BLOCK_BIT_SYNC_POSITION);

#if IS_KINEMATIC
private:

// Allow do_homing_move to access internal functions, such as buffer_segment.
friend void do_homing_move(const AxisEnum, const float, const feedRate_t, const bool);
#endif
static void buffer_sync_block(const BlockFlagBit flag=BLOCK_BIT_SYNC_POSITION);

/**
* Planner::buffer_segment
* @fn Planner::buffer_segment
*
* Add a new linear movement to the buffer in axis units.
* @brief Add a single linear movement.
* @details Add a new linear movement to the buffer in axis units.
* Leveling and kinematics should be applied before calling this.
*
* Leveling and kinematics should be applied ahead of calling this.
* @param abce Target position in mm and/or degrees
* @param cart_dist_mm The pre-calculated move lengths for all axes, in mm
* @param fr_mm_s (Target) speed of the move
* @param extruder Optional target extruder (otherwise active_extruder)
* @param hints Optional parameters to aid planner calculations
*
* a,b,c,e - target positions in mm and/or degrees
* fr_mm_s - (target) speed of the move
* extruder - optional target extruder (otherwise active_extruder)
* hints - optional parameters to aid planner calculations
* @return false if no segment was queued due to cleaning, cold extrusion, full queue, etc...
*/
static bool buffer_segment(const abce_pos_t &abce
OPTARG(HAS_DIST_MM_ARG, const xyze_float_t &cart_dist_mm),
Expand All @@ -903,17 +907,19 @@ class Planner {
const PlannerHints &hints=PlannerHints()
);

public:

/**
* Add a new linear movement to the buffer.
* The target is cartesian. It's translated to
* delta/scara if needed.
* @fn Planner::buffer_line
*
* @brief Add a new linear movement to the buffer.
* @details The target is cartesian. It's translated to
* delta/scara if needed.
*
* @param cart Target position in mm or degrees
* @param fr_mm_s (Target) speed of the move (mm/s)
* @param extruder Optional target extruder (otherwise active_extruder)
* @param hints Optional parameters to aid planner calculations
*
* cart - target position in mm or degrees
* fr_mm_s - (target) speed of the move (mm/s)
* extruder - optional target extruder (otherwise active_extruder)
* hints - optional parameters to aid planner calculations
* @return false if no segment was queued due to cleaning, cold extrusion, full queue, etc...
*/
static bool buffer_line(const xyze_pos_t &cart, const_feedRate_t fr_mm_s,
const uint8_t extruder=active_extruder,
Expand Down Expand Up @@ -1091,6 +1097,11 @@ class Planner {

static void recalculate(const_float_t safe_exit_speed_sqr);

#if IS_KINEMATIC
// Allow do_homing_move to access internal functions, such as buffer_segment.
friend void do_homing_move(const AxisEnum, const float, const feedRate_t, const bool);
#endif

#if HAS_JUNCTION_DEVIATION

FORCE_INLINE static void normalize_junction_vector(xyze_float_t &vector) {
Expand Down

0 comments on commit f17d1a9

Please sign in to comment.