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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244
  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 "Marlin.h"
  65. #if ENABLED(MESH_BED_LEVELING)
  66. #include "mesh_bed_leveling.h"
  67. #endif
  68. Planner planner;
  69. // public:
  70. /**
  71. * A ring buffer of moves described in steps
  72. */
  73. block_t Planner::block_buffer[BLOCK_BUFFER_SIZE];
  74. volatile uint8_t Planner::block_buffer_head = 0; // Index of the next block to be pushed
  75. volatile uint8_t Planner::block_buffer_tail = 0;
  76. float Planner::max_feedrate_mm_s[NUM_AXIS], // Max speeds in mm per second
  77. Planner::axis_steps_per_mm[NUM_AXIS],
  78. Planner::steps_to_mm[NUM_AXIS];
  79. unsigned long Planner::max_acceleration_steps_per_s2[NUM_AXIS],
  80. Planner::max_acceleration_mm_per_s2[NUM_AXIS]; // Use M201 to override by software
  81. millis_t Planner::min_segment_time;
  82. float Planner::min_feedrate_mm_s,
  83. Planner::acceleration, // Normal acceleration mm/s^2 DEFAULT ACCELERATION for all printing moves. M204 SXXXX
  84. Planner::retract_acceleration, // Retract acceleration mm/s^2 filament pull-back and push-forward while standing still in the other axes M204 TXXXX
  85. Planner::travel_acceleration, // Travel acceleration mm/s^2 DEFAULT ACCELERATION for all NON printing moves. M204 MXXXX
  86. Planner::max_jerk[XYZE], // The largest speed change requiring no acceleration
  87. Planner::min_travel_feedrate_mm_s;
  88. #if HAS_ABL
  89. bool Planner::abl_enabled = false; // Flag that auto bed leveling is enabled
  90. #endif
  91. #if ABL_PLANAR
  92. matrix_3x3 Planner::bed_level_matrix; // Transform to compensate for bed level
  93. #endif
  94. #if ENABLED(AUTOTEMP)
  95. float Planner::autotemp_max = 250,
  96. Planner::autotemp_min = 210,
  97. Planner::autotemp_factor = 0.1;
  98. bool Planner::autotemp_enabled = false;
  99. #endif
  100. // private:
  101. long Planner::position[NUM_AXIS] = { 0 };
  102. float Planner::previous_speed[NUM_AXIS],
  103. Planner::previous_nominal_speed;
  104. #if ENABLED(DISABLE_INACTIVE_EXTRUDER)
  105. uint8_t Planner::g_uc_extruder_last_move[EXTRUDERS] = { 0 };
  106. #endif // DISABLE_INACTIVE_EXTRUDER
  107. #ifdef XY_FREQUENCY_LIMIT
  108. // Old direction bits. Used for speed calculations
  109. unsigned char Planner::old_direction_bits = 0;
  110. // Segment times (in µs). Used for speed calculations
  111. long Planner::axis_segment_time[2][3] = { {MAX_FREQ_TIME + 1, 0, 0}, {MAX_FREQ_TIME + 1, 0, 0} };
  112. #endif
  113. /**
  114. * Class and Instance Methods
  115. */
  116. Planner::Planner() { init(); }
  117. void Planner::init() {
  118. block_buffer_head = block_buffer_tail = 0;
  119. memset(position, 0, sizeof(position));
  120. memset(previous_speed, 0, sizeof(previous_speed));
  121. previous_nominal_speed = 0.0;
  122. #if ABL_PLANAR
  123. bed_level_matrix.set_to_identity();
  124. #endif
  125. }
  126. /**
  127. * Calculate trapezoid parameters, multiplying the entry- and exit-speeds
  128. * by the provided factors.
  129. */
  130. void Planner::calculate_trapezoid_for_block(block_t* const block, const float &entry_factor, const float &exit_factor) {
  131. uint32_t initial_rate = ceil(block->nominal_rate * entry_factor),
  132. final_rate = ceil(block->nominal_rate * exit_factor); // (steps per second)
  133. // Limit minimal step rate (Otherwise the timer will overflow.)
  134. NOLESS(initial_rate, 120);
  135. NOLESS(final_rate, 120);
  136. int32_t accel = block->acceleration_steps_per_s2,
  137. accelerate_steps = ceil(estimate_acceleration_distance(initial_rate, block->nominal_rate, accel)),
  138. decelerate_steps = floor(estimate_acceleration_distance(block->nominal_rate, final_rate, -accel)),
  139. plateau_steps = block->step_event_count - accelerate_steps - decelerate_steps;
  140. // Is the Plateau of Nominal Rate smaller than nothing? That means no cruising, and we will
  141. // have to use intersection_distance() to calculate when to abort accel and start braking
  142. // in order to reach the final_rate exactly at the end of this block.
  143. if (plateau_steps < 0) {
  144. accelerate_steps = ceil(intersection_distance(initial_rate, final_rate, accel, block->step_event_count));
  145. NOLESS(accelerate_steps, 0); // Check limits due to numerical round-off
  146. 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)
  147. plateau_steps = 0;
  148. }
  149. #if ENABLED(ADVANCE)
  150. volatile int32_t initial_advance = block->advance * sq(entry_factor),
  151. final_advance = block->advance * sq(exit_factor);
  152. #endif // ADVANCE
  153. // block->accelerate_until = accelerate_steps;
  154. // block->decelerate_after = accelerate_steps+plateau_steps;
  155. CRITICAL_SECTION_START; // Fill variables used by the stepper in a critical section
  156. if (!block->busy) { // Don't update variables if block is busy.
  157. block->accelerate_until = accelerate_steps;
  158. block->decelerate_after = accelerate_steps + plateau_steps;
  159. block->initial_rate = initial_rate;
  160. block->final_rate = final_rate;
  161. #if ENABLED(ADVANCE)
  162. block->initial_advance = initial_advance;
  163. block->final_advance = final_advance;
  164. #endif
  165. }
  166. CRITICAL_SECTION_END;
  167. }
  168. // "Junction jerk" in this context is the immediate change in speed at the junction of two blocks.
  169. // This method will calculate the junction jerk as the euclidean distance between the nominal
  170. // velocities of the respective blocks.
  171. //inline float junction_jerk(block_t *before, block_t *after) {
  172. // return sqrt(
  173. // pow((before->speed_x-after->speed_x), 2)+pow((before->speed_y-after->speed_y), 2));
  174. //}
  175. // The kernel called by recalculate() when scanning the plan from last to first entry.
  176. void Planner::reverse_pass_kernel(block_t* const current, const block_t *next) {
  177. if (!current || !next) return;
  178. // If entry speed is already at the maximum entry speed, no need to recheck. Block is cruising.
  179. // If not, block in state of acceleration or deceleration. Reset entry speed to maximum and
  180. // check for maximum allowable speed reductions to ensure maximum possible planned speed.
  181. float max_entry_speed = current->max_entry_speed;
  182. if (current->entry_speed != max_entry_speed) {
  183. // If nominal length true, max junction speed is guaranteed to be reached. Only compute
  184. // for max allowable speed if block is decelerating and nominal length is false.
  185. current->entry_speed = ((current->flag & BLOCK_FLAG_NOMINAL_LENGTH) || max_entry_speed <= next->entry_speed)
  186. ? max_entry_speed
  187. : min(max_entry_speed, max_allowable_speed(-current->acceleration, next->entry_speed, current->millimeters));
  188. current->flag |= BLOCK_FLAG_RECALCULATE;
  189. }
  190. }
  191. /**
  192. * recalculate() needs to go over the current plan twice.
  193. * Once in reverse and once forward. This implements the reverse pass.
  194. */
  195. void Planner::reverse_pass() {
  196. if (movesplanned() > 3) {
  197. block_t* block[3] = { NULL, NULL, NULL };
  198. // Make a local copy of block_buffer_tail, because the interrupt can alter it
  199. CRITICAL_SECTION_START;
  200. uint8_t tail = block_buffer_tail;
  201. CRITICAL_SECTION_END
  202. uint8_t b = BLOCK_MOD(block_buffer_head - 3);
  203. while (b != tail) {
  204. b = prev_block_index(b);
  205. block[2] = block[1];
  206. block[1] = block[0];
  207. block[0] = &block_buffer[b];
  208. reverse_pass_kernel(block[1], block[2]);
  209. }
  210. }
  211. }
  212. // The kernel called by recalculate() when scanning the plan from first to last entry.
  213. void Planner::forward_pass_kernel(const block_t* previous, block_t* const current) {
  214. if (!previous) return;
  215. // If the previous block is an acceleration block, but it is not long enough to complete the
  216. // full speed change within the block, we need to adjust the entry speed accordingly. Entry
  217. // speeds have already been reset, maximized, and reverse planned by reverse planner.
  218. // If nominal length is true, max junction speed is guaranteed to be reached. No need to recheck.
  219. if (!(previous->flag & BLOCK_FLAG_NOMINAL_LENGTH)) {
  220. if (previous->entry_speed < current->entry_speed) {
  221. float entry_speed = min(current->entry_speed,
  222. max_allowable_speed(-previous->acceleration, previous->entry_speed, previous->millimeters));
  223. // Check for junction speed change
  224. if (current->entry_speed != entry_speed) {
  225. current->entry_speed = entry_speed;
  226. current->flag |= BLOCK_FLAG_RECALCULATE;
  227. }
  228. }
  229. }
  230. }
  231. /**
  232. * recalculate() needs to go over the current plan twice.
  233. * Once in reverse and once forward. This implements the forward pass.
  234. */
  235. void Planner::forward_pass() {
  236. block_t* block[3] = { NULL, NULL, NULL };
  237. for (uint8_t b = block_buffer_tail; b != block_buffer_head; b = next_block_index(b)) {
  238. block[0] = block[1];
  239. block[1] = block[2];
  240. block[2] = &block_buffer[b];
  241. forward_pass_kernel(block[0], block[1]);
  242. }
  243. forward_pass_kernel(block[1], block[2]);
  244. }
  245. /**
  246. * Recalculate the trapezoid speed profiles for all blocks in the plan
  247. * according to the entry_factor for each junction. Must be called by
  248. * recalculate() after updating the blocks.
  249. */
  250. void Planner::recalculate_trapezoids() {
  251. int8_t block_index = block_buffer_tail;
  252. block_t *current, *next = NULL;
  253. while (block_index != block_buffer_head) {
  254. current = next;
  255. next = &block_buffer[block_index];
  256. if (current) {
  257. // Recalculate if current block entry or exit junction speed has changed.
  258. if ((current->flag & BLOCK_FLAG_RECALCULATE) || (next->flag & BLOCK_FLAG_RECALCULATE)) {
  259. // NOTE: Entry and exit factors always > 0 by all previous logic operations.
  260. float nom = current->nominal_speed;
  261. calculate_trapezoid_for_block(current, current->entry_speed / nom, next->entry_speed / nom);
  262. current->flag &= ~BLOCK_FLAG_RECALCULATE; // Reset current only to ensure next trapezoid is computed
  263. }
  264. }
  265. block_index = next_block_index(block_index);
  266. }
  267. // Last/newest block in buffer. Exit speed is set with MINIMUM_PLANNER_SPEED. Always recalculated.
  268. if (next) {
  269. float nom = next->nominal_speed;
  270. calculate_trapezoid_for_block(next, next->entry_speed / nom, (MINIMUM_PLANNER_SPEED) / nom);
  271. next->flag &= ~BLOCK_FLAG_RECALCULATE;
  272. }
  273. }
  274. /*
  275. * Recalculate the motion plan according to the following algorithm:
  276. *
  277. * 1. Go over every block in reverse order...
  278. *
  279. * Calculate a junction speed reduction (block_t.entry_factor) so:
  280. *
  281. * a. The junction jerk is within the set limit, and
  282. *
  283. * b. No speed reduction within one block requires faster
  284. * deceleration than the one, true constant acceleration.
  285. *
  286. * 2. Go over every block in chronological order...
  287. *
  288. * Dial down junction speed reduction values if:
  289. * a. The speed increase within one block would require faster
  290. * acceleration than the one, true constant acceleration.
  291. *
  292. * After that, all blocks will have an entry_factor allowing all speed changes to
  293. * be performed using only the one, true constant acceleration, and where no junction
  294. * jerk is jerkier than the set limit, Jerky. Finally it will:
  295. *
  296. * 3. Recalculate "trapezoids" for all blocks.
  297. */
  298. void Planner::recalculate() {
  299. reverse_pass();
  300. forward_pass();
  301. recalculate_trapezoids();
  302. }
  303. #if ENABLED(AUTOTEMP)
  304. void Planner::getHighESpeed() {
  305. static float oldt = 0;
  306. if (!autotemp_enabled) return;
  307. if (thermalManager.degTargetHotend(0) + 2 < autotemp_min) return; // probably temperature set to zero.
  308. float high = 0.0;
  309. for (uint8_t b = block_buffer_tail; b != block_buffer_head; b = next_block_index(b)) {
  310. block_t* block = &block_buffer[b];
  311. if (block->steps[X_AXIS] || block->steps[Y_AXIS] || block->steps[Z_AXIS]) {
  312. float se = (float)block->steps[E_AXIS] / block->step_event_count * block->nominal_speed; // mm/sec;
  313. NOLESS(high, se);
  314. }
  315. }
  316. float t = autotemp_min + high * autotemp_factor;
  317. t = constrain(t, autotemp_min, autotemp_max);
  318. if (oldt > t) {
  319. t *= (1 - (AUTOTEMP_OLDWEIGHT));
  320. t += (AUTOTEMP_OLDWEIGHT) * oldt;
  321. }
  322. oldt = t;
  323. thermalManager.setTargetHotend(t, 0);
  324. }
  325. #endif //AUTOTEMP
  326. /**
  327. * Maintain fans, paste extruder pressure,
  328. */
  329. void Planner::check_axes_activity() {
  330. unsigned char axis_active[NUM_AXIS] = { 0 },
  331. tail_fan_speed[FAN_COUNT];
  332. #if FAN_COUNT > 0
  333. for (uint8_t i = 0; i < FAN_COUNT; i++) tail_fan_speed[i] = fanSpeeds[i];
  334. #endif
  335. #if ENABLED(BARICUDA)
  336. #if HAS_HEATER_1
  337. unsigned char tail_valve_pressure = baricuda_valve_pressure;
  338. #endif
  339. #if HAS_HEATER_2
  340. unsigned char tail_e_to_p_pressure = baricuda_e_to_p_pressure;
  341. #endif
  342. #endif
  343. if (blocks_queued()) {
  344. #if FAN_COUNT > 0
  345. for (uint8_t i = 0; i < FAN_COUNT; i++) tail_fan_speed[i] = block_buffer[block_buffer_tail].fan_speed[i];
  346. #endif
  347. block_t* block;
  348. #if ENABLED(BARICUDA)
  349. block = &block_buffer[block_buffer_tail];
  350. #if HAS_HEATER_1
  351. tail_valve_pressure = block->valve_pressure;
  352. #endif
  353. #if HAS_HEATER_2
  354. tail_e_to_p_pressure = block->e_to_p_pressure;
  355. #endif
  356. #endif
  357. for (uint8_t b = block_buffer_tail; b != block_buffer_head; b = next_block_index(b)) {
  358. block = &block_buffer[b];
  359. LOOP_XYZE(i) if (block->steps[i]) axis_active[i]++;
  360. }
  361. }
  362. #if ENABLED(DISABLE_X)
  363. if (!axis_active[X_AXIS]) disable_x();
  364. #endif
  365. #if ENABLED(DISABLE_Y)
  366. if (!axis_active[Y_AXIS]) disable_y();
  367. #endif
  368. #if ENABLED(DISABLE_Z)
  369. if (!axis_active[Z_AXIS]) disable_z();
  370. #endif
  371. #if ENABLED(DISABLE_E)
  372. if (!axis_active[E_AXIS]) {
  373. disable_e0();
  374. disable_e1();
  375. disable_e2();
  376. disable_e3();
  377. }
  378. #endif
  379. #if FAN_COUNT > 0
  380. #if defined(FAN_MIN_PWM)
  381. #define CALC_FAN_SPEED(f) (tail_fan_speed[f] ? ( FAN_MIN_PWM + (tail_fan_speed[f] * (255 - FAN_MIN_PWM)) / 255 ) : 0)
  382. #else
  383. #define CALC_FAN_SPEED(f) tail_fan_speed[f]
  384. #endif
  385. #ifdef FAN_KICKSTART_TIME
  386. static millis_t fan_kick_end[FAN_COUNT] = { 0 };
  387. #define KICKSTART_FAN(f) \
  388. if (tail_fan_speed[f]) { \
  389. millis_t ms = millis(); \
  390. if (fan_kick_end[f] == 0) { \
  391. fan_kick_end[f] = ms + FAN_KICKSTART_TIME; \
  392. tail_fan_speed[f] = 255; \
  393. } else { \
  394. if (PENDING(ms, fan_kick_end[f])) { \
  395. tail_fan_speed[f] = 255; \
  396. } \
  397. } \
  398. } else { \
  399. fan_kick_end[f] = 0; \
  400. }
  401. #if HAS_FAN0
  402. KICKSTART_FAN(0);
  403. #endif
  404. #if HAS_FAN1
  405. KICKSTART_FAN(1);
  406. #endif
  407. #if HAS_FAN2
  408. KICKSTART_FAN(2);
  409. #endif
  410. #endif //FAN_KICKSTART_TIME
  411. #if ENABLED(FAN_SOFT_PWM)
  412. #if HAS_FAN0
  413. thermalManager.fanSpeedSoftPwm[0] = CALC_FAN_SPEED(0);
  414. #endif
  415. #if HAS_FAN1
  416. thermalManager.fanSpeedSoftPwm[1] = CALC_FAN_SPEED(1);
  417. #endif
  418. #if HAS_FAN2
  419. thermalManager.fanSpeedSoftPwm[2] = CALC_FAN_SPEED(2);
  420. #endif
  421. #else
  422. #if HAS_FAN0
  423. analogWrite(FAN_PIN, CALC_FAN_SPEED(0));
  424. #endif
  425. #if HAS_FAN1
  426. analogWrite(FAN1_PIN, CALC_FAN_SPEED(1));
  427. #endif
  428. #if HAS_FAN2
  429. analogWrite(FAN2_PIN, CALC_FAN_SPEED(2));
  430. #endif
  431. #endif
  432. #endif // FAN_COUNT > 0
  433. #if ENABLED(AUTOTEMP)
  434. getHighESpeed();
  435. #endif
  436. #if ENABLED(BARICUDA)
  437. #if HAS_HEATER_1
  438. analogWrite(HEATER_1_PIN, tail_valve_pressure);
  439. #endif
  440. #if HAS_HEATER_2
  441. analogWrite(HEATER_2_PIN, tail_e_to_p_pressure);
  442. #endif
  443. #endif
  444. }
  445. #if PLANNER_LEVELING
  446. /**
  447. * lx, ly, lz - logical (cartesian, not delta) positions in mm
  448. */
  449. void Planner::apply_leveling(float &lx, float &ly, float &lz) {
  450. #if HAS_ABL
  451. if (!abl_enabled) return;
  452. #endif
  453. #if ENABLED(MESH_BED_LEVELING)
  454. if (mbl.active())
  455. lz += mbl.get_z(RAW_X_POSITION(lx), RAW_Y_POSITION(ly));
  456. #elif ABL_PLANAR
  457. float dx = RAW_X_POSITION(lx) - (X_TILT_FULCRUM),
  458. dy = RAW_Y_POSITION(ly) - (Y_TILT_FULCRUM),
  459. dz = RAW_Z_POSITION(lz);
  460. apply_rotation_xyz(bed_level_matrix, dx, dy, dz);
  461. lx = LOGICAL_X_POSITION(dx + X_TILT_FULCRUM);
  462. ly = LOGICAL_Y_POSITION(dy + Y_TILT_FULCRUM);
  463. lz = LOGICAL_Z_POSITION(dz);
  464. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  465. float tmp[XYZ] = { lx, ly, 0 };
  466. lz += bilinear_z_offset(tmp);
  467. #endif
  468. }
  469. void Planner::unapply_leveling(float logical[XYZ]) {
  470. #if HAS_ABL
  471. if (!abl_enabled) return;
  472. #endif
  473. #if ENABLED(MESH_BED_LEVELING)
  474. if (mbl.active())
  475. logical[Z_AXIS] -= mbl.get_z(RAW_X_POSITION(logical[X_AXIS]), RAW_Y_POSITION(logical[Y_AXIS]));
  476. #elif ABL_PLANAR
  477. matrix_3x3 inverse = matrix_3x3::transpose(bed_level_matrix);
  478. float dx = RAW_X_POSITION(logical[X_AXIS]) - (X_TILT_FULCRUM),
  479. dy = RAW_Y_POSITION(logical[Y_AXIS]) - (Y_TILT_FULCRUM),
  480. dz = RAW_Z_POSITION(logical[Z_AXIS]);
  481. apply_rotation_xyz(inverse, dx, dy, dz);
  482. logical[X_AXIS] = LOGICAL_X_POSITION(dx + X_TILT_FULCRUM);
  483. logical[Y_AXIS] = LOGICAL_Y_POSITION(dy + Y_TILT_FULCRUM);
  484. logical[Z_AXIS] = LOGICAL_Z_POSITION(dz);
  485. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  486. logical[Z_AXIS] -= bilinear_z_offset(logical);
  487. #endif
  488. }
  489. #endif // PLANNER_LEVELING
  490. /**
  491. * Planner::_buffer_line
  492. *
  493. * Add a new linear movement to the buffer.
  494. *
  495. * Leveling and kinematics should be applied ahead of calling this.
  496. *
  497. * a,b,c,e - target positions in mm or degrees
  498. * fr_mm_s - (target) speed of the move
  499. * extruder - target extruder
  500. */
  501. void Planner::_buffer_line(const float &a, const float &b, const float &c, const float &e, float fr_mm_s, const uint8_t extruder) {
  502. // Calculate the buffer head after we push this byte
  503. int next_buffer_head = next_block_index(block_buffer_head);
  504. // If the buffer is full: good! That means we are well ahead of the robot.
  505. // Rest here until there is room in the buffer.
  506. while (block_buffer_tail == next_buffer_head) idle();
  507. // The target position of the tool in absolute steps
  508. // Calculate target position in absolute steps
  509. //this should be done after the wait, because otherwise a M92 code within the gcode disrupts this calculation somehow
  510. long target[XYZE] = {
  511. lround(a * axis_steps_per_mm[X_AXIS]),
  512. lround(b * axis_steps_per_mm[Y_AXIS]),
  513. lround(c * axis_steps_per_mm[Z_AXIS]),
  514. lround(e * axis_steps_per_mm[E_AXIS])
  515. };
  516. long da = target[X_AXIS] - position[X_AXIS],
  517. db = target[Y_AXIS] - position[Y_AXIS],
  518. dc = target[Z_AXIS] - position[Z_AXIS];
  519. /*
  520. SERIAL_ECHOPAIR(" Planner FR:", fr_mm_s);
  521. SERIAL_CHAR(' ');
  522. #if IS_KINEMATIC
  523. SERIAL_ECHOPAIR("A:", a);
  524. SERIAL_ECHOPAIR(" (", da);
  525. SERIAL_ECHOPAIR(") B:", b);
  526. #else
  527. SERIAL_ECHOPAIR("X:", a);
  528. SERIAL_ECHOPAIR(" (", da);
  529. SERIAL_ECHOPAIR(") Y:", b);
  530. #endif
  531. SERIAL_ECHOPAIR(" (", db);
  532. #if ENABLED(DELTA)
  533. SERIAL_ECHOPAIR(") C:", c);
  534. #else
  535. SERIAL_ECHOPAIR(") Z:", c);
  536. #endif
  537. SERIAL_ECHOPAIR(" (", dc);
  538. SERIAL_CHAR(')');
  539. SERIAL_EOL;
  540. //*/
  541. // DRYRUN ignores all temperature constraints and assures that the extruder is instantly satisfied
  542. if (DEBUGGING(DRYRUN)) position[E_AXIS] = target[E_AXIS];
  543. long de = target[E_AXIS] - position[E_AXIS];
  544. #if ENABLED(PREVENT_COLD_EXTRUSION)
  545. if (de) {
  546. if (thermalManager.tooColdToExtrude(extruder)) {
  547. position[E_AXIS] = target[E_AXIS]; // Behave as if the move really took place, but ignore E part
  548. de = 0; // no difference
  549. SERIAL_ECHO_START;
  550. SERIAL_ECHOLNPGM(MSG_ERR_COLD_EXTRUDE_STOP);
  551. }
  552. #if ENABLED(PREVENT_LENGTHY_EXTRUDE)
  553. if (labs(de) > axis_steps_per_mm[E_AXIS] * (EXTRUDE_MAXLENGTH)) {
  554. position[E_AXIS] = target[E_AXIS]; // Behave as if the move really took place, but ignore E part
  555. de = 0; // no difference
  556. SERIAL_ECHO_START;
  557. SERIAL_ECHOLNPGM(MSG_ERR_LONG_EXTRUDE_STOP);
  558. }
  559. #endif
  560. }
  561. #endif
  562. // Prepare to set up new block
  563. block_t* block = &block_buffer[block_buffer_head];
  564. // Mark block as not busy (Not executed by the stepper interrupt)
  565. block->busy = false;
  566. // Number of steps for each axis
  567. #if ENABLED(COREXY)
  568. // corexy planning
  569. // these equations follow the form of the dA and dB equations on http://www.corexy.com/theory.html
  570. block->steps[A_AXIS] = labs(da + db);
  571. block->steps[B_AXIS] = labs(da - db);
  572. block->steps[Z_AXIS] = labs(dc);
  573. #elif ENABLED(COREXZ)
  574. // corexz planning
  575. block->steps[A_AXIS] = labs(da + dc);
  576. block->steps[Y_AXIS] = labs(db);
  577. block->steps[C_AXIS] = labs(da - dc);
  578. #elif ENABLED(COREYZ)
  579. // coreyz planning
  580. block->steps[X_AXIS] = labs(da);
  581. block->steps[B_AXIS] = labs(db + dc);
  582. block->steps[C_AXIS] = labs(db - dc);
  583. #else
  584. // default non-h-bot planning
  585. block->steps[X_AXIS] = labs(da);
  586. block->steps[Y_AXIS] = labs(db);
  587. block->steps[Z_AXIS] = labs(dc);
  588. #endif
  589. block->steps[E_AXIS] = labs(de) * volumetric_multiplier[extruder] * flow_percentage[extruder] * 0.01 + 0.5;
  590. block->step_event_count = MAX4(block->steps[X_AXIS], block->steps[Y_AXIS], block->steps[Z_AXIS], block->steps[E_AXIS]);
  591. // Bail if this is a zero-length block
  592. if (block->step_event_count < MIN_STEPS_PER_SEGMENT) return;
  593. // For a mixing extruder, get a magnified step_event_count for each
  594. #if ENABLED(MIXING_EXTRUDER)
  595. for (uint8_t i = 0; i < MIXING_STEPPERS; i++)
  596. block->mix_event_count[i] = UNEAR_ZERO(mixing_factor[i]) ? 0 : block->step_event_count / mixing_factor[i];
  597. #endif
  598. #if FAN_COUNT > 0
  599. for (uint8_t i = 0; i < FAN_COUNT; i++) block->fan_speed[i] = fanSpeeds[i];
  600. #endif
  601. #if ENABLED(BARICUDA)
  602. block->valve_pressure = baricuda_valve_pressure;
  603. block->e_to_p_pressure = baricuda_e_to_p_pressure;
  604. #endif
  605. // Compute direction bit-mask for this block
  606. uint8_t dm = 0;
  607. #if ENABLED(COREXY)
  608. if (da < 0) SBI(dm, X_HEAD); // Save the real Extruder (head) direction in X Axis
  609. if (db < 0) SBI(dm, Y_HEAD); // ...and Y
  610. if (dc < 0) SBI(dm, Z_AXIS);
  611. if (da + db < 0) SBI(dm, A_AXIS); // Motor A direction
  612. if (da - db < 0) SBI(dm, B_AXIS); // Motor B direction
  613. #elif ENABLED(COREXZ)
  614. if (da < 0) SBI(dm, X_HEAD); // Save the real Extruder (head) direction in X Axis
  615. if (db < 0) SBI(dm, Y_AXIS);
  616. if (dc < 0) SBI(dm, Z_HEAD); // ...and Z
  617. if (da + dc < 0) SBI(dm, A_AXIS); // Motor A direction
  618. if (da - dc < 0) SBI(dm, C_AXIS); // Motor C direction
  619. #elif ENABLED(COREYZ)
  620. if (da < 0) SBI(dm, X_AXIS);
  621. if (db < 0) SBI(dm, Y_HEAD); // Save the real Extruder (head) direction in Y Axis
  622. if (dc < 0) SBI(dm, Z_HEAD); // ...and Z
  623. if (db + dc < 0) SBI(dm, B_AXIS); // Motor B direction
  624. if (db - dc < 0) SBI(dm, C_AXIS); // Motor C direction
  625. #else
  626. if (da < 0) SBI(dm, X_AXIS);
  627. if (db < 0) SBI(dm, Y_AXIS);
  628. if (dc < 0) SBI(dm, Z_AXIS);
  629. #endif
  630. if (de < 0) SBI(dm, E_AXIS);
  631. block->direction_bits = dm;
  632. block->active_extruder = extruder;
  633. //enable active axes
  634. #if ENABLED(COREXY)
  635. if (block->steps[A_AXIS] || block->steps[B_AXIS]) {
  636. enable_x();
  637. enable_y();
  638. }
  639. #if DISABLED(Z_LATE_ENABLE)
  640. if (block->steps[Z_AXIS]) enable_z();
  641. #endif
  642. #elif ENABLED(COREXZ)
  643. if (block->steps[A_AXIS] || block->steps[C_AXIS]) {
  644. enable_x();
  645. enable_z();
  646. }
  647. if (block->steps[Y_AXIS]) enable_y();
  648. #else
  649. if (block->steps[X_AXIS]) enable_x();
  650. if (block->steps[Y_AXIS]) enable_y();
  651. #if DISABLED(Z_LATE_ENABLE)
  652. if (block->steps[Z_AXIS]) enable_z();
  653. #endif
  654. #endif
  655. // Enable extruder(s)
  656. if (block->steps[E_AXIS]) {
  657. #if ENABLED(DISABLE_INACTIVE_EXTRUDER) // Enable only the selected extruder
  658. for (int i = 0; i < EXTRUDERS; i++)
  659. if (g_uc_extruder_last_move[i] > 0) g_uc_extruder_last_move[i]--;
  660. switch(extruder) {
  661. case 0:
  662. enable_e0();
  663. #if ENABLED(DUAL_X_CARRIAGE)
  664. if (extruder_duplication_enabled) {
  665. enable_e1();
  666. g_uc_extruder_last_move[1] = (BLOCK_BUFFER_SIZE) * 2;
  667. }
  668. #endif
  669. g_uc_extruder_last_move[0] = (BLOCK_BUFFER_SIZE) * 2;
  670. #if EXTRUDERS > 1
  671. if (g_uc_extruder_last_move[1] == 0) disable_e1();
  672. #if EXTRUDERS > 2
  673. if (g_uc_extruder_last_move[2] == 0) disable_e2();
  674. #if EXTRUDERS > 3
  675. if (g_uc_extruder_last_move[3] == 0) disable_e3();
  676. #endif
  677. #endif
  678. #endif
  679. break;
  680. #if EXTRUDERS > 1
  681. case 1:
  682. enable_e1();
  683. g_uc_extruder_last_move[1] = (BLOCK_BUFFER_SIZE) * 2;
  684. if (g_uc_extruder_last_move[0] == 0) disable_e0();
  685. #if EXTRUDERS > 2
  686. if (g_uc_extruder_last_move[2] == 0) disable_e2();
  687. #if EXTRUDERS > 3
  688. if (g_uc_extruder_last_move[3] == 0) disable_e3();
  689. #endif
  690. #endif
  691. break;
  692. #if EXTRUDERS > 2
  693. case 2:
  694. enable_e2();
  695. g_uc_extruder_last_move[2] = (BLOCK_BUFFER_SIZE) * 2;
  696. if (g_uc_extruder_last_move[0] == 0) disable_e0();
  697. if (g_uc_extruder_last_move[1] == 0) disable_e1();
  698. #if EXTRUDERS > 3
  699. if (g_uc_extruder_last_move[3] == 0) disable_e3();
  700. #endif
  701. break;
  702. #if EXTRUDERS > 3
  703. case 3:
  704. enable_e3();
  705. g_uc_extruder_last_move[3] = (BLOCK_BUFFER_SIZE) * 2;
  706. if (g_uc_extruder_last_move[0] == 0) disable_e0();
  707. if (g_uc_extruder_last_move[1] == 0) disable_e1();
  708. if (g_uc_extruder_last_move[2] == 0) disable_e2();
  709. break;
  710. #endif // EXTRUDERS > 3
  711. #endif // EXTRUDERS > 2
  712. #endif // EXTRUDERS > 1
  713. }
  714. #else
  715. enable_e0();
  716. enable_e1();
  717. enable_e2();
  718. enable_e3();
  719. #endif
  720. }
  721. if (block->steps[E_AXIS])
  722. NOLESS(fr_mm_s, min_feedrate_mm_s);
  723. else
  724. NOLESS(fr_mm_s, min_travel_feedrate_mm_s);
  725. /**
  726. * This part of the code calculates the total length of the movement.
  727. * For cartesian bots, the X_AXIS is the real X movement and same for Y_AXIS.
  728. * But for corexy bots, that is not true. The "X_AXIS" and "Y_AXIS" motors (that should be named to A_AXIS
  729. * and B_AXIS) cannot be used for X and Y length, because A=X+Y and B=X-Y.
  730. * So we need to create other 2 "AXIS", named X_HEAD and Y_HEAD, meaning the real displacement of the Head.
  731. * Having the real displacement of the head, we can calculate the total movement length and apply the desired speed.
  732. */
  733. #if ENABLED(COREXY) || ENABLED(COREXZ) || ENABLED(COREYZ)
  734. float delta_mm[7];
  735. #if ENABLED(COREXY)
  736. delta_mm[X_HEAD] = da * steps_to_mm[A_AXIS];
  737. delta_mm[Y_HEAD] = db * steps_to_mm[B_AXIS];
  738. delta_mm[Z_AXIS] = dc * steps_to_mm[Z_AXIS];
  739. delta_mm[A_AXIS] = (da + db) * steps_to_mm[A_AXIS];
  740. delta_mm[B_AXIS] = (da - db) * steps_to_mm[B_AXIS];
  741. #elif ENABLED(COREXZ)
  742. delta_mm[X_HEAD] = da * steps_to_mm[A_AXIS];
  743. delta_mm[Y_AXIS] = db * steps_to_mm[Y_AXIS];
  744. delta_mm[Z_HEAD] = dc * steps_to_mm[C_AXIS];
  745. delta_mm[A_AXIS] = (da + dc) * steps_to_mm[A_AXIS];
  746. delta_mm[C_AXIS] = (da - dc) * steps_to_mm[C_AXIS];
  747. #elif ENABLED(COREYZ)
  748. delta_mm[X_AXIS] = da * steps_to_mm[X_AXIS];
  749. delta_mm[Y_HEAD] = db * steps_to_mm[B_AXIS];
  750. delta_mm[Z_HEAD] = dc * steps_to_mm[C_AXIS];
  751. delta_mm[B_AXIS] = (db + dc) * steps_to_mm[B_AXIS];
  752. delta_mm[C_AXIS] = (db - dc) * steps_to_mm[C_AXIS];
  753. #endif
  754. #else
  755. float delta_mm[4];
  756. delta_mm[X_AXIS] = da * steps_to_mm[X_AXIS];
  757. delta_mm[Y_AXIS] = db * steps_to_mm[Y_AXIS];
  758. delta_mm[Z_AXIS] = dc * steps_to_mm[Z_AXIS];
  759. #endif
  760. delta_mm[E_AXIS] = 0.01 * (de * steps_to_mm[E_AXIS]) * volumetric_multiplier[extruder] * flow_percentage[extruder];
  761. 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) {
  762. block->millimeters = fabs(delta_mm[E_AXIS]);
  763. }
  764. else {
  765. block->millimeters = sqrt(
  766. #if ENABLED(COREXY)
  767. sq(delta_mm[X_HEAD]) + sq(delta_mm[Y_HEAD]) + sq(delta_mm[Z_AXIS])
  768. #elif ENABLED(COREXZ)
  769. sq(delta_mm[X_HEAD]) + sq(delta_mm[Y_AXIS]) + sq(delta_mm[Z_HEAD])
  770. #elif ENABLED(COREYZ)
  771. sq(delta_mm[X_AXIS]) + sq(delta_mm[Y_HEAD]) + sq(delta_mm[Z_HEAD])
  772. #else
  773. sq(delta_mm[X_AXIS]) + sq(delta_mm[Y_AXIS]) + sq(delta_mm[Z_AXIS])
  774. #endif
  775. );
  776. }
  777. float inverse_millimeters = 1.0 / block->millimeters; // Inverse millimeters to remove multiple divides
  778. // Calculate moves/second for this move. No divide by zero due to previous checks.
  779. float inverse_mm_s = fr_mm_s * inverse_millimeters;
  780. int moves_queued = movesplanned();
  781. // Slow down when the buffer starts to empty, rather than wait at the corner for a buffer refill
  782. #if ENABLED(OLD_SLOWDOWN) || ENABLED(SLOWDOWN)
  783. bool mq = moves_queued > 1 && moves_queued < (BLOCK_BUFFER_SIZE) / 2;
  784. #if ENABLED(OLD_SLOWDOWN)
  785. if (mq) fr_mm_s *= 2.0 * moves_queued / (BLOCK_BUFFER_SIZE);
  786. #endif
  787. #if ENABLED(SLOWDOWN)
  788. // segment time im micro seconds
  789. unsigned long segment_time = lround(1000000.0/inverse_mm_s);
  790. if (mq) {
  791. if (segment_time < min_segment_time) {
  792. // buffer is draining, add extra time. The amount of time added increases if the buffer is still emptied more.
  793. inverse_mm_s = 1000000.0 / (segment_time + lround(2 * (min_segment_time - segment_time) / moves_queued));
  794. #ifdef XY_FREQUENCY_LIMIT
  795. segment_time = lround(1000000.0 / inverse_mm_s);
  796. #endif
  797. }
  798. }
  799. #endif
  800. #endif
  801. block->nominal_speed = block->millimeters * inverse_mm_s; // (mm/sec) Always > 0
  802. block->nominal_rate = ceil(block->step_event_count * inverse_mm_s); // (step/sec) Always > 0
  803. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  804. static float filwidth_e_count = 0, filwidth_delay_dist = 0;
  805. //FMM update ring buffer used for delay with filament measurements
  806. if (extruder == FILAMENT_SENSOR_EXTRUDER_NUM && filwidth_delay_index[1] >= 0) { //only for extruder with filament sensor and if ring buffer is initialized
  807. const int MMD_CM = MAX_MEASUREMENT_DELAY + 1, MMD_MM = MMD_CM * 10;
  808. // increment counters with next move in e axis
  809. filwidth_e_count += delta_mm[E_AXIS];
  810. filwidth_delay_dist += delta_mm[E_AXIS];
  811. // Only get new measurements on forward E movement
  812. if (filwidth_e_count > 0.0001) {
  813. // Loop the delay distance counter (modulus by the mm length)
  814. while (filwidth_delay_dist >= MMD_MM) filwidth_delay_dist -= MMD_MM;
  815. // Convert into an index into the measurement array
  816. filwidth_delay_index[0] = (int)(filwidth_delay_dist * 0.1 + 0.0001);
  817. // If the index has changed (must have gone forward)...
  818. if (filwidth_delay_index[0] != filwidth_delay_index[1]) {
  819. filwidth_e_count = 0; // Reset the E movement counter
  820. int8_t meas_sample = thermalManager.widthFil_to_size_ratio() - 100; // Subtract 100 to reduce magnitude - to store in a signed char
  821. do {
  822. filwidth_delay_index[1] = (filwidth_delay_index[1] + 1) % MMD_CM; // The next unused slot
  823. measurement_delay[filwidth_delay_index[1]] = meas_sample; // Store the measurement
  824. } while (filwidth_delay_index[0] != filwidth_delay_index[1]); // More slots to fill?
  825. }
  826. }
  827. }
  828. #endif
  829. // Calculate and limit speed in mm/sec for each axis
  830. float current_speed[NUM_AXIS], speed_factor = 1.0; // factor <1 decreases speed
  831. LOOP_XYZE(i) {
  832. float cs = fabs(current_speed[i] = delta_mm[i] * inverse_mm_s);
  833. if (cs > max_feedrate_mm_s[i]) NOMORE(speed_factor, max_feedrate_mm_s[i] / cs);
  834. }
  835. // Max segment time in µs.
  836. #ifdef XY_FREQUENCY_LIMIT
  837. // Check and limit the xy direction change frequency
  838. unsigned char direction_change = block->direction_bits ^ old_direction_bits;
  839. old_direction_bits = block->direction_bits;
  840. segment_time = lround((float)segment_time / speed_factor);
  841. long xs0 = axis_segment_time[X_AXIS][0],
  842. xs1 = axis_segment_time[X_AXIS][1],
  843. xs2 = axis_segment_time[X_AXIS][2],
  844. ys0 = axis_segment_time[Y_AXIS][0],
  845. ys1 = axis_segment_time[Y_AXIS][1],
  846. ys2 = axis_segment_time[Y_AXIS][2];
  847. if (TEST(direction_change, X_AXIS)) {
  848. xs2 = axis_segment_time[X_AXIS][2] = xs1;
  849. xs1 = axis_segment_time[X_AXIS][1] = xs0;
  850. xs0 = 0;
  851. }
  852. xs0 = axis_segment_time[X_AXIS][0] = xs0 + segment_time;
  853. if (TEST(direction_change, Y_AXIS)) {
  854. ys2 = axis_segment_time[Y_AXIS][2] = axis_segment_time[Y_AXIS][1];
  855. ys1 = axis_segment_time[Y_AXIS][1] = axis_segment_time[Y_AXIS][0];
  856. ys0 = 0;
  857. }
  858. ys0 = axis_segment_time[Y_AXIS][0] = ys0 + segment_time;
  859. long max_x_segment_time = MAX3(xs0, xs1, xs2),
  860. max_y_segment_time = MAX3(ys0, ys1, ys2),
  861. min_xy_segment_time = min(max_x_segment_time, max_y_segment_time);
  862. if (min_xy_segment_time < MAX_FREQ_TIME) {
  863. float low_sf = speed_factor * min_xy_segment_time / (MAX_FREQ_TIME);
  864. NOMORE(speed_factor, low_sf);
  865. }
  866. #endif // XY_FREQUENCY_LIMIT
  867. // Correct the speed
  868. if (speed_factor < 1.0) {
  869. LOOP_XYZE(i) current_speed[i] *= speed_factor;
  870. block->nominal_speed *= speed_factor;
  871. block->nominal_rate *= speed_factor;
  872. }
  873. // Compute and limit the acceleration rate for the trapezoid generator.
  874. float steps_per_mm = block->step_event_count / block->millimeters;
  875. if (!block->steps[X_AXIS] && !block->steps[Y_AXIS] && !block->steps[Z_AXIS]) {
  876. block->acceleration_steps_per_s2 = ceil(retract_acceleration * steps_per_mm); // convert to: acceleration steps/sec^2
  877. }
  878. else {
  879. // Limit acceleration per axis
  880. block->acceleration_steps_per_s2 = ceil((block->steps[E_AXIS] ? acceleration : travel_acceleration) * steps_per_mm);
  881. if (max_acceleration_steps_per_s2[X_AXIS] < (block->acceleration_steps_per_s2 * block->steps[X_AXIS]) / block->step_event_count)
  882. block->acceleration_steps_per_s2 = (max_acceleration_steps_per_s2[X_AXIS] * block->step_event_count) / block->steps[X_AXIS];
  883. if (max_acceleration_steps_per_s2[Y_AXIS] < (block->acceleration_steps_per_s2 * block->steps[Y_AXIS]) / block->step_event_count)
  884. block->acceleration_steps_per_s2 = (max_acceleration_steps_per_s2[Y_AXIS] * block->step_event_count) / block->steps[Y_AXIS];
  885. if (max_acceleration_steps_per_s2[Z_AXIS] < (block->acceleration_steps_per_s2 * block->steps[Z_AXIS]) / block->step_event_count)
  886. block->acceleration_steps_per_s2 = (max_acceleration_steps_per_s2[Z_AXIS] * block->step_event_count) / block->steps[Z_AXIS];
  887. if (max_acceleration_steps_per_s2[E_AXIS] < (block->acceleration_steps_per_s2 * block->steps[E_AXIS]) / block->step_event_count)
  888. block->acceleration_steps_per_s2 = (max_acceleration_steps_per_s2[E_AXIS] * block->step_event_count) / block->steps[E_AXIS];
  889. }
  890. block->acceleration = block->acceleration_steps_per_s2 / steps_per_mm;
  891. block->acceleration_rate = (long)(block->acceleration_steps_per_s2 * 16777216.0 / ((F_CPU) * 0.125));
  892. #if 0 // Use old jerk for now
  893. float junction_deviation = 0.1;
  894. // Compute path unit vector
  895. double unit_vec[XYZ];
  896. unit_vec[X_AXIS] = delta_mm[X_AXIS] * inverse_millimeters;
  897. unit_vec[Y_AXIS] = delta_mm[Y_AXIS] * inverse_millimeters;
  898. unit_vec[Z_AXIS] = delta_mm[Z_AXIS] * inverse_millimeters;
  899. // Compute maximum allowable entry speed at junction by centripetal acceleration approximation.
  900. // Let a circle be tangent to both previous and current path line segments, where the junction
  901. // deviation is defined as the distance from the junction to the closest edge of the circle,
  902. // collinear with the circle center. The circular segment joining the two paths represents the
  903. // path of centripetal acceleration. Solve for max velocity based on max acceleration about the
  904. // radius of the circle, defined indirectly by junction deviation. This may be also viewed as
  905. // path width or max_jerk in the previous grbl version. This approach does not actually deviate
  906. // from path, but used as a robust way to compute cornering speeds, as it takes into account the
  907. // nonlinearities of both the junction angle and junction velocity.
  908. double vmax_junction = MINIMUM_PLANNER_SPEED; // Set default max junction speed
  909. // Skip first block or when previous_nominal_speed is used as a flag for homing and offset cycles.
  910. if ((block_buffer_head != block_buffer_tail) && (previous_nominal_speed > 0.0)) {
  911. // Compute cosine of angle between previous and current path. (prev_unit_vec is negative)
  912. // NOTE: Max junction velocity is computed without sin() or acos() by trig half angle identity.
  913. double cos_theta = - previous_unit_vec[X_AXIS] * unit_vec[X_AXIS]
  914. - previous_unit_vec[Y_AXIS] * unit_vec[Y_AXIS]
  915. - previous_unit_vec[Z_AXIS] * unit_vec[Z_AXIS] ;
  916. // Skip and use default max junction speed for 0 degree acute junction.
  917. if (cos_theta < 0.95) {
  918. vmax_junction = min(previous_nominal_speed, block->nominal_speed);
  919. // Skip and avoid divide by zero for straight junctions at 180 degrees. Limit to min() of nominal speeds.
  920. if (cos_theta > -0.95) {
  921. // Compute maximum junction velocity based on maximum acceleration and junction deviation
  922. double sin_theta_d2 = sqrt(0.5 * (1.0 - cos_theta)); // Trig half angle identity. Always positive.
  923. NOMORE(vmax_junction, sqrt(block->acceleration * junction_deviation * sin_theta_d2 / (1.0 - sin_theta_d2)));
  924. }
  925. }
  926. }
  927. #endif
  928. // Start with a safe speed
  929. float vmax_junction = max_jerk[X_AXIS] * 0.5, vmax_junction_factor = 1.0;
  930. if (max_jerk[Y_AXIS] * 0.5 < fabs(current_speed[Y_AXIS])) NOMORE(vmax_junction, max_jerk[Y_AXIS] * 0.5);
  931. if (max_jerk[Z_AXIS] * 0.5 < fabs(current_speed[Z_AXIS])) NOMORE(vmax_junction, max_jerk[Z_AXIS] * 0.5);
  932. if (max_jerk[E_AXIS] * 0.5 < fabs(current_speed[E_AXIS])) NOMORE(vmax_junction, max_jerk[E_AXIS] * 0.5);
  933. NOMORE(vmax_junction, block->nominal_speed);
  934. float safe_speed = vmax_junction;
  935. if (moves_queued > 1 && previous_nominal_speed > 0.0001) {
  936. //if ((fabs(previous_speed[X_AXIS]) > 0.0001) || (fabs(previous_speed[Y_AXIS]) > 0.0001)) {
  937. vmax_junction = block->nominal_speed;
  938. //}
  939. float dsx = fabs(current_speed[X_AXIS] - previous_speed[X_AXIS]),
  940. dsy = fabs(current_speed[Y_AXIS] - previous_speed[Y_AXIS]),
  941. dsz = fabs(current_speed[Z_AXIS] - previous_speed[Z_AXIS]),
  942. dse = fabs(current_speed[E_AXIS] - previous_speed[E_AXIS]);
  943. if (dsx > max_jerk[X_AXIS]) NOMORE(vmax_junction_factor, max_jerk[X_AXIS] / dsx);
  944. if (dsy > max_jerk[Y_AXIS]) NOMORE(vmax_junction_factor, max_jerk[Y_AXIS] / dsy);
  945. if (dsz > max_jerk[Z_AXIS]) NOMORE(vmax_junction_factor, max_jerk[Z_AXIS] / dsz);
  946. if (dse > max_jerk[E_AXIS]) NOMORE(vmax_junction_factor, max_jerk[E_AXIS] / dse);
  947. vmax_junction = min(previous_nominal_speed, vmax_junction * vmax_junction_factor); // Limit speed to max previous speed
  948. }
  949. block->max_entry_speed = vmax_junction;
  950. // Initialize block entry speed. Compute based on deceleration to user-defined MINIMUM_PLANNER_SPEED.
  951. float v_allowable = max_allowable_speed(-block->acceleration, MINIMUM_PLANNER_SPEED, block->millimeters);
  952. block->entry_speed = min(vmax_junction, v_allowable);
  953. // Initialize planner efficiency flags
  954. // Set flag if block will always reach maximum junction speed regardless of entry/exit speeds.
  955. // If a block can de/ac-celerate from nominal speed to zero within the length of the block, then
  956. // the current block and next block junction speeds are guaranteed to always be at their maximum
  957. // junction speeds in deceleration and acceleration, respectively. This is due to how the current
  958. // block nominal speed limits both the current and next maximum junction speeds. Hence, in both
  959. // the reverse and forward planners, the corresponding block junction speed will always be at the
  960. // the maximum junction speed and may always be ignored for any speed reduction checks.
  961. block->flag &= ~BLOCK_FLAG_NOMINAL_LENGTH;
  962. if (block->nominal_speed <= v_allowable) block->flag |= BLOCK_FLAG_NOMINAL_LENGTH;
  963. block->flag |= BLOCK_FLAG_RECALCULATE; // Always calculate trapezoid for new block
  964. // Update previous path unit_vector and nominal speed
  965. memcpy(previous_speed, current_speed, sizeof(previous_speed));
  966. previous_nominal_speed = block->nominal_speed;
  967. #if ENABLED(LIN_ADVANCE)
  968. // block->steps[E_AXIS] == block->step_event_count: A problem occurs when there's a very tiny move before a retract.
  969. // In this case, the retract and the move will be executed together.
  970. // This leads to an enormous number of advance steps due to a huge e_acceleration.
  971. // The math is correct, but you don't want a retract move done with advance!
  972. // So this situation is filtered out here.
  973. if (!block->steps[E_AXIS] || (!block->steps[X_AXIS] && !block->steps[Y_AXIS] && !block->steps[Z_AXIS]) || stepper.get_advance_k() == 0 || (uint32_t) block->steps[E_AXIS] == block->step_event_count) {
  974. block->use_advance_lead = false;
  975. }
  976. else {
  977. block->use_advance_lead = true;
  978. block->e_speed_multiplier8 = (block->steps[E_AXIS] << 8) / block->step_event_count;
  979. }
  980. #elif ENABLED(ADVANCE)
  981. // Calculate advance rate
  982. if (!block->steps[E_AXIS] || (!block->steps[X_AXIS] && !block->steps[Y_AXIS] && !block->steps[Z_AXIS])) {
  983. block->advance_rate = 0;
  984. block->advance = 0;
  985. }
  986. else {
  987. long acc_dist = estimate_acceleration_distance(0, block->nominal_rate, block->acceleration_steps_per_s2);
  988. float advance = ((STEPS_PER_CUBIC_MM_E) * (EXTRUDER_ADVANCE_K)) * HYPOT(current_speed[E_AXIS], EXTRUSION_AREA) * 256;
  989. block->advance = advance;
  990. block->advance_rate = acc_dist ? advance / (float)acc_dist : 0;
  991. }
  992. /**
  993. SERIAL_ECHO_START;
  994. SERIAL_ECHOPGM("advance :");
  995. SERIAL_ECHO(block->advance/256.0);
  996. SERIAL_ECHOPGM("advance rate :");
  997. SERIAL_ECHOLN(block->advance_rate/256.0);
  998. */
  999. #endif // ADVANCE or LIN_ADVANCE
  1000. calculate_trapezoid_for_block(block, block->entry_speed / block->nominal_speed, safe_speed / block->nominal_speed);
  1001. // Move buffer head
  1002. block_buffer_head = next_buffer_head;
  1003. // Update the position (only when a move was queued)
  1004. memcpy(position, target, sizeof(position));
  1005. recalculate();
  1006. stepper.wake_up();
  1007. } // buffer_line()
  1008. /**
  1009. * Directly set the planner XYZ position (and stepper positions)
  1010. * converting mm (or angles for SCARA) into steps.
  1011. *
  1012. * On CORE machines stepper ABC will be translated from the given XYZ.
  1013. */
  1014. void Planner::_set_position_mm(const float &a, const float &b, const float &c, const float &e) {
  1015. long na = position[X_AXIS] = lround(a * axis_steps_per_mm[X_AXIS]),
  1016. nb = position[Y_AXIS] = lround(b * axis_steps_per_mm[Y_AXIS]),
  1017. nc = position[Z_AXIS] = lround(c * axis_steps_per_mm[Z_AXIS]),
  1018. ne = position[E_AXIS] = lround(e * axis_steps_per_mm[E_AXIS]);
  1019. stepper.set_position(na, nb, nc, ne);
  1020. previous_nominal_speed = 0.0; // Resets planner junction speeds. Assumes start from rest.
  1021. memset(previous_speed, 0, sizeof(previous_speed));
  1022. }
  1023. void Planner::set_position_mm_kinematic(const float position[NUM_AXIS]) {
  1024. #if PLANNER_LEVELING
  1025. float pos[XYZ] = { position[X_AXIS], position[Y_AXIS], position[Z_AXIS] };
  1026. apply_leveling(pos);
  1027. #else
  1028. const float * const pos = position;
  1029. #endif
  1030. #if IS_KINEMATIC
  1031. inverse_kinematics(pos);
  1032. _set_position_mm(delta[A_AXIS], delta[B_AXIS], delta[C_AXIS], position[E_AXIS]);
  1033. #else
  1034. _set_position_mm(pos[X_AXIS], pos[Y_AXIS], pos[Z_AXIS], position[E_AXIS]);
  1035. #endif
  1036. }
  1037. /**
  1038. * Sync from the stepper positions. (e.g., after an interrupted move)
  1039. */
  1040. void Planner::sync_from_steppers() {
  1041. LOOP_XYZE(i) position[i] = stepper.position((AxisEnum)i);
  1042. }
  1043. /**
  1044. * Setters for planner position (also setting stepper position).
  1045. */
  1046. void Planner::set_position_mm(const AxisEnum axis, const float& v) {
  1047. position[axis] = lround(v * axis_steps_per_mm[axis]);
  1048. stepper.set_position(axis, v);
  1049. previous_speed[axis] = 0.0;
  1050. }
  1051. // Recalculate the steps/s^2 acceleration rates, based on the mm/s^2
  1052. void Planner::reset_acceleration_rates() {
  1053. LOOP_XYZE(i)
  1054. max_acceleration_steps_per_s2[i] = max_acceleration_mm_per_s2[i] * axis_steps_per_mm[i];
  1055. }
  1056. // Recalculate position, steps_to_mm if axis_steps_per_mm changes!
  1057. void Planner::refresh_positioning() {
  1058. LOOP_XYZE(i) steps_to_mm[i] = 1.0 / axis_steps_per_mm[i];
  1059. set_position_mm_kinematic(current_position);
  1060. reset_acceleration_rates();
  1061. }
  1062. #if ENABLED(AUTOTEMP)
  1063. void Planner::autotemp_M109() {
  1064. autotemp_enabled = code_seen('F');
  1065. if (autotemp_enabled) autotemp_factor = code_value_temp_diff();
  1066. if (code_seen('S')) autotemp_min = code_value_temp_abs();
  1067. if (code_seen('B')) autotemp_max = code_value_temp_abs();
  1068. }
  1069. #endif