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

planner.cpp 41KB

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