2009-01-25 00:48:56 +01:00
|
|
|
/*
|
|
|
|
gcode.c - rs274/ngc parser.
|
|
|
|
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
|
2011-09-07 03:39:14 +02:00
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* This code is inspired by the Arduino GCode Interpreter by Mike Ellery and the NIST RS274/NGC Interpreter
|
|
|
|
by Kramer, Proctor and Messina. */
|
|
|
|
|
2014-01-11 04:22:10 +01:00
|
|
|
#include "system.h"
|
2011-02-05 00:45:41 +01:00
|
|
|
#include "settings.h"
|
2014-02-09 18:46:34 +01:00
|
|
|
#include "protocol.h"
|
2014-01-11 04:22:10 +01:00
|
|
|
#include "gcode.h"
|
2009-01-25 00:48:56 +01:00
|
|
|
#include "motion_control.h"
|
|
|
|
#include "spindle_control.h"
|
2012-09-21 19:14:13 +02:00
|
|
|
#include "coolant_control.h"
|
2012-11-01 16:37:27 +01:00
|
|
|
#include "report.h"
|
2009-02-03 09:56:45 +01:00
|
|
|
|
2012-11-02 02:48:55 +01:00
|
|
|
// Declare gc extern struct
|
|
|
|
parser_state_t gc;
|
2009-01-25 00:48:56 +01:00
|
|
|
|
2009-02-03 09:56:45 +01:00
|
|
|
#define FAIL(status) gc.status_code = status;
|
2009-01-25 00:48:56 +01:00
|
|
|
|
2013-10-30 02:10:39 +01:00
|
|
|
static uint8_t next_statement(char *letter, float *float_ptr, char *line, uint8_t *char_counter);
|
|
|
|
static void gc_convert_arc_radius_mode(float *target) __attribute__((noinline));
|
|
|
|
|
2009-01-25 00:48:56 +01:00
|
|
|
|
2011-02-19 23:03:10 +01:00
|
|
|
static void select_plane(uint8_t axis_0, uint8_t axis_1, uint8_t axis_2)
|
2009-02-11 00:37:33 +01:00
|
|
|
{
|
|
|
|
gc.plane_axis_0 = axis_0;
|
|
|
|
gc.plane_axis_1 = axis_1;
|
|
|
|
gc.plane_axis_2 = axis_2;
|
|
|
|
}
|
|
|
|
|
2013-10-30 02:10:39 +01:00
|
|
|
|
2012-01-29 04:41:08 +01:00
|
|
|
void gc_init()
|
|
|
|
{
|
2009-02-03 09:56:45 +01:00
|
|
|
memset(&gc, 0, sizeof(gc));
|
2012-12-17 00:23:24 +01:00
|
|
|
gc.feed_rate = settings.default_feed_rate;
|
2009-02-11 00:37:33 +01:00
|
|
|
select_plane(X_AXIS, Y_AXIS, Z_AXIS);
|
2011-02-18 23:04:12 +01:00
|
|
|
gc.absolute_mode = true;
|
2012-10-13 21:11:43 +02:00
|
|
|
|
2012-11-02 02:48:55 +01:00
|
|
|
// Load default G54 coordinate system.
|
|
|
|
if (!(settings_read_coord_data(gc.coord_select,gc.coord_system))) {
|
|
|
|
report_status_message(STATUS_SETTING_READ_FAIL);
|
|
|
|
}
|
2009-01-25 00:48:56 +01:00
|
|
|
}
|
|
|
|
|
2013-10-30 02:10:39 +01:00
|
|
|
|
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
|
|
|
// Sets g-code parser position in mm. Input in steps. Called by the system abort and hard
|
|
|
|
// limit pull-off routines.
|
2013-10-30 02:10:39 +01:00
|
|
|
void gc_sync_position(int32_t x, int32_t y, int32_t z)
|
2012-01-29 04:41:08 +01:00
|
|
|
{
|
2013-10-30 02:10:39 +01:00
|
|
|
uint8_t i;
|
|
|
|
for (i=0; i<N_AXIS; i++) {
|
|
|
|
gc.position[i] = sys.position[i]/settings.steps_per_mm[i];
|
|
|
|
}
|
2012-01-29 04:41:08 +01:00
|
|
|
}
|
|
|
|
|
2013-10-30 02:10:39 +01:00
|
|
|
|
2012-10-08 23:57:58 +02:00
|
|
|
static float to_millimeters(float value)
|
2012-02-11 19:59:35 +01:00
|
|
|
{
|
2010-07-03 00:23:28 +02:00
|
|
|
return(gc.inches_mode ? (value * MM_PER_INCH) : value);
|
2009-01-25 00:48:56 +01:00
|
|
|
}
|
|
|
|
|
2013-10-30 02:10:39 +01:00
|
|
|
|
2009-01-25 00:48:56 +01:00
|
|
|
// Executes one line of 0-terminated G-Code. The line is assumed to contain only uppercase
|
2011-08-16 03:39:44 +02:00
|
|
|
// characters and signed floating point values (no whitespace). Comments and block delete
|
2013-10-30 02:10:39 +01:00
|
|
|
// characters have been removed. In this function, all units and positions are converted and
|
|
|
|
// exported to grbl's internal functions in terms of (mm, mm/min) and absolute machine
|
|
|
|
// coordinates, respectively.
|
2012-01-29 04:41:08 +01:00
|
|
|
uint8_t gc_execute_line(char *line)
|
|
|
|
{
|
2011-02-18 22:59:16 +01:00
|
|
|
uint8_t char_counter = 0;
|
2009-01-25 00:48:56 +01:00
|
|
|
char letter;
|
2012-10-08 23:57:58 +02:00
|
|
|
float value;
|
2012-02-11 19:59:35 +01:00
|
|
|
int int_value;
|
2009-01-25 00:48:56 +01:00
|
|
|
|
2012-02-11 19:59:35 +01:00
|
|
|
uint16_t modal_group_words = 0; // Bitflag variable to track and check modal group words in block
|
|
|
|
uint8_t axis_words = 0; // Bitflag to track which XYZ(ABC) parameters exist in block
|
|
|
|
|
2012-10-08 23:57:58 +02:00
|
|
|
float inverse_feed_rate = -1; // negative inverse_feed_rate means no inverse_feed_rate specified
|
2012-02-11 19:59:35 +01:00
|
|
|
uint8_t absolute_override = false; // true(1) = absolute motion for this block only {G53}
|
|
|
|
uint8_t non_modal_action = NON_MODAL_NONE; // Tracks the actions of modal group 0 (non-modal)
|
2009-01-25 00:48:56 +01:00
|
|
|
|
2013-10-30 02:10:39 +01:00
|
|
|
float target[N_AXIS];
|
2012-02-11 19:59:35 +01:00
|
|
|
clear_vector(target); // XYZ(ABC) axes parameters.
|
2013-10-30 02:10:39 +01:00
|
|
|
|
2014-02-26 20:10:07 +01:00
|
|
|
#ifdef USE_LINE_NUMBERS
|
2014-02-19 15:48:09 +01:00
|
|
|
int32_t line_number = 0;
|
2014-02-26 20:10:07 +01:00
|
|
|
#endif
|
2013-10-30 02:10:39 +01:00
|
|
|
gc.arc_radius = 0;
|
|
|
|
clear_vector(gc.arc_offset); // IJK Arc offsets are incremental. Value of zero indicates no change.
|
|
|
|
|
2011-02-18 22:59:16 +01:00
|
|
|
gc.status_code = STATUS_OK;
|
2009-01-25 00:48:56 +01:00
|
|
|
|
2012-02-11 19:59:35 +01:00
|
|
|
/* Pass 1: Commands and set all modes. Check for modal group violations.
|
|
|
|
NOTE: Modal group numbers are defined in Table 4 of NIST RS274-NGC v3, pg.20 */
|
|
|
|
uint8_t group_number = MODAL_GROUP_NONE;
|
2010-07-03 00:04:10 +02:00
|
|
|
while(next_statement(&letter, &value, line, &char_counter)) {
|
2009-01-25 00:48:56 +01:00
|
|
|
int_value = trunc(value);
|
|
|
|
switch(letter) {
|
|
|
|
case 'G':
|
2012-02-11 19:59:35 +01:00
|
|
|
// Set modal group values
|
|
|
|
switch(int_value) {
|
|
|
|
case 4: case 10: case 28: case 30: case 53: case 92: group_number = MODAL_GROUP_0; break;
|
|
|
|
case 0: case 1: case 2: case 3: case 80: group_number = MODAL_GROUP_1; break;
|
|
|
|
case 17: case 18: case 19: group_number = MODAL_GROUP_2; break;
|
|
|
|
case 90: case 91: group_number = MODAL_GROUP_3; break;
|
|
|
|
case 93: case 94: group_number = MODAL_GROUP_5; break;
|
|
|
|
case 20: case 21: group_number = MODAL_GROUP_6; break;
|
|
|
|
case 54: case 55: case 56: case 57: case 58: case 59: group_number = MODAL_GROUP_12; break;
|
|
|
|
}
|
|
|
|
// Set 'G' commands
|
2012-01-29 04:41:08 +01:00
|
|
|
switch(int_value) {
|
|
|
|
case 0: gc.motion_mode = MOTION_MODE_SEEK; break;
|
|
|
|
case 1: gc.motion_mode = MOTION_MODE_LINEAR; break;
|
|
|
|
case 2: gc.motion_mode = MOTION_MODE_CW_ARC; break;
|
|
|
|
case 3: gc.motion_mode = MOTION_MODE_CCW_ARC; break;
|
2012-02-11 19:59:35 +01:00
|
|
|
case 4: non_modal_action = NON_MODAL_DWELL; break;
|
|
|
|
case 10: non_modal_action = NON_MODAL_SET_COORDINATE_DATA; break;
|
2012-01-29 04:41:08 +01:00
|
|
|
case 17: select_plane(X_AXIS, Y_AXIS, Z_AXIS); break;
|
2013-12-07 16:40:25 +01:00
|
|
|
case 18: select_plane(Z_AXIS, X_AXIS, Y_AXIS); break;
|
2012-01-29 04:41:08 +01:00
|
|
|
case 19: select_plane(Y_AXIS, Z_AXIS, X_AXIS); break;
|
|
|
|
case 20: gc.inches_mode = true; break;
|
|
|
|
case 21: gc.inches_mode = false; break;
|
2012-10-10 06:01:10 +02:00
|
|
|
case 28: case 30:
|
2012-11-02 02:48:55 +01:00
|
|
|
int_value = trunc(10*value); // Multiply by 10 to pick up Gxx.1
|
2012-11-01 16:37:27 +01:00
|
|
|
switch(int_value) {
|
|
|
|
case 280: non_modal_action = NON_MODAL_GO_HOME_0; break;
|
|
|
|
case 281: non_modal_action = NON_MODAL_SET_HOME_0; break;
|
|
|
|
case 300: non_modal_action = NON_MODAL_GO_HOME_1; break;
|
|
|
|
case 301: non_modal_action = NON_MODAL_SET_HOME_1; break;
|
|
|
|
default: FAIL(STATUS_UNSUPPORTED_STATEMENT);
|
2012-10-10 06:01:10 +02:00
|
|
|
}
|
|
|
|
break;
|
2014-02-25 21:19:52 +01:00
|
|
|
case 38:
|
|
|
|
int_value = trunc(10*value); // Multiply by 10 to pick up Gxx.1
|
|
|
|
switch(int_value) {
|
|
|
|
case 382: non_modal_action = NON_MODAL_PROBE_WITH_ERROR; break;
|
|
|
|
case 383: non_modal_action = NON_MODAL_PROBE_NO_ERROR; break;
|
|
|
|
default: FAIL(STATUS_UNSUPPORTED_STATEMENT);
|
|
|
|
}
|
|
|
|
break;
|
2012-01-29 04:41:08 +01:00
|
|
|
case 53: absolute_override = true; break;
|
2012-02-11 19:59:35 +01:00
|
|
|
case 54: case 55: case 56: case 57: case 58: case 59:
|
2012-11-02 02:48:55 +01:00
|
|
|
gc.coord_select = int_value-54;
|
2012-02-11 19:59:35 +01:00
|
|
|
break;
|
|
|
|
case 80: gc.motion_mode = MOTION_MODE_CANCEL; break;
|
2012-01-29 04:41:08 +01:00
|
|
|
case 90: gc.absolute_mode = true; break;
|
|
|
|
case 91: gc.absolute_mode = false; break;
|
2012-02-11 19:59:35 +01:00
|
|
|
case 92:
|
|
|
|
int_value = trunc(10*value); // Multiply by 10 to pick up G92.1
|
|
|
|
switch(int_value) {
|
|
|
|
case 920: non_modal_action = NON_MODAL_SET_COORDINATE_OFFSET; break;
|
|
|
|
case 921: non_modal_action = NON_MODAL_RESET_COORDINATE_OFFSET; break;
|
|
|
|
default: FAIL(STATUS_UNSUPPORTED_STATEMENT);
|
|
|
|
}
|
|
|
|
break;
|
2012-01-29 04:41:08 +01:00
|
|
|
case 93: gc.inverse_feed_rate_mode = true; break;
|
|
|
|
case 94: gc.inverse_feed_rate_mode = false; break;
|
|
|
|
default: FAIL(STATUS_UNSUPPORTED_STATEMENT);
|
|
|
|
}
|
2012-02-11 19:59:35 +01:00
|
|
|
break;
|
2009-01-25 00:48:56 +01:00
|
|
|
case 'M':
|
2012-02-11 19:59:35 +01:00
|
|
|
// Set modal group values
|
2012-01-29 04:41:08 +01:00
|
|
|
switch(int_value) {
|
2012-02-11 19:59:35 +01:00
|
|
|
case 0: case 1: case 2: case 30: group_number = MODAL_GROUP_4; break;
|
|
|
|
case 3: case 4: case 5: group_number = MODAL_GROUP_7; break;
|
2014-01-02 20:12:35 +01:00
|
|
|
case 7: case 8: case 9: group_number = MODAL_GROUP_8; break;
|
2012-02-11 19:59:35 +01:00
|
|
|
}
|
|
|
|
// Set 'M' commands
|
|
|
|
switch(int_value) {
|
|
|
|
case 0: gc.program_flow = PROGRAM_FLOW_PAUSED; break; // Program pause
|
2012-11-16 05:53:11 +01:00
|
|
|
case 1: break; // Optional stop not supported. Ignore.
|
2012-01-29 04:41:08 +01:00
|
|
|
case 2: case 30: gc.program_flow = PROGRAM_FLOW_COMPLETED; break; // Program end and reset
|
2014-01-11 04:22:10 +01:00
|
|
|
case 3: gc.spindle_direction = SPINDLE_ENABLE_CW; break;
|
|
|
|
case 4: gc.spindle_direction = SPINDLE_ENABLE_CCW; break;
|
|
|
|
case 5: gc.spindle_direction = SPINDLE_DISABLE; break;
|
2012-09-22 01:55:02 +02:00
|
|
|
#ifdef ENABLE_M7
|
2012-09-21 19:14:13 +02:00
|
|
|
case 7: gc.coolant_mode = COOLANT_MIST_ENABLE; break;
|
|
|
|
#endif
|
|
|
|
case 8: gc.coolant_mode = COOLANT_FLOOD_ENABLE; break;
|
|
|
|
case 9: gc.coolant_mode = COOLANT_DISABLE; break;
|
2012-01-29 04:41:08 +01:00
|
|
|
default: FAIL(STATUS_UNSUPPORTED_STATEMENT);
|
|
|
|
}
|
|
|
|
break;
|
2012-02-11 19:59:35 +01:00
|
|
|
}
|
|
|
|
// Check for modal group multiple command violations in the current block
|
|
|
|
if (group_number) {
|
|
|
|
if ( bit_istrue(modal_group_words,bit(group_number)) ) {
|
|
|
|
FAIL(STATUS_MODAL_GROUP_VIOLATION);
|
|
|
|
} else {
|
|
|
|
bit_true(modal_group_words,bit(group_number));
|
|
|
|
}
|
|
|
|
group_number = MODAL_GROUP_NONE; // Reset for next command.
|
2009-01-25 00:48:56 +01:00
|
|
|
}
|
2012-02-11 19:59:35 +01:00
|
|
|
}
|
|
|
|
|
2009-01-25 00:48:56 +01:00
|
|
|
// If there were any errors parsing this line, we will return right away with the bad news
|
2009-02-03 09:56:45 +01:00
|
|
|
if (gc.status_code) { return(gc.status_code); }
|
2012-02-11 19:59:35 +01:00
|
|
|
|
|
|
|
/* Pass 2: Parameters. All units converted according to current block commands. Position
|
|
|
|
parameters are converted and flagged to indicate a change. These can have multiple connotations
|
2013-10-30 02:10:39 +01:00
|
|
|
for different commands. Each will be converted to their proper value upon execution.
|
|
|
|
NOTE: Grbl unconventionally pre-converts these parameter values based on the block G and M
|
|
|
|
commands. This is set out of the order of execution defined by NIST only for code efficiency/size
|
|
|
|
purposes, but should not affect proper g-code execution. */
|
|
|
|
float p = 0;
|
2012-02-11 19:59:35 +01:00
|
|
|
uint8_t l = 0;
|
2010-07-03 00:04:10 +02:00
|
|
|
char_counter = 0;
|
|
|
|
while(next_statement(&letter, &value, line, &char_counter)) {
|
2009-01-25 00:48:56 +01:00
|
|
|
switch(letter) {
|
2014-02-07 00:10:27 +01:00
|
|
|
case 'G': case 'M': break; // Ignore command statements and line numbers
|
2009-01-25 00:48:56 +01:00
|
|
|
case 'F':
|
2012-10-22 00:55:59 +02:00
|
|
|
if (value <= 0) { FAIL(STATUS_INVALID_STATEMENT); } // Must be greater than zero
|
2012-01-29 04:41:08 +01:00
|
|
|
if (gc.inverse_feed_rate_mode) {
|
2012-02-11 19:59:35 +01:00
|
|
|
inverse_feed_rate = to_millimeters(value); // seconds per motion for this motion only
|
2012-01-29 04:41:08 +01:00
|
|
|
} else {
|
2012-02-11 19:59:35 +01:00
|
|
|
gc.feed_rate = to_millimeters(value); // millimeters per minute
|
2011-02-04 23:48:10 +01:00
|
|
|
}
|
2012-01-29 04:41:08 +01:00
|
|
|
break;
|
2013-10-30 02:10:39 +01:00
|
|
|
case 'I': case 'J': case 'K': gc.arc_offset[letter-'I'] = to_millimeters(value); break;
|
2012-02-11 19:59:35 +01:00
|
|
|
case 'L': l = trunc(value); break;
|
2014-02-26 20:10:07 +01:00
|
|
|
case 'N':
|
|
|
|
#ifdef USE_LINE_NUMBERS
|
|
|
|
line_number = trunc(value);
|
|
|
|
#endif
|
|
|
|
break;
|
2012-02-11 19:59:35 +01:00
|
|
|
case 'P': p = value; break;
|
2013-10-30 02:10:39 +01:00
|
|
|
case 'R': gc.arc_radius = to_millimeters(value); break;
|
2012-02-11 19:59:35 +01:00
|
|
|
case 'S':
|
2012-10-22 00:55:59 +02:00
|
|
|
if (value < 0) { FAIL(STATUS_INVALID_STATEMENT); } // Cannot be negative
|
2014-01-11 04:22:10 +01:00
|
|
|
gc.spindle_speed = value;
|
2012-01-29 04:41:08 +01:00
|
|
|
break;
|
2012-02-11 19:59:35 +01:00
|
|
|
case 'T':
|
2012-10-22 00:55:59 +02:00
|
|
|
if (value < 0) { FAIL(STATUS_INVALID_STATEMENT); } // Cannot be negative
|
2012-02-11 19:59:35 +01:00
|
|
|
gc.tool = trunc(value);
|
|
|
|
break;
|
|
|
|
case 'X': target[X_AXIS] = to_millimeters(value); bit_true(axis_words,bit(X_AXIS)); break;
|
|
|
|
case 'Y': target[Y_AXIS] = to_millimeters(value); bit_true(axis_words,bit(Y_AXIS)); break;
|
|
|
|
case 'Z': target[Z_AXIS] = to_millimeters(value); bit_true(axis_words,bit(Z_AXIS)); break;
|
2012-10-08 23:57:58 +02:00
|
|
|
default: FAIL(STATUS_UNSUPPORTED_STATEMENT);
|
2009-01-25 00:48:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-03 09:56:45 +01:00
|
|
|
// If there were any errors parsing this line, we will return right away with the bad news
|
|
|
|
if (gc.status_code) { return(gc.status_code); }
|
2012-02-11 19:59:35 +01:00
|
|
|
|
2013-10-30 02:10:39 +01:00
|
|
|
// Initialize axis index
|
|
|
|
uint8_t idx;
|
|
|
|
|
|
|
|
/* Execute Commands: Perform by order of execution defined in NIST RS274-NGC.v3, Table 8, pg.41. */
|
|
|
|
|
2012-12-17 00:23:24 +01:00
|
|
|
// ([F]: Set feed rate.)
|
2012-11-19 03:52:16 +01:00
|
|
|
|
|
|
|
if (sys.state != STATE_CHECK_MODE) {
|
|
|
|
// ([M6]: Tool change should be executed here.)
|
|
|
|
|
|
|
|
// [M3,M4,M5]: Update spindle state
|
2014-02-09 18:46:34 +01:00
|
|
|
if (bit_istrue(modal_group_words,bit(MODAL_GROUP_7))) {
|
|
|
|
spindle_run(gc.spindle_direction, gc.spindle_speed);
|
|
|
|
}
|
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
|
|
|
|
2012-11-19 03:52:16 +01:00
|
|
|
// [*M7,M8,M9]: Update coolant state
|
2014-02-09 18:46:34 +01:00
|
|
|
if (bit_istrue(modal_group_words,bit(MODAL_GROUP_8))) {
|
|
|
|
coolant_run(gc.coolant_mode);
|
|
|
|
}
|
2012-11-19 03:52:16 +01:00
|
|
|
}
|
2012-02-11 19:59:35 +01:00
|
|
|
|
2012-11-01 16:37:27 +01:00
|
|
|
// [G54,G55,...,G59]: Coordinate system selection
|
|
|
|
if ( bit_istrue(modal_group_words,bit(MODAL_GROUP_12)) ) { // Check if called in block
|
|
|
|
float coord_data[N_AXIS];
|
2012-11-02 02:48:55 +01:00
|
|
|
if (!(settings_read_coord_data(gc.coord_select,coord_data))) { return(STATUS_SETTING_READ_FAIL); }
|
|
|
|
memcpy(gc.coord_system,coord_data,sizeof(coord_data));
|
2012-11-01 16:37:27 +01:00
|
|
|
}
|
|
|
|
|
2012-02-11 19:59:35 +01:00
|
|
|
// [G4,G10,G28,G30,G92,G92.1]: Perform dwell, set coordinate system data, homing, or set axis offsets.
|
|
|
|
// NOTE: These commands are in the same modal group, hence are mutually exclusive. G53 is in this
|
|
|
|
// modal group and do not effect these actions.
|
|
|
|
switch (non_modal_action) {
|
|
|
|
case NON_MODAL_DWELL:
|
|
|
|
if (p < 0) { // Time cannot be negative.
|
2012-10-22 00:55:59 +02:00
|
|
|
FAIL(STATUS_INVALID_STATEMENT);
|
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
|
|
|
} else {
|
2012-11-08 04:53:03 +01:00
|
|
|
// Ignore dwell in check gcode modes
|
2012-11-16 05:53:11 +01:00
|
|
|
if (sys.state != STATE_CHECK_MODE) { mc_dwell(p); }
|
2012-02-11 19:59:35 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NON_MODAL_SET_COORDINATE_DATA:
|
|
|
|
int_value = trunc(p); // Convert p value to int.
|
2013-10-30 02:10:39 +01:00
|
|
|
if ((l != 2 && l != 20) || (int_value < 0 || int_value > N_COORDINATE_SYSTEM)) { // L2 and L20. P1=G54, P2=G55, ...
|
2012-02-11 19:59:35 +01:00
|
|
|
FAIL(STATUS_UNSUPPORTED_STATEMENT);
|
2012-11-01 16:37:27 +01:00
|
|
|
} else if (!axis_words && l==2) { // No axis words.
|
2012-10-22 00:55:59 +02:00
|
|
|
FAIL(STATUS_INVALID_STATEMENT);
|
2012-02-11 19:59:35 +01:00
|
|
|
} else {
|
2013-10-30 02:10:39 +01:00
|
|
|
if (int_value > 0) { int_value--; } // Adjust P1-P6 index to EEPROM coordinate data indexing.
|
|
|
|
else { int_value = gc.coord_select; } // Index P0 as the active coordinate system
|
|
|
|
float coord_data[N_AXIS];
|
|
|
|
if (!settings_read_coord_data(int_value,coord_data)) { return(STATUS_SETTING_READ_FAIL); }
|
|
|
|
// Update axes defined only in block. Always in machine coordinates. Can change non-active system.
|
|
|
|
for (idx=0; idx<N_AXIS; idx++) { // Axes indices are consistent, so loop may be used.
|
|
|
|
if (bit_istrue(axis_words,bit(idx)) ) {
|
|
|
|
if (l == 20) {
|
|
|
|
coord_data[idx] = gc.position[idx]-target[idx]; // L20: Update axis current position to target
|
|
|
|
} else {
|
|
|
|
coord_data[idx] = target[idx]; // L2: Update coordinate system axis
|
|
|
|
}
|
2012-11-01 16:37:27 +01:00
|
|
|
}
|
2012-02-11 19:59:35 +01:00
|
|
|
}
|
2013-10-30 02:10:39 +01:00
|
|
|
settings_write_coord_data(int_value,coord_data);
|
|
|
|
// Update system coordinate system if currently active.
|
|
|
|
if (gc.coord_select == int_value) { memcpy(gc.coord_system,coord_data,sizeof(coord_data)); }
|
2012-02-11 19:59:35 +01:00
|
|
|
}
|
|
|
|
axis_words = 0; // Axis words used. Lock out from motion modes by clearing flags.
|
2009-01-25 00:48:56 +01:00
|
|
|
break;
|
2012-11-01 16:37:27 +01:00
|
|
|
case NON_MODAL_GO_HOME_0: case NON_MODAL_GO_HOME_1:
|
2012-02-11 19:59:35 +01:00
|
|
|
// Move to intermediate position before going home. Obeys current coordinate system and offsets
|
|
|
|
// and absolute and incremental modes.
|
|
|
|
if (axis_words) {
|
|
|
|
// Apply absolute mode coordinate offsets or incremental mode offsets.
|
2013-10-30 02:10:39 +01:00
|
|
|
for (idx=0; idx<N_AXIS; idx++) { // Axes indices are consistent, so loop may be used.
|
|
|
|
if ( bit_istrue(axis_words,bit(idx)) ) {
|
2012-02-11 19:59:35 +01:00
|
|
|
if (gc.absolute_mode) {
|
2013-10-30 02:10:39 +01:00
|
|
|
target[idx] += gc.coord_system[idx] + gc.coord_offset[idx];
|
2012-02-11 19:59:35 +01:00
|
|
|
} else {
|
2013-10-30 02:10:39 +01:00
|
|
|
target[idx] += gc.position[idx];
|
2012-02-11 19:59:35 +01:00
|
|
|
}
|
|
|
|
} else {
|
2013-10-30 02:10:39 +01:00
|
|
|
target[idx] = gc.position[idx];
|
2012-02-11 19:59:35 +01:00
|
|
|
}
|
|
|
|
}
|
2014-02-26 20:10:07 +01:00
|
|
|
#ifdef USE_LINE_NUMBERS
|
2014-02-07 00:10:27 +01:00
|
|
|
mc_line(target, -1.0, false, line_number);
|
2014-02-26 20:10:07 +01:00
|
|
|
#else
|
|
|
|
mc_line(target, -1.0, false);
|
|
|
|
#endif
|
2012-02-11 19:59:35 +01:00
|
|
|
}
|
2012-11-01 16:37:27 +01:00
|
|
|
// Retreive G28/30 go-home position data (in machine coordinates) from EEPROM
|
|
|
|
float coord_data[N_AXIS];
|
2013-10-30 02:10:39 +01:00
|
|
|
if (non_modal_action == NON_MODAL_GO_HOME_0) {
|
|
|
|
if (!settings_read_coord_data(SETTING_INDEX_G28,coord_data)) { return(STATUS_SETTING_READ_FAIL); }
|
|
|
|
} else {
|
|
|
|
if (!settings_read_coord_data(SETTING_INDEX_G30,coord_data)) { return(STATUS_SETTING_READ_FAIL); }
|
|
|
|
}
|
2014-02-26 20:10:07 +01:00
|
|
|
#ifdef USE_LINE_NUMBERS
|
2014-02-07 00:10:27 +01:00
|
|
|
mc_line(coord_data, -1.0, false, line_number);
|
2014-02-26 20:10:07 +01:00
|
|
|
#else
|
|
|
|
mc_line(coord_data, -1.0, false);
|
|
|
|
#endif
|
2012-11-26 06:02:33 +01:00
|
|
|
memcpy(gc.position, coord_data, sizeof(coord_data)); // gc.position[] = coord_data[];
|
2012-02-11 19:59:35 +01:00
|
|
|
axis_words = 0; // Axis words used. Lock out from motion modes by clearing flags.
|
2012-11-01 16:37:27 +01:00
|
|
|
break;
|
2014-02-25 21:19:52 +01:00
|
|
|
case NON_MODAL_PROBE_WITH_ERROR:
|
|
|
|
if (!axis_words) { // No axis words
|
|
|
|
FAIL(STATUS_INVALID_STATEMENT);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(mc_probe_cycle(target, (gc.inverse_feed_rate_mode) ? inverse_feed_rate : gc.feed_rate, gc.inverse_feed_rate_mode, line_number)){
|
|
|
|
FAIL(STATUS_PROBE_ERROR);
|
|
|
|
}
|
|
|
|
axis_words = 0;
|
|
|
|
break;
|
|
|
|
case NON_MODAL_PROBE_NO_ERROR:
|
|
|
|
if (!axis_words) { // No axis words
|
|
|
|
FAIL(STATUS_INVALID_STATEMENT);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mc_probe_cycle(target, (gc.inverse_feed_rate_mode) ? inverse_feed_rate : gc.feed_rate, gc.inverse_feed_rate_mode, line_number);
|
|
|
|
axis_words = 0;
|
|
|
|
break;
|
2012-11-01 16:37:27 +01:00
|
|
|
case NON_MODAL_SET_HOME_0: case NON_MODAL_SET_HOME_1:
|
2013-10-30 02:10:39 +01:00
|
|
|
if (non_modal_action == NON_MODAL_SET_HOME_0) {
|
|
|
|
settings_write_coord_data(SETTING_INDEX_G28,gc.position);
|
|
|
|
} else {
|
|
|
|
settings_write_coord_data(SETTING_INDEX_G30,gc.position);
|
|
|
|
}
|
2012-11-01 16:37:27 +01:00
|
|
|
break;
|
2012-02-11 19:59:35 +01:00
|
|
|
case NON_MODAL_SET_COORDINATE_OFFSET:
|
|
|
|
if (!axis_words) { // No axis words
|
2012-10-22 00:55:59 +02:00
|
|
|
FAIL(STATUS_INVALID_STATEMENT);
|
2012-02-11 19:59:35 +01:00
|
|
|
} else {
|
|
|
|
// Update axes defined only in block. Offsets current system to defined value. Does not update when
|
|
|
|
// active coordinate system is selected, but is still active unless G92.1 disables it.
|
2013-10-30 02:10:39 +01:00
|
|
|
for (idx=0; idx<N_AXIS; idx++) { // Axes indices are consistent, so loop may be used.
|
|
|
|
if (bit_istrue(axis_words,bit(idx)) ) {
|
|
|
|
gc.coord_offset[idx] = gc.position[idx]-gc.coord_system[idx]-target[idx];
|
2012-02-11 19:59:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
axis_words = 0; // Axis words used. Lock out from motion modes by clearing flags.
|
|
|
|
break;
|
|
|
|
case NON_MODAL_RESET_COORDINATE_OFFSET:
|
2012-11-02 02:48:55 +01:00
|
|
|
clear_vector(gc.coord_offset); // Disable G92 offsets by zeroing offset vector.
|
2012-02-11 19:59:35 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// [G0,G1,G2,G3,G80]: Perform motion modes.
|
|
|
|
// NOTE: Commands G10,G28,G30,G92 lock out and prevent axis words from use in motion modes.
|
|
|
|
// Enter motion modes only if there are axis words or a motion mode command word in the block.
|
|
|
|
if ( bit_istrue(modal_group_words,bit(MODAL_GROUP_1)) || axis_words ) {
|
|
|
|
|
|
|
|
// G1,G2,G3 require F word in inverse time mode.
|
|
|
|
if ( gc.inverse_feed_rate_mode ) {
|
|
|
|
if (inverse_feed_rate < 0 && gc.motion_mode != MOTION_MODE_CANCEL) {
|
2012-10-22 00:55:59 +02:00
|
|
|
FAIL(STATUS_INVALID_STATEMENT);
|
2012-02-11 19:59:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Absolute override G53 only valid with G0 and G1 active.
|
|
|
|
if ( absolute_override && !(gc.motion_mode == MOTION_MODE_SEEK || gc.motion_mode == MOTION_MODE_LINEAR)) {
|
2012-10-22 00:55:59 +02:00
|
|
|
FAIL(STATUS_INVALID_STATEMENT);
|
2012-02-11 19:59:35 +01:00
|
|
|
}
|
|
|
|
// Report any errors.
|
|
|
|
if (gc.status_code) { return(gc.status_code); }
|
|
|
|
|
|
|
|
// Convert all target position data to machine coordinates for executing motion. Apply
|
|
|
|
// absolute mode coordinate offsets or incremental mode offsets.
|
|
|
|
// NOTE: Tool offsets may be appended to these conversions when/if this feature is added.
|
2013-10-30 02:10:39 +01:00
|
|
|
for (idx=0; idx<N_AXIS; idx++) { // Axes indices are consistent, so loop may be used to save flash space.
|
|
|
|
if ( bit_istrue(axis_words,bit(idx)) ) {
|
2012-02-11 19:59:35 +01:00
|
|
|
if (!absolute_override) { // Do not update target in absolute override mode
|
|
|
|
if (gc.absolute_mode) {
|
2013-10-30 02:10:39 +01:00
|
|
|
target[idx] += gc.coord_system[idx] + gc.coord_offset[idx]; // Absolute mode
|
2012-02-11 19:59:35 +01:00
|
|
|
} else {
|
2013-10-30 02:10:39 +01:00
|
|
|
target[idx] += gc.position[idx]; // Incremental mode
|
2012-02-11 19:59:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2013-10-30 02:10:39 +01:00
|
|
|
target[idx] = gc.position[idx]; // No axis word in block. Keep same axis position.
|
2012-02-11 19:59:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (gc.motion_mode) {
|
|
|
|
case MOTION_MODE_CANCEL:
|
2012-10-22 00:55:59 +02:00
|
|
|
if (axis_words) { FAIL(STATUS_INVALID_STATEMENT); } // No axis words allowed while active.
|
2012-02-11 19:59:35 +01:00
|
|
|
break;
|
|
|
|
case MOTION_MODE_SEEK:
|
2012-10-22 00:55:59 +02:00
|
|
|
if (!axis_words) { FAIL(STATUS_INVALID_STATEMENT);}
|
2014-02-26 20:10:07 +01:00
|
|
|
else {
|
|
|
|
#ifdef USE_LINE_NUMBERS
|
|
|
|
mc_line(target, -1.0, false, line_number);
|
|
|
|
#else
|
|
|
|
mc_line(target, -1.0, false);
|
|
|
|
#endif
|
|
|
|
}
|
2012-02-11 19:59:35 +01:00
|
|
|
break;
|
|
|
|
case MOTION_MODE_LINEAR:
|
2012-11-01 16:37:27 +01:00
|
|
|
// TODO: Inverse time requires F-word with each statement. Need to do a check. Also need
|
|
|
|
// to check for initial F-word upon startup. Maybe just set to zero upon initialization
|
|
|
|
// and after an inverse time move and then check for non-zero feed rate each time. This
|
|
|
|
// should be efficient and effective.
|
2012-10-22 00:55:59 +02:00
|
|
|
if (!axis_words) { FAIL(STATUS_INVALID_STATEMENT);}
|
2014-02-26 20:10:07 +01:00
|
|
|
else {
|
|
|
|
#ifdef USE_LINE_NUMBERS
|
|
|
|
mc_line(target, (gc.inverse_feed_rate_mode) ? inverse_feed_rate : gc.feed_rate, gc.inverse_feed_rate_mode, line_number);
|
|
|
|
#else
|
|
|
|
mc_line(target, (gc.inverse_feed_rate_mode) ? inverse_feed_rate : gc.feed_rate, gc.inverse_feed_rate_mode);
|
|
|
|
#endif
|
|
|
|
}
|
2012-02-11 19:59:35 +01:00
|
|
|
break;
|
|
|
|
case MOTION_MODE_CW_ARC: case MOTION_MODE_CCW_ARC:
|
|
|
|
// Check if at least one of the axes of the selected plane has been specified. If in center
|
|
|
|
// format arc mode, also check for at least one of the IJK axes of the selected plane was sent.
|
|
|
|
if ( !( bit_false(axis_words,bit(gc.plane_axis_2)) ) ||
|
2013-10-30 02:10:39 +01:00
|
|
|
( !gc.arc_radius && !gc.arc_offset[gc.plane_axis_0] && !gc.arc_offset[gc.plane_axis_1] ) ) {
|
2012-10-22 00:55:59 +02:00
|
|
|
FAIL(STATUS_INVALID_STATEMENT);
|
2012-02-11 19:59:35 +01:00
|
|
|
} else {
|
2013-10-30 02:10:39 +01:00
|
|
|
if (gc.arc_radius != 0) { // Arc Radius Mode
|
|
|
|
// Compute arc radius and offsets
|
|
|
|
gc_convert_arc_radius_mode(target);
|
|
|
|
if (gc.status_code) { return(gc.status_code); }
|
2012-02-11 19:59:35 +01:00
|
|
|
} else { // Arc Center Format Offset Mode
|
2013-10-30 02:10:39 +01:00
|
|
|
gc.arc_radius = hypot(gc.arc_offset[gc.plane_axis_0], gc.arc_offset[gc.plane_axis_1]); // Compute arc radius for mc_arc
|
2012-01-29 04:41:08 +01:00
|
|
|
}
|
2009-02-01 11:58:21 +01:00
|
|
|
|
2012-01-29 04:41:08 +01:00
|
|
|
// Set clockwise/counter-clockwise sign for mc_arc computations
|
|
|
|
uint8_t isclockwise = false;
|
|
|
|
if (gc.motion_mode == MOTION_MODE_CW_ARC) { isclockwise = true; }
|
|
|
|
|
|
|
|
// Trace the arc
|
2014-02-26 20:10:07 +01:00
|
|
|
#ifdef USE_LINE_NUMBERS
|
2013-10-30 02:10:39 +01:00
|
|
|
mc_arc(gc.position, target, gc.arc_offset, gc.plane_axis_0, gc.plane_axis_1, gc.plane_axis_2,
|
2012-01-29 04:41:08 +01:00
|
|
|
(gc.inverse_feed_rate_mode) ? inverse_feed_rate : gc.feed_rate, gc.inverse_feed_rate_mode,
|
2014-02-07 00:10:27 +01:00
|
|
|
gc.arc_radius, isclockwise, line_number);
|
2014-02-26 20:10:07 +01:00
|
|
|
#else
|
|
|
|
mc_arc(gc.position, target, gc.arc_offset, gc.plane_axis_0, gc.plane_axis_1, gc.plane_axis_2,
|
|
|
|
(gc.inverse_feed_rate_mode) ? inverse_feed_rate : gc.feed_rate, gc.inverse_feed_rate_mode,
|
|
|
|
gc.arc_radius, isclockwise);
|
|
|
|
#endif
|
2012-02-11 19:59:35 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Report any errors.
|
|
|
|
if (gc.status_code) { return(gc.status_code); }
|
|
|
|
|
|
|
|
// As far as the parser is concerned, the position is now == target. In reality the
|
|
|
|
// motion control system might still be processing the action and the real tool position
|
|
|
|
// in any intermediate location.
|
2012-11-01 16:37:27 +01:00
|
|
|
memcpy(gc.position, target, sizeof(target)); // gc.position[] = target[];
|
2009-01-25 00:48:56 +01:00
|
|
|
}
|
|
|
|
|
2012-02-11 19:59:35 +01:00
|
|
|
// M0,M1,M2,M30: Perform non-running program flow actions. During a program pause, the buffer may
|
|
|
|
// refill and can only be resumed by the cycle start run-time command.
|
2014-02-09 18:46:34 +01:00
|
|
|
if (gc.program_flow) {
|
|
|
|
protocol_buffer_synchronize(); // Finish all remaining buffered motions. Program paused when complete.
|
2012-11-02 02:48:55 +01:00
|
|
|
sys.auto_start = false; // Disable auto cycle start. Forces pause until cycle start issued.
|
2014-02-09 18:46:34 +01:00
|
|
|
|
2012-09-20 04:50:24 +02:00
|
|
|
// If complete, reset to reload defaults (G92.2,G54,G17,G90,G94,M48,G40,M5,M9). Otherwise,
|
|
|
|
// re-enable program flow after pause complete, where cycle start will resume the program.
|
2012-11-15 01:36:29 +01:00
|
|
|
if (gc.program_flow == PROGRAM_FLOW_COMPLETED) { mc_reset(); }
|
2012-09-20 04:50:24 +02:00
|
|
|
else { gc.program_flow = PROGRAM_FLOW_RUNNING; }
|
2014-02-09 18:46:34 +01:00
|
|
|
}
|
2012-01-29 04:41:08 +01:00
|
|
|
|
2009-02-03 09:56:45 +01:00
|
|
|
return(gc.status_code);
|
2009-01-25 00:48:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parses the next statement and leaves the counter on the first character following
|
|
|
|
// the statement. Returns 1 if there was a statements, 0 if end of string was reached
|
|
|
|
// or there was an error (check state.status_code).
|
2013-10-30 02:10:39 +01:00
|
|
|
static uint8_t next_statement(char *letter, float *float_ptr, char *line, uint8_t *char_counter)
|
2012-01-29 04:41:08 +01:00
|
|
|
{
|
2010-07-03 00:04:10 +02:00
|
|
|
if (line[*char_counter] == 0) {
|
2009-01-25 00:48:56 +01:00
|
|
|
return(0); // No more statements
|
|
|
|
}
|
|
|
|
|
2010-07-03 00:04:10 +02:00
|
|
|
*letter = line[*char_counter];
|
2009-01-25 00:48:56 +01:00
|
|
|
if((*letter < 'A') || (*letter > 'Z')) {
|
2011-02-18 22:59:16 +01:00
|
|
|
FAIL(STATUS_EXPECTED_COMMAND_LETTER);
|
2009-01-25 00:48:56 +01:00
|
|
|
return(0);
|
|
|
|
}
|
2010-07-03 00:04:10 +02:00
|
|
|
(*char_counter)++;
|
2012-10-08 23:57:58 +02:00
|
|
|
if (!read_float(line, char_counter, float_ptr)) {
|
2011-02-18 22:59:16 +01:00
|
|
|
FAIL(STATUS_BAD_NUMBER_FORMAT);
|
2009-01-25 00:48:56 +01:00
|
|
|
return(0);
|
|
|
|
};
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
2013-10-30 02:10:39 +01:00
|
|
|
|
|
|
|
static void gc_convert_arc_radius_mode(float *target)
|
|
|
|
{
|
|
|
|
/* We need to calculate the center of the circle that has the designated radius and passes
|
|
|
|
through both the current position and the target position. This method calculates the following
|
|
|
|
set of equations where [x,y] is the vector from current to target position, d == magnitude of
|
|
|
|
that vector, h == hypotenuse of the triangle formed by the radius of the circle, the distance to
|
|
|
|
the center of the travel vector. A vector perpendicular to the travel vector [-y,x] is scaled to the
|
|
|
|
length of h [-y/d*h, x/d*h] and added to the center of the travel vector [x/2,y/2] to form the new point
|
|
|
|
[i,j] at [x/2-y/d*h, y/2+x/d*h] which will be the center of our arc.
|
|
|
|
|
|
|
|
d^2 == x^2 + y^2
|
|
|
|
h^2 == r^2 - (d/2)^2
|
|
|
|
i == x/2 - y/d*h
|
|
|
|
j == y/2 + x/d*h
|
|
|
|
|
|
|
|
O <- [i,j]
|
|
|
|
- |
|
|
|
|
r - |
|
|
|
|
- |
|
|
|
|
- | h
|
|
|
|
- |
|
|
|
|
[0,0] -> C -----------------+--------------- T <- [x,y]
|
|
|
|
| <------ d/2 ---->|
|
|
|
|
|
|
|
|
C - Current position
|
|
|
|
T - Target position
|
|
|
|
O - center of circle that pass through both C and T
|
|
|
|
d - distance from C to T
|
|
|
|
r - designated radius
|
|
|
|
h - distance from center of CT to O
|
|
|
|
|
|
|
|
Expanding the equations:
|
|
|
|
|
|
|
|
d -> sqrt(x^2 + y^2)
|
|
|
|
h -> sqrt(4 * r^2 - x^2 - y^2)/2
|
|
|
|
i -> (x - (y * sqrt(4 * r^2 - x^2 - y^2)) / sqrt(x^2 + y^2)) / 2
|
|
|
|
j -> (y + (x * sqrt(4 * r^2 - x^2 - y^2)) / sqrt(x^2 + y^2)) / 2
|
|
|
|
|
|
|
|
Which can be written:
|
|
|
|
|
|
|
|
i -> (x - (y * sqrt(4 * r^2 - x^2 - y^2))/sqrt(x^2 + y^2))/2
|
|
|
|
j -> (y + (x * sqrt(4 * r^2 - x^2 - y^2))/sqrt(x^2 + y^2))/2
|
|
|
|
|
|
|
|
Which we for size and speed reasons optimize to:
|
|
|
|
|
|
|
|
h_x2_div_d = sqrt(4 * r^2 - x^2 - y^2)/sqrt(x^2 + y^2)
|
|
|
|
i = (x - (y * h_x2_div_d))/2
|
|
|
|
j = (y + (x * h_x2_div_d))/2 */
|
|
|
|
|
|
|
|
// Calculate the change in position along each selected axis
|
|
|
|
float x = target[gc.plane_axis_0]-gc.position[gc.plane_axis_0];
|
|
|
|
float y = target[gc.plane_axis_1]-gc.position[gc.plane_axis_1];
|
|
|
|
|
|
|
|
clear_vector(gc.arc_offset);
|
|
|
|
// First, use h_x2_div_d to compute 4*h^2 to check if it is negative or r is smaller
|
|
|
|
// than d. If so, the sqrt of a negative number is complex and error out.
|
|
|
|
float h_x2_div_d = 4 * gc.arc_radius*gc.arc_radius - x*x - y*y;
|
|
|
|
if (h_x2_div_d < 0) { FAIL(STATUS_ARC_RADIUS_ERROR); return; }
|
|
|
|
// Finish computing h_x2_div_d.
|
|
|
|
h_x2_div_d = -sqrt(h_x2_div_d)/hypot(x,y); // == -(h * 2 / d)
|
|
|
|
// Invert the sign of h_x2_div_d if the circle is counter clockwise (see sketch below)
|
|
|
|
if (gc.motion_mode == MOTION_MODE_CCW_ARC) { h_x2_div_d = -h_x2_div_d; }
|
|
|
|
|
|
|
|
/* The counter clockwise circle lies to the left of the target direction. When offset is positive,
|
|
|
|
the left hand circle will be generated - when it is negative the right hand circle is generated.
|
|
|
|
|
|
|
|
|
|
|
|
T <-- Target position
|
|
|
|
|
|
|
|
^
|
|
|
|
Clockwise circles with this center | Clockwise circles with this center will have
|
|
|
|
will have > 180 deg of angular travel | < 180 deg of angular travel, which is a good thing!
|
|
|
|
\ | /
|
|
|
|
center of arc when h_x2_div_d is positive -> x <----- | -----> x <- center of arc when h_x2_div_d is negative
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
C <-- Current position */
|
|
|
|
|
|
|
|
|
|
|
|
// Negative R is g-code-alese for "I want a circle with more than 180 degrees of travel" (go figure!),
|
|
|
|
// even though it is advised against ever generating such circles in a single line of g-code. By
|
|
|
|
// inverting the sign of h_x2_div_d the center of the circles is placed on the opposite side of the line of
|
|
|
|
// travel and thus we get the unadvisably long arcs as prescribed.
|
|
|
|
if (gc.arc_radius < 0) {
|
|
|
|
h_x2_div_d = -h_x2_div_d;
|
|
|
|
gc.arc_radius = -gc.arc_radius; // Finished with r. Set to positive for mc_arc
|
|
|
|
}
|
|
|
|
// Complete the operation by calculating the actual center of the arc
|
|
|
|
gc.arc_offset[gc.plane_axis_0] = 0.5*(x-(y*h_x2_div_d));
|
|
|
|
gc.arc_offset[gc.plane_axis_1] = 0.5*(y+(x*h_x2_div_d));
|
|
|
|
}
|
|
|
|
|
2011-01-31 21:32:36 +01:00
|
|
|
/*
|
2012-02-11 19:59:35 +01:00
|
|
|
Not supported:
|
2011-01-31 21:32:36 +01:00
|
|
|
|
2010-07-03 00:04:10 +02:00
|
|
|
- Canned cycles
|
|
|
|
- Tool radius compensation
|
|
|
|
- A,B,C-axes
|
|
|
|
- Evaluation of expressions
|
|
|
|
- Variables
|
|
|
|
- Probing
|
2012-11-16 05:53:11 +01:00
|
|
|
- Override control (TBD)
|
2012-02-11 19:59:35 +01:00
|
|
|
- Tool changes
|
2012-11-16 05:53:11 +01:00
|
|
|
- Switches
|
2012-09-22 01:55:02 +02:00
|
|
|
|
|
|
|
(*) Indicates optional parameter, enabled through config.h and re-compile
|
2012-02-11 19:59:35 +01:00
|
|
|
group 0 = {G92.2, G92.3} (Non modal: Cancel and re-enable G92 offsets)
|
|
|
|
group 1 = {G38.2, G81 - G89} (Motion modes: straight probe, canned cycles)
|
2012-11-16 05:53:11 +01:00
|
|
|
group 4 = {M1} (Optional stop, ignored)
|
2012-02-11 19:59:35 +01:00
|
|
|
group 6 = {M6} (Tool change)
|
2012-09-22 01:55:02 +02:00
|
|
|
group 8 = {*M7} enable mist coolant
|
2010-07-03 00:04:10 +02:00
|
|
|
group 9 = {M48, M49} enable/disable feed and speed override switches
|
|
|
|
group 13 = {G61, G61.1, G64} path control mode
|
2011-10-12 04:51:04 +02:00
|
|
|
*/
|