grblHAL core  20241107
system.h File Reference
#include "gcode.h"
#include "probe.h"
#include "alarms.h"
#include "messages.h"

Go to the source code of this file.

Data Structures

union  step_control_t
 
union  control_signals_t
 
union  spindle_stop_t
 
union  report_tracking_flags_t
 
struct  overrides_t
 
union  system_override_delay_t
 
union  system_flags_t
 
struct  signal_event_t
 
struct  work_envelope_t
 
struct  system
 Global system variables struct. More...
 
union  sys_command_flags_t
 
struct  sys_command_t
 
struct  sys_commands_str
 

Macros

#define system_set_exec_state_flag(mask)   hal.set_bits_atomic(&sys.rt_exec_state, (mask))
 
#define system_clear_exec_state_flag(mask)   hal.clear_bits_atomic(&sys.rt_exec_state, (mask))
 
#define system_clear_exec_states()   hal.set_value_atomic(&sys.rt_exec_state, 0)
 
#define system_set_exec_alarm(code)   hal.set_value_atomic(&sys.rt_exec_alarm, (uint_fast16_t)(code))
 
#define system_clear_exec_alarm()   hal.set_value_atomic(&sys.rt_exec_alarm, 0)
 
System executor bit map.

Used internally by realtime protocol as realtime command flags, which notifies the main program to execute the specified realtime command asynchronously.

NOTE: The system executor uses an unsigned 16-bit volatile variable (16 flag limit.) The default flags are always false, so the realtime protocol only needs to check for a non-zero value to know when there is a realtime command to execute.

#define EXEC_STATUS_REPORT   bit(0)
 
#define EXEC_CYCLE_START   bit(1)
 
#define EXEC_CYCLE_COMPLETE   bit(2)
 
#define EXEC_FEED_HOLD   bit(3)
 
#define EXEC_STOP   bit(4)
 
#define EXEC_RESET   bit(5)
 
#define EXEC_SAFETY_DOOR   bit(6)
 
#define EXEC_MOTION_CANCEL   bit(7)
 
#define EXEC_SLEEP   bit(8)
 
#define EXEC_TOOL_CHANGE   bit(9)
 
#define EXEC_PID_REPORT   bit(10)
 
#define EXEC_GCODE_REPORT   bit(11)
 
#define EXEC_TLO_REPORT   bit(12)
 
#define EXEC_RT_COMMAND   bit(13)
 
#define EXEC_DOOR_CLOSED   bit(14)
 
System state bit map.

The state variable (type sys_state_t) primarily tracks the individual states of grblHAL to manage each without overlapping. It is also used as a messaging flag for critical events.

It is encapsulated by the main state machine in state_machine.c and can only be set via the state_set() or state_update() functions and read via the state_get() function.

NOTE: flags are mutually exclusive, bit map allows testing for multiple states (except STATE_IDLE) in a single statement

#define STATE_IDLE   0
 Must be zero. No flags. More...
 
#define STATE_ALARM   bit(0)
 In alarm state. Locks out all g-code processes. Allows settings access. More...
 
#define STATE_CHECK_MODE   bit(1)
 G-code check mode. Locks out planner and motion only. More...
 
#define STATE_HOMING   bit(2)
 Performing homing cycle. More...
 
#define STATE_CYCLE   bit(3)
 Cycle is running or motions are being executed. More...
 
#define STATE_HOLD   bit(4)
 Active feed hold. More...
 
#define STATE_JOG   bit(5)
 Jogging mode. More...
 
#define STATE_SAFETY_DOOR   bit(6)
 Safety door is ajar. Feed holds and de-energizes system. More...
 
#define STATE_SLEEP   bit(7)
 Sleep state. More...
 
#define STATE_ESTOP   bit(8)
 EStop mode, reports and is mainly handled similar to alarm state. More...
 
#define STATE_TOOL_CHANGE   bit(9)
 Manual tool change, similar to STATE_HOLD - but stops spindle and allows jogging. More...
 

Typedefs

typedef uint_fast16_t rt_exec_t
 See rt_exec. More...
 
typedef uint_fast16_t sys_state_t
 See sys_state. More...
 
