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

planner.cpp 56KB

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