My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

planner.cpp 41KB

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