Skip to content

Commit

Permalink
Merge branch 'master' into arduino-uno-r4
Browse files Browse the repository at this point in the history
  • Loading branch information
Paciente8159 committed Nov 30, 2024
2 parents 57ebe6e + 14b589f commit 44bff83
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 104 deletions.
201 changes: 100 additions & 101 deletions uCNC/src/core/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,9 @@ static uint8_t parser_fetch_command(parser_state_t *new_state, parser_words_t *w

static uint8_t parser_validate_command(parser_state_t *new_state, parser_words_t *words, parser_cmd_explicit_t *cmd)
{
bool requires_feed = true;
bool has_axis = CHECKFLAG(cmd->words, GCODE_ALL_AXIS);

// only alow groups 3, 6 and modal G53
if (cnc_get_exec_state(EXEC_JOG))
{
Expand Down Expand Up @@ -990,6 +993,7 @@ static uint8_t parser_validate_command(parser_state_t *new_state, parser_words_t
#ifndef DISABLE_G10_SUPPORT
case G10:
// G10
requires_feed = false;
// if no P or L is present
if (!(cmd->words & (GCODE_WORD_P | GCODE_WORD_L)))
{
Expand All @@ -1000,8 +1004,10 @@ static uint8_t parser_validate_command(parser_state_t *new_state, parser_words_t
{
return STATUS_GCODE_UNSUPPORTED_COMMAND;
}
// P is not between 1 and N of coord systems
// P is not between 1 and N of coord systems
#ifndef DISABLE_HOME_SUPPORT
if (words->p != 28 && words->p != 30)
#endif
{
if (words->p < 0 || words->p > COORD_SYS_COUNT)
{
Expand All @@ -1011,7 +1017,8 @@ static uint8_t parser_validate_command(parser_state_t *new_state, parser_words_t
break;
#endif
case G92:
if (!CHECKFLAG(cmd->words, GCODE_ALL_AXIS))
requires_feed = false;
if (!has_axis)
{
return STATUS_GCODE_NO_AXIS_WORDS;
}
Expand All @@ -1030,17 +1037,20 @@ static uint8_t parser_validate_command(parser_state_t *new_state, parser_words_t
// group 1 - motion (incomplete)
// TODO
// subset of canned cycles
if (CHECKFLAG(cmd->groups, GCODE_GROUP_MOTION))
if (has_axis || CHECKFLAG(cmd->groups, GCODE_GROUP_MOTION))
{
switch (new_state->groups.motion)
{
#ifndef IGNORE_G0_G1_MISSING_AXIS_WORDS
case G0: // G0
requires_feed = false;
__FALL_THROUGH__
case G1: // G1
#ifdef IGNORE_G0_G1_MISSING_AXIS_WORDS
break;
#endif
#ifndef DISABLE_PROBING_SUPPORT
case G38: // G38.2, G38.3, G38.4, G38.5
if (!CHECKFLAG(cmd->words, GCODE_ALL_AXIS))
if (!has_axis)
{
return STATUS_GCODE_NO_AXIS_WORDS;
}
Expand Down Expand Up @@ -1102,11 +1112,11 @@ static uint8_t parser_validate_command(parser_state_t *new_state, parser_words_t
break;
#endif
case G80: // G80 and
if (CHECKFLAG(cmd->words, GCODE_ALL_AXIS) && !cmd->group_0_1_useaxis)
if (has_axis)
{
return STATUS_GCODE_AXIS_WORDS_EXIST;
}

requires_feed = false;
break;
#ifdef ENABLE_G39_H_MAPPING
case G39:
Expand Down Expand Up @@ -1134,7 +1144,7 @@ static uint8_t parser_validate_command(parser_state_t *new_state, parser_words_t
default: // G81..G89 canned cycles (partially implemented)
// It is an error if:
// X, Y, and Z words are all missing during a canned cycle,
if (!CHECKFLAG(cmd->words, GCODE_ALL_AXIS))
if (!has_axis)
{
return STATUS_GCODE_NO_AXIS_WORDS;
}
Expand Down Expand Up @@ -1167,7 +1177,7 @@ static uint8_t parser_validate_command(parser_state_t *new_state, parser_words_t
}

// group 5 - feed rate mode
if (new_state->groups.motion != G0)
if (requires_feed && has_axis)
{
if (!CHECKFLAG(cmd->words, GCODE_WORD_F))
{
Expand All @@ -1190,7 +1200,6 @@ static uint8_t parser_validate_command(parser_state_t *new_state, parser_words_t
}
}
}

// group 2 - plane selection (nothing to be checked)
// group 3 - distance mode (nothing to be checked)

Expand Down Expand Up @@ -1414,12 +1423,16 @@ static uint8_t parser_exec_command(parser_state_t *new_state, parser_words_t *wo
// 9. overrides
block_data.motion_flags.bit.feed_override = new_state->groups.feed_speed_override ? 1 : 0;

// 10. dwell
if (new_state->groups.nonmodal == G4)
// 10. dwell (or if any other nonmodal command except G53 requires a sync motion)
if (new_state->groups.nonmodal != 0 && new_state->groups.nonmodal != G53)
{
// calc dwell in milliseconds
block_data.dwell = MAX(block_data.dwell, (uint16_t)lroundf(MIN(words->p * 1000.0f, 65535)));
new_state->groups.nonmodal = 0;
itp_sync();
if (new_state->groups.nonmodal == G4)
{
// calc dwell in milliseconds
block_data.dwell = MAX(block_data.dwell, (uint16_t)lroundf(MIN(words->p * 1000.0f, 65535)));
new_state->groups.nonmodal = 0;
}
}

// after all spindle, overrides, coolant and dwells are set
Expand Down Expand Up @@ -1520,6 +1533,7 @@ static uint8_t parser_exec_command(parser_state_t *new_state, parser_words_t *wo
#ifndef DISABLE_COORD_SYS_SUPPORT
if (CHECKFLAG(cmd->groups, GCODE_GROUP_COORDSYS))
{
itp_sync();
parser_parameters.coord_system_index = new_state->groups.coord_system;
parser_coordinate_system_load(parser_parameters.coord_system_index, parser_parameters.coord_system_offset);
parser_wco_counter = 0;
Expand All @@ -1540,9 +1554,13 @@ static uint8_t parser_exec_command(parser_state_t *new_state, parser_words_t *wo

// 17. set distance mode (G90, G91)
memcpy(target, parser_last_pos, sizeof(parser_last_pos));
// absolute distances if distance mode is G90
bool abspos = (new_state->groups.distance_mode == G90);
// or if any nonmodal command is active (execept G4 that auto clears itself)
abspos |= (new_state->groups.nonmodal != 0);
// or if any nonmodal command is active (execept G4 that auto clears itself)

// for all not explicitly declared target retain their position or add offset
bool abspos = (new_state->groups.distance_mode == G90) | (new_state->groups.nonmodal == G53);
#ifdef AXIS_X
if (CHECKFLAG(cmd->words, GCODE_WORD_X))
{
Expand Down Expand Up @@ -1631,6 +1649,9 @@ static uint8_t parser_exec_command(parser_state_t *new_state, parser_words_t *wo
}
#endif
break;
case G92:
index = G92OFFSET;
break;
case G92_1: // G92.1
memset(g92permanentoffset, 0, sizeof(g92permanentoffset));
__FALL_THROUGH__
Expand All @@ -1645,45 +1666,47 @@ static uint8_t parser_exec_command(parser_state_t *new_state, parser_words_t *wo
parser_wco_counter = 0;
new_state->groups.nonmodal = 0; // this command is compatible with motion commands
break;
case G53:
index = 254;
new_state->groups.nonmodal = 0; // this command is compatible with motion commands
break;
}

// check from were to read the previous values for the target array
if (index == 255)
float coords[AXIS_COUNT];
bool relative_target = true;
float *relative_offset = NULL;
switch (index)
{
switch (new_state->groups.nonmodal)
case 254: // G53 (passthrough)
break;
case 255: // No nonmodal
if ((new_state->groups.distance_mode == G90))
{
case G53:
// G28 and G30 make the planed motion (absolute or relative)
// case G28:
// case G30:
break;
default:
if ((new_state->groups.distance_mode == G90))
for (uint8_t i = AXIS_COUNT; i != 0;)
{
for (uint8_t i = AXIS_COUNT; i != 0;)
i--;
if (CHECKFLAG(cmd->words, (1 << i)))
{
i--;
if (CHECKFLAG(cmd->words, (1 << i)))
{
target[i] += parser_parameters.coord_system_offset[i] + parser_parameters.g92_offset[i];
}
target[i] += parser_parameters.coord_system_offset[i] + parser_parameters.g92_offset[i];
}
}
#ifdef AXIS_TOOL
if (CHECKFLAG(cmd->words, (1 << AXIS_TOOL)))
{
target[AXIS_TOOL] += parser_parameters.tool_length_offset;
}
#endif
if (CHECKFLAG(cmd->words, (1 << AXIS_TOOL)))
{
target[AXIS_TOOL] += parser_parameters.tool_length_offset;
}
break;
#endif
}
}

// stores G10 L2 command in the right address
break;
case G92OFFSET:
relative_offset = parser_parameters.coord_system_offset;
memcpy(coords, parser_parameters.g92_offset, sizeof(coords));
break;
#ifndef DISABLE_G10_SUPPORT
if (index <= G30HOME)
{
float coords[AXIS_COUNT];
default:
relative_offset = parser_parameters.g92_offset;
relative_target = (words->l == 20);
if (index == parser_parameters.coord_system_index)
{
memcpy(coords, parser_parameters.coord_system_offset, sizeof(coords));
Expand All @@ -1692,31 +1715,47 @@ static uint8_t parser_exec_command(parser_state_t *new_state, parser_words_t *wo
{
parser_coordinate_system_load(index, coords);
}
break;
#endif
}

if (index <= G92OFFSET)
{
for (uint8_t i = AXIS_COUNT; i != 0;)
{
i--;
if (CHECKFLAG(cmd->words, (1 << i)))
{
if (words->l == 20)
{
coords[i] = -(target[i] - parser_last_pos[i] - parser_parameters.g92_offset[i]);
}
else
{
coords[i] = target[i];
}
coords[i] = (relative_target) ? -(target[i] - parser_last_pos[i] + relative_offset[i]) : target[i];
}
}
#ifdef AXIS_TOOL
if (words->l == 20)
if (relative_target)
{
if (CHECKFLAG(cmd->words, (1 << AXIS_TOOL)))
{
coords[AXIS_TOOL] += parser_parameters.tool_length_offset;
}
}
#endif
}

// stores G10 or G92 command in the right address
switch (index)
{
case 254:
case 255:
break;
case G92OFFSET:
memcpy(parser_parameters.g92_offset, coords, sizeof(parser_parameters.g92_offset));
memcpy(g92permanentoffset, parser_parameters.g92_offset, sizeof(g92permanentoffset));
#ifdef G92_STORE_NONVOLATILE
settings_save(G92ADDRESS, (uint8_t *)&g92permanentoffset, PARSER_PARAM_SIZE);
#endif
parser_wco_counter = 0;
break;
#ifndef DISABLE_G10_SUPPORT
default:
settings_save(SETTINGS_PARSER_PARAMETERS_ADDRESS_OFFSET + (index * PARSER_PARAM_ADDR_OFFSET), (uint8_t *)coords, PARSER_PARAM_SIZE);
#ifndef DISABLE_COORDINATES_SYSTEM_RAM
memcpy(&coordinate_systems[index], coords, PARSER_PARAM_SIZE);
Expand All @@ -1726,60 +1765,16 @@ static uint8_t parser_exec_command(parser_state_t *new_state, parser_words_t *wo
memcpy(parser_parameters.coord_system_offset, coords, PARSER_PARAM_SIZE);
}
parser_wco_counter = 0;
}
break;
#endif
}

// laser disabled in nonmodal moves
if (g_settings.laser_mode && new_state->groups.nonmodal)
{
block_data.spindle = 0;
}

switch (new_state->groups.nonmodal)
{
#ifndef DISABLE_HOME_SUPPORT
case G28: // G28
case G30: // G30
block_data.feed = FLT_MAX;
if (CHECKFLAG(cmd->words, GCODE_ALL_AXIS))
{
error = mc_line(target, &block_data);
update_tools = false;
if (error)
{
return error;
}
}

if (new_state->groups.nonmodal == G28)
{
parser_coordinate_system_load(G28HOME, target);
}
else
{
parser_coordinate_system_load(G30HOME, target);
}
error = mc_line((float *)&target, &block_data);
// saves position
memcpy(parser_last_pos, target, sizeof(parser_last_pos));
break;
#endif
case G92: // G92
for (uint8_t i = AXIS_COUNT; i != 0;)
{
i--;
parser_parameters.g92_offset[i] = -(target[i] - parser_last_pos[i] - parser_parameters.g92_offset[i]);
}
memcpy(g92permanentoffset, parser_parameters.g92_offset, sizeof(g92permanentoffset));
#ifdef G92_STORE_NONVOLATILE
settings_save(G92ADDRESS, (uint8_t *)&g92permanentoffset, PARSER_PARAM_SIZE);
#endif
parser_wco_counter = 0;
break;
case G53:
new_state->groups.nonmodal = 0; // this command is compatible with motion commands
break;
}

// 20. perform motion (G0 to G3, G80 to G89), as modified (possibly) by G53.
// G80 does no motion
// G81 to G89 is executed in a separate function and uses G53,G0,G1 and G4 has building blocks
Expand Down Expand Up @@ -2333,8 +2328,6 @@ static uint8_t parser_gcode_word(uint8_t code, uint8_t mantissa, parser_state_t
}
}

new_state->groups.motion_mantissa = mantissa;

switch (code)
{
// motion codes
Expand Down Expand Up @@ -2392,6 +2385,7 @@ static uint8_t parser_gcode_word(uint8_t code, uint8_t mantissa, parser_state_t
#endif
new_group |= GCODE_GROUP_MOTION;
new_state->groups.motion = code;
new_state->groups.motion_mantissa = mantissa;
break;
#ifndef DISABLE_ARC_SUPPORT
case 17:
Expand Down Expand Up @@ -2803,6 +2797,7 @@ void parser_reset(bool fullreset)
parser_state.groups.path_mode = G61;
#endif
parser_state.groups.motion = G1; // G1
parser_state.groups.motion_mantissa = 0;
parser_state.groups.units = G21; // G21
parser_wco_counter = 0;
#ifdef ENABLE_G39_H_MAPPING
Expand Down Expand Up @@ -3228,6 +3223,10 @@ uint8_t parser_exec_command_block(parser_state_t *new_state, parser_words_t *wor
}
}

// set possible feed and spindle changes embedded in the canned command
new_state->feedrate = canned_state.feedrate;
new_state->spindle = canned_state.spindle;

return error;
}
#endif
Expand Down
Loading

0 comments on commit 44bff83

Please sign in to comment.