typedef struct system system_t
 Global system variables struct. More...
 
typedef status_code_t(* sys_command_ptr) (sys_state_t state, char *args)
 
typedef const char *(* sys_help_ptr) (const char *command)
 
typedef struct sys_commands_str sys_commands_t
 

Enumerations

enum  machine_mode_t { Mode_Standard = 0 , Mode_Laser , Mode_Lathe }
 
enum  parking_state_t {
  Parking_DoorClosed = 0 , Parking_DoorAjar , Parking_Retracting , Parking_Cancel ,
  Parking_Resuming
}
 
enum  hold_state_t { Hold_NotHolding = 0 , Hold_Complete = 1 , Hold_Pending = 2 }
 
enum  report_tracking_t {
  Report_ClearAll = 0 , Report_MPGMode = (1 << 0) , Report_Scaling = (1 << 1) , Report_Homed = (1 << 2) ,
  Report_LatheXMode = (1 << 3) , Report_Spindle = (1 << 4) , Report_Coolant = (1 << 5) , Report_Overrides = (1 << 6) ,
  Report_Tool = (1 << 7) , Report_WCO = (1 << 8) , Report_GWCO = (1 << 9) , Report_ToolOffset = (1 << 10) ,
  Report_M66Result = (1 << 11) , Report_PWM = (1 << 12) , Report_Motor = (1 << 13) , Report_Encoder = (1 << 14) ,
  Report_TLOReference = (1 << 15) , Report_Fan = (1 << 16) , Report_SpindleId = (1 << 17) , Report_ForceWCO = (1 << 29) ,
  Report_CycleStart = (1 << 30) , Report_All = 0x8003FFFF
}
 

Functions

status_code_t system_execute_line (char *line)
 Directs and executes one line of input from protocol_process. More...
 
void system_execute_startup (void)
 Executes user startup scripts, if stored. More...
 
void system_flag_wco_change (void)
 Called on a work coordinate (WCO) changes. More...
 
void system_convert_array_steps_to_mpos (float *position, int32_t *steps)
 Sets machine position. Must be sent a 'step' array. More...
 
bool system_xy_at_fixture (coord_system_id_t id, float tolerance)
 Checks if XY position is within coordinate system XY with given tolerance. More...
 
void system_raise_alarm (alarm_code_t alarm)
 Raise and report a system alarm. More...
 
void system_init_switches (void)
 
void system_command_help (void)
 
void system_output_help (const sys_command_t *commands, uint32_t num_commands)
 
void system_register_commands (sys_commands_t *commands)
 
void system_clear_tlo_reference (axes_signals_t homing_cycle)
 Called on homing state changes. More...
 
void system_add_rt_report (report_tracking_t report)
 Set(s) or clear all active realtime report addon flag(s) for the next report. More...
 
report_tracking_flags_t system_get_rt_report_flags (void)
 Get the active realtime report addon flags for the next report. More...
 
void control_interrupt_handler (control_signals_t signals)
 Pin change interrupt handler for pin-out commands, i.e. cycle start, feed hold, reset etc. Mainly sets the realtime command execute variable to have the main program execute these when its ready. This works exactly like the character-based realtime commands when picked off directly from the incoming data stream. More...
 

Variables

system_t sys
 System global variable structure. More...
 

Macro Definition Documentation

◆ EXEC_CYCLE_COMPLETE

#define EXEC_CYCLE_COMPLETE   bit(2)

◆ EXEC_CYCLE_START

#define EXEC_CYCLE_START   bit(1)

◆ EXEC_DOOR_CLOSED

#define EXEC_DOOR_CLOSED   bit(14)

◆ EXEC_FEED_HOLD

#define EXEC_FEED_HOLD   bit(3)

◆ EXEC_GCODE_REPORT

#define EXEC_GCODE_REPORT   bit(11)

◆ EXEC_MOTION_CANCEL

#define EXEC_MOTION_CANCEL   bit(7)

◆ EXEC_PID_REPORT

#define EXEC_PID_REPORT   bit(10)

◆ EXEC_RESET

#define EXEC_RESET   bit(5)

◆ EXEC_RT_COMMAND

