My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

planner.cpp 45KB

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