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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643
  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. Planner planner;
  80. // public:
  81. /**
  82. * A ring buffer of moves described in steps
  83. */
  84. block_t Planner::block_buffer[BLOCK_BUFFER_SIZE];
  85. volatile uint8_t Planner::block_buffer_head = 0, // Index of the next block to be pushed
  86. Planner::block_buffer_tail = 0;
  87. float Planner::max_feedrate_mm_s[XYZE_N], // Max speeds in mm per second
  88. Planner::axis_steps_per_mm[XYZE_N],
  89. Planner::steps_to_mm[XYZE_N];
  90. #if ENABLED(DISTINCT_E_FACTORS)
  91. uint8_t Planner::last_extruder = 0; // Respond to extruder change
  92. #endif
  93. int16_t Planner::flow_percentage[EXTRUDERS] = ARRAY_BY_EXTRUDERS1(100); // Extrusion factor for each extruder
  94. float Planner::e_factor[EXTRUDERS]; // The flow percentage and volumetric multiplier combine to scale E movement
  95. #if DISABLED(NO_VOLUMETRICS)
  96. 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
  97. Planner::volumetric_area_nominal = CIRCLE_AREA((DEFAULT_NOMINAL_FILAMENT_DIA) * 0.5), // Nominal cross-sectional area
  98. Planner::volumetric_multiplier[EXTRUDERS]; // Reciprocal of cross-sectional area of filament (in mm^2). Pre-calculated to reduce computation in the planner
  99. #endif
  100. uint32_t Planner::max_acceleration_steps_per_s2[XYZE_N],
  101. Planner::max_acceleration_mm_per_s2[XYZE_N]; // Use M201 to override by software
  102. uint32_t Planner::min_segment_time_us;
  103. // Initialized by settings.load()
  104. float Planner::min_feedrate_mm_s,
  105. Planner::acceleration, // Normal acceleration mm/s^2 DEFAULT ACCELERATION for all printing moves. M204 SXXXX
  106. Planner::retract_acceleration, // Retract acceleration mm/s^2 filament pull-back and push-forward while standing still in the other axes M204 TXXXX
  107. Planner::travel_acceleration, // Travel acceleration mm/s^2 DEFAULT ACCELERATION for all NON printing moves. M204 MXXXX
  108. Planner::max_jerk[XYZE], // The largest speed change requiring no acceleration
  109. Planner::min_travel_feedrate_mm_s;
  110. #if HAS_LEVELING
  111. bool Planner::leveling_active = false; // Flag that auto bed leveling is enabled
  112. #if ABL_PLANAR
  113. matrix_3x3 Planner::bed_level_matrix; // Transform to compensate for bed level
  114. #endif
  115. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  116. float Planner::z_fade_height, // Initialized by settings.load()
  117. Planner::inverse_z_fade_height,
  118. Planner::last_fade_z;
  119. #endif
  120. #else
  121. constexpr bool Planner::leveling_active;
  122. #endif
  123. #if ENABLED(SKEW_CORRECTION)
  124. #if ENABLED(SKEW_CORRECTION_GCODE)
  125. float Planner::xy_skew_factor;
  126. #else
  127. constexpr float Planner::xy_skew_factor;
  128. #endif
  129. #if ENABLED(SKEW_CORRECTION_FOR_Z) && ENABLED(SKEW_CORRECTION_GCODE)
  130. float Planner::xz_skew_factor, Planner::yz_skew_factor;
  131. #else
  132. constexpr float Planner::xz_skew_factor, Planner::yz_skew_factor;
  133. #endif
  134. #endif
  135. #if ENABLED(AUTOTEMP)
  136. float Planner::autotemp_max = 250,
  137. Planner::autotemp_min = 210,
  138. Planner::autotemp_factor = 0.1;
  139. bool Planner::autotemp_enabled = false;
  140. #endif
  141. // private:
  142. int32_t Planner::position[NUM_AXIS] = { 0 };
  143. uint32_t Planner::cutoff_long;
  144. float Planner::previous_speed[NUM_AXIS],
  145. Planner::previous_nominal_speed;
  146. #if ENABLED(DISABLE_INACTIVE_EXTRUDER)
  147. uint8_t Planner::g_uc_extruder_last_move[EXTRUDERS] = { 0 };
  148. #endif
  149. #ifdef XY_FREQUENCY_LIMIT
  150. // Old direction bits. Used for speed calculations
  151. unsigned char Planner::old_direction_bits = 0;
  152. // Segment times (in µs). Used for speed calculations
  153. uint32_t Planner::axis_segment_time_us[2][3] = { { MAX_FREQ_TIME_US + 1, 0, 0 }, { MAX_FREQ_TIME_US + 1, 0, 0 } };
  154. #endif
  155. #if ENABLED(LIN_ADVANCE)
  156. float Planner::extruder_advance_k, // Initialized by settings.load()
  157. Planner::advance_ed_ratio, // Initialized by settings.load()
  158. Planner::position_float[XYZE], // Needed for accurate maths. Steps cannot be used!
  159. Planner::lin_dist_xy,
  160. Planner::lin_dist_e;
  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() > 3) {
  248. const uint8_t endnr = BLOCK_MOD(block_buffer_tail + 2); // tail is running. tail+1 shouldn't be altered because it's connected to the running block.
  249. // tail+2 because the index is not yet advanced when checked
  250. uint8_t blocknr = prev_block_index(block_buffer_head);
  251. block_t* current = &block_buffer[blocknr];
  252. do {
  253. const block_t * const next = current;
  254. blocknr = prev_block_index(blocknr);
  255. current = &block_buffer[blocknr];
  256. if (TEST(current->flag, BLOCK_BIT_START_FROM_FULL_HALT)) // Up to this every block is already optimized.
  257. break;
  258. reverse_pass_kernel(current, next);
  259. } while (blocknr != endnr);
  260. }
  261. }
  262. // The kernel called by recalculate() when scanning the plan from first to last entry.
  263. void Planner::forward_pass_kernel(const block_t * const previous, block_t* const current) {
  264. if (!previous) return;
  265. // If the previous block is an acceleration block, but it is not long enough to complete the
  266. // full speed change within the block, we need to adjust the entry speed accordingly. Entry
  267. // speeds have already been reset, maximized, and reverse planned by reverse planner.
  268. // If nominal length is true, max junction speed is guaranteed to be reached. No need to recheck.
  269. if (!TEST(previous->flag, BLOCK_BIT_NOMINAL_LENGTH)) {
  270. if (previous->entry_speed < current->entry_speed) {
  271. float entry_speed = min(current->entry_speed,
  272. max_allowable_speed(-previous->acceleration, previous->entry_speed, previous->millimeters));
  273. // Check for junction speed change
  274. if (current->entry_speed != entry_speed) {
  275. current->entry_speed = entry_speed;
  276. SBI(current->flag, BLOCK_BIT_RECALCULATE);
  277. }
  278. }
  279. }
  280. }
  281. /**
  282. * recalculate() needs to go over the current plan twice.
  283. * Once in reverse and once forward. This implements the forward pass.
  284. */
  285. void Planner::forward_pass() {
  286. block_t* block[3] = { NULL, NULL, NULL };
  287. for (uint8_t b = block_buffer_tail; b != block_buffer_head; b = next_block_index(b)) {
  288. block[0] = block[1];
  289. block[1] = block[2];
  290. block[2] = &block_buffer[b];
  291. forward_pass_kernel(block[0], block[1]);
  292. }
  293. forward_pass_kernel(block[1], block[2]);
  294. }
  295. /**
  296. * Recalculate the trapezoid speed profiles for all blocks in the plan
  297. * according to the entry_factor for each junction. Must be called by
  298. * recalculate() after updating the blocks.
  299. */
  300. void Planner::recalculate_trapezoids() {
  301. int8_t block_index = block_buffer_tail;
  302. block_t *current, *next = NULL;
  303. while (block_index != block_buffer_head) {
  304. current = next;
  305. next = &block_buffer[block_index];
  306. if (current) {
  307. // Recalculate if current block entry or exit junction speed has changed.
  308. if (TEST(current->flag, BLOCK_BIT_RECALCULATE) || TEST(next->flag, BLOCK_BIT_RECALCULATE)) {
  309. // NOTE: Entry and exit factors always > 0 by all previous logic operations.
  310. const float nomr = 1.0 / current->nominal_speed;
  311. calculate_trapezoid_for_block(current, current->entry_speed * nomr, next->entry_speed * nomr);
  312. CBI(current->flag, BLOCK_BIT_RECALCULATE); // Reset current only to ensure next trapezoid is computed
  313. }
  314. }
  315. block_index = next_block_index(block_index);
  316. }
  317. // Last/newest block in buffer. Exit speed is set with MINIMUM_PLANNER_SPEED. Always recalculated.
  318. if (next) {
  319. const float nomr = 1.0 / next->nominal_speed;
  320. calculate_trapezoid_for_block(next, next->entry_speed * nomr, (MINIMUM_PLANNER_SPEED) * nomr);
  321. CBI(next->flag, BLOCK_BIT_RECALCULATE);
  322. }
  323. }
  324. /*
  325. * Recalculate the motion plan according to the following algorithm:
  326. *
  327. * 1. Go over every block in reverse order...
  328. *
  329. * Calculate a junction speed reduction (block_t.entry_factor) so:
  330. *
  331. * a. The junction jerk is within the set limit, and
  332. *
  333. * b. No speed reduction within one block requires faster
  334. * deceleration than the one, true constant acceleration.
  335. *
  336. * 2. Go over every block in chronological order...
  337. *
  338. * Dial down junction speed reduction values if:
  339. * a. The speed increase within one block would require faster
  340. * acceleration than the one, true constant acceleration.
  341. *
  342. * After that, all blocks will have an entry_factor allowing all speed changes to
  343. * be performed using only the one, true constant acceleration, and where no junction
  344. * jerk is jerkier than the set limit, Jerky. Finally it will:
  345. *
  346. * 3. Recalculate "trapezoids" for all blocks.
  347. */
  348. void Planner::recalculate() {
  349. reverse_pass();
  350. forward_pass();
  351. recalculate_trapezoids();
  352. }
  353. #if ENABLED(AUTOTEMP)
  354. void Planner::getHighESpeed() {
  355. static float oldt = 0;
  356. if (!autotemp_enabled) return;
  357. if (thermalManager.degTargetHotend(0) + 2 < autotemp_min) return; // probably temperature set to zero.
  358. float high = 0.0;
  359. for (uint8_t b = block_buffer_tail; b != block_buffer_head; b = next_block_index(b)) {
  360. block_t* block = &block_buffer[b];
  361. if (block->steps[X_AXIS] || block->steps[Y_AXIS] || block->steps[Z_AXIS]) {
  362. float se = (float)block->steps[E_AXIS] / block->step_event_count * block->nominal_speed; // mm/sec;
  363. NOLESS(high, se);
  364. }
  365. }
  366. float t = autotemp_min + high * autotemp_factor;
  367. t = constrain(t, autotemp_min, autotemp_max);
  368. if (t < oldt) t = t * (1 - (AUTOTEMP_OLDWEIGHT)) + oldt * (AUTOTEMP_OLDWEIGHT);
  369. oldt = t;
  370. thermalManager.setTargetHotend(t, 0);
  371. }
  372. #endif // AUTOTEMP
  373. /**
  374. * Maintain fans, paste extruder pressure,
  375. */
  376. void Planner::check_axes_activity() {
  377. unsigned char axis_active[NUM_AXIS] = { 0 },
  378. tail_fan_speed[FAN_COUNT];
  379. #if ENABLED(BARICUDA)
  380. #if HAS_HEATER_1
  381. uint8_t tail_valve_pressure;
  382. #endif
  383. #if HAS_HEATER_2
  384. uint8_t tail_e_to_p_pressure;
  385. #endif
  386. #endif
  387. if (blocks_queued()) {
  388. #if FAN_COUNT > 0
  389. for (uint8_t i = 0; i < FAN_COUNT; i++)
  390. tail_fan_speed[i] = block_buffer[block_buffer_tail].fan_speed[i];
  391. #endif
  392. block_t* block;
  393. #if ENABLED(BARICUDA)
  394. block = &block_buffer[block_buffer_tail];
  395. #if HAS_HEATER_1
  396. tail_valve_pressure = block->valve_pressure;
  397. #endif
  398. #if HAS_HEATER_2
  399. tail_e_to_p_pressure = block->e_to_p_pressure;
  400. #endif
  401. #endif
  402. for (uint8_t b = block_buffer_tail; b != block_buffer_head; b = next_block_index(b)) {
  403. block = &block_buffer[b];
  404. LOOP_XYZE(i) if (block->steps[i]) axis_active[i]++;
  405. }
  406. }
  407. else {
  408. #if FAN_COUNT > 0
  409. for (uint8_t i = 0; i < FAN_COUNT; i++) tail_fan_speed[i] = fanSpeeds[i];
  410. #endif
  411. #if ENABLED(BARICUDA)
  412. #if HAS_HEATER_1
  413. tail_valve_pressure = baricuda_valve_pressure;
  414. #endif
  415. #if HAS_HEATER_2
  416. tail_e_to_p_pressure = baricuda_e_to_p_pressure;
  417. #endif
  418. #endif
  419. }
  420. #if ENABLED(DISABLE_X)
  421. if (!axis_active[X_AXIS]) disable_X();
  422. #endif
  423. #if ENABLED(DISABLE_Y)
  424. if (!axis_active[Y_AXIS]) disable_Y();
  425. #endif
  426. #if ENABLED(DISABLE_Z)
  427. if (!axis_active[Z_AXIS]) disable_Z();
  428. #endif
  429. #if ENABLED(DISABLE_E)
  430. if (!axis_active[E_AXIS]) disable_e_steppers();
  431. #endif
  432. #if FAN_COUNT > 0
  433. #if FAN_KICKSTART_TIME > 0
  434. static millis_t fan_kick_end[FAN_COUNT] = { 0 };
  435. #define KICKSTART_FAN(f) \
  436. if (tail_fan_speed[f]) { \
  437. millis_t ms = millis(); \
  438. if (fan_kick_end[f] == 0) { \
  439. fan_kick_end[f] = ms + FAN_KICKSTART_TIME; \
  440. tail_fan_speed[f] = 255; \
  441. } else if (PENDING(ms, fan_kick_end[f])) \
  442. tail_fan_speed[f] = 255; \
  443. } else fan_kick_end[f] = 0
  444. #if HAS_FAN0
  445. KICKSTART_FAN(0);
  446. #endif
  447. #if HAS_FAN1
  448. KICKSTART_FAN(1);
  449. #endif
  450. #if HAS_FAN2
  451. KICKSTART_FAN(2);
  452. #endif
  453. #endif // FAN_KICKSTART_TIME > 0
  454. #ifdef FAN_MIN_PWM
  455. #define CALC_FAN_SPEED(f) (tail_fan_speed[f] ? ( FAN_MIN_PWM + (tail_fan_speed[f] * (255 - FAN_MIN_PWM)) / 255 ) : 0)
  456. #else
  457. #define CALC_FAN_SPEED(f) tail_fan_speed[f]
  458. #endif
  459. #if ENABLED(FAN_SOFT_PWM)
  460. #if HAS_FAN0
  461. thermalManager.soft_pwm_amount_fan[0] = CALC_FAN_SPEED(0);
  462. #endif
  463. #if HAS_FAN1
  464. thermalManager.soft_pwm_amount_fan[1] = CALC_FAN_SPEED(1);
  465. #endif
  466. #if HAS_FAN2
  467. thermalManager.soft_pwm_amount_fan[2] = CALC_FAN_SPEED(2);
  468. #endif
  469. #else
  470. #if HAS_FAN0
  471. analogWrite(FAN_PIN, CALC_FAN_SPEED(0));
  472. #endif
  473. #if HAS_FAN1
  474. analogWrite(FAN1_PIN, CALC_FAN_SPEED(1));
  475. #endif
  476. #if HAS_FAN2
  477. analogWrite(FAN2_PIN, CALC_FAN_SPEED(2));
  478. #endif
  479. #endif
  480. #endif // FAN_COUNT > 0
  481. #if ENABLED(AUTOTEMP)
  482. getHighESpeed();
  483. #endif
  484. #if ENABLED(BARICUDA)
  485. #if HAS_HEATER_1
  486. analogWrite(HEATER_1_PIN, tail_valve_pressure);
  487. #endif
  488. #if HAS_HEATER_2
  489. analogWrite(HEATER_2_PIN, tail_e_to_p_pressure);
  490. #endif
  491. #endif
  492. }
  493. #if DISABLED(NO_VOLUMETRICS)
  494. /**
  495. * Get a volumetric multiplier from a filament diameter.
  496. * This is the reciprocal of the circular cross-section area.
  497. * Return 1.0 with volumetric off or a diameter of 0.0.
  498. */
  499. inline float calculate_volumetric_multiplier(const float &diameter) {
  500. return (parser.volumetric_enabled && diameter) ? 1.0 / CIRCLE_AREA(diameter * 0.5) : 1.0;
  501. }
  502. /**
  503. * Convert the filament sizes into volumetric multipliers.
  504. * The multiplier converts a given E value into a length.
  505. */
  506. void Planner::calculate_volumetric_multipliers() {
  507. for (uint8_t i = 0; i < COUNT(filament_size); i++) {
  508. volumetric_multiplier[i] = calculate_volumetric_multiplier(filament_size[i]);
  509. refresh_e_factor(i);
  510. }
  511. }
  512. #endif // !NO_VOLUMETRICS
  513. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  514. /**
  515. * Convert the ratio value given by the filament width sensor
  516. * into a volumetric multiplier. Conversion differs when using
  517. * linear extrusion vs volumetric extrusion.
  518. */
  519. void Planner::calculate_volumetric_for_width_sensor(const int8_t encoded_ratio) {
  520. // Reconstitute the nominal/measured ratio
  521. const float nom_meas_ratio = 1.0 + 0.01 * encoded_ratio,
  522. ratio_2 = sq(nom_meas_ratio);
  523. volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] = parser.volumetric_enabled
  524. ? ratio_2 / CIRCLE_AREA(filament_width_nominal * 0.5) // Volumetric uses a true volumetric multiplier
  525. : ratio_2; // Linear squares the ratio, which scales the volume
  526. refresh_e_factor(FILAMENT_SENSOR_EXTRUDER_NUM);
  527. }
  528. #endif
  529. #if PLANNER_LEVELING
  530. /**
  531. * rx, ry, rz - Cartesian positions in mm
  532. * Leveled XYZ on completion
  533. */
  534. void Planner::apply_leveling(float &rx, float &ry, float &rz) {
  535. #if ENABLED(SKEW_CORRECTION)
  536. skew(rx, ry, rz);
  537. #endif
  538. if (!leveling_active) return;
  539. #if ABL_PLANAR
  540. float dx = rx - (X_TILT_FULCRUM),
  541. dy = ry - (Y_TILT_FULCRUM);
  542. apply_rotation_xyz(bed_level_matrix, dx, dy, rz);
  543. rx = dx + X_TILT_FULCRUM;
  544. ry = dy + Y_TILT_FULCRUM;
  545. #else
  546. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  547. const float fade_scaling_factor = fade_scaling_factor_for_z(rz);
  548. if (!fade_scaling_factor) return;
  549. #elif HAS_MESH
  550. constexpr float fade_scaling_factor = 1.0;
  551. #endif
  552. #if ENABLED(AUTO_BED_LEVELING_BILINEAR)
  553. const float raw[XYZ] = { rx, ry, 0 };
  554. #endif
  555. rz += (
  556. #if ENABLED(AUTO_BED_LEVELING_UBL)
  557. ubl.get_z_correction(rx, ry) * fade_scaling_factor
  558. #elif ENABLED(MESH_BED_LEVELING)
  559. mbl.get_z(rx, ry
  560. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  561. , fade_scaling_factor
  562. #endif
  563. )
  564. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  565. bilinear_z_offset(raw) * fade_scaling_factor
  566. #else
  567. 0
  568. #endif
  569. );
  570. #endif
  571. }
  572. void Planner::unapply_leveling(float raw[XYZ]) {
  573. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  574. const float fade_scaling_factor = fade_scaling_factor_for_z(raw[Z_AXIS]);
  575. #else
  576. constexpr float fade_scaling_factor = 1.0;
  577. #endif
  578. if (leveling_active && fade_scaling_factor) {
  579. #if ABL_PLANAR
  580. matrix_3x3 inverse = matrix_3x3::transpose(bed_level_matrix);
  581. float dx = raw[X_AXIS] - (X_TILT_FULCRUM),
  582. dy = raw[Y_AXIS] - (Y_TILT_FULCRUM);
  583. apply_rotation_xyz(inverse, dx, dy, raw[Z_AXIS]);
  584. raw[X_AXIS] = dx + X_TILT_FULCRUM;
  585. raw[Y_AXIS] = dy + Y_TILT_FULCRUM;
  586. #else // !ABL_PLANAR
  587. raw[Z_AXIS] -= (
  588. #if ENABLED(AUTO_BED_LEVELING_UBL)
  589. ubl.get_z_correction(raw[X_AXIS], raw[Y_AXIS]) * fade_scaling_factor
  590. #elif ENABLED(MESH_BED_LEVELING)
  591. mbl.get_z(raw[X_AXIS], raw[Y_AXIS]
  592. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  593. , fade_scaling_factor
  594. #endif
  595. )
  596. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  597. bilinear_z_offset(raw) * fade_scaling_factor
  598. #else
  599. 0
  600. #endif
  601. );
  602. #endif // !ABL_PLANAR
  603. }
  604. #if ENABLED(SKEW_CORRECTION)
  605. unskew(raw[X_AXIS], raw[Y_AXIS], raw[Z_AXIS]);
  606. #endif
  607. }
  608. #endif // PLANNER_LEVELING
  609. /**
  610. * Planner::_buffer_steps
  611. *
  612. * Add a new linear movement to the buffer (in terms of steps).
  613. *
  614. * target - target position in steps units
  615. * fr_mm_s - (target) speed of the move
  616. * extruder - target extruder
  617. */
  618. void Planner::_buffer_steps(const int32_t (&target)[XYZE], float fr_mm_s, const uint8_t extruder) {
  619. const int32_t da = target[X_AXIS] - position[X_AXIS],
  620. db = target[Y_AXIS] - position[Y_AXIS],
  621. dc = target[Z_AXIS] - position[Z_AXIS];
  622. int32_t de = target[E_AXIS] - position[E_AXIS];
  623. /* <-- add a slash to enable
  624. SERIAL_ECHOPAIR(" _buffer_steps FR:", fr_mm_s);
  625. SERIAL_ECHOPAIR(" A:", target[A_AXIS]);
  626. SERIAL_ECHOPAIR(" (", da);
  627. SERIAL_ECHOPAIR(" steps) B:", target[B_AXIS]);
  628. SERIAL_ECHOPAIR(" (", db);
  629. SERIAL_ECHOPAIR(" steps) C:", target[C_AXIS]);
  630. SERIAL_ECHOPAIR(" (", dc);
  631. SERIAL_ECHOPAIR(" steps) E:", target[E_AXIS]);
  632. SERIAL_ECHOPAIR(" (", de);
  633. SERIAL_ECHOLNPGM(" steps)");
  634. //*/
  635. // If LIN_ADVANCE is disabled then do E move prevention with integers
  636. // Otherwise it's done in _buffer_segment.
  637. #if DISABLED(LIN_ADVANCE) && (ENABLED(PREVENT_COLD_EXTRUSION) || ENABLED(PREVENT_LENGTHY_EXTRUDE))
  638. if (de) {
  639. #if ENABLED(PREVENT_COLD_EXTRUSION)
  640. if (thermalManager.tooColdToExtrude(extruder)) {
  641. position[E_AXIS] = target[E_AXIS]; // Behave as if the move really took place, but ignore E part
  642. de = 0; // no difference
  643. SERIAL_ECHO_START();
  644. SERIAL_ECHOLNPGM(MSG_ERR_COLD_EXTRUDE_STOP);
  645. }
  646. #endif // PREVENT_COLD_EXTRUSION
  647. #if ENABLED(PREVENT_LENGTHY_EXTRUDE)
  648. 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
  649. position[E_AXIS] = target[E_AXIS]; // Behave as if the move really took place, but ignore E part
  650. de = 0; // no difference
  651. SERIAL_ECHO_START();
  652. SERIAL_ECHOLNPGM(MSG_ERR_LONG_EXTRUDE_STOP);
  653. }
  654. #endif // PREVENT_LENGTHY_EXTRUDE
  655. }
  656. #endif // !LIN_ADVANCE && (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 = 0x00;
  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. const float inverse_millimeters = 1.0 / block->millimeters; // Inverse millimeters to remove multiple divides
  908. // Calculate inverse time for this move. No divide by zero due to previous checks.
  909. // Example: At 120mm/s a 60mm move takes 0.5s. So this will give 2.0.
  910. float inverse_secs = fr_mm_s * inverse_millimeters;
  911. const uint8_t moves_queued = movesplanned();
  912. // Slow down when the buffer starts to empty, rather than wait at the corner for a buffer refill
  913. #if ENABLED(SLOWDOWN) || ENABLED(ULTRA_LCD) || defined(XY_FREQUENCY_LIMIT)
  914. // Segment time im micro seconds
  915. uint32_t segment_time_us = LROUND(1000000.0 / inverse_secs);
  916. #endif
  917. #if ENABLED(SLOWDOWN)
  918. if (WITHIN(moves_queued, 2, (BLOCK_BUFFER_SIZE) / 2 - 1)) {
  919. if (segment_time_us < min_segment_time_us) {
  920. // buffer is draining, add extra time. The amount of time added increases if the buffer is still emptied more.
  921. const uint32_t nst = segment_time_us + LROUND(2 * (min_segment_time_us - segment_time_us) / moves_queued);
  922. inverse_secs = 1000000.0 / nst;
  923. #if defined(XY_FREQUENCY_LIMIT) || ENABLED(ULTRA_LCD)
  924. segment_time_us = nst;
  925. #endif
  926. }
  927. }
  928. #endif
  929. #if ENABLED(ULTRA_LCD)
  930. CRITICAL_SECTION_START
  931. block_buffer_runtime_us += segment_time_us;
  932. CRITICAL_SECTION_END
  933. #endif
  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. constexpr 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 (!UNEAR_ZERO(filwidth_e_count)) {
  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 int8_t meas_sample = thermalManager.widthFil_to_size_ratio();
  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, calculate minimum acceleration ratio
  961. float current_speed[NUM_AXIS], speed_factor = 1.0; // factor <1 decreases speed
  962. float max_stepper_speed = 0, min_axis_accel_ratio = 1; // ratio < 1 means acceleration ramp needed
  963. LOOP_XYZE(i) {
  964. const float cs = FABS((current_speed[i] = delta_mm[i] * inverse_secs));
  965. if (cs > max_jerk[i])
  966. NOMORE(min_axis_accel_ratio, max_jerk[i] / cs);
  967. NOLESS(max_stepper_speed, cs);
  968. #if ENABLED(DISTINCT_E_FACTORS)
  969. if (i == E_AXIS) i += extruder;
  970. #endif
  971. if (cs > max_feedrate_mm_s[i]) NOMORE(speed_factor, max_feedrate_mm_s[i] / cs);
  972. }
  973. // Max segment time in µs.
  974. #ifdef XY_FREQUENCY_LIMIT
  975. // Check and limit the xy direction change frequency
  976. const unsigned char direction_change = block->direction_bits ^ old_direction_bits;
  977. old_direction_bits = block->direction_bits;
  978. segment_time_us = LROUND((float)segment_time_us / speed_factor);
  979. uint32_t xs0 = axis_segment_time_us[X_AXIS][0],
  980. xs1 = axis_segment_time_us[X_AXIS][1],
  981. xs2 = axis_segment_time_us[X_AXIS][2],
  982. ys0 = axis_segment_time_us[Y_AXIS][0],
  983. ys1 = axis_segment_time_us[Y_AXIS][1],
  984. ys2 = axis_segment_time_us[Y_AXIS][2];
  985. if (TEST(direction_change, X_AXIS)) {
  986. xs2 = axis_segment_time_us[X_AXIS][2] = xs1;
  987. xs1 = axis_segment_time_us[X_AXIS][1] = xs0;
  988. xs0 = 0;
  989. }
  990. xs0 = axis_segment_time_us[X_AXIS][0] = xs0 + segment_time_us;
  991. if (TEST(direction_change, Y_AXIS)) {
  992. ys2 = axis_segment_time_us[Y_AXIS][2] = axis_segment_time_us[Y_AXIS][1];
  993. ys1 = axis_segment_time_us[Y_AXIS][1] = axis_segment_time_us[Y_AXIS][0];
  994. ys0 = 0;
  995. }
  996. ys0 = axis_segment_time_us[Y_AXIS][0] = ys0 + segment_time_us;
  997. const uint32_t max_x_segment_time = MAX3(xs0, xs1, xs2),
  998. max_y_segment_time = MAX3(ys0, ys1, ys2),
  999. min_xy_segment_time = min(max_x_segment_time, max_y_segment_time);
  1000. if (min_xy_segment_time < MAX_FREQ_TIME_US) {
  1001. const float low_sf = speed_factor * min_xy_segment_time / (MAX_FREQ_TIME_US);
  1002. NOMORE(speed_factor, low_sf);
  1003. }
  1004. #endif // XY_FREQUENCY_LIMIT
  1005. block->nominal_speed = max_stepper_speed; // (mm/sec) Always > 0
  1006. block->nominal_rate = CEIL(block->step_event_count * inverse_secs); // (step/sec) Always > 0
  1007. // Correct the speed
  1008. if (speed_factor < 1.0) {
  1009. LOOP_XYZE(i) current_speed[i] *= speed_factor;
  1010. block->nominal_speed *= speed_factor;
  1011. block->nominal_rate *= speed_factor;
  1012. }
  1013. float safe_speed = block->nominal_speed * min_axis_accel_ratio;
  1014. static float previous_safe_speed;
  1015. // Compute and limit the acceleration rate for the trapezoid generator.
  1016. const float steps_per_mm = block->step_event_count * inverse_millimeters;
  1017. uint32_t accel;
  1018. if (!block->steps[X_AXIS] && !block->steps[Y_AXIS] && !block->steps[Z_AXIS]) {
  1019. // convert to: acceleration steps/sec^2
  1020. accel = CEIL(retract_acceleration * steps_per_mm);
  1021. }
  1022. else {
  1023. #define LIMIT_ACCEL_LONG(AXIS,INDX) do{ \
  1024. if (block->steps[AXIS] && max_acceleration_steps_per_s2[AXIS+INDX] < accel) { \
  1025. const uint32_t comp = max_acceleration_steps_per_s2[AXIS+INDX] * block->step_event_count; \
  1026. if (accel * block->steps[AXIS] > comp) accel = comp / block->steps[AXIS]; \
  1027. } \
  1028. }while(0)
  1029. #define LIMIT_ACCEL_FLOAT(AXIS,INDX) do{ \
  1030. if (block->steps[AXIS] && max_acceleration_steps_per_s2[AXIS+INDX] < accel) { \
  1031. const float comp = (float)max_acceleration_steps_per_s2[AXIS+INDX] * (float)block->step_event_count; \
  1032. if ((float)accel * (float)block->steps[AXIS] > comp) accel = comp / (float)block->steps[AXIS]; \
  1033. } \
  1034. }while(0)
  1035. // Start with print or travel acceleration
  1036. accel = CEIL((esteps ? acceleration : travel_acceleration) * steps_per_mm);
  1037. #if ENABLED(DISTINCT_E_FACTORS)
  1038. #define ACCEL_IDX extruder
  1039. #else
  1040. #define ACCEL_IDX 0
  1041. #endif
  1042. // Limit acceleration per axis
  1043. if (block->step_event_count <= cutoff_long) {
  1044. LIMIT_ACCEL_LONG(X_AXIS, 0);
  1045. LIMIT_ACCEL_LONG(Y_AXIS, 0);
  1046. LIMIT_ACCEL_LONG(Z_AXIS, 0);
  1047. LIMIT_ACCEL_LONG(E_AXIS, ACCEL_IDX);
  1048. }
  1049. else {
  1050. LIMIT_ACCEL_FLOAT(X_AXIS, 0);
  1051. LIMIT_ACCEL_FLOAT(Y_AXIS, 0);
  1052. LIMIT_ACCEL_FLOAT(Z_AXIS, 0);
  1053. LIMIT_ACCEL_FLOAT(E_AXIS, ACCEL_IDX);
  1054. }
  1055. }
  1056. block->acceleration_steps_per_s2 = accel;
  1057. block->acceleration = accel / steps_per_mm;
  1058. block->acceleration_rate = (long)(accel * 16777216.0 / (HAL_STEPPER_TIMER_RATE)); // 16777216 = <<24
  1059. // Initial limit on the segment entry velocity
  1060. float vmax_junction;
  1061. #if 0 // Use old jerk for now
  1062. float junction_deviation = 0.1;
  1063. // Compute path unit vector
  1064. double unit_vec[XYZ] = {
  1065. delta_mm[X_AXIS] * inverse_millimeters,
  1066. delta_mm[Y_AXIS] * inverse_millimeters,
  1067. delta_mm[Z_AXIS] * inverse_millimeters
  1068. };
  1069. /*
  1070. Compute maximum allowable entry speed at junction by centripetal acceleration approximation.
  1071. Let a circle be tangent to both previous and current path line segments, where the junction
  1072. deviation is defined as the distance from the junction to the closest edge of the circle,
  1073. collinear with the circle center.
  1074. The circular segment joining the two paths represents the path of centripetal acceleration.
  1075. Solve for max velocity based on max acceleration about the radius of the circle, defined
  1076. indirectly by junction deviation.
  1077. This may be also viewed as path width or max_jerk in the previous grbl version. This approach
  1078. does not actually deviate from path, but used as a robust way to compute cornering speeds, as
  1079. it takes into account the nonlinearities of both the junction angle and junction velocity.
  1080. */
  1081. vmax_junction = MINIMUM_PLANNER_SPEED; // Set default max junction speed
  1082. // Skip first block or when previous_nominal_speed is used as a flag for homing and offset cycles.
  1083. if (moves_queued && !UNEAR_ZERO(previous_nominal_speed)) {
  1084. // Compute cosine of angle between previous and current path. (prev_unit_vec is negative)
  1085. // NOTE: Max junction velocity is computed without sin() or acos() by trig half angle identity.
  1086. const float cos_theta = - previous_unit_vec[X_AXIS] * unit_vec[X_AXIS]
  1087. - previous_unit_vec[Y_AXIS] * unit_vec[Y_AXIS]
  1088. - previous_unit_vec[Z_AXIS] * unit_vec[Z_AXIS];
  1089. // Skip and use default max junction speed for 0 degree acute junction.
  1090. if (cos_theta < 0.95) {
  1091. vmax_junction = min(previous_nominal_speed, block->nominal_speed);
  1092. // Skip and avoid divide by zero for straight junctions at 180 degrees. Limit to min() of nominal speeds.
  1093. if (cos_theta > -0.95) {
  1094. // Compute maximum junction velocity based on maximum acceleration and junction deviation
  1095. float sin_theta_d2 = SQRT(0.5 * (1.0 - cos_theta)); // Trig half angle identity. Always positive.
  1096. NOMORE(vmax_junction, SQRT(block->acceleration * junction_deviation * sin_theta_d2 / (1.0 - sin_theta_d2)));
  1097. }
  1098. }
  1099. }
  1100. #endif
  1101. if (moves_queued && !UNEAR_ZERO(previous_nominal_speed)) {
  1102. // Estimate a maximum velocity allowed at a joint of two successive segments.
  1103. // If this maximum velocity allowed is lower than the minimum of the entry / exit safe velocities,
  1104. // then the machine is not coasting anymore and the safe entry / exit velocities shall be used.
  1105. // The junction velocity will be shared between successive segments. Limit the junction velocity to their minimum.
  1106. // Pick the smaller of the nominal speeds. Higher speed shall not be achieved at the junction during coasting.
  1107. vmax_junction = min(block->nominal_speed, previous_nominal_speed);
  1108. // Factor to multiply the previous / current nominal velocities to get componentwise limited velocities.
  1109. float v_factor = 1;
  1110. uint8_t limited = 0;
  1111. // Now limit the jerk in all axes.
  1112. const float smaller_speed_factor = vmax_junction / previous_nominal_speed;
  1113. LOOP_XYZE(axis) {
  1114. // Limit an axis. We have to differentiate: coasting, reversal of an axis, full stop.
  1115. float v_exit = previous_speed[axis] * smaller_speed_factor,
  1116. v_entry = current_speed[axis];
  1117. if (limited) {
  1118. v_exit *= v_factor;
  1119. v_entry *= v_factor;
  1120. }
  1121. // Calculate jerk depending on whether the axis is coasting in the same direction or reversing.
  1122. const float jerk = (v_exit > v_entry)
  1123. ? // coasting axis reversal
  1124. ( (v_entry > 0 || v_exit < 0) ? (v_exit - v_entry) : max(v_exit, -v_entry) )
  1125. : // v_exit <= v_entry coasting axis reversal
  1126. ( (v_entry < 0 || v_exit > 0) ? (v_entry - v_exit) : max(-v_exit, v_entry) );
  1127. if (jerk > max_jerk[axis]) {
  1128. v_factor *= max_jerk[axis] / jerk;
  1129. ++limited;
  1130. }
  1131. }
  1132. if (limited) vmax_junction *= v_factor;
  1133. // Now the transition velocity is known, which maximizes the shared exit / entry velocity while
  1134. // respecting the jerk factors, it may be possible, that applying separate safe exit / entry velocities will achieve faster prints.
  1135. const float vmax_junction_threshold = vmax_junction * 0.99f;
  1136. if (previous_safe_speed > vmax_junction_threshold && safe_speed > vmax_junction_threshold) {
  1137. // Not coasting. The machine will stop and start the movements anyway,
  1138. // better to start the segment from start.
  1139. SBI(block->flag, BLOCK_BIT_START_FROM_FULL_HALT);
  1140. vmax_junction = safe_speed;
  1141. }
  1142. }
  1143. else {
  1144. SBI(block->flag, BLOCK_BIT_START_FROM_FULL_HALT);
  1145. vmax_junction = safe_speed;
  1146. }
  1147. // Max entry speed of this block equals the max exit speed of the previous block.
  1148. block->max_entry_speed = vmax_junction;
  1149. // Initialize block entry speed. Compute based on deceleration to user-defined MINIMUM_PLANNER_SPEED.
  1150. const float v_allowable = max_allowable_speed(-block->acceleration, MINIMUM_PLANNER_SPEED, block->millimeters);
  1151. block->entry_speed = min(vmax_junction, v_allowable);
  1152. // Initialize planner efficiency flags
  1153. // Set flag if block will always reach maximum junction speed regardless of entry/exit speeds.
  1154. // If a block can de/ac-celerate from nominal speed to zero within the length of the block, then
  1155. // the current block and next block junction speeds are guaranteed to always be at their maximum
  1156. // junction speeds in deceleration and acceleration, respectively. This is due to how the current
  1157. // block nominal speed limits both the current and next maximum junction speeds. Hence, in both
  1158. // the reverse and forward planners, the corresponding block junction speed will always be at the
  1159. // the maximum junction speed and may always be ignored for any speed reduction checks.
  1160. block->flag |= BLOCK_FLAG_RECALCULATE | (block->nominal_speed <= v_allowable ? BLOCK_FLAG_NOMINAL_LENGTH : 0);
  1161. // Update previous path unit_vector and nominal speed
  1162. COPY(previous_speed, current_speed);
  1163. previous_nominal_speed = block->nominal_speed;
  1164. previous_safe_speed = safe_speed;
  1165. #if ENABLED(LIN_ADVANCE)
  1166. /**
  1167. *
  1168. * Use LIN_ADVANCE for blocks if all these are true:
  1169. *
  1170. * esteps && (block->steps[X_AXIS] || block->steps[Y_AXIS]) : This is a print move
  1171. *
  1172. * extruder_advance_k : There is an advance factor set.
  1173. *
  1174. * esteps != block->step_event_count : A problem occurs if the move before a retract is too small.
  1175. * In that case, the retract and move will be executed together.
  1176. * This leads to too many advance steps due to a huge e_acceleration.
  1177. * The math is good, but we must avoid retract moves with advance!
  1178. * lin_dist_e > 0 : Extruder is running forward (e.g., for "Wipe while retracting" (Slic3r) or "Combing" (Cura) moves)
  1179. */
  1180. block->use_advance_lead = esteps && (block->steps[X_AXIS] || block->steps[Y_AXIS])
  1181. && extruder_advance_k
  1182. && (uint32_t)esteps != block->step_event_count
  1183. && lin_dist_e > 0;
  1184. if (block->use_advance_lead)
  1185. block->abs_adv_steps_multiplier8 = LROUND(
  1186. extruder_advance_k
  1187. * (UNEAR_ZERO(advance_ed_ratio) ? lin_dist_e / lin_dist_xy : advance_ed_ratio) // Use the fixed ratio, if set
  1188. * (block->nominal_speed / (float)block->nominal_rate)
  1189. * axis_steps_per_mm[E_AXIS_N] * 256.0
  1190. );
  1191. #endif // LIN_ADVANCE
  1192. const float bnsr = 1.0 / block->nominal_speed;
  1193. calculate_trapezoid_for_block(block, block->entry_speed * bnsr, safe_speed * bnsr);
  1194. // Move buffer head
  1195. block_buffer_head = next_buffer_head;
  1196. // Update the position (only when a move was queued)
  1197. static_assert(COUNT(target) > 1, "Parameter to _buffer_steps must be (&target)[XYZE]!");
  1198. COPY(position, target);
  1199. recalculate();
  1200. } // _buffer_steps()
  1201. /**
  1202. * Planner::buffer_segment
  1203. *
  1204. * Add a new linear movement to the buffer in axis units.
  1205. *
  1206. * Leveling and kinematics should be applied ahead of calling this.
  1207. *
  1208. * a,b,c,e - target positions in mm and/or degrees
  1209. * fr_mm_s - (target) speed of the move
  1210. * extruder - target extruder
  1211. */
  1212. 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) {
  1213. // When changing extruders recalculate steps corresponding to the E position
  1214. #if ENABLED(DISTINCT_E_FACTORS)
  1215. if (last_extruder != extruder && axis_steps_per_mm[E_AXIS_N] != axis_steps_per_mm[E_AXIS + last_extruder]) {
  1216. position[E_AXIS] = LROUND(position[E_AXIS] * axis_steps_per_mm[E_AXIS_N] * steps_to_mm[E_AXIS + last_extruder]);
  1217. last_extruder = extruder;
  1218. }
  1219. #endif
  1220. // The target position of the tool in absolute steps
  1221. // Calculate target position in absolute steps
  1222. const int32_t target[XYZE] = {
  1223. LROUND(a * axis_steps_per_mm[X_AXIS]),
  1224. LROUND(b * axis_steps_per_mm[Y_AXIS]),
  1225. LROUND(c * axis_steps_per_mm[Z_AXIS]),
  1226. LROUND(e * axis_steps_per_mm[E_AXIS_N])
  1227. };
  1228. /* <-- add a slash to enable
  1229. SERIAL_ECHOPAIR(" buffer_segment FR:", fr_mm_s);
  1230. #if IS_KINEMATIC
  1231. SERIAL_ECHOPAIR(" A:", a);
  1232. SERIAL_ECHOPAIR(" (", position[A_AXIS]);
  1233. SERIAL_ECHOPAIR("->", target[A_AXIS]);
  1234. SERIAL_ECHOPAIR(") B:", b);
  1235. #else
  1236. SERIAL_ECHOPAIR(" X:", a);
  1237. SERIAL_ECHOPAIR(" (", position[X_AXIS]);
  1238. SERIAL_ECHOPAIR("->", target[X_AXIS]);
  1239. SERIAL_ECHOPAIR(") Y:", b);
  1240. #endif
  1241. SERIAL_ECHOPAIR(" (", position[Y_AXIS]);
  1242. SERIAL_ECHOPAIR("->", target[Y_AXIS]);
  1243. #if ENABLED(DELTA)
  1244. SERIAL_ECHOPAIR(") C:", c);
  1245. #else
  1246. SERIAL_ECHOPAIR(") Z:", c);
  1247. #endif
  1248. SERIAL_ECHOPAIR(" (", position[Z_AXIS]);
  1249. SERIAL_ECHOPAIR("->", target[Z_AXIS]);
  1250. SERIAL_ECHOPAIR(") E:", e);
  1251. SERIAL_ECHOPAIR(" (", position[E_AXIS]);
  1252. SERIAL_ECHOPAIR("->", target[E_AXIS]);
  1253. SERIAL_ECHOLNPGM(")");
  1254. //*/
  1255. // DRYRUN prevents E moves from taking place
  1256. if (DEBUGGING(DRYRUN)) {
  1257. position[E_AXIS] = target[E_AXIS];
  1258. #if ENABLED(LIN_ADVANCE)
  1259. position_float[E_AXIS] = e;
  1260. #endif
  1261. }
  1262. #if ENABLED(LIN_ADVANCE)
  1263. lin_dist_e = e - position_float[E_AXIS];
  1264. #endif
  1265. // If LIN_ADVANCE is enabled then do E move prevention with floats
  1266. // Otherwise it's done in _buffer_steps.
  1267. #if ENABLED(LIN_ADVANCE) && (ENABLED(PREVENT_COLD_EXTRUSION) || ENABLED(PREVENT_LENGTHY_EXTRUDE))
  1268. if (lin_dist_e) {
  1269. #if ENABLED(PREVENT_COLD_EXTRUSION)
  1270. if (thermalManager.tooColdToExtrude(extruder)) {
  1271. position_float[E_AXIS] = e; // Behave as if the move really took place, but ignore E part
  1272. position[E_AXIS] = target[E_AXIS];
  1273. lin_dist_e = 0;
  1274. SERIAL_ECHO_START();
  1275. SERIAL_ECHOLNPGM(MSG_ERR_COLD_EXTRUDE_STOP);
  1276. }
  1277. #endif // PREVENT_COLD_EXTRUSION
  1278. #if ENABLED(PREVENT_LENGTHY_EXTRUDE)
  1279. if (lin_dist_e * e_factor[extruder] > (EXTRUDE_MAXLENGTH)) {
  1280. position_float[E_AXIS] = e; // Behave as if the move really took place, but ignore E part
  1281. position[E_AXIS] = target[E_AXIS];
  1282. lin_dist_e = 0;
  1283. SERIAL_ECHO_START();
  1284. SERIAL_ECHOLNPGM(MSG_ERR_LONG_EXTRUDE_STOP);
  1285. }
  1286. #endif // PREVENT_LENGTHY_EXTRUDE
  1287. }
  1288. #endif // LIN_ADVANCE && (PREVENT_COLD_EXTRUSION || PREVENT_LENGTHY_EXTRUDE)
  1289. #if ENABLED(LIN_ADVANCE)
  1290. if (lin_dist_e > 0)
  1291. lin_dist_xy = HYPOT(a - position_float[X_AXIS], b - position_float[Y_AXIS]);
  1292. #endif
  1293. // Always split the first move into two (if not homing or probing)
  1294. if (!blocks_queued()) {
  1295. #define _BETWEEN(A) (position[A##_AXIS] + target[A##_AXIS]) >> 1
  1296. const int32_t between[XYZE] = { _BETWEEN(X), _BETWEEN(Y), _BETWEEN(Z), _BETWEEN(E) };
  1297. DISABLE_STEPPER_DRIVER_INTERRUPT();
  1298. #if ENABLED(LIN_ADVANCE)
  1299. lin_dist_xy *= 0.5;
  1300. lin_dist_e *= 0.5;
  1301. #endif
  1302. _buffer_steps(between, fr_mm_s, extruder);
  1303. #if ENABLED(LIN_ADVANCE)
  1304. position_float[X_AXIS] = (position_float[X_AXIS] + a) * 0.5;
  1305. position_float[Y_AXIS] = (position_float[Y_AXIS] + b) * 0.5;
  1306. //position_float[Z_AXIS] = (position_float[Z_AXIS] + c) * 0.5;
  1307. position_float[E_AXIS] = (position_float[E_AXIS] + e) * 0.5;
  1308. #endif
  1309. const uint8_t next = block_buffer_head;
  1310. _buffer_steps(target, fr_mm_s, extruder);
  1311. SBI(block_buffer[next].flag, BLOCK_BIT_CONTINUED);
  1312. ENABLE_STEPPER_DRIVER_INTERRUPT();
  1313. }
  1314. else
  1315. _buffer_steps(target, fr_mm_s, extruder);
  1316. stepper.wake_up();
  1317. #if ENABLED(LIN_ADVANCE)
  1318. position_float[X_AXIS] = a;
  1319. position_float[Y_AXIS] = b;
  1320. //position_float[Z_AXIS] = c;
  1321. position_float[E_AXIS] = e;
  1322. #endif
  1323. } // buffer_segment()
  1324. /**
  1325. * Directly set the planner XYZ position (and stepper positions)
  1326. * converting mm (or angles for SCARA) into steps.
  1327. *
  1328. * On CORE machines stepper ABC will be translated from the given XYZ.
  1329. */
  1330. void Planner::_set_position_mm(const float &a, const float &b, const float &c, const float &e) {
  1331. #if ENABLED(DISTINCT_E_FACTORS)
  1332. #define _EINDEX (E_AXIS + active_extruder)
  1333. last_extruder = active_extruder;
  1334. #else
  1335. #define _EINDEX E_AXIS
  1336. #endif
  1337. const int32_t na = position[X_AXIS] = LROUND(a * axis_steps_per_mm[X_AXIS]),
  1338. nb = position[Y_AXIS] = LROUND(b * axis_steps_per_mm[Y_AXIS]),
  1339. nc = position[Z_AXIS] = LROUND(c * axis_steps_per_mm[Z_AXIS]),
  1340. ne = position[E_AXIS] = LROUND(e * axis_steps_per_mm[_EINDEX]);
  1341. #if ENABLED(LIN_ADVANCE)
  1342. position_float[X_AXIS] = a;
  1343. position_float[Y_AXIS] = b;
  1344. //position_float[Z_AXIS] = c;
  1345. position_float[E_AXIS] = e;
  1346. #endif
  1347. stepper.set_position(na, nb, nc, ne);
  1348. previous_nominal_speed = 0.0; // Resets planner junction speeds. Assumes start from rest.
  1349. ZERO(previous_speed);
  1350. }
  1351. void Planner::set_position_mm_kinematic(const float (&cart)[XYZE]) {
  1352. #if PLANNER_LEVELING
  1353. float raw[XYZ] = { cart[X_AXIS], cart[Y_AXIS], cart[Z_AXIS] };
  1354. apply_leveling(raw);
  1355. #else
  1356. const float (&raw)[XYZE] = cart;
  1357. #endif
  1358. #if IS_KINEMATIC
  1359. inverse_kinematics(raw);
  1360. _set_position_mm(delta[A_AXIS], delta[B_AXIS], delta[C_AXIS], cart[E_AXIS]);
  1361. #else
  1362. _set_position_mm(raw[X_AXIS], raw[Y_AXIS], raw[Z_AXIS], cart[E_AXIS]);
  1363. #endif
  1364. }
  1365. /**
  1366. * Sync from the stepper positions. (e.g., after an interrupted move)
  1367. */
  1368. void Planner::sync_from_steppers() {
  1369. LOOP_XYZE(i) {
  1370. position[i] = stepper.position((AxisEnum)i);
  1371. #if ENABLED(LIN_ADVANCE)
  1372. position_float[i] = position[i] * steps_to_mm[i
  1373. #if ENABLED(DISTINCT_E_FACTORS)
  1374. + (i == E_AXIS ? active_extruder : 0)
  1375. #endif
  1376. ];
  1377. #endif
  1378. }
  1379. }
  1380. /**
  1381. * Setters for planner position (also setting stepper position).
  1382. */
  1383. void Planner::set_position_mm(const AxisEnum axis, const float &v) {
  1384. #if ENABLED(DISTINCT_E_FACTORS)
  1385. const uint8_t axis_index = axis + (axis == E_AXIS ? active_extruder : 0);
  1386. last_extruder = active_extruder;
  1387. #else
  1388. const uint8_t axis_index = axis;
  1389. #endif
  1390. position[axis] = LROUND(v * axis_steps_per_mm[axis_index]);
  1391. #if ENABLED(LIN_ADVANCE)
  1392. position_float[axis] = v;
  1393. #endif
  1394. stepper.set_position(axis, v);
  1395. previous_speed[axis] = 0.0;
  1396. }
  1397. // Recalculate the steps/s^2 acceleration rates, based on the mm/s^2
  1398. void Planner::reset_acceleration_rates() {
  1399. #if ENABLED(DISTINCT_E_FACTORS)
  1400. #define HIGHEST_CONDITION (i < E_AXIS || i == E_AXIS + active_extruder)
  1401. #else
  1402. #define HIGHEST_CONDITION true
  1403. #endif
  1404. uint32_t highest_rate = 1;
  1405. LOOP_XYZE_N(i) {
  1406. max_acceleration_steps_per_s2[i] = max_acceleration_mm_per_s2[i] * axis_steps_per_mm[i];
  1407. if (HIGHEST_CONDITION) NOLESS(highest_rate, max_acceleration_steps_per_s2[i]);
  1408. }
  1409. cutoff_long = 4294967295UL / highest_rate;
  1410. }
  1411. // Recalculate position, steps_to_mm if axis_steps_per_mm changes!
  1412. void Planner::refresh_positioning() {
  1413. LOOP_XYZE_N(i) steps_to_mm[i] = 1.0 / axis_steps_per_mm[i];
  1414. set_position_mm_kinematic(current_position);
  1415. reset_acceleration_rates();
  1416. }
  1417. #if ENABLED(AUTOTEMP)
  1418. void Planner::autotemp_M104_M109() {
  1419. autotemp_enabled = parser.seen('F');
  1420. if (autotemp_enabled) autotemp_factor = parser.value_celsius_diff();
  1421. if (parser.seen('S')) autotemp_min = parser.value_celsius();
  1422. if (parser.seen('B')) autotemp_max = parser.value_celsius();
  1423. }
  1424. #endif