grblHAL core  20250104
planner.h
Go to the documentation of this file.
1 /*
2  planner.h - buffers movement commands and manages the acceleration profile plan
3 
4  Part of grblHAL
5 
6  Copyright (c) 2019-2024 Terje Io
7  Copyright (c) 2011-2016 Sungeun K. Jeon for Gnea Research LLC
8  Copyright (c) 2009-2011 Simen Svale Skogsrud
9 
10  grblHAL is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  grblHAL is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with grblHAL. If not, see <http://www.gnu.org/licenses/>.
22 */
23 
24 #ifndef _PLANNER_H_
25 #define _PLANNER_H_
26 
27 typedef union {
28  uint32_t value;
29  struct {
30  uint16_t rapid_motion :1,
43  };
45 
46 // This struct stores a linear movement of a g-code block motion with its critical "nominal" values
47 // are as specified in the source g-code.
48 typedef struct plan_block {
49  // Fields used by the bresenham algorithm for tracing the line
50  // NOTE: Used by stepper algorithm to execute the block correctly. Do not alter these values.
51  uint32_t steps[N_AXIS]; // Step count along each axis
52  uint32_t step_event_count; // The maximum step axis count and number of steps required to complete this block.
53  axes_signals_t direction_bits; // The direction bit set for this block (refers to *_DIRECTION_PIN in config.h)
54 
56  // Block condition data to ensure correct execution depending on states and overrides.
57  gc_override_flags_t overrides; // Block bitfield variable for overrides
58  planner_cond_t condition; // Block bitfield variable defining block run conditions. Copied from pl_line_data.
59  int32_t line_number; // Block line number for real-time reporting. Copied from pl_line_data.
60 
61  // Fields used by the motion planner to manage acceleration. Some of these values may be updated
62  // by the stepper module during execution of special motion cases for replanning purposes.
63  float entry_speed_sqr; // The current planned entry speed at block junction in (mm/min)^2
64  float max_entry_speed_sqr; // Maximum allowable entry speed based on the minimum of junction limit and
65  // neighboring nominal speeds with overrides in (mm/min)^2
66  float acceleration; // Effective acceleration over plannerblock calculated from trapezoidal movement plan. Does not change in trapezoidal mode.
67 #if ENABLE_JERK_ACCELERATION
68  float max_acceleration; // Axis-limit adjusted line acceleration in (mm/min^2). Does not change.
69  float jerk; // Axis-limit adjusted jerk value in (mm/min^3). Does not change.
70 #endif
71  float millimeters; // The remaining distance for this block to be executed in (mm).
72  // NOTE: This value may be altered by stepper algorithm during execution.
73 
74  // Stored rate limiting data used by planner when changes occur.
75  float max_junction_speed_sqr; // Junction entry speed limit based on direction vectors in (mm/min)^2
76  float rapid_rate; // Axis-limit adjusted maximum rate for this block direction in (mm/min)
77  float programmed_rate; // Programmed rate of this block (mm/min).
78 #ifdef KINEMATICS_API
79  float rate_multiplier; // Rate multiplier of this block.
80 #endif
81 #if ENABLE_ACCELERATION_PROFILES
82  float acceleration_factor; // Stores the currently used acceleration factor.
83 #endif
84  // Stored spindle speed data used by spindle overrides and resuming methods.
85  spindle_t spindle; // Block spindle parameters. Copied from pl_line_data.
86 
87  char *message; // Message to be displayed when block is executed.
89  struct plan_block *prev, *next; // Linked list pointers, DO NOT MOVE - these MUST be the last elements in the struct!
91 
92 
93 // Planner data prototype. Must be used when passing new motions to the planner.
94 typedef struct {
95  float feed_rate; // Desired feed rate for line motion. Value is ignored, if rapid motion.
96 #ifdef KINEMATICS_API
97  float rate_multiplier; // Feed rate multiplier.
98 #endif
99 #if ENABLE_ACCELERATION_PROFILES
100  float acceleration_factor; // Stores the currently used acceleration factor.
101 #endif
102 #if ENABLE_PATH_BLENDING
103  float path_tolerance;
104  float cam_tolerance;
105 #endif
106  spindle_t spindle; // Desired spindle parameters, such as RPM, through line motion.
107  planner_cond_t condition; // Bitfield variable to indicate planner conditions. See defines above.
108  gc_override_flags_t overrides; // Block bitfield variable for overrides
110  int32_t line_number; // Desired line number to report when executing.
111 // void *parameters; // TODO: pointer to extra parameters, for canned cycles and threading?
112  char *message; // Message to be displayed when block is executed.
115 
116 
117 // Define planner variables
118 typedef struct {
119  int32_t position[N_AXIS]; // The planner position of the tool in absolute steps. Kept separate
120  // from g-code position for movements requiring multiple line motions,
121  // i.e. arcs, canned cycles, and backlash compensation.
122  float previous_unit_vec[N_AXIS]; // Unit vector of previous path line segment
123  float previous_nominal_speed; // Nominal speed of previous path line segment
124 } planner_t;
125 
126 // Initialize and reset the motion plan subsystem
127 bool plan_reset (void); // Reset all
128 
129 uint_fast16_t plan_get_buffer_size (void);
130 
131 // Add a new linear movement to the buffer. target[N_AXIS] is the signed, absolute target position
132 // in millimeters. Feed rate specifies the speed of the motion. If feed rate is inverted, the feed
133 // rate is taken to mean "frequency" and would complete the operation in 1/feed_rate minutes.
134 bool plan_buffer_line (float *target, plan_line_data_t *pl_data);
135 
136 // Called when the current block is no longer needed. Discards the block and makes the memory
137 // available for new blocks.
138 void plan_discard_current_block (void);
139 
140 // Gets the planner block for the special system motion cases. (Parking/Homing)
142 
143 // Gets the current block. Returns NULL if buffer empty
145 
146 // Called by step segment buffer when computing executing block velocity profile.
148 
149 // Called by main program during planner calculations and step segment buffer during initialization.
151 
152 // Get the planner position vectors.
153 float *plan_get_position (void);
154 
155 // Reset the planner position vector (in steps)
156 void plan_sync_position (void);
157 
158 // Reinitialize plan with a partially completed block
159 void plan_cycle_reinitialize (void);
160 
161 // Returns the number of available blocks in the planner buffer.
162 uint_fast16_t plan_get_block_buffer_available (void);
163 
164 // Returns the status of the block ring buffer. True, if buffer is full.
165 bool plan_check_full_buffer (void);
166 
167 void plan_feed_override (override_t feed_override, override_t rapid_override);
168 
169 void plan_data_init (plan_line_data_t *plan_data);
170 
171 #endif
#define N_AXIS
Defines number of axes supported - minimum 3, maximum 8. If more than 3 axes are configured a complia...
Definition: config.h:42
int8_t offset_id_t
Definition: gcode.h:36
uint_fast16_t override_t
Definition: grbl.h:236
void plan_feed_override(override_t feed_override, override_t rapid_override)
Definition: planner.c:702
void plan_cycle_reinitialize(void)
Definition: planner.c:674
void plan_sync_position(void)
Definition: planner.c:654
bool plan_reset(void)
Definition: planner.c:225
float plan_compute_profile_nominal_speed(plan_block_t *block)
Definition: planner.c:311
uint_fast16_t plan_get_block_buffer_available(void)
Definition: planner.c:664
uint_fast16_t plan_get_buffer_size(void)
Definition: planner.c:220
float plan_get_exec_block_exit_speed_sqr(void)
Definition: planner.c:295
bool plan_check_full_buffer(void)
Definition: planner.c:303
struct plan_block plan_block_t
void plan_discard_current_block(void)
Definition: planner.c:269
plan_block_t * plan_get_system_motion_block(void)
Definition: planner.c:282
bool plan_buffer_line(float *target, plan_line_data_t *pl_data)
Definition: planner.c:399
float * plan_get_position(void)
Definition: planner.c:638
void plan_data_init(plan_line_data_t *plan_data)
Definition: planner.c:735
plan_block_t * plan_get_current_block(void)
Definition: planner.c:289
Data for M62, M63 and M67 commands when executed synchronized with motion.
Definition: gcode.h:275
Definition: planner.h:48
char * message
Definition: planner.h:87
float rapid_rate
Definition: planner.h:76
float max_entry_speed_sqr
Definition: planner.h:64
axes_signals_t direction_bits
Definition: planner.h:53
gc_override_flags_t overrides
Definition: planner.h:57
struct plan_block * next
Definition: planner.h:89
spindle_t spindle
Definition: planner.h:85
int32_t line_number
Definition: planner.h:59
planner_cond_t condition
Definition: planner.h:58
uint32_t step_event_count
Definition: planner.h:52
float acceleration
Definition: planner.h:66
struct plan_block * prev
Definition: planner.h:89
float entry_speed_sqr
Definition: planner.h:63
uint32_t steps[N_AXIS]
Definition: planner.h:51
float max_junction_speed_sqr
Definition: planner.h:75
float programmed_rate
Definition: planner.h:77
offset_id_t offset_id
Definition: planner.h:55
float millimeters
Definition: planner.h:71
output_command_t * output_commands
Definition: planner.h:88
Definition: planner.h:94
char * message
Definition: planner.h:112
gc_override_flags_t overrides
Definition: planner.h:108
spindle_t spindle
Definition: planner.h:106
int32_t line_number
Definition: planner.h:110
planner_cond_t condition
Definition: planner.h:107
float feed_rate
Definition: planner.h:95
offset_id_t offset_id
Definition: planner.h:109
output_command_t * output_commands
Definition: planner.h:113
Definition: planner.h:118
float previous_nominal_speed
Definition: planner.h:123
Definition: gcode.h:515
Definition: nuts_bolts.h:125
Definition: coolant_control.h:27
Override flags.
Definition: gcode.h:335
Definition: planner.h:27
uint16_t is_laser_ppi_mode
Definition: planner.h:38
uint16_t backlash_motion
Definition: planner.h:33
uint16_t system_motion
Definition: planner.h:31
uint16_t unassigned
Definition: planner.h:41
uint16_t target_validated
Definition: planner.h:40
uint16_t jog_motion
Definition: planner.h:32
coolant_state_t coolant
Definition: planner.h:42
uint16_t units_per_rev
Definition: planner.h:36
uint16_t target_valid
Definition: planner.h:39
uint16_t inverse_time
Definition: planner.h:35
uint16_t is_rpm_rate_adjusted
Definition: planner.h:37
uint16_t no_feed_override
Definition: planner.h:34
uint32_t value
Definition: planner.h:28
uint16_t rapid_motion
Definition: planner.h:30