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.
This commit is contained in:
Sonny Jeon
2012-11-03 11:32:23 -06:00
parent 303cf59f52
commit 4c711a4af7
25 changed files with 453 additions and 370 deletions

View File

@ -26,11 +26,8 @@
#include "print.h"
#include "settings.h"
#include "config.h"
#include <math.h>
#include "nuts_bolts.h"
#include <avr/pgmspace.h>
#include "stepper.h"
#include "planner.h"
#include "report.h"
#include "motion_control.h"
@ -43,8 +40,24 @@ void protocol_init()
{
char_counter = 0; // Reset line input
iscomment = false;
report_init_message(); // Welcome message
}
// Executes user startup script, if stored.
void protocol_execute_startup()
{
uint8_t n;
for (n=0; n < N_STARTUP_LINE; n++) {
if (!(settings_read_startup_line(n, line))) {
report_status_message(STATUS_SETTING_READ_FAIL);
} else {
if (line[0] != 0) {
printString(line); // Echo startup line to indicate execution.
report_status_message(gc_execute_line(line));
}
}
}
}
// 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
@ -65,7 +78,16 @@ void protocol_execute_runtime()
// System alarm. Something has gone wrong. Disable everything by entering an infinite
// loop until system reset/abort.
if (rt_exec & EXEC_ALARM) {
if (bit_isfalse(rt_exec,EXEC_RESET)) { // Ignore loop if reset is already issued
// Report the cause of the alarm here in the main program.
if (sys.state == STATE_LIMIT) { report_status_message(STATUS_HARD_LIMIT); }
if (sys.state == STATE_CYCLE) { // Pick up abort during active cycle.
report_status_message(STATUS_ABORT_CYCLE);
sys.state = STATE_LOST;
}
// Ignore loop if reset is already issued. In other words, a normal system abort/reset
// will not enter this loop, only a critical event not controlled by the user will.
if (bit_isfalse(rt_exec,EXEC_RESET)) {
sys.state = STATE_ALARM;
report_feedback_message(MESSAGE_SYSTEM_ALARM);
while (bit_isfalse(sys.execute,EXEC_RESET)) { sleep_mode(); }
}
@ -74,15 +96,7 @@ void protocol_execute_runtime()
// System abort. Steppers have already been force stopped.
if (rt_exec & EXEC_RESET) {
sys.abort = true;
// If the cycle is active before killing the motion, the event will likely caused a loss
// of position since there is no controlled deceleration(feed hold) to a stop.
// TODO: Add force home option upon position lost. Need to verify that cycle start isn't
// set false by anything but the stepper module. Also, need to look at a better place for
// this. Main.c?
// if (sys.cycle_start) { protocol_feedback_message(MESSAGE_POSITION_LOST); }
sys.abort = true;
return; // Nothing else to do but exit.
}
@ -98,7 +112,7 @@ void protocol_execute_runtime()
bit_false(sys.execute,EXEC_FEED_HOLD);
}
// Reinitializes the stepper module running flags and re-plans the buffer after a feed hold.
// Reinitializes the stepper module running state and, if a feed hold, re-plans the buffer.
// NOTE: EXEC_CYCLE_STOP is set by the stepper subsystem when a cycle or feed hold completes.
if (rt_exec & EXEC_CYCLE_STOP) {
st_cycle_reinitialize();
@ -133,6 +147,7 @@ uint8_t protocol_execute_line(char *line)
if(line[0] == '$') {
uint8_t char_counter = 1;
uint8_t helper_var = 0; // Helper variable
float parameter, value;
switch( line[char_counter] ) {
case 0 : report_grbl_help(); break;
@ -149,8 +164,11 @@ uint8_t protocol_execute_line(char *line)
else { report_gcode_modes(); }
break;
case 'H' : // Perform homing cycle
if (bit_istrue(settings.flags,BITFLAG_HOMING_ENABLE)) { mc_go_home(); }
else { return(STATUS_SETTING_DISABLED); }
if (bit_istrue(settings.flags,BITFLAG_HOMING_ENABLE)) {
// Only perform homing if Grbl is idle or lost.
if ( sys.state==STATE_IDLE || sys.state==STATE_LOST ) { mc_go_home(); }
else { return(STATUS_HOMING_ERROR); }
} else { return(STATUS_SETTING_DISABLED); }
break;
// case 'J' : break; // Jogging methods
// TODO: Here jogging can be placed for execution as a seperate subprogram. It does not need to be
@ -164,41 +182,62 @@ uint8_t protocol_execute_line(char *line)
// handled by the planner. It would be possible for the jog subprogram to insert blocks into the
// block buffer without having the planner plan them. It would need to manage de/ac-celerations
// on its own carefully. This approach could be effective and possibly size/memory efficient.
// case 'N' : // Start up blocks
// if(!read_float(line, &char_counter, &parameter)) { return(STATUS_BAD_NUMBER_FORMAT); }
// if(line[char_counter++] != '=') { return(STATUS_UNSUPPORTED_STATEMENT); }
// // Extract startup block, execute, and store.
// for (char_counter = 0; char_counter < LINE_BUFFER_SIZE-3; char_counter++) {
// line[char_counter] = line[char_counter+3];
// }
// uint8_t status = gc_execute_line(line);
// if (status) { return(status); }
// else { settings_store_startup_block(line); }
// break;
case 'B' : // Toggle block delete
if ( line[++char_counter] != 0 ) { return(STATUS_UNSUPPORTED_STATEMENT); }
sys.switches ^= BITFLAG_BLOCK_DELETE;
if (bit_istrue(sys.switches,BITFLAG_BLOCK_DELETE)) { report_feedback_message(MESSAGE_SWITCH_ON); }
else { report_feedback_message(MESSAGE_SWITCH_OFF); }
case 'S' : // Switch modes
// Set helper_var as switch bitmask or clearing flag
switch (line[++char_counter]) {
case 0 : helper_var = 0; break;
case '0' : helper_var = BITFLAG_CHECK_GCODE; break;
case '1' : helper_var = BITFLAG_DRY_RUN; break;
case '2' : helper_var = BITFLAG_BLOCK_DELETE; break;
case '3' : helper_var = BITFLAG_SINGLE_BLOCK; break;
case '4' : helper_var = BITFLAG_OPT_STOP; break;
default : return(STATUS_INVALID_STATEMENT);
}
if (helper_var) {
if ( line[++char_counter] != 0 ) { return(STATUS_UNSUPPORTED_STATEMENT); }
gc.switches ^= helper_var;
if (bit_istrue(gc.switches,helper_var)) { report_feedback_message(MESSAGE_SWITCH_ON); }
else { report_feedback_message(MESSAGE_SWITCH_OFF); }
} else {
gc.switches = helper_var; // Clear all switches
report_feedback_message(MESSAGE_SWITCHES_CLEARED);
}
break;
case 'S' : // Toggle single block mode
if ( line[++char_counter] != 0 ) { return(STATUS_UNSUPPORTED_STATEMENT); }
sys.switches ^= BITFLAG_SINGLE_BLOCK;
if (bit_istrue(sys.switches,BITFLAG_SINGLE_BLOCK)) { report_feedback_message(MESSAGE_SWITCH_ON); }
else { report_feedback_message(MESSAGE_SWITCH_OFF); }
break;
case 'O' : // Toggle optional stop
if ( line[++char_counter] != 0 ) { return(STATUS_UNSUPPORTED_STATEMENT); }
sys.switches ^= BITFLAG_OPT_STOP;
if (bit_istrue(sys.switches,BITFLAG_OPT_STOP)) { report_feedback_message(MESSAGE_SWITCH_ON); }
else { report_feedback_message(MESSAGE_SWITCH_OFF); }
break;
default : // Store global setting
case 'N' : // Startup lines.
if ( line[++char_counter] == 0 ) { // Print startup lines
for (helper_var=0; helper_var < N_STARTUP_LINE; helper_var++) {
if (!(settings_read_startup_line(helper_var, line))) {
report_status_message(STATUS_SETTING_READ_FAIL);
} else {
report_startup_line(helper_var,line);
}
}
break;
} else { // Store startup line
helper_var = true; // Set helper_var to flag storing method.
// No break. Continues into default: to read remaining command characters.
}
default : // Storing setting methods
if(!read_float(line, &char_counter, &parameter)) { return(STATUS_BAD_NUMBER_FORMAT); }
if(line[char_counter++] != '=') { return(STATUS_UNSUPPORTED_STATEMENT); }
if(!read_float(line, &char_counter, &value)) { return(STATUS_BAD_NUMBER_FORMAT); }
if(line[char_counter] != 0) { return(STATUS_UNSUPPORTED_STATEMENT); }
return(settings_store_global_setting(parameter, value));
if (helper_var) { // Store startup line
// Prepare sending gcode block to gcode parser by shifting all characters
helper_var = char_counter; // Set helper variable as counter to start of gcode block
do {
line[char_counter-helper_var] = line[char_counter];
} while (line[char_counter++] != 0);
// Execute gcode block to ensure block is valid.
helper_var = gc_execute_line(line); // Set helper_var to returned status code.
if (helper_var) { return(helper_var); }
else {
helper_var = trunc(parameter); // Set helper_var to int value of parameter
settings_store_startup_line(helper_var,line);
}
} else { // Store global setting.
if(!read_float(line, &char_counter, &value)) { return(STATUS_BAD_NUMBER_FORMAT); }
if(line[char_counter] != 0) { return(STATUS_UNSUPPORTED_STATEMENT); }
return(settings_store_global_setting(parameter, value));
}
}
return(STATUS_OK); // If '$' command makes it to here, then everything's ok.
@ -249,7 +288,7 @@ void protocol_process()
// Throw away whitepace and control characters
} else if (c == '/') {
// Disable block delete and throw away characters. Will ignore until EOL.
if (bit_istrue(sys.switches,BITFLAG_BLOCK_DELETE)) {
if (bit_istrue(gc.switches,BITFLAG_BLOCK_DELETE)) {
iscomment = true;
}
} else if (c == '(') {