Skip to content

Commit

Permalink
Refactored g-code parser. Saved 60bytes flash and some ram. Edited Re…
Browse files Browse the repository at this point in the history
…adme.

- Freed up another 60 bytes of flash and 12-24 bytes of stack RAM by
using the pre-allocated IJK arc offset vector that is guaranteed to be
not in use. Only G10 and G28/30 require fetching from EEPROM and
retaining extra data. Both commands use axis words, which rules out
G2/3 arcs using IJK offsets existing in same block. Not ideal, but
every byte helps.

- Edited README.
  • Loading branch information
chamnit committed Sep 28, 2016
1 parent 669735b commit bf5fc48
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 27 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ Grbl includes full acceleration management with look ahead. That means the contr
##Update Summary for v1.1
- **IMPORTANT:** Your EEPROM will be wiped and restored with new settings. This is due to the addition of two new spindle speed '$' settings.

- **Real-time Overrides** : Alters the machine running state immediately with feed, rapid, spindle speed, spindle stop, and coolant toggle controls. This huge contribution by the Grbl project is feature that is common only on industrial machines, often used to optimize cutting while the job is running. Most hobby CNC's try to mimic this with varying results.
- **Real-time Overrides** : Alters the machine running state immediately with feed, rapid, spindle speed, spindle stop, and coolant toggle controls. This awesome new feature is common only on industrial machines, often used to optimize speeds and feeds while a job is running. Most hobby CNC's try to mimic this behavior, but usually have large amounts of lag. Grbl executes overrides in realtime and within tens of milliseconds.

- **Jogging Mode** : New jogging commands seperate from the g-code parser, so that the parser state doesn't get altered and cause a potential crash if not restored properly. Documentation is included on how to use this with a joystick with a low-latency.
- **Jogging Mode** : The new jogging commands are independent of the g-code parser, so that the parser state doesn't get altered and cause a potential crash if not restored properly. Documentation is included on how this works and how it can be used to control your machine via a joystick or rotary dial with a low-latency, satisfying response.

- **Laser Mode** : Grbl now has a "laser" mode, where consecutive G1, G2, and G3 commands do not stop and continuously move through them with a spindle speed change. Spindle speed overrides also work with laser mode so you can tweak the laser power, if you need to during the job. Switch between "laser" mode and "normal" mode via a `$` setting.
- **Laser Mode** : The new "laser" mode will cause Grbl to move continuously through consecutive G1, G2, and G3 commands with spindle speed changes. When "laser" mode is disabled, Grbl will instead come to a stop to ensure a spindle comes up to speed properly. Spindle speed overrides also work with laser mode so you can tweak the laser power, if you need to during the job. Switch between "laser" mode and "normal" mode via a `$` setting.

- **Significant Interface Improvements**: Just this one time and done simultaneously with adding override controls, Grbl has tweaked the communication interface to make it easier for developers to write better GUIs. _NOTE: GUIs need to specifically update their code to be compatible with v1.1 and later._
- **Significant Interface Improvements**: Just this one time and done simultaneously with adding override data and controls, Grbl has tweaked the communication interface to make it easier for developers to write and maintain their GUIs. _NOTE: GUIs need to specifically update their code to be compatible with v1.1 and later._

- **New Status Reports**: To account for the additional override data, status reports have been tweaked to cram more data into it, while still being smaller than before. Documentation is included, outlining how it has been changed.
- **Improved Error/Alarm Feedback** : All Grbl error and alarm messages have been changed to providing a code. Each code is associated with a specific problem, so users will know exactly what is wrong without having to guess. Documentation and an easy to parse CSV is included in the repo.
Expand Down
6 changes: 6 additions & 0 deletions doc/log/commit_log_v1.1.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
----------------
Date: 2016-09-27
Author: Sonny Jeon
Subject: Update README and clarifications in jogging document.


----------------
Date: 2016-09-26
Author: Sonny Jeon
Expand Down
47 changes: 25 additions & 22 deletions grbl/gcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ uint8_t gc_execute_line(char *line)
uint8_t axis_command = AXIS_COMMAND_NONE;
uint8_t axis_0, axis_1, axis_linear;
uint8_t coord_select = 0; // Tracks G10 P coordinate selection for execution
float coordinate_data[N_AXIS]; // Multi-use variable to store coordinate data for execution
float parameter_data[N_AXIS]; // Multi-use variable to store parameter data for execution

