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 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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 <inttypes.h>
  37. //#include <math.h>
  38. //#include <stdlib.h>
  39. #include "Marlin.h"
  40. #include "Configuration.h"
  41. #include "pins.h"
  42. #include "fastio.h"
  43. #include "planner.h"
  44. #include "stepper.h"
  45. #include "temperature.h"
  46. #include "ultralcd.h"
  47. //===========================================================================
  48. //=============================public variables ============================
  49. //===========================================================================
  50. unsigned long minsegmenttime;
  51. float max_feedrate[4]; // set the max speeds
  52. float axis_steps_per_unit[4];
  53. long max_acceleration_units_per_sq_second[4]; // Use M201 to override by software
  54. float minimumfeedrate;
  55. float acceleration; // Normal acceleration mm/s^2 THIS IS THE DEFAULT ACCELERATION for all moves. M204 SXXXX
  56. float retract_acceleration; // mm/s^2 filament pull-pack and push-forward while standing still in the other axis M204 TXXXX
  57. float max_xy_jerk; //speed than can be stopped at once, if i understand correctly.
  58. float max_z_jerk;
  59. float mintravelfeedrate;
  60. unsigned long axis_steps_per_sqr_second[NUM_AXIS];
  61. // The current position of the tool in absolute steps
  62. long position[4]; //rescaled from extern when axis_steps_per_unit are changed by gcode
  63. #ifdef AUTOTEMP
  64. float high_e_speed=0;
  65. #endif
  66. //===========================================================================
  67. //=============================private variables ============================
  68. //===========================================================================
  69. static block_t block_buffer[BLOCK_BUFFER_SIZE]; // A ring buffer for motion instfructions
  70. static volatile unsigned char block_buffer_head; // Index of the next block to be pushed
  71. static volatile unsigned char block_buffer_tail; // Index of the block to process now
  72. //===========================================================================
  73. //=============================functions ============================
  74. //===========================================================================
  75. #define ONE_MINUTE_OF_MICROSECONDS 60000000.0
  76. // Calculates the distance (not time) it takes to accelerate from initial_rate to target_rate using the
  77. // given acceleration:
  78. inline float estimate_acceleration_distance(float initial_rate, float target_rate, float acceleration) {
  79. if (acceleration!=0) {
  80. return((target_rate*target_rate-initial_rate*initial_rate)/
  81. (2.0*acceleration));
  82. }
  83. else {
  84. return 0.0; // acceleration was 0, set acceleration distance to 0
  85. }
  86. }
  87. // This function gives you the point at which you must start braking (at the rate of -acceleration) if
  88. // you started at speed initial_rate and accelerated until this point and want to end at the final_rate after
  89. // a total travel of distance. This can be used to compute the intersection point between acceleration and
  90. // deceleration in the cases where the trapezoid has no plateau (i.e. never reaches maximum speed)
  91. inline float intersection_distance(float initial_rate, float final_rate, float acceleration, float distance) {
  92. if (acceleration!=0) {
  93. return((2.0*acceleration*distance-initial_rate*initial_rate+final_rate*final_rate)/
  94. (4.0*acceleration) );
  95. }
  96. else {
  97. return 0.0; // acceleration was 0, set intersection distance to 0
  98. }
  99. }
  100. // Calculates trapezoid parameters so that the entry- and exit-speed is compensated by the provided factors.
  101. void calculate_trapezoid_for_block(block_t *block, float entry_speed, float exit_speed) {
  102. if(block->busy == true) return; // If block is busy then bail out.
  103. float entry_factor = entry_speed / block->nominal_speed;
  104. float exit_factor = exit_speed / block->nominal_speed;
  105. long initial_rate = ceil(block->nominal_rate*entry_factor);
  106. long final_rate = ceil(block->nominal_rate*exit_factor);
  107. #ifdef ADVANCE
  108. long initial_advance = block->advance*entry_factor*entry_factor;
  109. long final_advance = block->advance*exit_factor*exit_factor;
  110. #endif // ADVANCE
  111. // Limit minimal step rate (Otherwise the timer will overflow.)
  112. if(initial_rate <120) initial_rate=120;
  113. if(final_rate < 120) final_rate=120;
  114. // Calculate the acceleration steps
  115. long acceleration = block->acceleration_st;
  116. long accelerate_steps = estimate_acceleration_distance(initial_rate, block->nominal_rate, acceleration);
  117. long decelerate_steps = estimate_acceleration_distance(final_rate, block->nominal_rate, acceleration);
  118. // Calculate the size of Plateau of Nominal Rate.
  119. long plateau_steps = block->step_event_count-accelerate_steps-decelerate_steps;
  120. // Is the Plateau of Nominal Rate smaller than nothing? That means no cruising, and we will
  121. // have to use intersection_distance() to calculate when to abort acceleration and start braking
  122. // in order to reach the final_rate exactly at the end of this block.
  123. if (plateau_steps < 0) {
  124. accelerate_steps = intersection_distance(initial_rate, final_rate, acceleration, block->step_event_count);
  125. plateau_steps = 0;
  126. }
  127. long decelerate_after = accelerate_steps+plateau_steps;
  128. CRITICAL_SECTION_START; // Fill variables used by the stepper in a critical section
  129. if(block->busy == false) { // Don't update variables if block is busy.
  130. block->accelerate_until = accelerate_steps;
  131. block->decelerate_after = decelerate_after;
  132. block->initial_rate = initial_rate;
  133. block->final_rate = final_rate;
  134. #ifdef ADVANCE
  135. block->initial_advance = initial_advance;
  136. block->final_advance = final_advance;
  137. #endif //ADVANCE
  138. }
  139. CRITICAL_SECTION_END;
  140. }
  141. // Calculates the maximum allowable speed at this point when you must be able to reach target_velocity using the
  142. // acceleration within the allotted distance.
  143. inline float max_allowable_speed(float acceleration, float target_velocity, float distance) {
  144. return sqrt(target_velocity*target_velocity-2*acceleration*60*60*distance);
  145. }
  146. // "Junction jerk" in this context is the immediate change in speed at the junction of two blocks.
  147. // This method will calculate the junction jerk as the euclidean distance between the nominal
  148. // velocities of the respective blocks.
  149. inline float junction_jerk(block_t *before, block_t *after) {
  150. return sqrt(
  151. pow((before->speed_x-after->speed_x), 2)+pow((before->speed_y-after->speed_y), 2));
  152. }
  153. // Return the safe speed which is max_jerk/2, e.g. the
  154. // speed under which you cannot exceed max_jerk no matter what you do.
  155. float safe_speed(block_t *block) {
  156. float safe_speed;
  157. safe_speed = max_xy_jerk/2;
  158. if(abs(block->speed_z) > max_z_jerk/2)
  159. safe_speed = max_z_jerk/2;
  160. if (safe_speed > block->nominal_speed)
  161. safe_speed = block->nominal_speed;
  162. return safe_speed;
  163. }
  164. // The kernel called by planner_recalculate() when scanning the plan from last to first entry.
  165. void planner_reverse_pass_kernel(block_t *previous, block_t *current, block_t *next) {
  166. if(!current) {
  167. return;
  168. }
  169. float entry_speed = current->nominal_speed;
  170. float exit_factor;
  171. float exit_speed;
  172. if (next) {
  173. exit_speed = next->entry_speed;
  174. }
  175. else {
  176. exit_speed = safe_speed(current);
  177. }
  178. // Calculate the entry_factor for the current block.
  179. if (previous) {
  180. // Reduce speed so that junction_jerk is within the maximum allowed
  181. float jerk = junction_jerk(previous, current);
  182. if((previous->steps_x == 0) && (previous->steps_y == 0)) {
  183. entry_speed = safe_speed(current);
  184. }
  185. else if (jerk > max_xy_jerk) {
  186. entry_speed = (max_xy_jerk/jerk) * entry_speed;
  187. }
  188. if(abs(previous->speed_z - current->speed_z) > max_z_jerk) {
  189. entry_speed = (max_z_jerk/abs(previous->speed_z - current->speed_z)) * entry_speed;
  190. }
  191. // If the required deceleration across the block is too rapid, reduce the entry_factor accordingly.
  192. if (entry_speed > exit_speed) {
  193. float max_entry_speed = max_allowable_speed(-current->acceleration,exit_speed, current->millimeters);
  194. if (max_entry_speed < entry_speed) {
  195. entry_speed = max_entry_speed;
  196. }
  197. }
  198. }
  199. else {
  200. entry_speed = safe_speed(current);
  201. }
  202. // Store result
  203. current->entry_speed = entry_speed;
  204. }
  205. // planner_recalculate() needs to go over the current plan twice. Once in reverse and once forward. This
  206. // implements the reverse pass.
  207. void planner_reverse_pass() {
  208. char block_index = block_buffer_head;
  209. if(((block_buffer_head-block_buffer_tail + BLOCK_BUFFER_SIZE) & (BLOCK_BUFFER_SIZE - 1)) > 3) {
  210. block_index = (block_buffer_head - 3) & (BLOCK_BUFFER_SIZE - 1);
  211. block_t *block[5] = {
  212. NULL, NULL, NULL, NULL, NULL };
  213. while(block_index != block_buffer_tail) {
  214. block_index = (block_index-1) & (BLOCK_BUFFER_SIZE -1);
  215. block[2]= block[1];
  216. block[1]= block[0];
  217. block[0] = &block_buffer[block_index];
  218. planner_reverse_pass_kernel(block[0], block[1], block[2]);
  219. }
  220. planner_reverse_pass_kernel(NULL, block[0], block[1]);
  221. }
  222. }
  223. // The kernel called by planner_recalculate() when scanning the plan from first to last entry.
  224. void planner_forward_pass_kernel(block_t *previous, block_t *current, block_t *next) {
  225. if(!current) {
  226. return;
  227. }
  228. if(previous) {
  229. // If the previous block is an acceleration block, but it is not long enough to
  230. // complete the full speed change within the block, we need to adjust out entry
  231. // speed accordingly. Remember current->entry_factor equals the exit factor of
  232. // the previous block.
  233. if(previous->entry_speed < current->entry_speed) {
  234. float max_entry_speed = max_allowable_speed(-previous->acceleration, previous->entry_speed, previous->millimeters);
  235. if (max_entry_speed < current->entry_speed) {
  236. current->entry_speed = max_entry_speed;
  237. }
  238. }
  239. }
  240. }
  241. // planner_recalculate() needs to go over the current plan twice. Once in reverse and once forward. This
  242. // implements the forward pass.
  243. void planner_forward_pass() {
  244. char block_index = block_buffer_tail;
  245. block_t *block[3] = {
  246. NULL, NULL, NULL };
  247. while(block_index != block_buffer_head) {
  248. block[0] = block[1];
  249. block[1] = block[2];
  250. block[2] = &block_buffer[block_index];
  251. planner_forward_pass_kernel(block[0],block[1],block[2]);
  252. block_index = (block_index+1) & (BLOCK_BUFFER_SIZE - 1);
  253. }
  254. planner_forward_pass_kernel(block[1], block[2], NULL);
  255. }
  256. // Recalculates the trapezoid speed profiles for all blocks in the plan according to the
  257. // entry_factor for each junction. Must be called by planner_recalculate() after
  258. // updating the blocks.
  259. void planner_recalculate_trapezoids() {
  260. char block_index = block_buffer_tail;
  261. block_t *current;
  262. block_t *next = NULL;
  263. while(block_index != block_buffer_head) {
  264. current = next;
  265. next = &block_buffer[block_index];
  266. if (current) {
  267. calculate_trapezoid_for_block(current, current->entry_speed, next->entry_speed);
  268. }
  269. block_index = (block_index+1) & (BLOCK_BUFFER_SIZE - 1);
  270. }
  271. calculate_trapezoid_for_block(next, next->entry_speed, safe_speed(next));
  272. }
  273. // Recalculates the motion plan according to the following algorithm:
  274. //
  275. // 1. Go over every block in reverse order and calculate a junction speed reduction (i.e. block_t.entry_factor)
  276. // so that:
  277. // a. The junction jerk is within the set limit
  278. // b. No speed reduction within one block requires faster deceleration than the one, true constant
  279. // acceleration.
  280. // 2. Go over every block in chronological order and dial down junction speed reduction values if
  281. // a. The speed increase within one block would require faster accelleration than the one, true
  282. // constant acceleration.
  283. //
  284. // When these stages are complete all blocks have an entry_factor that will allow all speed changes to
  285. // be performed using only the one, true constant acceleration, and where no junction jerk is jerkier than
  286. // the set limit. Finally it will:
  287. //
  288. // 3. Recalculate trapezoids for all blocks.
  289. void planner_recalculate() {
  290. planner_reverse_pass();
  291. planner_forward_pass();
  292. planner_recalculate_trapezoids();
  293. }
  294. void plan_init() {
  295. block_buffer_head = 0;
  296. block_buffer_tail = 0;
  297. memset(position, 0, sizeof(position)); // clear position
  298. }
  299. void plan_discard_current_block() {
  300. if (block_buffer_head != block_buffer_tail) {
  301. block_buffer_tail = (block_buffer_tail + 1) & (BLOCK_BUFFER_SIZE - 1);
  302. }
  303. }
  304. block_t *plan_get_current_block() {
  305. if (block_buffer_head == block_buffer_tail) {
  306. return(NULL);
  307. }
  308. block_t *block = &block_buffer[block_buffer_tail];
  309. block->busy = true;
  310. return(block);
  311. }
  312. #ifdef AUTOTEMP
  313. void getHighESpeed()
  314. {
  315. if(degTargetHotend0()+2<AUTOTEMP_MIN) //probably temperature set to zero.
  316. return; //do nothing
  317. float high=0;
  318. char block_index = block_buffer_tail;
  319. while(block_index != block_buffer_head) {
  320. float se=block_buffer[block_index].speed_e;
  321. if(se>high)
  322. {
  323. high=se;
  324. }
  325. block_index = (block_index+1) & (BLOCK_BUFFER_SIZE - 1);
  326. }
  327. high_e_speed=high*axis_steps_per_unit[E_AXIS]/(1000000.0); //so it is independent of the esteps/mm. before
  328. float g=AUTOTEMP_MIN+high_e_speed*AUTOTEMP_FACTOR;
  329. float t=constrain(AUTOTEMP_MIN,g,AUTOTEMP_MAX);
  330. setTargetHotend0(t);
  331. SERIAL_ECHO_START;
  332. SERIAL_ECHOPAIR("highe",high_e_speed);
  333. SERIAL_ECHOPAIR(" t",t);
  334. SERIAL_ECHOLN("");
  335. }
  336. #endif
  337. void check_axes_activity() {
  338. unsigned char x_active = 0;
  339. unsigned char y_active = 0;
  340. unsigned char z_active = 0;
  341. unsigned char e_active = 0;
  342. block_t *block;
  343. if(block_buffer_tail != block_buffer_head) {
  344. char block_index = block_buffer_tail;
  345. while(block_index != block_buffer_head) {
  346. block = &block_buffer[block_index];
  347. if(block->steps_x != 0) x_active++;
  348. if(block->steps_y != 0) y_active++;
  349. if(block->steps_z != 0) z_active++;
  350. if(block->steps_e != 0) e_active++;
  351. block_index = (block_index+1) & (BLOCK_BUFFER_SIZE - 1);
  352. }
  353. }
  354. if((DISABLE_X) && (x_active == 0)) disable_x();
  355. if((DISABLE_Y) && (y_active == 0)) disable_y();
  356. if((DISABLE_Z) && (z_active == 0)) disable_z();
  357. if((DISABLE_E) && (e_active == 0)) disable_e();
  358. }
  359. // Add a new linear movement to the buffer. steps_x, _y and _z is the absolute position in
  360. // mm. Microseconds specify how many microseconds the move should take to perform. To aid acceleration
  361. // calculation the caller must also provide the physical length of the line in millimeters.
  362. void plan_buffer_line(const float &x, const float &y, const float &z, const float &e, float feed_rate)
  363. {
  364. // Calculate the buffer head after we push this byte
  365. int next_buffer_head = (block_buffer_head + 1) & (BLOCK_BUFFER_SIZE - 1);
  366. // If the buffer is full: good! That means we are well ahead of the robot.
  367. // Rest here until there is room in the buffer.
  368. while(block_buffer_tail == next_buffer_head) {
  369. manage_heater();
  370. manage_inactivity(1);
  371. LCD_STATUS;
  372. }
  373. // The target position of the tool in absolute steps
  374. // Calculate target position in absolute steps
  375. //this should be done after the wait, because otherwise a M92 code within the gcode disrupts this calculation somehow
  376. long target[4];
  377. target[X_AXIS] = lround(x*axis_steps_per_unit[X_AXIS]);
  378. target[Y_AXIS] = lround(y*axis_steps_per_unit[Y_AXIS]);
  379. target[Z_AXIS] = lround(z*axis_steps_per_unit[Z_AXIS]);
  380. target[E_AXIS] = lround(e*axis_steps_per_unit[E_AXIS]);
  381. // Prepare to set up new block
  382. block_t *block = &block_buffer[block_buffer_head];
  383. // Mark block as not busy (Not executed by the stepper interrupt)
  384. block->busy = false;
  385. // Number of steps for each axis
  386. block->steps_x = labs(target[X_AXIS]-position[X_AXIS]);
  387. block->steps_y = labs(target[Y_AXIS]-position[Y_AXIS]);
  388. block->steps_z = labs(target[Z_AXIS]-position[Z_AXIS]);
  389. block->steps_e = labs(target[E_AXIS]-position[E_AXIS]);
  390. block->step_event_count = max(block->steps_x, max(block->steps_y, max(block->steps_z, block->steps_e)));
  391. // Bail if this is a zero-length block
  392. if (block->step_event_count <=dropsegments) {
  393. return;
  394. };
  395. //enable active axes
  396. if(block->steps_x != 0) enable_x();
  397. if(block->steps_y != 0) enable_y();
  398. if(block->steps_z != 0) enable_z();
  399. if(block->steps_e != 0) enable_e();
  400. float delta_x_mm = (target[X_AXIS]-position[X_AXIS])/axis_steps_per_unit[X_AXIS];
  401. float delta_y_mm = (target[Y_AXIS]-position[Y_AXIS])/axis_steps_per_unit[Y_AXIS];
  402. float delta_z_mm = (target[Z_AXIS]-position[Z_AXIS])/axis_steps_per_unit[Z_AXIS];
  403. float delta_e_mm = (target[E_AXIS]-position[E_AXIS])/axis_steps_per_unit[E_AXIS];
  404. block->millimeters = sqrt(square(delta_x_mm) + square(delta_y_mm) + square(delta_z_mm) + square(delta_e_mm));
  405. unsigned long microseconds;
  406. if (block->steps_e == 0) {
  407. if(feed_rate<mintravelfeedrate) feed_rate=mintravelfeedrate;
  408. }
  409. else {
  410. if(feed_rate<minimumfeedrate) feed_rate=minimumfeedrate;
  411. }
  412. microseconds = lround((block->millimeters/feed_rate)*1000000);
  413. // slow down when de buffer starts to empty, rather than wait at the corner for a buffer refill
  414. // reduces/removes corner blobs as the machine won't come to a full stop.
  415. int blockcount=(block_buffer_head-block_buffer_tail + BLOCK_BUFFER_SIZE) & (BLOCK_BUFFER_SIZE - 1);
  416. if ((blockcount>0) && (blockcount < (BLOCK_BUFFER_SIZE - 4))) {
  417. if (microseconds<minsegmenttime) { // buffer is draining, add extra time. The amount of time added increases if the buffer is still emptied more.
  418. microseconds=microseconds+lround(2*(minsegmenttime-microseconds)/blockcount);
  419. }
  420. }
  421. else {
  422. if (microseconds<minsegmenttime) microseconds=minsegmenttime;
  423. }
  424. // END OF SLOW DOWN SECTION
  425. // Calculate speed in mm/minute for each axis
  426. float multiplier = 60.0*1000000.0/microseconds;
  427. block->speed_z = delta_z_mm * multiplier;
  428. block->speed_x = delta_x_mm * multiplier;
  429. block->speed_y = delta_y_mm * multiplier;
  430. block->speed_e = delta_e_mm * multiplier;
  431. // Limit speed per axis
  432. float speed_factor = 1; //factor <=1 do decrease speed
  433. if(abs(block->speed_x) > max_feedrate[X_AXIS]) {
  434. speed_factor = max_feedrate[X_AXIS] / abs(block->speed_x);
  435. //if(speed_factor > tmp_speed_factor) speed_factor = tmp_speed_factor; /is not need here because auf the init above
  436. }
  437. if(abs(block->speed_y) > max_feedrate[Y_AXIS]){
  438. float tmp_speed_factor = max_feedrate[Y_AXIS] / abs(block->speed_y);
  439. if(speed_factor > tmp_speed_factor) speed_factor = tmp_speed_factor;
  440. }
  441. if(abs(block->speed_z) > max_feedrate[Z_AXIS]){
  442. float tmp_speed_factor = max_feedrate[Z_AXIS] / abs(block->speed_z);
  443. if(speed_factor > tmp_speed_factor) speed_factor = tmp_speed_factor;
  444. }
  445. if(abs(block->speed_e) > max_feedrate[E_AXIS]){
  446. float tmp_speed_factor = max_feedrate[E_AXIS] / abs(block->speed_e);
  447. if(speed_factor > tmp_speed_factor) speed_factor = tmp_speed_factor;
  448. }
  449. multiplier = multiplier * speed_factor;
  450. block->speed_z = delta_z_mm * multiplier;
  451. block->speed_x = delta_x_mm * multiplier;
  452. block->speed_y = delta_y_mm * multiplier;
  453. block->speed_e = delta_e_mm * multiplier;
  454. block->nominal_speed = block->millimeters * multiplier;
  455. block->nominal_rate = ceil(block->step_event_count * multiplier / 60);
  456. if(block->nominal_rate < 120)
  457. block->nominal_rate = 120;
  458. block->entry_speed = safe_speed(block);
  459. // Compute the acceleration rate for the trapezoid generator.
  460. float travel_per_step = block->millimeters/block->step_event_count;
  461. if(block->steps_x == 0 && block->steps_y == 0 && block->steps_z == 0) {
  462. block->acceleration_st = ceil( (retract_acceleration)/travel_per_step); // convert to: acceleration steps/sec^2
  463. }
  464. else {
  465. block->acceleration_st = ceil( (acceleration)/travel_per_step); // convert to: acceleration steps/sec^2
  466. float tmp_acceleration = (float)block->acceleration_st / (float)block->step_event_count;
  467. // Limit acceleration per axis
  468. if((tmp_acceleration * block->steps_x) > axis_steps_per_sqr_second[X_AXIS]) {
  469. block->acceleration_st = axis_steps_per_sqr_second[X_AXIS];
  470. tmp_acceleration = (float)block->acceleration_st / (float)block->step_event_count;
  471. }
  472. if((tmp_acceleration * block->steps_y) > axis_steps_per_sqr_second[Y_AXIS]) {
  473. block->acceleration_st = axis_steps_per_sqr_second[Y_AXIS];
  474. tmp_acceleration = (float)block->acceleration_st / (float)block->step_event_count;
  475. }
  476. if((tmp_acceleration * block->steps_e) > axis_steps_per_sqr_second[E_AXIS]) {
  477. block->acceleration_st = axis_steps_per_sqr_second[E_AXIS];
  478. tmp_acceleration = (float)block->acceleration_st / (float)block->step_event_count;
  479. }
  480. if((tmp_acceleration * block->steps_z) > axis_steps_per_sqr_second[Z_AXIS]) {
  481. block->acceleration_st = axis_steps_per_sqr_second[Z_AXIS];
  482. tmp_acceleration = (float)block->acceleration_st / (float)block->step_event_count;
  483. }
  484. }
  485. block->acceleration = block->acceleration_st * travel_per_step;
  486. block->acceleration_rate = (long)((float)block->acceleration_st * 8.388608);
  487. #ifdef ADVANCE
  488. // Calculate advance rate
  489. if((block->steps_e == 0) || (block->steps_x == 0 && block->steps_y == 0 && block->steps_z == 0)) {
  490. block->advance_rate = 0;
  491. block->advance = 0;
  492. }
  493. else {
  494. long acc_dist = estimate_acceleration_distance(0, block->nominal_rate, block->acceleration_st);
  495. float advance = (STEPS_PER_CUBIC_MM_E * EXTRUDER_ADVANCE_K) *
  496. (block->speed_e * block->speed_e * EXTRUTION_AREA * EXTRUTION_AREA / 3600.0)*65536;
  497. block->advance = advance;
  498. if(acc_dist == 0) {
  499. block->advance_rate = 0;
  500. }
  501. else {
  502. block->advance_rate = advance / (float)acc_dist;
  503. }
  504. }
  505. #endif // ADVANCE
  506. // compute a preliminary conservative acceleration trapezoid
  507. float safespeed = safe_speed(block);
  508. calculate_trapezoid_for_block(block, safespeed, safespeed);
  509. // Compute direction bits for this block
  510. block->direction_bits = 0;
  511. if (target[X_AXIS] < position[X_AXIS]) {
  512. block->direction_bits |= (1<<X_AXIS);
  513. }
  514. if (target[Y_AXIS] < position[Y_AXIS]) {
  515. block->direction_bits |= (1<<Y_AXIS);
  516. }
  517. if (target[Z_AXIS] < position[Z_AXIS]) {
  518. block->direction_bits |= (1<<Z_AXIS);
  519. }
  520. if (target[E_AXIS] < position[E_AXIS]) {
  521. block->direction_bits |= (1<<E_AXIS);
  522. }
  523. // Move buffer head
  524. block_buffer_head = next_buffer_head;
  525. // Update position
  526. memcpy(position, target, sizeof(target)); // position[] = target[]
  527. planner_recalculate();
  528. #ifdef AUTOTEMP
  529. getHighESpeed();
  530. #endif
  531. st_wake_up();
  532. }
  533. void plan_set_position(const float &x, const float &y, const float &z, const float &e)
  534. {
  535. position[X_AXIS] = lround(x*axis_steps_per_unit[X_AXIS]);
  536. position[Y_AXIS] = lround(y*axis_steps_per_unit[Y_AXIS]);
  537. position[Z_AXIS] = lround(z*axis_steps_per_unit[Z_AXIS]);
  538. position[E_AXIS] = lround(e*axis_steps_per_unit[E_AXIS]);
  539. }