grblHAL core  20260411
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 "messages.h"
30 #if NGC_EXPRESSIONS_ENABLE
31 #include "vfs.h"
32 #endif
33 
45 #define EXEC_STATUS_REPORT bit(0)
46 #define EXEC_CYCLE_START bit(1)
47 #define EXEC_CYCLE_COMPLETE bit(2)
48 #define EXEC_FEED_HOLD bit(3)
49 #define EXEC_STOP bit(4)
50 #define EXEC_RESET bit(5)
51 #define EXEC_SAFETY_DOOR bit(6)
52 #define EXEC_MOTION_CANCEL bit(7)
53 #define EXEC_MOTION_CANCEL_FAST bit(7) // Permanently enabled for now
54 #define EXEC_SLEEP bit(9)
55 #define EXEC_TOOL_CHANGE bit(10)
56 #define EXEC_PID_REPORT bit(11)
57 #define EXEC_GCODE_REPORT bit(12)
58 #define EXEC_TLO_REPORT bit(13)
59 #define EXEC_RT_COMMAND bit(14)
60 #define EXEC_DOOR_CLOSED bit(15)
62 
64 
75 #define STATE_IDLE 0
76 #define STATE_ALARM bit(0)
77 #define STATE_CHECK_MODE bit(1)
78 #define STATE_HOMING bit(2)
79 #define STATE_CYCLE bit(3)
80 #define STATE_HOLD bit(4)
81 #define STATE_JOG bit(5)
82 #define STATE_SAFETY_DOOR bit(6)
83 #define STATE_SLEEP bit(7)
84 #define STATE_ESTOP bit(8)
85 #define STATE_TOOL_CHANGE bit(9)
87 
89 
91 #define STATE_HAS_SUBSTATE (STATE_CYCLE|STATE_HOLD|STATE_ESTOP|STATE_ALARM|STATE_SAFETY_DOOR)
92 
94 
101 typedef enum {
114 } __attribute__ ((__packed__)) system_state_t;
116 
117 #ifdef ARDUINO
118 
119 typedef enum {
120  Mode_Standard = 0,
121  Mode_Laser,
122  Mode_Lathe
124 
125 #else
126 
127 typedef uint8_t machine_mode_t;
128 
132  Mode_Lathe
133 };
134 
135 #endif
136 
137 typedef enum {
144 
145 typedef enum {
148  Hold_Pending = 2
150 
151 typedef uint_fast16_t rt_exec_t;
152 typedef uint_fast16_t sys_state_t;
153 
154 // Define step segment generator state flags.
155 typedef union {
156  uint8_t flags;
157  struct {
158  uint8_t end_motion :1,
163  };
165 
166 // Define spindle stop override control states.
167 typedef union {
168  uint8_t value;
169  struct {
170  uint8_t enabled :1,
175  };
177 
178 #ifdef PID_LOG
179 
180 typedef struct {
181  uint_fast16_t idx;
182  float setpoint;
183  float t_sample;
184  float target[PID_LOG];
185  float actual[PID_LOG];
186 } pid_data_t;
187 
188 #endif
189 
190 typedef struct {
196 } overrides_t;
197 
198 typedef union {
199  uint8_t flags;
200  struct {
201  uint8_t feedrate :1,
204  unused :5;
205  };
207 
208 typedef union {
209  uint16_t value;
210  struct {
211  uint16_t mpg_mode :1,
214  exit :1,
224  unused :3;
225  };
227 
228 typedef struct {
232 
233 typedef struct {
237 
239 // NOTE: probe_position and position variables may need to be declared as volatiles, if problems arise.
240 typedef struct system {
241  bool abort;
242  bool cancel;
243  bool suspend;
247  volatile bool steppers_deenergize;
252  overrides_t override;
260  volatile uint_fast16_t rt_exec_alarm;
261  int32_t var5399;
262 #ifdef PID_LOG
263  pid_data_t pid_log;
264 #endif
266 
273 
277  bool cold_start;
281  bool mpg_mode;
283  int32_t position[N_AXIS];
288 
289 typedef status_code_t (*sys_command_ptr)(sys_state_t state, char *args);
290 typedef const char *(*sys_help_ptr)(const char *command);
291 
292 typedef union {
293  uint8_t flags;
294  struct {
295  uint8_t noargs :1,
298  unused :5;
299  };
301 
302 typedef struct
303 {
304  const char *command;
307  union {
308  const char *str;
310  } help;
311 } sys_command_t;
312 
313 typedef struct sys_commands_str {
314  const uint8_t n_commands;
317  struct sys_commands_str *(*on_get_commands)(void);
319 
320 extern system_t sys;
321 
323 void system_execute_startup (void *data);
324 void system_flag_wco_change (void);
325 void system_convert_array_steps_to_mpos (float *position, int32_t *steps);
326 bool system_xy_at_fixture (coord_system_id_t id, float tolerance);
327 void system_raise_alarm (alarm_code_t alarm);
328 void system_init_switches (void);
329 void system_command_help (void);
330 void system_output_help (const sys_command_t *commands, uint32_t num_commands);
332 
333 void system_clear_tlo_reference (axes_signals_t homing_cycle);
334 
335 // Special handlers for setting and clearing grblHAL's real-time execution flags.
336 #define system_set_exec_state_flag(mask) hal.set_bits_atomic(&sys.rt_exec_state, (mask))
337 #define system_clear_exec_state_flag(mask) hal.clear_bits_atomic(&sys.rt_exec_state, (mask))
338 #define system_clear_exec_states() hal.set_value_atomic(&sys.rt_exec_state, 0)
339 #define system_set_exec_alarm(code) hal.set_value_atomic(&sys.rt_exec_alarm, (uint_fast16_t)(code))
340 #define system_clear_exec_alarm() hal.set_value_atomic(&sys.rt_exec_alarm, 0)
341 
343 
344 #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:179
uint_fast16_t override_t
Definition: grbl.h:262
probing_state_t
Definition: probe.h:33
Definition: system.h:190
override_t rapid_rate
Rapids override value in percent.
Definition: system.h:192
override_t spindle_rpm
NOTE: Not used by the core, it maintain per spindle override in spindle_param_t
Definition: system.h:193
gc_override_flags_t control
Tracks override control states.
Definition: system.h:195
override_t feed_rate
Feed rate override value in percent.
Definition: system.h:191
spindle_stop_t spindle_stop
Tracks spindle stop override states.
Definition: system.h:194
Definition: system.h:228
limit_signals_t limits
Definition: system.h:230
control_signals_t control
Definition: system.h:229
Definition: system.h:303
sys_command_flags_t flags
Definition: system.h:306
sys_command_ptr execute
Definition: system.h:305
const char * command
Definition: system.h:304
sys_help_ptr fn
Definition: system.h:309
const char * str
Definition: system.h:308
Definition: system.h:313
const uint8_t n_commands
Definition: system.h:314
struct sys_commands_str * next
Definition: system.h:316
const sys_command_t * commands
Definition: system.h:315
Global system variables struct.
Definition: system.h:240
axes_signals_t tlo_reference_set
Axes with tool length reference offset set.
Definition: system.h:270
axes_signals_t homed
Indicates which axes has been homed.
Definition: system.h:267
float home_position[N_AXIS]
Home position for homed axes.
Definition: system.h:268
int32_t probe_position[N_AXIS]
Last probe position in machine coordinates and steps.
Definition: system.h:257
bool steppers_enabled
Set to true when all steppers are enabled.
Definition: system.h:280
int32_t var5399
Last result from M66 - wait on input.
Definition: system.h:261
parking_state_t parking_state
Tracks parking state.
Definition: system.h:254
system_override_delay_t override_delay
Flags for delayed overrides.
Definition: system.h:253
volatile probing_state_t probing_state
Probing state value. Used to coordinate the probing cycle with stepper ISR.
Definition: system.h:258
bool blocking_event
Set when a blocking event that requires reset to clear is active.
Definition: system.h:246
bool position_lost
Set when mc_reset is called when machine is moving.
Definition: system.h:244
bool reset_pending
Set when reset processing is underway.
Definition: system.h:245
bool ioinit_pending
Definition: system.h:278
volatile bool steppers_deenergize
Set to true to deenergize stepperes.
Definition: system.h:247
hold_state_t holding_state
Tracks holding state.
Definition: system.h:255
axes_signals_t hard_limits
temporary?, will be removed when available in settings.
Definition: system.h:284
alarm_code_t alarm_pending
Delayed alarm, currently used for probe protection.
Definition: system.h:276
axes_signals_t homing_axis_lock
Locks axes when limits engage. Used as an axis motion mask in the stepper ISR.
Definition: system.h:250
bool suspend
System suspend state flag.
Definition: system.h:243
volatile rt_exec_t rt_exec_state
Realtime executor bitflag variable for state management. See EXEC bitmasks.
Definition: system.h:259
bool abort
System abort flag. Forces exit back to main loop for reset.
Definition: system.h:241
bool driver_started
Set to true when driver initialization is completed.
Definition: system.h:279
axes_signals_t homing
Axes with homing enabled.
Definition: system.h:251
int32_t tlo_reference[N_AXIS]
Tool length reference offset.
Definition: system.h:271
int32_t position[N_AXIS]
Real-time machine (aka home) position vector in steps.
Definition: system.h:283
bool cold_start
Set to true on boot, is false on subsequent soft resets.
Definition: system.h:277
volatile uint_fast16_t rt_exec_alarm
Realtime executor bitflag variable for setting various alarms.
Definition: system.h:260
bool mpg_mode
To be moved to system_flags_t.
Definition: system.h:281
work_envelope_t work_envelope
Work envelope, only valid for homed axes.
Definition: system.h:269
volatile system_flags_t flags
Assorted state flags.
Definition: system.h:248
signal_event_t last_event
Last signal events (control and limits signal).
Definition: system.h:282
step_control_t step_control
Governs the step segment generator depending on system state.
Definition: system.h:249
axes_signals_t soft_limits
temporary, will be removed when available in settings.
Definition: system.h:285
alarm_code_t alarm
Current alarm, only valid if system state is STATE_ALARM.
Definition: system.h:275
bool cancel
System cancel flag.
Definition: system.h:242
coord_system_id_t probe_coordsys_id
Coordinate system in which last probe took place.
Definition: system.h:256
Definition: system.h:233
coord_data_t max
Definition: system.h:235
coord_data_t min
Definition: system.h:234
void system_clear_tlo_reference(axes_signals_t homing_cycle)
Called on homing state changes.
Definition: system.c:1170
void system_execute_startup(void *data)
Executes user startup scripts, if stored.
Definition: system.c:162
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:61
hold_state_t
Definition: system.h:145
@ Hold_Pending
2
Definition: system.h:148
@ Hold_Complete
1
Definition: system.h:147
@ Hold_NotHolding
0
Definition: system.h:146
const char *(* sys_help_ptr)(const char *command)
Definition: system.h:290
status_code_t(* sys_command_ptr)(sys_state_t state, char *args)
Definition: system.h:289
void system_output_help(const sys_command_t *commands, uint32_t num_commands)
Definition: system.c:1048
void system_command_help(void)
Definition: system.c:1058
void system_register_commands(sys_commands_t *commands)
Definition: system.c:1011
void system_init_switches(void)
Definition: system.c:46
void system_flag_wco_change(void)
Called on a work coordinate (WCO) changes.
Definition: system.c:1193
system_t sys
System global variable structure.
Definition: grbllib.c:89
struct system system_t
Global system variables struct.
machine_mode_t
Definition: system.h:129
@ Mode_Standard
0
Definition: system.h:130
@ Mode_Lathe
2
Definition: system.h:132
@ Mode_Laser
1
Definition: system.h:131
parking_state_t
Definition: system.h:137
@ Parking_Retracting
2
Definition: system.h:140
@ Parking_Cancel
3
Definition: system.h:141
@ Parking_DoorClosed
0
Definition: system.h:138
@ Parking_DoorAjar
1
Definition: system.h:139
@ Parking_Resuming
4
Definition: system.h:142
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:1229
uint_fast16_t rt_exec_t
See rt_exec.
Definition: system.h:151
void system_convert_array_steps_to_mpos(float *position, int32_t *steps)
Sets machine position. Must be sent a 'step' array.
Definition: system.c:1211
status_code_t system_execute_line(char *line)
Directs and executes one line of input from protocol_process.
Definition: system.c:1084
void system_raise_alarm(alarm_code_t alarm)
Raise and report a system alarm.
Definition: system.c:1247
system_state_t
Definition: system.h:101
@ SystemState_DoorOpen
Definition: system.h:109
@ SystemState_CheckMode
Definition: system.h:104
@ SystemState_Jog
Definition: system.h:108
@ SystemState_Undefined
Definition: system.h:113
@ SystemState_Cycle
Definition: system.h:106
@ SystemState_EStop
Definition: system.h:111
@ SystemState_Hold
Definition: system.h:107
@ SystemState_ToolChange
Definition: system.h:112
@ SystemState_Sleep
Definition: system.h:110
@ SystemState_Homing
Definition: system.h:105
@ SystemState_Alarm
Definition: system.h:103
@ SystemState_Idle
Definition: system.h:102
struct sys_commands_str sys_commands_t
uint_fast16_t sys_state_t
See sys_state.
Definition: system.h:152
Definition: nuts_bolts.h:246
Definition: nuts_bolts.h:416
Coordinate data.
Definition: nuts_bolts.h:289
Override flags.
Definition: gcode.h:367
Limit switches struct, consists of four packed axes_signals_t structs in 32 bits.
Definition: nuts_bolts.h:385
Definition: system.h:167
uint8_t enabled
Definition: system.h:170
uint8_t restore_cycle
Definition: system.h:173
uint8_t unassigned
Definition: system.h:174
uint8_t value
Definition: system.h:168
uint8_t initiate
Definition: system.h:171
uint8_t restore
Definition: system.h:172
Definition: system.h:155
uint8_t end_motion
Definition: system.h:158
uint8_t unassigned
Definition: system.h:162
uint8_t execute_sys_motion
Definition: system.h:160
uint8_t flags
Definition: system.h:156
uint8_t update_spindle_rpm
Definition: system.h:161
uint8_t execute_hold
Definition: system.h:159
Definition: system.h:292
uint8_t help_fn
Definition: system.h:297
uint8_t flags
Definition: system.h:293
uint8_t allow_blocking
System command can be used when blocking event is active.
Definition: system.h:296
uint8_t noargs
System command does not handle arguments.
Definition: system.h:295
uint8_t unused
Definition: system.h:298
Definition: system.h:208
uint16_t is_parking
Set to true when CMD_SAFETY_DOOR is received.
Definition: system.h:223
uint16_t exit
System exit flag. Used in combination with abort to terminate main loop.
Definition: system.h:214
uint16_t feed_hold_pending
Definition: system.h:216
uint16_t keep_input
Set to true to not flush stream input buffer on executing STOP.
Definition: system.h:219
uint16_t optional_stop_disable
Definition: system.h:217
uint16_t unused
Definition: system.h:224
uint16_t soft_limit
Tracks soft limit errors for the state machine.
Definition: system.h:213
uint16_t value
Definition: system.h:209
uint16_t probe_succeeded
Tracks if last probing cycle was successful.
Definition: system.h:212
uint16_t auto_reporting
Set to true when auto real time reporting is enabled.
Definition: system.h:220
uint16_t travel_changed
Set to true when maximum travel settings has changed.
Definition: system.h:221
uint16_t mpg_mode
MPG mode flag. Set when switched to secondary input stream. (unused for now).
Definition: system.h:211
uint16_t block_delete_enabled
Set to true to enable block delete.
Definition: system.h:215
uint16_t single_block
Set to true to disable M1 (optional stop), via realtime command.
Definition: system.h:218
uint16_t is_homing
Definition: system.h:222
Definition: system.h:198
uint8_t spindle
Definition: system.h:203
uint8_t feedrate
Definition: system.h:201
uint8_t coolant
Definition: system.h:202
uint8_t flags
Definition: system.h:199
uint8_t unused
Definition: system.h:204