gnea\grbl-Mega  1.0f
Source Code Documentation ( Internal Workings )
system.h File Reference
#include "grbl.h"

Go to the source code of this file.

Data Structures

struct  system_t
 Define global system variables. More...
 

Macros

#define EXEC_STATUS_REPORT   bit(0)
 Define system executor bit map. Used internally by realtime protocol as realtime command flags,. More...
 
#define EXEC_CYCLE_START   bit(1)
 bitmask 00000010 More...
 
#define EXEC_CYCLE_STOP   bit(2)
 bitmask 00000100 More...
 
#define EXEC_FEED_HOLD   bit(3)
 bitmask 00001000 More...
 
#define EXEC_RESET   bit(4)
 bitmask 00010000 More...
 
#define EXEC_SAFETY_DOOR   bit(5)
 bitmask 00100000 More...
 
#define EXEC_MOTION_CANCEL   bit(6)
 bitmask 01000000 More...
 
#define EXEC_SLEEP   bit(7)
 bitmask 10000000 More...
 
#define EXEC_ALARM_HARD_LIMIT   1
 Alarm executor codes. Valid values (1-255). Zero is reserved. More...
 
#define EXEC_ALARM_SOFT_LIMIT   2
 
#define EXEC_ALARM_ABORT_CYCLE   3
 
#define EXEC_ALARM_PROBE_FAIL_INITIAL   4
 
#define EXEC_ALARM_PROBE_FAIL_CONTACT   5
 
#define EXEC_ALARM_HOMING_FAIL_RESET   6
 
#define EXEC_ALARM_HOMING_FAIL_DOOR   7
 
#define EXEC_ALARM_HOMING_FAIL_PULLOFF   8
 
#define EXEC_ALARM_HOMING_FAIL_APPROACH   9
 
#define EXEC_FEED_OVR_RESET   bit(0)
 Override bit maps. Realtime bitflags to control feed, rapid, spindle, and coolant overrides. More...
 
#define EXEC_FEED_OVR_COARSE_PLUS   bit(1)
 
#define EXEC_FEED_OVR_COARSE_MINUS   bit(2)
 
#define EXEC_FEED_OVR_FINE_PLUS   bit(3)
 
#define EXEC_FEED_OVR_FINE_MINUS   bit(4)
 
#define EXEC_RAPID_OVR_RESET   bit(5)
 
#define EXEC_RAPID_OVR_MEDIUM   bit(6)
 
#define EXEC_RAPID_OVR_LOW   bit(7)
 
#define EXEC_SPINDLE_OVR_RESET   bit(0)
 
#define EXEC_SPINDLE_OVR_COARSE_PLUS   bit(1)
 
#define EXEC_SPINDLE_OVR_COARSE_MINUS   bit(2)
 
#define EXEC_SPINDLE_OVR_FINE_PLUS   bit(3)
 
#define EXEC_SPINDLE_OVR_FINE_MINUS   bit(4)
 
#define EXEC_SPINDLE_OVR_STOP   bit(5)
 
#define EXEC_COOLANT_FLOOD_OVR_TOGGLE   bit(6)
 
#define EXEC_COOLANT_MIST_OVR_TOGGLE   bit(7)
 
#define STATE_IDLE   0
 Define system state bit map. The state variable primarily tracks the individual functions. 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 SUSPEND_DISABLE   0
 Define system suspend flags. Used in various ways to manage suspend states and procedures. More...
 
#define SUSPEND_HOLD_COMPLETE   bit(0)
 Indicates initial feed hold is complete. More...
 
#define SUSPEND_RESTART_RETRACT   bit(1)
 Flag to indicate a retract from a restore parking motion. More...
 
#define SUSPEND_RETRACT_COMPLETE   bit(2)
 (Safety door only) Indicates retraction and de-energizing is complete. More...
 
#define SUSPEND_INITIATE_RESTORE   bit(3)
 (Safety door only) Flag to initiate resume procedures from a cycle start. More...
 
#define SUSPEND_RESTORE_COMPLETE   bit(4)
 (Safety door only) Indicates ready to resume normal operation. More...
 
#define SUSPEND_SAFETY_DOOR_AJAR   bit(5)
 Tracks safety door state for resuming. More...
 
#define SUSPEND_MOTION_CANCEL   bit(6)
 Indicates a canceled resume motion. Currently used by probing routine. More...
 
#define SUSPEND_JOG_CANCEL   bit(7)
 Indicates a jog cancel in process and to reset buffers when complete. More...
 
#define STEP_CONTROL_NORMAL_OP   0
 Define step segment generator state flags. More...
 
