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

planner.cpp 40KB

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