LAT Hologramm-Software 2.0
Loading...
Searching...
No Matches
system.c
Go to the documentation of this file.
1/*
2 system.c - Handles system level commands and real-time processes
3 Part of Grbl
4
5 Copyright (c) 2014-2016 Sungeun K. Jeon for Gnea Research LLC
6
7 Grbl is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 Grbl is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Grbl. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21#include "grbl.h"
22
23
25{
26 CONTROL_DDR &= ~(CONTROL_MASK); // Configure as input pins
27 #ifdef DISABLE_CONTROL_PIN_PULL_UP
28 CONTROL_PORT &= ~(CONTROL_MASK); // Normal low operation. Requires external pull-down.
29 #else
30 CONTROL_PORT |= CONTROL_MASK; // Enable internal pull-up resistors. Normal high operation.
31 #endif
32 CONTROL_PCMSK |= CONTROL_MASK; // Enable specific pins of the Pin Change Interrupt
33 PCICR |= (1 << CONTROL_INT); // Enable Pin Change Interrupt
34}
35
36
37// Returns control pin state as a uint8 bitfield. Each bit indicates the input pin state, where
38// triggered is 1 and not triggered is 0. Invert mask is applied. Bitfield organization is
39// defined by the CONTROL_PIN_INDEX in the header file.
41{
42 uint8_t control_state = 0;
43 uint8_t pin = (CONTROL_PIN & CONTROL_MASK) ^ CONTROL_MASK;
44 #ifdef INVERT_CONTROL_PIN_MASK
45 pin ^= INVERT_CONTROL_PIN_MASK;
46 #endif
47 if (pin) {
48 #ifdef ENABLE_SAFETY_DOOR_INPUT_PIN
49 if (bit_istrue(pin,(1<<CONTROL_SAFETY_DOOR_BIT))) { control_state |= CONTROL_PIN_INDEX_SAFETY_DOOR; }
50 #else
51 if (bit_istrue(pin,(1<<CONTROL_FEED_HOLD_BIT))) { control_state |= CONTROL_PIN_INDEX_FEED_HOLD; }
52 #endif
53 if (bit_istrue(pin,(1<<CONTROL_RESET_BIT))) { control_state |= CONTROL_PIN_INDEX_RESET; }
54 if (bit_istrue(pin,(1<<CONTROL_CYCLE_START_BIT))) { control_state |= CONTROL_PIN_INDEX_CYCLE_START; }
55 }
56 return(control_state);
57}
58
59
60// Pin change interrupt for pin-out commands, i.e. cycle start, feed hold, and reset. Sets
61// only the realtime command execute variable to have the main program execute these when
62// its ready. This works exactly like the character-based realtime commands when picked off
63// directly from the incoming serial data stream.
64ISR(CONTROL_INT_vect)
65{
66 uint8_t pin = system_control_get_state();
67 if (pin) {
69 mc_reset();
70 }
73 }
74 #ifndef ENABLE_SAFETY_DOOR_INPUT_PIN
77 #else
78 if (bit_istrue(pin,CONTROL_PIN_INDEX_SAFETY_DOOR)) {
80 #endif
81 }
82 }
83}
84
85
86// Returns if safety door is ajar(T) or closed(F), based on pin state.
88{
89 #ifdef ENABLE_SAFETY_DOOR_INPUT_PIN
90 return(system_control_get_state() & CONTROL_PIN_INDEX_SAFETY_DOOR);
91 #else
92 return(false); // Input pin not enabled, so just return that it's closed.
93 #endif
94}
95
96
97// Executes user startup script, if stored.
99{
100 uint8_t n;
101 for (n=0; n < N_STARTUP_LINE; n++) {
102 if (!(settings_read_startup_line(n, line))) {
103 line[0] = 0;
105 } else {
106 if (line[0] != 0) {
107 uint8_t status_code = gc_execute_line(line);
109 }
110 }
111 }
112}
113
114
115// Directs and executes one line of formatted input from protocol_process. While mostly
116// incoming streaming g-code blocks, this also executes Grbl internal commands, such as
117// settings, initiating the homing cycle, and toggling switch states. This differs from
118// the realtime command module by being susceptible to when Grbl is ready to execute the
119// next line during a cycle, so for switches like block delete, the switch only effects
120// the lines that are processed afterward, not necessarily real-time during a cycle,
121// since there are motions already stored in the buffer. However, this 'lag' should not
122// be an issue, since these commands are not typically used during a cycle.
124{
125 uint8_t char_counter = 1;
126 uint8_t helper_var = 0; // Helper variable
127 float parameter, value;
128 switch( line[char_counter] ) {
129 case 0 : report_grbl_help(); break;
130 case 'J' : // Jogging
131 // Execute only if in IDLE or JOG states.
132 if (sys.state != STATE_IDLE && sys.state != STATE_JOG) { return(STATUS_IDLE_ERROR); }
133 if(line[2] != '=') { return(STATUS_INVALID_STATEMENT); }
134 return(gc_execute_line(line)); // NOTE: $J= is ignored inside g-code parser and used to detect jog motions.
135 break;
136 case '$': case 'G': case 'C': case 'X':
137 if ( line[2] != 0 ) { return(STATUS_INVALID_STATEMENT); }
138 switch( line[1] ) {
139 case '$' : // Prints Grbl settings
140 if ( sys.state & (STATE_CYCLE | STATE_HOLD) ) { return(STATUS_IDLE_ERROR); } // Block during cycle. Takes too long to print.
141 else { report_grbl_settings(); }
142 break;
143 case 'G' : // Prints gcode parser state
144 // TODO: Move this to realtime commands for GUIs to request this data during suspend-state.
146 break;
147 case 'C' : // Set check g-code mode [IDLE/CHECK]
148 // Perform reset when toggling off. Check g-code mode should only work if Grbl
149 // is idle and ready, regardless of alarm locks. This is mainly to keep things
150 // simple and consistent.
151 if ( sys.state == STATE_CHECK_MODE ) {
152 mc_reset();
154 } else {
155 if (sys.state) { return(STATUS_IDLE_ERROR); } // Requires no alarm mode.
158 }
159 break;
160 case 'X' : // Disable alarm lock [ALARM]
161 if (sys.state == STATE_ALARM) {
162 // Block if safety door is ajar.
166 // Don't run startup script. Prevents stored moves in startup from causing accidents.
167 } // Otherwise, no effect.
168 break;
169 }
170 break;
171 default :
172 // Block any system command that requires the state as IDLE/ALARM. (i.e. EEPROM, homing)
173 if ( !(sys.state == STATE_IDLE || sys.state == STATE_ALARM) ) { return(STATUS_IDLE_ERROR); }
174 switch( line[1] ) {
175 case '#' : // Print Grbl NGC parameters
176 if ( line[2] != 0 ) { return(STATUS_INVALID_STATEMENT); }
177 else { report_ngc_parameters(); }
178 break;
179 case 'H' : // Perform homing cycle [IDLE/ALARM]
181 if (system_check_safety_door_ajar()) { return(STATUS_CHECK_DOOR); } // Block if safety door is ajar.
182 sys.state = STATE_HOMING; // Set system state variable
183 if (line[2] == 0) {
185 #ifdef HOMING_SINGLE_AXIS_COMMANDS
186 } else if (line[3] == 0) {
187 switch (line[2]) {
188 case 'X': mc_homing_cycle(HOMING_CYCLE_X); break;
189 case 'Y': mc_homing_cycle(HOMING_CYCLE_Y); break;
190 case 'Z': mc_homing_cycle(HOMING_CYCLE_Z); break;
191 default: return(STATUS_INVALID_STATEMENT);
192 }
193 #endif
194 } else { return(STATUS_INVALID_STATEMENT); }
195 if (!sys.abort) { // Execute startup scripts after successful homing.
196 sys.state = STATE_IDLE; // Set to IDLE when complete.
197 st_go_idle(); // Set steppers to the settings idle state before returning.
198 if (line[2] == 0) { system_execute_startup(line); }
199 }
200 break;
201 case 'S' : // Puts Grbl to sleep [IDLE/ALARM]
202 if ((line[2] != 'L') || (line[3] != 'P') || (line[4] != 0)) { return(STATUS_INVALID_STATEMENT); }
203 system_set_exec_state_flag(EXEC_SLEEP); // Set to execute sleep mode immediately
204 break;
205 case 'I' : // Print or store build info. [IDLE/ALARM]
206 if ( line[++char_counter] == 0 ) {
209 #ifdef ENABLE_BUILD_INFO_WRITE_COMMAND
210 } else { // Store startup line [IDLE/ALARM]
211 if(line[char_counter++] != '=') { return(STATUS_INVALID_STATEMENT); }
212 helper_var = char_counter; // Set helper variable as counter to start of user info line.
213 do {
214 line[char_counter-helper_var] = line[char_counter];
215 } while (line[char_counter++] != 0);
217 #endif
218 }
219 break;
220 case 'R' : // Restore defaults [IDLE/ALARM]
221 if ((line[2] != 'S') || (line[3] != 'T') || (line[4] != '=') || (line[6] != 0)) { return(STATUS_INVALID_STATEMENT); }
222 switch (line[5]) {
223 #ifdef ENABLE_RESTORE_EEPROM_DEFAULT_SETTINGS
225 #endif
226 #ifdef ENABLE_RESTORE_EEPROM_CLEAR_PARAMETERS
228 #endif
229 #ifdef ENABLE_RESTORE_EEPROM_WIPE_ALL
230 case '*': settings_restore(SETTINGS_RESTORE_ALL); break;
231 #endif
232 default: return(STATUS_INVALID_STATEMENT);
233 }
235 mc_reset(); // Force reset to ensure settings are initialized correctly.
236 break;
237 case 'N' : // Startup lines. [IDLE/ALARM]
238 if ( line[++char_counter] == 0 ) { // Print startup lines
239 for (helper_var=0; helper_var < N_STARTUP_LINE; helper_var++) {
240 if (!(settings_read_startup_line(helper_var, line))) {
242 } else {
243 report_startup_line(helper_var,line);
244 }
245 }
246 break;
247 } else { // Store startup line [IDLE Only] Prevents motion during ALARM.
248 if (sys.state != STATE_IDLE) { return(STATUS_IDLE_ERROR); } // Store only when idle.
249 helper_var = true; // Set helper_var to flag storing method.
250 // No break. Continues into default: to read remaining command characters.
251 }
252 default : // Storing setting methods [IDLE/ALARM]
253 if(!read_float(line, &char_counter, &parameter)) { return(STATUS_BAD_NUMBER_FORMAT); }
254 if(line[char_counter++] != '=') { return(STATUS_INVALID_STATEMENT); }
255 if (helper_var) { // Store startup line
256 // Prepare sending gcode block to gcode parser by shifting all characters
257 helper_var = char_counter; // Set helper variable as counter to start of gcode block
258 do {
259 line[char_counter-helper_var] = line[char_counter];
260 } while (line[char_counter++] != 0);
261 // Execute gcode block to ensure block is valid.
262 helper_var = gc_execute_line(line); // Set helper_var to returned status code.
263 if (helper_var) { return(helper_var); }
264 else {
265 helper_var = trunc(parameter); // Set helper_var to int value of parameter
267 }
268 } else { // Store global setting.
269 if(!read_float(line, &char_counter, &value)) { return(STATUS_BAD_NUMBER_FORMAT); }
270 if((line[char_counter] != 0) || (parameter > 255)) { return(STATUS_INVALID_STATEMENT); }
271 return(settings_store_global_setting((uint8_t)parameter, value));
272 }
273 }
274 }
275 return(STATUS_OK); // If '$' command makes it to here, then everything's ok.
276}
277
278
279
281{
282 #ifdef FORCE_BUFFER_SYNC_DURING_WCO_CHANGE
284 #endif
286}
287
288
289// Returns machine position of axis 'idx'. Must be sent a 'step' array.
290// NOTE: If motor steps and machine position are not in the same coordinate frame, this function
291// serves as a central place to compute the transformation.
292float system_convert_axis_steps_to_mpos(int32_t *steps, uint8_t idx)
293{
294 float pos;
295 #ifdef COREXY
296 if (idx==X_AXIS) {
297 pos = (float)system_convert_corexy_to_x_axis_steps(steps) / settings.steps_per_mm[idx];
298 } else if (idx==Y_AXIS) {
299 pos = (float)system_convert_corexy_to_y_axis_steps(steps) / settings.steps_per_mm[idx];
300 } else {
301 pos = steps[idx]/settings.steps_per_mm[idx];
302 }
303 #else
304 pos = steps[idx]/settings.steps_per_mm[idx];
305 #endif
306 return(pos);
307}
308
309
310void system_convert_array_steps_to_mpos(float *position, int32_t *steps)
311{
312 uint8_t idx;
313 for (idx=0; idx<N_AXIS; idx++) {
314 position[idx] = system_convert_axis_steps_to_mpos(steps, idx);
315 }
316 return;
317}
318
319
320// CoreXY calculation only. Returns x or y-axis "steps" based on CoreXY motor steps.
321#ifdef COREXY
322 int32_t system_convert_corexy_to_x_axis_steps(int32_t *steps)
323 {
324 return( (steps[A_MOTOR] + steps[B_MOTOR])/2 );
325 }
326 int32_t system_convert_corexy_to_y_axis_steps(int32_t *steps)
327 {
328 return( (steps[A_MOTOR] - steps[B_MOTOR])/2 );
329 }
330#endif
331
332
333// Checks and reports if target array exceeds machine travel limits.
334uint8_t system_check_travel_limits(float *target)
335{
336 uint8_t idx;
337 for (idx=0; idx<N_AXIS; idx++) {
338 #ifdef HOMING_FORCE_SET_ORIGIN
339 // When homing forced set origin is enabled, soft limits checks need to account for directionality.
340 // NOTE: max_travel is stored as negative
342 if (target[idx] < 0 || target[idx] > -settings.max_travel[idx]) { return(true); }
343 } else {
344 if (target[idx] > 0 || target[idx] < settings.max_travel[idx]) { return(true); }
345 }
346 #else
347 // NOTE: max_travel is stored as negative
348 if (target[idx] > 0 || target[idx] < settings.max_travel[idx]) { return(true); }
349 #endif
350 }
351 return(false);
352}
353
354
355// Special handlers for setting and clearing Grbl's real-time execution flags.
356void system_set_exec_state_flag(uint8_t mask) {
357 uint8_t sreg = SREG;
358 cli();
359 sys_rt_exec_state |= (mask);
360 SREG = sreg;
361}
362
364 uint8_t sreg = SREG;
365 cli();
366 sys_rt_exec_state &= ~(mask);
367 SREG = sreg;
368}
369
370void system_set_exec_alarm(uint8_t code) {
371 uint8_t sreg = SREG;
372 cli();
373 sys_rt_exec_alarm = code;
374 SREG = sreg;
375}
376
378 uint8_t sreg = SREG;
379 cli();
381 SREG = sreg;
382}
383
385 uint8_t sreg = SREG;
386 cli();
388 SREG = sreg;
389}
390
392 uint8_t sreg = SREG;
393 cli();
395 SREG = sreg;
396}
397
399 uint8_t sreg = SREG;
400 cli();
402 SREG = sreg;
403}
404
406 uint8_t sreg = SREG;
407 cli();
409 SREG = sreg;
410}
#define N_STARTUP_LINE
Definition: config.h:135
uint8_t gc_execute_line(char *line)
Definition: gcode.c:66
volatile uint8_t sys_rt_exec_alarm
Definition: main.c:31
system_t sys
Definition: main.c:26
volatile uint8_t sys_rt_exec_accessory_override
Definition: main.c:33
volatile uint8_t sys_rt_exec_motion_override
Definition: main.c:32
volatile uint8_t sys_rt_exec_state
Definition: main.c:30
void mc_homing_cycle(uint8_t cycle_mask)
void mc_reset()
#define HOMING_CYCLE_Z
#define HOMING_CYCLE_Y
#define HOMING_CYCLE_X
#define HOMING_CYCLE_ALL
uint8_t read_float(char *line, uint8_t *char_counter, float *float_ptr)
Definition: nuts_bolts.c:35
#define X_AXIS
Definition: nuts_bolts.h:32
#define bit_isfalse(x, mask)
Definition: nuts_bolts.h:65
#define bit(n)
Definition: nuts_bolts.h:61
#define bit_istrue(x, mask)
Definition: nuts_bolts.h:64
#define Y_AXIS
Definition: nuts_bolts.h:33
#define N_AXIS
Definition: nuts_bolts.h:31
#define bit_true(x, mask)
Definition: nuts_bolts.h:62
void protocol_buffer_synchronize()
Definition: protocol.c:169
static char line[LINE_BUFFER_SIZE]
Definition: protocol.c:30
void report_ngc_parameters()
Definition: report.c:245
void report_grbl_settings()
Definition: report.c:183
void report_feedback_message(uint8_t message_code)
Definition: report.c:138
void report_gcode_modes()
Definition: report.c:275
void report_execute_startup_message(char *line, uint8_t status_code)
Definition: report.c:361
void report_startup_line(uint8_t n, char *line)
Definition: report.c:352
void report_build_info(char *line)
Definition: report.c:370
void report_grbl_help()
Definition: report.c:176
void report_status_message(uint8_t status_code)
Definition: report.c:112
#define STATUS_IDLE_ERROR
Definition: report.h:32
#define STATUS_OK
Definition: report.h:24
#define MESSAGE_RESTORE_DEFAULTS
Definition: report.h:83
#define STATUS_SETTING_DISABLED
Definition: report.h:29
#define STATUS_BAD_NUMBER_FORMAT
Definition: report.h:26
#define STATUS_CHECK_DOOR
Definition: report.h:37
#define MESSAGE_ENABLED
Definition: report.h:78
#define STATUS_INVALID_STATEMENT
Definition: report.h:27
#define MESSAGE_ALARM_UNLOCK
Definition: report.h:77
#define MESSAGE_DISABLED
Definition: report.h:79
#define STATUS_SETTING_READ_FAIL
Definition: report.h:31
uint8_t settings_read_startup_line(uint8_t n, char *line)
Definition: settings.c:136
void settings_store_build_info(char *line)
Definition: settings.c:76
void settings_store_startup_line(uint8_t n, char *line)
Definition: settings.c:64
uint8_t settings_store_global_setting(uint8_t parameter, float value)
Definition: settings.c:193
uint8_t settings_read_build_info(char *line)
Definition: settings.c:150
settings_t settings
Definition: settings.c:24
void settings_restore(uint8_t restore_flag)
Definition: settings.c:104
#define BITFLAG_HOMING_ENABLE
Definition: settings.h:46
#define SETTINGS_RESTORE_PARAMETERS
Definition: settings.h:57
#define SETTINGS_RESTORE_DEFAULTS
Definition: settings.h:56
void st_go_idle()
Definition: stepper.c:250
float steps_per_mm[N_AXIS]
Definition: settings.h:89
float max_travel[N_AXIS]
Definition: settings.h:92
uint8_t flags
Definition: settings.h:106
uint8_t homing_dir_mask
Definition: settings.h:108
uint8_t abort
Definition: system.h:129
uint8_t report_wco_counter
Definition: system.h:143
uint8_t state
Definition: system.h:128
void system_clear_exec_accessory_overrides()
Definition: system.c:405
void system_clear_exec_alarm()
Definition: system.c:377
void system_set_exec_state_flag(uint8_t mask)
Definition: system.c:356
void system_clear_exec_state_flag(uint8_t mask)
Definition: system.c:363
void system_set_exec_accessory_override_flag(uint8_t mask)
Definition: system.c:391
void system_init()
Definition: system.c:24
float system_convert_axis_steps_to_mpos(int32_t *steps, uint8_t idx)
Definition: system.c:292
void system_set_exec_alarm(uint8_t code)
Definition: system.c:370
ISR(CONTROL_INT_vect)
Definition: system.c:64
uint8_t system_control_get_state()
Definition: system.c:40
uint8_t system_check_safety_door_ajar()
Definition: system.c:87
void system_execute_startup(char *line)
Definition: system.c:98
uint8_t system_execute_line(char *line)
Definition: system.c:123
void system_convert_array_steps_to_mpos(float *position, int32_t *steps)
Definition: system.c:310
uint8_t system_check_travel_limits(float *target)
Definition: system.c:334
void system_flag_wco_change()
Definition: system.c:280
void system_set_exec_motion_override_flag(uint8_t mask)
Definition: system.c:384
void system_clear_exec_motion_overrides()
Definition: system.c:398
#define STATE_ALARM
Definition: system.h:77
#define STATE_CHECK_MODE
Definition: system.h:78
#define CONTROL_PIN_INDEX_CYCLE_START
Definition: system.h:115
#define STATE_JOG
Definition: system.h:82
#define STATE_CYCLE
Definition: system.h:80
#define CONTROL_PIN_INDEX_RESET
Definition: system.h:113
#define STATE_HOMING
Definition: system.h:79
#define CONTROL_PIN_INDEX_FEED_HOLD
Definition: system.h:114
#define EXEC_FEED_HOLD
Definition: system.h:34
#define STATE_IDLE
Definition: system.h:76
#define EXEC_SLEEP
Definition: system.h:38
#define EXEC_SAFETY_DOOR
Definition: system.h:36
#define STATE_HOLD
Definition: system.h:81
#define EXEC_CYCLE_START
Definition: system.h:32