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

planner.cpp 56KB

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