My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

planner.cpp 56KB

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