123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908 |
- #ifndef PLANNER_H
- #define PLANNER_H
- #include "types.h"
- #include "enum.h"
- #include "Marlin.h"
- #if ABL_PLANAR
- #include "vector_3.h"
- #endif
- enum BlockFlagBit : char {
-
- BLOCK_BIT_RECALCULATE,
-
-
-
- BLOCK_BIT_NOMINAL_LENGTH,
-
- BLOCK_BIT_CONTINUED,
-
- BLOCK_BIT_SYNC_POSITION
- };
- enum BlockFlag : char {
- BLOCK_FLAG_RECALCULATE = _BV(BLOCK_BIT_RECALCULATE),
- BLOCK_FLAG_NOMINAL_LENGTH = _BV(BLOCK_BIT_NOMINAL_LENGTH),
- BLOCK_FLAG_CONTINUED = _BV(BLOCK_BIT_CONTINUED),
- BLOCK_FLAG_SYNC_POSITION = _BV(BLOCK_BIT_SYNC_POSITION)
- };
- typedef struct {
- volatile uint8_t flag;
- #if ENABLED(UNREGISTERED_MOVE_SUPPORT)
- bool count_it;
- #endif
-
- float nominal_speed_sqr,
- entry_speed_sqr,
- max_entry_speed_sqr,
- millimeters,
- acceleration;
- union {
-
- struct {
-
- uint32_t steps[NUM_AXIS];
- };
-
- struct {
- int32_t position[NUM_AXIS];
- };
- };
- uint32_t step_event_count;
- uint8_t active_extruder;
- #if ENABLED(MIXING_EXTRUDER)
- uint32_t mix_steps[MIXING_STEPPERS];
- #endif
-
- uint32_t accelerate_until,
- decelerate_after;
- #if ENABLED(S_CURVE_ACCELERATION)
- uint32_t cruise_rate,
- acceleration_time,
- deceleration_time,
- acceleration_time_inverse,
- deceleration_time_inverse;
- #else
- uint32_t acceleration_rate;
- #endif
- uint8_t direction_bits;
-
- #if ENABLED(LIN_ADVANCE)
- bool use_advance_lead;
- uint16_t advance_speed,
- max_adv_steps,
- final_adv_steps;
- float e_D_ratio;
- #endif
- uint32_t nominal_rate,
- initial_rate,
- final_rate,
- acceleration_steps_per_s2;
- #if FAN_COUNT > 0
- uint16_t fan_speed[FAN_COUNT];
- #endif
- #if ENABLED(BARICUDA)
- uint8_t valve_pressure, e_to_p_pressure;
- #endif
- uint32_t segment_time_us;
- } block_t;
- #define HAS_POSITION_FLOAT (ENABLED(LIN_ADVANCE) || HAS_FEEDRATE_SCALING)
- #define BLOCK_MOD(n) ((n)&(BLOCK_BUFFER_SIZE-1))
- class Planner {
- public:
-
- static block_t block_buffer[BLOCK_BUFFER_SIZE];
- static volatile uint8_t block_buffer_head,
- block_buffer_nonbusy,
- block_buffer_planned,
- block_buffer_tail;
- static uint16_t cleaning_buffer_counter;
- static uint8_t delay_before_delivering;
- #if ENABLED(DISTINCT_E_FACTORS)
- static uint8_t last_extruder;
- #endif
- static int16_t flow_percentage[EXTRUDERS];
- static float e_factor[EXTRUDERS];
- #if DISABLED(NO_VOLUMETRICS)
- static float filament_size[EXTRUDERS],
- volumetric_area_nominal,
- volumetric_multiplier[EXTRUDERS];
-
- #endif
- static uint32_t max_acceleration_mm_per_s2[NUM_AXIS_N],
- max_acceleration_steps_per_s2[NUM_AXIS_N],
- min_segment_time_us;
- static float max_feedrate_mm_s[NUM_AXIS_N],
- axis_steps_per_mm[NUM_AXIS_N],
- steps_to_mm[NUM_AXIS_N],
- min_feedrate_mm_s,
- acceleration,
- retract_acceleration,
- travel_acceleration,
- min_travel_feedrate_mm_s;
- #if ENABLED(JUNCTION_DEVIATION)
- static float junction_deviation_mm;
- #if ENABLED(LIN_ADVANCE)
- #if ENABLED(DISTINCT_E_FACTORS)
- static float max_e_jerk[EXTRUDERS];
- #else
- static float max_e_jerk;
- #endif
- #endif
- #else
- static float max_jerk[NUM_AXIS];
- #endif
- #if ENABLED(LINE_BUILDUP_COMPENSATION_FEATURE)
-
- static float k0[MOV_AXIS],
- k1[MOV_AXIS],
- k2[MOV_AXIS],
- sqrtk1[MOV_AXIS];
- #endif
- #if HAS_LEVELING
- static bool leveling_active;
- #if ABL_PLANAR
- static matrix_3x3 bed_level_matrix;
- #endif
- #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
- static float z_fade_height, inverse_z_fade_height;
- #endif
- #else
- static constexpr bool leveling_active = false;
- #endif
- #if ENABLED(LIN_ADVANCE)
- static float extruder_advance_K;
- #endif
- #if HAS_POSITION_FLOAT
- static float position_float[NUM_AXIS];
- #endif
- #if ENABLED(SKEW_CORRECTION)
- #if ENABLED(SKEW_CORRECTION_GCODE)
- static float xy_skew_factor;
- #else
- static constexpr float xy_skew_factor = XY_SKEW_FACTOR;
- #endif
- #if ENABLED(SKEW_CORRECTION_FOR_Z)
- #if ENABLED(SKEW_CORRECTION_GCODE)
- static float xz_skew_factor, yz_skew_factor;
- #else
- static constexpr float xz_skew_factor = XZ_SKEW_FACTOR, yz_skew_factor = YZ_SKEW_FACTOR;
- #endif
- #else
- static constexpr float xz_skew_factor = 0, yz_skew_factor = 0;
- #endif
- #endif
- #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
- static bool abort_on_endstop_hit;
- #endif
- private:
-
- static int32_t position[NUM_AXIS];
-
- static float previous_speed[NUM_AXIS];
-
- static float previous_nominal_speed_sqr;
-
- static uint32_t cutoff_long;
- #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
- static float last_fade_z;
- #endif
- #if ENABLED(DISABLE_INACTIVE_EXTRUDER)
-
- static uint8_t g_uc_extruder_last_move[EXTRUDERS];
- #endif // DISABLE_INACTIVE_EXTRUDER
- #ifdef XY_FREQUENCY_LIMIT
-
- #define MAX_FREQ_TIME_US (uint32_t)(1000000.0 / XY_FREQUENCY_LIMIT)
-
- static unsigned char old_direction_bits;
-
- static uint32_t axis_segment_time_us[2][3];
- #endif
- #if ENABLED(ULTRA_LCD)
- volatile static uint32_t block_buffer_runtime_us;
- #endif
- public:
-
- Planner();
- void init();
-
- static void reset_acceleration_rates();
- static void refresh_positioning();
- FORCE_INLINE static void refresh_e_factor(const uint8_t e) {
- e_factor[e] = (flow_percentage[e] * 0.01f
- #if DISABLED(NO_VOLUMETRICS)
- * volumetric_multiplier[e]
- #endif
- );
- }
-
- static void check_axes_activity();
-
- static void calculate_volumetric_multipliers();
- #if ENABLED(FILAMENT_WIDTH_SENSOR)
- void calculate_volumetric_for_width_sensor(const int8_t encoded_ratio);
- #endif
- #if DISABLED(NO_VOLUMETRICS)
- FORCE_INLINE static void set_filament_size(const uint8_t e, const float &v) {
- filament_size[e] = v;
-
- for (uint8_t i = 0; i < COUNT(filament_size); i++)
- if (!filament_size[i]) filament_size[i] = DEFAULT_NOMINAL_FILAMENT_DIA;
- }
- #endif
- #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
-
- inline static float fade_scaling_factor_for_z(const float &rz) {
- static float z_fade_factor = 1;
- if (z_fade_height) {
- if (rz >= z_fade_height) return 0;
- if (last_fade_z != rz) {
- last_fade_z = rz;
- z_fade_factor = 1 - rz * inverse_z_fade_height;
- }
- return z_fade_factor;
- }
- return 1;
- }
- FORCE_INLINE static void force_fade_recalc() { last_fade_z = -999.999f; }
- FORCE_INLINE static void set_z_fade_height(const float &zfh) {
- z_fade_height = zfh > 0 ? zfh : 0;
- inverse_z_fade_height = RECIPROCAL(z_fade_height);
- force_fade_recalc();
- }
- FORCE_INLINE static bool leveling_active_at_z(const float &rz) {
- return !z_fade_height || rz < z_fade_height;
- }
- #else
- FORCE_INLINE static float fade_scaling_factor_for_z(const float &rz) {
- UNUSED(rz);
- return 1;
- }
- FORCE_INLINE static bool leveling_active_at_z(const float &rz) { UNUSED(rz); return true; }
- #endif
- #if ENABLED(SKEW_CORRECTION)
- FORCE_INLINE static void skew(float &cx, float &cy, const float &cz) {
- if (WITHIN(cx, X_MIN_POS + 1, X_MAX_POS) && WITHIN(cy, Y_MIN_POS + 1, Y_MAX_POS)) {
- const float sx = cx - cy * xy_skew_factor - cz * (xz_skew_factor - (xy_skew_factor * yz_skew_factor)),
- sy = cy - cz * yz_skew_factor;
- if (WITHIN(sx, X_MIN_POS, X_MAX_POS) && WITHIN(sy, Y_MIN_POS, Y_MAX_POS)) {
- cx = sx; cy = sy;
- }
- }
- }
- FORCE_INLINE static void unskew(float &cx, float &cy, const float &cz) {
- if (WITHIN(cx, X_MIN_POS, X_MAX_POS) && WITHIN(cy, Y_MIN_POS, Y_MAX_POS)) {
- const float sx = cx + cy * xy_skew_factor + cz * xz_skew_factor,
- sy = cy + cz * yz_skew_factor;
- if (WITHIN(sx, X_MIN_POS, X_MAX_POS) && WITHIN(sy, Y_MIN_POS, Y_MAX_POS)) {
- cx = sx; cy = sy;
- }
- }
- }
- #endif // SKEW_CORRECTION
- #if PLANNER_LEVELING || HAS_UBL_AND_CURVES
-
- static void apply_leveling(float &rx, float &ry, float &rz);
- FORCE_INLINE static void apply_leveling(float (&raw)[XYZ]) { apply_leveling(raw[X_AXIS], raw[Y_AXIS], raw[Z_AXIS]); }
- #endif
- #if PLANNER_LEVELING
- #define ARG_X float rx
- #define ARG_Y float ry
- #define ARG_Z float rz
- #if ENABLED(HANGPRINTER)
- #define ARG_E1 float re1
- #endif
- static void unapply_leveling(float raw[XYZ]);
- #else
- #define ARG_X const float &rx
- #define ARG_Y const float &ry
- #define ARG_Z const float &rz
- #if ENABLED(HANGPRINTER)
- #define ARG_E1 const float &re1
- #endif
- #endif
-
- FORCE_INLINE static uint8_t movesplanned() { return BLOCK_MOD(block_buffer_head - block_buffer_tail); }
-
- FORCE_INLINE static uint8_t nonbusy_movesplanned() { return BLOCK_MOD(block_buffer_head - block_buffer_nonbusy); }
-
- FORCE_INLINE static void clear_block_buffer() { block_buffer_nonbusy = block_buffer_planned = block_buffer_head = block_buffer_tail = 0; }
-
- FORCE_INLINE static bool is_full() { return block_buffer_tail == next_block_index(block_buffer_head); }
-
- FORCE_INLINE static uint8_t moves_free() { return BLOCK_BUFFER_SIZE - 1 - movesplanned(); }
-
- FORCE_INLINE static block_t* get_next_free_block(uint8_t &next_buffer_head, const uint8_t count=1) {
-
- while (moves_free() < count) { idle(); }
-
- next_buffer_head = next_block_index(block_buffer_head);
- return &block_buffer[block_buffer_head];
- }
-
- static bool _buffer_steps(const int32_t (&target)[NUM_AXIS]
- #if HAS_POSITION_FLOAT
- , const float (&target_float)[NUM_AXIS]
- #endif
- , float fr_mm_s, const uint8_t extruder, const float &millimeters=0.0
- #if ENABLED(UNREGISTERED_MOVE_SUPPORT)
- , const bool count_it=true
- #endif
- );
-
- static bool _populate_block(block_t * const block, bool split_move,
- const int32_t (&target)[NUM_AXIS]
- #if HAS_POSITION_FLOAT
- , const float (&target_float)[NUM_AXIS]
- #endif
- , float fr_mm_s, const uint8_t extruder, const float &millimeters=0.0
- #if ENABLED(UNREGISTERED_MOVE_SUPPORT)
- , const bool count_it=true
- #endif
- );
-
- static void buffer_sync_block();
-
- static bool buffer_segment(const float &a, const float &b, const float &c,
- #if ENABLED(HANGPRINTER)
- const float &d,
- #endif
- const float &e, const float &fr_mm_s, const uint8_t extruder, const float &millimeters=0.0
- #if ENABLED(UNREGISTERED_MOVE_SUPPORT)
- , bool count_it=true
- #endif
- );
- static void _set_position_mm(const float &a, const float &b, const float &c,
- #if ENABLED(HANGPRINTER)
- const float &d,
- #endif
- const float &e
- );
-
- FORCE_INLINE static bool buffer_line(ARG_X, ARG_Y, ARG_Z,
- #if ENABLED(HANGPRINTER)
- ARG_E1,
- #endif
- const float &e, const float &fr_mm_s, const uint8_t extruder, const float millimeters = 0.0
- ) {
- #if PLANNER_LEVELING && IS_CARTESIAN
- apply_leveling(rx, ry, rz);
- #endif
- return buffer_segment(rx, ry, rz,
- #if ENABLED(HANGPRINTER)
- re1,
- #endif
- e, fr_mm_s, extruder, millimeters
- );
- }
-
- FORCE_INLINE static bool buffer_line_kinematic(const float (&cart)[XYZE], const float &fr_mm_s, const uint8_t extruder, const float millimeters = 0.0) {
- #if PLANNER_LEVELING
- float raw[XYZ] = { cart[X_AXIS], cart[Y_AXIS], cart[Z_AXIS] };
- apply_leveling(raw);
- #else
- const float (&raw)[XYZE] = cart;
- #endif
- #if IS_KINEMATIC
- inverse_kinematics(raw);
- return buffer_segment(
- #if ENABLED(HANGPRINTER)
- line_lengths[A_AXIS], line_lengths[B_AXIS], line_lengths[C_AXIS], line_lengths[D_AXIS]
- #else
- delta[A_AXIS], delta[B_AXIS], delta[C_AXIS]
- #endif
- , cart[E_CART], fr_mm_s, extruder, millimeters
- );
- #else
- return buffer_segment(raw[X_AXIS], raw[Y_AXIS], raw[Z_AXIS], cart[E_CART], fr_mm_s, extruder, millimeters);
- #endif
- }
-
- FORCE_INLINE static void set_position_mm(ARG_X, ARG_Y, ARG_Z,
- #if ENABLED(HANGPRINTER)
- ARG_E1,
- #endif
- const float &e
- ) {
- #if PLANNER_LEVELING && IS_CARTESIAN
- apply_leveling(rx, ry, rz);
- #endif
- _set_position_mm(rx, ry, rz,
- #if ENABLED(HANGPRINTER)
- re1,
- #endif
- e
- );
- }
- static void set_position_mm_kinematic(const float (&cart)[XYZE]);
- static void set_position_mm(const AxisEnum axis, const float &v);
- FORCE_INLINE static void set_z_position_mm(const float &z) { set_position_mm(Z_AXIS, z); }
- FORCE_INLINE static void set_e_position_mm(const float &e) { set_position_mm(E_AXIS, e); }
-
- static float get_axis_position_mm(const AxisEnum axis);
-
- #if IS_SCARA
- FORCE_INLINE static float get_axis_position_degrees(const AxisEnum axis) { return get_axis_position_mm(axis); }
- #endif
-
-
- static void quick_stop();
-
- static void endstop_triggered(const AxisEnum axis);
-
- static float triggered_position_mm(const AxisEnum axis);
-
- static void synchronize();
-
- static void finish_and_disable();
-
-
- static void tick() {
- if (cleaning_buffer_counter) {
- --cleaning_buffer_counter;
- #if ENABLED(SD_FINISHED_STEPPERRELEASE) && defined(SD_FINISHED_RELEASECOMMAND)
- if (!cleaning_buffer_counter) enqueue_and_echo_commands_P(PSTR(SD_FINISHED_RELEASECOMMAND));
- #endif
- }
- }
-
- FORCE_INLINE static bool has_blocks_queued() { return (block_buffer_head != block_buffer_tail); }
-
- static block_t* get_current_block() {
-
- const uint8_t nr_moves = movesplanned();
-
- if (nr_moves) {
-
- if (delay_before_delivering) {
- --delay_before_delivering;
-
-
- if (nr_moves < 3 && delay_before_delivering) return NULL;
- delay_before_delivering = 0;
- }
-
- block_t * const block = &block_buffer[block_buffer_tail];
-
- if (TEST(block->flag, BLOCK_BIT_RECALCULATE)) return NULL;
- #if ENABLED(ULTRA_LCD)
- block_buffer_runtime_us -= block->segment_time_us;
- #endif
-
- block_buffer_nonbusy = next_block_index(block_buffer_tail);
-
- if (block_buffer_tail == block_buffer_planned)
- block_buffer_planned = block_buffer_nonbusy;
-
- return block;
- }
-
- #if ENABLED(ULTRA_LCD)
- clear_block_buffer_runtime();
- #endif
- return NULL;
- }
-
- FORCE_INLINE static void discard_current_block() {
- if (has_blocks_queued())
- block_buffer_tail = next_block_index(block_buffer_tail);
- }
- #if ENABLED(ULTRA_LCD)
- static uint16_t block_buffer_runtime() {
- bool was_enabled = STEPPER_ISR_ENABLED();
- if (was_enabled) DISABLE_STEPPER_DRIVER_INTERRUPT();
- millis_t bbru = block_buffer_runtime_us;
- if (was_enabled) ENABLE_STEPPER_DRIVER_INTERRUPT();
-
-
-
- bbru >>= 10;
-
- NOMORE(bbru, 0xFFFFul);
- return bbru;
- }
- static void clear_block_buffer_runtime() {
- bool was_enabled = STEPPER_ISR_ENABLED();
- if (was_enabled) DISABLE_STEPPER_DRIVER_INTERRUPT();
- block_buffer_runtime_us = 0;
- if (was_enabled) ENABLE_STEPPER_DRIVER_INTERRUPT();
- }
- #endif
- #if ENABLED(AUTOTEMP)
- static float autotemp_min, autotemp_max, autotemp_factor;
- static bool autotemp_enabled;
- static void getHighESpeed();
- static void autotemp_M104_M109();
- #endif
- #if ENABLED(JUNCTION_DEVIATION)
- FORCE_INLINE static void recalculate_max_e_jerk() {
- #define GET_MAX_E_JERK(N) SQRT(SQRT(0.5) * junction_deviation_mm * (N) * RECIPROCAL(1.0 - SQRT(0.5)))
- #if ENABLED(LIN_ADVANCE)
- #if ENABLED(DISTINCT_E_FACTORS)
- for (uint8_t i = 0; i < EXTRUDERS; i++)
- max_e_jerk[i] = GET_MAX_E_JERK(max_acceleration_mm_per_s2[E_AXIS + i]);
- #else
- max_e_jerk = GET_MAX_E_JERK(max_acceleration_mm_per_s2[E_AXIS]);
- #endif
- #endif
- }
- #endif
- private:
-
- static constexpr uint8_t next_block_index(const uint8_t block_index) { return BLOCK_MOD(block_index + 1); }
- static constexpr uint8_t prev_block_index(const uint8_t block_index) { return BLOCK_MOD(block_index - 1); }
-
- static float estimate_acceleration_distance(const float &initial_rate, const float &target_rate, const float &accel) {
- if (accel == 0) return 0;
- return (sq(target_rate) - sq(initial_rate)) / (accel * 2);
- }
-
- static float intersection_distance(const float &initial_rate, const float &final_rate, const float &accel, const float &distance) {
- if (accel == 0) return 0;
- return (accel * 2 * distance - sq(initial_rate) + sq(final_rate)) / (accel * 4);
- }
-
- static float max_allowable_speed_sqr(const float &accel, const float &target_velocity_sqr, const float &distance) {
- return target_velocity_sqr - 2 * accel * distance;
- }
- #if ENABLED(S_CURVE_ACCELERATION)
-
- static float final_speed(const float &initial_velocity, const float &accel, const float &distance) {
- return SQRT(sq(initial_velocity) + 2 * accel * distance);
- }
- #endif
- static void calculate_trapezoid_for_block(block_t* const block, const float &entry_factor, const float &exit_factor);
- static void reverse_pass_kernel(block_t* const current, const block_t * const next);
- static void forward_pass_kernel(const block_t * const previous, block_t* const current, uint8_t block_index);
- static void reverse_pass();
- static void forward_pass();
- static void recalculate_trapezoids();
- static void recalculate();
- #if ENABLED(JUNCTION_DEVIATION)
- FORCE_INLINE static void normalize_junction_vector(float (&vector)[XYZE]) {
- float magnitude_sq = 0;
- LOOP_XYZE(idx) if (vector[idx]) magnitude_sq += sq(vector[idx]);
- const float inv_magnitude = RSQRT(magnitude_sq);
- LOOP_XYZE(idx) vector[idx] *= inv_magnitude;
- }
- FORCE_INLINE static float limit_value_by_axis_maximum(const float &max_value, float (&unit_vec)[XYZE]) {
- float limit_value = max_value;
- LOOP_XYZE(idx) if (unit_vec[idx])
- NOMORE(limit_value, ABS(max_acceleration_mm_per_s2[idx] / unit_vec[idx]));
- return limit_value;
- }
- #endif // JUNCTION_DEVIATION
- };
- #define PLANNER_XY_FEEDRATE() (MIN(planner.max_feedrate_mm_s[X_AXIS], planner.max_feedrate_mm_s[Y_AXIS]))
- extern Planner planner;
- #endif // PLANNER_H
|