grblHAL core  20260908
system.h
Go to the documentation of this file.
1 /*
2  system.h - Header for system level commands and real-time processes
3 
4  Part of grblHAL
5 
6  Copyright (c) 2017-2026 Terje Io
7  Copyright (c) 2014-2016 Sungeun K. Jeon for Gnea Research LLC
8 
9  grblHAL is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  grblHAL is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with grblHAL. If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 #ifndef _SYSTEM_H_
24 #define _SYSTEM_H_
25 
26 #include "gcode.h"
27 #include "probe.h"
28 #include "alarms.h"
29 #include "stream.h"
30 #include "messages.h"
31 #if NGC_EXPRESSIONS_ENABLE
32 #include "vfs.h"
33 #endif
34 
46 #define EXEC_STATUS_REPORT bit(0)
47 #define EXEC_CYCLE_START bit(1)
48 #define EXEC_CYCLE_COMPLETE bit(2)
49 #define EXEC_FEED_HOLD bit(3)
50 #define EXEC_STOP bit(4)
51 #define EXEC_RESET bit(5)
52 #define EXEC_SAFETY_DOOR bit(6)
53 #define EXEC_MOTION_CANCEL bit(7)
54 #define EXEC_MOTION_CANCEL_FAST bit(7) // Permanently enabled for now
55 #define EXEC_SLEEP bit(9)
56 #define EXEC_TOOL_CHANGE bit(10)
57 #define EXEC_PID_REPORT bit(11)
58 #define EXEC_GCODE_REPORT bit(12)
59 #define EXEC_TLO_REPORT bit(13)
60 #define EXEC_RT_COMMAND bit(14)
61 #define EXEC_DOOR_CLOSED bit(15)
63 
65 
76 #define STATE_IDLE 0
77 #define STATE_ALARM bit(0)
78 #define STATE_CHECK_MODE bit(1)
79 #define STATE_HOMING bit(2)
80 #define STATE_CYCLE bit(3)
81 #define STATE_HOLD bit(4)
82 #define STATE_JOG bit(5)
83 #define STATE_SAFETY_DOOR bit(6)
84 #define STATE_SLEEP bit(7)
85 #define STATE_ESTOP bit(8)
86 #define STATE_TOOL_CHANGE bit(9)
88 
90 
92 #define STATE_HAS_SUBSTATE (STATE_CYCLE|STATE_HOLD|STATE_ESTOP|STATE_ALARM|STATE_SAFETY_DOOR)
93 
95 
102 typedef enum {
115 } __attribute__ ((__packed__)) system_state_t;
117 
118 #ifdef ARDUINO
119 
120 typedef enum {
121  Mode_Standard = 0,
122  Mode_Laser,
123  Mode_Lathe
125 
126 #else
127 
128 typedef uint8_t machine_mode_t;
129 
133  Mode_Lathe
134 };
135 
136 #endif
137 
138 typedef enum {
145 
146 typedef enum {
149  Hold_Pending = 2
151 
152 typedef uint_fast16_t rt_exec_t;
153 typedef uint_fast16_t sys_state_t;
154 
155 // Define step segment generator state flags.
156 typedef union {
157  uint8_t flags;
158  struct {
159  uint8_t end_motion :1,
164  };
166 
167 // Define spindle stop override control states.
168 typedef union {
169  uint8_t value;
170  struct {
171  uint8_t enabled :1,
176  };
178 
179 #ifdef PID_LOG
180 
181 typedef struct {
182  uint_fast16_t idx;
183  float setpoint;
184  float t_sample;
185  float target[PID_LOG];
186  float actual[PID_LOG];
187 } pid_data_t;
188 
189 #endif
190 
191 typedef struct {
197 } overrides_t;
198 
199 typedef union {
200  uint8_t flags;
201  struct {
202  uint8_t feedrate :1,
205  unused :5;
206  };
208 
209 typedef union {
210  uint16_t value;
211  struct {
212  uint16_t mpg_mode :1,
215  exit :1,
226  unused :2;
227  };
229 
230 typedef struct {
234 
235 typedef struct {
239 
241 // NOTE: probe_position and position variables may need to be declared as volatiles, if problems arise.
242 typedef struct system {
243  bool abort;
244  bool cancel;
245  bool suspend;
249  volatile bool steppers_deenergize;
254  overrides_t override;
262  volatile uint_fast16_t rt_exec_alarm;
263  int32_t var5399;
264 #ifdef PID_LOG
265  pid_data_t pid_log;
266 #endif
268 
275 
279  bool cold_start;
283  bool mpg_mode;
285  int32_t position[N_AXIS];
290 
291 typedef status_code_t (*sys_command_ptr)(sys_state_t state, char *args);
292 typedef const char *(*sys_help_ptr)(const char *command);
293 
294 typedef union {
295  uint8_t flags;
296  struct {
297  uint8_t noargs :1,
299  help_fn :1,
301  unused :4;
302  };
304 
305 typedef struct
306 {
307  const char *command;
310  union {
311  const char *str;
313  } help;
314 } sys_command_t;
315 
316 typedef struct sys_commands_str {
317  const uint8_t n_commands;
320  struct sys_commands_str *(*on_get_commands)(void);
322 
323 extern system_t sys;
324 
325 uint8_t system_n_axis (void);
326 uint8_t system_axis_mask (void);
327 uint8_t system_claim_axis (void);
328 
330 void system_execute_startup (void *data);
331 void system_flag_wco_change (void);
332 void system_convert_array_steps_to_mpos (float *position, int32_t *steps);
333 bool system_xy_at_fixture (coord_system_id_t id, float tolerance);
334 void system_raise_alarm (alarm_code_t alarm);
335 void system_init_switches (void);
336 void system_command_help (void);
337 void system_output_help (const sys_command_t *commands, uint32_t num_commands);
339 
340 void system_clear_tlo_reference (axes_signals_t homing_cycle);
341 
342 // Special handlers for setting and clearing grblHAL's real-time execution flags.
343 #define system_set_exec_state_flag(mask) hal.set_bits_atomic(&sys.rt_exec_state, (mask))
344 #define system_clear_exec_state_flag(mask) hal.clear_bits_atomic(&sys.rt_exec_state, (mask))
345 #define system_clear_exec_states() hal.set_value_atomic(&sys.rt_exec_state, 0)
346 #define system_set_exec_alarm(code) hal.set_value_atomic(&sys.rt_exec_alarm, (uint_fast16_t)(code))
347 #define system_clear_exec_alarm() hal.set_value_atomic(&sys.rt_exec_alarm, 0)
348 
350 
351 #endif
alarm_code_t
Definition: alarms.h:29
#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
status_code_t
Definition: errors.h:31
coord_system_id_t
Definition: gcode.h:186
uint_fast16_t override_t
Definition: grbl.h:263
probing_state_t
Definition: probe.h:33
void(* stream_write_ptr)(const char *s)
Pointer to function for writing a null terminated string to the output stream.
Definition: stream.h:225
Definition: system.h:191
override_t rapid_rate
Rapids override value in percent.
Definition: system.h:193
override_t spindle_rpm
NOTE: Not used by the core, it maintain per spindle override in spindle_param_t
Definition: system.h:194
gc_override_flags_t control
Tracks override control states.
Definition: system.h:196
override_t feed_rate
Feed rate override value in percent.
Definition: system.h:192
spindle_stop_t spindle_stop
Tracks spindle stop override states.
Definition: system.h:195
Definition: system.h:230
limit_signals_t limits
Definition: system.h:232
control_signals_t control
Definition: system.h:231
Definition: system.h:306
sys_command_flags_t flags
Definition: system.h:309
sys_command_ptr execute
Definition: system.h:308
const char * command
Definition: system.h:307
sys_help_ptr fn
Definition: system.h:312
const char * str
Definition: system.h:311
Definition: system.h:316
const uint8_t n_commands
Definition: system.h:317
struct sys_commands_str * next
Definition: system.h:319
const sys_command_t * commands
Definition: system.h:318
Global system variables struct.
Definition: system.h:242
axes_signals_t tlo_reference_set
Axes with tool length reference offset set.
Definition: system.h:272
axes_signals_t homed
Indicates which axes has been homed.
Definition: system.h:269
float home_position[N_AXIS]
Home position for homed axes.
Definition: system.h:270
int32_t probe_position[N_AXIS]
Last probe position in machine coordinates and steps.
Definition: system.h:259
bool steppers_enabled
Set to true when all steppers are enabled.
Definition: system.h:282
int32_t var5399
Last result from M66 - wait on input.
Definition: system.h:263
parking_state_t parking_state
Tracks parking state.
Definition: system.h:256
system_override_delay_t override_delay
Flags for delayed overrides.
Definition: system.h:255
volatile probing_state_t probing_state
Probing state value. Used to coordinate the probing cycle with stepper ISR.
Definition: system.h:260
bool blocking_event
Set when a blocking event that requires reset to clear is active.
Definition: system.h:248
bool position_lost
Set when mc_reset is called when machine is moving.
Definition: system.h:246
bool reset_pending
Set when reset processing is underway.
Definition: system.h:247
bool ioinit_pending
Definition: system.h:280
volatile bool steppers_deenergize
Set to true to deenergize stepperes.
Definition: system.h:249
hold_state_t holding_state
Tracks holding state.
Definition: system.h:257
axes_signals_t hard_limits
temporary?, will be removed when available in settings.
Definition: system.h:286
alarm_code_t alarm_pending
Delayed alarm, currently used for probe protection.
Definition: system.h:278
axes_signals_t homing_axis_lock
Locks axes when limits engage. Used as an axis motion mask in the stepper ISR.
Definition: system.h:252
bool suspend
System suspend state flag.
Definition: system.h:245
volatile rt_exec_t rt_exec_state
Realtime executor bitflag variable for state management. See EXEC bitmasks.
Definition: system.h:261
bool abort
System abort flag. Forces exit back to main loop for reset.
Definition: system.h:243
bool driver_started
Set to true when driver initialization is completed.
Definition: system.h:281
axes_signals_t homing
Axes with homing enabled.
Definition: system.h:253
int32_t tlo_reference[N_AXIS]
Tool length reference offset.
Definition: system.h:273
int32_t position[N_AXIS]
Real-time machine (aka home) position vector in steps.
Definition: system.h:285
bool cold_start
Set to true on boot, is false on subsequent soft resets.
Definition: system.h:279
volatile uint_fast16_t rt_exec_alarm
Realtime executor bitflag variable for setting various alarms.
Definition: system.h:262
bool mpg_mode
To be moved to system_flags_t.
Definition: system.h:283
work_envelope_t work_envelope
Work envelope, only valid for homed axes.
Definition: system.h:271
volatile system_flags_t flags
Assorted state flags.
Definition: system.h:250
signal_event_t last_event
Last signal events (control and limits signal).
Definition: system.h:284
step_control_t step_control
Governs the step segment generator depending on system state.
Definition: system.h:251
axes_signals_t soft_limits
temporary, will be removed when available in settings.
Definition: system.h:287
alarm_code_t alarm
Current alarm, only valid if system state is STATE_ALARM.
Definition: system.h:277
bool cancel
System cancel flag.
Definition: system.h:244
coord_system_id_t probe_coordsys_id
Coordinate system in which last probe took place.
Definition: system.h:258
Definition: system.h:235
coord_data_t max
Definition: system.h:237
coord_data_t min
Definition: system.h:236
void system_clear_tlo_reference(axes_signals_t homing_cycle)
Called on homing state changes.
Definition: system.c:1234
uint8_t system_n_axis(void)
Definition: system.c:53
void system_execute_startup(void *data)
Executes user startup scripts, if stored.
Definition: system.c:185
void control_interrupt_handler(control_signals_t signals)
Pin change interrupt handler for pin-out commands, i.e. cycle start, feed hold, reset etc....
Definition: system.c:84
hold_state_t
Definition: system.h:146
@ Hold_Pending
2
Definition: system.h:149
@ Hold_Complete
1
Definition: system.h:148
@ Hold_NotHolding
0
Definition: system.h:147
const char *(* sys_help_ptr)(const char *command)
Definition: system.h:292
status_code_t(* sys_command_ptr)(sys_state_t state, char *args)
Definition: system.h:291
void system_output_help(const sys_command_t *commands, uint32_t num_commands)
Definition: system.c:1107
void system_command_help(void)
Definition: system.c:1117
void system_register_commands(sys_commands_t *commands)
Definition: system.c:1070
void system_init_switches(void)
Definition: system.c:69
void system_flag_wco_change(void)
Called on a work coordinate (WCO) changes.
Definition: system.c:1257
system_t sys
System global variable structure.
Definition: grbllib.c:73
struct system system_t
Global system variables struct.
machine_mode_t
Definition: system.h:130
@ Mode_Standard
0
Definition: system.h:131
@ Mode_Lathe
2
Definition: system.h:133
@ Mode_Laser
1
Definition: system.h:132
parking_state_t
Definition: system.h:138
@ Parking_Retracting
2
Definition: system.h:141
@ Parking_Cancel
3
Definition: system.h:142
@ Parking_DoorClosed
0
Definition: system.h:139
@ Parking_DoorAjar
1
Definition: system.h:140
@ Parking_Resuming
4
Definition: system.h:143
bool system_xy_at_fixture(coord_system_id_t id, float tolerance)
Checks if XY position is within coordinate system XY with given tolerance.
Definition: system.c:1293
uint_fast16_t rt_exec_t
See rt_exec.
Definition: system.h:152
void system_convert_array_steps_to_mpos(float *position, int32_t *steps)
Sets machine position. Must be sent a 'step' array.
Definition: system.c:1275
uint8_t system_axis_mask(void)
Definition: system.c:58
void system_raise_alarm(alarm_code_t alarm)
Raise and report a system alarm.
Definition: system.c:1311
status_code_t system_execute_line(char *line, stream_write_ptr write)
Directs and executes one line of input from protocol_process.
Definition: system.c:1144
system_state_t
Definition: system.h:102
@ SystemState_DoorOpen
Definition: system.h:110
@ SystemState_CheckMode
Definition: system.h:105
@ SystemState_Jog
Definition: system.h:109
@ SystemState_Undefined
Definition: system.h:114
@ SystemState_Cycle
Definition: system.h:107
@ SystemState_EStop
Definition: system.h:112
@ SystemState_Hold
Definition: system.h:108
@ SystemState_ToolChange
Definition: system.h:113
@ SystemState_Sleep
Definition: system.h:111
@ SystemState_Homing
Definition: system.h:106
@ SystemState_Alarm
Definition: system.h:104
@ SystemState_Idle
Definition: system.h:103
uint8_t system_claim_axis(void)
Definition: system.c:48
struct sys_commands_str sys_commands_t
uint_fast16_t sys_state_t
See sys_state.
Definition: system.h:153
Definition: nuts_bolts.h:248
Definition: nuts_bolts.h:436
Coordinate data.
Definition: nuts_bolts.h:291
Override flags.
Definition: gcode.h:396
Limit switches struct, consists of four packed axes_signals_t structs in 32 bits.
Definition: nuts_bolts.h:405
Definition: system.h:168
uint8_t enabled
Definition: system.h:171
uint8_t restore_cycle
Definition: system.h:174
uint8_t unassigned
Definition: system.h:175
uint8_t value
Definition: system.h:169
uint8_t initiate
Definition: system.h:172
uint8_t restore
Definition: system.h:173
Definition: system.h:156
uint8_t end_motion
Definition: system.h:159
uint8_t unassigned
Definition: system.h:163
uint8_t execute_sys_motion
Definition: system.h:161
uint8_t flags
Definition: system.h:157
uint8_t update_spindle_rpm
Definition: system.h:162
uint8_t execute_hold
Definition: system.h:160
Definition: system.h:294
uint8_t help_fn
Command help is dynamic (output is via function call).
Definition: system.h:299
uint8_t allow_redirect
Command output can be to other other streams than the current.
Definition: system.h:300
uint8_t flags
Definition: system.h:295
uint8_t allow_blocking
Command can be executed when blocking event is active.
Definition: system.h:298
uint8_t noargs
Command does not handle arguments.
Definition: system.h:297
uint8_t unused
Definition: system.h:301
Definition: system.h:209
uint16_t is_parking
Set to true when CMD_SAFETY_DOOR is received.
Definition: system.h:224
uint16_t soft_estop
Definition: system.h:225
uint16_t exit
System exit flag. Used in combination with abort to terminate main loop.
Definition: system.h:215
uint16_t feed_hold_pending
Definition: system.h:217
uint16_t keep_input
Set to true to not flush stream input buffer on executing STOP.
Definition: system.h:220
uint16_t optional_stop_disable
Definition: system.h:218
uint16_t unused
Definition: system.h:226
uint16_t soft_limit
Tracks soft limit errors for the state machine.
Definition: system.h:214
uint16_t value
Definition: system.h:210
uint16_t probe_succeeded
Tracks if last probing cycle was successful.
Definition: system.h:213
uint16_t auto_reporting
Set to true when auto real time reporting is enabled.
Definition: system.h:221
uint16_t travel_changed
Set to true when maximum travel settings has changed.
Definition: system.h:222
uint16_t mpg_mode
MPG mode flag. Set when switched to secondary input stream. (unused for now).
Definition: system.h:212
uint16_t block_delete_enabled
Set to true to enable block delete.
Definition: system.h:216
uint16_t single_block
Set to true to disable M1 (optional stop), via realtime command.
Definition: system.h:219
uint16_t is_homing
Definition: system.h:223
Definition: system.h:199
uint8_t spindle
Definition: system.h:204
uint8_t feedrate
Definition: system.h:202
uint8_t coolant
Definition: system.h:203
uint8_t flags
Definition: system.h:200
uint8_t unused
Definition: system.h:205