#define STEP_CONTROL_END_MOTION   bit(0)
 
#define STEP_CONTROL_EXECUTE_HOLD   bit(1)
 
#define STEP_CONTROL_EXECUTE_SYS_MOTION   bit(2)
 
#define STEP_CONTROL_UPDATE_SPINDLE_PWM   bit(3)
 
#define N_CONTROL_PIN   4
 Define control pin index for Grbl internal use. Pin maps may change, but these values don't. More...
 
#define CONTROL_PIN_INDEX_SAFETY_DOOR   bit(0)
 
#define CONTROL_PIN_INDEX_RESET   bit(1)
 
#define CONTROL_PIN_INDEX_FEED_HOLD   bit(2)
 
#define CONTROL_PIN_INDEX_CYCLE_START   bit(3)
 
#define SPINDLE_STOP_OVR_DISABLED   0
 Define spindle stop override control states. More...
 
#define SPINDLE_STOP_OVR_ENABLED   bit(0)
 
#define SPINDLE_STOP_OVR_INITIATE   bit(1)
 
#define SPINDLE_STOP_OVR_RESTORE   bit(2)
 
#define SPINDLE_STOP_OVR_RESTORE_CYCLE   bit(3)
 

Functions

void system_init ()
 Initialize the serial protocol. More...
 
uint8_t system_control_get_state ()
 Returns bitfield of control pin states, organized by CONTROL_PIN_INDEX. (1=triggered, 0=not triggered). More...
 
uint8_t system_check_safety_door_ajar ()
 Returns if safety door is open or closed, based on pin state. More...
 
uint8_t system_execute_line (char *line)
 Executes an internal system command, defined as a string starting with a '$'. More...
 
void system_execute_startup (char *line)
 Execute the startup script lines stored in EEPROM upon initialization. More...
 
void system_flag_wco_change ()
 
float system_convert_axis_steps_to_mpos (int32_t *steps, uint8_t idx)
 Returns machine position of axis 'idx'. Must be sent a 'step' array. More...
 
void system_convert_array_steps_to_mpos (float *position, int32_t *steps)
 Updates a machine 'position' array based on the 'step' array sent. More...
 
uint8_t system_check_travel_limits (float *target)
 CoreXY calculation only. Returns x or y-axis "steps" based on CoreXY motor steps. More...
 
void system_set_exec_state_flag (uint8_t mask)
 Special handlers for setting and clearing Grbl's real-time execution flags. More...
 
void system_clear_exec_state_flag (uint8_t mask)
 
void system_set_exec_alarm (uint8_t code)
 
void system_clear_exec_alarm ()
 
void system_set_exec_motion_override_flag (uint8_t mask)
 
void system_set_exec_accessory_override_flag (uint8_t mask)
 
void system_clear_exec_motion_overrides ()
 
void system_clear_exec_accessory_overrides ()
 

Variables

system_t sys
 Declare system global variable structure. More...
 
int32_t sys_position [N_AXIS]
 NOTE: These position variables may need to be declared as volatiles, if problems arise. More...
 
int32_t sys_probe_position [N_AXIS]
 Last probe position in machine coordinates and steps. More...
 
volatile uint8_t sys_probe_state
 Probing state value. Used to coordinate the probing cycle with stepper ISR. More...
 
volatile uint8_t sys_rt_exec_state
 Global realtime executor bitflag variable for state management. See EXEC bitmasks. More...
 
volatile uint8_t sys_rt_exec_alarm
 Global realtime executor bitflag variable for setting various alarms. More...
 
volatile uint8_t sys_rt_exec_motion_override
 Global realtime executor bitflag variable for motion-based overrides. More...
 
volatile uint8_t sys_rt_exec_accessory_override
 Global realtime executor bitflag variable for spindle/coolant overrides. More...
 

Data Structure Documentation

struct system_t

Define global system variables.

Definition at line 111 of file system.h.

Data Fields
uint8_t abort System abort flag. Forces exit back to main loop for reset.
uint8_t f_override Feed rate override value in percent.
uint8_t homing_axis_lock Locks axes when limits engage. Used as an axis motion mask in the stepper ISR.
uint8_t probe_succeeded Tracks if last probing cycle was successful.
uint8_t r_override Rapids override value in percent.
uint8_t report_ovr_counter Tracks when to add override data to status reports.
uint8_t report_wco_counter Tracks when to add work coordinate offset data to status reports.
uint8_t soft_limit Tracks soft limit errors for the state machine. (boolean)
float spindle_speed
uint8_t spindle_speed_ovr Spindle speed value in percent.
uint8_t spindle_stop_ovr Tracks spindle stop override states.
uint8_t state Tracks the current system state of Grbl.
uint8_t step_control Governs the step segment generator depending on system state.
uint8_t suspend System suspend bitflag variable that manages holds, cancels, and safety door.

