blob: 55e6c406acea5ced99beffe145f97b60fcbb83e7 [file] [log] [blame]
Luigi Santivetti69972f92019-11-12 22:55:40 +00001/*
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
24void system_init()
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.
40uint8_t system_control_get_state()
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) {
68 if (bit_istrue(pin,CONTROL_PIN_INDEX_RESET)) {
69 mc_reset();
70 }
71 if (bit_istrue(pin,CONTROL_PIN_INDEX_CYCLE_START)) {
72 bit_true(sys_rt_exec_state, EXEC_CYCLE_START);
73 }
74 #ifndef ENABLE_SAFETY_DOOR_INPUT_PIN
75 if (bit_istrue(pin,CONTROL_PIN_INDEX_FEED_HOLD)) {
76 bit_true(sys_rt_exec_state, EXEC_FEED_HOLD);
77 #else
78 if (bit_istrue(pin,CONTROL_PIN_INDEX_SAFETY_DOOR)) {
79 bit_true(sys_rt_exec_state, EXEC_SAFETY_DOOR);
80 #endif
81 }
82 }
83}
84
85
86// Returns if safety door is ajar(T) or closed(F), based on pin state.
87uint8_t system_check_safety_door_ajar()
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.
98void system_execute_startup(char *line)
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;
104 report_execute_startup_message(line,STATUS_SETTING_READ_FAIL);
105 } else {
106 if (line[0] != 0) {
107 uint8_t status_code = gc_execute_line(line);
108 report_execute_startup_message(line,status_code);
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.
123uint8_t system_execute_line(char *line)
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.
145 report_gcode_modes();
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();
153 report_feedback_message(MESSAGE_DISABLED);
154 } else {
155 if (sys.state) { return(STATUS_IDLE_ERROR); } // Requires no alarm mode.
156 sys.state = STATE_CHECK_MODE;
157 report_feedback_message(MESSAGE_ENABLED);
158 }
159 break;
160 case 'X' : // Disable alarm lock [ALARM]
161 if (sys.state == STATE_ALARM) {
162 // Block if safety door is ajar.
163 if (system_check_safety_door_ajar()) { return(STATUS_CHECK_DOOR); }
164 report_feedback_message(MESSAGE_ALARM_UNLOCK);
165 sys.state = STATE_IDLE;
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]
180 if (bit_isfalse(settings.flags,BITFLAG_HOMING_ENABLE)) {return(STATUS_SETTING_DISABLED); }
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) {
184 mc_homing_cycle(HOMING_CYCLE_ALL);
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 ) {
207 settings_read_build_info(line);
208 report_build_info(line);
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);
216 settings_store_build_info(line);
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
224 case '$': settings_restore(SETTINGS_RESTORE_DEFAULTS); break;
225 #endif
226 #ifdef ENABLE_RESTORE_EEPROM_CLEAR_PARAMETERS
227 case '#': settings_restore(SETTINGS_RESTORE_PARAMETERS); break;
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 }
234 report_feedback_message(MESSAGE_RESTORE_DEFAULTS);
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))) {
241 report_status_message(STATUS_SETTING_READ_FAIL);
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
266 settings_store_startup_line(helper_var,line);
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
280void system_flag_wco_change()
281{
282 #ifdef FORCE_BUFFER_SYNC_DURING_WCO_CHANGE
283 protocol_buffer_synchronize();
284 #endif
285 sys.report_wco_counter = 0;
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
341 if (bit_istrue(settings.homing_dir_mask,bit(idx))) {
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
363void system_clear_exec_state_flag(uint8_t mask) {
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
377void system_clear_exec_alarm() {
378 uint8_t sreg = SREG;
379 cli();
380 sys_rt_exec_alarm = 0;
381 SREG = sreg;
382}
383
384void system_set_exec_motion_override_flag(uint8_t mask) {
385 uint8_t sreg = SREG;
386 cli();
387 sys_rt_exec_motion_override |= (mask);
388 SREG = sreg;
389}
390
391void system_set_exec_accessory_override_flag(uint8_t mask) {
392 uint8_t sreg = SREG;
393 cli();
394 sys_rt_exec_accessory_override |= (mask);
395 SREG = sreg;
396}
397
398void system_clear_exec_motion_overrides() {
399 uint8_t sreg = SREG;
400 cli();
401 sys_rt_exec_motion_override = 0;
402 SREG = sreg;
403}
404
405void system_clear_exec_accessory_overrides() {
406 uint8_t sreg = SREG;
407 cli();
408 sys_rt_exec_accessory_override = 0;
409 SREG = sreg;
410}