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

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