My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

planner.cpp 55KB

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