// Initialize bitflag tracking variables for axis indices compatible operations.
uint8_t axis_words = 0; // XYZ tracking
Expand Down Expand Up @@ -526,11 +524,12 @@ uint8_t gc_execute_line(char *line)
// is active. The read pauses the processor temporarily and may cause a rare crash. For
// future versions on processors with enough memory, all coordinate data should be stored
// in memory and written to EEPROM only when there is not a cycle active.
memcpy(coordinate_data,gc_state.coord_system,sizeof(gc_state.coord_system));
float block_coord_system[N_AXIS];
memcpy(block_coord_system,gc_state.coord_system,sizeof(gc_state.coord_system));
if ( bit_istrue(command_words,bit(MODAL_GROUP_G12)) ) { // Check if called in block
if (gc_block.modal.coord_select > N_COORDINATE_SYSTEM) { FAIL(STATUS_GCODE_UNSUPPORTED_COORD_SYS); } // [Greater than N sys]
if (gc_state.modal.coord_select != gc_block.modal.coord_select) {
if (!(settings_read_coord_data(gc_block.modal.coord_select,coordinate_data))) { FAIL(STATUS_SETTING_READ_FAIL); }
if (!(settings_read_coord_data(gc_block.modal.coord_select,block_coord_system))) { FAIL(STATUS_SETTING_READ_FAIL); }
}
}

Expand Down Expand Up @@ -562,21 +561,24 @@ uint8_t gc_execute_line(char *line)
// Determine coordinate system to change and try to load from EEPROM.
if (coord_select > 0) { coord_select--; } // Adjust P1-P6 index to EEPROM coordinate data indexing.
else { coord_select = gc_block.modal.coord_select; } // Index P0 as the active coordinate system
if (!settings_read_coord_data(coord_select,parameter_data)) { FAIL(STATUS_SETTING_READ_FAIL); } // [EEPROM read fail]

// NOTE: Store parameter data in IJK values. By rule, they are not in use with this command.
if (!settings_read_coord_data(coord_select,gc_block.values.ijk)) { FAIL(STATUS_SETTING_READ_FAIL); } // [EEPROM read fail]

