grblHAL core  20251012
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-2025 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 
96 typedef enum {
109 } __attribute__ ((__packed__)) system_state_t;
111 
112 #ifdef ARDUINO
113 
114 typedef enum {
115  Mode_Standard = 0,
116  Mode_Laser,
117  Mode_Lathe
119 
120 #else
121 
122 typedef uint8_t machine_mode_t;
123 
127  Mode_Lathe
128 };
129 
130 #endif
131 
132 typedef enum {
139 
140 typedef enum {
143  Hold_Pending = 2
145 
146 typedef uint_fast16_t rt_exec_t;
147 typedef uint_fast16_t sys_state_t;
148 
149 // Define step segment generator state flags.
150 typedef union {
151  uint8_t flags;
152  struct {
153  uint8_t end_motion :1,
158  };
160 
161 // NOTE: the pin_function_t enum must be kept in sync with any changes!
162 typedef union {
163  uint16_t bits;
164  uint16_t mask;
165  uint16_t value;
166  struct {
167  uint16_t reset :1,
173  e_stop :1,
183  };
185 
186 // Define spindle stop override control states.
187 typedef union {
188  uint8_t value;
189  struct {
190  uint8_t enabled :1,
195  };
197 
198 #ifdef PID_LOG
199 
200 typedef struct {
201  uint_fast16_t idx;
202  float setpoint;
203  float t_sample;
204  float target[PID_LOG];
205  float actual[PID_LOG];
206 } pid_data_t;
207 
208 #endif
209 
210 typedef enum {
212  Report_MPGMode = (1 << 0),
213  Report_Scaling = (1 << 1),
214  Report_Homed = (1 << 2),
215  Report_LatheXMode = (1 << 3),
216  Report_Spindle = (1 << 4),
217  Report_Coolant = (1 << 5),
218  Report_Overrides = (1 << 6),
219  Report_Tool = (1 << 7),
220  Report_WCO = (1 << 8),
221  Report_GWCO = (1 << 9),
222  Report_ToolOffset = (1 << 10),
223  Report_M66Result = (1 << 11),
224  Report_PWM = (1 << 12),
225  Report_Motor = (1 << 13),
226  Report_Encoder = (1 << 14),
227  Report_TLOReference = (1 << 15),
228  Report_Fan = (1 << 16),
229  Report_SpindleId = (1 << 17),
230  Report_ProbeId = (1 << 18),
231  Report_DistanceToGo = (1 << 19),
232  Report_ForceWCO = (1 << 29),
233  Report_CycleStart = (1 << 30),
234  Report_All = 0x8003FFFF
236 
237 typedef union {
238  uint32_t value;
239  struct {
240  uint32_t mpg_mode :1,
241  scaling :1,
242  homed :1,
243  xmode :1,
244  spindle :1,
245  coolant :1,
247  tool :1,
248  wco :1,
249  gwco :1,
252  pwm :1,
253  motor :1,
254  encoder :1,
256  fan :1,
258  probe_id :1,
260  unassigned :9, //
263  all :1;
264  };
266 
267 typedef struct {
273 } overrides_t;
274 
275 typedef union {
276  uint8_t flags;
277  struct {
278  uint8_t feedrate :1,
281  unused :5;
282  };
284 
285 typedef union {
286  uint16_t value;
287  struct {
288  uint16_t mpg_mode :1,
291  exit :1,
300  unused :4;
301  };
303 
304 typedef struct {
308 
309 typedef struct {
313 
315 // NOTE: probe_position and position variables may need to be declared as volatiles, if problems arise.
316 typedef struct system {
317  bool abort;
318  bool cancel;
319  bool suspend;
323  volatile bool steppers_deenergize;
329  overrides_t override;
338  volatile uint_fast16_t rt_exec_alarm;
339  int32_t var5399;
340 #ifdef PID_LOG
341  pid_data_t pid_log;
342 #endif
344 
351 
354  bool cold_start;
357  bool mpg_mode;
359  int32_t position[N_AXIS];
364 
365 typedef status_code_t (*sys_command_ptr)(sys_state_t state, char *args);
366 typedef const char *(*sys_help_ptr)(const char *command);
367 
368 typedef union {
369  uint8_t flags;
370  struct {
371  uint8_t noargs :1,
374  unused :5;
375  };
377 
378 typedef struct
379 {
380  const char *command;
383  union {
384  const char *str;
386  } help;
387 } sys_command_t;
388 
389 typedef struct sys_commands_str {
390  const uint8_t n_commands;
393  struct sys_commands_str *(*on_get_commands)(void);
395 
396 extern system_t sys;
397 
399 void system_execute_startup (void *data);
400 void system_flag_wco_change (void);
401 void system_convert_array_steps_to_mpos (float *position, int32_t *steps);
402 bool system_xy_at_fixture (coord_system_id_t id, float tolerance);
403 void system_raise_alarm (alarm_code_t alarm);
404 void system_init_switches (void);
405 void system_command_help (void);
406 void system_output_help (const sys_command_t *commands, uint32_t num_commands);
408 
409 void system_clear_tlo_reference (axes_signals_t homing_cycle);
412 
413 // Special handlers for setting and clearing grblHAL's real-time execution flags.
414 #define system_set_exec_state_flag(mask) hal.set_bits_atomic(&sys.rt_exec_state, (mask))
415 #define system_clear_exec_state_flag(mask) hal.clear_bits_atomic(&sys.rt_exec_state, (mask))
416 #define system_clear_exec_states() hal.set_value_atomic(&sys.rt_exec_state, 0)
417 #define system_set_exec_alarm(code) hal.set_value_atomic(&sys.rt_exec_alarm, (uint_fast16_t)(code))
418 #define system_clear_exec_alarm() hal.set_value_atomic(&sys.rt_exec_alarm, 0)
419 
421 
422 #endif
alarm_code_t
Definition: alarms.h:28
#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:30
coord_system_id_t
Definition: gcode.h:162
uint_fast16_t override_t
Definition: grbl.h:259
probing_state_t
Definition: probe.h:27
Definition: system.h:267
override_t rapid_rate
Rapids override value in percent.
Definition: system.h:269
override_t spindle_rpm
NOTE: Not used by the core, it maintain per spindle override in spindle_param_t
Definition: system.h:270
gc_override_flags_t control
Tracks override control states.
Definition: system.h:272
override_t feed_rate
Feed rate override value in percent.
Definition: system.h:268
spindle_stop_t spindle_stop
Tracks spindle stop override states.
Definition: system.h:271
Definition: system.h:304
limit_signals_t limits
Definition: system.h:306
control_signals_t control
Definition: system.h:305
Definition: system.h:379
sys_command_flags_t flags
Definition: system.h:382
sys_command_ptr execute
Definition: system.h:381
const char * command
Definition: system.h:380
sys_help_ptr fn
Definition: system.h:385
const char * str
Definition: system.h:384
Definition: system.h:389
const uint8_t n_commands
Definition: system.h:390
struct sys_commands_str * next
Definition: system.h:392
const sys_command_t * commands
Definition: system.h:391
Global system variables struct.
Definition: system.h:316
axes_signals_t tlo_reference_set
Axes with tool length reference offset set.
Definition: system.h:348
axes_signals_t homed
Indicates which axes has been homed.
Definition: system.h:345
float home_position[N_AXIS]
Home position for homed axes.
Definition: system.h:346
int32_t probe_position[N_AXIS]
Last probe position in machine coordinates and steps.
Definition: system.h:335
int32_t var5399
Last result from M66 - wait on input.
Definition: system.h:339
parking_state_t parking_state
Tracks parking state.
Definition: system.h:332
system_override_delay_t override_delay
Flags for delayed overrides.
Definition: system.h:330
volatile probing_state_t probing_state
Probing state value. Used to coordinate the probing cycle with stepper ISR.
Definition: system.h:336
bool blocking_event
Set when a blocking event that requires reset to clear is active.
Definition: system.h:322
bool position_lost
Set when mc_reset is called when machine is moving.
Definition: system.h:320
bool reset_pending
Set when reset processing is underway.
Definition: system.h:321
bool ioinit_pending
Definition: system.h:355
volatile bool steppers_deenergize
Set to true to deenergize stepperes.
Definition: system.h:323
hold_state_t holding_state
Tracks holding state.
Definition: system.h:333
axes_signals_t hard_limits
temporary?, will be removed when available in settings.
Definition: system.h:360
alarm_code_t alarm_pending
Delayed alarm, currently used for probe protection.
Definition: system.h:324
axes_signals_t homing_axis_lock
Locks axes when limits engage. Used as an axis motion mask in the stepper ISR.
Definition: system.h:327
bool suspend
System suspend state flag.
Definition: system.h:319
volatile rt_exec_t rt_exec_state
Realtime executor bitflag variable for state management. See EXEC bitmasks.
Definition: system.h:337
bool abort
System abort flag. Forces exit back to main loop for reset.
Definition: system.h:317
bool driver_started
Set to true when driver initialization is completed.
Definition: system.h:356
axes_signals_t homing
Axes with homing enabled.
Definition: system.h:328
int32_t tlo_reference[N_AXIS]
Tool length reference offset.
Definition: system.h:349
int32_t position[N_AXIS]
Real-time machine (aka home) position vector in steps.
Definition: system.h:359
bool cold_start
Set to true on boot, is false on subsequent soft resets.
Definition: system.h:354
volatile uint_fast16_t rt_exec_alarm
Realtime executor bitflag variable for setting various alarms.
Definition: system.h:338
bool mpg_mode
To be moved to system_flags_t.
Definition: system.h:357
work_envelope_t work_envelope
Work envelope, only valid for homed axes.
Definition: system.h:347
report_tracking_flags_t report
Tracks when to add data to status reports.
Definition: system.h:331
volatile system_flags_t flags
Assorted state flags.
Definition: system.h:325
signal_event_t last_event
Last signal events (control and limits signal).
Definition: system.h:358
step_control_t step_control
Governs the step segment generator depending on system state.
Definition: system.h:326
axes_signals_t soft_limits
temporary, will be removed when available in settings.
Definition: system.h:361
alarm_code_t alarm
Current alarm, only valid if system state is STATE_ALARM.
Definition: system.h:353
bool cancel
System cancel flag.
Definition: system.h:318
coord_system_id_t probe_coordsys_id
Coordinate system in which last probe took place.
Definition: system.h:334
Definition: system.h:309
coord_data_t max
Definition: system.h:311
coord_data_t min
Definition: system.h:310
report_tracking_flags_t system_get_rt_report_flags(void)
Get the active realtime report addon flags for the next report.
Definition: system.c:1266
void system_clear_tlo_reference(axes_signals_t homing_cycle)
Called on homing state changes.
Definition: system.c:1169
void system_execute_startup(void *data)
Executes user startup scripts, if stored.
Definition: system.c:155
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:140
@ Hold_Pending
2
Definition: system.h:143
@ Hold_Complete
1
Definition: system.h:142
@ Hold_NotHolding
0
Definition: system.h:141
const char *(* sys_help_ptr)(const char *command)
Definition: system.h:366
status_code_t(* sys_command_ptr)(sys_state_t state, char *args)
Definition: system.h:365
void system_output_help(const sys_command_t *commands, uint32_t num_commands)
Definition: system.c:1027
void system_command_help(void)
Definition: system.c:1037
void system_register_commands(sys_commands_t *commands)
Definition: system.c:990
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:1192
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:124
@ Mode_Standard
0
Definition: system.h:125
@ Mode_Lathe
2
Definition: system.h:127
@ Mode_Laser
1
Definition: system.h:126
parking_state_t
Definition: system.h:132
@ Parking_Retracting
2
Definition: system.h:135
@ Parking_Cancel
3
Definition: system.h:136
@ Parking_DoorClosed
0
Definition: system.h:133
@ Parking_DoorAjar
1
Definition: system.h:134
@ Parking_Resuming
4
Definition: system.h:137
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:1228
uint_fast16_t rt_exec_t
See rt_exec.
Definition: system.h:146
report_tracking_t
Definition: system.h:210
@ Report_PWM
Definition: system.h:224
@ Report_Scaling
Definition: system.h:213
@ Report_Homed
Definition: system.h:214
@ Report_ClearAll
Definition: system.h:211
@ Report_GWCO
Definition: system.h:221
@ Report_DistanceToGo
Definition: system.h:231
@ Report_WCO
Definition: system.h:220
@ Report_Overrides
Definition: system.h:218
@ Report_CycleStart
Definition: system.h:233
@ Report_Motor
Definition: system.h:225
@ Report_All
Definition: system.h:234
@ Report_MPGMode
Definition: system.h:212
@ Report_LatheXMode
Definition: system.h:215
@ Report_TLOReference
Definition: system.h:227
@ Report_SpindleId
Definition: system.h:229
@ Report_ForceWCO
Definition: system.h:232
@ Report_Encoder
Definition: system.h:226
@ Report_Fan
Definition: system.h:228
@ Report_Coolant
Definition: system.h:217
@ Report_Spindle
Definition: system.h:216
@ Report_ToolOffset
Definition: system.h:222
@ Report_ProbeId
Definition: system.h:230
@ Report_Tool
Definition: system.h:219
@ Report_M66Result
Definition: system.h:223
void system_convert_array_steps_to_mpos(float *position, int32_t *steps)
Sets machine position. Must be sent a 'step' array.
Definition: system.c:1210
status_code_t system_execute_line(char *line)
Directs and executes one line of input from protocol_process.
Definition: system.c:1063
void system_add_rt_report(report_tracking_t report)
Set(s) or clear all active realtime report addon flag(s) for the next report.
Definition: system.c:1276
void system_raise_alarm(alarm_code_t alarm)
Raise and report a system alarm.
Definition: system.c:1245
system_state_t
Definition: system.h:96
@ SystemState_DoorOpen
Definition: system.h:104
@ SystemState_CheckMode
Definition: system.h:99
@ SystemState_Jog
Definition: system.h:103
@ SystemState_Undefined
Definition: system.h:108
@ SystemState_Cycle
Definition: system.h:101
@ SystemState_EStop
Definition: system.h:106
@ SystemState_Hold
Definition: system.h:102
@ SystemState_ToolChange
Definition: system.h:107
@ SystemState_Sleep
Definition: system.h:105
@ SystemState_Homing
Definition: system.h:100
@ SystemState_Alarm
Definition: system.h:98
@ SystemState_Idle
Definition: system.h:97
struct sys_commands_str sys_commands_t
uint_fast16_t sys_state_t
See sys_state.
Definition: system.h:147
Definition: nuts_bolts.h:123
Definition: system.h:162
uint16_t probe_overtravel
used for probe (toolsetter) protection
Definition: system.h:180
uint16_t bits
Definition: system.h:163
uint16_t stop_disable
Definition: system.h:172
uint16_t limits_override
Definition: system.h:177
uint16_t cycle_start
Definition: system.h:169
uint16_t feed_hold
Definition: system.h:168
uint16_t probe_disconnected
Definition: system.h:174
uint16_t reset
Definition: system.h:167
uint16_t safety_door_ajar
Definition: system.h:170
uint16_t motor_fault
Definition: system.h:175
uint16_t motor_warning
Definition: system.h:176
uint16_t deasserted
used for probe protection
Definition: system.h:182
uint16_t mask
Definition: system.h:164
uint16_t e_stop
M1.
Definition: system.h:173
uint16_t block_delete
Definition: system.h:171
uint16_t value
Definition: system.h:165
uint16_t probe_triggered
used for probe protection
Definition: system.h:181
uint16_t tls_overtravel
Definition: system.h:179
uint16_t single_block
Definition: system.h:178
Coordinate data.
Definition: nuts_bolts.h:140
Override flags.
Definition: gcode.h:333
Limit switches struct, consists of four packed axes_signals_t structs in 32 bits.
Definition: nuts_bolts.h:219
Definition: system.h:237
uint32_t tlo_reference
Tool length offset reference changed.
Definition: system.h:255
uint32_t force_wco
Add work coordinates (due to WCO changed during motion).
Definition: system.h:261
uint32_t tool
Tool changed.
Definition: system.h:247
uint32_t wco
Add work coordinates.
Definition: system.h:248
uint32_t cycle_start
Cycle start signal triggered. NOTE: do NOT add to Report_All enum above!
Definition: system.h:262
uint32_t unassigned
Definition: system.h:260
uint32_t gwco
Add work coordinate.
Definition: system.h:249
uint32_t fan
Fan on/off changed.
Definition: system.h:256
uint32_t distance_to_go
Distance to go.
Definition: system.h:259
uint32_t mpg_mode
MPG mode changed.
Definition: system.h:240
uint32_t all
Set when CMD_STATUS_REPORT_ALL is requested, may be used by user code.
Definition: system.h:263
uint32_t motor
Add motor information (optional: to be added by driver).
Definition: system.h:253
uint32_t spindle_id
Spindle changed.
Definition: system.h:257
uint32_t tool_offset
Tool offsets changed.
Definition: system.h:250
uint32_t m66result
M66 result updated.
Definition: system.h:251
uint32_t probe_id
Probe changed.
Definition: system.h:258
uint32_t spindle
Spindle state changed.
Definition: system.h:244
uint32_t homed
Homed state changed.
Definition: system.h:242
uint32_t scaling
Scaling (G50/G51) changed.
Definition: system.h:241
uint32_t pwm
Add PWM information (optional: to be added by driver).
Definition: system.h:252
uint32_t encoder
Add encoder information (optional: to be added by driver).
Definition: system.h:254
uint32_t value
Definition: system.h:238
uint32_t coolant
Coolant state changed.
Definition: system.h:245
uint32_t overrides
Overrides changed.
Definition: system.h:246
uint32_t xmode
Lathe radius/diameter mode changed.
Definition: system.h:243
Definition: system.h:187
uint8_t enabled
Definition: system.h:190
uint8_t restore_cycle
Definition: system.h:193
uint8_t unassigned
Definition: system.h:194
uint8_t value
Definition: system.h:188
uint8_t initiate
Definition: system.h:191
uint8_t restore
Definition: system.h:192
Definition: system.h:150
uint8_t end_motion
Definition: system.h:153
uint8_t unassigned
Definition: system.h:157
uint8_t execute_sys_motion
Definition: system.h:155
uint8_t flags
Definition: system.h:151
uint8_t update_spindle_rpm
Definition: system.h:156
uint8_t execute_hold
Definition: system.h:154
Definition: system.h:368
uint8_t help_fn
Definition: system.h:373
uint8_t flags
Definition: system.h:369
uint8_t allow_blocking
System command can be used when blocking event is active.
Definition: system.h:372
uint8_t noargs
System command does not handle arguments.
Definition: system.h:371
uint8_t unused
Definition: system.h:374
Definition: system.h:285
uint16_t exit
System exit flag. Used in combination with abort to terminate main loop.
Definition: system.h:291
uint16_t feed_hold_pending
Definition: system.h:293
uint16_t keep_input
Set to true to not flush stream input buffer on executing STOP.
Definition: system.h:296
uint16_t optional_stop_disable
Definition: system.h:294
uint16_t unused
Definition: system.h:300
uint16_t soft_limit
Tracks soft limit errors for the state machine.
Definition: system.h:290
uint16_t value
Definition: system.h:286
uint16_t probe_succeeded
Tracks if last probing cycle was successful.
Definition: system.h:289
uint16_t auto_reporting
Set to true when auto real time reporting is enabled.
Definition: system.h:297
uint16_t travel_changed
Set to true when maximum travel settings has changed.
Definition: system.h:298
uint16_t mpg_mode
MPG mode flag. Set when switched to secondary input stream. (unused for now).
Definition: system.h:288
uint16_t block_delete_enabled
Set to true to enable block delete.
Definition: system.h:292
uint16_t single_block
Set to true to disable M1 (optional stop), via realtime command.
Definition: system.h:295
uint16_t is_homing
Definition: system.h:299
Definition: system.h:275
uint8_t spindle
Definition: system.h:280
uint8_t feedrate
Definition: system.h:278
uint8_t coolant
Definition: system.h:279
uint8_t flags
Definition: system.h:276
uint8_t unused
Definition: system.h:281