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

planner.cpp 23KB

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