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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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 sqrt(target_velocity*target_velocity-2*acceleration*60*60*distance);
  134. }
  135. // "Junction jerk" in this context is the immediate change in speed at the junction of two blocks.
  136. // This method will calculate the junction jerk as the euclidean distance between the nominal
  137. // velocities of the respective blocks.
  138. inline float junction_jerk(block_t *before, block_t *after) {
  139. return sqrt(
  140. pow((before->speed_x-after->speed_x), 2)+pow((before->speed_y-after->speed_y), 2));
  141. }
  142. // Return the safe speed which is max_jerk/2, e.g. the
  143. // speed under which you cannot exceed max_jerk no matter what you do.
  144. float safe_speed(block_t *block) {
  145. float safe_speed;
  146. safe_speed = max_xy_jerk/2;
  147. if(abs(block->speed_z) > max_z_jerk/2)
  148. safe_speed = max_z_jerk/2;
  149. if (safe_speed > block->nominal_speed)
  150. safe_speed = block->nominal_speed;
  151. return safe_speed;
  152. }
  153. // The kernel called by planner_recalculate() when scanning the plan from last to first entry.
  154. void planner_reverse_pass_kernel(block_t *previous, block_t *current, block_t *next) {
  155. if(!current) {
  156. return;
  157. }
  158. float entry_speed = current->nominal_speed;
  159. float exit_factor;
  160. float exit_speed;
  161. if (next) {
  162. exit_speed = next->entry_speed;
  163. }
  164. else {
  165. exit_speed = safe_speed(current);
  166. }
  167. // Calculate the entry_factor for the current block.
  168. if (previous) {
  169. // Reduce speed so that junction_jerk is within the maximum allowed
  170. float jerk = junction_jerk(previous, current);
  171. if((previous->steps_x == 0) && (previous->steps_y == 0)) {
  172. entry_speed = safe_speed(current);
  173. }
  174. else if (jerk > max_xy_jerk) {
  175. entry_speed = (max_xy_jerk/jerk) * entry_speed;
  176. }
  177. if(abs(previous->speed_z - current->speed_z) > max_z_jerk) {
  178. entry_speed = (max_z_jerk/abs(previous->speed_z - current->speed_z)) * entry_speed;
  179. }
  180. // If the required deceleration across the block is too rapid, reduce the entry_factor accordingly.
  181. if (entry_speed > exit_speed) {
  182. float max_entry_speed = max_allowable_speed(-current->acceleration,exit_speed, current->millimeters);
  183. if (max_entry_speed < entry_speed) {
  184. entry_speed = max_entry_speed;
  185. }
  186. }
  187. }
  188. else {
  189. entry_speed = safe_speed(current);
  190. }
  191. // Store result
  192. current->entry_speed = entry_speed;
  193. }
  194. // planner_recalculate() needs to go over the current plan twice. Once in reverse and once forward. This
  195. // implements the reverse pass.
  196. void planner_reverse_pass() {
  197. char block_index = block_buffer_head;
  198. if(((block_buffer_head-block_buffer_tail + BLOCK_BUFFER_SIZE) & (BLOCK_BUFFER_SIZE - 1)) > 3) {
  199. block_index = (block_buffer_head - 3) & (BLOCK_BUFFER_SIZE - 1);
  200. block_t *block[5] = {
  201. NULL, NULL, NULL, NULL, NULL };
  202. while(block_index != block_buffer_tail) {
  203. block_index = (block_index-1) & (BLOCK_BUFFER_SIZE -1);
  204. block[2]= block[1];
  205. block[1]= block[0];
  206. block[0] = &block_buffer[block_index];
  207. planner_reverse_pass_kernel(block[0], block[1], block[2]);
  208. }
  209. planner_reverse_pass_kernel(NULL, block[0], block[1]);
  210. }
  211. }
  212. // The kernel called by planner_recalculate() when scanning the plan from first to last entry.
  213. void planner_forward_pass_kernel(block_t *previous, block_t *current, block_t *next) {
  214. if(!current) {
  215. return;
  216. }
  217. if(previous) {
  218. // If the previous block is an acceleration block, but it is not long enough to
  219. // complete the full speed change within the block, we need to adjust out entry
  220. // speed accordingly. Remember current->entry_factor equals the exit factor of
  221. // the previous block.
  222. if(previous->entry_speed < current->entry_speed) {
  223. float max_entry_speed = max_allowable_speed(-previous->acceleration, previous->entry_speed, previous->millimeters);
  224. if (max_entry_speed < current->entry_speed) {
  225. current->entry_speed = max_entry_speed;
  226. }
  227. }
  228. }
  229. }
  230. // planner_recalculate() needs to go over the current plan twice. Once in reverse and once forward. This
  231. // implements the forward pass.
  232. void planner_forward_pass() {
  233. char block_index = block_buffer_tail;
  234. block_t *block[3] = {
  235. NULL, NULL, NULL };
  236. while(block_index != block_buffer_head) {
  237. block[0] = block[1];
  238. block[1] = block[2];
  239. block[2] = &block_buffer[block_index];
  240. planner_forward_pass_kernel(block[0],block[1],block[2]);
  241. block_index = (block_index+1) & (BLOCK_BUFFER_SIZE - 1);
  242. }
  243. planner_forward_pass_kernel(block[1], block[2], NULL);
  244. }
  245. // Recalculates the trapezoid speed profiles for all blocks in the plan according to the
  246. // entry_factor for each junction. Must be called by planner_recalculate() after
  247. // updating the blocks.
  248. void planner_recalculate_trapezoids() {
  249. char block_index = block_buffer_tail;
  250. block_t *current;
  251. block_t *next = NULL;
  252. while(block_index != block_buffer_head) {
  253. current = next;
  254. next = &block_buffer[block_index];
  255. if (current) {
  256. calculate_trapezoid_for_block(current, current->entry_speed, next->entry_speed);
  257. }
  258. block_index = (block_index+1) & (BLOCK_BUFFER_SIZE - 1);
  259. }
  260. calculate_trapezoid_for_block(next, next->entry_speed, safe_speed(next));
  261. }
  262. // Recalculates the motion plan according to the following algorithm:
  263. //
  264. // 1. Go over every block in reverse order and calculate a junction speed reduction (i.e. block_t.entry_factor)
  265. // so that:
  266. // a. The junction jerk is within the set limit
  267. // b. No speed reduction within one block requires faster deceleration than the one, true constant
  268. // acceleration.
  269. // 2. Go over every block in chronological order and dial down junction speed reduction values if
  270. // a. The speed increase within one block would require faster accelleration than the one, true
  271. // constant acceleration.
  272. //
  273. // When these stages are complete all blocks have an entry_factor that will allow all speed changes to
  274. // be performed using only the one, true constant acceleration, and where no junction jerk is jerkier than
  275. // the set limit. Finally it will:
  276. //
  277. // 3. Recalculate trapezoids for all blocks.
  278. void planner_recalculate() {
  279. planner_reverse_pass();
  280. planner_forward_pass();
  281. planner_recalculate_trapezoids();
  282. }
  283. void plan_init() {
  284. block_buffer_head = 0;
  285. block_buffer_tail = 0;
  286. memset(position, 0, sizeof(position)); // clear position
  287. }
  288. void plan_discard_current_block() {
  289. if (block_buffer_head != block_buffer_tail) {
  290. block_buffer_tail = (block_buffer_tail + 1) & (BLOCK_BUFFER_SIZE - 1);
  291. }
  292. }
  293. block_t *plan_get_current_block() {
  294. if (block_buffer_head == block_buffer_tail) {
  295. return(NULL);
  296. }
  297. block_t *block = &block_buffer[block_buffer_tail];
  298. block->busy = true;
  299. return(block);
  300. }
  301. void check_axes_activity() {
  302. unsigned char x_active = 0;
  303. unsigned char y_active = 0;
  304. unsigned char z_active = 0;
  305. unsigned char e_active = 0;
  306. block_t *block;
  307. if(block_buffer_tail != block_buffer_head) {
  308. char block_index = block_buffer_tail;
  309. while(block_index != block_buffer_head) {
  310. block = &block_buffer[block_index];
  311. if(block->steps_x != 0) x_active++;
  312. if(block->steps_y != 0) y_active++;
  313. if(block->steps_z != 0) z_active++;
  314. if(block->steps_e != 0) e_active++;
  315. block_index = (block_index+1) & (BLOCK_BUFFER_SIZE - 1);
  316. }
  317. }
  318. if((DISABLE_X) && (x_active == 0)) disable_x();
  319. if((DISABLE_Y) && (y_active == 0)) disable_y();
  320. if((DISABLE_Z) && (z_active == 0)) disable_z();
  321. if((DISABLE_E) && (e_active == 0)) disable_e();
  322. }
  323. // Add a new linear movement to the buffer. steps_x, _y and _z is the absolute position in
  324. // mm. Microseconds specify how many microseconds the move should take to perform. To aid acceleration
  325. // calculation the caller must also provide the physical length of the line in millimeters.
  326. void plan_buffer_line(const float &x, const float &y, const float &z, const float &e, float feed_rate)
  327. {
  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. speed_factor = max_feedrate[X_AXIS] / abs(block->speed_x);
  399. //if(speed_factor > tmp_speed_factor) speed_factor = tmp_speed_factor; /is not need here because auf the init above
  400. }
  401. if(abs(block->speed_y) > max_feedrate[Y_AXIS]){
  402. float tmp_speed_factor = max_feedrate[Y_AXIS] / abs(block->speed_y);
  403. if(speed_factor > tmp_speed_factor) speed_factor = tmp_speed_factor;
  404. }
  405. if(abs(block->speed_z) > max_feedrate[Z_AXIS]){
  406. float tmp_speed_factor = max_feedrate[Z_AXIS] / abs(block->speed_z);
  407. if(speed_factor > tmp_speed_factor) speed_factor = tmp_speed_factor;
  408. }
  409. if(abs(block->speed_e) > max_feedrate[E_AXIS]){
  410. float tmp_speed_factor = max_feedrate[E_AXIS] / abs(block->speed_e);
  411. if(speed_factor > tmp_speed_factor) speed_factor = tmp_speed_factor;
  412. }
  413. multiplier = multiplier * speed_factor;
  414. block->speed_z = delta_z_mm * multiplier;
  415. block->speed_x = delta_x_mm * multiplier;
  416. block->speed_y = delta_y_mm * multiplier;
  417. block->speed_e = delta_e_mm * multiplier;
  418. block->nominal_speed = block->millimeters * multiplier;
  419. block->nominal_rate = ceil(block->step_event_count * multiplier / 60);
  420. if(block->nominal_rate < 120)
  421. block->nominal_rate = 120;
  422. block->entry_speed = safe_speed(block);
  423. // Compute the acceleration rate for the trapezoid generator.
  424. float travel_per_step = block->millimeters/block->step_event_count;
  425. if(block->steps_x == 0 && block->steps_y == 0 && block->steps_z == 0) {
  426. block->acceleration_st = ceil( (retract_acceleration)/travel_per_step); // convert to: acceleration steps/sec^2
  427. }
  428. else {
  429. block->acceleration_st = ceil( (acceleration)/travel_per_step); // convert to: acceleration steps/sec^2
  430. float tmp_acceleration = (float)block->acceleration_st / (float)block->step_event_count;
  431. // Limit acceleration per axis
  432. if((tmp_acceleration * block->steps_x) > axis_steps_per_sqr_second[X_AXIS]) {
  433. block->acceleration_st = axis_steps_per_sqr_second[X_AXIS];
  434. tmp_acceleration = (float)block->acceleration_st / (float)block->step_event_count;
  435. }
  436. if((tmp_acceleration * block->steps_y) > axis_steps_per_sqr_second[Y_AXIS]) {
  437. block->acceleration_st = axis_steps_per_sqr_second[Y_AXIS];
  438. tmp_acceleration = (float)block->acceleration_st / (float)block->step_event_count;
  439. }
  440. if((tmp_acceleration * block->steps_e) > axis_steps_per_sqr_second[E_AXIS]) {
  441. block->acceleration_st = axis_steps_per_sqr_second[E_AXIS];
  442. tmp_acceleration = (float)block->acceleration_st / (float)block->step_event_count;
  443. }
  444. if((tmp_acceleration * block->steps_z) > axis_steps_per_sqr_second[Z_AXIS]) {
  445. block->acceleration_st = axis_steps_per_sqr_second[Z_AXIS];
  446. tmp_acceleration = (float)block->acceleration_st / (float)block->step_event_count;
  447. }
  448. }
  449. block->acceleration = block->acceleration_st * travel_per_step;
  450. block->acceleration_rate = (long)((float)block->acceleration_st * 8.388608);
  451. #ifdef ADVANCE
  452. // Calculate advance rate
  453. if((block->steps_e == 0) || (block->steps_x == 0 && block->steps_y == 0 && block->steps_z == 0)) {
  454. block->advance_rate = 0;
  455. block->advance = 0;
  456. }
  457. else {
  458. long acc_dist = estimate_acceleration_distance(0, block->nominal_rate, block->acceleration_st);
  459. float advance = (STEPS_PER_CUBIC_MM_E * EXTRUDER_ADVANCE_K) *
  460. (block->speed_e * block->speed_e * EXTRUTION_AREA * EXTRUTION_AREA / 3600.0)*65536;
  461. block->advance = advance;
  462. if(acc_dist == 0) {
  463. block->advance_rate = 0;
  464. }
  465. else {
  466. block->advance_rate = advance / (float)acc_dist;
  467. }
  468. }
  469. #endif // ADVANCE
  470. // compute a preliminary conservative acceleration trapezoid
  471. float safespeed = safe_speed(block);
  472. calculate_trapezoid_for_block(block, safespeed, safespeed);
  473. // Compute direction bits for this block
  474. block->direction_bits = 0;
  475. if (target[X_AXIS] < position[X_AXIS]) {
  476. block->direction_bits |= (1<<X_AXIS);
  477. }
  478. if (target[Y_AXIS] < position[Y_AXIS]) {
  479. block->direction_bits |= (1<<Y_AXIS);
  480. }
  481. if (target[Z_AXIS] < position[Z_AXIS]) {
  482. block->direction_bits |= (1<<Z_AXIS);
  483. }
  484. if (target[E_AXIS] < position[E_AXIS]) {
  485. block->direction_bits |= (1<<E_AXIS);
  486. }
  487. // Move buffer head
  488. block_buffer_head = next_buffer_head;
  489. // Update position
  490. memcpy(position, target, sizeof(target)); // position[] = target[]
  491. planner_recalculate();
  492. st_wake_up();
  493. }
  494. void plan_set_position(const float &x, const float &y, const float &z, const float &e)
  495. {
  496. position[X_AXIS] = lround(x*axis_steps_per_unit[X_AXIS]);
  497. position[Y_AXIS] = lround(y*axis_steps_per_unit[Y_AXIS]);
  498. position[Z_AXIS] = lround(z*axis_steps_per_unit[Z_AXIS]);
  499. position[E_AXIS] = lround(e*axis_steps_per_unit[E_AXIS]);
  500. }