#define EXEC_RT_COMMAND   bit(13)

◆ EXEC_SAFETY_DOOR

#define EXEC_SAFETY_DOOR   bit(6)

◆ EXEC_SLEEP

#define EXEC_SLEEP   bit(8)

◆ EXEC_STATUS_REPORT

#define EXEC_STATUS_REPORT   bit(0)

◆ EXEC_STOP

#define EXEC_STOP   bit(4)

◆ EXEC_TLO_REPORT

#define EXEC_TLO_REPORT   bit(12)

◆ EXEC_TOOL_CHANGE

#define EXEC_TOOL_CHANGE   bit(9)

◆ STATE_ALARM

#define STATE_ALARM   bit(0)

In alarm state. Locks out all g-code processes. Allows settings access.

◆ STATE_CHECK_MODE

#define STATE_CHECK_MODE   bit(1)

G-code check mode. Locks out planner and motion only.

◆ STATE_CYCLE

#define STATE_CYCLE   bit(3)

Cycle is running or motions are being executed.

◆ STATE_ESTOP

#define STATE_ESTOP   bit(8)

EStop mode, reports and is mainly handled similar to alarm state.

◆ STATE_HOLD

#define STATE_HOLD   bit(4)

Active feed hold.

◆ STATE_HOMING

#define STATE_HOMING   bit(2)

Performing homing cycle.

◆ STATE_IDLE

#define STATE_IDLE   0

Must be zero. No flags.

◆ STATE_JOG

#define STATE_JOG   bit(5)

Jogging mode.

◆ STATE_SAFETY_DOOR

#define STATE_SAFETY_DOOR   bit(6)

Safety door is ajar. Feed holds and de-energizes system.

◆ STATE_SLEEP

#define STATE_SLEEP   bit(7)

Sleep state.

◆ STATE_TOOL_CHANGE

#define STATE_TOOL_CHANGE   bit(9)

Manual tool change, similar to STATE_HOLD - but stops spindle and allows jogging.

◆ system_clear_exec_alarm

#define system_clear_exec_alarm ( )    hal.set_value_atomic(&sys.rt_exec_alarm, 0)

◆ system_clear_exec_state_flag

#define system_clear_exec_state_flag (   mask)    hal.clear_bits_atomic(&sys.rt_exec_state, (mask))

◆ system_clear_exec_states

#define system_clear_exec_states ( )    hal.set_value_atomic(&sys.rt_exec_state, 0)

◆ system_set_exec_alarm

#define system_set_exec_alarm (   code)    hal.set_value_atomic(&sys.rt_exec_alarm, (uint_fast16_t)(code))

◆ system_set_exec_state_flag

#define system_set_exec_state_flag (   mask)    hal.set_bits_atomic(&sys.rt_exec_state, (mask))

Typedef Documentation

◆ rt_exec_t

typedef uint_fast16_t rt_exec_t

See rt_exec.

◆ sys_command_ptr

typedef status_code_t(* sys_command_ptr) (sys_state_t state, char *args)

◆ sys_commands_t

◆ sys_help_ptr

typedef const char*(* sys_help_ptr) (const char *command)

◆ sys_state_t

typedef uint_fast16_t sys_state_t

See sys_state.

◆ system_t

typedef struct system system_t

Global system variables struct.

Enumeration Type Documentation

◆ hold_state_t

Enumerator
Hold_NotHolding 

0

Hold_Complete 

1

Hold_Pending 

2

◆ machine_mode_t

Enumerator
Mode_Standard 
Mode_Laser 
Mode_Lathe 

◆ parking_state_t

Enumerator
Parking_DoorClosed 

0

Parking_DoorAjar 

1

Parking_Retracting 

2

Parking_Cancel 

3

Parking_Resuming 

4

◆ report_tracking_t

Enumerator
Report_ClearAll 
Report_MPGMode 
Report_Scaling 
Report_Homed 
Report_LatheXMode 
Report_Spindle 
Report_Coolant 
Report_Overrides 
Report_Tool 
Report_WCO 
Report_GWCO 
Report_ToolOffset 
Report_M66Result 
Report_PWM 
Report_Motor 
Report_Encoder 
Report_TLOReference 
Report_Fan 
Report_SpindleId 
Report_ForceWCO 
Report_CycleStart 
Report_All 

