gnea\grbl-Mega  1.0f
Source Code Documentation ( Internal Workings )
protocol.c
Go to the documentation of this file.
1 /*
2  protocol.c - controls Grbl execution protocol and procedures
3  Part of Grbl
4 
5  Copyright (c) 2011-2016 Sungeun K. Jeon for Gnea Research LLC
6  Copyright (c) 2009-2011 Simen Svale Skogsrud
7 
8  Grbl is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  Grbl is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with Grbl. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include "grbl.h"
24 #define LINE_FLAG_OVERFLOW bit(0)
25 #define LINE_FLAG_COMMENT_PARENTHESES bit(1)
26 #define LINE_FLAG_COMMENT_SEMICOLON bit(2)
27 
28 
29 static char line[LINE_BUFFER_SIZE];
30 
31 static void protocol_exec_rt_suspend();
32 
33 
34 /*
35  GRBL PRIMARY LOOP:
36 */
38 {
39 // Perform some machine checks to make sure everything is good to go.
40  #ifdef CHECK_LIMITS_AT_INIT
42  if (limits_get_state()) {
45  }
46  }
47  #endif
48 // Check for and report alarm state after a reset, error, or an initial power up.
49 //
51 // Re-initialize the sleep state as an ALARM mode to ensure user homes or acknowledges.
52  if (sys.state & (STATE_ALARM | STATE_SLEEP)) {
55  } else {
56 // Check if the safety door is open.
61  }
62 // All systems go!
64  }
65 
66 // ---------------------------------------------------------------------------------
67 // Primary loop! Upon a system abort, this exits back to main() to reset the system.
68 // This is also where Grbl idles while waiting for something to do.
69 // ---------------------------------------------------------------------------------
70 
71  uint8_t line_flags = 0;
72  uint8_t char_counter = 0;
73  uint8_t c;
74  for (;;) {
75 
76 // Process one line of incoming serial data, as the data becomes available. Performs an
77 // initial filtering by removing spaces and comments and capitalizing all letters.
78  while((c = serial_read()) != SERIAL_NO_DATA) {
79  if ((c == '\n') || (c == '\r')) {
80 
82  if (sys.abort) { return; }
83 
84  line[char_counter] = 0;
85  #ifdef REPORT_ECHO_LINE_RECEIVED
87  #endif
88 
89 // Direct and execute one line of formatted input, and report status of execution.
90  if (line_flags & LINE_FLAG_OVERFLOW) {
91 // Report line overflow error.
93  } else if (line[0] == 0) {
94 // Empty or comment line. For syncing purposes.
96  } else if (line[0] == '$') {
97 // Grbl '$' system command
99  } else if (sys.state & (STATE_ALARM | STATE_JOG)) {
100 // Everything else is gcode. Block if in alarm or jog mode.
102  } else {
103 // Parse and execute g-code block.
105  }
106 
107 // Reset tracking data for next line.
108  line_flags = 0;
109  char_counter = 0;
110 
111  } else {
112 
113  if (line_flags) {
114 // Throw away all (except EOL) comment characters and overflow characters.
115  if (c == ')') {
116 // End of '()' comment. Resume line allowed.
117  if (line_flags & LINE_FLAG_COMMENT_PARENTHESES) { line_flags &= ~(LINE_FLAG_COMMENT_PARENTHESES); }
118  }
119  } else {
120  if (c <= ' ') {
121 // Throw away whitepace and control characters
122  } else if (c == '/') {
123 // Block delete NOT SUPPORTED. Ignore character.
124 //
126  } else if (c == '(') {
127 // Enable comments flag and ignore all characters until ')' or EOL.
128 //
130 // In the future, we could simply remove the items within the comments, but retain the
131 // comment control characters, so that the g-code parser can error-check it.
132  line_flags |= LINE_FLAG_COMMENT_PARENTHESES;
133  } else if (c == ';') {
134 //
136  line_flags |= LINE_FLAG_COMMENT_SEMICOLON;
138 // } else if (c == '%') {
139 // Program start-end percent sign NOT SUPPORTED.
140 //
142 // where, during a program, the system auto-cycle start will continue to execute
143 // everything until the next '%' sign. This will help fix resuming issues with certain
144 // functions that empty the planner buffer to execute its task on-time.
145  } else if (char_counter >= (LINE_BUFFER_SIZE-1)) {
146 // Detect line buffer overflow and set flag.
147  line_flags |= LINE_FLAG_OVERFLOW;
148  } else if (c >= 'a' && c <= 'z') {
149  line[char_counter++] = c-'a'+'A';
150  } else {
151  line[char_counter++] = c;
152  }
153  }
154 
155  }
156  }
157 
158 // If there are no more characters in the serial read buffer to be processed and executed,
159 // this indicates that g-code streaming has either filled the planner buffer or has
160 // completed. In either case, auto-cycle start, if enabled, any queued moves.
162 
164  if (sys.abort) { return; }
165 
166  #ifdef SLEEP_ENABLE
167 // Check for sleep conditions and execute auto-park, if timeout duration elapses.
168  sleep_check();
169  #endif
170  }
171 
172  return;
173 }
174 
176 // during a synchronize call, if it should happen. Also, waits for clean cycle end.
178 {
179 // If system is queued, ensure cycle resumes if the auto start flag is present.
181  do {
183  if (sys.abort) { return; }
184  } while (plan_get_current_block() || (sys.state == STATE_CYCLE));
185 }
186 
188 // actively parsing commands.
189 //
191 // when one of these conditions exist respectively: There are no more blocks sent (i.e. streaming
192 // is finished, single commands), a command that needs to wait for the motions in the buffer to
193 // execute calls a buffer sync, or the planner buffer is full and ready to go.
195 {
196  if (plan_get_current_block() != NULL) {
198  }
199 }
200 
202 // from various check points in the main program, primarily where there may be a while loop waiting
203 // for a buffer to clear space or any point where the execution time from the last check point may
204 // be more than a fraction of a second. This is a way to execute realtime commands asynchronously
205 // (aka multitasking) with grbl's g-code parsing and planning functions. This function also serves
206 // as an interface for the interrupts to set the system realtime flags, where only the main program
207 // handles them, removing the need to define more computationally-expensive volatile variables. This
208 // also provides a controlled way to execute certain tasks without having two or more instances of
209 // the same task, such as the planner recalculating the buffer upon a feedhold or overrides.
210 //
212 // limit switches, or the main program.
214 {
216  if (sys.suspend) { protocol_exec_rt_suspend(); }
217 }
218 
220 // machine and controls the various real-time features Grbl has to offer.
221 //
224 {
225  uint8_t rt_exec;
226  rt_exec = sys_rt_exec_alarm;
227  if (rt_exec) {
228 // System alarm. Everything has shutdown by something that has gone severely wrong. Report
229 // the source of the error to the user. If critical, Grbl disables by entering an infinite
230 // loop until system reset/abort.
231  sys.state = STATE_ALARM;
232  report_alarm_message(rt_exec);
233 // Halt everything upon a critical event flag. Currently hard and soft limits flag this.
234  if ((rt_exec == EXEC_ALARM_HARD_LIMIT) || (rt_exec == EXEC_ALARM_SOFT_LIMIT)) {
237  do {
238 // Block everything, except reset and status reports, until user issues reset or power
239 // cycles. Hard limits typically occur while unattended or not paying attention. Gives
240 // the user and a GUI time to do what is needed before resetting, like killing the
241 // incoming stream. The same could be said about soft limits. While the position is not
242 // lost, continued streaming could cause a serious crash if by chance it gets executed.
244  }
246  }
247 
248  rt_exec = sys_rt_exec_state;
249  if (rt_exec) {
250 
251 // Execute system abort.
252  if (rt_exec & EXEC_RESET) {
253  sys.abort = true;
254  return;
255  }
256 
257 // Execute and serial print status
258  if (rt_exec & EXEC_STATUS_REPORT) {
260  system_clear_exec_state_flag(EXEC_STATUS_REPORT);
261  }
262 
263 //
265 // main program processes until either reset or resumed. This ensures a hold completes safely.
267 
268 // State check for allowable states for hold methods.
269  if (!(sys.state & (STATE_ALARM | STATE_CHECK_MODE))) {
270 
271 // If in CYCLE or JOG states, immediately initiate a motion HOLD.
272  if (sys.state & (STATE_CYCLE | STATE_JOG)) {
276  if (sys.state == STATE_JOG) {
277  if (!(rt_exec & EXEC_SLEEP)) { sys.suspend |= SUSPEND_JOG_CANCEL; }
278  }
279  }
280  }
281 // If IDLE, Grbl is not in motion. Simply indicate suspend state and hold is complete.
283 
284 // Execute and flag a motion cancel with deceleration and return to idle. Used primarily by probing cycle
285 // to halt and cancel the remainder of the motion.
286  if (rt_exec & EXEC_MOTION_CANCEL) {
287 // MOTION_CANCEL only occurs during a CYCLE, but a HOLD and SAFETY_DOOR may been initiated beforehand
288 // to hold the CYCLE. Motion cancel is valid for a single planner block motion only, while jog cancel
289 // will handle and clear multiple planner block motions.
291  }
293 
294 // Execute a feed hold with deceleration, if required. Then, suspend system.
295  if (rt_exec & EXEC_FEED_HOLD) {
296 // Block SAFETY_DOOR, JOG, and SLEEP states from changing to HOLD state.
298  }
299 
300 // Execute a safety door stop with a feed hold and disable spindle/coolant.
301 //
303 // devices (spindle/coolant), and blocks resuming until switch is re-engaged.
304  if (rt_exec & EXEC_SAFETY_DOOR) {
306 // If jogging, block safety door methods until jog cancel is complete. Just flag that it happened.
307  if (!(sys.suspend & SUSPEND_JOG_CANCEL)) {
308 // Check if the safety re-opened during a restore parking motion only. Ignore if
309 // already retracting, parked or in sleep state.
310  if (sys.state == STATE_SAFETY_DOOR) {
312  #ifdef PARKING_ENABLE
313 // Set hold and reset appropriate control flags to restart parking sequence.
318  }
319  #endif
322  }
323  }
325  }
326 //
328 // are executed if the door switch closes and the state returns to HOLD.
330  }
331 
332  }
333 
334  if (rt_exec & EXEC_SLEEP) {
336  sys.state = STATE_SLEEP;
337  }
338 
340  }
341 
342 // Execute a cycle start by starting the stepper interrupt to begin executing the blocks in queue.
343  if (rt_exec & EXEC_CYCLE_START) {
344 // Block if called at same time as the hold commands: feed hold, motion cancel, and safety door.
345 // Ensures auto-cycle-start doesn't resume a hold without an explicit user-input.
346  if (!(rt_exec & (EXEC_FEED_HOLD | EXEC_MOTION_CANCEL | EXEC_SAFETY_DOOR))) {
347 // Resume door state when parking motion has retracted and door has been closed.
350  sys.state = STATE_IDLE;
351  } else if (sys.suspend & SUSPEND_RETRACT_COMPLETE) {
352 // Flag to re-energize powered components and restore original position, if disabled by SAFETY_DOOR.
353 //
355 // the retraction execution is complete, which implies the initial feed hold is not active. To
356 // restore normal operation, the restore procedures must be initiated by the following flag. Once,
357 // they are complete, it will call CYCLE_START automatically to resume and exit the suspend.
359  }
360  }
361 // Cycle start only when IDLE or when a hold is complete and ready to resume.
365  } else {
366 // Start cycle only if queued motions exist in planner buffer and the motion is not canceled.
371  st_prep_buffer();
372  st_wake_up();
373  } else {
375  sys.state = STATE_IDLE;
376  }
377  }
378  }
379  }
380  system_clear_exec_state_flag(EXEC_CYCLE_START);
381  }
382 
383  if (rt_exec & EXEC_CYCLE_STOP) {
384 // Reinitializes the cycle plan and stepper system after a feed hold for a resume. Called by
385 // realtime command execution in the main program, ensuring that the planner re-plans safely.
386 //
388 // cycle reinitializations. The stepper path should continue exactly as if nothing has happened.
389 //
392 // Hold complete. Set to indicate ready to resume. Remain in HOLD or DOOR states until user
393 // has issued a resume command or reset.
397  } else {
398 // Motion complete. Includes CYCLE/JOG/HOMING states and jog cancel/motion cancel/soft limit events.
399 //
401  if (sys.suspend & SUSPEND_JOG_CANCEL) {
403  plan_reset();
404  st_reset();
407  }
412  } else {
414  sys.state = STATE_IDLE;
415  }
416  }
417  system_clear_exec_state_flag(EXEC_CYCLE_STOP);
418  }
419  }
420 
421 // Execute overrides.
422  rt_exec = sys_rt_exec_motion_override;
423  if (rt_exec) {
425 
426  uint8_t new_f_override = sys.f_override;
427  if (rt_exec & EXEC_FEED_OVR_RESET) { new_f_override = DEFAULT_FEED_OVERRIDE; }
428  if (rt_exec & EXEC_FEED_OVR_COARSE_PLUS) { new_f_override += FEED_OVERRIDE_COARSE_INCREMENT; }
429  if (rt_exec & EXEC_FEED_OVR_COARSE_MINUS) { new_f_override -= FEED_OVERRIDE_COARSE_INCREMENT; }
430  if (rt_exec & EXEC_FEED_OVR_FINE_PLUS) { new_f_override += FEED_OVERRIDE_FINE_INCREMENT; }
431  if (rt_exec & EXEC_FEED_OVR_FINE_MINUS) { new_f_override -= FEED_OVERRIDE_FINE_INCREMENT; }
432  new_f_override = min(new_f_override,MAX_FEED_RATE_OVERRIDE);
433  new_f_override = max(new_f_override,MIN_FEED_RATE_OVERRIDE);
434 
435  uint8_t new_r_override = sys.r_override;
436  if (rt_exec & EXEC_RAPID_OVR_RESET) { new_r_override = DEFAULT_RAPID_OVERRIDE; }
437  if (rt_exec & EXEC_RAPID_OVR_MEDIUM) { new_r_override = RAPID_OVERRIDE_MEDIUM; }
438  if (rt_exec & EXEC_RAPID_OVR_LOW) { new_r_override = RAPID_OVERRIDE_LOW; }
439 
440  if ((new_f_override != sys.f_override) || (new_r_override != sys.r_override)) {
441  sys.f_override = new_f_override;
442  sys.r_override = new_r_override;
443  sys.report_ovr_counter = 0;
446  }
447  }
448 
450  if (rt_exec) {
452 
453 //
455  uint8_t last_s_override = sys.spindle_speed_ovr;
456  if (rt_exec & EXEC_SPINDLE_OVR_RESET) { last_s_override = DEFAULT_SPINDLE_SPEED_OVERRIDE; }
457  if (rt_exec & EXEC_SPINDLE_OVR_COARSE_PLUS) { last_s_override += SPINDLE_OVERRIDE_COARSE_INCREMENT; }
458  if (rt_exec & EXEC_SPINDLE_OVR_COARSE_MINUS) { last_s_override -= SPINDLE_OVERRIDE_COARSE_INCREMENT; }
459  if (rt_exec & EXEC_SPINDLE_OVR_FINE_PLUS) { last_s_override += SPINDLE_OVERRIDE_FINE_INCREMENT; }
460  if (rt_exec & EXEC_SPINDLE_OVR_FINE_MINUS) { last_s_override -= SPINDLE_OVERRIDE_FINE_INCREMENT; }
461  last_s_override = min(last_s_override,MAX_SPINDLE_SPEED_OVERRIDE);
462  last_s_override = max(last_s_override,MIN_SPINDLE_SPEED_OVERRIDE);
463 
464  if (last_s_override != sys.spindle_speed_ovr) {
466  sys.spindle_speed_ovr = last_s_override;
467  sys.report_ovr_counter = 0;
468  }
469 
470  if (rt_exec & EXEC_SPINDLE_OVR_STOP) {
471 // Spindle stop override allowed only while in HOLD state.
472 //
474  if (sys.state == STATE_HOLD) {
477  }
478  }
479 
480 //
482 // run state can be determined by checking the parser state.
484  if ((sys.state == STATE_IDLE) || (sys.state & (STATE_CYCLE | STATE_HOLD))) {
485  uint8_t coolant_state = gc_state.modal.coolant;
486  if (rt_exec & EXEC_COOLANT_MIST_OVR_TOGGLE) {
487  if (coolant_state & COOLANT_MIST_ENABLE) { bit_false(coolant_state,COOLANT_MIST_ENABLE); }
488  else { coolant_state |= COOLANT_MIST_ENABLE; }
489  }
490  if (rt_exec & EXEC_COOLANT_FLOOD_OVR_TOGGLE) {
491  if (coolant_state & COOLANT_FLOOD_ENABLE) { bit_false(coolant_state,COOLANT_FLOOD_ENABLE); }
492  else { coolant_state |= COOLANT_FLOOD_ENABLE; }
493  }
494  coolant_set_state(coolant_state);
495  gc_state.modal.coolant = coolant_state;
496  }
497  }
498  }
499 
500  #ifdef DEBUG
501  if (sys_rt_exec_debug) {
502  report_realtime_debug();
503  sys_rt_exec_debug = 0;
504  }
505  #endif
506 
507 // Reload step segment buffer
509  st_prep_buffer();
510  }
511 
512 }
513 
515 // The system will enter this loop, create local variables for suspend tasks, and return to
516 // whatever function that invoked the suspend, such that Grbl resumes normal operation.
517 // This function is written in a way to promote custom parking motions. Simply use this as a
518 // template
519 static void protocol_exec_rt_suspend()
520 {
521  #ifdef PARKING_ENABLE
522 // Declare and initialize parking local variables
523  float restore_target[N_AXIS];
524  float parking_target[N_AXIS];
525  float retract_waypoint = PARKING_PULLOUT_INCREMENT;
528  memset(pl_data,0,sizeof(plan_line_data_t));
531  #endif
532 
534  uint8_t restore_condition;
535  float restore_spindle_speed;
536  if (block == NULL) {
537  restore_condition = (gc_state.modal.spindle | gc_state.modal.coolant);
538  restore_spindle_speed = gc_state.spindle_speed;
539  } else {
540  restore_condition = block->condition;
541  restore_spindle_speed = block->spindle_speed;
542  }
543  #ifdef DISABLE_LASER_DURING_HOLD
546  }
547  #endif
548 
549  while (sys.suspend) {
550 
551  if (sys.abort) { return; }
552 
553 // Block until initial hold is complete and the machine has stopped motion.
555 
556 // Parking manager. Handles de/re-energizing, switch state checks, and parking motions for
557 // the safety door and sleep states.
559 
560 // Handles retraction motions and de-energizing.
562 
563 // Ensure any prior spindle stop override is disabled at start of safety door routine.
565 
566  #ifndef PARKING_ENABLE
567 
570 
571  #else
572 
573 // Get current position and store restore location and spindle retract waypoint.
576  memcpy(restore_target,parking_target,sizeof(parking_target));
577  retract_waypoint += restore_target[PARKING_AXIS];
578  retract_waypoint = min(retract_waypoint,PARKING_TARGET);
579  }
580 
581 // Execute slow pull-out parking retract motion. Parking requires homing enabled, the
582 // current location not exceeding the parking target location, and laser mode disabled.
583 //
586  (parking_target[PARKING_AXIS] < PARKING_TARGET) &&
588 
589 // Retract spindle by pullout distance. Ensure retraction motion moves away from
590 // the workpiece and waypoint motion doesn't exceed the parking target location.
591  if (parking_target[PARKING_AXIS] < retract_waypoint) {
592  parking_target[PARKING_AXIS] = retract_waypoint;
593  pl_data->feed_rate = PARKING_PULLOUT_RATE;
594  pl_data->condition |= (restore_condition & PL_COND_ACCESSORY_MASK);
595  pl_data->spindle_speed = restore_spindle_speed;
596  mc_parking_motion(parking_target, pl_data);
597  }
598 
599 //
602  pl_data->spindle_speed = 0.0;
605 
606 // Execute fast parking retract motion to parking target location.
607  if (parking_target[PARKING_AXIS] < PARKING_TARGET) {
608  parking_target[PARKING_AXIS] = PARKING_TARGET;
609  pl_data->feed_rate = PARKING_RATE;
610  mc_parking_motion(parking_target, pl_data);
611  }
612 
613  } else {
614 
615 // Parking motion not possible. Just disable the spindle and coolant.
616 //
620 
621  }
622 
623  #endif
624 
627 
628  } else {
629 
630 
631  if (sys.state == STATE_SLEEP) {
633 // Spindle and coolant should already be stopped, but do it again just to be sure.
636  st_go_idle();
637  while (!(sys.abort)) { protocol_exec_rt_system(); }
638  return;
639  }
640 
641 // Allows resuming from parking/safety door. Actively checks if safety door is closed and ready to resume.
642  if (sys.state == STATE_SAFETY_DOOR) {
645  }
646  }
647 
648 // Handles parking restore and safety door resume.
650 
651  #ifdef PARKING_ENABLE
652 // Execute fast restore motion to the pull-out position. Parking requires homing enabled.
653 //
656 // Check to ensure the motion doesn't move below pull-out position.
657  if (parking_target[PARKING_AXIS] <= PARKING_TARGET) {
658  parking_target[PARKING_AXIS] = retract_waypoint;
659  pl_data->feed_rate = PARKING_RATE;
660  mc_parking_motion(parking_target, pl_data);
661  }
662  }
663  #endif
664 
665 // Delayed Tasks: Restart spindle and coolant, delay to power-up, then resume cycle.
667 // Block if safety door re-opened during prior restore actions.
670 // When in laser mode, ignore spindle spin-up delay. Set to turn on laser when cycle starts.
672  } else {
673  spindle_set_state((restore_condition & (PL_COND_FLAG_SPINDLE_CW | PL_COND_FLAG_SPINDLE_CCW)), restore_spindle_speed);
675  }
676  }
677  }
679 // Block if safety door re-opened during prior restore actions.
681 //
685  }
686  }
687 
688  #ifdef PARKING_ENABLE
689 // Execute slow plunge motion from pull-out position to resume position.
691 // Block if safety door re-opened during prior restore actions.
693 // Regardless if the retract parking motion was a valid/safe motion or not, the
694 // restore parking motion should logically be valid, either by returning to the
695 // original position through valid machine space or by not moving at all.
696  pl_data->feed_rate = PARKING_PULLOUT_RATE;
697  pl_data->condition |= (restore_condition & PL_COND_ACCESSORY_MASK);
698  pl_data->spindle_speed = restore_spindle_speed;
699  mc_parking_motion(restore_target, pl_data);
700  }
701  }
702  #endif
703 
707  }
708  }
709 
710  }
711 
712 
713  } else {
714 
715 // Feed hold manager. Controls spindle stop override states.
716 //
718  if (sys.spindle_stop_ovr) {
719 // Handles beginning of spindle stop
724  } else {
726  }
727 // Handles restoring of spindle state
732 // When in laser mode, ignore spindle spin-up delay. Set to turn on laser when cycle starts.
734  } else {
735  spindle_set_state((restore_condition & (PL_COND_FLAG_SPINDLE_CW | PL_COND_FLAG_SPINDLE_CCW)), restore_spindle_speed);
736  }
737  }
740  }
742  }
743  } else {
744 // Handles spindle state during hold.
746 //
749  spindle_set_state((restore_condition & (PL_COND_FLAG_SPINDLE_CW | PL_COND_FLAG_SPINDLE_CCW)), restore_spindle_speed);
751  }
752  }
753 
754  }
755  }
756 
757  #ifdef SLEEP_ENABLE
758 // Check for sleep conditions and execute auto-park, if timeout duration elapses.
759 // Sleep is valid for both hold and door states, if the spindle or coolant are on or
760 // set to be re-enabled.
761  sleep_check();
762  #endif
763 
765 
766  }
767 }
void protocol_main_loop()
Starts Grbl main loop. It handles all incoming characters from the serial port and executes...
Definition: protocol.c:37
plan_block_t * plan_get_current_block()
Returns address of first planner block, if available. Called by various main program functions...
Definition: planner.c:231
#define DEFAULT_FEED_OVERRIDE
Definition: config.h:210
#define EXEC_RAPID_OVR_RESET
Definition: system.h:56
void plan_sync_position()
Reset the planner position vectors. Called by the system abort/initialization routine.
Definition: planner.c:471
#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 EXEC_FEED_OVR_COARSE_PLUS
Definition: system.h:52
void protocol_auto_cycle_start()
Auto-cycle start triggers when there is a motion ready to execute and if the main program is not...
Definition: protocol.c:194
#define SAFETY_DOOR_COOLANT_DELAY
Float (seconds)
Definition: config.h:159
uint8_t serial_read()
Fetches the first byte in the serial read buffer. Called by main program.
Definition: serial.c:122
#define EXEC_RAPID_OVR_MEDIUM
Definition: system.h:57
#define STATE_HOLD
Active feed hold.
Definition: system.h:77
#define DEFAULT_RAPID_OVERRIDE
100%. Don't change this value.
Definition: config.h:216
void spindle_set_state(uint8_t state, float rpm)
Immediately sets spindle running state with direction and spindle rpm via PWM, if enabled...
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 spindle_speed_ovr
Spindle speed value in percent.
Definition: system.h:121
#define MESSAGE_SLEEP_MODE
Definition: report.h:81
void delay_sec(float seconds, uint8_t mode)
Non-blocking delay function used for general operation and suspend features.
Definition: nuts_bolts.c:111
#define SUSPEND_SAFETY_DOOR_AJAR
Tracks safety door state for resuming.
Definition: system.h:88
#define MIN_SPINDLE_SPEED_OVERRIDE
Percent of programmed spindle speed (1-100). Usually 10%.
Definition: config.h:223
void gc_sync_position()
Sets g-code parser position in mm. Input in steps. Called by the system abort and hard...
Definition: gcode.c:53
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 LINE_FLAG_OVERFLOW
Define line flags. Includes comment type tracking and line overflow detection.
Definition: protocol.c:24
#define bit_true(x, mask)
Definition: nuts_bolts.h:58
#define STEP_CONTROL_UPDATE_SPINDLE_PWM
Definition: system.h:96
uint8_t system_execute_line(char *line)
Directs and executes one line of formatted input from protocol_process. While mostly.
Definition: system.c:106
void report_alarm_message(uint8_t alarm_code)
Prints alarm messages.
Definition: report.c:122
void report_realtime_status()
Prints realtime status report.
Definition: report.c:414
#define SUSPEND_INITIATE_RESTORE
(Safety door only) Flag to initiate resume procedures from a cycle start.
Definition: system.h:86
gc_modal_t modal
Definition: gcode.h:196
#define PARKING_MOTION_LINE_NUMBER
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
#define STATUS_OVERFLOW
Definition: report.h:34
void protocol_buffer_synchronize()
Block until all buffered steps are executed or in a cycle state. Works with feed hold.
Definition: protocol.c:177
uint8_t suspend
System suspend bitflag variable that manages holds, cancels, and safety door.
Definition: system.h:114
Planner data prototype. Must be used when passing new motions to the planner.
Definition: planner.h:78
void system_clear_exec_accessory_overrides()
Definition: system.c:389
#define COOLANT_MIST_ENABLE
NOTE: Uses planner condition bit flag)
Definition: gcode.h:114
void report_feedback_message(uint8_t message_code)
Prints feedback messages. This serves as a centralized method to provide additional.
Definition: report.c:135
#define PARKING_TARGET
Parking axis target. In mm, as machine coordinate [-max_travel,0].
Definition: config.h:514
#define EXEC_CYCLE_STOP
bitmask 00000100
Definition: system.h:33
float spindle_speed
Desired spindle speed through line motion.
Definition: planner.h:80
void plan_cycle_reinitialize()
Re-initialize buffer plan with a partially completed block, assumed to exist at the buffer tail...
Definition: planner.c:509
void plan_update_velocity_profile_parameters()
Re-calculates buffered motions profile parameters upon a motion-based override change.
Definition: planner.c:278
#define PL_COND_ACCESSORY_MASK
Definition: planner.h:42
#define MESSAGE_ALARM_LOCK
Definition: report.h:72
#define PL_COND_FLAG_SPINDLE_CW
Definition: planner.h:37
void sleep_check()
Checks running conditions for sleep. If satisfied, enables sleep countdown and executes.
Definition: sleep.c:88
volatile uint8_t sys_rt_exec_alarm
Global realtime executor bitflag variable for setting various alarms.
Definition: system.h:135
#define MAX_SPINDLE_SPEED_OVERRIDE
Percent of programmed spindle speed (100-255). Usually 200%.
Definition: config.h:222
uint8_t condition
Block bitflag variable defining block run conditions. Copied from pl_line_data.
Definition: planner.h:55
#define SUSPEND_RESTART_RETRACT
Flag to indicate a retract from a restore parking motion.
Definition: system.h:84
uint8_t state
Tracks the current system state of Grbl.
Definition: system.h:112
#define EXEC_RESET
bitmask 00010000
Definition: system.h:35
#define PARKING_PULLOUT_RATE
Pull-out/plunge slow feed rate in mm/min.
Definition: config.h:516
#define STEP_CONTROL_EXECUTE_HOLD
Definition: system.h:94
#define PARKING_RATE
Parking fast rate after pull-out in mm/min.
Definition: config.h:515
#define DEFAULT_SPINDLE_SPEED_OVERRIDE
100%. Don't change this value.
Definition: config.h:221
#define EXEC_FEED_OVR_COARSE_MINUS
Definition: system.h:53
#define BITFLAG_LASER_MODE
Definition: settings.h:36
#define SUSPEND_RETRACT_COMPLETE
(Safety door only) Indicates retraction and de-energizing is complete.
Definition: system.h:85
void plan_reset()
Initialize and reset the motion plan subsystem.
Definition: planner.c:198
uint8_t f_override
Feed rate override value in percent.
Definition: system.h:119
#define EXEC_SPINDLE_OVR_STOP
Definition: system.h:66
#define EXEC_SAFETY_DOOR
bitmask 00100000
Definition: system.h:36
#define BITFLAG_HOMING_ENABLE
Definition: settings.h:39
#define MESSAGE_CHECK_LIMITS
Definition: report.h:77
#define PL_COND_FLAG_SYSTEM_MOTION
Single motion. Circumvents planner state. Used by home/park.
Definition: planner.h:34
float spindle_speed
Block spindle speed. Copied from pl_line_data.
Definition: planner.h:74
#define LINE_BUFFER_SIZE
Line buffer size from the serial input stream to be executed.
Definition: protocol.h:26
#define SPINDLE_DISABLE
Modal Group M7: Spindle control.
Definition: gcode.h:105
#define max(a, b)
Definition: nuts_bolts.h:53
#define MIN_FEED_RATE_OVERRIDE
Percent of programmed feed rate (1-100). Usually 50% or 1%.
Definition: config.h:212
uint8_t abort
System abort flag. Forces exit back to main loop for reset.
Definition: system.h:113
void mc_parking_motion(float *parking_target, plan_line_data_t *pl_data)
Plans and executes the single special motion case for parking. Independent of main planner buffer...
void system_clear_exec_alarm()
Definition: system.c:361
#define STATE_SLEEP
Sleep state.
Definition: system.h:80
#define SAFETY_DOOR_SPINDLE_DELAY
This option causes the feed hold input to act as a safety door switch. A safety door, when triggered,.
Definition: config.h:158
volatile uint8_t sys_rt_exec_motion_override
Global realtime executor bitflag variable for motion-based overrides.
Definition: system.h:136
#define EXEC_ALARM_SOFT_LIMIT
Definition: system.h:41
#define STATE_CYCLE
Cycle is running or motions are being executed.
Definition: system.h:76
uint8_t flags
Contains default boolean settings.
Definition: settings.h:92
#define STATUS_SYSTEM_GC_LOCK
Definition: report.h:32
#define STATE_CHECK_MODE
G-code check mode. Locks out planner and motion only.
Definition: system.h:74
#define PL_COND_FLAG_COOLANT_FLOOD
Definition: planner.h:39
#define RAPID_OVERRIDE_MEDIUM
Percent of rapid (1-99). Usually 50%.
Definition: config.h:217
#define STATE_ALARM
In alarm state. Locks out all g-code processes. Allows settings access.
Definition: system.h:73
#define EXEC_CYCLE_START
bitmask 00000010
Definition: system.h:32
void report_echo_line_received(char *line)
Prints the character string line Grbl has received from the user, which has been pre-parsed,.
Definition: report.c:402
system_t sys
Declare system global variable structure.
Definition: main.c:25
uint8_t soft_limit
Tracks soft limit errors for the state machine. (boolean)
Definition: system.h:115
#define EXEC_COOLANT_MIST_OVR_TOGGLE
Definition: system.h:68
int32_t sys_position[N_AXIS]
NOTE: These position variables may need to be declared as volatiles, if problems arise.
Definition: system.h:130
uint8_t report_ovr_counter
Tracks when to add override data to status reports.
Definition: system.h:123
#define MESSAGE_SAFETY_DOOR_AJAR
Definition: report.h:76
uint8_t coolant
{M7,M8,M9}
Definition: gcode.h:177
#define LINE_FLAG_COMMENT_SEMICOLON
Definition: protocol.c:26
void st_go_idle()
Stepper shutdown.
Definition: stepper.c:224
#define EXEC_SPINDLE_OVR_FINE_PLUS
Definition: system.h:64
#define EXEC_SPINDLE_OVR_RESET
Definition: system.h:61
#define EXEC_FEED_OVR_FINE_PLUS
Definition: system.h:54
void protocol_exec_rt_system()
Executes run-time commands, when required. This function primarily operates as Grbl's state...
Definition: protocol.c:223
void system_clear_exec_state_flag(uint8_t mask)
Definition: system.c:347
#define bit_isfalse(x, mask)
Definition: nuts_bolts.h:61
#define SPINDLE_STOP_OVR_ENABLED
Definition: system.h:105
#define EXEC_COOLANT_FLOOD_OVR_TOGGLE
Definition: system.h:67
uint8_t step_control
Governs the step segment generator depending on system state.
Definition: system.h:116
void st_prep_buffer()
Prepares step segment buffer. Continuously called from main program.
Definition: stepper.c:594
#define COOLANT_DISABLE
NOTE: Uses planner condition bit flag)
Definition: gcode.h:111
void st_wake_up()
BLOCK VELOCITY PROFILE DEFINITION.
Definition: stepper.c:199
int32_t line_number
Desired line number to report when executing.
Definition: planner.h:81
#define EXEC_MOTION_CANCEL
bitmask 01000000
Definition: system.h:37
#define SUSPEND_MOTION_CANCEL
Indicates a canceled resume motion. Currently used by probing routine.
Definition: system.h:89
#define BITFLAG_HARD_LIMIT_ENABLE
Definition: settings.h:38
#define EXEC_SPINDLE_OVR_COARSE_MINUS
Definition: system.h:63
#define EXEC_SLEEP
bitmask 10000000
Definition: system.h:38
#define EXEC_ALARM_HARD_LIMIT
Alarm executor codes. Valid values (1-255). Zero is reserved.
Definition: system.h:40
volatile uint8_t sys_rt_exec_state
Global realtime executor bitflag variable for state management. See EXEC bitmasks.
Definition: system.h:134
#define MAX_FEED_RATE_OVERRIDE
Percent of programmed feed rate (100-255). Usually 120% or 200%.
Definition: config.h:211
memset(pl_data, 0, sizeof(plan_line_data_t))
Zero pl_data struct.
uint8_t spindle
{M3,M4,M5}
Definition: gcode.h:178
#define EXEC_FEED_HOLD
bitmask 00001000
Definition: system.h:34
memcpy(block_coord_system, gc_state.coord_system, sizeof(gc_state.coord_system))
#define STEP_CONTROL_NORMAL_OP
Define step segment generator state flags.
Definition: system.h:92
#define SPINDLE_STOP_OVR_RESTORE
Definition: system.h:107
void system_set_exec_accessory_override_flag(uint8_t mask)
Definition: system.c:375
uint8_t r_override
Rapids override value in percent.
Definition: system.h:120
#define PL_COND_FLAG_NO_FEED_OVERRIDE
Motion does not honor feed override.
Definition: planner.h:35
settings_t settings
Definition: settings.c:24
#define COOLANT_FLOOD_ENABLE
M8 (.
Definition: gcode.h:112
#define MESSAGE_CRITICAL_EVENT
Define Grbl feedback message codes. Valid values (0-255).
Definition: report.h:71
#define EXEC_SPINDLE_OVR_FINE_MINUS
Definition: system.h:65
#define SPINDLE_STOP_OVR_INITIATE
Definition: system.h:106
plan_line_data_t plan_data
STEP 4: EXECUTE!! Assumes that all error-checking has been completed and no failure modes exist...
Definition: gcode.c:858
#define FEED_OVERRIDE_COARSE_INCREMENT
(1-99). Usually 10%.
Definition: config.h:213
#define SPINDLE_OVERRIDE_FINE_INCREMENT
(1-99).
Definition: config.h:225
#define LINE_FLAG_COMMENT_PARENTHESES
Definition: protocol.c:25
#define MESSAGE_SPINDLE_RESTORE
Definition: report.h:80
#define SPINDLE_STOP_OVR_DISABLED
Define spindle stop override control states.
Definition: system.h:104
#define SPINDLE_OVERRIDE_COARSE_INCREMENT
(1-99). Usually 10%.
Definition: config.h:224
#define bit_false(x, mask)
Definition: nuts_bolts.h:59
#define EXEC_RAPID_OVR_LOW
Definition: system.h:58
float spindle_speed
RPM.
Definition: gcode.h:198
#define min(a, b)
Definition: nuts_bolts.h:54
#define SUSPEND_RESTORE_COMPLETE
(Safety door only) Indicates ready to resume normal operation.
Definition: system.h:87
#define STATE_JOG
Jogging mode.
Definition: system.h:78
#define STATE_HOMING
Performing homing cycle.
Definition: system.h:75
#define SPINDLE_STOP_OVR_RESTORE_CYCLE
Definition: system.h:108
void protocol_execute_realtime()
This function is the general interface to Grbl's real-time command execution system. It is called.
Definition: protocol.c:213
float feed_rate
Desired feed rate for line motion. Value is ignored, if rapid motion.
Definition: planner.h:79
#define SERIAL_NO_DATA
Definition: serial.h:33
#define RAPID_OVERRIDE_LOW
Percent of rapid (1-99). Usually 25%.
Definition: config.h:218
#define SUSPEND_HOLD_COMPLETE
Indicates initial feed hold is complete.
Definition: system.h:83
#define SUSPEND_JOG_CANCEL
Indicates a jog cancel in process and to reset buffers when complete.
Definition: system.h:90
uint8_t spindle_stop_ovr
Tracks spindle stop override states.
Definition: system.h:122
uint8_t condition
Bitflag variable to indicate planner conditions. See defines above.
Definition: planner.h:82
This struct stores a linear movement of a g-code block motion with its critical "nominal" values...
Definition: planner.h:46
void st_update_plan_block_parameters()
Called by planner_recalculate() when the executing block is updated by the new plan.
Definition: stepper.c:526
volatile uint8_t sys_rt_exec_accessory_override
Global realtime executor bitflag variable for spindle/coolant overrides.
Definition: system.h:137
#define PARKING_AXIS
By default, Grbl disables feed rate overrides for all G38.x probe cycle commands. Although this...
Definition: config.h:513
#define STEP_CONTROL_EXECUTE_SYS_MOTION
Definition: system.h:95
#define EXEC_STATUS_REPORT
Define system executor bit map. Used internally by realtime protocol as realtime command flags...
Definition: system.h:31
#define EXEC_FEED_OVR_RESET
Override bit maps. Realtime bitflags to control feed, rapid, spindle, and coolant overrides...
Definition: system.h:51
#define PARKING_PULLOUT_INCREMENT
Spindle pull-out and plunge distance in mm. Incremental distance.
Definition: config.h:517
#define SUSPEND_DISABLE
Define system suspend flags. Used in various ways to manage suspend states and procedures.
Definition: system.h:82
parser_state_t gc_state
Declare gc extern struct.
Definition: gcode.c:35
#define PL_COND_FLAG_SPINDLE_CCW
Definition: planner.h:38
void system_execute_startup(char *line)
Executes user startup script, if stored.
Definition: system.c:82
#define FEED_OVERRIDE_FINE_INCREMENT
(1-99). Usually 1%.
Definition: config.h:214
plan_line_data_t * pl_data
Definition: gcode.c:859
#define STATUS_OK
Define Grbl status codes. Valid values (0-255)
Definition: report.h:23
void st_reset()
Reset and clear stepper subsystem variables.
Definition: stepper.c:476
uint8_t limits_get_state()
Returns limit state as a bit-wise uint8 variable. Each bit indicates an axis limit, where.
Definition: limits.c:66
void system_clear_exec_motion_overrides()
Definition: system.c:382
#define EXEC_FEED_OVR_FINE_MINUS
Definition: system.h:55
#define STATE_IDLE
Define system state bit map. The state variable primarily tracks the individual functions.
Definition: system.h:72
#define DELAY_MODE_SYS_SUSPEND
Definition: nuts_bolts.h:48
#define EXEC_SPINDLE_OVR_COARSE_PLUS
Definition: system.h:62
void coolant_set_state(uint8_t mode)
Main program only. Immediately sets flood coolant running state and also mist coolant,.
#define bit_istrue(x, mask)
Definition: nuts_bolts.h:60
#define STATE_SAFETY_DOOR
Safety door is ajar. Feed holds and de-energizes system.
Definition: system.h:79