My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

planner.cpp 54KB

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