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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. unsigned long minsegmenttime;
  48. float max_feedrate[4]; // set the max speeds
  49. float axis_steps_per_unit[4];
  50. long max_acceleration_units_per_sq_second[4]; // Use M201 to override by software
  51. float minimumfeedrate;
  52. float acceleration; // Normal acceleration mm/s^2 THIS IS THE DEFAULT ACCELERATION for all moves. M204 SXXXX
  53. float retract_acceleration; // mm/s^2 filament pull-pack and push-forward while standing still in the other axis M204 TXXXX
  54. float max_xy_jerk; //speed than can be stopped at once, if i understand correctly.
  55. float max_z_jerk;
  56. float mintravelfeedrate;
  57. unsigned long axis_steps_per_sqr_second[NUM_AXIS];
  58. // Manage heater variables.
  59. static block_t block_buffer[BLOCK_BUFFER_SIZE]; // A ring buffer for motion instfructions
  60. static volatile unsigned char block_buffer_head; // Index of the next block to be pushed
  61. static volatile unsigned char block_buffer_tail; // Index of the block to process now
  62. // The current position of the tool in absolute steps
  63. long position[4];
  64. #define ONE_MINUTE_OF_MICROSECONDS 60000000.0
  65. // Calculates the distance (not time) it takes to accelerate from initial_rate to target_rate using the
  66. // given acceleration:
  67. inline float estimate_acceleration_distance(float initial_rate, float target_rate, float acceleration) {
  68. if (acceleration!=0) {
  69. return((target_rate*target_rate-initial_rate*initial_rate)/
  70. (2.0*acceleration));
  71. }
  72. else {
  73. return 0.0; // acceleration was 0, set acceleration distance to 0
  74. }
  75. }
  76. // This function gives you the point at which you must start braking (at the rate of -acceleration) if
  77. // you started at speed initial_rate and accelerated until this point and want to end at the final_rate after
  78. // a total travel of distance. This can be used to compute the intersection point between acceleration and
  79. // deceleration in the cases where the trapezoid has no plateau (i.e. never reaches maximum speed)
  80. inline float intersection_distance(float initial_rate, float final_rate, float acceleration, float distance) {
  81. if (acceleration!=0) {
  82. return((2.0*acceleration*distance-initial_rate*initial_rate+final_rate*final_rate)/
  83. (4.0*acceleration) );
  84. }
  85. else {
  86. return 0.0; // acceleration was 0, set intersection distance to 0
  87. }
  88. }
  89. // Calculates trapezoid parameters so that the entry- and exit-speed is compensated by the provided factors.
  90. void calculate_trapezoid_for_block(block_t *block, float entry_speed, float exit_speed) {
  91. if(block->busy == true) return; // If block is busy then bail out.
  92. float entry_factor = entry_speed / block->nominal_speed;
  93. float exit_factor = exit_speed / block->nominal_speed;
  94. long initial_rate = ceil(block->nominal_rate*entry_factor);
  95. long final_rate = ceil(block->nominal_rate*exit_factor);
  96. #ifdef ADVANCE
  97. long initial_advance = block->advance*entry_factor*entry_factor;
  98. long final_advance = block->advance*exit_factor*exit_factor;
  99. #endif // ADVANCE
  100. // Limit minimal step rate (Otherwise the timer will overflow.)
  101. if(initial_rate <120) initial_rate=120;
  102. if(final_rate < 120) final_rate=120;
  103. // Calculate the acceleration steps
  104. long acceleration = block->acceleration_st;
  105. long accelerate_steps = estimate_acceleration_distance(initial_rate, block->nominal_rate, acceleration);
  106. long decelerate_steps = estimate_acceleration_distance(final_rate, block->nominal_rate, acceleration);
  107. // Calculate the size of Plateau of Nominal Rate.
  108. long plateau_steps = block->step_event_count-accelerate_steps-decelerate_steps;
  109. // Is the Plateau of Nominal Rate smaller than nothing? That means no cruising, and we will
  110. // have to use intersection_distance() to calculate when to abort acceleration and start braking
  111. // in order to reach the final_rate exactly at the end of this block.
  112. if (plateau_steps < 0) {
  113. accelerate_steps = intersection_distance(initial_rate, final_rate, acceleration, block->step_event_count);
  114. plateau_steps = 0;
  115. }
  116. long decelerate_after = accelerate_steps+plateau_steps;
  117. CRITICAL_SECTION_START; // Fill variables used by the stepper in a critical section
  118. if(block->busy == false) { // Don't update variables if block is busy.
  119. block->accelerate_until = accelerate_steps;
  120. block->decelerate_after = decelerate_after;
  121. block->initial_rate = initial_rate;
  122. block->final_rate = final_rate;
  123. #ifdef ADVANCE
  124. block->initial_advance = initial_advance;
  125. block->final_advance = final_advance;
  126. #endif //ADVANCE
  127. }
  128. CRITICAL_SECTION_END;
  129. }
  130. // Calculates the maximum allowable speed at this point when you must be able to reach target_velocity using the
  131. // acceleration within the allotted distance.
  132. inline float max_allowable_speed(float acceleration, float target_velocity, float distance) {
  133. return(
  134. sqrt(target_velocity*target_velocity-2*acceleration*60*60*distance)
  135. );
  136. }
  137. // "Junction jerk" in this context is the immediate change in speed at the junction of two blocks.
  138. // This method will calculate the junction jerk as the euclidean distance between the nominal
  139. // velocities of the respective blocks.
  140. inline float junction_jerk(block_t *before, block_t *after) {
  141. return(sqrt(
  142. pow((before->speed_x-after->speed_x), 2)+
  143. pow((before->speed_y-after->speed_y), 2)));
  144. }
  145. // Return the safe speed which is max_jerk/2, e.g. the
  146. // speed under which you cannot exceed max_jerk no matter what you do.
  147. float safe_speed(block_t *block) {
  148. float safe_speed;
  149. safe_speed = max_xy_jerk/2;
  150. if(abs(block->speed_z) > max_z_jerk/2) safe_speed = max_z_jerk/2;
  151. if (safe_speed > block->nominal_speed) 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(float x, float y, float z, float e, float feed_rate) {
  328. // Calculate the buffer head after we push this byte
  329. int next_buffer_head = (block_buffer_head + 1) & (BLOCK_BUFFER_SIZE - 1);
  330. // If the buffer is full: good! That means we are well ahead of the robot.
  331. // Rest here until there is room in the buffer.
  332. while(block_buffer_tail == next_buffer_head) {
  333. manage_heater();
  334. manage_inactivity(1);
  335. LCD_STATUS;
  336. }
  337. // The target position of the tool in absolute steps
  338. // Calculate target position in absolute steps
  339. //this should be done after the wait, because otherwise a M92 code within the gcode disrupts this calculation somehow
  340. long target[4];
  341. target[X_AXIS] = lround(x*axis_steps_per_unit[X_AXIS]);
  342. target[Y_AXIS] = lround(y*axis_steps_per_unit[Y_AXIS]);
  343. target[Z_AXIS] = lround(z*axis_steps_per_unit[Z_AXIS]);
  344. target[E_AXIS] = lround(e*axis_steps_per_unit[E_AXIS]);
  345. // Prepare to set up new block
  346. block_t *block = &block_buffer[block_buffer_head];
  347. // Mark block as not busy (Not executed by the stepper interrupt)
  348. block->busy = false;
  349. // Number of steps for each axis
  350. block->steps_x = labs(target[X_AXIS]-position[X_AXIS]);
  351. block->steps_y = labs(target[Y_AXIS]-position[Y_AXIS]);
  352. block->steps_z = labs(target[Z_AXIS]-position[Z_AXIS]);
  353. block->steps_e = labs(target[E_AXIS]-position[E_AXIS]);
  354. block->step_event_count = max(block->steps_x, max(block->steps_y, max(block->steps_z, block->steps_e)));
  355. // Bail if this is a zero-length block
  356. if (block->step_event_count <=dropsegments) {
  357. return;
  358. };
  359. //enable active axes
  360. if(block->steps_x != 0) enable_x();
  361. if(block->steps_y != 0) enable_y();
  362. if(block->steps_z != 0) enable_z();
  363. if(block->steps_e != 0) enable_e();
  364. float delta_x_mm = (target[X_AXIS]-position[X_AXIS])/axis_steps_per_unit[X_AXIS];
  365. float delta_y_mm = (target[Y_AXIS]-position[Y_AXIS])/axis_steps_per_unit[Y_AXIS];
  366. float delta_z_mm = (target[Z_AXIS]-position[Z_AXIS])/axis_steps_per_unit[Z_AXIS];
  367. float delta_e_mm = (target[E_AXIS]-position[E_AXIS])/axis_steps_per_unit[E_AXIS];
  368. block->millimeters = sqrt(square(delta_x_mm) + square(delta_y_mm) + square(delta_z_mm) + square(delta_e_mm));
  369. unsigned long microseconds;
  370. if (block->steps_e == 0) {
  371. if(feed_rate<mintravelfeedrate) feed_rate=mintravelfeedrate;
  372. }
  373. else {
  374. if(feed_rate<minimumfeedrate) feed_rate=minimumfeedrate;
  375. }
  376. microseconds = lround((block->millimeters/feed_rate)*1000000);
  377. // slow down when de buffer starts to empty, rather than wait at the corner for a buffer refill
  378. // reduces/removes corner blobs as the machine won't come to a full stop.
  379. int blockcount=(block_buffer_head-block_buffer_tail + BLOCK_BUFFER_SIZE) & (BLOCK_BUFFER_SIZE - 1);
  380. if ((blockcount>0) && (blockcount < (BLOCK_BUFFER_SIZE - 4))) {
  381. if (microseconds<minsegmenttime) { // buffer is draining, add extra time. The amount of time added increases if the buffer is still emptied more.
  382. microseconds=microseconds+lround(2*(minsegmenttime-microseconds)/blockcount);
  383. }
  384. }
  385. else {
  386. if (microseconds<minsegmenttime) microseconds=minsegmenttime;
  387. }
  388. // END OF SLOW DOWN SECTION
  389. // Calculate speed in mm/minute for each axis
  390. float multiplier = 60.0*1000000.0/microseconds;
  391. block->speed_z = delta_z_mm * multiplier;
  392. block->speed_x = delta_x_mm * multiplier;
  393. block->speed_y = delta_y_mm * multiplier;
  394. block->speed_e = delta_e_mm * multiplier;
  395. // Limit speed per axis
  396. float speed_factor = 1; //factor <=1 do decrease speed
  397. if(abs(block->speed_x) > max_feedrate[X_AXIS]) {
  398. //// [ErikDeBruijn] IS THIS THE BUG WE'RE LOOING FOR????
  399. //// [bernhard] No its not, according to Zalm.
  400. //// the if would always be true, since tmp_speedfactor <=0 due the inial if, so its safe to set. the next lines actually compare.
  401. speed_factor = max_feedrate[X_AXIS] / abs(block->speed_x);
  402. //if(speed_factor > tmp_speed_factor) speed_factor = tmp_speed_factor;
  403. }
  404. if(abs(block->speed_y) > max_feedrate[Y_AXIS]){
  405. float tmp_speed_factor = max_feedrate[Y_AXIS] / abs(block->speed_y);
  406. if(speed_factor > tmp_speed_factor) speed_factor = tmp_speed_factor;
  407. }
  408. if(abs(block->speed_z) > max_feedrate[Z_AXIS]){
  409. float tmp_speed_factor = max_feedrate[Z_AXIS] / abs(block->speed_z);
  410. if(speed_factor > tmp_speed_factor) speed_factor = tmp_speed_factor;
  411. }
  412. if(abs(block->speed_e) > max_feedrate[E_AXIS]){
  413. float tmp_speed_factor = max_feedrate[E_AXIS] / abs(block->speed_e);
  414. if(speed_factor > tmp_speed_factor) speed_factor = tmp_speed_factor;
  415. }
  416. multiplier = multiplier * speed_factor;
  417. block->speed_z = delta_z_mm * multiplier;
  418. block->speed_x = delta_x_mm * multiplier;
  419. block->speed_y = delta_y_mm * multiplier;
  420. block->speed_e = delta_e_mm * multiplier;
  421. block->nominal_speed = block->millimeters * multiplier;
  422. block->nominal_rate = ceil(block->step_event_count * multiplier / 60);
  423. if(block->nominal_rate < 120) block->nominal_rate = 120;
  424. block->entry_speed = safe_speed(block);
  425. // Compute the acceleration rate for the trapezoid generator.
  426. float travel_per_step = block->millimeters/block->step_event_count;
  427. if(block->steps_x == 0 && block->steps_y == 0 && block->steps_z == 0) {
  428. block->acceleration_st = ceil( (retract_acceleration)/travel_per_step); // convert to: acceleration steps/sec^2
  429. }
  430. else {
  431. block->acceleration_st = ceil( (acceleration)/travel_per_step); // convert to: acceleration steps/sec^2
  432. float tmp_acceleration = (float)block->acceleration_st / (float)block->step_event_count;
  433. // Limit acceleration per axis
  434. if((tmp_acceleration * block->steps_x) > axis_steps_per_sqr_second[X_AXIS]) {
  435. block->acceleration_st = axis_steps_per_sqr_second[X_AXIS];
  436. tmp_acceleration = (float)block->acceleration_st / (float)block->step_event_count;
  437. }
  438. if((tmp_acceleration * block->steps_y) > axis_steps_per_sqr_second[Y_AXIS]) {
  439. block->acceleration_st = axis_steps_per_sqr_second[Y_AXIS];
  440. tmp_acceleration = (float)block->acceleration_st / (float)block->step_event_count;
  441. }
  442. if((tmp_acceleration * block->steps_e) > axis_steps_per_sqr_second[E_AXIS]) {
  443. block->acceleration_st = axis_steps_per_sqr_second[E_AXIS];
  444. tmp_acceleration = (float)block->acceleration_st / (float)block->step_event_count;
  445. }
  446. if((tmp_acceleration * block->steps_z) > axis_steps_per_sqr_second[Z_AXIS]) {
  447. block->acceleration_st = axis_steps_per_sqr_second[Z_AXIS];
  448. tmp_acceleration = (float)block->acceleration_st / (float)block->step_event_count;
  449. }
  450. }
  451. block->acceleration = block->acceleration_st * travel_per_step;
  452. block->acceleration_rate = (long)((float)block->acceleration_st * 8.388608);
  453. #ifdef ADVANCE
  454. // Calculate advance rate
  455. if((block->steps_e == 0) || (block->steps_x == 0 && block->steps_y == 0 && block->steps_z == 0)) {
  456. block->advance_rate = 0;
  457. block->advance = 0;
  458. }
  459. else {
  460. long acc_dist = estimate_acceleration_distance(0, block->nominal_rate, block->acceleration_st);
  461. float advance = (STEPS_PER_CUBIC_MM_E * EXTRUDER_ADVANCE_K) *
  462. (block->speed_e * block->speed_e * EXTRUTION_AREA * EXTRUTION_AREA / 3600.0)*65536;
  463. block->advance = advance;
  464. if(acc_dist == 0) {
  465. block->advance_rate = 0;
  466. }
  467. else {
  468. block->advance_rate = advance / (float)acc_dist;
  469. }
  470. }
  471. #endif // ADVANCE
  472. // compute a preliminary conservative acceleration trapezoid
  473. float safespeed = safe_speed(block);
  474. calculate_trapezoid_for_block(block, safespeed, safespeed);
  475. // Compute direction bits for this block
  476. block->direction_bits = 0;
  477. if (target[X_AXIS] < position[X_AXIS]) {
  478. block->direction_bits |= (1<<X_AXIS);
  479. }
  480. if (target[Y_AXIS] < position[Y_AXIS]) {
  481. block->direction_bits |= (1<<Y_AXIS);
  482. }
  483. if (target[Z_AXIS] < position[Z_AXIS]) {
  484. block->direction_bits |= (1<<Z_AXIS);
  485. }
  486. if (target[E_AXIS] < position[E_AXIS]) {
  487. block->direction_bits |= (1<<E_AXIS);
  488. }
  489. // Move buffer head
  490. block_buffer_head = next_buffer_head;
  491. // Update position
  492. memcpy(position, target, sizeof(target)); // position[] = target[]
  493. planner_recalculate();
  494. st_wake_up();
  495. }
  496. void plan_set_position(float x, float y, float z, float e)
  497. {
  498. position[X_AXIS] = lround(x*axis_steps_per_unit[X_AXIS]);
  499. position[Y_AXIS] = lround(y*axis_steps_per_unit[Y_AXIS]);
  500. position[Z_AXIS] = lround(z*axis_steps_per_unit[Z_AXIS]);
  501. position[E_AXIS] = lround(e*axis_steps_per_unit[E_AXIS]);
  502. }