// Pre-calculate the coordinate data changes. NOTE: Uses parameter_data since coordinate_data may be in use by G54-59.
// Pre-calculate the coordinate data changes.
for (idx=0; idx<N_AXIS; idx++) { // Axes indices are consistent, so loop may be used.
// Update axes defined only in block. Always in machine coordinates. Can change non-active system.
if (bit_istrue(axis_words,bit(idx)) ) {
if (gc_block.values.l == 20) {
// L20: Update coordinate system axis at current position (with modifiers) with programmed value
parameter_data[idx] = gc_state.position[idx]-gc_state.coord_offset[idx]-gc_block.values.xyz[idx];
if (idx == TOOL_LENGTH_OFFSET_AXIS) { parameter_data[idx] -= gc_state.tool_length_offset; }
// WPos = MPos - WCS - G92 - TLO -> WCS = MPos - G92 - TLO - WPos
gc_block.values.ijk[idx] = gc_state.position[idx]-gc_state.coord_offset[idx]-gc_block.values.xyz[idx];
if (idx == TOOL_LENGTH_OFFSET_AXIS) { gc_block.values.ijk[idx] -= gc_state.tool_length_offset; }
} else {
// L2: Update coordinate system axis to programmed value.
parameter_data[idx] = gc_block.values.xyz[idx];
gc_block.values.ijk[idx] = gc_block.values.xyz[idx];
}
}
} // Else, keep current stored value.
}
break;
case NON_MODAL_SET_COORDINATE_OFFSET:
Expand All @@ -587,7 +589,8 @@ uint8_t gc_execute_line(char *line)
// active coordinate system is selected, but is still active unless G92.1 disables it.
for (idx=0; idx<N_AXIS; idx++) { // Axes indices are consistent, so loop may be used.
if (bit_istrue(axis_words,bit(idx)) ) {
gc_block.values.xyz[idx] = gc_state.position[idx]-coordinate_data[idx]-gc_block.values.xyz[idx];
// WPos = MPos - WCS - G92 - TLO -> G92 = MPos - WCS - TLO - WPos
gc_block.values.xyz[idx] = gc_state.position[idx]-block_coord_system[idx]-gc_block.values.xyz[idx];
if (idx == TOOL_LENGTH_OFFSET_AXIS) { gc_block.values.xyz[idx] -= gc_state.tool_length_offset; }
} else {
gc_block.values.xyz[idx] = gc_state.coord_offset[idx];
Expand All @@ -612,7 +615,7 @@ uint8_t gc_execute_line(char *line)
if (gc_block.non_modal_command != NON_MODAL_ABSOLUTE_OVERRIDE) {
// Apply coordinate offsets based on distance mode.
if (gc_block.modal.distance == DISTANCE_MODE_ABSOLUTE) {
gc_block.values.xyz[idx] += coordinate_data[idx] + gc_state.coord_offset[idx];
gc_block.values.xyz[idx] += block_coord_system[idx] + gc_state.coord_offset[idx];
if (idx == TOOL_LENGTH_OFFSET_AXIS) { gc_block.values.xyz[idx] += gc_state.tool_length_offset; }
} else { // Incremental mode
gc_block.values.xyz[idx] += gc_state.position[idx];
Expand All @@ -629,15 +632,16 @@ uint8_t gc_execute_line(char *line)
case NON_MODAL_GO_HOME_1: // G30
// [G28/30 Errors]: Cutter compensation is enabled.
// Retreive G28/30 go-home position data (in machine coordinates) from EEPROM
// NOTE: Store parameter data in IJK values. By rule, they are not in use with this command.
if (gc_block.non_modal_command == NON_MODAL_GO_HOME_0) {
if (!settings_read_coord_data(SETTING_INDEX_G28,parameter_data)) { FAIL(STATUS_SETTING_READ_FAIL); }
if (!settings_read_coord_data(SETTING_INDEX_G28,gc_block.values.ijk)) { FAIL(STATUS_SETTING_READ_FAIL); }
} else { // == NON_MODAL_GO_HOME_1
if (!settings_read_coord_data(SETTING_INDEX_G30,parameter_data)) { FAIL(STATUS_SETTING_READ_FAIL); }
if (!settings_read_coord_data(SETTING_INDEX_G30,gc_block.values.ijk)) { FAIL(STATUS_SETTING_READ_FAIL); }
}
if (axis_words) {
// Move only the axes specified in secondary move.
for (idx=0; idx<N_AXIS; idx++) {
if (!(axis_words & (1<<idx))) { parameter_data[idx] = gc_state.position[idx]; }
if (!(axis_words & (1<<idx))) { gc_block.values.ijk[idx] = gc_state.position[idx]; }
}
} else {
axis_command = AXIS_COMMAND_NONE; // Set to none if no intermediate motion.
Expand Down Expand Up @@ -964,7 +968,7 @@ uint8_t gc_execute_line(char *line)
// [15. Coordinate system selection ]:
if (gc_state.modal.coord_select != gc_block.modal.coord_select) {
gc_state.modal.coord_select = gc_block.modal.coord_select;
memcpy(gc_state.coord_system,coordinate_data,sizeof(coordinate_data));
memcpy(gc_state.coord_system,block_coord_system,N_AXIS*sizeof(float));
system_flag_wco_change();
}

Expand All @@ -979,10 +983,10 @@ uint8_t gc_execute_line(char *line)
// [19. Go to predefined position, Set G10, or Set axis offsets ]:
switch(gc_block.non_modal_command) {
case NON_MODAL_SET_COORDINATE_DATA:
settings_write_coord_data(coord_select,parameter_data);
settings_write_coord_data(coord_select,gc_block.values.xyz);
// Update system coordinate system if currently active.
if (gc_state.modal.coord_select == coord_select) {
memcpy(gc_state.coord_system,parameter_data,sizeof(parameter_data));
memcpy(gc_state.coord_system,gc_block.values.xyz,N_AXIS*sizeof(float));
system_flag_wco_change();
}
break;
Expand All @@ -991,8 +995,8 @@ uint8_t gc_execute_line(char *line)
// and absolute and incremental modes.
pl_data->condition |= PL_COND_FLAG_RAPID_MOTION; // Set rapid motion condition flag.
if (axis_command) { mc_line(gc_block.values.xyz, pl_data); }
mc_line(parameter_data, pl_data);
memcpy(gc_state.position, parameter_data, sizeof(parameter_data));
mc_line(gc_block.values.ijk, pl_data);
memcpy(gc_state.position, gc_block.values.ijk, N_AXIS*sizeof(float));
break;
case NON_MODAL_SET_HOME_0:
settings_write_coord_data(SETTING_INDEX_G28,gc_state.position);
Expand Down Expand Up @@ -1085,8 +1089,7 @@ uint8_t gc_execute_line(char *line)

// Execute coordinate change and spindle/coolant stop.
if (sys.state != STATE_CHECK_MODE) {
if (!(settings_read_coord_data(gc_state.modal.coord_select,coordinate_data))) { FAIL(STATUS_SETTING_READ_FAIL); }
memcpy(gc_state.coord_system,coordinate_data,sizeof(coordinate_data));
if (!(settings_read_coord_data(gc_state.modal.coord_select,gc_state.coord_system))) { FAIL(STATUS_SETTING_READ_FAIL); }
system_flag_wco_change(); // Set to refresh immediately just in case something altered.
spindle_stop();
coolant_set_state(COOLANT_DISABLE);
Expand Down
2 changes: 1 addition & 1 deletion grbl/grbl.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

// Grbl versioning system
#define GRBL_VERSION "1.1b"
#define GRBL_VERSION_BUILD "20160926"
#define GRBL_VERSION_BUILD "20160927"

// Define standard libraries used by Grbl.
#include <avr/io.h>
Expand Down

0 comments on commit bf5fc48

Please sign in to comment.