My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

planner.h 36KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. /**
  24. * planner.h
  25. *
  26. * Buffer movement commands and manage the acceleration profile plan
  27. *
  28. * Derived from Grbl
  29. * Copyright (c) 2009-2011 Simen Svale Skogsrud
  30. */
  31. #include "../MarlinCore.h"
  32. #if ENABLED(JD_HANDLE_SMALL_SEGMENTS)
  33. // Enable this option for perfect accuracy but maximum
  34. // computation. Should be fine on ARM processors.
  35. //#define JD_USE_MATH_ACOS
  36. // Disable this option to save 120 bytes of PROGMEM,
  37. // but incur increased computation and a reduction
  38. // in accuracy.
  39. #define JD_USE_LOOKUP_TABLE
  40. #endif
  41. #include "motion.h"
  42. #include "../gcode/queue.h"
  43. #if ENABLED(DELTA)
  44. #include "delta.h"
  45. #endif
  46. #if ABL_PLANAR
  47. #include "../libs/vector_3.h" // for matrix_3x3
  48. #endif
  49. #if ENABLED(FWRETRACT)
  50. #include "../feature/fwretract.h"
  51. #endif
  52. #if ENABLED(MIXING_EXTRUDER)
  53. #include "../feature/mixing.h"
  54. #endif
  55. #if HAS_CUTTER
  56. #include "../feature/spindle_laser_types.h"
  57. #endif
  58. #if ENABLED(DIRECT_STEPPING)
  59. #include "../feature/direct_stepping.h"
  60. #define IS_PAGE(B) TEST(B->flag, BLOCK_BIT_IS_PAGE)
  61. #else
  62. #define IS_PAGE(B) false
  63. #endif
  64. // Feedrate for manual moves
  65. #ifdef MANUAL_FEEDRATE
  66. constexpr xyze_feedrate_t _mf = MANUAL_FEEDRATE,
  67. manual_feedrate_mm_s { _mf.x / 60.0f, _mf.y / 60.0f, _mf.z / 60.0f, _mf.e / 60.0f };
  68. #endif
  69. #if IS_KINEMATIC && HAS_JUNCTION_DEVIATION
  70. #define HAS_DIST_MM_ARG 1
  71. #endif
  72. enum BlockFlagBit : char {
  73. // Recalculate trapezoids on entry junction. For optimization.
  74. BLOCK_BIT_RECALCULATE,
  75. // Nominal speed always reached.
  76. // i.e., The segment is long enough, so the nominal speed is reachable if accelerating
  77. // from a safe speed (in consideration of jerking from zero speed).
  78. BLOCK_BIT_NOMINAL_LENGTH,
  79. // The block is segment 2+ of a longer move
  80. BLOCK_BIT_CONTINUED,
  81. // Sync the stepper counts from the block
  82. BLOCK_BIT_SYNC_POSITION
  83. // Direct stepping page
  84. #if ENABLED(DIRECT_STEPPING)
  85. , BLOCK_BIT_IS_PAGE
  86. #endif
  87. // Sync the fan speeds from the block
  88. #if ENABLED(LASER_SYNCHRONOUS_M106_M107)
  89. , BLOCK_BIT_SYNC_FANS
  90. #endif
  91. };
  92. enum BlockFlag : char {
  93. BLOCK_FLAG_RECALCULATE = _BV(BLOCK_BIT_RECALCULATE)
  94. , BLOCK_FLAG_NOMINAL_LENGTH = _BV(BLOCK_BIT_NOMINAL_LENGTH)
  95. , BLOCK_FLAG_CONTINUED = _BV(BLOCK_BIT_CONTINUED)
  96. , BLOCK_FLAG_SYNC_POSITION = _BV(BLOCK_BIT_SYNC_POSITION)
  97. #if ENABLED(DIRECT_STEPPING)
  98. , BLOCK_FLAG_IS_PAGE = _BV(BLOCK_BIT_IS_PAGE)
  99. #endif
  100. #if ENABLED(LASER_SYNCHRONOUS_M106_M107)
  101. , BLOCK_FLAG_SYNC_FANS = _BV(BLOCK_BIT_SYNC_FANS)
  102. #endif
  103. };
  104. #define BLOCK_MASK_SYNC ( BLOCK_FLAG_SYNC_POSITION | TERN0(LASER_SYNCHRONOUS_M106_M107, BLOCK_FLAG_SYNC_FANS) )
  105. #if ENABLED(LASER_POWER_INLINE)
  106. typedef struct {
  107. bool isPlanned:1;
  108. bool isEnabled:1;
  109. bool dir:1;
  110. bool Reserved:6;
  111. } power_status_t;
  112. typedef struct {
  113. power_status_t status; // See planner settings for meaning
  114. uint8_t power; // Ditto; When in trapezoid mode this is nominal power
  115. #if ENABLED(LASER_POWER_INLINE_TRAPEZOID)
  116. uint8_t power_entry; // Entry power for the laser
  117. #if DISABLED(LASER_POWER_INLINE_TRAPEZOID_CONT)
  118. uint8_t power_exit; // Exit power for the laser
  119. uint32_t entry_per, // Steps per power increment (to avoid floats in stepper calcs)
  120. exit_per; // Steps per power decrement
  121. #endif
  122. #endif
  123. } block_laser_t;
  124. #endif
  125. /**
  126. * struct block_t
  127. *
  128. * A single entry in the planner buffer.
  129. * Tracks linear movement over multiple axes.
  130. *
  131. * The "nominal" values are as-specified by gcode, and
  132. * may never actually be reached due to acceleration limits.
  133. */
  134. typedef struct block_t {
  135. volatile uint8_t flag; // Block flags (See BlockFlag enum above) - Modified by ISR and main thread!
  136. // Fields used by the motion planner to manage acceleration
  137. float nominal_speed_sqr, // The nominal speed for this block in (mm/sec)^2
  138. entry_speed_sqr, // Entry speed at previous-current junction in (mm/sec)^2
  139. max_entry_speed_sqr, // Maximum allowable junction entry speed in (mm/sec)^2
  140. millimeters, // The total travel of this block in mm
  141. acceleration; // acceleration mm/sec^2
  142. union {
  143. abce_ulong_t steps; // Step count along each axis
  144. abce_long_t position; // New position to force when this sync block is executed
  145. };
  146. uint32_t step_event_count; // The number of step events required to complete this block
  147. #if HAS_MULTI_EXTRUDER
  148. uint8_t extruder; // The extruder to move (if E move)
  149. #else
  150. static constexpr uint8_t extruder = 0;
  151. #endif
  152. #if ENABLED(MIXING_EXTRUDER)
  153. mixer_comp_t b_color[MIXING_STEPPERS]; // Normalized color for the mixing steppers
  154. #endif
  155. // Settings for the trapezoid generator
  156. uint32_t accelerate_until, // The index of the step event on which to stop acceleration
  157. decelerate_after; // The index of the step event on which to start decelerating
  158. #if ENABLED(S_CURVE_ACCELERATION)
  159. uint32_t cruise_rate, // The actual cruise rate to use, between end of the acceleration phase and start of deceleration phase
  160. acceleration_time, // Acceleration time and deceleration time in STEP timer counts
  161. deceleration_time,
  162. acceleration_time_inverse, // Inverse of acceleration and deceleration periods, expressed as integer. Scale depends on CPU being used
  163. deceleration_time_inverse;
  164. #else
  165. uint32_t acceleration_rate; // The acceleration rate used for acceleration calculation
  166. #endif
  167. uint8_t direction_bits; // The direction bit set for this block (refers to *_DIRECTION_BIT in config.h)
  168. // Advance extrusion
  169. #if ENABLED(LIN_ADVANCE)
  170. bool use_advance_lead;
  171. uint16_t advance_speed, // STEP timer value for extruder speed offset ISR
  172. max_adv_steps, // max. advance steps to get cruising speed pressure (not always nominal_speed!)
  173. final_adv_steps; // advance steps due to exit speed
  174. float e_D_ratio;
  175. #endif
  176. uint32_t nominal_rate, // The nominal step rate for this block in step_events/sec
  177. initial_rate, // The jerk-adjusted step rate at start of block
  178. final_rate, // The minimal rate at exit
  179. acceleration_steps_per_s2; // acceleration steps/sec^2
  180. #if ENABLED(DIRECT_STEPPING)
  181. page_idx_t page_idx; // Page index used for direct stepping
  182. #endif
  183. #if HAS_CUTTER
  184. cutter_power_t cutter_power; // Power level for Spindle, Laser, etc.
  185. #endif
  186. #if HAS_FAN
  187. uint8_t fan_speed[FAN_COUNT];
  188. #endif
  189. #if ENABLED(BARICUDA)
  190. uint8_t valve_pressure, e_to_p_pressure;
  191. #endif
  192. #if HAS_WIRED_LCD
  193. uint32_t segment_time_us;
  194. #endif
  195. #if ENABLED(POWER_LOSS_RECOVERY)
  196. uint32_t sdpos;
  197. #endif
  198. #if ENABLED(LASER_POWER_INLINE)
  199. block_laser_t laser;
  200. #endif
  201. } block_t;
  202. #if ANY(LIN_ADVANCE, SCARA_FEEDRATE_SCALING, GRADIENT_MIX, LCD_SHOW_E_TOTAL)
  203. #define HAS_POSITION_FLOAT 1
  204. #endif
  205. #define BLOCK_MOD(n) ((n)&(BLOCK_BUFFER_SIZE-1))
  206. #if ENABLED(LASER_POWER_INLINE)
  207. typedef struct {
  208. /**
  209. * Laser status flags
  210. */
  211. power_status_t status;
  212. /**
  213. * Laser power: 0 or 255 in case of PWM-less laser,
  214. * or the OCR (oscillator count register) value;
  215. *
  216. * Using OCR instead of raw power, because it avoids
  217. * floating point operations during the move loop.
  218. */
  219. uint8_t power;
  220. } laser_state_t;
  221. #endif
  222. typedef struct {
  223. uint32_t max_acceleration_mm_per_s2[XYZE_N], // (mm/s^2) M201 XYZE
  224. min_segment_time_us; // (µs) M205 B
  225. float axis_steps_per_mm[XYZE_N]; // (steps) M92 XYZE - Steps per millimeter
  226. feedRate_t max_feedrate_mm_s[XYZE_N]; // (mm/s) M203 XYZE - Max speeds
  227. float acceleration, // (mm/s^2) M204 S - Normal acceleration. DEFAULT ACCELERATION for all printing moves.
  228. retract_acceleration, // (mm/s^2) M204 R - Retract acceleration. Filament pull-back and push-forward while standing still in the other axes
  229. travel_acceleration; // (mm/s^2) M204 T - Travel acceleration. DEFAULT ACCELERATION for all NON printing moves.
  230. feedRate_t min_feedrate_mm_s, // (mm/s) M205 S - Minimum linear feedrate
  231. min_travel_feedrate_mm_s; // (mm/s) M205 T - Minimum travel feedrate
  232. } planner_settings_t;
  233. #if DISABLED(SKEW_CORRECTION)
  234. #define XY_SKEW_FACTOR 0
  235. #define XZ_SKEW_FACTOR 0
  236. #define YZ_SKEW_FACTOR 0
  237. #endif
  238. typedef struct {
  239. #if ENABLED(SKEW_CORRECTION_GCODE)
  240. float xy;
  241. #if ENABLED(SKEW_CORRECTION_FOR_Z)
  242. float xz, yz;
  243. #else
  244. const float xz = XZ_SKEW_FACTOR, yz = YZ_SKEW_FACTOR;
  245. #endif
  246. #else
  247. const float xy = XY_SKEW_FACTOR,
  248. xz = XZ_SKEW_FACTOR, yz = YZ_SKEW_FACTOR;
  249. #endif
  250. } skew_factor_t;
  251. #if ENABLED(DISABLE_INACTIVE_EXTRUDER)
  252. typedef IF<(BLOCK_BUFFER_SIZE > 64), uint16_t, uint8_t>::type last_move_t;
  253. #endif
  254. class Planner {
  255. public:
  256. /**
  257. * The move buffer, calculated in stepper steps
  258. *
  259. * block_buffer is a ring buffer...
  260. *
  261. * head,tail : indexes for write,read
  262. * head==tail : the buffer is empty
  263. * head!=tail : blocks are in the buffer
  264. * head==(tail-1)%size : the buffer is full
  265. *
  266. * Writer of head is Planner::buffer_segment().
  267. * Reader of tail is Stepper::isr(). Always consider tail busy / read-only
  268. */
  269. static block_t block_buffer[BLOCK_BUFFER_SIZE];
  270. static volatile uint8_t block_buffer_head, // Index of the next block to be pushed
  271. block_buffer_nonbusy, // Index of the first non busy block
  272. block_buffer_planned, // Index of the optimally planned block
  273. block_buffer_tail; // Index of the busy block, if any
  274. static uint16_t cleaning_buffer_counter; // A counter to disable queuing of blocks
  275. static uint8_t delay_before_delivering; // This counter delays delivery of blocks when queue becomes empty to allow the opportunity of merging blocks
  276. #if ENABLED(DISTINCT_E_FACTORS)
  277. static uint8_t last_extruder; // Respond to extruder change
  278. #endif
  279. #if ENABLED(DIRECT_STEPPING)
  280. static uint32_t last_page_step_rate; // Last page step rate given
  281. static xyze_bool_t last_page_dir; // Last page direction given
  282. #endif
  283. #if EXTRUDERS
  284. static int16_t flow_percentage[EXTRUDERS]; // Extrusion factor for each extruder
  285. static float e_factor[EXTRUDERS]; // The flow percentage and volumetric multiplier combine to scale E movement
  286. #endif
  287. #if DISABLED(NO_VOLUMETRICS)
  288. static float filament_size[EXTRUDERS], // diameter of filament (in millimeters), typically around 1.75 or 2.85, 0 disables the volumetric calculations for the extruder
  289. volumetric_area_nominal, // Nominal cross-sectional area
  290. volumetric_multiplier[EXTRUDERS]; // Reciprocal of cross-sectional area of filament (in mm^2). Pre-calculated to reduce computation in the planner
  291. // May be auto-adjusted by a filament width sensor
  292. #endif
  293. #if ENABLED(VOLUMETRIC_EXTRUDER_LIMIT)
  294. static float volumetric_extruder_limit[EXTRUDERS], // Maximum mm^3/sec the extruder can handle
  295. volumetric_extruder_feedrate_limit[EXTRUDERS]; // Feedrate limit (mm/s) calculated from volume limit
  296. #endif
  297. static planner_settings_t settings;
  298. #if ENABLED(LASER_POWER_INLINE)
  299. static laser_state_t laser_inline;
  300. #endif
  301. static uint32_t max_acceleration_steps_per_s2[XYZE_N]; // (steps/s^2) Derived from mm_per_s2
  302. static float steps_to_mm[XYZE_N]; // Millimeters per step
  303. #if HAS_JUNCTION_DEVIATION
  304. static float junction_deviation_mm; // (mm) M205 J
  305. #if HAS_LINEAR_E_JERK
  306. static float max_e_jerk[DISTINCT_E]; // Calculated from junction_deviation_mm
  307. #endif
  308. #endif
  309. #if HAS_CLASSIC_JERK
  310. // (mm/s^2) M205 XYZ(E) - The largest speed change requiring no acceleration.
  311. static TERN(HAS_LINEAR_E_JERK, xyz_pos_t, xyze_pos_t) max_jerk;
  312. #endif
  313. #if HAS_LEVELING
  314. static bool leveling_active; // Flag that bed leveling is enabled
  315. #if ABL_PLANAR
  316. static matrix_3x3 bed_level_matrix; // Transform to compensate for bed level
  317. #endif
  318. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  319. static float z_fade_height, inverse_z_fade_height;
  320. #endif
  321. #else
  322. static constexpr bool leveling_active = false;
  323. #endif
  324. #if ENABLED(LIN_ADVANCE)
  325. static float extruder_advance_K[EXTRUDERS];
  326. #endif
  327. /**
  328. * The current position of the tool in absolute steps
  329. * Recalculated if any axis_steps_per_mm are changed by gcode
  330. */
  331. static xyze_long_t position;
  332. #if HAS_POSITION_FLOAT
  333. static xyze_pos_t position_float;
  334. #endif
  335. #if IS_KINEMATIC
  336. static xyze_pos_t position_cart;
  337. #endif
  338. static skew_factor_t skew_factor;
  339. #if ENABLED(SD_ABORT_ON_ENDSTOP_HIT)
  340. static bool abort_on_endstop_hit;
  341. #endif
  342. #ifdef XY_FREQUENCY_LIMIT
  343. static int8_t xy_freq_limit_hz; // Minimum XY frequency setting
  344. static float xy_freq_min_speed_factor; // Minimum speed factor setting
  345. static int32_t xy_freq_min_interval_us; // Minimum segment time based on xy_freq_limit_hz
  346. static inline void refresh_frequency_limit() {
  347. //xy_freq_min_interval_us = xy_freq_limit_hz ?: LROUND(1000000.0f / xy_freq_limit_hz);
  348. if (xy_freq_limit_hz)
  349. xy_freq_min_interval_us = LROUND(1000000.0f / xy_freq_limit_hz);
  350. }
  351. static inline void set_min_speed_factor_u8(const uint8_t v255) {
  352. xy_freq_min_speed_factor = float(ui8_to_percent(v255)) / 100;
  353. }
  354. static inline void set_frequency_limit(const uint8_t hz) {
  355. xy_freq_limit_hz = constrain(hz, 0, 100);
  356. refresh_frequency_limit();
  357. }
  358. #endif
  359. private:
  360. /**
  361. * Speed of previous path line segment
  362. */
  363. static xyze_float_t previous_speed;
  364. /**
  365. * Nominal speed of previous path line segment (mm/s)^2
  366. */
  367. static float previous_nominal_speed_sqr;
  368. /**
  369. * Limit where 64bit math is necessary for acceleration calculation
  370. */
  371. static uint32_t cutoff_long;
  372. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  373. static float last_fade_z;
  374. #endif
  375. #if ENABLED(DISABLE_INACTIVE_EXTRUDER)
  376. // Counters to manage disabling inactive extruders
  377. static last_move_t g_uc_extruder_last_move[EXTRUDERS];
  378. #endif
  379. #if HAS_WIRED_LCD
  380. volatile static uint32_t block_buffer_runtime_us; // Theoretical block buffer runtime in µs
  381. #endif
  382. public:
  383. /**
  384. * Instance Methods
  385. */
  386. Planner();
  387. void init();
  388. /**
  389. * Static (class) Methods
  390. */
  391. // Recalculate steps/s^2 accelerations based on mm/s^2 settings
  392. static void reset_acceleration_rates();
  393. /**
  394. * Recalculate 'position' and 'steps_to_mm'.
  395. * Must be called whenever settings.axis_steps_per_mm changes!
  396. */
  397. static void refresh_positioning();
  398. // For an axis set the Maximum Acceleration in mm/s^2
  399. static void set_max_acceleration(const uint8_t axis, float inMaxAccelMMS2);
  400. // For an axis set the Maximum Feedrate in mm/s
  401. static void set_max_feedrate(const uint8_t axis, float inMaxFeedrateMMS);
  402. // For an axis set the Maximum Jerk (instant change) in mm/s
  403. #if HAS_CLASSIC_JERK
  404. static void set_max_jerk(const AxisEnum axis, float inMaxJerkMMS);
  405. #else
  406. static inline void set_max_jerk(const AxisEnum, const float&) {}
  407. #endif
  408. #if EXTRUDERS
  409. FORCE_INLINE static void refresh_e_factor(const uint8_t e) {
  410. e_factor[e] = flow_percentage[e] * 0.01f * TERN(NO_VOLUMETRICS, 1.0f, volumetric_multiplier[e]);
  411. }
  412. static inline void set_flow(const uint8_t e, const int16_t flow) {
  413. flow_percentage[e] = flow;
  414. refresh_e_factor(e);
  415. }
  416. #endif
  417. // Manage fans, paste pressure, etc.
  418. static void check_axes_activity();
  419. // Apply fan speeds
  420. #if HAS_FAN
  421. static void sync_fan_speeds(uint8_t (&fan_speed)[FAN_COUNT]);
  422. #if FAN_KICKSTART_TIME
  423. static void kickstart_fan(uint8_t (&fan_speed)[FAN_COUNT], const millis_t &ms, const uint8_t f);
  424. #else
  425. FORCE_INLINE static void kickstart_fan(uint8_t (&)[FAN_COUNT], const millis_t &, const uint8_t) {}
  426. #endif
  427. #endif
  428. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  429. void apply_filament_width_sensor(const int8_t encoded_ratio);
  430. static inline float volumetric_percent(const bool vol) {
  431. return 100.0f * (vol
  432. ? volumetric_area_nominal / volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM]
  433. : volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM]
  434. );
  435. }
  436. #endif
  437. #if DISABLED(NO_VOLUMETRICS)
  438. // Update multipliers based on new diameter measurements
  439. static void calculate_volumetric_multipliers();
  440. #if ENABLED(VOLUMETRIC_EXTRUDER_LIMIT)
  441. // Update pre calculated extruder feedrate limits based on volumetric values
  442. static void calculate_volumetric_extruder_limit(const uint8_t e);
  443. static void calculate_volumetric_extruder_limits();
  444. #endif
  445. FORCE_INLINE static void set_filament_size(const uint8_t e, const float &v) {
  446. filament_size[e] = v;
  447. if (v > 0) volumetric_area_nominal = CIRCLE_AREA(v * 0.5); //TODO: should it be per extruder
  448. // make sure all extruders have some sane value for the filament size
  449. LOOP_L_N(i, COUNT(filament_size))
  450. if (!filament_size[i]) filament_size[i] = DEFAULT_NOMINAL_FILAMENT_DIA;
  451. }
  452. #endif
  453. #if ENABLED(VOLUMETRIC_EXTRUDER_LIMIT)
  454. FORCE_INLINE static void set_volumetric_extruder_limit(const uint8_t e, const float &v) {
  455. volumetric_extruder_limit[e] = v;
  456. calculate_volumetric_extruder_limit(e);
  457. }
  458. #endif
  459. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  460. /**
  461. * Get the Z leveling fade factor based on the given Z height,
  462. * re-calculating only when needed.
  463. *
  464. * Returns 1.0 if planner.z_fade_height is 0.0.
  465. * Returns 0.0 if Z is past the specified 'Fade Height'.
  466. */
  467. static inline float fade_scaling_factor_for_z(const float &rz) {
  468. static float z_fade_factor = 1;
  469. if (!z_fade_height) return 1;
  470. if (rz >= z_fade_height) return 0;
  471. if (last_fade_z != rz) {
  472. last_fade_z = rz;
  473. z_fade_factor = 1 - rz * inverse_z_fade_height;
  474. }
  475. return z_fade_factor;
  476. }
  477. FORCE_INLINE static void force_fade_recalc() { last_fade_z = -999.999f; }
  478. FORCE_INLINE static void set_z_fade_height(const float &zfh) {
  479. z_fade_height = zfh > 0 ? zfh : 0;
  480. inverse_z_fade_height = RECIPROCAL(z_fade_height);
  481. force_fade_recalc();
  482. }
  483. FORCE_INLINE static bool leveling_active_at_z(const float &rz) {
  484. return !z_fade_height || rz < z_fade_height;
  485. }
  486. #else
  487. FORCE_INLINE static float fade_scaling_factor_for_z(const float&) { return 1; }
  488. FORCE_INLINE static bool leveling_active_at_z(const float&) { return true; }
  489. #endif
  490. #if ENABLED(SKEW_CORRECTION)
  491. FORCE_INLINE static void skew(float &cx, float &cy, const float &cz) {
  492. if (COORDINATE_OKAY(cx, X_MIN_POS + 1, X_MAX_POS) && COORDINATE_OKAY(cy, Y_MIN_POS + 1, Y_MAX_POS)) {
  493. const float sx = cx - cy * skew_factor.xy - cz * (skew_factor.xz - (skew_factor.xy * skew_factor.yz)),
  494. sy = cy - cz * skew_factor.yz;
  495. if (COORDINATE_OKAY(sx, X_MIN_POS, X_MAX_POS) && COORDINATE_OKAY(sy, Y_MIN_POS, Y_MAX_POS)) {
  496. cx = sx; cy = sy;
  497. }
  498. }
  499. }
  500. FORCE_INLINE static void skew(xyz_pos_t &raw) { skew(raw.x, raw.y, raw.z); }
  501. FORCE_INLINE static void unskew(float &cx, float &cy, const float &cz) {
  502. if (COORDINATE_OKAY(cx, X_MIN_POS, X_MAX_POS) && COORDINATE_OKAY(cy, Y_MIN_POS, Y_MAX_POS)) {
  503. const float sx = cx + cy * skew_factor.xy + cz * skew_factor.xz,
  504. sy = cy + cz * skew_factor.yz;
  505. if (COORDINATE_OKAY(sx, X_MIN_POS, X_MAX_POS) && COORDINATE_OKAY(sy, Y_MIN_POS, Y_MAX_POS)) {
  506. cx = sx; cy = sy;
  507. }
  508. }
  509. }
  510. FORCE_INLINE static void unskew(xyz_pos_t &raw) { unskew(raw.x, raw.y, raw.z); }
  511. #endif // SKEW_CORRECTION
  512. #if HAS_LEVELING
  513. /**
  514. * Apply leveling to transform a cartesian position
  515. * as it will be given to the planner and steppers.
  516. */
  517. static void apply_leveling(xyz_pos_t &raw);
  518. static void unapply_leveling(xyz_pos_t &raw);
  519. FORCE_INLINE static void force_unapply_leveling(xyz_pos_t &raw) {
  520. leveling_active = true;
  521. unapply_leveling(raw);
  522. leveling_active = false;
  523. }
  524. #else
  525. FORCE_INLINE static void apply_leveling(xyz_pos_t&) {}
  526. FORCE_INLINE static void unapply_leveling(xyz_pos_t&) {}
  527. #endif
  528. #if ENABLED(FWRETRACT)
  529. static void apply_retract(float &rz, float &e);
  530. FORCE_INLINE static void apply_retract(xyze_pos_t &raw) { apply_retract(raw.z, raw.e); }
  531. static void unapply_retract(float &rz, float &e);
  532. FORCE_INLINE static void unapply_retract(xyze_pos_t &raw) { unapply_retract(raw.z, raw.e); }
  533. #endif
  534. #if HAS_POSITION_MODIFIERS
  535. FORCE_INLINE static void apply_modifiers(xyze_pos_t &pos, bool leveling=ENABLED(PLANNER_LEVELING)) {
  536. TERN_(SKEW_CORRECTION, skew(pos));
  537. if (leveling) apply_leveling(pos);
  538. TERN_(FWRETRACT, apply_retract(pos));
  539. }
  540. FORCE_INLINE static void unapply_modifiers(xyze_pos_t &pos, bool leveling=ENABLED(PLANNER_LEVELING)) {
  541. TERN_(FWRETRACT, unapply_retract(pos));
  542. if (leveling) unapply_leveling(pos);
  543. TERN_(SKEW_CORRECTION, unskew(pos));
  544. }
  545. #endif // HAS_POSITION_MODIFIERS
  546. // Number of moves currently in the planner including the busy block, if any
  547. FORCE_INLINE static uint8_t movesplanned() { return BLOCK_MOD(block_buffer_head - block_buffer_tail); }
  548. // Number of nonbusy moves currently in the planner
  549. FORCE_INLINE static uint8_t nonbusy_movesplanned() { return BLOCK_MOD(block_buffer_head - block_buffer_nonbusy); }
  550. // Remove all blocks from the buffer
  551. FORCE_INLINE static void clear_block_buffer() { block_buffer_nonbusy = block_buffer_planned = block_buffer_head = block_buffer_tail = 0; }
  552. // Check if movement queue is full
  553. FORCE_INLINE static bool is_full() { return block_buffer_tail == next_block_index(block_buffer_head); }
  554. // Get count of movement slots free
  555. FORCE_INLINE static uint8_t moves_free() { return BLOCK_BUFFER_SIZE - 1 - movesplanned(); }
  556. /**
  557. * Planner::get_next_free_block
  558. *
  559. * - Get the next head indices (passed by reference)
  560. * - Wait for the number of spaces to open up in the planner
  561. * - Return the first head block
  562. */
  563. FORCE_INLINE static block_t* get_next_free_block(uint8_t &next_buffer_head, const uint8_t count=1) {
  564. // Wait until there are enough slots free
  565. while (moves_free() < count) { idle(); }
  566. // Return the first available block
  567. next_buffer_head = next_block_index(block_buffer_head);
  568. return &block_buffer[block_buffer_head];
  569. }
  570. /**
  571. * Planner::_buffer_steps
  572. *
  573. * Add a new linear movement to the buffer (in terms of steps).
  574. *
  575. * target - target position in steps units
  576. * fr_mm_s - (target) speed of the move
  577. * extruder - target extruder
  578. * millimeters - the length of the movement, if known
  579. *
  580. * Returns true if movement was buffered, false otherwise
  581. */
  582. static bool _buffer_steps(const xyze_long_t &target
  583. #if HAS_POSITION_FLOAT
  584. , const xyze_pos_t &target_float
  585. #endif
  586. #if HAS_DIST_MM_ARG
  587. , const xyze_float_t &cart_dist_mm
  588. #endif
  589. , feedRate_t fr_mm_s, const uint8_t extruder, const float &millimeters=0.0
  590. );
  591. /**
  592. * Planner::_populate_block
  593. *
  594. * Fills a new linear movement in the block (in terms of steps).
  595. *
  596. * target - target position in steps units
  597. * fr_mm_s - (target) speed of the move
  598. * extruder - target extruder
  599. * millimeters - the length of the movement, if known
  600. *
  601. * Returns true is movement is acceptable, false otherwise
  602. */
  603. static bool _populate_block(block_t * const block, bool split_move,
  604. const xyze_long_t &target
  605. #if HAS_POSITION_FLOAT
  606. , const xyze_pos_t &target_float
  607. #endif
  608. #if HAS_DIST_MM_ARG
  609. , const xyze_float_t &cart_dist_mm
  610. #endif
  611. , feedRate_t fr_mm_s, const uint8_t extruder, const float &millimeters=0.0
  612. );
  613. /**
  614. * Planner::buffer_sync_block
  615. * Add a block to the buffer that just updates the position or in
  616. * case of LASER_SYNCHRONOUS_M106_M107 the fan pwm
  617. */
  618. static void buffer_sync_block(
  619. TERN_(LASER_SYNCHRONOUS_M106_M107, uint8_t sync_flag=BLOCK_FLAG_SYNC_POSITION)
  620. );
  621. #if IS_KINEMATIC
  622. private:
  623. // Allow do_homing_move to access internal functions, such as buffer_segment.
  624. friend void do_homing_move(const AxisEnum, const float, const feedRate_t, const bool);
  625. #endif
  626. /**
  627. * Planner::buffer_segment
  628. *
  629. * Add a new linear movement to the buffer in axis units.
  630. *
  631. * Leveling and kinematics should be applied ahead of calling this.
  632. *
  633. * a,b,c,e - target positions in mm and/or degrees
  634. * fr_mm_s - (target) speed of the move
  635. * extruder - target extruder
  636. * millimeters - the length of the movement, if known
  637. */
  638. static bool buffer_segment(const float &a, const float &b, const float &c, const float &e
  639. #if HAS_DIST_MM_ARG
  640. , const xyze_float_t &cart_dist_mm
  641. #endif
  642. , const feedRate_t &fr_mm_s, const uint8_t extruder, const float &millimeters=0.0
  643. );
  644. FORCE_INLINE static bool buffer_segment(abce_pos_t &abce
  645. #if HAS_DIST_MM_ARG
  646. , const xyze_float_t &cart_dist_mm
  647. #endif
  648. , const feedRate_t &fr_mm_s, const uint8_t extruder, const float &millimeters=0.0
  649. ) {
  650. return buffer_segment(abce.a, abce.b, abce.c, abce.e
  651. #if HAS_DIST_MM_ARG
  652. , cart_dist_mm
  653. #endif
  654. , fr_mm_s, extruder, millimeters);
  655. }
  656. public:
  657. /**
  658. * Add a new linear movement to the buffer.
  659. * The target is cartesian. It's translated to
  660. * delta/scara if needed.
  661. *
  662. * rx,ry,rz,e - target position in mm or degrees
  663. * fr_mm_s - (target) speed of the move (mm/s)
  664. * extruder - target extruder
  665. * millimeters - the length of the movement, if known
  666. * inv_duration - the reciprocal if the duration of the movement, if known (kinematic only if feeedrate scaling is enabled)
  667. */
  668. static bool buffer_line(const float &rx, const float &ry, const float &rz, const float &e, const feedRate_t &fr_mm_s, const uint8_t extruder, const float millimeters=0.0
  669. #if ENABLED(SCARA_FEEDRATE_SCALING)
  670. , const float &inv_duration=0.0
  671. #endif
  672. );
  673. FORCE_INLINE static bool buffer_line(const xyze_pos_t &cart, const feedRate_t &fr_mm_s, const uint8_t extruder, const float millimeters=0.0
  674. #if ENABLED(SCARA_FEEDRATE_SCALING)
  675. , const float &inv_duration=0.0
  676. #endif
  677. ) {
  678. return buffer_line(cart.x, cart.y, cart.z, cart.e, fr_mm_s, extruder, millimeters
  679. #if ENABLED(SCARA_FEEDRATE_SCALING)
  680. , inv_duration
  681. #endif
  682. );
  683. }
  684. #if ENABLED(DIRECT_STEPPING)
  685. static void buffer_page(const page_idx_t page_idx, const uint8_t extruder, const uint16_t num_steps);
  686. #endif
  687. /**
  688. * Set the planner.position and individual stepper positions.
  689. * Used by G92, G28, G29, and other procedures.
  690. *
  691. * The supplied position is in the cartesian coordinate space and is
  692. * translated in to machine space as needed. Modifiers such as leveling
  693. * and skew are also applied.
  694. *
  695. * Multiplies by axis_steps_per_mm[] and does necessary conversion
  696. * for COREXY / COREXZ / COREYZ to set the corresponding stepper positions.
  697. *
  698. * Clears previous speed values.
  699. */
  700. static void set_position_mm(const float &rx, const float &ry, const float &rz, const float &e);
  701. FORCE_INLINE static void set_position_mm(const xyze_pos_t &cart) { set_position_mm(cart.x, cart.y, cart.z, cart.e); }
  702. static void set_e_position_mm(const float &e);
  703. /**
  704. * Set the planner.position and individual stepper positions.
  705. *
  706. * The supplied position is in machine space, and no additional
  707. * conversions are applied.
  708. */
  709. static void set_machine_position_mm(const float &a, const float &b, const float &c, const float &e);
  710. FORCE_INLINE static void set_machine_position_mm(const abce_pos_t &abce) { set_machine_position_mm(abce.a, abce.b, abce.c, abce.e); }
  711. /**
  712. * Get an axis position according to stepper position(s)
  713. * For CORE machines apply translation from ABC to XYZ.
  714. */
  715. static float get_axis_position_mm(const AxisEnum axis);
  716. static inline abce_pos_t get_axis_positions_mm() {
  717. const abce_pos_t out = {
  718. get_axis_position_mm(A_AXIS),
  719. get_axis_position_mm(B_AXIS),
  720. get_axis_position_mm(C_AXIS),
  721. get_axis_position_mm(E_AXIS)
  722. };
  723. return out;
  724. }
  725. // SCARA AB axes are in degrees, not mm
  726. #if IS_SCARA
  727. FORCE_INLINE static float get_axis_position_degrees(const AxisEnum axis) { return get_axis_position_mm(axis); }
  728. #endif
  729. // Called to force a quick stop of the machine (for example, when
  730. // a Full Shutdown is required, or when endstops are hit)
  731. static void quick_stop();
  732. // Called when an endstop is triggered. Causes the machine to stop inmediately
  733. static void endstop_triggered(const AxisEnum axis);
  734. // Triggered position of an axis in mm (not core-savvy)
  735. static float triggered_position_mm(const AxisEnum axis);
  736. // Block until all buffered steps are executed / cleaned
  737. static void synchronize();
  738. // Wait for moves to finish and disable all steppers
  739. static void finish_and_disable();
  740. // Periodic tick to handle cleaning timeouts
  741. // Called from the Temperature ISR at ~1kHz
  742. static void tick() {
  743. if (cleaning_buffer_counter) --cleaning_buffer_counter;
  744. }
  745. /**
  746. * Does the buffer have any blocks queued?
  747. */
  748. FORCE_INLINE static bool has_blocks_queued() { return (block_buffer_head != block_buffer_tail); }
  749. /**
  750. * Get the current block for processing
  751. * and mark the block as busy.
  752. * Return nullptr if the buffer is empty
  753. * or if there is a first-block delay.
  754. *
  755. * WARNING: Called from Stepper ISR context!
  756. */
  757. static block_t* get_current_block();
  758. /**
  759. * "Release" the current block so its slot can be reused.
  760. * Called when the current block is no longer needed.
  761. */
  762. FORCE_INLINE static void release_current_block() {
  763. if (has_blocks_queued())
  764. block_buffer_tail = next_block_index(block_buffer_tail);
  765. }
  766. #if HAS_WIRED_LCD
  767. static uint16_t block_buffer_runtime();
  768. static void clear_block_buffer_runtime();
  769. #endif
  770. #if ENABLED(AUTOTEMP)
  771. static celsius_t autotemp_min, autotemp_max;
  772. static float autotemp_factor;
  773. static bool autotemp_enabled;
  774. static void autotemp_update();
  775. static void autotemp_M104_M109();
  776. static void getHighESpeed();
  777. #endif
  778. #if HAS_LINEAR_E_JERK
  779. FORCE_INLINE static void recalculate_max_e_jerk() {
  780. const float prop = junction_deviation_mm * SQRT(0.5) / (1.0f - SQRT(0.5));
  781. LOOP_L_N(i, EXTRUDERS)
  782. max_e_jerk[E_INDEX_N(i)] = SQRT(prop * settings.max_acceleration_mm_per_s2[E_INDEX_N(i)]);
  783. }
  784. #endif
  785. private:
  786. #if ENABLED(AUTOTEMP)
  787. #if ENABLED(AUTOTEMP_PROPORTIONAL)
  788. static void _autotemp_update_from_hotend();
  789. #else
  790. static inline void _autotemp_update_from_hotend() {}
  791. #endif
  792. #endif
  793. /**
  794. * Get the index of the next / previous block in the ring buffer
  795. */
  796. static constexpr uint8_t next_block_index(const uint8_t block_index) { return BLOCK_MOD(block_index + 1); }
  797. static constexpr uint8_t prev_block_index(const uint8_t block_index) { return BLOCK_MOD(block_index - 1); }
  798. /**
  799. * Calculate the distance (not time) it takes to accelerate
  800. * from initial_rate to target_rate using the given acceleration:
  801. */
  802. static float estimate_acceleration_distance(const float &initial_rate, const float &target_rate, const float &accel) {
  803. if (accel == 0) return 0; // accel was 0, set acceleration distance to 0
  804. return (sq(target_rate) - sq(initial_rate)) / (accel * 2);
  805. }
  806. /**
  807. * Return the point at which you must start braking (at the rate of -'accel') if
  808. * you start at 'initial_rate', accelerate (until reaching the point), and want to end at
  809. * 'final_rate' after traveling 'distance'.
  810. *
  811. * This is used to compute the intersection point between acceleration and deceleration
  812. * in cases where the "trapezoid" has no plateau (i.e., never reaches maximum speed)
  813. */
  814. static float intersection_distance(const float &initial_rate, const float &final_rate, const float &accel, const float &distance) {
  815. if (accel == 0) return 0; // accel was 0, set intersection distance to 0
  816. return (accel * 2 * distance - sq(initial_rate) + sq(final_rate)) / (accel * 4);
  817. }
  818. /**
  819. * Calculate the maximum allowable speed squared at this point, in order
  820. * to reach 'target_velocity_sqr' using 'acceleration' within a given
  821. * 'distance'.
  822. */
  823. static float max_allowable_speed_sqr(const float &accel, const float &target_velocity_sqr, const float &distance) {
  824. return target_velocity_sqr - 2 * accel * distance;
  825. }
  826. #if ENABLED(S_CURVE_ACCELERATION)
  827. /**
  828. * Calculate the speed reached given initial speed, acceleration and distance
  829. */
  830. static float final_speed(const float &initial_velocity, const float &accel, const float &distance) {
  831. return SQRT(sq(initial_velocity) + 2 * accel * distance);
  832. }
  833. #endif
  834. static void calculate_trapezoid_for_block(block_t* const block, const float &entry_factor, const float &exit_factor);
  835. static void reverse_pass_kernel(block_t* const current, const block_t * const next);
  836. static void forward_pass_kernel(const block_t * const previous, block_t* const current, uint8_t block_index);
  837. static void reverse_pass();
  838. static void forward_pass();
  839. static void recalculate_trapezoids();
  840. static void recalculate();
  841. #if HAS_JUNCTION_DEVIATION
  842. FORCE_INLINE static void normalize_junction_vector(xyze_float_t &vector) {
  843. float magnitude_sq = 0;
  844. LOOP_XYZE(idx) if (vector[idx]) magnitude_sq += sq(vector[idx]);
  845. vector *= RSQRT(magnitude_sq);
  846. }
  847. FORCE_INLINE static float limit_value_by_axis_maximum(const float &max_value, xyze_float_t &unit_vec) {
  848. float limit_value = max_value;
  849. LOOP_XYZE(idx) {
  850. if (unit_vec[idx]) {
  851. if (limit_value * ABS(unit_vec[idx]) > settings.max_acceleration_mm_per_s2[idx])
  852. limit_value = ABS(settings.max_acceleration_mm_per_s2[idx] / unit_vec[idx]);
  853. }
  854. }
  855. return limit_value;
  856. }
  857. #endif // !CLASSIC_JERK
  858. };
  859. #define PLANNER_XY_FEEDRATE() _MIN(planner.settings.max_feedrate_mm_s[X_AXIS], planner.settings.max_feedrate_mm_s[Y_AXIS])
  860. extern Planner planner;