My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

planner.cpp 41KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  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. #elif ENABLED(COREYZ)
  478. // coreyz planning
  479. block->steps[X_AXIS] = labs(dx);
  480. block->steps[B_AXIS] = labs(dy + dz);
  481. block->steps[C_AXIS] = labs(dy - dz);
  482. #else
  483. // default non-h-bot planning
  484. block->steps[X_AXIS] = labs(dx);
  485. block->steps[Y_AXIS] = labs(dy);
  486. block->steps[Z_AXIS] = labs(dz);
  487. #endif
  488. block->steps[E_AXIS] = labs(de);
  489. block->steps[E_AXIS] *= volumetric_multiplier[extruder];
  490. block->steps[E_AXIS] *= extruder_multiplier[extruder];
  491. block->steps[E_AXIS] /= 100;
  492. block->step_event_count = max(block->steps[X_AXIS], max(block->steps[Y_AXIS], max(block->steps[Z_AXIS], block->steps[E_AXIS])));
  493. // Bail if this is a zero-length block
  494. if (block->step_event_count <= dropsegments) return;
  495. #if FAN_COUNT > 0
  496. for (uint8_t i = 0; i < FAN_COUNT; i++) block->fan_speed[i] = fanSpeeds[i];
  497. #endif
  498. #if ENABLED(BARICUDA)
  499. block->valve_pressure = baricuda_valve_pressure;
  500. block->e_to_p_pressure = baricuda_e_to_p_pressure;
  501. #endif
  502. // Compute direction bits for this block
  503. uint8_t db = 0;
  504. #if ENABLED(COREXY)
  505. if (dx < 0) SBI(db, X_HEAD); // Save the real Extruder (head) direction in X Axis
  506. if (dy < 0) SBI(db, Y_HEAD); // ...and Y
  507. if (dz < 0) SBI(db, Z_AXIS);
  508. if (dx + dy < 0) SBI(db, A_AXIS); // Motor A direction
  509. if (dx - dy < 0) SBI(db, B_AXIS); // Motor B direction
  510. #elif ENABLED(COREXZ)
  511. if (dx < 0) SBI(db, X_HEAD); // Save the real Extruder (head) direction in X Axis
  512. if (dy < 0) SBI(db, Y_AXIS);
  513. if (dz < 0) SBI(db, Z_HEAD); // ...and Z
  514. if (dx + dz < 0) SBI(db, A_AXIS); // Motor A direction
  515. if (dx - dz < 0) SBI(db, C_AXIS); // Motor C direction
  516. #elif ENABLED(COREYZ)
  517. if (dx < 0) SBI(db, X_AXIS);
  518. if (dy < 0) SBI(db, Y_HEAD); // Save the real Extruder (head) direction in Y Axis
  519. if (dz < 0) SBI(db, Z_HEAD); // ...and Z
  520. if (dy + dz < 0) SBI(db, B_AXIS); // Motor B direction
  521. if (dy - dz < 0) SBI(db, C_AXIS); // Motor C 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) || ENABLED(COREXZ) || ENABLED(COREYZ)
  631. float delta_mm[6];
  632. #if ENABLED(COREXY)
  633. delta_mm[X_HEAD] = dx / axis_steps_per_unit[A_AXIS];
  634. delta_mm[Y_HEAD] = dy / axis_steps_per_unit[B_AXIS];
  635. delta_mm[Z_AXIS] = dz / axis_steps_per_unit[Z_AXIS];
  636. delta_mm[A_AXIS] = (dx + dy) / axis_steps_per_unit[A_AXIS];
  637. delta_mm[B_AXIS] = (dx - dy) / axis_steps_per_unit[B_AXIS];
  638. #elif ENABLED(COREXZ)
  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. #elif ENABLED(COREYZ)
  645. delta_mm[X_AXIS] = dx / axis_steps_per_unit[A_AXIS];
  646. delta_mm[Y_HEAD] = dy / axis_steps_per_unit[Y_AXIS];
  647. delta_mm[Z_HEAD] = dz / axis_steps_per_unit[C_AXIS];
  648. delta_mm[B_AXIS] = (dy + dz) / axis_steps_per_unit[B_AXIS];
  649. delta_mm[C_AXIS] = (dy - dz) / axis_steps_per_unit[C_AXIS];
  650. #endif
  651. #else
  652. float delta_mm[4];
  653. delta_mm[X_AXIS] = dx / axis_steps_per_unit[X_AXIS];
  654. delta_mm[Y_AXIS] = dy / axis_steps_per_unit[Y_AXIS];
  655. delta_mm[Z_AXIS] = dz / axis_steps_per_unit[Z_AXIS];
  656. #endif
  657. delta_mm[E_AXIS] = (de / axis_steps_per_unit[E_AXIS]) * volumetric_multiplier[extruder] * extruder_multiplier[extruder] / 100.0;
  658. if (block->steps[X_AXIS] <= dropsegments && block->steps[Y_AXIS] <= dropsegments && block->steps[Z_AXIS] <= dropsegments) {
  659. block->millimeters = fabs(delta_mm[E_AXIS]);
  660. }
  661. else {
  662. block->millimeters = sqrt(
  663. #if ENABLED(COREXY)
  664. square(delta_mm[X_HEAD]) + square(delta_mm[Y_HEAD]) + square(delta_mm[Z_AXIS])
  665. #elif ENABLED(COREXZ)
  666. square(delta_mm[X_HEAD]) + square(delta_mm[Y_AXIS]) + square(delta_mm[Z_HEAD])
  667. #elif ENABLED(COREYZ)
  668. square(delta_mm[X_AXIS]) + square(delta_mm[Y_HEAD]) + square(delta_mm[Z_HEAD])
  669. #else
  670. square(delta_mm[X_AXIS]) + square(delta_mm[Y_AXIS]) + square(delta_mm[Z_AXIS])
  671. #endif
  672. );
  673. }
  674. float inverse_millimeters = 1.0 / block->millimeters; // Inverse millimeters to remove multiple divides
  675. // Calculate moves/second for this move. No divide by zero due to previous checks.
  676. float inverse_second = feed_rate * inverse_millimeters;
  677. int moves_queued = movesplanned();
  678. // Slow down when the buffer starts to empty, rather than wait at the corner for a buffer refill
  679. #if ENABLED(OLD_SLOWDOWN) || ENABLED(SLOWDOWN)
  680. bool mq = moves_queued > 1 && moves_queued < (BLOCK_BUFFER_SIZE) / 2;
  681. #if ENABLED(OLD_SLOWDOWN)
  682. if (mq) feed_rate *= 2.0 * moves_queued / (BLOCK_BUFFER_SIZE);
  683. #endif
  684. #if ENABLED(SLOWDOWN)
  685. // segment time im micro seconds
  686. unsigned long segment_time = lround(1000000.0/inverse_second);
  687. if (mq) {
  688. if (segment_time < min_segment_time) {
  689. // buffer is draining, add extra time. The amount of time added increases if the buffer is still emptied more.
  690. inverse_second = 1000000.0 / (segment_time + lround(2 * (min_segment_time - segment_time) / moves_queued));
  691. #ifdef XY_FREQUENCY_LIMIT
  692. segment_time = lround(1000000.0 / inverse_second);
  693. #endif
  694. }
  695. }
  696. #endif
  697. #endif
  698. block->nominal_speed = block->millimeters * inverse_second; // (mm/sec) Always > 0
  699. block->nominal_rate = ceil(block->step_event_count * inverse_second); // (step/sec) Always > 0
  700. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  701. static float filwidth_e_count = 0, filwidth_delay_dist = 0;
  702. //FMM update ring buffer used for delay with filament measurements
  703. if (extruder == FILAMENT_SENSOR_EXTRUDER_NUM && filwidth_delay_index2 >= 0) { //only for extruder with filament sensor and if ring buffer is initialized
  704. const int MMD_CM = MAX_MEASUREMENT_DELAY + 1, MMD_MM = MMD_CM * 10;
  705. // increment counters with next move in e axis
  706. filwidth_e_count += delta_mm[E_AXIS];
  707. filwidth_delay_dist += delta_mm[E_AXIS];
  708. // Only get new measurements on forward E movement
  709. if (filwidth_e_count > 0.0001) {
  710. // Loop the delay distance counter (modulus by the mm length)
  711. while (filwidth_delay_dist >= MMD_MM) filwidth_delay_dist -= MMD_MM;
  712. // Convert into an index into the measurement array
  713. filwidth_delay_index1 = (int)(filwidth_delay_dist / 10.0 + 0.0001);
  714. // If the index has changed (must have gone forward)...
  715. if (filwidth_delay_index1 != filwidth_delay_index2) {
  716. filwidth_e_count = 0; // Reset the E movement counter
  717. int8_t meas_sample = thermalManager.widthFil_to_size_ratio() - 100; // Subtract 100 to reduce magnitude - to store in a signed char
  718. do {
  719. filwidth_delay_index2 = (filwidth_delay_index2 + 1) % MMD_CM; // The next unused slot
  720. measurement_delay[filwidth_delay_index2] = meas_sample; // Store the measurement
  721. } while (filwidth_delay_index1 != filwidth_delay_index2); // More slots to fill?
  722. }
  723. }
  724. }
  725. #endif
  726. // Calculate and limit speed in mm/sec for each axis
  727. float current_speed[NUM_AXIS];
  728. float speed_factor = 1.0; //factor <=1 do decrease speed
  729. for (int i = 0; i < NUM_AXIS; i++) {
  730. current_speed[i] = delta_mm[i] * inverse_second;
  731. float cs = fabs(current_speed[i]), mf = max_feedrate[i];
  732. if (cs > mf) speed_factor = min(speed_factor, mf / cs);
  733. }
  734. // Max segement time in us.
  735. #ifdef XY_FREQUENCY_LIMIT
  736. // Check and limit the xy direction change frequency
  737. unsigned char direction_change = block->direction_bits ^ old_direction_bits;
  738. old_direction_bits = block->direction_bits;
  739. segment_time = lround((float)segment_time / speed_factor);
  740. long xs0 = axis_segment_time[X_AXIS][0],
  741. xs1 = axis_segment_time[X_AXIS][1],
  742. xs2 = axis_segment_time[X_AXIS][2],
  743. ys0 = axis_segment_time[Y_AXIS][0],
  744. ys1 = axis_segment_time[Y_AXIS][1],
  745. ys2 = axis_segment_time[Y_AXIS][2];
  746. if (TEST(direction_change, X_AXIS)) {
  747. xs2 = axis_segment_time[X_AXIS][2] = xs1;
  748. xs1 = axis_segment_time[X_AXIS][1] = xs0;
  749. xs0 = 0;
  750. }
  751. xs0 = axis_segment_time[X_AXIS][0] = xs0 + segment_time;
  752. if (TEST(direction_change, Y_AXIS)) {
  753. ys2 = axis_segment_time[Y_AXIS][2] = axis_segment_time[Y_AXIS][1];
  754. ys1 = axis_segment_time[Y_AXIS][1] = axis_segment_time[Y_AXIS][0];
  755. ys0 = 0;
  756. }
  757. ys0 = axis_segment_time[Y_AXIS][0] = ys0 + segment_time;
  758. long max_x_segment_time = max(xs0, max(xs1, xs2)),
  759. max_y_segment_time = max(ys0, max(ys1, ys2)),
  760. min_xy_segment_time = min(max_x_segment_time, max_y_segment_time);
  761. if (min_xy_segment_time < MAX_FREQ_TIME) {
  762. float low_sf = speed_factor * min_xy_segment_time / (MAX_FREQ_TIME);
  763. speed_factor = min(speed_factor, low_sf);
  764. }
  765. #endif // XY_FREQUENCY_LIMIT
  766. // Correct the speed
  767. if (speed_factor < 1.0) {
  768. for (unsigned char i = 0; i < NUM_AXIS; i++) current_speed[i] *= speed_factor;
  769. block->nominal_speed *= speed_factor;
  770. block->nominal_rate *= speed_factor;
  771. }
  772. // Compute and limit the acceleration rate for the trapezoid generator.
  773. float steps_per_mm = block->step_event_count / block->millimeters;
  774. long bsx = block->steps[X_AXIS], bsy = block->steps[Y_AXIS], bsz = block->steps[Z_AXIS], bse = block->steps[E_AXIS];
  775. if (bsx == 0 && bsy == 0 && bsz == 0) {
  776. block->acceleration_st = ceil(retract_acceleration * steps_per_mm); // convert to: acceleration steps/sec^2
  777. }
  778. else if (bse == 0) {
  779. block->acceleration_st = ceil(travel_acceleration * steps_per_mm); // convert to: acceleration steps/sec^2
  780. }
  781. else {
  782. block->acceleration_st = ceil(acceleration * steps_per_mm); // convert to: acceleration steps/sec^2
  783. }
  784. // Limit acceleration per axis
  785. unsigned long acc_st = block->acceleration_st,
  786. xsteps = axis_steps_per_sqr_second[X_AXIS],
  787. ysteps = axis_steps_per_sqr_second[Y_AXIS],
  788. zsteps = axis_steps_per_sqr_second[Z_AXIS],
  789. esteps = axis_steps_per_sqr_second[E_AXIS],
  790. allsteps = block->step_event_count;
  791. if (xsteps < (acc_st * bsx) / allsteps) acc_st = (xsteps * allsteps) / bsx;
  792. if (ysteps < (acc_st * bsy) / allsteps) acc_st = (ysteps * allsteps) / bsy;
  793. if (zsteps < (acc_st * bsz) / allsteps) acc_st = (zsteps * allsteps) / bsz;
  794. if (esteps < (acc_st * bse) / allsteps) acc_st = (esteps * allsteps) / bse;
  795. block->acceleration_st = acc_st;
  796. block->acceleration = acc_st / steps_per_mm;
  797. block->acceleration_rate = (long)(acc_st * 16777216.0 / (F_CPU / 8.0));
  798. #if 0 // Use old jerk for now
  799. float junction_deviation = 0.1;
  800. // Compute path unit vector
  801. double unit_vec[3];
  802. unit_vec[X_AXIS] = delta_mm[X_AXIS] * inverse_millimeters;
  803. unit_vec[Y_AXIS] = delta_mm[Y_AXIS] * inverse_millimeters;
  804. unit_vec[Z_AXIS] = delta_mm[Z_AXIS] * inverse_millimeters;
  805. // Compute maximum allowable entry speed at junction by centripetal acceleration approximation.
  806. // Let a circle be tangent to both previous and current path line segments, where the junction
  807. // deviation is defined as the distance from the junction to the closest edge of the circle,
  808. // collinear with the circle center. The circular segment joining the two paths represents the
  809. // path of centripetal acceleration. Solve for max velocity based on max acceleration about the
  810. // radius of the circle, defined indirectly by junction deviation. This may be also viewed as
  811. // path width or max_jerk in the previous grbl version. This approach does not actually deviate
  812. // from path, but used as a robust way to compute cornering speeds, as it takes into account the
  813. // nonlinearities of both the junction angle and junction velocity.
  814. double vmax_junction = MINIMUM_PLANNER_SPEED; // Set default max junction speed
  815. // Skip first block or when previous_nominal_speed is used as a flag for homing and offset cycles.
  816. if ((block_buffer_head != block_buffer_tail) && (previous_nominal_speed > 0.0)) {
  817. // Compute cosine of angle between previous and current path. (prev_unit_vec is negative)
  818. // NOTE: Max junction velocity is computed without sin() or acos() by trig half angle identity.
  819. double cos_theta = - previous_unit_vec[X_AXIS] * unit_vec[X_AXIS]
  820. - previous_unit_vec[Y_AXIS] * unit_vec[Y_AXIS]
  821. - previous_unit_vec[Z_AXIS] * unit_vec[Z_AXIS] ;
  822. // Skip and use default max junction speed for 0 degree acute junction.
  823. if (cos_theta < 0.95) {
  824. vmax_junction = min(previous_nominal_speed, block->nominal_speed);
  825. // Skip and avoid divide by zero for straight junctions at 180 degrees. Limit to min() of nominal speeds.
  826. if (cos_theta > -0.95) {
  827. // Compute maximum junction velocity based on maximum acceleration and junction deviation
  828. double sin_theta_d2 = sqrt(0.5 * (1.0 - cos_theta)); // Trig half angle identity. Always positive.
  829. vmax_junction = min(vmax_junction,
  830. sqrt(block->acceleration * junction_deviation * sin_theta_d2 / (1.0 - sin_theta_d2)));
  831. }
  832. }
  833. }
  834. #endif
  835. // Start with a safe speed
  836. float vmax_junction = max_xy_jerk / 2;
  837. float vmax_junction_factor = 1.0;
  838. float mz2 = max_z_jerk / 2, me2 = max_e_jerk / 2;
  839. float csz = current_speed[Z_AXIS], cse = current_speed[E_AXIS];
  840. if (fabs(csz) > mz2) vmax_junction = min(vmax_junction, mz2);
  841. if (fabs(cse) > me2) vmax_junction = min(vmax_junction, me2);
  842. vmax_junction = min(vmax_junction, block->nominal_speed);
  843. float safe_speed = vmax_junction;
  844. if ((moves_queued > 1) && (previous_nominal_speed > 0.0001)) {
  845. float dsx = current_speed[X_AXIS] - previous_speed[X_AXIS],
  846. dsy = current_speed[Y_AXIS] - previous_speed[Y_AXIS],
  847. dsz = fabs(csz - previous_speed[Z_AXIS]),
  848. dse = fabs(cse - previous_speed[E_AXIS]),
  849. jerk = sqrt(dsx * dsx + dsy * dsy);
  850. // if ((fabs(previous_speed[X_AXIS]) > 0.0001) || (fabs(previous_speed[Y_AXIS]) > 0.0001)) {
  851. vmax_junction = block->nominal_speed;
  852. // }
  853. if (jerk > max_xy_jerk) vmax_junction_factor = max_xy_jerk / jerk;
  854. if (dsz > max_z_jerk) vmax_junction_factor = min(vmax_junction_factor, max_z_jerk / dsz);
  855. if (dse > max_e_jerk) vmax_junction_factor = min(vmax_junction_factor, max_e_jerk / dse);
  856. vmax_junction = min(previous_nominal_speed, vmax_junction * vmax_junction_factor); // Limit speed to max previous speed
  857. }
  858. block->max_entry_speed = vmax_junction;
  859. // Initialize block entry speed. Compute based on deceleration to user-defined MINIMUM_PLANNER_SPEED.
  860. double v_allowable = max_allowable_speed(-block->acceleration, MINIMUM_PLANNER_SPEED, block->millimeters);
  861. block->entry_speed = min(vmax_junction, v_allowable);
  862. // Initialize planner efficiency flags
  863. // Set flag if block will always reach maximum junction speed regardless of entry/exit speeds.
  864. // If a block can de/ac-celerate from nominal speed to zero within the length of the block, then
  865. // the current block and next block junction speeds are guaranteed to always be at their maximum
  866. // junction speeds in deceleration and acceleration, respectively. This is due to how the current
  867. // block nominal speed limits both the current and next maximum junction speeds. Hence, in both
  868. // the reverse and forward planners, the corresponding block junction speed will always be at the
  869. // the maximum junction speed and may always be ignored for any speed reduction checks.
  870. block->nominal_length_flag = (block->nominal_speed <= v_allowable);
  871. block->recalculate_flag = true; // Always calculate trapezoid for new block
  872. // Update previous path unit_vector and nominal speed
  873. for (int i = 0; i < NUM_AXIS; i++) previous_speed[i] = current_speed[i];
  874. previous_nominal_speed = block->nominal_speed;
  875. #if ENABLED(ADVANCE)
  876. // Calculate advance rate
  877. if (!bse || (!bsx && !bsy && !bsz)) {
  878. block->advance_rate = 0;
  879. block->advance = 0;
  880. }
  881. else {
  882. long acc_dist = estimate_acceleration_distance(0, block->nominal_rate, block->acceleration_st);
  883. float advance = ((STEPS_PER_CUBIC_MM_E) * (EXTRUDER_ADVANCE_K)) * (cse * cse * (EXTRUSION_AREA) * (EXTRUSION_AREA)) * 256;
  884. block->advance = advance;
  885. block->advance_rate = acc_dist ? advance / (float)acc_dist : 0;
  886. }
  887. /**
  888. SERIAL_ECHO_START;
  889. SERIAL_ECHOPGM("advance :");
  890. SERIAL_ECHO(block->advance/256.0);
  891. SERIAL_ECHOPGM("advance rate :");
  892. SERIAL_ECHOLN(block->advance_rate/256.0);
  893. */
  894. #endif // ADVANCE
  895. calculate_trapezoid_for_block(block, block->entry_speed / block->nominal_speed, safe_speed / block->nominal_speed);
  896. // Move buffer head
  897. block_buffer_head = next_buffer_head;
  898. // Update position
  899. for (int i = 0; i < NUM_AXIS; i++) position[i] = target[i];
  900. recalculate();
  901. stepper.wake_up();
  902. } // buffer_line()
  903. #if ENABLED(AUTO_BED_LEVELING_FEATURE) && DISABLED(DELTA)
  904. /**
  905. * Get the XYZ position of the steppers as a vector_3.
  906. *
  907. * On CORE machines XYZ is derived from ABC.
  908. */
  909. vector_3 Planner::adjusted_position() {
  910. 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));
  911. //pos.debug("in Planner::adjusted_position");
  912. //bed_level_matrix.debug("in Planner::adjusted_position");
  913. matrix_3x3 inverse = matrix_3x3::transpose(bed_level_matrix);
  914. //inverse.debug("in Planner::inverse");
  915. pos.apply_rotation(inverse);
  916. //pos.debug("after rotation");
  917. return pos;
  918. }
  919. #endif // AUTO_BED_LEVELING_FEATURE && !DELTA
  920. /**
  921. * Directly set the planner XYZ position (hence the stepper positions).
  922. *
  923. * On CORE machines stepper ABC will be translated from the given XYZ.
  924. */
  925. #if ENABLED(AUTO_BED_LEVELING_FEATURE) || ENABLED(MESH_BED_LEVELING)
  926. void Planner::set_position(float x, float y, float z, const float& e)
  927. #else
  928. void Planner::set_position(const float& x, const float& y, const float& z, const float& e)
  929. #endif // AUTO_BED_LEVELING_FEATURE || MESH_BED_LEVELING
  930. {
  931. #if ENABLED(MESH_BED_LEVELING)
  932. if (mbl.active) z += mbl.get_z(x - home_offset[X_AXIS], y - home_offset[Y_AXIS]);
  933. #elif ENABLED(AUTO_BED_LEVELING_FEATURE)
  934. apply_rotation_xyz(bed_level_matrix, x, y, z);
  935. #endif
  936. long nx = position[X_AXIS] = lround(x * axis_steps_per_unit[X_AXIS]),
  937. ny = position[Y_AXIS] = lround(y * axis_steps_per_unit[Y_AXIS]),
  938. nz = position[Z_AXIS] = lround(z * axis_steps_per_unit[Z_AXIS]),
  939. ne = position[E_AXIS] = lround(e * axis_steps_per_unit[E_AXIS]);
  940. stepper.set_position(nx, ny, nz, ne);
  941. previous_nominal_speed = 0.0; // Resets planner junction speeds. Assumes start from rest.
  942. for (int i = 0; i < NUM_AXIS; i++) previous_speed[i] = 0.0;
  943. }
  944. /**
  945. * Directly set the planner E position (hence the stepper E position).
  946. */
  947. void Planner::set_e_position(const float& e) {
  948. position[E_AXIS] = lround(e * axis_steps_per_unit[E_AXIS]);
  949. stepper.set_e_position(position[E_AXIS]);
  950. }
  951. // Recalculate the steps/s^2 acceleration rates, based on the mm/s^2
  952. void Planner::reset_acceleration_rates() {
  953. for (int i = 0; i < NUM_AXIS; i++)
  954. axis_steps_per_sqr_second[i] = max_acceleration_units_per_sq_second[i] * axis_steps_per_unit[i];
  955. }
  956. #if ENABLED(AUTOTEMP)
  957. void Planner::autotemp_M109() {
  958. autotemp_enabled = code_seen('F');
  959. if (autotemp_enabled) autotemp_factor = code_value();
  960. if (code_seen('S')) autotemp_min = code_value();
  961. if (code_seen('B')) autotemp_max = code_value();
  962. }
  963. #endif