Macro Definition Documentation

#define CONTROL_PIN_INDEX_CYCLE_START   bit(3)

Definition at line 102 of file system.h.

#define CONTROL_PIN_INDEX_FEED_HOLD   bit(2)

Definition at line 101 of file system.h.

#define CONTROL_PIN_INDEX_RESET   bit(1)

Definition at line 100 of file system.h.

#define CONTROL_PIN_INDEX_SAFETY_DOOR   bit(0)

Definition at line 99 of file system.h.

#define EXEC_ALARM_ABORT_CYCLE   3

Definition at line 42 of file system.h.

#define EXEC_ALARM_HARD_LIMIT   1

Alarm executor codes. Valid values (1-255). Zero is reserved.

Definition at line 40 of file system.h.

#define EXEC_ALARM_HOMING_FAIL_APPROACH   9

Definition at line 48 of file system.h.

#define EXEC_ALARM_HOMING_FAIL_DOOR   7

Definition at line 46 of file system.h.

#define EXEC_ALARM_HOMING_FAIL_PULLOFF   8

Definition at line 47 of file system.h.

#define EXEC_ALARM_HOMING_FAIL_RESET   6

Definition at line 45 of file system.h.

#define EXEC_ALARM_PROBE_FAIL_CONTACT   5

Definition at line 44 of file system.h.

#define EXEC_ALARM_PROBE_FAIL_INITIAL   4

Definition at line 43 of file system.h.

#define EXEC_ALARM_SOFT_LIMIT   2

Definition at line 41 of file system.h.

#define EXEC_COOLANT_FLOOD_OVR_TOGGLE   bit(6)

Definition at line 67 of file system.h.

#define EXEC_COOLANT_MIST_OVR_TOGGLE   bit(7)

Definition at line 68 of file system.h.

#define EXEC_CYCLE_START   bit(1)

bitmask 00000010

Definition at line 32 of file system.h.

#define EXEC_CYCLE_STOP   bit(2)

bitmask 00000100

Definition at line 33 of file system.h.

#define EXEC_FEED_HOLD   bit(3)

bitmask 00001000

Definition at line 34 of file system.h.

#define EXEC_FEED_OVR_COARSE_MINUS   bit(2)

Definition at line 53 of file system.h.

#define EXEC_FEED_OVR_COARSE_PLUS   bit(1)

Definition at line 52 of file system.h.

#define EXEC_FEED_OVR_FINE_MINUS   bit(4)

Definition at line 55 of file system.h.

#define EXEC_FEED_OVR_FINE_PLUS   bit(3)

Definition at line 54 of file system.h.

#define EXEC_FEED_OVR_RESET   bit(0)

Override bit maps. Realtime bitflags to control feed, rapid, spindle, and coolant overrides.

Definition at line 51 of file system.h.

#define EXEC_MOTION_CANCEL   bit(6)

bitmask 01000000

Definition at line 37 of file system.h.

#define EXEC_RAPID_OVR_LOW   bit(7)

Definition at line 58 of file system.h.

#define EXEC_RAPID_OVR_MEDIUM   bit(6)

Definition at line 57 of file system.h.

#define EXEC_RAPID_OVR_RESET   bit(5)

Definition at line 56 of file system.h.

#define EXEC_RESET   bit(4)

bitmask 00010000

Definition at line 35 of file system.h.

#define EXEC_SAFETY_DOOR   bit(5)

bitmask 00100000

Definition at line 36 of file system.h.

#define EXEC_SLEEP   bit(7)

bitmask 10000000

Definition at line 38 of file system.h.

#define EXEC_SPINDLE_OVR_COARSE_MINUS   bit(2)

Definition at line 63 of file system.h.

#define EXEC_SPINDLE_OVR_COARSE_PLUS   bit(1)

Definition at line 62 of file system.h.

#define EXEC_SPINDLE_OVR_FINE_MINUS   bit(4)

Definition at line 65 of file system.h.

#define EXEC_SPINDLE_OVR_FINE_PLUS   bit(3)

Definition at line 64 of file system.h.

#define EXEC_SPINDLE_OVR_RESET   bit(0)

Definition at line 61 of file system.h.

#define EXEC_SPINDLE_OVR_STOP   bit(5)

Definition at line 66 of file system.h.

#define EXEC_STATUS_REPORT   bit(0)

Define system executor bit map. Used internally by realtime protocol as realtime command flags,.

