My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

planner.cpp 55KB

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