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 55KB

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