2009-01-25 00:48:56 +01:00
|
|
|
/*
|
2014-01-11 04:22:10 +01:00
|
|
|
protocol.c - controls Grbl execution procedures
|
2009-01-25 00:48:56 +01:00
|
|
|
Part of Grbl
|
|
|
|
|
2013-12-31 06:02:05 +01:00
|
|
|
Copyright (c) 2011-2014 Sungeun K. Jeon
|
2011-01-14 16:45:18 +01:00
|
|
|
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
2009-01-25 00:48:56 +01:00
|
|
|
|
|
|
|
Grbl is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Grbl is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Grbl. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2014-01-11 04:22:10 +01:00
|
|
|
#include "system.h"
|
2011-05-31 22:45:38 +02:00
|
|
|
#include "serial.h"
|
2011-02-05 00:45:41 +01:00
|
|
|
#include "settings.h"
|
2014-01-11 04:22:10 +01:00
|
|
|
#include "protocol.h"
|
|
|
|
#include "gcode.h"
|
2011-12-09 02:47:48 +01:00
|
|
|
#include "stepper.h"
|
2012-11-01 16:37:27 +01:00
|
|
|
#include "motion_control.h"
|
2014-01-11 04:22:10 +01:00
|
|
|
#include "report.h"
|
2009-01-25 00:48:56 +01:00
|
|
|
|
2013-10-30 02:10:39 +01:00
|
|
|
|
2014-01-11 04:22:10 +01:00
|
|
|
static char line[LINE_BUFFER_SIZE]; // Line to be executed. Zero-terminated.
|
2011-12-09 02:47:48 +01:00
|
|
|
|
2013-10-30 02:10:39 +01:00
|
|
|
|
2011-12-09 02:47:48 +01:00
|
|
|
// Executes run-time commands, when required. This is called from various check points in the main
|
|
|
|
// program, primarily where there may be a while loop waiting for a buffer to clear space or any
|
|
|
|
// point where the execution time from the last check point may be more than a fraction of a second.
|
|
|
|
// This is a way to execute runtime commands asynchronously (aka multitasking) with grbl's g-code
|
2012-01-06 18:10:41 +01:00
|
|
|
// parsing and planning functions. This function also serves as an interface for the interrupts to
|
2012-10-22 00:55:59 +02:00
|
|
|
// set the system runtime flags, where only the main program handles them, removing the need to
|
2012-11-01 16:37:27 +01:00
|
|
|
// define more computationally-expensive volatile variables. This also provides a controlled way to
|
|
|
|
// execute certain tasks without having two or more instances of the same task, such as the planner
|
|
|
|
// recalculating the buffer upon a feedhold or override.
|
2012-11-15 01:36:29 +01:00
|
|
|
// NOTE: The sys.execute variable flags are set by any process, step or serial interrupts, pinouts,
|
|
|
|
// limit switches, or the main program.
|
2011-12-09 02:47:48 +01:00
|
|
|
void protocol_execute_runtime()
|
|
|
|
{
|
2013-10-30 02:10:39 +01:00
|
|
|
// Reload step segment buffer
|
|
|
|
st_prep_buffer();
|
|
|
|
|
2012-01-06 18:10:41 +01:00
|
|
|
if (sys.execute) { // Enter only if any bit flag is true
|
|
|
|
uint8_t rt_exec = sys.execute; // Avoid calling volatile multiple times
|
Hard limits, homing direction, pull-off limits after homing, status reports in mm or inches, system alarm, and more.
- Thank you statement added for Alden Hart of Synthetos.
- Hard limits option added, which also works with homing by pulling off
the switches to help prevent unintended triggering. Hard limits use a
interrupt to sense a falling edge pin change and immediately go into
alarm mode, which stops everything and forces the user to issue a reset
(Ctrl-x) or reboot.
- Auto cycle start now a configuration option.
- Alarm mode: A new method to kill all Grbl processes in the event of
something catastrophic or potentially catastropic. Just works with hard
limits for now, but will be expanded to include g-code errors (most
likely) and other events.
- Updated status reports to be configurable in inches or mm mode. Much
more to do here, but this is the first step.
- New settings: auto cycle start, hard limit enable, homing direction
mask (which works the same as the stepper mask), homing pulloff
distance (or distance traveled from homed machine zero to prevent
accidental limit trip).
- Minor memory liberation and calculation speed ups.
2012-10-17 05:29:45 +02:00
|
|
|
|
2012-11-15 01:36:29 +01:00
|
|
|
// System alarm. Everything has shutdown by something that has gone severely wrong. Report
|
|
|
|
// the source of the error to the user. If critical, Grbl disables by entering an infinite
|
|
|
|
// loop until system reset/abort.
|
|
|
|
if (rt_exec & (EXEC_ALARM | EXEC_CRIT_EVENT)) {
|
|
|
|
sys.state = STATE_ALARM; // Set system alarm state
|
2013-10-30 02:10:39 +01:00
|
|
|
|
|
|
|
// Critical event. Only hard/soft limit errors currently qualify.
|
|
|
|
if (rt_exec & EXEC_CRIT_EVENT) {
|
|
|
|
report_alarm_message(ALARM_LIMIT_ERROR);
|
2012-11-06 05:40:52 +01:00
|
|
|
report_feedback_message(MESSAGE_CRITICAL_EVENT);
|
2012-11-15 01:36:29 +01:00
|
|
|
bit_false(sys.execute,EXEC_RESET); // Disable any existing reset
|
|
|
|
do {
|
|
|
|
// Nothing. Block EVERYTHING until user issues reset or power cycles. Hard limits
|
|
|
|
// typically occur while unattended or not paying attention. Gives the user time
|
2013-10-30 02:10:39 +01:00
|
|
|
// to do what is needed before resetting, like killing the incoming stream. The
|
|
|
|
// same could be said about soft limits. While the position is not lost, the incoming
|
|
|
|
// stream could be still engaged and cause a serious crash if it continues afterwards.
|
2012-11-15 01:36:29 +01:00
|
|
|
} while (bit_isfalse(sys.execute,EXEC_RESET));
|
|
|
|
|
|
|
|
// Standard alarm event. Only abort during motion qualifies.
|
|
|
|
} else {
|
|
|
|
// Runtime abort command issued during a cycle, feed hold, or homing cycle. Message the
|
|
|
|
// user that position may have been lost and set alarm state to enable the alarm lockout
|
|
|
|
// to indicate the possible severity of the problem.
|
|
|
|
report_alarm_message(ALARM_ABORT_CYCLE);
|
2012-11-06 05:40:52 +01:00
|
|
|
}
|
2012-11-15 01:36:29 +01:00
|
|
|
bit_false(sys.execute,(EXEC_ALARM | EXEC_CRIT_EVENT));
|
Hard limits, homing direction, pull-off limits after homing, status reports in mm or inches, system alarm, and more.
- Thank you statement added for Alden Hart of Synthetos.
- Hard limits option added, which also works with homing by pulling off
the switches to help prevent unintended triggering. Hard limits use a
interrupt to sense a falling edge pin change and immediately go into
alarm mode, which stops everything and forces the user to issue a reset
(Ctrl-x) or reboot.
- Auto cycle start now a configuration option.
- Alarm mode: A new method to kill all Grbl processes in the event of
something catastrophic or potentially catastropic. Just works with hard
limits for now, but will be expanded to include g-code errors (most
likely) and other events.
- Updated status reports to be configurable in inches or mm mode. Much
more to do here, but this is the first step.
- New settings: auto cycle start, hard limit enable, homing direction
mask (which works the same as the stepper mask), homing pulloff
distance (or distance traveled from homed machine zero to prevent
accidental limit trip).
- Minor memory liberation and calculation speed ups.
2012-10-17 05:29:45 +02:00
|
|
|
}
|
2011-12-09 02:47:48 +01:00
|
|
|
|
2012-11-15 01:36:29 +01:00
|
|
|
// Execute system abort.
|
2012-01-06 18:10:41 +01:00
|
|
|
if (rt_exec & EXEC_RESET) {
|
2012-11-05 21:32:29 +01:00
|
|
|
sys.abort = true; // Only place this is set true.
|
2011-12-09 02:47:48 +01:00
|
|
|
return; // Nothing else to do but exit.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute and serial print status
|
2012-01-06 18:10:41 +01:00
|
|
|
if (rt_exec & EXEC_STATUS_REPORT) {
|
2012-11-01 16:37:27 +01:00
|
|
|
report_realtime_status();
|
2012-01-10 16:34:48 +01:00
|
|
|
bit_false(sys.execute,EXEC_STATUS_REPORT);
|
2011-12-09 02:47:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initiate stepper feed hold
|
2012-01-06 18:10:41 +01:00
|
|
|
if (rt_exec & EXEC_FEED_HOLD) {
|
2013-10-30 02:10:39 +01:00
|
|
|
// !!! During a cycle, the segment buffer has just been reloaded and full. So the math involved
|
|
|
|
// with the feed hold should be fine for most, if not all, operational scenarios.
|
2012-01-06 18:10:41 +01:00
|
|
|
st_feed_hold(); // Initiate feed hold.
|
|
|
|
bit_false(sys.execute,EXEC_FEED_HOLD);
|
2011-12-09 02:47:48 +01:00
|
|
|
}
|
|
|
|
|
New startup script setting. New dry run, check gcode switches. New system state variable. Lots of reorganizing.
(All v0.8 features installed. Still likely buggy, but now thourough
testing will need to start to squash them all. As soon as we're done,
this will be pushed to master and v0.9 development will be started.
Please report ANY issues to us so we can get this rolled out ASAP.)
- User startup script! A user can now save one (up to 5 as compile-time
option) block of g-code in EEPROM memory. This will be run everytime
Grbl resets. Mainly to be used as a way to set your preferences, like
G21, G54, etc.
- New dry run and check g-code switches. Dry run moves ALL motions at
rapids rate ignoring spindle, coolant, and dwell commands. For rapid
physical proofing of your code. The check g-code switch ignores all
motion and provides the user a way to check if there are any errors in
their program that Grbl may not like.
- Program restart! (sort of). Program restart is typically an advanced
feature that allows users to restart a program mid-stream. The check
g-code switch can perform this feature by enabling the switch at the
start of the program, and disabling it at the desired point with some
minimal changes.
- New system state variable. This state variable tracks all of the
different state processes that Grbl performs, i.e. cycle start, feed
hold, homing, etc. This is mainly for making managing of these task
easier and more clear.
- Position lost state variable. Only when homing is enabled, Grbl will
refuse to move until homing is completed and position is known. This is
mainly for safety. Otherwise, it will let users fend for themselves.
- Moved the default settings defines into config.h. The plan is to
eventually create a set of config.h's for particular as-built machines
to help users from doing it themselves.
- Moved around misc defines into .h files. And lots of other little
things.
2012-11-03 18:32:23 +01:00
|
|
|
// Reinitializes the stepper module running state and, if a feed hold, re-plans the buffer.
|
2012-01-06 18:10:41 +01:00
|
|
|
// NOTE: EXEC_CYCLE_STOP is set by the stepper subsystem when a cycle or feed hold completes.
|
|
|
|
if (rt_exec & EXEC_CYCLE_STOP) {
|
2011-12-09 02:47:48 +01:00
|
|
|
st_cycle_reinitialize();
|
2012-01-06 18:10:41 +01:00
|
|
|
bit_false(sys.execute,EXEC_CYCLE_STOP);
|
2011-12-09 02:47:48 +01:00
|
|
|
}
|
|
|
|
|
2012-01-06 18:10:41 +01:00
|
|
|
if (rt_exec & EXEC_CYCLE_START) {
|
2011-12-09 02:47:48 +01:00
|
|
|
st_cycle_start(); // Issue cycle start command to stepper subsystem
|
Hard limits, homing direction, pull-off limits after homing, status reports in mm or inches, system alarm, and more.
- Thank you statement added for Alden Hart of Synthetos.
- Hard limits option added, which also works with homing by pulling off
the switches to help prevent unintended triggering. Hard limits use a
interrupt to sense a falling edge pin change and immediately go into
alarm mode, which stops everything and forces the user to issue a reset
(Ctrl-x) or reboot.
- Auto cycle start now a configuration option.
- Alarm mode: A new method to kill all Grbl processes in the event of
something catastrophic or potentially catastropic. Just works with hard
limits for now, but will be expanded to include g-code errors (most
likely) and other events.
- Updated status reports to be configurable in inches or mm mode. Much
more to do here, but this is the first step.
- New settings: auto cycle start, hard limit enable, homing direction
mask (which works the same as the stepper mask), homing pulloff
distance (or distance traveled from homed machine zero to prevent
accidental limit trip).
- Minor memory liberation and calculation speed ups.
2012-10-17 05:29:45 +02:00
|
|
|
if (bit_istrue(settings.flags,BITFLAG_AUTO_START)) {
|
2012-01-06 18:10:41 +01:00
|
|
|
sys.auto_start = true; // Re-enable auto start after feed hold.
|
Hard limits, homing direction, pull-off limits after homing, status reports in mm or inches, system alarm, and more.
- Thank you statement added for Alden Hart of Synthetos.
- Hard limits option added, which also works with homing by pulling off
the switches to help prevent unintended triggering. Hard limits use a
interrupt to sense a falling edge pin change and immediately go into
alarm mode, which stops everything and forces the user to issue a reset
(Ctrl-x) or reboot.
- Auto cycle start now a configuration option.
- Alarm mode: A new method to kill all Grbl processes in the event of
something catastrophic or potentially catastropic. Just works with hard
limits for now, but will be expanded to include g-code errors (most
likely) and other events.
- Updated status reports to be configurable in inches or mm mode. Much
more to do here, but this is the first step.
- New settings: auto cycle start, hard limit enable, homing direction
mask (which works the same as the stepper mask), homing pulloff
distance (or distance traveled from homed machine zero to prevent
accidental limit trip).
- Minor memory liberation and calculation speed ups.
2012-10-17 05:29:45 +02:00
|
|
|
}
|
2012-01-06 18:10:41 +01:00
|
|
|
bit_false(sys.execute,EXEC_CYCLE_START);
|
Hard limits, homing direction, pull-off limits after homing, status reports in mm or inches, system alarm, and more.
- Thank you statement added for Alden Hart of Synthetos.
- Hard limits option added, which also works with homing by pulling off
the switches to help prevent unintended triggering. Hard limits use a
interrupt to sense a falling edge pin change and immediately go into
alarm mode, which stops everything and forces the user to issue a reset
(Ctrl-x) or reboot.
- Auto cycle start now a configuration option.
- Alarm mode: A new method to kill all Grbl processes in the event of
something catastrophic or potentially catastropic. Just works with hard
limits for now, but will be expanded to include g-code errors (most
likely) and other events.
- Updated status reports to be configurable in inches or mm mode. Much
more to do here, but this is the first step.
- New settings: auto cycle start, hard limit enable, homing direction
mask (which works the same as the stepper mask), homing pulloff
distance (or distance traveled from homed machine zero to prevent
accidental limit trip).
- Minor memory liberation and calculation speed ups.
2012-10-17 05:29:45 +02:00
|
|
|
}
|
2012-11-01 16:37:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Overrides flag byte (sys.override) and execution should be installed here, since they
|
|
|
|
// are runtime and require a direct and controlled interface to the main stepper program.
|
2011-12-09 02:47:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-01 16:37:27 +01:00
|
|
|
// Directs and executes one line of formatted input from protocol_process. While mostly
|
2014-01-11 04:22:10 +01:00
|
|
|
// incoming streaming g-code blocks, this also directs and executes Grbl internal commands,
|
|
|
|
// such as settings, initiating the homing cycle, and toggling switch states.
|
|
|
|
// TODO: Eventually re-organize this function to more cleanly organize order of operations,
|
|
|
|
// which will hopefully reduce some of the current spaghetti logic and dynamic memory usage.
|
|
|
|
static void protocol_execute_line(char *line)
|
|
|
|
{
|
|
|
|
protocol_execute_runtime(); // Runtime command check point.
|
|
|
|
if (sys.abort) { return; } // Bail to calling function upon system abort
|
|
|
|
|
|
|
|
uint8_t status;
|
|
|
|
if (line[0] == 0) {
|
|
|
|
// Empty or comment line. Send status message for syncing purposes.
|
|
|
|
status = STATUS_OK;
|
|
|
|
|
|
|
|
} else if (line[0] == '$') {
|
|
|
|
// Grbl '$' system command
|
|
|
|
status = system_execute_line(line);
|
|
|
|
|
2011-02-18 22:59:16 +01:00
|
|
|
} else {
|
2014-01-11 04:22:10 +01:00
|
|
|
// Everything else is gcode. Send to g-code parser!
|
|
|
|
// TODO: Separate the parsing from the g-code execution. Need to re-write the parser
|
|
|
|
// completely to do this. First parse the line completely, checking for modal group
|
|
|
|
// errors and storing all of the g-code words. Then, send the stored g-code words to
|
|
|
|
// a separate g-code executor. This will be more in-line with actual g-code protocol.
|
|
|
|
status = gc_execute_line(line);
|
|
|
|
|
2011-02-18 22:59:16 +01:00
|
|
|
}
|
2014-01-11 04:22:10 +01:00
|
|
|
|
|
|
|
report_status_message(status);
|
2011-02-18 22:59:16 +01:00
|
|
|
}
|
|
|
|
|
2011-12-09 02:47:48 +01:00
|
|
|
|
2011-02-18 22:19:01 +01:00
|
|
|
void protocol_process()
|
2009-01-25 00:48:56 +01:00
|
|
|
{
|
2014-01-11 04:22:10 +01:00
|
|
|
// ------------------------------------------------------------
|
|
|
|
// Complete initialization procedures upon a power-up or reset.
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
|
|
|
// Print welcome message
|
|
|
|
report_init_message();
|
2011-12-09 02:47:48 +01:00
|
|
|
|
2014-01-11 04:22:10 +01:00
|
|
|
// Check for and report alarm state after a reset, error, or an initial power up.
|
|
|
|
if (sys.state == STATE_ALARM) {
|
|
|
|
report_feedback_message(MESSAGE_ALARM_LOCK);
|
|
|
|
} else {
|
|
|
|
// All systems go!
|
|
|
|
sys.state = STATE_IDLE; // Set system to ready. Clear all state flags.
|
|
|
|
system_execute_startup(line); // Execute startup script.
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------
|
|
|
|
// Main loop! Upon a system abort, this exits back to main() to reset the system.
|
|
|
|
// ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
uint8_t iscomment = false;
|
|
|
|
uint8_t char_counter = 0;
|
|
|
|
uint8_t c;
|
|
|
|
for (;;) {
|
|
|
|
|
|
|
|
// Process one line of incoming serial data, as the data becomes available. Performs an
|
|
|
|
// initial filtering by removing spaces and comments and capitalizing all letters.
|
|
|
|
while((c = serial_read()) != SERIAL_NO_DATA) {
|
|
|
|
if ((c == '\n') || (c == '\r')) { // End of line reached
|
|
|
|
line[char_counter] = 0; // Set string termination character.
|
|
|
|
protocol_execute_line(line); // Line is complete. Execute it!
|
|
|
|
iscomment = false;
|
|
|
|
char_counter = 0;
|
2011-08-16 03:39:44 +02:00
|
|
|
} else {
|
2014-01-11 04:22:10 +01:00
|
|
|
if (iscomment) {
|
|
|
|
// Throw away all comment characters
|
|
|
|
if (c == ')') {
|
|
|
|
// End of comment. Resume line.
|
|
|
|
iscomment = false;
|
|
|
|
}
|
2011-08-16 03:39:44 +02:00
|
|
|
} else {
|
2014-01-11 04:22:10 +01:00
|
|
|
if (c <= ' ') {
|
|
|
|
// Throw away whitepace and control characters
|
|
|
|
} else if (c == '/') {
|
|
|
|
// Block delete not supported. Ignore character.
|
|
|
|
} else if (c == '(') {
|
|
|
|
// Enable comments flag and ignore all characters until ')' or EOL.
|
|
|
|
iscomment = true;
|
|
|
|
} else if (char_counter >= LINE_BUFFER_SIZE-1) {
|
|
|
|
// Detect line buffer overflow. Report error and reset line buffer.
|
|
|
|
report_status_message(STATUS_OVERFLOW);
|
|
|
|
iscomment = false;
|
|
|
|
char_counter = 0;
|
|
|
|
} else if (c >= 'a' && c <= 'z') { // Upcase lowercase
|
|
|
|
line[char_counter++] = c-'a'+'A';
|
|
|
|
} else {
|
|
|
|
line[char_counter++] = c;
|
|
|
|
}
|
2011-08-16 03:39:44 +02:00
|
|
|
}
|
|
|
|
}
|
2009-01-25 00:48:56 +01:00
|
|
|
}
|
2014-01-11 04:22:10 +01:00
|
|
|
|
|
|
|
protocol_execute_runtime(); // Runtime command check point.
|
|
|
|
if (sys.abort) { return; } // Bail to main() program loop to reset system.
|
|
|
|
|
|
|
|
// If there are no more characters in the serial read buffer to be processed and executed,
|
|
|
|
// this indicates that g-code streaming has either filled the planner buffer or has
|
|
|
|
// completed. In either case, auto-cycle start, if enabled, any queued moves.
|
|
|
|
mc_auto_cycle_start();
|
|
|
|
|
2009-01-25 00:48:56 +01:00
|
|
|
}
|
2014-01-11 04:22:10 +01:00
|
|
|
|
|
|
|
return; /* Never reached */
|
2009-01-25 00:48:56 +01:00
|
|
|
}
|