NOTE: The system executor uses an unsigned 8-bit volatile variable (8 flag limit.) The default bitmask 00000001

Definition at line 31 of file system.h.

#define N_CONTROL_PIN   4

Define control pin index for Grbl internal use. Pin maps may change, but these values don't.

Definition at line 98 of file system.h.

#define SPINDLE_STOP_OVR_DISABLED   0

Define spindle stop override control states.

Must be zero.

Definition at line 104 of file system.h.

#define SPINDLE_STOP_OVR_ENABLED   bit(0)

Definition at line 105 of file system.h.

#define SPINDLE_STOP_OVR_INITIATE   bit(1)

Definition at line 106 of file system.h.

#define SPINDLE_STOP_OVR_RESTORE   bit(2)

Definition at line 107 of file system.h.

#define SPINDLE_STOP_OVR_RESTORE_CYCLE   bit(3)

Definition at line 108 of file system.h.

#define STATE_ALARM   bit(0)

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

Definition at line 73 of file system.h.

#define STATE_CHECK_MODE   bit(1)

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

Definition at line 74 of file system.h.

#define STATE_CYCLE   bit(3)

Cycle is running or motions are being executed.

Definition at line 76 of file system.h.

#define STATE_HOLD   bit(4)

Active feed hold.

Definition at line 77 of file system.h.

#define STATE_HOMING   bit(2)

Performing homing cycle.

Definition at line 75 of file system.h.

#define STATE_IDLE   0

Define system state bit map. The state variable primarily tracks the individual functions.

Must be zero. No flags.

Definition at line 72 of file system.h.

#define STATE_JOG   bit(5)

Jogging mode.

Definition at line 78 of file system.h.

#define STATE_SAFETY_DOOR   bit(6)

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

Definition at line 79 of file system.h.

#define STATE_SLEEP   bit(7)

Sleep state.

Definition at line 80 of file system.h.

#define STEP_CONTROL_END_MOTION   bit(0)

Definition at line 93 of file system.h.

#define STEP_CONTROL_EXECUTE_HOLD   bit(1)

Definition at line 94 of file system.h.

#define STEP_CONTROL_EXECUTE_SYS_MOTION   bit(2)

Definition at line 95 of file system.h.

#define STEP_CONTROL_NORMAL_OP   0

Define step segment generator state flags.

Must be zero.

Definition at line 92 of file system.h.

#define STEP_CONTROL_UPDATE_SPINDLE_PWM   bit(3)

Definition at line 96 of file system.h.

#define SUSPEND_DISABLE   0

Define system suspend flags. Used in various ways to manage suspend states and procedures.

Must be zero.

Definition at line 82 of file system.h.

#define SUSPEND_HOLD_COMPLETE   bit(0)

Indicates initial feed hold is complete.

Definition at line 83 of file system.h.

#define SUSPEND_INITIATE_RESTORE   bit(3)

(Safety door only) Flag to initiate resume procedures from a cycle start.

Definition at line 86 of file system.h.

#define SUSPEND_JOG_CANCEL   bit(7)

Indicates a jog cancel in process and to reset buffers when complete.

Definition at line 90 of file system.h.

#define SUSPEND_MOTION_CANCEL   bit(6)

Indicates a canceled resume motion. Currently used by probing routine.

Definition at line 89 of file system.h.

#define SUSPEND_RESTART_RETRACT   bit(1)

Flag to indicate a retract from a restore parking motion.

Definition at line 84 of file system.h.

#define SUSPEND_RESTORE_COMPLETE   bit(4)

(Safety door only) Indicates ready to resume normal operation.

Definition at line 87 of file system.h.

#define SUSPEND_RETRACT_COMPLETE   bit(2)

(Safety door only) Indicates retraction and de-energizing is complete.

Definition at line 85 of file system.h.

#define SUSPEND_SAFETY_DOOR_AJAR   bit(5)

Tracks safety door state for resuming.

Definition at line 88 of file system.h.

Function Documentation

uint8_t system_check_safety_door_ajar ( )

Returns if safety door is open or closed, based on pin state.

Returns if safety door is open or closed, based on pin state.

Definition at line 76 of file system.c.

uint8_t system_check_travel_limits ( float *  target)

CoreXY calculation only. Returns x or y-axis "steps" based on CoreXY motor steps.

Checks and reports if target array exceeds machine travel limits.

NOTE: max_travel is stored as negative

Definition at line 317 of file system.c.

void system_clear_exec_accessory_overrides ( )

Definition at line 389 of file system.c.

void system_clear_exec_alarm ( )

Definition at line 361 of file system.c.

void system_clear_exec_motion_overrides ( )

Definition at line 382 of file system.c.

