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.cpp 57KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * planner.cpp
  24. *
  25. * Buffer movement commands and manage the acceleration profile plan
  26. *
  27. * Derived from Grbl
  28. * Copyright (c) 2009-2011 Simen Svale Skogsrud
  29. *
  30. * The ring buffer implementation gleaned from the wiring_serial library by David A. Mellis.
  31. *
  32. *
  33. * Reasoning behind the mathematics in this module (in the key of 'Mathematica'):
  34. *
  35. * s == speed, a == acceleration, t == time, d == distance
  36. *
  37. * Basic definitions:
  38. * Speed[s_, a_, t_] := s + (a*t)
  39. * Travel[s_, a_, t_] := Integrate[Speed[s, a, t], t]
  40. *
  41. * Distance to reach a specific speed with a constant acceleration:
  42. * Solve[{Speed[s, a, t] == m, Travel[s, a, t] == d}, d, t]
  43. * d -> (m^2 - s^2)/(2 a) --> estimate_acceleration_distance()
  44. *
  45. * Speed after a given distance of travel with constant acceleration:
  46. * Solve[{Speed[s, a, t] == m, Travel[s, a, t] == d}, m, t]
  47. * m -> Sqrt[2 a d + s^2]
  48. *
  49. * DestinationSpeed[s_, a_, d_] := Sqrt[2 a d + s^2]
  50. *
  51. * When to start braking (di) to reach a specified destination speed (s2) after accelerating
  52. * from initial speed s1 without ever stopping at a plateau:
  53. * Solve[{DestinationSpeed[s1, a, di] == DestinationSpeed[s2, a, d - di]}, di]
  54. * di -> (2 a d - s1^2 + s2^2)/(4 a) --> intersection_distance()
  55. *
  56. * IntersectionDistance[s1_, s2_, a_, d_] := (2 a d - s1^2 + s2^2)/(4 a)
  57. *
  58. */
  59. #include "planner.h"
  60. #include "stepper.h"
  61. #include "motion.h"
  62. #include "../module/temperature.h"
  63. #include "../lcd/ultralcd.h"
  64. #include "../core/language.h"
  65. #include "../gcode/parser.h"
  66. #include "../Marlin.h"
  67. #if HAS_LEVELING
  68. #include "../feature/bedlevel/bedlevel.h"
  69. #endif
  70. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  71. #include "../feature/filwidth.h"
  72. #endif
  73. #if ENABLED(BARICUDA)
  74. #include "../feature/baricuda.h"
  75. #endif
  76. #if ENABLED(MIXING_EXTRUDER)
  77. #include "../feature/mixing.h"
  78. #endif
  79. Planner planner;
  80. // public:
  81. /**
  82. * A ring buffer of moves described in steps
  83. */
  84. block_t Planner::block_buffer[BLOCK_BUFFER_SIZE];
  85. volatile uint8_t Planner::block_buffer_head = 0, // Index of the next block to be pushed
  86. Planner::block_buffer_tail = 0;
  87. float Planner::max_feedrate_mm_s[XYZE_N], // Max speeds in mm per second
  88. Planner::axis_steps_per_mm[XYZE_N],
  89. Planner::steps_to_mm[XYZE_N];
  90. #if ENABLED(DISTINCT_E_FACTORS)
  91. uint8_t Planner::last_extruder = 0; // Respond to extruder change
  92. #endif
  93. int16_t Planner::flow_percentage[EXTRUDERS] = ARRAY_BY_EXTRUDERS1(100); // Extrusion factor for each extruder
  94. float Planner::e_factor[EXTRUDERS], // The flow percentage and volumetric multiplier combine to scale E movement
  95. Planner::filament_size[EXTRUDERS], // diameter of filament (in millimeters), typically around 1.75 or 2.85, 0 disables the volumetric calculations for the extruder
  96. Planner::volumetric_area_nominal = CIRCLE_AREA((DEFAULT_NOMINAL_FILAMENT_DIA) * 0.5), // Nominal cross-sectional area
  97. Planner::volumetric_multiplier[EXTRUDERS]; // Reciprocal of cross-sectional area of filament (in mm^2). Pre-calculated to reduce computation in the planner
  98. uint32_t Planner::max_acceleration_steps_per_s2[XYZE_N],
  99. Planner::max_acceleration_mm_per_s2[XYZE_N]; // Use M201 to override by software
  100. uint32_t Planner::min_segment_time_us;
  101. // Initialized by settings.load()
  102. float Planner::min_feedrate_mm_s,
  103. Planner::acceleration, // Normal acceleration mm/s^2 DEFAULT ACCELERATION for all printing moves. M204 SXXXX
  104. Planner::retract_acceleration, // Retract acceleration mm/s^2 filament pull-back and push-forward while standing still in the other axes M204 TXXXX
  105. Planner::travel_acceleration, // Travel acceleration mm/s^2 DEFAULT ACCELERATION for all NON printing moves. M204 MXXXX
  106. Planner::max_jerk[XYZE], // The largest speed change requiring no acceleration
  107. Planner::min_travel_feedrate_mm_s;
  108. #if HAS_LEVELING
  109. bool Planner::leveling_active = false; // Flag that auto bed leveling is enabled
  110. #if ABL_PLANAR
  111. matrix_3x3 Planner::bed_level_matrix; // Transform to compensate for bed level
  112. #endif
  113. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  114. float Planner::z_fade_height, // Initialized by settings.load()
  115. Planner::inverse_z_fade_height,
  116. Planner::last_fade_z;
  117. #endif
  118. #endif
  119. #if ENABLED(SKEW_CORRECTION)
  120. #if ENABLED(SKEW_CORRECTION_GCODE)
  121. // Initialized by settings.load()
  122. float Planner::xy_skew_factor;
  123. #if ENABLED(SKEW_CORRECTION_FOR_Z)
  124. float Planner::xz_skew_factor, Planner::yz_skew_factor;
  125. #else
  126. constexpr float Planner::xz_skew_factor, Planner::yz_skew_factor;
  127. #endif
  128. #else
  129. constexpr float Planner::xy_skew_factor, Planner::xz_skew_factor, Planner::yz_skew_factor;
  130. #endif
  131. #endif
  132. #if ENABLED(AUTOTEMP)
  133. float Planner::autotemp_max = 250,
  134. Planner::autotemp_min = 210,
  135. Planner::autotemp_factor = 0.1;
  136. bool Planner::autotemp_enabled = false;
  137. #endif
  138. // private:
  139. int32_t Planner::position[NUM_AXIS] = { 0 };
  140. uint32_t Planner::cutoff_long;
  141. float Planner::previous_speed[NUM_AXIS],
  142. Planner::previous_nominal_speed;
  143. #if ENABLED(DISABLE_INACTIVE_EXTRUDER)
  144. uint8_t Planner::g_uc_extruder_last_move[EXTRUDERS] = { 0 };
  145. #endif
  146. #ifdef XY_FREQUENCY_LIMIT
  147. // Old direction bits. Used for speed calculations
  148. unsigned char Planner::old_direction_bits = 0;
  149. // Segment times (in µs). Used for speed calculations
  150. uint32_t Planner::axis_segment_time_us[2][3] = { { MAX_FREQ_TIME_US + 1, 0, 0 }, { MAX_FREQ_TIME_US + 1, 0, 0 } };
  151. #endif
  152. #if ENABLED(LIN_ADVANCE)
  153. float Planner::extruder_advance_k, // Initialized by settings.load()
  154. Planner::advance_ed_ratio; // Initialized by settings.load()
  155. #endif
  156. #if ENABLED(ULTRA_LCD)
  157. volatile uint32_t Planner::block_buffer_runtime_us = 0;
  158. #endif
  159. /**
  160. * Class and Instance Methods
  161. */
  162. Planner::Planner() { init(); }
  163. void Planner::init() {
  164. block_buffer_head = block_buffer_tail = 0;
  165. ZERO(position);
  166. ZERO(previous_speed);
  167. previous_nominal_speed = 0.0;
  168. #if ABL_PLANAR
  169. bed_level_matrix.set_to_identity();
  170. #endif
  171. }
  172. #define MINIMAL_STEP_RATE 120
  173. /**
  174. * Calculate trapezoid parameters, multiplying the entry- and exit-speeds
  175. * by the provided factors.
  176. */
  177. void Planner::calculate_trapezoid_for_block(block_t* const block, const float &entry_factor, const float &exit_factor) {
  178. uint32_t initial_rate = CEIL(block->nominal_rate * entry_factor),
  179. final_rate = CEIL(block->nominal_rate * exit_factor); // (steps per second)
  180. // Limit minimal step rate (Otherwise the timer will overflow.)
  181. NOLESS(initial_rate, MINIMAL_STEP_RATE);
  182. NOLESS(final_rate, MINIMAL_STEP_RATE);
  183. const int32_t accel = block->acceleration_steps_per_s2;
  184. // Steps required for acceleration, deceleration to/from nominal rate
  185. int32_t accelerate_steps = CEIL(estimate_acceleration_distance(initial_rate, block->nominal_rate, accel)),
  186. decelerate_steps = FLOOR(estimate_acceleration_distance(block->nominal_rate, final_rate, -accel)),
  187. // Steps between acceleration and deceleration, if any
  188. plateau_steps = block->step_event_count - accelerate_steps - decelerate_steps;
  189. // Does accelerate_steps + decelerate_steps exceed step_event_count?
  190. // Then we can't possibly reach the nominal rate, there will be no cruising.
  191. // Use intersection_distance() to calculate accel / braking time in order to
  192. // reach the final_rate exactly at the end of this block.
  193. if (plateau_steps < 0) {
  194. accelerate_steps = CEIL(intersection_distance(initial_rate, final_rate, accel, block->step_event_count));
  195. NOLESS(accelerate_steps, 0); // Check limits due to numerical round-off
  196. accelerate_steps = min((uint32_t)accelerate_steps, block->step_event_count);//(We can cast here to unsigned, because the above line ensures that we are above zero)
  197. plateau_steps = 0;
  198. }
  199. // block->accelerate_until = accelerate_steps;
  200. // block->decelerate_after = accelerate_steps+plateau_steps;
  201. CRITICAL_SECTION_START; // Fill variables used by the stepper in a critical section
  202. if (!TEST(block->flag, BLOCK_BIT_BUSY)) { // Don't update variables if block is busy.
  203. block->accelerate_until = accelerate_steps;
  204. block->decelerate_after = accelerate_steps + plateau_steps;
  205. block->initial_rate = initial_rate;
  206. block->final_rate = final_rate;
  207. }
  208. CRITICAL_SECTION_END;
  209. }
  210. // "Junction jerk" in this context is the immediate change in speed at the junction of two blocks.
  211. // This method will calculate the junction jerk as the euclidean distance between the nominal
  212. // velocities of the respective blocks.
  213. //inline float junction_jerk(block_t *before, block_t *after) {
  214. // return SQRT(
  215. // POW((before->speed_x-after->speed_x), 2)+POW((before->speed_y-after->speed_y), 2));
  216. //}
  217. // The kernel called by recalculate() when scanning the plan from last to first entry.
  218. void Planner::reverse_pass_kernel(block_t* const current, const block_t *next) {
  219. if (!current || !next) return;
  220. // If entry speed is already at the maximum entry speed, no need to recheck. Block is cruising.
  221. // If not, block in state of acceleration or deceleration. Reset entry speed to maximum and
  222. // check for maximum allowable speed reductions to ensure maximum possible planned speed.
  223. float max_entry_speed = current->max_entry_speed;
  224. if (current->entry_speed != max_entry_speed) {
  225. // If nominal length true, max junction speed is guaranteed to be reached. Only compute
  226. // for max allowable speed if block is decelerating and nominal length is false.
  227. current->entry_speed = (TEST(current->flag, BLOCK_BIT_NOMINAL_LENGTH) || max_entry_speed <= next->entry_speed)
  228. ? max_entry_speed
  229. : min(max_entry_speed, max_allowable_speed(-current->acceleration, next->entry_speed, current->millimeters));
  230. SBI(current->flag, BLOCK_BIT_RECALCULATE);
  231. }
  232. }
  233. /**
  234. * recalculate() needs to go over the current plan twice.
  235. * Once in reverse and once forward. This implements the reverse pass.
  236. */
  237. void Planner::reverse_pass() {
  238. if (movesplanned() > 3) {
  239. block_t* block[3] = { NULL, NULL, NULL };
  240. // Make a local copy of block_buffer_tail, because the interrupt can alter it
  241. // Is a critical section REALLY needed for a single byte change?
  242. //CRITICAL_SECTION_START;
  243. uint8_t tail = block_buffer_tail;
  244. //CRITICAL_SECTION_END
  245. uint8_t b = BLOCK_MOD(block_buffer_head - 3);
  246. while (b != tail) {
  247. if (block[0] && TEST(block[0]->flag, BLOCK_BIT_START_FROM_FULL_HALT)) break;
  248. b = prev_block_index(b);
  249. block[2] = block[1];
  250. block[1] = block[0];
  251. block[0] = &block_buffer[b];
  252. reverse_pass_kernel(block[1], block[2]);
  253. }
  254. }
  255. }
  256. // The kernel called by recalculate() when scanning the plan from first to last entry.
  257. void Planner::forward_pass_kernel(const block_t* previous, block_t* const current) {
  258. if (!previous) return;
  259. // If the previous block is an acceleration block, but it is not long enough to complete the
  260. // full speed change within the block, we need to adjust the entry speed accordingly. Entry
  261. // speeds have already been reset, maximized, and reverse planned by reverse planner.
  262. // If nominal length is true, max junction speed is guaranteed to be reached. No need to recheck.
  263. if (!TEST(previous->flag, BLOCK_BIT_NOMINAL_LENGTH)) {
  264. if (previous->entry_speed < current->entry_speed) {
  265. float entry_speed = min(current->entry_speed,
  266. max_allowable_speed(-previous->acceleration, previous->entry_speed, previous->millimeters));
  267. // Check for junction speed change
  268. if (current->entry_speed != entry_speed) {
  269. current->entry_speed = entry_speed;
  270. SBI(current->flag, BLOCK_BIT_RECALCULATE);
  271. }
  272. }
  273. }
  274. }
  275. /**
  276. * recalculate() needs to go over the current plan twice.
  277. * Once in reverse and once forward. This implements the forward pass.
  278. */
  279. void Planner::forward_pass() {
  280. block_t* block[3] = { NULL, NULL, NULL };
  281. for (uint8_t b = block_buffer_tail; b != block_buffer_head; b = next_block_index(b)) {
  282. block[0] = block[1];
  283. block[1] = block[2];
  284. block[2] = &block_buffer[b];
  285. forward_pass_kernel(block[0], block[1]);
  286. }
  287. forward_pass_kernel(block[1], block[2]);
  288. }
  289. /**
  290. * Recalculate the trapezoid speed profiles for all blocks in the plan
  291. * according to the entry_factor for each junction. Must be called by
  292. * recalculate() after updating the blocks.
  293. */
  294. void Planner::recalculate_trapezoids() {
  295. int8_t block_index = block_buffer_tail;
  296. block_t *current, *next = NULL;
  297. while (block_index != block_buffer_head) {
  298. current = next;
  299. next = &block_buffer[block_index];
  300. if (current) {
  301. // Recalculate if current block entry or exit junction speed has changed.
  302. if (TEST(current->flag, BLOCK_BIT_RECALCULATE) || TEST(next->flag, BLOCK_BIT_RECALCULATE)) {
  303. // NOTE: Entry and exit factors always > 0 by all previous logic operations.
  304. float nom = current->nominal_speed;
  305. calculate_trapezoid_for_block(current, current->entry_speed / nom, next->entry_speed / nom);
  306. CBI(current->flag, BLOCK_BIT_RECALCULATE); // Reset current only to ensure next trapezoid is computed
  307. }
  308. }
  309. block_index = next_block_index(block_index);
  310. }
  311. // Last/newest block in buffer. Exit speed is set with MINIMUM_PLANNER_SPEED. Always recalculated.
  312. if (next) {
  313. float nom = next->nominal_speed;
  314. calculate_trapezoid_for_block(next, next->entry_speed / nom, (MINIMUM_PLANNER_SPEED) / nom);
  315. CBI(next->flag, BLOCK_BIT_RECALCULATE);
  316. }
  317. }
  318. /*
  319. * Recalculate the motion plan according to the following algorithm:
  320. *
  321. * 1. Go over every block in reverse order...
  322. *
  323. * Calculate a junction speed reduction (block_t.entry_factor) so:
  324. *
  325. * a. The junction jerk is within the set limit, and
  326. *
  327. * b. No speed reduction within one block requires faster
  328. * deceleration than the one, true constant acceleration.
  329. *
  330. * 2. Go over every block in chronological order...
  331. *
  332. * Dial down junction speed reduction values if:
  333. * a. The speed increase within one block would require faster
  334. * acceleration than the one, true constant acceleration.
  335. *
  336. * After that, all blocks will have an entry_factor allowing all speed changes to
  337. * be performed using only the one, true constant acceleration, and where no junction
  338. * jerk is jerkier than the set limit, Jerky. Finally it will:
  339. *
  340. * 3. Recalculate "trapezoids" for all blocks.
  341. */
  342. void Planner::recalculate() {
  343. reverse_pass();
  344. forward_pass();
  345. recalculate_trapezoids();
  346. }
  347. #if ENABLED(AUTOTEMP)
  348. void Planner::getHighESpeed() {
  349. static float oldt = 0;
  350. if (!autotemp_enabled) return;
  351. if (thermalManager.degTargetHotend(0) + 2 < autotemp_min) return; // probably temperature set to zero.
  352. float high = 0.0;
  353. for (uint8_t b = block_buffer_tail; b != block_buffer_head; b = next_block_index(b)) {
  354. block_t* block = &block_buffer[b];
  355. if (block->steps[X_AXIS] || block->steps[Y_AXIS] || block->steps[Z_AXIS]) {
  356. float se = (float)block->steps[E_AXIS] / block->step_event_count * block->nominal_speed; // mm/sec;
  357. NOLESS(high, se);
  358. }
  359. }
  360. float t = autotemp_min + high * autotemp_factor;
  361. t = constrain(t, autotemp_min, autotemp_max);
  362. if (t < oldt) t = t * (1 - (AUTOTEMP_OLDWEIGHT)) + oldt * (AUTOTEMP_OLDWEIGHT);
  363. oldt = t;
  364. thermalManager.setTargetHotend(t, 0);
  365. }
  366. #endif // AUTOTEMP
  367. /**
  368. * Maintain fans, paste extruder pressure,
  369. */
  370. void Planner::check_axes_activity() {
  371. unsigned char axis_active[NUM_AXIS] = { 0 },
  372. tail_fan_speed[FAN_COUNT];
  373. #if ENABLED(BARICUDA)
  374. #if HAS_HEATER_1
  375. uint8_t tail_valve_pressure;
  376. #endif
  377. #if HAS_HEATER_2
  378. uint8_t tail_e_to_p_pressure;
  379. #endif
  380. #endif
  381. if (blocks_queued()) {
  382. #if FAN_COUNT > 0
  383. for (uint8_t i = 0; i < FAN_COUNT; i++)
  384. tail_fan_speed[i] = block_buffer[block_buffer_tail].fan_speed[i];
  385. #endif
  386. block_t* block;
  387. #if ENABLED(BARICUDA)
  388. block = &block_buffer[block_buffer_tail];
  389. #if HAS_HEATER_1
  390. tail_valve_pressure = block->valve_pressure;
  391. #endif
  392. #if HAS_HEATER_2
  393. tail_e_to_p_pressure = block->e_to_p_pressure;
  394. #endif
  395. #endif
  396. for (uint8_t b = block_buffer_tail; b != block_buffer_head; b = next_block_index(b)) {
  397. block = &block_buffer[b];
  398. LOOP_XYZE(i) if (block->steps[i]) axis_active[i]++;
  399. }
  400. }
  401. else {
  402. #if FAN_COUNT > 0
  403. for (uint8_t i = 0; i < FAN_COUNT; i++) tail_fan_speed[i] = fanSpeeds[i];
  404. #endif
  405. #if ENABLED(BARICUDA)
  406. #if HAS_HEATER_1
  407. tail_valve_pressure = baricuda_valve_pressure;
  408. #endif
  409. #if HAS_HEATER_2
  410. tail_e_to_p_pressure = baricuda_e_to_p_pressure;
  411. #endif
  412. #endif
  413. }
  414. #if ENABLED(DISABLE_X)
  415. if (!axis_active[X_AXIS]) disable_X();
  416. #endif
  417. #if ENABLED(DISABLE_Y)
  418. if (!axis_active[Y_AXIS]) disable_Y();
  419. #endif
  420. #if ENABLED(DISABLE_Z)
  421. if (!axis_active[Z_AXIS]) disable_Z();
  422. #endif
  423. #if ENABLED(DISABLE_E)
  424. if (!axis_active[E_AXIS]) disable_e_steppers();
  425. #endif
  426. #if FAN_COUNT > 0
  427. #if FAN_KICKSTART_TIME > 0
  428. static millis_t fan_kick_end[FAN_COUNT] = { 0 };
  429. #define KICKSTART_FAN(f) \
  430. if (tail_fan_speed[f]) { \
  431. millis_t ms = millis(); \
  432. if (fan_kick_end[f] == 0) { \
  433. fan_kick_end[f] = ms + FAN_KICKSTART_TIME; \
  434. tail_fan_speed[f] = 255; \
  435. } else if (PENDING(ms, fan_kick_end[f])) \
  436. tail_fan_speed[f] = 255; \
  437. } else fan_kick_end[f] = 0
  438. #if HAS_FAN0
  439. KICKSTART_FAN(0);
  440. #endif
  441. #if HAS_FAN1
  442. KICKSTART_FAN(1);
  443. #endif
  444. #if HAS_FAN2
  445. KICKSTART_FAN(2);
  446. #endif
  447. #endif // FAN_KICKSTART_TIME > 0
  448. #ifdef FAN_MIN_PWM
  449. #define CALC_FAN_SPEED(f) (tail_fan_speed[f] ? ( FAN_MIN_PWM + (tail_fan_speed[f] * (255 - FAN_MIN_PWM)) / 255 ) : 0)
  450. #else
  451. #define CALC_FAN_SPEED(f) tail_fan_speed[f]
  452. #endif
  453. #if ENABLED(FAN_SOFT_PWM)
  454. #if HAS_FAN0
  455. thermalManager.soft_pwm_amount_fan[0] = CALC_FAN_SPEED(0);
  456. #endif
  457. #if HAS_FAN1
  458. thermalManager.soft_pwm_amount_fan[1] = CALC_FAN_SPEED(1);
  459. #endif
  460. #if HAS_FAN2
  461. thermalManager.soft_pwm_amount_fan[2] = CALC_FAN_SPEED(2);
  462. #endif
  463. #else
  464. #if HAS_FAN0
  465. analogWrite(FAN_PIN, CALC_FAN_SPEED(0));
  466. #endif
  467. #if HAS_FAN1
  468. analogWrite(FAN1_PIN, CALC_FAN_SPEED(1));
  469. #endif
  470. #if HAS_FAN2
  471. analogWrite(FAN2_PIN, CALC_FAN_SPEED(2));
  472. #endif
  473. #endif
  474. #endif // FAN_COUNT > 0
  475. #if ENABLED(AUTOTEMP)
  476. getHighESpeed();
  477. #endif
  478. #if ENABLED(BARICUDA)
  479. #if HAS_HEATER_1
  480. analogWrite(HEATER_1_PIN, tail_valve_pressure);
  481. #endif
  482. #if HAS_HEATER_2
  483. analogWrite(HEATER_2_PIN, tail_e_to_p_pressure);
  484. #endif
  485. #endif
  486. }
  487. inline float calculate_volumetric_multiplier(const float &diameter) {
  488. return (parser.volumetric_enabled && diameter) ? 1.0 / CIRCLE_AREA(diameter * 0.5) : 1.0;
  489. }
  490. void Planner::calculate_volumetric_multipliers() {
  491. for (uint8_t i = 0; i < COUNT(filament_size); i++) {
  492. volumetric_multiplier[i] = calculate_volumetric_multiplier(filament_size[i]);
  493. refresh_e_factor(i);
  494. }
  495. }
  496. #if PLANNER_LEVELING
  497. /**
  498. * rx, ry, rz - Cartesian positions in mm
  499. * Leveled XYZ on completion
  500. */
  501. void Planner::apply_leveling(float &rx, float &ry, float &rz) {
  502. #if ENABLED(SKEW_CORRECTION)
  503. if (WITHIN(rx, X_MIN_POS + 1, X_MAX_POS) && WITHIN(ry, Y_MIN_POS + 1, Y_MAX_POS)) {
  504. const float tempry = ry - (rz * planner.yz_skew_factor),
  505. temprx = rx - (ry * planner.xy_skew_factor) - (rz * (planner.xz_skew_factor - (planner.xy_skew_factor * planner.yz_skew_factor)));
  506. if (WITHIN(temprx, X_MIN_POS, X_MAX_POS) && WITHIN(tempry, Y_MIN_POS, Y_MAX_POS)) {
  507. rx = temprx;
  508. ry = tempry;
  509. }
  510. else
  511. SERIAL_ECHOLN(MSG_SKEW_WARN);
  512. }
  513. #endif
  514. if (!leveling_active) return;
  515. #if ABL_PLANAR
  516. float dx = rx - (X_TILT_FULCRUM),
  517. dy = ry - (Y_TILT_FULCRUM);
  518. apply_rotation_xyz(bed_level_matrix, dx, dy, rz);
  519. rx = dx + X_TILT_FULCRUM;
  520. ry = dy + Y_TILT_FULCRUM;
  521. #else
  522. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  523. const float fade_scaling_factor = fade_scaling_factor_for_z(rz);
  524. if (!fade_scaling_factor) return;
  525. #elif HAS_MESH
  526. constexpr float fade_scaling_factor = 1.0;
  527. #endif
  528. #if ENABLED(AUTO_BED_LEVELING_BILINEAR)
  529. const float raw[XYZ] = { rx, ry, 0 };
  530. #endif
  531. rz += (
  532. #if ENABLED(AUTO_BED_LEVELING_UBL) // UBL_DELTA
  533. ubl.get_z_correction(rx, ry) * fade_scaling_factor
  534. #elif ENABLED(MESH_BED_LEVELING)
  535. mbl.get_z(rx, ry
  536. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  537. , fade_scaling_factor
  538. #endif
  539. )
  540. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  541. bilinear_z_offset(raw) * fade_scaling_factor
  542. #else
  543. 0
  544. #endif
  545. );
  546. #endif
  547. }
  548. void Planner::unapply_leveling(float raw[XYZ]) {
  549. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  550. const float fade_scaling_factor = fade_scaling_factor_for_z(raw[Z_AXIS]);
  551. #else
  552. constexpr float fade_scaling_factor = 1.0;
  553. #endif
  554. if (leveling_active && fade_scaling_factor) {
  555. #if ABL_PLANAR
  556. matrix_3x3 inverse = matrix_3x3::transpose(bed_level_matrix);
  557. float dx = raw[X_AXIS] - (X_TILT_FULCRUM),
  558. dy = raw[Y_AXIS] - (Y_TILT_FULCRUM);
  559. apply_rotation_xyz(inverse, dx, dy, raw[Z_AXIS]);
  560. raw[X_AXIS] = dx + X_TILT_FULCRUM;
  561. raw[Y_AXIS] = dy + Y_TILT_FULCRUM;
  562. #else // !ABL_PLANAR
  563. raw[Z_AXIS] -= (
  564. #if ENABLED(AUTO_BED_LEVELING_UBL)
  565. ubl.get_z_correction(raw[X_AXIS], raw[Y_AXIS]) * fade_scaling_factor
  566. #elif ENABLED(MESH_BED_LEVELING)
  567. mbl.get_z(raw[X_AXIS], raw[Y_AXIS]
  568. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  569. , fade_scaling_factor
  570. #endif
  571. )
  572. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  573. bilinear_z_offset(raw) * fade_scaling_factor
  574. #else
  575. 0
  576. #endif
  577. );
  578. #endif // !ABL_PLANAR
  579. }
  580. #if ENABLED(SKEW_CORRECTION)
  581. if (WITHIN(raw[X_AXIS], X_MIN_POS, X_MAX_POS) && WITHIN(raw[Y_AXIS], Y_MIN_POS, Y_MAX_POS)) {
  582. const float temprx = raw[X_AXIS] + raw[Y_AXIS] * planner.xy_skew_factor + raw[Z_AXIS] * planner.xz_skew_factor,
  583. tempry = raw[Y_AXIS] + raw[Z_AXIS] * planner.yz_skew_factor;
  584. if (WITHIN(temprx, X_MIN_POS, X_MAX_POS) && WITHIN(tempry, Y_MIN_POS, Y_MAX_POS)) {
  585. raw[X_AXIS] = temprx;
  586. raw[Y_AXIS] = tempry;
  587. }
  588. }
  589. #endif
  590. }
  591. #endif // PLANNER_LEVELING
  592. /**
  593. * Planner::_buffer_steps
  594. *
  595. * Add a new linear movement to the buffer (in terms of steps).
  596. *
  597. * target - target position in steps units
  598. * fr_mm_s - (target) speed of the move
  599. * extruder - target extruder
  600. */
  601. void Planner::_buffer_steps(const int32_t (&target)[XYZE], float fr_mm_s, const uint8_t extruder) {
  602. const int32_t da = target[X_AXIS] - position[X_AXIS],
  603. db = target[Y_AXIS] - position[Y_AXIS],
  604. dc = target[Z_AXIS] - position[Z_AXIS];
  605. int32_t de = target[E_AXIS] - position[E_AXIS];
  606. /* <-- add a slash to enable
  607. SERIAL_ECHOPAIR(" _buffer_steps FR:", fr_mm_s);
  608. SERIAL_ECHOPAIR(" A:", target[A_AXIS]);
  609. SERIAL_ECHOPAIR(" (", da);
  610. SERIAL_ECHOPAIR(" steps) B:", target[B_AXIS]);
  611. SERIAL_ECHOPAIR(" (", db);
  612. SERIAL_ECHOPAIR(" steps) C:", target[C_AXIS]);
  613. SERIAL_ECHOPAIR(" (", dc);
  614. SERIAL_ECHOPAIR(" steps) E:", target[E_AXIS]);
  615. SERIAL_ECHOPAIR(" (", de);
  616. SERIAL_ECHOLNPGM(" steps)");
  617. //*/
  618. #if ENABLED(PREVENT_COLD_EXTRUSION) || ENABLED(PREVENT_LENGTHY_EXTRUDE)
  619. if (de) {
  620. #if ENABLED(PREVENT_COLD_EXTRUSION)
  621. if (thermalManager.tooColdToExtrude(extruder)) {
  622. position[E_AXIS] = target[E_AXIS]; // Behave as if the move really took place, but ignore E part
  623. de = 0; // no difference
  624. SERIAL_ECHO_START();
  625. SERIAL_ECHOLNPGM(MSG_ERR_COLD_EXTRUDE_STOP);
  626. }
  627. #endif // PREVENT_COLD_EXTRUSION
  628. #if ENABLED(PREVENT_LENGTHY_EXTRUDE)
  629. if (labs(de * e_factor[extruder]) > (int32_t)axis_steps_per_mm[E_AXIS_N] * (EXTRUDE_MAXLENGTH)) { // It's not important to get max. extrusion length in a precision < 1mm, so save some cycles and cast to int
  630. position[E_AXIS] = target[E_AXIS]; // Behave as if the move really took place, but ignore E part
  631. de = 0; // no difference
  632. SERIAL_ECHO_START();
  633. SERIAL_ECHOLNPGM(MSG_ERR_LONG_EXTRUDE_STOP);
  634. }
  635. #endif // PREVENT_LENGTHY_EXTRUDE
  636. }
  637. #endif // PREVENT_COLD_EXTRUSION || PREVENT_LENGTHY_EXTRUDE
  638. // Compute direction bit-mask for this block
  639. uint8_t dm = 0;
  640. #if CORE_IS_XY
  641. if (da < 0) SBI(dm, X_HEAD); // Save the real Extruder (head) direction in X Axis
  642. if (db < 0) SBI(dm, Y_HEAD); // ...and Y
  643. if (dc < 0) SBI(dm, Z_AXIS);
  644. if (da + db < 0) SBI(dm, A_AXIS); // Motor A direction
  645. if (CORESIGN(da - db) < 0) SBI(dm, B_AXIS); // Motor B direction
  646. #elif CORE_IS_XZ
  647. if (da < 0) SBI(dm, X_HEAD); // Save the real Extruder (head) direction in X Axis
  648. if (db < 0) SBI(dm, Y_AXIS);
  649. if (dc < 0) SBI(dm, Z_HEAD); // ...and Z
  650. if (da + dc < 0) SBI(dm, A_AXIS); // Motor A direction
  651. if (CORESIGN(da - dc) < 0) SBI(dm, C_AXIS); // Motor C direction
  652. #elif CORE_IS_YZ
  653. if (da < 0) SBI(dm, X_AXIS);
  654. if (db < 0) SBI(dm, Y_HEAD); // Save the real Extruder (head) direction in Y Axis
  655. if (dc < 0) SBI(dm, Z_HEAD); // ...and Z
  656. if (db + dc < 0) SBI(dm, B_AXIS); // Motor B direction
  657. if (CORESIGN(db - dc) < 0) SBI(dm, C_AXIS); // Motor C direction
  658. #else
  659. if (da < 0) SBI(dm, X_AXIS);
  660. if (db < 0) SBI(dm, Y_AXIS);
  661. if (dc < 0) SBI(dm, Z_AXIS);
  662. #endif
  663. if (de < 0) SBI(dm, E_AXIS);
  664. const float esteps_float = de * e_factor[extruder];
  665. const int32_t esteps = abs(esteps_float) + 0.5;
  666. // Calculate the buffer head after we push this byte
  667. const uint8_t next_buffer_head = next_block_index(block_buffer_head);
  668. // If the buffer is full: good! That means we are well ahead of the robot.
  669. // Rest here until there is room in the buffer.
  670. while (block_buffer_tail == next_buffer_head) idle();
  671. // Prepare to set up new block
  672. block_t* block = &block_buffer[block_buffer_head];
  673. // Clear all flags, including the "busy" bit
  674. block->flag = 0;
  675. // Set direction bits
  676. block->direction_bits = dm;
  677. // Number of steps for each axis
  678. // See http://www.corexy.com/theory.html
  679. #if CORE_IS_XY
  680. block->steps[A_AXIS] = labs(da + db);
  681. block->steps[B_AXIS] = labs(da - db);
  682. block->steps[Z_AXIS] = labs(dc);
  683. #elif CORE_IS_XZ
  684. block->steps[A_AXIS] = labs(da + dc);
  685. block->steps[Y_AXIS] = labs(db);
  686. block->steps[C_AXIS] = labs(da - dc);
  687. #elif CORE_IS_YZ
  688. block->steps[X_AXIS] = labs(da);
  689. block->steps[B_AXIS] = labs(db + dc);
  690. block->steps[C_AXIS] = labs(db - dc);
  691. #else
  692. // default non-h-bot planning
  693. block->steps[X_AXIS] = labs(da);
  694. block->steps[Y_AXIS] = labs(db);
  695. block->steps[Z_AXIS] = labs(dc);
  696. #endif
  697. block->steps[E_AXIS] = esteps;
  698. block->step_event_count = MAX4(block->steps[X_AXIS], block->steps[Y_AXIS], block->steps[Z_AXIS], esteps);
  699. // Bail if this is a zero-length block
  700. if (block->step_event_count < MIN_STEPS_PER_SEGMENT) return;
  701. // For a mixing extruder, get a magnified step_event_count for each
  702. #if ENABLED(MIXING_EXTRUDER)
  703. for (uint8_t i = 0; i < MIXING_STEPPERS; i++)
  704. block->mix_event_count[i] = mixing_factor[i] * block->step_event_count;
  705. #endif
  706. #if FAN_COUNT > 0
  707. for (uint8_t i = 0; i < FAN_COUNT; i++) block->fan_speed[i] = fanSpeeds[i];
  708. #endif
  709. #if ENABLED(BARICUDA)
  710. block->valve_pressure = baricuda_valve_pressure;
  711. block->e_to_p_pressure = baricuda_e_to_p_pressure;
  712. #endif
  713. block->active_extruder = extruder;
  714. //enable active axes
  715. #if CORE_IS_XY
  716. if (block->steps[A_AXIS] || block->steps[B_AXIS]) {
  717. enable_X();
  718. enable_Y();
  719. }
  720. #if DISABLED(Z_LATE_ENABLE)
  721. if (block->steps[Z_AXIS]) enable_Z();
  722. #endif
  723. #elif CORE_IS_XZ
  724. if (block->steps[A_AXIS] || block->steps[C_AXIS]) {
  725. enable_X();
  726. enable_Z();
  727. }
  728. if (block->steps[Y_AXIS]) enable_Y();
  729. #elif CORE_IS_YZ
  730. if (block->steps[B_AXIS] || block->steps[C_AXIS]) {
  731. enable_Y();
  732. enable_Z();
  733. }
  734. if (block->steps[X_AXIS]) enable_X();
  735. #else
  736. if (block->steps[X_AXIS]) enable_X();
  737. if (block->steps[Y_AXIS]) enable_Y();
  738. #if DISABLED(Z_LATE_ENABLE)
  739. if (block->steps[Z_AXIS]) enable_Z();
  740. #endif
  741. #endif
  742. // Enable extruder(s)
  743. if (esteps) {
  744. #if ENABLED(DISABLE_INACTIVE_EXTRUDER) // Enable only the selected extruder
  745. #define DISABLE_IDLE_E(N) if (!g_uc_extruder_last_move[N]) disable_E##N();
  746. for (uint8_t i = 0; i < EXTRUDERS; i++)
  747. if (g_uc_extruder_last_move[i] > 0) g_uc_extruder_last_move[i]--;
  748. switch(extruder) {
  749. case 0:
  750. enable_E0();
  751. g_uc_extruder_last_move[0] = (BLOCK_BUFFER_SIZE) * 2;
  752. #if ENABLED(DUAL_X_CARRIAGE) || ENABLED(DUAL_NOZZLE_DUPLICATION_MODE)
  753. if (extruder_duplication_enabled) {
  754. enable_E1();
  755. g_uc_extruder_last_move[1] = (BLOCK_BUFFER_SIZE) * 2;
  756. }
  757. #endif
  758. #if EXTRUDERS > 1
  759. DISABLE_IDLE_E(1);
  760. #if EXTRUDERS > 2
  761. DISABLE_IDLE_E(2);
  762. #if EXTRUDERS > 3
  763. DISABLE_IDLE_E(3);
  764. #if EXTRUDERS > 4
  765. DISABLE_IDLE_E(4);
  766. #endif // EXTRUDERS > 4
  767. #endif // EXTRUDERS > 3
  768. #endif // EXTRUDERS > 2
  769. #endif // EXTRUDERS > 1
  770. break;
  771. #if EXTRUDERS > 1
  772. case 1:
  773. enable_E1();
  774. g_uc_extruder_last_move[1] = (BLOCK_BUFFER_SIZE) * 2;
  775. DISABLE_IDLE_E(0);
  776. #if EXTRUDERS > 2
  777. DISABLE_IDLE_E(2);
  778. #if EXTRUDERS > 3
  779. DISABLE_IDLE_E(3);
  780. #if EXTRUDERS > 4
  781. DISABLE_IDLE_E(4);
  782. #endif // EXTRUDERS > 4
  783. #endif // EXTRUDERS > 3
  784. #endif // EXTRUDERS > 2
  785. break;
  786. #if EXTRUDERS > 2
  787. case 2:
  788. enable_E2();
  789. g_uc_extruder_last_move[2] = (BLOCK_BUFFER_SIZE) * 2;
  790. DISABLE_IDLE_E(0);
  791. DISABLE_IDLE_E(1);
  792. #if EXTRUDERS > 3
  793. DISABLE_IDLE_E(3);
  794. #if EXTRUDERS > 4
  795. DISABLE_IDLE_E(4);
  796. #endif
  797. #endif
  798. break;
  799. #if EXTRUDERS > 3
  800. case 3:
  801. enable_E3();
  802. g_uc_extruder_last_move[3] = (BLOCK_BUFFER_SIZE) * 2;
  803. DISABLE_IDLE_E(0);
  804. DISABLE_IDLE_E(1);
  805. DISABLE_IDLE_E(2);
  806. #if EXTRUDERS > 4
  807. DISABLE_IDLE_E(4);
  808. #endif
  809. break;
  810. #if EXTRUDERS > 4
  811. case 4:
  812. enable_E4();
  813. g_uc_extruder_last_move[4] = (BLOCK_BUFFER_SIZE) * 2;
  814. DISABLE_IDLE_E(0);
  815. DISABLE_IDLE_E(1);
  816. DISABLE_IDLE_E(2);
  817. DISABLE_IDLE_E(3);
  818. break;
  819. #endif // EXTRUDERS > 4
  820. #endif // EXTRUDERS > 3
  821. #endif // EXTRUDERS > 2
  822. #endif // EXTRUDERS > 1
  823. }
  824. #else
  825. enable_E0();
  826. enable_E1();
  827. enable_E2();
  828. enable_E3();
  829. enable_E4();
  830. #endif
  831. }
  832. if (esteps)
  833. NOLESS(fr_mm_s, min_feedrate_mm_s);
  834. else
  835. NOLESS(fr_mm_s, min_travel_feedrate_mm_s);
  836. /**
  837. * This part of the code calculates the total length of the movement.
  838. * For cartesian bots, the X_AXIS is the real X movement and same for Y_AXIS.
  839. * But for corexy bots, that is not true. The "X_AXIS" and "Y_AXIS" motors (that should be named to A_AXIS
  840. * and B_AXIS) cannot be used for X and Y length, because A=X+Y and B=X-Y.
  841. * So we need to create other 2 "AXIS", named X_HEAD and Y_HEAD, meaning the real displacement of the Head.
  842. * Having the real displacement of the head, we can calculate the total movement length and apply the desired speed.
  843. */
  844. #if IS_CORE
  845. float delta_mm[Z_HEAD + 1];
  846. #if CORE_IS_XY
  847. delta_mm[X_HEAD] = da * steps_to_mm[A_AXIS];
  848. delta_mm[Y_HEAD] = db * steps_to_mm[B_AXIS];
  849. delta_mm[Z_AXIS] = dc * steps_to_mm[Z_AXIS];
  850. delta_mm[A_AXIS] = (da + db) * steps_to_mm[A_AXIS];
  851. delta_mm[B_AXIS] = CORESIGN(da - db) * steps_to_mm[B_AXIS];
  852. #elif CORE_IS_XZ
  853. delta_mm[X_HEAD] = da * steps_to_mm[A_AXIS];
  854. delta_mm[Y_AXIS] = db * steps_to_mm[Y_AXIS];
  855. delta_mm[Z_HEAD] = dc * steps_to_mm[C_AXIS];
  856. delta_mm[A_AXIS] = (da + dc) * steps_to_mm[A_AXIS];
  857. delta_mm[C_AXIS] = CORESIGN(da - dc) * steps_to_mm[C_AXIS];
  858. #elif CORE_IS_YZ
  859. delta_mm[X_AXIS] = da * steps_to_mm[X_AXIS];
  860. delta_mm[Y_HEAD] = db * steps_to_mm[B_AXIS];
  861. delta_mm[Z_HEAD] = dc * steps_to_mm[C_AXIS];
  862. delta_mm[B_AXIS] = (db + dc) * steps_to_mm[B_AXIS];
  863. delta_mm[C_AXIS] = CORESIGN(db - dc) * steps_to_mm[C_AXIS];
  864. #endif
  865. #else
  866. float delta_mm[XYZE];
  867. delta_mm[X_AXIS] = da * steps_to_mm[X_AXIS];
  868. delta_mm[Y_AXIS] = db * steps_to_mm[Y_AXIS];
  869. delta_mm[Z_AXIS] = dc * steps_to_mm[Z_AXIS];
  870. #endif
  871. delta_mm[E_AXIS] = esteps_float * steps_to_mm[E_AXIS_N];
  872. if (block->steps[X_AXIS] < MIN_STEPS_PER_SEGMENT && block->steps[Y_AXIS] < MIN_STEPS_PER_SEGMENT && block->steps[Z_AXIS] < MIN_STEPS_PER_SEGMENT) {
  873. block->millimeters = FABS(delta_mm[E_AXIS]);
  874. }
  875. else {
  876. block->millimeters = SQRT(
  877. #if CORE_IS_XY
  878. sq(delta_mm[X_HEAD]) + sq(delta_mm[Y_HEAD]) + sq(delta_mm[Z_AXIS])
  879. #elif CORE_IS_XZ
  880. sq(delta_mm[X_HEAD]) + sq(delta_mm[Y_AXIS]) + sq(delta_mm[Z_HEAD])
  881. #elif CORE_IS_YZ
  882. sq(delta_mm[X_AXIS]) + sq(delta_mm[Y_HEAD]) + sq(delta_mm[Z_HEAD])
  883. #else
  884. sq(delta_mm[X_AXIS]) + sq(delta_mm[Y_AXIS]) + sq(delta_mm[Z_AXIS])
  885. #endif
  886. );
  887. }
  888. float inverse_millimeters = 1.0 / block->millimeters; // Inverse millimeters to remove multiple divides
  889. // Calculate inverse time for this move. No divide by zero due to previous checks.
  890. // Example: At 120mm/s a 60mm move takes 0.5s. So this will give 2.0.
  891. float inverse_secs = fr_mm_s * inverse_millimeters;
  892. const uint8_t moves_queued = movesplanned();
  893. // Slow down when the buffer starts to empty, rather than wait at the corner for a buffer refill
  894. #if ENABLED(SLOWDOWN) || ENABLED(ULTRA_LCD) || defined(XY_FREQUENCY_LIMIT)
  895. // Segment time im micro seconds
  896. uint32_t segment_time_us = LROUND(1000000.0 / inverse_secs);
  897. #endif
  898. #if ENABLED(SLOWDOWN)
  899. if (WITHIN(moves_queued, 2, (BLOCK_BUFFER_SIZE) / 2 - 1)) {
  900. if (segment_time_us < min_segment_time_us) {
  901. // buffer is draining, add extra time. The amount of time added increases if the buffer is still emptied more.
  902. const uint32_t nst = segment_time_us + LROUND(2 * (min_segment_time_us - segment_time_us) / moves_queued);
  903. inverse_secs = 1000000.0 / nst;
  904. #if defined(XY_FREQUENCY_LIMIT) || ENABLED(ULTRA_LCD)
  905. segment_time_us = nst;
  906. #endif
  907. }
  908. }
  909. #endif
  910. #if ENABLED(ULTRA_LCD)
  911. CRITICAL_SECTION_START
  912. block_buffer_runtime_us += segment_time_us;
  913. CRITICAL_SECTION_END
  914. #endif
  915. block->nominal_speed = block->millimeters * inverse_secs; // (mm/sec) Always > 0
  916. block->nominal_rate = CEIL(block->step_event_count * inverse_secs); // (step/sec) Always > 0
  917. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  918. static float filwidth_e_count = 0, filwidth_delay_dist = 0;
  919. //FMM update ring buffer used for delay with filament measurements
  920. if (extruder == FILAMENT_SENSOR_EXTRUDER_NUM && filwidth_delay_index[1] >= 0) { //only for extruder with filament sensor and if ring buffer is initialized
  921. const int MMD_CM = MAX_MEASUREMENT_DELAY + 1, MMD_MM = MMD_CM * 10;
  922. // increment counters with next move in e axis
  923. filwidth_e_count += delta_mm[E_AXIS];
  924. filwidth_delay_dist += delta_mm[E_AXIS];
  925. // Only get new measurements on forward E movement
  926. if (!UNEAR_ZERO(filwidth_e_count)) {
  927. // Loop the delay distance counter (modulus by the mm length)
  928. while (filwidth_delay_dist >= MMD_MM) filwidth_delay_dist -= MMD_MM;
  929. // Convert into an index into the measurement array
  930. filwidth_delay_index[0] = int8_t(filwidth_delay_dist * 0.1);
  931. // If the index has changed (must have gone forward)...
  932. if (filwidth_delay_index[0] != filwidth_delay_index[1]) {
  933. filwidth_e_count = 0; // Reset the E movement counter
  934. const uint8_t meas_sample = thermalManager.widthFil_to_size_ratio() - 100; // Subtract 100 to reduce magnitude - to store in a signed char
  935. do {
  936. filwidth_delay_index[1] = (filwidth_delay_index[1] + 1) % MMD_CM; // The next unused slot
  937. measurement_delay[filwidth_delay_index[1]] = meas_sample; // Store the measurement
  938. } while (filwidth_delay_index[0] != filwidth_delay_index[1]); // More slots to fill?
  939. }
  940. }
  941. }
  942. #endif
  943. // Calculate and limit speed in mm/sec for each axis
  944. float current_speed[NUM_AXIS], speed_factor = 1.0; // factor <1 decreases speed
  945. LOOP_XYZE(i) {
  946. const float cs = FABS((current_speed[i] = delta_mm[i] * inverse_secs));
  947. #if ENABLED(DISTINCT_E_FACTORS)
  948. if (i == E_AXIS) i += extruder;
  949. #endif
  950. if (cs > max_feedrate_mm_s[i]) NOMORE(speed_factor, max_feedrate_mm_s[i] / cs);
  951. }
  952. // Max segment time in µs.
  953. #ifdef XY_FREQUENCY_LIMIT
  954. // Check and limit the xy direction change frequency
  955. const unsigned char direction_change = block->direction_bits ^ old_direction_bits;
  956. old_direction_bits = block->direction_bits;
  957. segment_time_us = LROUND((float)segment_time_us / speed_factor);
  958. uint32_t xs0 = axis_segment_time_us[X_AXIS][0],
  959. xs1 = axis_segment_time_us[X_AXIS][1],
  960. xs2 = axis_segment_time_us[X_AXIS][2],
  961. ys0 = axis_segment_time_us[Y_AXIS][0],
  962. ys1 = axis_segment_time_us[Y_AXIS][1],
  963. ys2 = axis_segment_time_us[Y_AXIS][2];
  964. if (TEST(direction_change, X_AXIS)) {
  965. xs2 = axis_segment_time_us[X_AXIS][2] = xs1;
  966. xs1 = axis_segment_time_us[X_AXIS][1] = xs0;
  967. xs0 = 0;
  968. }
  969. xs0 = axis_segment_time_us[X_AXIS][0] = xs0 + segment_time_us;
  970. if (TEST(direction_change, Y_AXIS)) {
  971. ys2 = axis_segment_time_us[Y_AXIS][2] = axis_segment_time_us[Y_AXIS][1];
  972. ys1 = axis_segment_time_us[Y_AXIS][1] = axis_segment_time_us[Y_AXIS][0];
  973. ys0 = 0;
  974. }
  975. ys0 = axis_segment_time_us[Y_AXIS][0] = ys0 + segment_time_us;
  976. const uint32_t max_x_segment_time = MAX3(xs0, xs1, xs2),
  977. max_y_segment_time = MAX3(ys0, ys1, ys2),
  978. min_xy_segment_time = min(max_x_segment_time, max_y_segment_time);
  979. if (min_xy_segment_time < MAX_FREQ_TIME_US) {
  980. const float low_sf = speed_factor * min_xy_segment_time / (MAX_FREQ_TIME_US);
  981. NOMORE(speed_factor, low_sf);
  982. }
  983. #endif // XY_FREQUENCY_LIMIT
  984. // Correct the speed
  985. if (speed_factor < 1.0) {
  986. LOOP_XYZE(i) current_speed[i] *= speed_factor;
  987. block->nominal_speed *= speed_factor;
  988. block->nominal_rate *= speed_factor;
  989. }
  990. // Compute and limit the acceleration rate for the trapezoid generator.
  991. const float steps_per_mm = block->step_event_count * inverse_millimeters;
  992. uint32_t accel;
  993. if (!block->steps[X_AXIS] && !block->steps[Y_AXIS] && !block->steps[Z_AXIS]) {
  994. // convert to: acceleration steps/sec^2
  995. accel = CEIL(retract_acceleration * steps_per_mm);
  996. }
  997. else {
  998. #define LIMIT_ACCEL_LONG(AXIS,INDX) do{ \
  999. if (block->steps[AXIS] && max_acceleration_steps_per_s2[AXIS+INDX] < accel) { \
  1000. const uint32_t comp = max_acceleration_steps_per_s2[AXIS+INDX] * block->step_event_count; \
  1001. if (accel * block->steps[AXIS] > comp) accel = comp / block->steps[AXIS]; \
  1002. } \
  1003. }while(0)
  1004. #define LIMIT_ACCEL_FLOAT(AXIS,INDX) do{ \
  1005. if (block->steps[AXIS] && max_acceleration_steps_per_s2[AXIS+INDX] < accel) { \
  1006. const float comp = (float)max_acceleration_steps_per_s2[AXIS+INDX] * (float)block->step_event_count; \
  1007. if ((float)accel * (float)block->steps[AXIS] > comp) accel = comp / (float)block->steps[AXIS]; \
  1008. } \
  1009. }while(0)
  1010. // Start with print or travel acceleration
  1011. accel = CEIL((esteps ? acceleration : travel_acceleration) * steps_per_mm);
  1012. #if ENABLED(DISTINCT_E_FACTORS)
  1013. #define ACCEL_IDX extruder
  1014. #else
  1015. #define ACCEL_IDX 0
  1016. #endif
  1017. // Limit acceleration per axis
  1018. if (block->step_event_count <= cutoff_long) {
  1019. LIMIT_ACCEL_LONG(X_AXIS, 0);
  1020. LIMIT_ACCEL_LONG(Y_AXIS, 0);
  1021. LIMIT_ACCEL_LONG(Z_AXIS, 0);
  1022. LIMIT_ACCEL_LONG(E_AXIS, ACCEL_IDX);
  1023. }
  1024. else {
  1025. LIMIT_ACCEL_FLOAT(X_AXIS, 0);
  1026. LIMIT_ACCEL_FLOAT(Y_AXIS, 0);
  1027. LIMIT_ACCEL_FLOAT(Z_AXIS, 0);
  1028. LIMIT_ACCEL_FLOAT(E_AXIS, ACCEL_IDX);
  1029. }
  1030. }
  1031. block->acceleration_steps_per_s2 = accel;
  1032. block->acceleration = accel / steps_per_mm;
  1033. block->acceleration_rate = (long)(accel * 16777216.0 / (HAL_STEPPER_TIMER_RATE)); // 16777216 = <<24
  1034. // Initial limit on the segment entry velocity
  1035. float vmax_junction;
  1036. #if 0 // Use old jerk for now
  1037. float junction_deviation = 0.1;
  1038. // Compute path unit vector
  1039. double unit_vec[XYZ] = {
  1040. delta_mm[X_AXIS] * inverse_millimeters,
  1041. delta_mm[Y_AXIS] * inverse_millimeters,
  1042. delta_mm[Z_AXIS] * inverse_millimeters
  1043. };
  1044. /*
  1045. Compute maximum allowable entry speed at junction by centripetal acceleration approximation.
  1046. Let a circle be tangent to both previous and current path line segments, where the junction
  1047. deviation is defined as the distance from the junction to the closest edge of the circle,
  1048. collinear with the circle center.
  1049. The circular segment joining the two paths represents the path of centripetal acceleration.
  1050. Solve for max velocity based on max acceleration about the radius of the circle, defined
  1051. indirectly by junction deviation.
  1052. This may be also viewed as path width or max_jerk in the previous grbl version. This approach
  1053. does not actually deviate from path, but used as a robust way to compute cornering speeds, as
  1054. it takes into account the nonlinearities of both the junction angle and junction velocity.
  1055. */
  1056. vmax_junction = MINIMUM_PLANNER_SPEED; // Set default max junction speed
  1057. // Skip first block or when previous_nominal_speed is used as a flag for homing and offset cycles.
  1058. if (moves_queued() && !UNEAR_ZERO(previous_nominal_speed)) {
  1059. // Compute cosine of angle between previous and current path. (prev_unit_vec is negative)
  1060. // NOTE: Max junction velocity is computed without sin() or acos() by trig half angle identity.
  1061. const float cos_theta = - previous_unit_vec[X_AXIS] * unit_vec[X_AXIS]
  1062. - previous_unit_vec[Y_AXIS] * unit_vec[Y_AXIS]
  1063. - previous_unit_vec[Z_AXIS] * unit_vec[Z_AXIS];
  1064. // Skip and use default max junction speed for 0 degree acute junction.
  1065. if (cos_theta < 0.95) {
  1066. vmax_junction = min(previous_nominal_speed, block->nominal_speed);
  1067. // Skip and avoid divide by zero for straight junctions at 180 degrees. Limit to min() of nominal speeds.
  1068. if (cos_theta > -0.95) {
  1069. // Compute maximum junction velocity based on maximum acceleration and junction deviation
  1070. float sin_theta_d2 = SQRT(0.5 * (1.0 - cos_theta)); // Trig half angle identity. Always positive.
  1071. NOMORE(vmax_junction, SQRT(block->acceleration * junction_deviation * sin_theta_d2 / (1.0 - sin_theta_d2)));
  1072. }
  1073. }
  1074. }
  1075. #endif
  1076. /**
  1077. * Adapted from Průša MKS firmware
  1078. * https://github.com/prusa3d/Prusa-Firmware
  1079. *
  1080. * Start with a safe speed (from which the machine may halt to stop immediately).
  1081. */
  1082. // Exit speed limited by a jerk to full halt of a previous last segment
  1083. static float previous_safe_speed;
  1084. float safe_speed = block->nominal_speed;
  1085. uint8_t limited = 0;
  1086. LOOP_XYZE(i) {
  1087. const float jerk = FABS(current_speed[i]), maxj = max_jerk[i];
  1088. if (jerk > maxj) {
  1089. if (limited) {
  1090. const float mjerk = maxj * block->nominal_speed;
  1091. if (jerk * safe_speed > mjerk) safe_speed = mjerk / jerk;
  1092. }
  1093. else {
  1094. ++limited;
  1095. safe_speed = maxj;
  1096. }
  1097. }
  1098. }
  1099. if (moves_queued > 1 && !UNEAR_ZERO(previous_nominal_speed)) {
  1100. // Estimate a maximum velocity allowed at a joint of two successive segments.
  1101. // If this maximum velocity allowed is lower than the minimum of the entry / exit safe velocities,
  1102. // then the machine is not coasting anymore and the safe entry / exit velocities shall be used.
  1103. // The junction velocity will be shared between successive segments. Limit the junction velocity to their minimum.
  1104. // Pick the smaller of the nominal speeds. Higher speed shall not be achieved at the junction during coasting.
  1105. vmax_junction = min(block->nominal_speed, previous_nominal_speed);
  1106. // Factor to multiply the previous / current nominal velocities to get componentwise limited velocities.
  1107. float v_factor = 1;
  1108. limited = 0;
  1109. // Now limit the jerk in all axes.
  1110. const float smaller_speed_factor = vmax_junction / previous_nominal_speed;
  1111. LOOP_XYZE(axis) {
  1112. // Limit an axis. We have to differentiate: coasting, reversal of an axis, full stop.
  1113. float v_exit = previous_speed[axis] * smaller_speed_factor,
  1114. v_entry = current_speed[axis];
  1115. if (limited) {
  1116. v_exit *= v_factor;
  1117. v_entry *= v_factor;
  1118. }
  1119. // Calculate jerk depending on whether the axis is coasting in the same direction or reversing.
  1120. const float jerk = (v_exit > v_entry)
  1121. ? // coasting axis reversal
  1122. ( (v_entry > 0 || v_exit < 0) ? (v_exit - v_entry) : max(v_exit, -v_entry) )
  1123. : // v_exit <= v_entry coasting axis reversal
  1124. ( (v_entry < 0 || v_exit > 0) ? (v_entry - v_exit) : max(-v_exit, v_entry) );
  1125. if (jerk > max_jerk[axis]) {
  1126. v_factor *= max_jerk[axis] / jerk;
  1127. ++limited;
  1128. }
  1129. }
  1130. if (limited) vmax_junction *= v_factor;
  1131. // Now the transition velocity is known, which maximizes the shared exit / entry velocity while
  1132. // respecting the jerk factors, it may be possible, that applying separate safe exit / entry velocities will achieve faster prints.
  1133. const float vmax_junction_threshold = vmax_junction * 0.99f;
  1134. if (previous_safe_speed > vmax_junction_threshold && safe_speed > vmax_junction_threshold) {
  1135. // Not coasting. The machine will stop and start the movements anyway,
  1136. // better to start the segment from start.
  1137. SBI(block->flag, BLOCK_BIT_START_FROM_FULL_HALT);
  1138. vmax_junction = safe_speed;
  1139. }
  1140. }
  1141. else {
  1142. SBI(block->flag, BLOCK_BIT_START_FROM_FULL_HALT);
  1143. vmax_junction = safe_speed;
  1144. }
  1145. // Max entry speed of this block equals the max exit speed of the previous block.
  1146. block->max_entry_speed = vmax_junction;
  1147. // Initialize block entry speed. Compute based on deceleration to user-defined MINIMUM_PLANNER_SPEED.
  1148. const float v_allowable = max_allowable_speed(-block->acceleration, MINIMUM_PLANNER_SPEED, block->millimeters);
  1149. block->entry_speed = min(vmax_junction, v_allowable);
  1150. // Initialize planner efficiency flags
  1151. // Set flag if block will always reach maximum junction speed regardless of entry/exit speeds.
  1152. // If a block can de/ac-celerate from nominal speed to zero within the length of the block, then
  1153. // the current block and next block junction speeds are guaranteed to always be at their maximum
  1154. // junction speeds in deceleration and acceleration, respectively. This is due to how the current
  1155. // block nominal speed limits both the current and next maximum junction speeds. Hence, in both
  1156. // the reverse and forward planners, the corresponding block junction speed will always be at the
  1157. // the maximum junction speed and may always be ignored for any speed reduction checks.
  1158. block->flag |= BLOCK_FLAG_RECALCULATE | (block->nominal_speed <= v_allowable ? BLOCK_FLAG_NOMINAL_LENGTH : 0);
  1159. // Update previous path unit_vector and nominal speed
  1160. COPY(previous_speed, current_speed);
  1161. previous_nominal_speed = block->nominal_speed;
  1162. previous_safe_speed = safe_speed;
  1163. #if ENABLED(LIN_ADVANCE)
  1164. /**
  1165. *
  1166. * Use LIN_ADVANCE for blocks if all these are true:
  1167. *
  1168. * esteps && (block->steps[X_AXIS] || block->steps[Y_AXIS]) : This is a print move
  1169. *
  1170. * extruder_advance_k : There is an advance factor set.
  1171. *
  1172. * esteps != block->step_event_count : A problem occurs if the move before a retract is too small.
  1173. * In that case, the retract and move will be executed together.
  1174. * This leads to too many advance steps due to a huge e_acceleration.
  1175. * The math is good, but we must avoid retract moves with advance!
  1176. * de > 0 : Extruder is running forward (e.g., for "Wipe while retracting" (Slic3r) or "Combing" (Cura) moves)
  1177. */
  1178. block->use_advance_lead = esteps && (block->steps[X_AXIS] || block->steps[Y_AXIS])
  1179. && extruder_advance_k
  1180. && (uint32_t)esteps != block->step_event_count
  1181. && de > 0;
  1182. if (block->use_advance_lead)
  1183. block->abs_adv_steps_multiplier8 = LROUND(
  1184. extruder_advance_k
  1185. * (UNEAR_ZERO(advance_ed_ratio) ? de * steps_to_mm[E_AXIS_N] / HYPOT(da * steps_to_mm[X_AXIS], db * steps_to_mm[Y_AXIS]) : advance_ed_ratio) // Use the fixed ratio, if set
  1186. * (block->nominal_speed / (float)block->nominal_rate)
  1187. * axis_steps_per_mm[E_AXIS_N] * 256.0
  1188. );
  1189. #endif // LIN_ADVANCE
  1190. calculate_trapezoid_for_block(block, block->entry_speed / block->nominal_speed, safe_speed / block->nominal_speed);
  1191. // Move buffer head
  1192. block_buffer_head = next_buffer_head;
  1193. // Update the position (only when a move was queued)
  1194. static_assert(COUNT(target) > 1, "array as function parameter should be declared as reference and with count");
  1195. COPY(position, target);
  1196. recalculate();
  1197. } // _buffer_steps()
  1198. /**
  1199. * Planner::_buffer_line
  1200. *
  1201. * Add a new linear movement to the buffer in axis units.
  1202. *
  1203. * Leveling and kinematics should be applied ahead of calling this.
  1204. *
  1205. * a,b,c,e - target positions in mm and/or degrees
  1206. * fr_mm_s - (target) speed of the move
  1207. * extruder - target extruder
  1208. */
  1209. 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) {
  1210. // When changing extruders recalculate steps corresponding to the E position
  1211. #if ENABLED(DISTINCT_E_FACTORS)
  1212. if (last_extruder != extruder && axis_steps_per_mm[E_AXIS_N] != axis_steps_per_mm[E_AXIS + last_extruder]) {
  1213. position[E_AXIS] = LROUND(position[E_AXIS] * axis_steps_per_mm[E_AXIS_N] * steps_to_mm[E_AXIS + last_extruder]);
  1214. last_extruder = extruder;
  1215. }
  1216. #endif
  1217. // The target position of the tool in absolute steps
  1218. // Calculate target position in absolute steps
  1219. const int32_t target[XYZE] = {
  1220. LROUND(a * axis_steps_per_mm[X_AXIS]),
  1221. LROUND(b * axis_steps_per_mm[Y_AXIS]),
  1222. LROUND(c * axis_steps_per_mm[Z_AXIS]),
  1223. LROUND(e * axis_steps_per_mm[E_AXIS_N])
  1224. };
  1225. /* <-- add a slash to enable
  1226. SERIAL_ECHOPAIR(" _buffer_line FR:", fr_mm_s);
  1227. #if IS_KINEMATIC
  1228. SERIAL_ECHOPAIR(" A:", a);
  1229. SERIAL_ECHOPAIR(" (", position[A_AXIS]);
  1230. SERIAL_ECHOPAIR("->", target[A_AXIS]);
  1231. SERIAL_ECHOPAIR(") B:", b);
  1232. #else
  1233. SERIAL_ECHOPAIR(" X:", a);
  1234. SERIAL_ECHOPAIR(" (", position[X_AXIS]);
  1235. SERIAL_ECHOPAIR("->", target[X_AXIS]);
  1236. SERIAL_ECHOPAIR(") Y:", b);
  1237. #endif
  1238. SERIAL_ECHOPAIR(" (", position[Y_AXIS]);
  1239. SERIAL_ECHOPAIR("->", target[Y_AXIS]);
  1240. #if ENABLED(DELTA)
  1241. SERIAL_ECHOPAIR(") C:", c);
  1242. #else
  1243. SERIAL_ECHOPAIR(") Z:", c);
  1244. #endif
  1245. SERIAL_ECHOPAIR(" (", position[Z_AXIS]);
  1246. SERIAL_ECHOPAIR("->", target[Z_AXIS]);
  1247. SERIAL_ECHOPAIR(") E:", e);
  1248. SERIAL_ECHOPAIR(" (", position[E_AXIS]);
  1249. SERIAL_ECHOPAIR("->", target[E_AXIS]);
  1250. SERIAL_ECHOLNPGM(")");
  1251. //*/
  1252. // DRYRUN ignores all temperature constraints and assures that the extruder is instantly satisfied
  1253. if (DEBUGGING(DRYRUN))
  1254. position[E_AXIS] = target[E_AXIS];
  1255. // Always split the first move into one longer and one shorter move
  1256. if (!blocks_queued()) {
  1257. #define _BETWEEN(A) (position[A##_AXIS] + target[A##_AXIS]) >> 1
  1258. const int32_t between[XYZE] = { _BETWEEN(X), _BETWEEN(Y), _BETWEEN(Z), _BETWEEN(E) };
  1259. DISABLE_STEPPER_DRIVER_INTERRUPT();
  1260. _buffer_steps(between, fr_mm_s, extruder);
  1261. _buffer_steps(target, fr_mm_s, extruder);
  1262. ENABLE_STEPPER_DRIVER_INTERRUPT();
  1263. }
  1264. else
  1265. _buffer_steps(target, fr_mm_s, extruder);
  1266. stepper.wake_up();
  1267. } // _buffer_line()
  1268. /**
  1269. * Directly set the planner XYZ position (and stepper positions)
  1270. * converting mm (or angles for SCARA) into steps.
  1271. *
  1272. * On CORE machines stepper ABC will be translated from the given XYZ.
  1273. */
  1274. void Planner::_set_position_mm(const float &a, const float &b, const float &c, const float &e) {
  1275. #if ENABLED(DISTINCT_E_FACTORS)
  1276. #define _EINDEX (E_AXIS + active_extruder)
  1277. last_extruder = active_extruder;
  1278. #else
  1279. #define _EINDEX E_AXIS
  1280. #endif
  1281. const int32_t na = position[X_AXIS] = LROUND(a * axis_steps_per_mm[X_AXIS]),
  1282. nb = position[Y_AXIS] = LROUND(b * axis_steps_per_mm[Y_AXIS]),
  1283. nc = position[Z_AXIS] = LROUND(c * axis_steps_per_mm[Z_AXIS]),
  1284. ne = position[E_AXIS] = LROUND(e * axis_steps_per_mm[_EINDEX]);
  1285. stepper.set_position(na, nb, nc, ne);
  1286. previous_nominal_speed = 0.0; // Resets planner junction speeds. Assumes start from rest.
  1287. ZERO(previous_speed);
  1288. }
  1289. void Planner::set_position_mm_kinematic(const float position[NUM_AXIS]) {
  1290. #if PLANNER_LEVELING
  1291. float lpos[XYZ] = { position[X_AXIS], position[Y_AXIS], position[Z_AXIS] };
  1292. apply_leveling(lpos);
  1293. #else
  1294. const float * const lpos = position;
  1295. #endif
  1296. #if IS_KINEMATIC
  1297. inverse_kinematics(lpos);
  1298. _set_position_mm(delta[A_AXIS], delta[B_AXIS], delta[C_AXIS], position[E_AXIS]);
  1299. #else
  1300. _set_position_mm(lpos[X_AXIS], lpos[Y_AXIS], lpos[Z_AXIS], position[E_AXIS]);
  1301. #endif
  1302. }
  1303. /**
  1304. * Sync from the stepper positions. (e.g., after an interrupted move)
  1305. */
  1306. void Planner::sync_from_steppers() {
  1307. LOOP_XYZE(i)
  1308. position[i] = stepper.position((AxisEnum)i);
  1309. }
  1310. /**
  1311. * Setters for planner position (also setting stepper position).
  1312. */
  1313. void Planner::set_position_mm(const AxisEnum axis, const float &v) {
  1314. #if ENABLED(DISTINCT_E_FACTORS)
  1315. const uint8_t axis_index = axis + (axis == E_AXIS ? active_extruder : 0);
  1316. last_extruder = active_extruder;
  1317. #else
  1318. const uint8_t axis_index = axis;
  1319. #endif
  1320. position[axis] = LROUND(v * axis_steps_per_mm[axis_index]);
  1321. stepper.set_position(axis, v);
  1322. previous_speed[axis] = 0.0;
  1323. }
  1324. // Recalculate the steps/s^2 acceleration rates, based on the mm/s^2
  1325. void Planner::reset_acceleration_rates() {
  1326. #if ENABLED(DISTINCT_E_FACTORS)
  1327. #define HIGHEST_CONDITION (i < E_AXIS || i == E_AXIS + active_extruder)
  1328. #else
  1329. #define HIGHEST_CONDITION true
  1330. #endif
  1331. uint32_t highest_rate = 1;
  1332. LOOP_XYZE_N(i) {
  1333. max_acceleration_steps_per_s2[i] = max_acceleration_mm_per_s2[i] * axis_steps_per_mm[i];
  1334. if (HIGHEST_CONDITION) NOLESS(highest_rate, max_acceleration_steps_per_s2[i]);
  1335. }
  1336. cutoff_long = 4294967295UL / highest_rate;
  1337. }
  1338. // Recalculate position, steps_to_mm if axis_steps_per_mm changes!
  1339. void Planner::refresh_positioning() {
  1340. LOOP_XYZE_N(i) steps_to_mm[i] = 1.0 / axis_steps_per_mm[i];
  1341. set_position_mm_kinematic(current_position);
  1342. reset_acceleration_rates();
  1343. }
  1344. #if ENABLED(AUTOTEMP)
  1345. void Planner::autotemp_M104_M109() {
  1346. autotemp_enabled = parser.seen('F');
  1347. if (autotemp_enabled) autotemp_factor = parser.value_celsius_diff();
  1348. if (parser.seen('S')) autotemp_min = parser.value_celsius();
  1349. if (parser.seen('B')) autotemp_max = parser.value_celsius();
  1350. }
  1351. #endif