Function Documentation

◆ control_interrupt_handler()

void control_interrupt_handler ( control_signals_t  signals)

Pin change interrupt handler for pin-out commands, i.e. cycle start, feed hold, reset etc. Mainly sets the realtime command execute variable to have the main program execute these when its ready. This works exactly like the character-based realtime commands when picked off directly from the incoming data stream.

Parameters
signalsa control_signals_t union holding status of the signals.

◆ system_add_rt_report()

void system_add_rt_report ( report_tracking_t  report)

Set(s) or clear all active realtime report addon flag(s) for the next report.

Fires the grbl.on_rt_reports_added event.

Parameters
reporta report_tracking_t enum containing the flag(s) to set or clear.

◆ system_clear_tlo_reference()

void system_clear_tlo_reference ( axes_signals_t  homing_cycle)

Called on homing state changes.

Clears the tool length offset (TLO) when linear axis or X- or Z-axis is homed in lathe mode. Typically called on the grbl.on_homing complete event.

Parameters
ida axes_signals_t holding the axis flags that homed status was changed for.

◆ system_command_help()

void system_command_help ( void  )

◆ system_convert_array_steps_to_mpos()

void system_convert_array_steps_to_mpos ( float *  position,
int32_t *  steps 
)

Sets machine position. Must be sent a 'step' array.

NOTE: If motor steps and machine position are not in the same coordinate frame, this function serves as a central place to compute the transformation.

Parameters
positionpointer to the target float array for the machine position.
stepspointer to the source step count array to transform.

◆ system_execute_line()

status_code_t system_execute_line ( char *  line)

Directs and executes one line of input from protocol_process.

While mostly incoming streaming g-code blocks, this also executes grblHAL internal commands, such as settings, initiating the homing cycle, and toggling switch states. This differs from the realtime command module by being susceptible to when grblHAL is ready to execute the next line during a cycle, so for switches like block delete, the switch only effects the lines that are processed afterward, not necessarily real-time during a cycle, since there are motions already stored in the buffer. However, this 'lag' should not be an issue, since these commands are not typically used during a cycle. If the command is not known to the core a grbl.on_unknown_sys_command event is raised so that plugin code can check if it is a command it supports.

NOTE: Code calling this function needs to provide the command in a writable buffer since the first part of the command (up to the first = character) is changed to uppercase and having spaces removed.

Parameters
linepointer to the command string.
Returns
status_code_t enum value; Status_OK if successfully handled, another relevant status code if not.

◆ system_execute_startup()

void system_execute_startup ( void  )

Executes user startup scripts, if stored.

◆ system_flag_wco_change()

void system_flag_wco_change ( void  )

Called on a work coordinate (WCO) changes.

If configured waits for the planner buffer to empty then fires the grbl.on_wco_changed event and sets the Report_WCO flag to add the WCO report element to the next status report.

◆ system_get_rt_report_flags()

report_tracking_flags_t system_get_rt_report_flags ( void  )

Get the active realtime report addon flags for the next report.

Returns
a report_tracking_flags_t union containing the flags.

◆ system_init_switches()

void system_init_switches ( void  )

◆ system_output_help()

void system_output_help ( const sys_command_t commands,
uint32_t  num_commands 
)

◆ system_raise_alarm()

void system_raise_alarm ( alarm_code_t  alarm)

Raise and report a system alarm.

Parameters
aalarm_code_t enum representing the alarm code.

◆ system_register_commands()

void system_register_commands ( sys_commands_t commands)

◆ system_xy_at_fixture()

bool system_xy_at_fixture ( coord_system_id_t  id,
float  tolerance 
)

Checks if XY position is within coordinate system XY with given tolerance.

Parameters
ida coord_system_id_t, typically CoordinateSystem_G59_3.
toleranceas the allowed radius the current position has to be within.
Returns
false if tolerance is 0 or position is outside the allowed radius, otherwise true.

Variable Documentation

◆ sys

system_t sys
extern

System global variable structure.