grblHAL core  20250514
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-2025 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,
44  };
46 
47 typedef union {
48  uint32_t value[N_AXIS];
49  struct {
50  uint32_t x;
51  uint32_t y;
52  uint32_t z;
53 #ifdef A_AXIS
54  uint32_t a;
55 #endif
56 #ifdef B_AXIS
57  uint32_t b;
58 #endif
59 #ifdef C_AXIS
60  uint32_t c;
61 #endif
62 #ifdef U_AXIS
63  uint32_t u;
64 #endif
65 #ifdef V_AXIS
66  uint32_t v;
67 #endif
68  };
69 } steps_t;
70 
71 // This struct stores a linear movement of a g-code block motion with its critical "nominal" values
72 // are as specified in the source g-code.
73 typedef struct plan_block {
74  // Fields used by the bresenham algorithm for tracing the line
75  // NOTE: Used by stepper algorithm to execute the block correctly. Do not alter these values.
76  steps_t steps; // Step count along each axis
77  uint32_t step_event_count; // The maximum step axis count and number of steps required to complete this block.
78  axes_signals_t direction; // The direction bit set for this block (refers to *_DIRECTION_PIN in config.h)
79 
81  // Block condition data to ensure correct execution depending on states and overrides.
82  gc_override_flags_t overrides; // Block bitfield variable for overrides
83  planner_cond_t condition; // Block bitfield variable defining block run conditions. Copied from pl_line_data.
84  int32_t line_number; // Block line number for real-time reporting. Copied from pl_line_data.
85 
86  // Fields used by the motion planner to manage acceleration. Some of these values may be updated
87  // by the stepper module during execution of special motion cases for replanning purposes.
88  float entry_speed_sqr; // The current planned entry speed at block junction in (mm/min)^2
89  float max_entry_speed_sqr; // Maximum allowable entry speed based on the minimum of junction limit and
90  // neighboring nominal speeds with overrides in (mm/min)^2
91  float acceleration; // Effective acceleration over plannerblock calculated from trapezoidal movement plan. Does not change in trapezoidal mode.
92 #if ENABLE_JERK_ACCELERATION
93  float max_acceleration; // Axis-limit adjusted line acceleration in (mm/min^2). Does not change.
94  float jerk; // Axis-limit adjusted jerk value in (mm/min^3). Does not change.
95 #endif
96  float millimeters; // The remaining distance for this block to be executed in (mm).
97  // NOTE: This value may be altered by stepper algorithm during execution.
98 
99  // Stored rate limiting data used by planner when changes occur.
100  float max_junction_speed_sqr; // Junction entry speed limit based on direction vectors in (mm/min)^2
101  float rapid_rate; // Axis-limit adjusted maximum rate for this block direction in (mm/min)
102  float programmed_rate; // Programmed rate of this block (mm/min).
103 #ifdef KINEMATICS_API
104  float rate_multiplier; // Rate multiplier of this block.
105 #endif
106 #if ENABLE_ACCELERATION_PROFILES
107  float acceleration_factor; // Stores the currently used acceleration factor.
108 #endif
109  // Stored spindle speed data used by spindle overrides and resuming methods.
110  spindle_t spindle; // Block spindle parameters. Copied from pl_line_data.
111 
112  char *message; // Message to be displayed when block is executed.
114  struct plan_block *prev, *next; // Linked list pointers, DO NOT MOVE - these MUST be the last elements in the struct!
116 
117 
118 // Planner data prototype. Must be used when passing new motions to the planner.
119 typedef struct {
120  float feed_rate; // Desired feed rate for line motion. Value is ignored, if rapid motion.
121 #ifdef KINEMATICS_API
122  float rate_multiplier; // Feed rate multiplier.
123 #endif
124 #if ENABLE_ACCELERATION_PROFILES
125  float acceleration_factor; // Stores the currently used acceleration factor.
126 #endif
127 #if ENABLE_PATH_BLENDING
128  float path_tolerance;
129  float cam_tolerance;
130 #endif
131  spindle_t spindle; // Desired spindle parameters, such as RPM, through line motion.
132  planner_cond_t condition; // Bitfield variable to indicate planner conditions. See defines above.
133  gc_override_flags_t overrides; // Block bitfield variable for overrides
135  int32_t line_number; // Desired line number to report when executing.
136 // void *parameters; // TODO: pointer to extra parameters, for canned cycles and threading?
137  char *message; // Message to be displayed when block is executed.
140 
141 
142 // Define planner variables
143 typedef struct {
144  int32_t position[N_AXIS]; // The planner position of the tool in absolute steps. Kept separate
145  // from g-code position for movements requiring multiple line motions,
146  // i.e. arcs, canned cycles, and backlash compensation.
147  float previous_unit_vec[N_AXIS]; // Unit vector of previous path line segment
148  float previous_nominal_speed; // Nominal speed of previous path line segment
149 } planner_t;
150 
151 // Initialize and reset the motion plan subsystem
152 bool plan_reset (void); // Reset all
153 
154 uint_fast16_t plan_get_buffer_size (void);
155 
156 // Add a new linear movement to the buffer. target[N_AXIS] is the signed, absolute target position
157 // in millimeters. Feed rate specifies the speed of the motion. If feed rate is inverted, the feed
158 // rate is taken to mean "frequency" and would complete the operation in 1/feed_rate minutes.
159 bool plan_buffer_line (float *target, plan_line_data_t *pl_data);
160 
161 // Called when the current block is no longer needed. Discards the block and makes the memory
162 // available for new blocks.
163 void plan_discard_current_block (void);
164 
165 // Gets the planner block for the special system motion cases. (Parking/Homing)
167 
168 // Gets the current block. Returns NULL if buffer empty
170 
171 // Called by step segment buffer when computing executing block velocity profile.
173 
174 // Called by main program during planner calculations and step segment buffer during initialization.
176 
177 // Get the planner position vectors.
178 float *plan_get_position (void);
179 
180 // Reset the planner position vector (in steps)
181 void plan_sync_position (void);
182 
183 // Reinitialize plan with a partially completed block
184 void plan_cycle_reinitialize (void);
185 
186 // Returns the number of available blocks in the planner buffer.
187 uint_fast16_t plan_get_block_buffer_available (void);
188 
189 // Returns the status of the block ring buffer. True, if buffer is full.
190 bool plan_check_full_buffer (void);
191 
192 void plan_feed_override (override_t feed_override, override_t rapid_override);
193 
194 void plan_data_init (plan_line_data_t *plan_data);
195 
196 #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:257
void plan_feed_override(override_t feed_override, override_t rapid_override)
Definition: planner.c:711
void plan_cycle_reinitialize(void)
Definition: planner.c:683
void plan_sync_position(void)
Definition: planner.c:663
bool plan_reset(void)
Definition: planner.c:221
float plan_compute_profile_nominal_speed(plan_block_t *block)
Definition: planner.c:307
uint_fast16_t plan_get_block_buffer_available(void)
Definition: planner.c:673
uint_fast16_t plan_get_buffer_size(void)
Definition: planner.c:216
float plan_get_exec_block_exit_speed_sqr(void)
Definition: planner.c:291
bool plan_check_full_buffer(void)
Definition: planner.c:299
struct plan_block plan_block_t
void plan_discard_current_block(void)
Definition: planner.c:265
plan_block_t * plan_get_system_motion_block(void)
Definition: planner.c:278
bool plan_buffer_line(float *target, plan_line_data_t *pl_data)
Definition: planner.c:395
float * plan_get_position(void)
Definition: planner.c:647
void plan_data_init(plan_line_data_t *plan_data)
Definition: planner.c:744
plan_block_t * plan_get_current_block(void)
Definition: planner.c:285
Data for M62, M63 and M67 commands when executed synchronized with motion.
Definition: gcode.h:279
Definition: planner.h:73
char * message
Definition: planner.h:112
float rapid_rate
Definition: planner.h:101
float max_entry_speed_sqr
Definition: planner.h:89
gc_override_flags_t overrides
Definition: planner.h:82
struct plan_block * next
Definition: planner.h:114
spindle_t spindle
Definition: planner.h:110
int32_t line_number
Definition: planner.h:84
planner_cond_t condition
Definition: planner.h:83
uint32_t step_event_count
Definition: planner.h:77
float acceleration
Definition: planner.h:91
struct plan_block * prev
Definition: planner.h:114
float entry_speed_sqr
Definition: planner.h:88
steps_t steps
Definition: planner.h:76
float max_junction_speed_sqr
Definition: planner.h:100
axes_signals_t direction
Definition: planner.h:78
float programmed_rate
Definition: planner.h:102
offset_id_t offset_id
Definition: planner.h:80
float millimeters
Definition: planner.h:96
output_command_t * output_commands
Definition: planner.h:113
Definition: planner.h:119
char * message
Definition: planner.h:137
gc_override_flags_t overrides
Definition: planner.h:133
spindle_t spindle
Definition: planner.h:131
int32_t line_number
Definition: planner.h:135
planner_cond_t condition
Definition: planner.h:132
float feed_rate
Definition: planner.h:120
offset_id_t offset_id
Definition: planner.h:134
output_command_t * output_commands
Definition: planner.h:138
Definition: planner.h:143
float previous_nominal_speed
Definition: planner.h:148
Definition: gcode.h:518
Definition: nuts_bolts.h:125
Definition: coolant_control.h:27
Override flags.
Definition: gcode.h:338
Definition: planner.h:27
uint16_t is_laser_ppi_mode
Definition: planner.h:38
uint16_t backlash_motion
Definition: planner.h:33
uint16_t probing_toolsetter
Definition: planner.h:41
uint16_t system_motion
Definition: planner.h:31
uint16_t unassigned
Definition: planner.h:42
uint16_t target_validated
Definition: planner.h:40
uint16_t jog_motion
Definition: planner.h:32
coolant_state_t coolant
Definition: planner.h:43
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
Definition: planner.h:47
uint32_t y
Definition: planner.h:51
uint32_t x
Definition: planner.h:50
uint32_t z
Definition: planner.h:52