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.

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