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

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