gnea\grbl-Mega  1.0f
Source Code Documentation ( Internal Workings )
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);
27  #ifdef DISABLE_CONTROL_PIN_PULL_UP
28  CONTROL_PORT &= ~(CONTROL_MASK);
29  #else
30  CONTROL_PORT |= CONTROL_MASK;
31  #endif
32  CONTROL_PCMSK |= CONTROL_MASK;
33  PCICR |= (1 << CONTROL_INT);
34 }
35 
37 // triggered is 1 and not triggered is 0. Invert mask is applied. Bitfield organization is
38 // defined by the CONTROL_PIN_INDEX in the header file.
40 {
41  uint8_t control_state = 0;
42  uint8_t pin = (CONTROL_PIN & CONTROL_MASK);
43  #ifdef INVERT_CONTROL_PIN_MASK
44  pin ^= INVERT_CONTROL_PIN_MASK;
45  #endif
46  if (pin) {
47  if (bit_isfalse(pin,(1<<CONTROL_SAFETY_DOOR_BIT))) { control_state |= CONTROL_PIN_INDEX_SAFETY_DOOR; }
48  if (bit_isfalse(pin,(1<<CONTROL_RESET_BIT))) { control_state |= CONTROL_PIN_INDEX_RESET; }
49  if (bit_isfalse(pin,(1<<CONTROL_FEED_HOLD_BIT))) { control_state |= CONTROL_PIN_INDEX_FEED_HOLD; }
50  if (bit_isfalse(pin,(1<<CONTROL_CYCLE_START_BIT))) { control_state |= CONTROL_PIN_INDEX_CYCLE_START; }
51  }
52  return(control_state);
53 }
54 
56 // only the realtime command execute variable to have the main program execute these when
57 // its ready. This works exactly like the character-based realtime commands when picked off
58 // directly from the incoming serial data stream.
59 ISR(CONTROL_INT_vect)
60 {
61  uint8_t pin = system_control_get_state();
62  if (pin) {
64  mc_reset();
65  } else if (bit_istrue(pin,CONTROL_PIN_INDEX_CYCLE_START)) {
67  } else if (bit_istrue(pin,CONTROL_PIN_INDEX_FEED_HOLD)) {
69  } else if (bit_istrue(pin,CONTROL_PIN_INDEX_SAFETY_DOOR)) {
71  }
72  }
73 }
74 
77 {
79 }
80 
82 void system_execute_startup(char *line)
83 {
84  uint8_t n;
85  for (n=0; n < N_STARTUP_LINE; n++) {
86  if (!(settings_read_startup_line(n, line))) {
87  line[0] = 0;
89  } else {
90  if (line[0] != 0) {
91  uint8_t status_code = gc_execute_line(line);
92  report_execute_startup_message(line,status_code);
93  }
94  }
95  }
96 }
97 
99 // incoming streaming g-code blocks, this also executes Grbl internal commands, such as
100 // settings, initiating the homing cycle, and toggling switch states. This differs from
101 // the realtime command module by being susceptible to when Grbl is ready to execute the
102 // next line during a cycle, so for switches like block delete, the switch only effects
103 // the lines that are processed afterward, not necessarily real-time during a cycle,
104 // since there are motions already stored in the buffer. However, this 'lag' should not
105 // be an issue, since these commands are not typically used during a cycle.
106 uint8_t system_execute_line(char *line)
107 {
108  uint8_t char_counter = 1;
109  uint8_t helper_var = 0;
110  float parameter, value;
111  switch( line[char_counter] ) {
112  case 0 : report_grbl_help(); break;
113  case 'J' :
114 // Execute only if in IDLE or JOG states.
115  if (sys.state != STATE_IDLE && sys.state != STATE_JOG) { return(STATUS_IDLE_ERROR); }
116  if(line[2] != '=') { return(STATUS_INVALID_STATEMENT); }
117  return(gc_execute_line(line));
118  break;
120  case '$': case 'G': case 'C': case 'X':
121  if ( line[2] != 0 ) { return(STATUS_INVALID_STATEMENT); }
122  switch( line[1] ) {
123  case '$' :
124  if ( sys.state & (STATE_CYCLE | STATE_HOLD) ) { return(STATUS_IDLE_ERROR); }
125  else { report_grbl_settings(); }
126  break;
127  case 'G' :
130  break;
131  case 'C' :
132 // Perform reset when toggling off. Check g-code mode should only work if Grbl
133 // is idle and ready, regardless of alarm locks. This is mainly to keep things
134 // simple and consistent.
135  if ( sys.state == STATE_CHECK_MODE ) {
136  mc_reset();
138  } else {
139  if (sys.state) { return(STATUS_IDLE_ERROR); }
142  }
143  break;
144  case 'X' :
145  if (sys.state == STATE_ALARM) {
146 // Block if safety door is ajar.
149  sys.state = STATE_IDLE;
150 // Don't run startup script. Prevents stored moves in startup from causing accidents.
151  }
152  break;
153  }
154  break;
155  default :
156 // Block any system command that requires the state as IDLE/ALARM. (i.e. EEPROM, homing)
157  if ( !(sys.state == STATE_IDLE || sys.state == STATE_ALARM) ) { return(STATUS_IDLE_ERROR); }
158  switch( line[1] ) {
159  case '#' :
160  if ( line[2] != 0 ) { return(STATUS_INVALID_STATEMENT); }
161  else { report_ngc_parameters(); }
162  break;
163  case 'H' :
167  if (line[2] == 0) {
169  #ifdef HOMING_SINGLE_AXIS_COMMANDS
170  } else if (line[3] == 0) {
171  switch (line[2]) {
172  case 'X': mc_homing_cycle(HOMING_CYCLE_X); break;
173  case 'Y': mc_homing_cycle(HOMING_CYCLE_Y); break;
174  case 'Z': mc_homing_cycle(HOMING_CYCLE_Z); break;
175  default: return(STATUS_INVALID_STATEMENT);
176  }
177  #endif
178  } else { return(STATUS_INVALID_STATEMENT); }
179  if (!sys.abort) {
180  sys.state = STATE_IDLE;
181  st_go_idle();
182  if (line[2] == 0) { system_execute_startup(line); }
183  }
184  break;
185  case 'S' :
186  if ((line[2] != 'L') || (line[3] != 'P') || (line[4] != 0)) { return(STATUS_INVALID_STATEMENT); }
188  break;
189  case 'I' :
190  if ( line[++char_counter] == 0 ) {
192  report_build_info(line);
193  #ifdef ENABLE_BUILD_INFO_WRITE_COMMAND
194  } else {
195  if(line[char_counter++] != '=') { return(STATUS_INVALID_STATEMENT); }
196  helper_var = char_counter;
197  do {
198  line[char_counter-helper_var] = line[char_counter];
199  } while (line[char_counter++] != 0);
201  #endif
202  }
203  break;
204  case 'R' :
205  if ((line[2] != 'S') || (line[3] != 'T') || (line[4] != '=') || (line[6] != 0)) { return(STATUS_INVALID_STATEMENT); }
206  switch (line[5]) {
207  #ifdef ENABLE_RESTORE_EEPROM_DEFAULT_SETTINGS
209  #endif
210  #ifdef ENABLE_RESTORE_EEPROM_CLEAR_PARAMETERS
212  #endif
213  #ifdef ENABLE_RESTORE_EEPROM_WIPE_ALL
214  case '*': settings_restore(SETTINGS_RESTORE_ALL); break;
215  #endif
216  default: return(STATUS_INVALID_STATEMENT);
217  }
219  mc_reset();
220  break;
221  case 'N' :
222  if ( line[++char_counter] == 0 ) {
223  for (helper_var=0; helper_var < N_STARTUP_LINE; helper_var++) {
224  if (!(settings_read_startup_line(helper_var, line))) {
226  } else {
227  report_startup_line(helper_var,line);
228  }
229  }
230  break;
231  } else {
232  if (sys.state != STATE_IDLE) { return(STATUS_IDLE_ERROR); }
233  helper_var = true;
234 // No break. Continues into default: to read remaining command characters.
235  }
236  default :
237  if(!read_float(line, &char_counter, &parameter)) { return(STATUS_BAD_NUMBER_FORMAT); }
238  if(line[char_counter++] != '=') { return(STATUS_INVALID_STATEMENT); }
239  if (helper_var) {
240 // Prepare sending gcode block to gcode parser by shifting all characters
241  helper_var = char_counter;
242  do {
243  line[char_counter-helper_var] = line[char_counter];
244  } while (line[char_counter++] != 0);
245  if (char_counter > EEPROM_LINE_SIZE) { return(STATUS_LINE_LENGTH_EXCEEDED); }
246 // Execute gcode block to ensure block is valid.
247  helper_var = gc_execute_line(line);
248  if (helper_var) { return(helper_var); }
249  else {
250  helper_var = trunc(parameter);
251  settings_store_startup_line(helper_var,line);
252  }
253  } else {
254  if(!read_float(line, &char_counter, &value)) { return(STATUS_BAD_NUMBER_FORMAT); }
255  if((line[char_counter] != 0) || (parameter > 255)) { return(STATUS_INVALID_STATEMENT); }
256  return(settings_store_global_setting((uint8_t)parameter, value));
257  }
258  }
259  }
260  return(STATUS_OK);
261 }
262 
263 
264 
266 {
267  #ifdef FORCE_BUFFER_SYNC_DURING_WCO_CHANGE
269  #endif
271 }
272 
274 //
276 // serves as a central place to compute the transformation.
277 float system_convert_axis_steps_to_mpos(int32_t *steps, uint8_t idx)
278 {
279  float pos;
280  #ifdef COREXY
281  if (idx==X_AXIS) {
282  pos = (float)system_convert_corexy_to_x_axis_steps(steps) / settings.steps_per_mm[idx];
283  } else if (idx==Y_AXIS) {
284  pos = (float)system_convert_corexy_to_y_axis_steps(steps) / settings.steps_per_mm[idx];
285  } else {
286  pos = steps[idx]/settings.steps_per_mm[idx];
287  }
288  #else
289  pos = steps[idx]/settings.steps_per_mm[idx];
290  #endif
291  return(pos);
292 }
293 
294 
295 void system_convert_array_steps_to_mpos(float *position, int32_t *steps)
296 {
297  uint8_t idx;
298  for (idx=0; idx<N_AXIS; idx++) {
299  position[idx] = system_convert_axis_steps_to_mpos(steps, idx);
300  }
301  return;
302 }
303 
305 #ifdef COREXY
306  int32_t system_convert_corexy_to_x_axis_steps(int32_t *steps)
307  {
308  return( (steps[A_MOTOR] + steps[B_MOTOR])/2 );
309  }
310  int32_t system_convert_corexy_to_y_axis_steps(int32_t *steps)
311  {
312  return( (steps[A_MOTOR] - steps[B_MOTOR])/2 );
313  }
314 #endif
315 
317 uint8_t system_check_travel_limits(float *target)
318 {
319  uint8_t idx;
320  for (idx=0; idx<N_AXIS; idx++) {
321  #ifdef HOMING_FORCE_SET_ORIGIN
322 // When homing forced set origin is enabled, soft limits checks need to account for directionality.
323 //
326  if (target[idx] < 0 || target[idx] > -settings.max_travel[idx]) { return(true); }
327  } else {
328  if (target[idx] > 0 || target[idx] < settings.max_travel[idx]) { return(true); }
329  }
330  #else
331 //
333  if (target[idx] > 0 || target[idx] < settings.max_travel[idx]) { return(true); }
334  #endif
335  }
336  return(false);
337 }
338 
340 void system_set_exec_state_flag(uint8_t mask) {
341  uint8_t sreg = SREG;
342  cli();
343  sys_rt_exec_state |= (mask);
344  SREG = sreg;
345 }
346 
347 void system_clear_exec_state_flag(uint8_t mask) {
348  uint8_t sreg = SREG;
349  cli();
350  sys_rt_exec_state &= ~(mask);
351  SREG = sreg;
352 }
353 
354 void system_set_exec_alarm(uint8_t code) {
355  uint8_t sreg = SREG;
356  cli();
357  sys_rt_exec_alarm = code;
358  SREG = sreg;
359 }
360 
362  uint8_t sreg = SREG;
363  cli();
364  sys_rt_exec_alarm = 0;
365  SREG = sreg;
366 }
367 
369  uint8_t sreg = SREG;
370  cli();
371  sys_rt_exec_motion_override |= (mask);
372  SREG = sreg;
373 }
374 
376  uint8_t sreg = SREG;
377  cli();
379  SREG = sreg;
380 }
381 
383  uint8_t sreg = SREG;
384  cli();
386  SREG = sreg;
387 }
388 
390  uint8_t sreg = SREG;
391  cli();
393  SREG = sreg;
394 }
uint8_t homing_dir_mask
Definition: settings.h:94
void mc_homing_cycle(uint8_t cycle_mask)
Perform homing cycle to locate and set machine zero. Only '$H' executes this command.
#define N_AXIS
Axis array index values. Must start with 0 and be continuous.
Definition: nuts_bolts.h:30
uint8_t system_check_safety_door_ajar()
Returns if safety door is ajar(T) or closed(F), based on pin state.
Definition: system.c:76
#define N_STARTUP_LINE
Enables single axis homing commands. $HX, $HY, and $HZ for X, Y, and Z-axis homing. The full homing.
Definition: config.h:127
void report_grbl_help()
Grbl help message.
Definition: report.c:171
void report_grbl_settings()
Grbl global settings print out.
Definition: report.c:178
#define STATE_HOLD
Active feed hold.
Definition: system.h:77
#define Y_AXIS
Definition: nuts_bolts.h:32
void system_convert_array_steps_to_mpos(float *position, int32_t *steps)
Updates a machine 'position' array based on the 'step' array sent.
Definition: system.c:295
uint8_t read_float(char *line, uint8_t *char_counter, float *float_ptr)
Extracts a floating point value from a string. The following code is based loosely on...
Definition: nuts_bolts.c:35
uint8_t gc_execute_line(char *line)
Executes one line of 0-terminated G-Code. The line is assumed to contain only uppercase.
Definition: gcode.c:63
void report_status_message(uint8_t status_code)
Handles the primary confirmation protocol response for streaming interfaces and human-feedback.
Definition: report.c:110
#define HOMING_CYCLE_X
#define bit_true(x, mask)
Definition: nuts_bolts.h:58
#define HOMING_CYCLE_Y
ISR(CONTROL_INT_vect)
Pin change interrupt for pin-out commands, i.e. cycle start, feed hold, and reset. Sets.
Definition: system.c:59
uint8_t system_execute_line(char *line)
Directs and executes one line of formatted input from protocol_process. While mostly.
Definition: system.c:106
#define MESSAGE_DISABLED
Definition: report.h:75
#define CONTROL_PIN_INDEX_FEED_HOLD
Definition: system.h:101
void system_flag_wco_change()
Definition: system.c:265
#define STATUS_CHECK_DOOR
Definition: report.h:36
#define STATUS_BAD_NUMBER_FORMAT
Definition: report.h:25
void system_set_exec_state_flag(uint8_t mask)
Special handlers for setting and clearing Grbl's real-time execution flags.
Definition: system.c:340
void protocol_buffer_synchronize()
Block until all buffered steps are executed or in a cycle state. Works with feed hold.
Definition: protocol.c:177
void report_ngc_parameters()
Prints Grbl NGC parameters (coordinate offsets, probing)
Definition: report.c:234
void system_clear_exec_accessory_overrides()
Definition: system.c:389
void settings_store_startup_line(uint8_t n, char *line)
Method to store startup lines into EEPROM.
Definition: settings.c:27
void report_feedback_message(uint8_t message_code)
Prints feedback messages. This serves as a centralized method to provide additional.
Definition: report.c:135
uint8_t system_control_get_state()
Returns control pin state as a uint8 bitfield. Each bit indicates the input pin state, where.
Definition: system.c:39
volatile uint8_t sys_rt_exec_alarm
Global realtime executor bitflag variable for setting various alarms.
Definition: system.h:135
uint8_t state
Tracks the current system state of Grbl.
Definition: system.h:112
void system_set_exec_motion_override_flag(uint8_t mask)
Definition: system.c:368
#define MESSAGE_RESTORE_DEFAULTS
Definition: report.h:79
#define STATUS_LINE_LENGTH_EXCEEDED
Definition: report.h:37
#define MESSAGE_ENABLED
Definition: report.h:74
#define STATUS_SETTING_DISABLED
Definition: report.h:28
#define EXEC_SAFETY_DOOR
bitmask 00100000
Definition: system.h:36
#define BITFLAG_HOMING_ENABLE
Definition: settings.h:39
void settings_store_build_info(char *line)
Method to store build info into EEPROM.
Definition: settings.c:39
#define X_AXIS
Axis indexing value.
Definition: nuts_bolts.h:31
uint8_t report_wco_counter
Tracks when to add work coordinate offset data to status reports.
Definition: system.h:124
#define HOMING_CYCLE_ALL
Must be zero.
#define bit(n)
Bit field and masking macros.
Definition: nuts_bolts.h:57
uint8_t abort
System abort flag. Forces exit back to main loop for reset.
Definition: system.h:113
#define STATUS_INVALID_STATEMENT
Definition: report.h:26
uint8_t settings_read_startup_line(uint8_t n, char *line)
Reads startup line from EEPROM. Updated pointed line string data.
Definition: settings.c:135
void system_clear_exec_alarm()
Definition: system.c:361
volatile uint8_t sys_rt_exec_motion_override
Global realtime executor bitflag variable for motion-based overrides.
Definition: system.h:136
void report_startup_line(uint8_t n, char *line)
Prints specified startup line.
Definition: report.c:325
#define STATE_CYCLE
Cycle is running or motions are being executed.
Definition: system.h:76
#define CONTROL_PIN_INDEX_CYCLE_START
Definition: system.h:102
void report_build_info(char *line)
Prints build info line.
Definition: report.c:342
uint8_t flags
Contains default boolean settings.
Definition: settings.h:92
#define STATE_CHECK_MODE
G-code check mode. Locks out planner and motion only.
Definition: system.h:74
void report_gcode_modes()
Print current gcode parser mode state.
Definition: report.c:263
#define MESSAGE_ALARM_UNLOCK
Definition: report.h:73
#define STATE_ALARM
In alarm state. Locks out all g-code processes. Allows settings access.
Definition: system.h:73
#define CONTROL_PIN_INDEX_RESET
Definition: system.h:100
#define EXEC_CYCLE_START
bitmask 00000010
Definition: system.h:32
system_t sys
Declare system global variable structure.
Definition: main.c:25
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.
Definition: system.c:277
#define HOMING_CYCLE_Z
#define SETTINGS_RESTORE_PARAMETERS
Definition: settings.h:48
void st_go_idle()
Stepper shutdown.
Definition: stepper.c:224
float steps_per_mm[N_AXIS]
Definition: settings.h:75
void system_clear_exec_state_flag(uint8_t mask)
Definition: system.c:347
#define bit_isfalse(x, mask)
Definition: nuts_bolts.h:61
uint8_t system_check_travel_limits(float *target)
CoreXY calculation only. Returns x or y-axis "steps" based on CoreXY motor steps. ...
Definition: system.c:317
float max_travel[N_AXIS]
Definition: settings.h:78
#define CONTROL_PIN_INDEX_SAFETY_DOOR
Definition: system.h:99
#define EXEC_SLEEP
bitmask 10000000
Definition: system.h:38
volatile uint8_t sys_rt_exec_state
Global realtime executor bitflag variable for state management. See EXEC bitmasks.
Definition: system.h:134
#define EXEC_FEED_HOLD
bitmask 00001000
Definition: system.h:34
void system_set_exec_accessory_override_flag(uint8_t mask)
Definition: system.c:375
settings_t settings
Definition: settings.c:24
void report_execute_startup_message(char *line, uint8_t status_code)
Definition: report.c:334
#define SETTINGS_RESTORE_DEFAULTS
Define settings restore bitflags.
Definition: settings.h:47
#define STATE_JOG
Jogging mode.
Definition: system.h:78
#define STATE_HOMING
Performing homing cycle.
Definition: system.h:75
uint8_t settings_read_build_info(char *line)
Reads startup line from EEPROM. Updated pointed line string data.
Definition: settings.c:148
void system_set_exec_alarm(uint8_t code)
Definition: system.c:354
void system_init()
Initialize the serial protocol.
Definition: system.c:24
#define STATUS_SETTING_READ_FAIL
Definition: report.h:30
void mc_reset()
Method to ready the system to reset by setting the realtime reset command and killing any...
volatile uint8_t sys_rt_exec_accessory_override
Global realtime executor bitflag variable for spindle/coolant overrides.
Definition: system.h:137
uint8_t settings_store_global_setting(uint8_t parameter, float value)
A helper method to set settings from command line.
Definition: settings.c:188
void system_execute_startup(char *line)
Executes user startup script, if stored.
Definition: system.c:82
#define STATUS_OK
Define Grbl status codes. Valid values (0-255)
Definition: report.h:23
#define STATUS_IDLE_ERROR
Definition: report.h:31
void settings_restore(uint8_t restore_flag)
Method to restore EEPROM-saved Grbl global settings back to defaults.
Definition: settings.c:65
void system_clear_exec_motion_overrides()
Definition: system.c:382
#define STATE_IDLE
Define system state bit map. The state variable primarily tracks the individual functions.
Definition: system.h:72
#define bit_istrue(x, mask)
Definition: nuts_bolts.h:60