void system_clear_exec_state_flag ( uint8_t  mask)

Definition at line 347 of file system.c.

uint8_t system_control_get_state ( )

Returns bitfield of control pin states, organized by CONTROL_PIN_INDEX. (1=triggered, 0=not triggered).

Returns bitfield of control pin states, organized by CONTROL_PIN_INDEX. (1=triggered, 0=not triggered).

Definition at line 39 of file system.c.

void system_convert_array_steps_to_mpos ( float *  position,
int32_t *  steps 
)

Updates a machine 'position' array based on the 'step' array sent.

Definition at line 295 of file system.c.

float system_convert_axis_steps_to_mpos ( int32_t *  steps,
uint8_t  idx 
)

Returns machine position of axis 'idx'. Must be sent a 'step' array.

NOTE: If motor steps and machine position are not in the same coordinate frame, this function

Definition at line 277 of file system.c.

uint8_t system_execute_line ( char *  line)

Executes an internal system command, defined as a string starting with a '$'.

Executes an internal system command, defined as a string starting with a '$'.

< Helper variable

< Jogging

<

NOTE: $J= is ignored inside g-code parser and used to detect jog motions.

< Prints Grbl settings

< Prints gcode parser state

Todo:
Move this to realtime commands for GUIs to request this data during suspend-state.

< Set check g-code mode [IDLE/CHECK]

< Disable alarm lock [ALARM]

< Print Grbl NGC parameters

< Perform homing cycle [IDLE/ALARM]

< Set system state variable

< Execute startup scripts after successful homing.

< Set to IDLE when complete.

< Set steppers to the settings idle state before returning.

< Puts Grbl to sleep [IDLE/ALARM]

< Set to execute sleep mode immediately

< Print or store build info. [IDLE/ALARM]

< Store startup line [IDLE/ALARM]

< Set helper variable as counter to start of user info line.

< Restore defaults [IDLE/ALARM]

< Force reset to ensure settings are initialized correctly.

< Startup lines. [IDLE/ALARM]

< Print startup lines

< Store startup line [IDLE Only] Prevents motion during ALARM.

< Set helper_var to flag storing method.

< Storing setting methods [IDLE/ALARM]

< Store startup line

< Set helper variable as counter to start of gcode block

< Set helper_var to returned status code.

< Set helper_var to int value of parameter

< Store global setting.

< If '$' command makes it to here, then everything's ok.

Definition at line 106 of file system.c.

void system_execute_startup ( char *  line)

Execute the startup script lines stored in EEPROM upon initialization.

Execute the startup script lines stored in EEPROM upon initialization.

Definition at line 82 of file system.c.

void system_flag_wco_change ( )

Definition at line 265 of file system.c.

void system_init ( )

Initialize the serial protocol.

< Configure as input pins

< Enable internal pull-up resistors. Normal high operation.

< Enable specific pins of the Pin Change Interrupt

< Enable Pin Change Interrupt

Definition at line 24 of file system.c.

void system_set_exec_accessory_override_flag ( uint8_t  mask)

Definition at line 375 of file system.c.

void system_set_exec_alarm ( uint8_t  code)

Definition at line 354 of file system.c.

void system_set_exec_motion_override_flag ( uint8_t  mask)

Definition at line 368 of file system.c.

void system_set_exec_state_flag ( uint8_t  mask)

Special handlers for setting and clearing Grbl's real-time execution flags.

Definition at line 340 of file system.c.

Variable Documentation

system_t sys

Declare system global variable structure.

Definition at line 25 of file main.c.

int32_t sys_position[N_AXIS]

NOTE: These position variables may need to be declared as volatiles, if problems arise.

Real-time machine (aka home) position vector in steps.

Definition at line 130 of file system.h.

int32_t sys_probe_position[N_AXIS]

Last probe position in machine coordinates and steps.

Definition at line 131 of file system.h.

volatile uint8_t sys_probe_state

Probing state value. Used to coordinate the probing cycle with stepper ISR.

Definition at line 133 of file system.h.

volatile uint8_t sys_rt_exec_accessory_override

Global realtime executor bitflag variable for spindle/coolant overrides.

Definition at line 137 of file system.h.

volatile uint8_t sys_rt_exec_alarm

Global realtime executor bitflag variable for setting various alarms.

Definition at line 135 of file system.h.

volatile uint8_t sys_rt_exec_motion_override

Global realtime executor bitflag variable for motion-based overrides.

Definition at line 136 of file system.h.

volatile uint8_t sys_rt_exec_state

Global realtime executor bitflag variable for state management. See EXEC bitmasks.

Definition at line 134 of file system.h.