My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

planner.cpp 56KB

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