User | Revision | Line number | New contents of line |
Sergunb |
0:8f0d870509fe
|
1
|
/*
|
Sergunb |
0:8f0d870509fe
|
2
|
system.h - Header for system level commands and real-time processes
|
Sergunb |
0:8f0d870509fe
|
3
|
Part of Grbl
|
Sergunb |
0:8f0d870509fe
|
4
|
|
Sergunb |
0:8f0d870509fe
|
5
|
Copyright (c) 2014-2016 Sungeun K. Jeon for Gnea Research LLC
|
Sergunb |
0:8f0d870509fe
|
6
|
|
Sergunb |
0:8f0d870509fe
|
7
|
Grbl is free software: you can redistribute it and/or modify
|
Sergunb |
0:8f0d870509fe
|
8
|
it under the terms of the GNU General Public License as published by
|
Sergunb |
0:8f0d870509fe
|
9
|
the Free Software Foundation, either version 3 of the License, or
|
Sergunb |
0:8f0d870509fe
|
10
|
(at your option) any later version.
|
Sergunb |
0:8f0d870509fe
|
11
|
|
Sergunb |
0:8f0d870509fe
|
12
|
Grbl is distributed in the hope that it will be useful,
|
Sergunb |
0:8f0d870509fe
|
13
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Sergunb |
0:8f0d870509fe
|
14
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Sergunb |
0:8f0d870509fe
|
15
|
GNU General Public License for more details.
|
Sergunb |
0:8f0d870509fe
|
16
|
|
Sergunb |
0:8f0d870509fe
|
17
|
You should have received a copy of the GNU General Public License
|
Sergunb |
0:8f0d870509fe
|
18
|
along with Grbl. If not, see <http://www.gnu.org/licenses/>.
|
Sergunb |
0:8f0d870509fe
|
19
|
*/
|
Sergunb |
0:8f0d870509fe
|
20
|
|
Sergunb |
0:8f0d870509fe
|
21
|
#ifndef system_h
|
Sergunb |
0:8f0d870509fe
|
22
|
#define system_h
|
Sergunb |
0:8f0d870509fe
|
23
|
|
Sergunb |
0:8f0d870509fe
|
24
|
#include "grbl.h"
|
Sergunb |
0:8f0d870509fe
|
25
|
|
Sergunb |
0:8f0d870509fe
|
26
|
// Define system executor bit map. Used internally by realtime protocol as realtime command flags,
|
Sergunb |
0:8f0d870509fe
|
27
|
// which notifies the main program to execute the specified realtime command asynchronously.
|
Sergunb |
0:8f0d870509fe
|
28
|
// NOTE: The system executor uses an unsigned 8-bit volatile variable (8 flag limit.) The default
|
Sergunb |
0:8f0d870509fe
|
29
|
// flags are always false, so the realtime protocol only needs to check for a non-zero value to
|
Sergunb |
0:8f0d870509fe
|
30
|
// know when there is a realtime command to execute.
|
Sergunb |
0:8f0d870509fe
|
31
|
#define EXEC_STATUS_REPORT bit(0) // bitmask 00000001
|
Sergunb |
0:8f0d870509fe
|
32
|
#define EXEC_CYCLE_START bit(1) // bitmask 00000010
|
Sergunb |
0:8f0d870509fe
|
33
|
#define EXEC_CYCLE_STOP bit(2) // bitmask 00000100
|
Sergunb |
0:8f0d870509fe
|
34
|
#define EXEC_FEED_HOLD bit(3) // bitmask 00001000
|
Sergunb |
0:8f0d870509fe
|
35
|
#define EXEC_RESET bit(4) // bitmask 00010000
|
Sergunb |
0:8f0d870509fe
|
36
|
#define EXEC_SAFETY_DOOR bit(5) // bitmask 00100000
|
Sergunb |
0:8f0d870509fe
|
37
|
#define EXEC_MOTION_CANCEL bit(6) // bitmask 01000000
|
Sergunb |
0:8f0d870509fe
|
38
|
#define EXEC_SLEEP bit(7) // bitmask 10000000
|
Sergunb |
0:8f0d870509fe
|
39
|
|
Sergunb |
0:8f0d870509fe
|
40
|
// Alarm executor codes. Valid values (1-255). Zero is reserved.
|
Sergunb |
0:8f0d870509fe
|
41
|
#define EXEC_ALARM_HARD_LIMIT 1
|
Sergunb |
0:8f0d870509fe
|
42
|
#define EXEC_ALARM_SOFT_LIMIT 2
|
Sergunb |
0:8f0d870509fe
|
43
|
#define EXEC_ALARM_ABORT_CYCLE 3
|
Sergunb |
0:8f0d870509fe
|
44
|
#define EXEC_ALARM_PROBE_FAIL_INITIAL 4
|
Sergunb |
0:8f0d870509fe
|
45
|
#define EXEC_ALARM_PROBE_FAIL_CONTACT 5
|
Sergunb |
0:8f0d870509fe
|
46
|
#define EXEC_ALARM_HOMING_FAIL_RESET 6
|
Sergunb |
0:8f0d870509fe
|
47
|
#define EXEC_ALARM_HOMING_FAIL_DOOR 7
|
Sergunb |
0:8f0d870509fe
|
48
|
#define EXEC_ALARM_HOMING_FAIL_PULLOFF 8
|
Sergunb |
0:8f0d870509fe
|
49
|
#define EXEC_ALARM_HOMING_FAIL_APPROACH 9
|
Sergunb |
0:8f0d870509fe
|
50
|
|
Sergunb |
0:8f0d870509fe
|
51
|
// Override bit maps. Realtime bitflags to control feed, rapid, spindle, and coolant overrides.
|
Sergunb |
0:8f0d870509fe
|
52
|
// Spindle/coolant and feed/rapids are separated into two controlling flag variables.
|
Sergunb |
0:8f0d870509fe
|
53
|
#define EXEC_FEED_OVR_RESET bit(0)
|
Sergunb |
0:8f0d870509fe
|
54
|
#define EXEC_FEED_OVR_COARSE_PLUS bit(1)
|
Sergunb |
0:8f0d870509fe
|
55
|
#define EXEC_FEED_OVR_COARSE_MINUS bit(2)
|
Sergunb |
0:8f0d870509fe
|
56
|
#define EXEC_FEED_OVR_FINE_PLUS bit(3)
|
Sergunb |
0:8f0d870509fe
|
57
|
#define EXEC_FEED_OVR_FINE_MINUS bit(4)
|
Sergunb |
0:8f0d870509fe
|
58
|
#define EXEC_RAPID_OVR_RESET bit(5)
|
Sergunb |
0:8f0d870509fe
|
59
|
#define EXEC_RAPID_OVR_MEDIUM bit(6)
|
Sergunb |
0:8f0d870509fe
|
60
|
#define EXEC_RAPID_OVR_LOW bit(7)
|
Sergunb |
0:8f0d870509fe
|
61
|
// #define EXEC_RAPID_OVR_EXTRA_LOW bit(*) // *NOT SUPPORTED*
|
Sergunb |
0:8f0d870509fe
|
62
|
|
Sergunb |
0:8f0d870509fe
|
63
|
#define EXEC_SPINDLE_OVR_RESET bit(0)
|
Sergunb |
0:8f0d870509fe
|
64
|
#define EXEC_SPINDLE_OVR_COARSE_PLUS bit(1)
|
Sergunb |
0:8f0d870509fe
|
65
|
#define EXEC_SPINDLE_OVR_COARSE_MINUS bit(2)
|
Sergunb |
0:8f0d870509fe
|
66
|
#define EXEC_SPINDLE_OVR_FINE_PLUS bit(3)
|
Sergunb |
0:8f0d870509fe
|
67
|
#define EXEC_SPINDLE_OVR_FINE_MINUS bit(4)
|
Sergunb |
0:8f0d870509fe
|
68
|
#define EXEC_SPINDLE_OVR_STOP bit(5)
|
Sergunb |
0:8f0d870509fe
|
69
|
#define EXEC_COOLANT_FLOOD_OVR_TOGGLE bit(6)
|
Sergunb |
0:8f0d870509fe
|
70
|
#define EXEC_COOLANT_MIST_OVR_TOGGLE bit(7)
|
Sergunb |
0:8f0d870509fe
|
71
|
|
Sergunb |
0:8f0d870509fe
|
72
|
// Define system state bit map. The state variable primarily tracks the individual functions
|
Sergunb |
0:8f0d870509fe
|
73
|
// of Grbl to manage each without overlapping. It is also used as a messaging flag for
|
Sergunb |
0:8f0d870509fe
|
74
|
// critical events.
|
Sergunb |
0:8f0d870509fe
|
75
|
#define STATE_IDLE 0 // Must be zero. No flags.
|
Sergunb |
0:8f0d870509fe
|
76
|
#define STATE_ALARM bit(0) // In alarm state. Locks out all g-code processes. Allows settings access.
|
Sergunb |
0:8f0d870509fe
|
77
|
#define STATE_CHECK_MODE bit(1) // G-code check mode. Locks out planner and motion only.
|
Sergunb |
0:8f0d870509fe
|
78
|
#define STATE_HOMING bit(2) // Performing homing cycle
|
Sergunb |
0:8f0d870509fe
|
79
|
#define STATE_CYCLE bit(3) // Cycle is running or motions are being executed.
|
Sergunb |
0:8f0d870509fe
|
80
|
#define STATE_HOLD bit(4) // Active feed hold
|
Sergunb |
0:8f0d870509fe
|
81
|
#define STATE_JOG bit(5) // Jogging mode.
|
Sergunb |
0:8f0d870509fe
|
82
|
#define STATE_SAFETY_DOOR bit(6) // Safety door is ajar. Feed holds and de-energizes system.
|
Sergunb |
0:8f0d870509fe
|
83
|
#define STATE_SLEEP bit(7) // Sleep state.
|
Sergunb |
0:8f0d870509fe
|
84
|
|
Sergunb |
0:8f0d870509fe
|
85
|
// Define system suspend flags. Used in various ways to manage suspend states and procedures.
|
Sergunb |
0:8f0d870509fe
|
86
|
#define SUSPEND_DISABLE 0 // Must be zero.
|
Sergunb |
0:8f0d870509fe
|
87
|
#define SUSPEND_HOLD_COMPLETE bit(0) // Indicates initial feed hold is complete.
|
Sergunb |
0:8f0d870509fe
|
88
|
#define SUSPEND_RESTART_RETRACT bit(1) // Flag to indicate a retract from a restore parking motion.
|
Sergunb |
0:8f0d870509fe
|
89
|
#define SUSPEND_RETRACT_COMPLETE bit(2) // (Safety door only) Indicates retraction and de-energizing is complete.
|
Sergunb |
0:8f0d870509fe
|
90
|
#define SUSPEND_INITIATE_RESTORE bit(3) // (Safety door only) Flag to initiate resume procedures from a cycle start.
|
Sergunb |
0:8f0d870509fe
|
91
|
#define SUSPEND_RESTORE_COMPLETE bit(4) // (Safety door only) Indicates ready to resume normal operation.
|
Sergunb |
0:8f0d870509fe
|
92
|
#define SUSPEND_SAFETY_DOOR_AJAR bit(5) // Tracks safety door state for resuming.
|
Sergunb |
0:8f0d870509fe
|
93
|
#define SUSPEND_MOTION_CANCEL bit(6) // Indicates a canceled resume motion. Currently used by probing routine.
|
Sergunb |
0:8f0d870509fe
|
94
|
#define SUSPEND_JOG_CANCEL bit(7) // Indicates a jog cancel in process and to reset buffers when complete.
|
Sergunb |
0:8f0d870509fe
|
95
|
|
Sergunb |
0:8f0d870509fe
|
96
|
// Define step segment generator state flags.
|
Sergunb |
0:8f0d870509fe
|
97
|
#define STEP_CONTROL_NORMAL_OP 0 // Must be zero.
|
Sergunb |
0:8f0d870509fe
|
98
|
#define STEP_CONTROL_END_MOTION bit(0)
|
Sergunb |
0:8f0d870509fe
|
99
|
#define STEP_CONTROL_EXECUTE_HOLD bit(1)
|
Sergunb |
0:8f0d870509fe
|
100
|
#define STEP_CONTROL_EXECUTE_SYS_MOTION bit(2)
|
Sergunb |
0:8f0d870509fe
|
101
|
#define STEP_CONTROL_UPDATE_SPINDLE_PWM bit(3)
|
Sergunb |
0:8f0d870509fe
|
102
|
|
Sergunb |
0:8f0d870509fe
|
103
|
// Define control pin index for Grbl internal use. Pin maps may change, but these values don't.
|
Sergunb |
0:8f0d870509fe
|
104
|
#ifdef ENABLE_SAFETY_DOOR_INPUT_PIN
|
Sergunb |
0:8f0d870509fe
|
105
|
#define N_CONTROL_PIN 4
|
Sergunb |
0:8f0d870509fe
|
106
|
#define CONTROL_PIN_INDEX_SAFETY_DOOR bit(0)
|
Sergunb |
0:8f0d870509fe
|
107
|
#define CONTROL_PIN_INDEX_RESET bit(1)
|
Sergunb |
0:8f0d870509fe
|
108
|
#define CONTROL_PIN_INDEX_FEED_HOLD bit(2)
|
Sergunb |
0:8f0d870509fe
|
109
|
#define CONTROL_PIN_INDEX_CYCLE_START bit(3)
|
Sergunb |
0:8f0d870509fe
|
110
|
#else
|
Sergunb |
0:8f0d870509fe
|
111
|
#define N_CONTROL_PIN 3
|
Sergunb |
0:8f0d870509fe
|
112
|
#define CONTROL_PIN_INDEX_RESET bit(0)
|
Sergunb |
0:8f0d870509fe
|
113
|
#define CONTROL_PIN_INDEX_FEED_HOLD bit(1)
|
Sergunb |
0:8f0d870509fe
|
114
|
#define CONTROL_PIN_INDEX_CYCLE_START bit(2)
|
Sergunb |
0:8f0d870509fe
|
115
|
#endif
|
Sergunb |
0:8f0d870509fe
|
116
|
|
Sergunb |
0:8f0d870509fe
|
117
|
// Define spindle stop override control states.
|
Sergunb |
0:8f0d870509fe
|
118
|
#define SPINDLE_STOP_OVR_DISABLED 0 // Must be zero.
|
Sergunb |
0:8f0d870509fe
|
119
|
#define SPINDLE_STOP_OVR_ENABLED bit(0)
|
Sergunb |
0:8f0d870509fe
|
120
|
#define SPINDLE_STOP_OVR_INITIATE bit(1)
|
Sergunb |
0:8f0d870509fe
|
121
|
#define SPINDLE_STOP_OVR_RESTORE bit(2)
|
Sergunb |
0:8f0d870509fe
|
122
|
#define SPINDLE_STOP_OVR_RESTORE_CYCLE bit(3)
|
Sergunb |
0:8f0d870509fe
|
123
|
|
Sergunb |
0:8f0d870509fe
|
124
|
|
Sergunb |
0:8f0d870509fe
|
125
|
// Define global system variables
|
Sergunb |
0:8f0d870509fe
|
126
|
typedef struct {
|
Sergunb |
0:8f0d870509fe
|
127
|
uint8_t state; // Tracks the current system state of Grbl.
|
Sergunb |
0:8f0d870509fe
|
128
|
uint8_t abort; // System abort flag. Forces exit back to main loop for reset.
|
Sergunb |
0:8f0d870509fe
|
129
|
uint8_t suspend; // System suspend bitflag variable that manages holds, cancels, and safety door.
|
Sergunb |
0:8f0d870509fe
|
130
|
uint8_t soft_limit; // Tracks soft limit errors for the state machine. (boolean)
|
Sergunb |
0:8f0d870509fe
|
131
|
uint8_t step_control; // Governs the step segment generator depending on system state.
|
Sergunb |
0:8f0d870509fe
|
132
|
uint8_t probe_succeeded; // Tracks if last probing cycle was successful.
|
Sergunb |
0:8f0d870509fe
|
133
|
uint8_t homing_axis_lock; // Locks axes when limits engage. Used as an axis motion mask in the stepper ISR.
|
Sergunb |
0:8f0d870509fe
|
134
|
uint8_t f_override; // Feed rate override value in percent
|
Sergunb |
0:8f0d870509fe
|
135
|
uint8_t r_override; // Rapids override value in percent
|
Sergunb |
0:8f0d870509fe
|
136
|
uint8_t spindle_speed_ovr; // Spindle speed value in percent
|
Sergunb |
0:8f0d870509fe
|
137
|
uint8_t spindle_stop_ovr; // Tracks spindle stop override states
|
Sergunb |
0:8f0d870509fe
|
138
|
uint8_t report_ovr_counter; // Tracks when to add override data to status reports.
|
Sergunb |
0:8f0d870509fe
|
139
|
uint8_t report_wco_counter; // Tracks when to add work coordinate offset data to status reports.
|
Sergunb |
0:8f0d870509fe
|
140
|
#ifdef ENABLE_PARKING_OVERRIDE_CONTROL
|
Sergunb |
0:8f0d870509fe
|
141
|
uint8_t override_ctrl; // Tracks override control states.
|
Sergunb |
0:8f0d870509fe
|
142
|
#endif
|
Sergunb |
0:8f0d870509fe
|
143
|
#ifdef VARIABLE_SPINDLE
|
Sergunb |
0:8f0d870509fe
|
144
|
float spindle_speed;
|
Sergunb |
0:8f0d870509fe
|
145
|
#endif
|
Sergunb |
0:8f0d870509fe
|
146
|
} system_t;
|
Sergunb |
0:8f0d870509fe
|
147
|
extern system_t sys;
|
Sergunb |
0:8f0d870509fe
|
148
|
|
Sergunb |
0:8f0d870509fe
|
149
|
// NOTE: These position variables may need to be declared as volatiles, if problems arise.
|
Sergunb |
0:8f0d870509fe
|
150
|
extern int32_t sys_position[N_AXIS]; // Real-time machine (aka home) position vector in steps.
|
Sergunb |
0:8f0d870509fe
|
151
|
extern int32_t sys_probe_position[N_AXIS]; // Last probe position in machine coordinates and steps.
|
Sergunb |
0:8f0d870509fe
|
152
|
|
Sergunb |
0:8f0d870509fe
|
153
|
extern volatile uint8_t sys_probe_state; // Probing state value. Used to coordinate the probing cycle with stepper ISR.
|
Sergunb |
0:8f0d870509fe
|
154
|
extern volatile uint8_t sys_rt_exec_state; // Global realtime executor bitflag variable for state management. See EXEC bitmasks.
|
Sergunb |
0:8f0d870509fe
|
155
|
extern volatile uint8_t sys_rt_exec_alarm; // Global realtime executor bitflag variable for setting various alarms.
|
Sergunb |
0:8f0d870509fe
|
156
|
extern volatile uint8_t sys_rt_exec_motion_override; // Global realtime executor bitflag variable for motion-based overrides.
|
Sergunb |
0:8f0d870509fe
|
157
|
extern volatile uint8_t sys_rt_exec_accessory_override; // Global realtime executor bitflag variable for spindle/coolant overrides.
|
Sergunb |
0:8f0d870509fe
|
158
|
|
Sergunb |
0:8f0d870509fe
|
159
|
#ifdef DEBUG
|
Sergunb |
0:8f0d870509fe
|
160
|
#define EXEC_DEBUG_REPORT bit(0)
|
Sergunb |
0:8f0d870509fe
|
161
|
extern volatile uint8_t sys_rt_exec_debug;
|
Sergunb |
0:8f0d870509fe
|
162
|
#endif
|
Sergunb |
0:8f0d870509fe
|
163
|
|
Sergunb |
0:8f0d870509fe
|
164
|
// Initialize the serial protocol
|
Sergunb |
0:8f0d870509fe
|
165
|
void system_init();
|
Sergunb |
0:8f0d870509fe
|
166
|
|
Sergunb |
0:8f0d870509fe
|
167
|
// Returns bitfield of control pin states, organized by CONTROL_PIN_INDEX. (1=triggered, 0=not triggered).
|
Sergunb |
0:8f0d870509fe
|
168
|
uint8_t system_control_get_state();
|
Sergunb |
0:8f0d870509fe
|
169
|
|
Sergunb |
0:8f0d870509fe
|
170
|
// Returns if safety door is open or closed, based on pin state.
|
Sergunb |
0:8f0d870509fe
|
171
|
uint8_t system_check_safety_door_ajar();
|
Sergunb |
0:8f0d870509fe
|
172
|
|
Sergunb |
0:8f0d870509fe
|
173
|
// Executes an internal system command, defined as a string starting with a '$'
|
Sergunb |
0:8f0d870509fe
|
174
|
uint8_t system_execute_line(char *line);
|
Sergunb |
0:8f0d870509fe
|
175
|
|
Sergunb |
0:8f0d870509fe
|
176
|
// Execute the startup script lines stored in EEPROM upon initialization
|
Sergunb |
0:8f0d870509fe
|
177
|
void system_execute_startup(char *line);
|
Sergunb |
0:8f0d870509fe
|
178
|
|
Sergunb |
0:8f0d870509fe
|
179
|
|
Sergunb |
0:8f0d870509fe
|
180
|
void system_flag_wco_change();
|
Sergunb |
0:8f0d870509fe
|
181
|
|
Sergunb |
0:8f0d870509fe
|
182
|
// Returns machine position of axis 'idx'. Must be sent a 'step' array.
|
Sergunb |
0:8f0d870509fe
|
183
|
float system_convert_axis_steps_to_mpos(int32_t *steps, uint8_t idx);
|
Sergunb |
0:8f0d870509fe
|
184
|
|
Sergunb |
0:8f0d870509fe
|
185
|
// Updates a machine 'position' array based on the 'step' array sent.
|
Sergunb |
0:8f0d870509fe
|
186
|
void system_convert_array_steps_to_mpos(float *position, int32_t *steps);
|
Sergunb |
0:8f0d870509fe
|
187
|
|
Sergunb |
0:8f0d870509fe
|
188
|
// CoreXY calculation only. Returns x or y-axis "steps" based on CoreXY motor steps.
|
Sergunb |
0:8f0d870509fe
|
189
|
#ifdef COREXY
|
Sergunb |
0:8f0d870509fe
|
190
|
int32_t system_convert_corexy_to_x_axis_steps(int32_t *steps);
|
Sergunb |
0:8f0d870509fe
|
191
|
int32_t system_convert_corexy_to_y_axis_steps(int32_t *steps);
|
Sergunb |
0:8f0d870509fe
|
192
|
#endif
|
Sergunb |
0:8f0d870509fe
|
193
|
|
Sergunb |
0:8f0d870509fe
|
194
|
// Checks and reports if target array exceeds machine travel limits.
|
Sergunb |
0:8f0d870509fe
|
195
|
uint8_t system_check_travel_limits(float *target);
|
Sergunb |
0:8f0d870509fe
|
196
|
|
Sergunb |
0:8f0d870509fe
|
197
|
// Special handlers for setting and clearing Grbl's real-time execution flags.
|
Sergunb |
0:8f0d870509fe
|
198
|
void system_set_exec_state_flag(uint8_t mask);
|
Sergunb |
0:8f0d870509fe
|
199
|
void system_clear_exec_state_flag(uint8_t mask);
|
Sergunb |
0:8f0d870509fe
|
200
|
void system_set_exec_alarm(uint8_t code);
|
Sergunb |
0:8f0d870509fe
|
201
|
void system_clear_exec_alarm();
|
Sergunb |
0:8f0d870509fe
|
202
|
void system_set_exec_motion_override_flag(uint8_t mask);
|
Sergunb |
0:8f0d870509fe
|
203
|
void system_set_exec_accessory_override_flag(uint8_t mask);
|
Sergunb |
0:8f0d870509fe
|
204
|
void system_clear_exec_motion_overrides();
|
Sergunb |
0:8f0d870509fe
|
205
|
void system_clear_exec_accessory_overrides();
|
Sergunb |
0:8f0d870509fe
|
206
|
|
Sergunb |
0:8f0d870509fe
|
207
|
|
Sergunb |
0:8f0d870509fe
|
208
|
#endif
|