grblHAL core  20241107
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; // Axis-limit adjusted line acceleration in (mm/min^2). Does not change.
67  float millimeters; // The remaining distance for this block to be executed in (mm).
68  // NOTE: This value may be altered by stepper algorithm during execution.
69 
70  // Stored rate limiting data used by planner when changes occur.
71  float max_junction_speed_sqr; // Junction entry speed limit based on direction vectors in (mm/min)^2
72  float rapid_rate; // Axis-limit adjusted maximum rate for this block direction in (mm/min)
73  float programmed_rate; // Programmed rate of this block (mm/min).
74 #ifdef KINEMATICS_API
75  float rate_multiplier; // Rate multiplier of this block.
76 #endif
77  // Stored spindle speed data used by spindle overrides and resuming methods.
78  spindle_t spindle; // Block spindle parameters. Copied from pl_line_data.
79 
80  char *message; // Message to be displayed when block is executed.
82  struct plan_block *prev, *next; // Linked list pointers, DO NOT MOVE - these MUST be the last elements in the struct!
84 
85 
86 // Planner data prototype. Must be used when passing new motions to the planner.
87 typedef struct {
88  float feed_rate; // Desired feed rate for line motion. Value is ignored, if rapid motion.
89 #ifdef KINEMATICS_API
90  float rate_multiplier; // Feed rate multiplier.
91 #endif
92 #if ENABLE_PATH_BLENDING
93  float path_tolerance;
94  float cam_tolerance;
95 #endif
96  spindle_t spindle; // Desired spindle parameters, such as RPM, through line motion.
97  planner_cond_t condition; // Bitfield variable to indicate planner conditions. See defines above.
98  gc_override_flags_t overrides; // Block bitfield variable for overrides
100  int32_t line_number; // Desired line number to report when executing.
101 // void *parameters; // TODO: pointer to extra parameters, for canned cycles and threading?
102  char *message; // Message to be displayed when block is executed.
105 
106 
107 // Define planner variables
108 typedef struct {
109  int32_t position[N_AXIS]; // The planner position of the tool in absolute steps. Kept separate
110  // from g-code position for movements requiring multiple line motions,
111  // i.e. arcs, canned cycles, and backlash compensation.
112  float previous_unit_vec[N_AXIS]; // Unit vector of previous path line segment
113  float previous_nominal_speed; // Nominal speed of previous path line segment
114 } planner_t;
115 
116 // Initialize and reset the motion plan subsystem
117 bool plan_reset (void); // Reset all
118 
119 uint_fast16_t plan_get_buffer_size (void);
120 
121 // Add a new linear movement to the buffer. target[N_AXIS] is the signed, absolute target position
122 // in millimeters. Feed rate specifies the speed of the motion. If feed rate is inverted, the feed
123 // rate is taken to mean "frequency" and would complete the operation in 1/feed_rate minutes.
124 bool plan_buffer_line (float *target, plan_line_data_t *pl_data);
125 
126 // Called when the current block is no longer needed. Discards the block and makes the memory
127 // available for new blocks.
128 void plan_discard_current_block (void);
129 
130 // Gets the planner block for the special system motion cases. (Parking/Homing)
132 
133 // Gets the current block. Returns NULL if buffer empty
135 
136 // Called by step segment buffer when computing executing block velocity profile.
138 
139 // Called by main program during planner calculations and step segment buffer during initialization.
141 
142 // Get the planner position vectors.
143 float *plan_get_position (void);
144 
145 // Reset the planner position vector (in steps)
146 void plan_sync_position (void);
147 
148 // Reinitialize plan with a partially completed block
149 void plan_cycle_reinitialize (void);
150 
151 // Returns the number of available blocks in the planner buffer.
152 uint_fast16_t plan_get_block_buffer_available (void);
153 
154 // Returns the status of the block ring buffer. True, if buffer is full.
155 bool plan_check_full_buffer (void);
156 
157 void plan_feed_override (override_t feed_override, override_t rapid_override);
158 
159 void plan_data_init (plan_line_data_t *plan_data);
160 
161 #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:665
void plan_cycle_reinitialize(void)
Definition: planner.c:637
void plan_sync_position(void)
Definition: planner.c:617
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:627
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:383
float * plan_get_position(void)
Definition: planner.c:601
void plan_data_init(plan_line_data_t *plan_data)
Definition: planner.c:690
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:269
Definition: planner.h:48
char * message
Definition: planner.h:80
float rapid_rate
Definition: planner.h:72
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:82
spindle_t spindle
Definition: planner.h:78
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:82
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:71
float programmed_rate
Definition: planner.h:73
offset_id_t offset_id
Definition: planner.h:55
float millimeters
Definition: planner.h:67
output_command_t * output_commands
Definition: planner.h:81
Definition: planner.h:87
char * message
Definition: planner.h:102
gc_override_flags_t overrides
Definition: planner.h:98
spindle_t spindle
Definition: planner.h:96
int32_t line_number
Definition: planner.h:100
planner_cond_t condition
Definition: planner.h:97
float feed_rate
Definition: planner.h:88
offset_id_t offset_id
Definition: planner.h:99
output_command_t * output_commands
Definition: planner.h:103
Definition: planner.h:108
float previous_nominal_speed
Definition: planner.h:113
Definition: gcode.h:499
Definition: nuts_bolts.h:130
Definition: coolant_control.h:26
Override flags.
Definition: gcode.h:329
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