Prusa3d Marlin fork
planner.h
1 /*
2  planner.h - buffers movement commands and manages the acceleration profile plan
3  Part of Grbl
4 
5  Copyright (c) 2009-2011 Simen Svale Skogsrud
6 
7  Grbl is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  Grbl is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with Grbl. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 // This module is to be considered a sub-module of stepper.c. Please don't include
22 // this file from any other module.
23 
24 #ifndef planner_h
25 #define planner_h
26 
27 #include "Marlin.h"
28 
29 #ifdef ENABLE_AUTO_BED_LEVELING
30 #include "vector_3.h"
31 #endif // ENABLE_AUTO_BED_LEVELING
32 
33 enum BlockFlag {
34  // Planner flag to recalculate trapezoids on entry junction.
35  // This flag has an optimization purpose only.
36  BLOCK_FLAG_RECALCULATE = 1,
37  // Planner flag for nominal speed always reached. That means, the segment is long enough, that the nominal speed
38  // may be reached if accelerating from a safe speed (in the regard of jerking from zero speed).
39  BLOCK_FLAG_NOMINAL_LENGTH = 2,
40  // If set, the machine will start from a halt at the start of this block,
41  // respecting the maximum allowed jerk.
42  BLOCK_FLAG_START_FROM_FULL_HALT = 4,
43  // If set, the stepper interrupt expects, that the number of steps to tick will be lower
44  // than 32767, therefore the DDA algorithm may run with 16bit resolution only.
45  // In addition, the stepper routine will not do any end stop checking for higher performance.
46  BLOCK_FLAG_DDA_LOWRES = 8,
47  // Block starts with Zeroed E counter
48  BLOCK_FLAG_E_RESET = 16,
49 };
50 
52 {
53  int32_t wide;
54  struct {
55  int16_t lo;
56  int16_t hi;
57  };
58 };
59 
61 {
62  uint32_t wide;
63  struct {
64  uint16_t lo;
65  uint16_t hi;
66  };
67 };
68 
69 // This struct is used when buffering the setup for each linear movement "nominal" values are as specified in
70 // the source g-code and may never actually be reached if acceleration management is active.
71 typedef struct {
72  // Fields used by the bresenham algorithm for tracing the line
73  // steps_x.y,z, step_event_count, acceleration_rate, direction_bits and active_extruder are set by plan_buffer_line().
74  dda_isteps_t steps[NUM_AXIS]; // Step count along each axis
75  dda_usteps_t step_event_count; // The number of step events required to complete this block
76  uint32_t acceleration_rate; // The acceleration rate used for acceleration calculation
77  unsigned char direction_bits; // The direction bit set for this block (refers to *_DIRECTION_BIT in config.h)
78  // accelerate_until and decelerate_after are set by calculate_trapezoid_for_block() and they need to be synchronized with the stepper interrupt controller.
79  uint32_t accelerate_until; // The index of the step event on which to stop acceleration
80  uint32_t decelerate_after; // The index of the step event on which to start decelerating
81 
82  // Fields used by the motion planner to manage acceleration
83 // float speed_x, speed_y, speed_z, speed_e; // Nominal mm/sec for each axis
84  // The nominal speed for this block in mm/sec.
85  // This speed may or may not be reached due to the jerk and acceleration limits.
86  float nominal_speed;
87  // Entry speed at previous-current junction in mm/sec, respecting the acceleration and jerk limits.
88  // The entry speed limit of the current block equals the exit speed of the preceding block.
89  float entry_speed;
90  // Maximum allowable junction entry speed in mm/sec. This value is also a maximum exit speed of the previous block.
91  float max_entry_speed;
92  // The total travel of this block in mm
93  float millimeters;
94  // acceleration mm/sec^2
95  float acceleration;
96 
97  // Bit flags defined by the BlockFlag enum.
98  uint8_t flag;
99 
100  // Settings for the trapezoid generator (runs inside an interrupt handler).
101  // Changing the following values in the planner needs to be synchronized with the interrupt handler by disabling the interrupts.
102  uint32_t nominal_rate; // The nominal step rate for this block in step_events/sec
103  uint32_t initial_rate; // The jerk-adjusted step rate at start of block
104  uint32_t final_rate; // The minimal rate at exit
105  uint32_t acceleration_steps_per_s2; // acceleration steps/sec^2
106  uint8_t fan_speed; // Print fan speed, ranges from 0 to 255
107  volatile char busy;
108 
109 
110  // Pre-calculated division for the calculate_trapezoid_for_block() routine to run faster.
111  float speed_factor;
112 
113 #ifdef LIN_ADVANCE
114  bool use_advance_lead; // Whether the current block uses LA
115  uint16_t advance_rate, // Step-rate for extruder speed
116  max_adv_steps, // max. advance steps to get cruising speed pressure (not always nominal_speed!)
117  final_adv_steps; // advance steps due to exit speed
118  uint8_t advance_step_loops; // Number of stepper ticks for each advance isr
119  float adv_comp; // Precomputed E compression factor
120 #endif
121 
122  // Save/recovery state data
123  float gcode_start_position[NUM_AXIS]; // Start (abs mm) of the original Gcode instruction
124  uint16_t segment_idx; // The index of the for loop that generates segments
125  uint16_t gcode_feedrate; // Default and/or move feedrate
126  uint16_t sdlen; // Length of the Gcode instruction
127 } block_t;
128 
129 #ifdef LIN_ADVANCE
130 extern float extruder_advance_K; // Linear-advance K factor
131 #endif
132 
133 #ifdef ENABLE_AUTO_BED_LEVELING
134 // this holds the required transform to compensate for bed level
135 extern matrix_3x3 plan_bed_level_matrix;
136 #endif // #ifdef ENABLE_AUTO_BED_LEVELING
137 
138 // Initialize the motion plan subsystem
139 void plan_init();
140 
141 // Add a new linear movement to the buffer. x, y and z is the signed, absolute target position in
142 // millimaters. Feed rate specifies the speed of the motion.
143 
144 #ifdef ENABLE_AUTO_BED_LEVELING
145 void plan_buffer_line(float x, float y, float z, const float &e, float feed_rate);
146 
147 // Get the position applying the bed level matrix if enabled
148 vector_3 plan_get_position();
149 #else
150 
155 void plan_buffer_line_curposXYZE(float feed_rate);
156 
157 void plan_buffer_line_destinationXYZE(float feed_rate);
158 
159 void plan_set_position_curposXYZE();
160 
161 void plan_buffer_line(float x, float y, float z, const float &e, float feed_rate, const float* gcode_start_position = NULL, uint16_t segment_idx = 0);
162 //void plan_buffer_line(const float &x, const float &y, const float &z, const float &e, float feed_rate, const uint8_t &extruder);
163 #endif // ENABLE_AUTO_BED_LEVELING
164 
165 // Set position. Used for G92 instructions.
166 //#ifdef ENABLE_AUTO_BED_LEVELING
167 void plan_set_position(float x, float y, float z, const float &e);
168 //#else
169 //void plan_set_position(const float &x, const float &y, const float &z, const float &e);
170 //#endif // ENABLE_AUTO_BED_LEVELING
171 
172 void plan_set_z_position(const float &z);
173 void plan_set_e_position(const float &e);
174 
175 // Reset the E position to zero at the start of the next segment
176 void plan_reset_next_e();
177 
178 inline void set_current_to_destination() { memcpy(current_position, destination, sizeof(current_position)); }
179 inline void set_destination_to_current() { memcpy(destination, current_position, sizeof(destination)); }
180 
181 extern bool e_active();
182 
183 void check_axes_activity();
184 
185 // Use M203 to override by software
186 extern float* max_feedrate;
187 
188 
189 // Use M201 to override by software
190 extern uint32_t* max_acceleration_mm_per_s2;
191 extern uint32_t max_acceleration_steps_per_s2[NUM_AXIS];
192 
193 extern long position[NUM_AXIS];
194 
195 
196 #ifdef AUTOTEMP
197  extern bool autotemp_enabled;
198  extern float autotemp_max;
199  extern float autotemp_min;
200  extern float autotemp_factor;
201 #endif
202 
203 
204 // Check for BLOCK_BUFFER_SIZE requirements
205 static_assert(!(BLOCK_BUFFER_SIZE & (BLOCK_BUFFER_SIZE - 1)),
206  "BLOCK_BUFFER_SIZE must be a power of two");
207 static_assert(BLOCK_BUFFER_SIZE <= (UINT8_MAX>>1),
208  "BLOCK_BUFFER_SIZE too large for uint8_t");
209 
210 extern block_t block_buffer[BLOCK_BUFFER_SIZE]; // A ring buffer for motion instfructions
211 // Index of the next block to be pushed into the planner queue.
212 extern volatile uint8_t block_buffer_head;
213 // Index of the first block in the planner queue.
214 // This is the block, which is being currently processed by the stepper routine,
215 // or which is first to be processed by the stepper routine.
216 extern volatile uint8_t block_buffer_tail;
217 // Called when the current block is no longer needed. Discards the block and makes the memory
218 // available for new blocks.
219 FORCE_INLINE void plan_discard_current_block()
220 {
221  if (block_buffer_head != block_buffer_tail) {
222  block_buffer_tail = (block_buffer_tail + 1) & (BLOCK_BUFFER_SIZE - 1);
223  }
224 }
225 
226 // Gets the current block. This is the block to be exectuted by the stepper routine.
227 // Mark this block as busy, so its velocities and acceperations will be no more recalculated
228 // by the planner routine.
229 // Returns NULL if buffer empty
230 FORCE_INLINE block_t *plan_get_current_block()
231 {
232  if (block_buffer_head == block_buffer_tail) {
233  return(NULL);
234  }
235  block_t *block = &block_buffer[block_buffer_tail];
236  block->busy = true;
237  return(block);
238 }
239 
240 // Returns true if the buffer has a queued block, false otherwise
241 FORCE_INLINE bool blocks_queued() {
242  return (block_buffer_head != block_buffer_tail);
243 }
244 
245 //return the nr of buffered moves
246 FORCE_INLINE uint8_t moves_planned() {
247  return (block_buffer_head + BLOCK_BUFFER_SIZE - block_buffer_tail) & (BLOCK_BUFFER_SIZE - 1);
248 }
249 
250 FORCE_INLINE bool planner_queue_full() {
251  uint8_t next_block_index = block_buffer_head;
252  if (++ next_block_index == BLOCK_BUFFER_SIZE)
253  next_block_index = 0;
254  return block_buffer_tail == next_block_index;
255 }
256 
257 // Reset machine position from stepper counters
258 extern void planner_reset_position();
259 
260 // Abort the stepper routine, clean up the block queue,
261 // wait for the steppers to stop,
262 // update planner's current position and the current_position of the front end.
263 extern void planner_abort_hard();
264 extern bool planner_aborted;
265 
266 #ifdef PREVENT_DANGEROUS_EXTRUDE
267 extern int extrude_min_temp;
268 void set_extrude_min_temp(int temp);
269 #endif
270 
271 void reset_acceleration_rates();
272 #endif
273 
274 void update_mode_profile();
275 
276 // #define PLANNER_DIAGNOSTICS
277 #ifdef PLANNER_DIAGNOSTICS
278 // Diagnostic functions to display planner buffer underflow on the display.
279 extern uint8_t planner_queue_min();
280 // Diagnostic function: Reset the minimum planner segments.
281 extern void planner_queue_min_reset();
282 #endif /* PLANNER_DIAGNOSTICS */
283 
284 extern void planner_add_sd_length(uint16_t sdlen);
285 
286 extern uint16_t planner_calc_sd_length();
Definition: planner.h:71
Definition: vector_3.h:46
Definition: vector_3.h:26
Definition: planner.h:52
Definition: planner.h:61