Skip to content

Commit

Permalink
Merge pull request #170 from Paciente8159/implement-report-status-mask
Browse files Browse the repository at this point in the history
implemented report status mask
  • Loading branch information
Paciente8159 authored Apr 18, 2022
2 parents 8233b49 + a3dcf5b commit e599acb
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 45 deletions.
14 changes: 7 additions & 7 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,30 @@
"type": "platformio-debug",
"request": "launch",
"name": "PIO Debug",
"executable": "c:/Users/JCEM/Documents/GitHub/uCNC/.pio/build/x_controller/firmware.elf",
"projectEnvName": "x_controller",
"executable": "c:/Users/JCEM/Documents/GitHub/uCNC/.pio/build/uno/firmware.elf",
"projectEnvName": "uno",
"toolchainBinDir": "C:/gcc-avr8/bin",
"internalConsoleOptions": "openOnSessionStart",
"preLaunchTask": {
"type": "PlatformIO",
"task": "Pre-Debug (x_controller)"
"task": "Pre-Debug (uno)"
}
},
{
"type": "platformio-debug",
"request": "launch",
"name": "PIO Debug (skip Pre-Debug)",
"executable": "c:/Users/JCEM/Documents/GitHub/uCNC/.pio/build/x_controller/firmware.elf",
"projectEnvName": "x_controller",
"executable": "c:/Users/JCEM/Documents/GitHub/uCNC/.pio/build/uno/firmware.elf",
"projectEnvName": "uno",
"toolchainBinDir": "C:/gcc-avr8/bin",
"internalConsoleOptions": "openOnSessionStart"
},
{
"type": "platformio-debug",
"request": "launch",
"name": "PIO Debug (without uploading)",
"executable": "c:/Users/JCEM/Documents/GitHub/uCNC/.pio/build/x_controller/firmware.elf",
"projectEnvName": "x_controller",
"executable": "c:/Users/JCEM/Documents/GitHub/uCNC/.pio/build/uno/firmware.elf",
"projectEnvName": "uno",
"toolchainBinDir": "C:/gcc-avr8/bin",
"internalConsoleOptions": "openOnSessionStart",
"loadMode": "manual"
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@
### Changed

- added option to accept G0 and G1 without explicit axis words to target (#169)
- implemented Grbl report status mask (#170)
- full Grbl welcome message emulation to fix recognition by PC softwares (#170)
- modified startup blocks execution to match Grbl behavior (#170)

### Fixed

- fixed parser wrong coordinates after coordinate offset command changes axis followed by a motion command with implicit target coordinates modified by the previous command, resulting in unexpected motion path (#170)
- forces run state flag during dwell (reported idle state) (#170)


## [1.4.1] - 2022-04-17

Expand Down
67 changes: 41 additions & 26 deletions uCNC/src/cnc.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@
#include "cnc.h"

#define LOOP_STARTUP_RESET 0
#define LOOP_RUNNING_FIRST_RUN 1
#define LOOP_RUNNING 2
#define LOOP_ERROR_RESET 3
#define LOOP_RUNNING 1
#define LOOP_ERROR_RESET 2

#define UNLOCK_ERROR 0
#define UNLOCK_OK 0
#define UNLOCK_LOCKED 1
#define UNLOCK_OK 2
#define UNLOCK_ERROR 2

#define RTCMD_NORMAL_MASK (RT_CMD_FEED_100 | RT_CMD_FEED_INC_COARSE | RT_CMD_FEED_DEC_COARSE | RT_CMD_FEED_INC_FINE | RT_CMD_FEED_DEC_FINE)
#define RTCMD_RAPID_MASK (RT_CMD_RAPIDFEED_100 | RT_CMD_RAPIDFEED_OVR1 | RT_CMD_RAPIDFEED_OVR2)
Expand All @@ -47,19 +46,22 @@ typedef struct
} cnc_state_t;

static cnc_state_t cnc_state;
bool cnc_status_report_lock;

static void cnc_check_fault_systems(void);
static bool cnc_check_interlocking(void);
static void cnc_exec_rt_commands(void);
static void cnc_io_dotasks(void);
static void cnc_reset(void);
static bool cnc_exec_cmd(void);
static void cnc_run_startup_blocks(void);

void cnc_init(void)
{
// initializes cnc state
#ifdef FORCE_GLOBALS_TO_0
memset(&cnc_state, 0, sizeof(cnc_state_t));
cnc_status_report_lock = false;
#endif
cnc_state.loop_state = LOOP_STARTUP_RESET;
// initializes all systems
Expand All @@ -78,14 +80,15 @@ void cnc_init(void)

void cnc_run(void)
{
// enters loop reset
cnc_reset();

cnc_state.loop_state = LOOP_RUNNING_FIRST_RUN;
serial_select(SERIAL_UART);

// tries to reset. If fails jumps to error
while (cnc_unlock(false))
while (cnc_unlock(false) != UNLOCK_ERROR)
{
serial_select(SERIAL_UART);
cnc_state.loop_state = LOOP_RUNNING;

do
{
} while (cnc_exec_cmd());
Expand Down Expand Up @@ -115,7 +118,15 @@ void cnc_run(void)
}
}
cnc_dotasks();
} while (io_get_controls() & ESTOP_MASK);
if (io_get_controls() & ESTOP_MASK)
{
cnc_state.loop_state = LOOP_STARTUP_RESET;
}
else
{
break;
}
} while (1);
}

bool cnc_exec_cmd(void)
Expand Down Expand Up @@ -143,8 +154,6 @@ bool cnc_exec_cmd(void)
{
protocol_send_error(error);
}

cnc_state.loop_state = LOOP_RUNNING;
}

return cnc_dotasks();
Expand Down Expand Up @@ -267,6 +276,7 @@ void cnc_home(void)

// sync's the motion control with the real time position
mc_sync_position();
cnc_run_startup_blocks();
}

void cnc_alarm(int8_t code)
Expand Down Expand Up @@ -333,19 +343,11 @@ uint8_t cnc_unlock(bool force)
io_enable_steppers(g_settings.step_enable_invert);
parser_reset();

if (cnc_state.loop_state < LOOP_RUNNING)
// hard reset
// if homing not enabled run startup blocks
if (cnc_state.loop_state == LOOP_STARTUP_RESET && !g_settings.homing_enabled)
{
serial_select(SERIAL_N0);
if (!cnc_exec_cmd())
{
return UNLOCK_ERROR;
}
serial_select(SERIAL_N1);
if (!cnc_exec_cmd())
{
return UNLOCK_ERROR;
}
serial_select(SERIAL_UART);
cnc_run_startup_blocks();
}
}

Expand Down Expand Up @@ -451,7 +453,6 @@ void cnc_delay_ms(uint32_t miliseconds)

void cnc_reset(void)
{
cnc_state.loop_state = LOOP_STARTUP_RESET;
// resets all realtime command flags
cnc_state.rt_cmd = RT_CMD_CLEAR;
cnc_state.feed_ovr_cmd = RT_CMD_CLEAR;
Expand Down Expand Up @@ -802,10 +803,24 @@ static void cnc_io_dotasks(void)
#endif
mcu_controls_changed_cb();

if (cnc_state.loop_state > LOOP_STARTUP_RESET && CHECKFLAG(cnc_state.rt_cmd, RT_CMD_REPORT))
if (cnc_status_report_lock)
{
return;
}

if (CHECKFLAG(cnc_state.rt_cmd, RT_CMD_REPORT))
{
// if a report request is sent, clear the respective flag
CLEARFLAG(cnc_state.rt_cmd, RT_CMD_REPORT);
protocol_send_status();
}
}

void cnc_run_startup_blocks(void)
{
serial_select(SERIAL_N0);
cnc_exec_cmd();
serial_select(SERIAL_N1);
cnc_exec_cmd();
serial_select(SERIAL_UART);
}
2 changes: 2 additions & 0 deletions uCNC/src/cnc.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ extern "C"
#include <stdbool.h>
#include <stdint.h>

extern bool cnc_status_report_lock;

void cnc_init(void);
void cnc_run(void);
// do events returns true if all OK and false if an ABORT alarm is reached
Expand Down
6 changes: 5 additions & 1 deletion uCNC/src/core/motion_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,11 @@ uint8_t mc_dwell(motion_data_t *block_data)
if (!mc_checkmode) // check mode (gcode simulation) doesn't send code to planner
{
mc_update_tools(block_data);
// restores/forces run flag
cnc_set_exec_state(EXEC_RUN);
cnc_delay_ms(block_data->dwell);
// clear forced run flag to allow next motions
cnc_clear_exec_state(EXEC_RUN);
}

return STATUS_OK;
Expand Down Expand Up @@ -599,7 +603,7 @@ uint8_t mc_home_axis(uint8_t axis, uint8_t axis_limit)
// flags homing clear by the unlock
cnc_set_exec_state(EXEC_HOMING);
mc_line(target, &block_data);

if (itp_sync() != STATUS_OK)
{
return STATUS_CRITICAL_FAIL;
Expand Down
21 changes: 18 additions & 3 deletions uCNC/src/core/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -1466,16 +1466,16 @@ uint8_t parser_exec_command(parser_state_t *new_state, parser_words_t *words, pa

return error;
}

// saves position
memcpy(parser_last_pos, target, sizeof(parser_last_pos));
}

if (error)
{
return error;
}

// saves position
memcpy(parser_last_pos, target, sizeof(parser_last_pos));

// stop (M0, M1, M2, M30, M60) (not implemented yet).
bool hold = false;
bool resetparser = false;
Expand Down Expand Up @@ -2682,3 +2682,18 @@ uint8_t parser_exec_command_block(parser_state_t *new_state, parser_words_t *wor
return error;
}
#endif

void parser_machine_to_work(float *axis)
{
for (uint8_t i = 0; i < AXIS_COUNT; i++)
{
axis[i] -= (parser_parameters.g92_offset[i] + parser_parameters.coord_system_offset[i]);
}

#ifdef AXIS_TOOL
if (parser_state.groups.tlo_mode != G49)
{
axis[AXIS_TOOL] -= parser_parameters.tool_length_offset;
}
#endif
}
1 change: 1 addition & 0 deletions uCNC/src/core/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ extern "C"
void parser_parameters_save(void);
void parser_sync_position(void);
void parser_reset(void);
void parser_machine_to_work(float *axis);
uint8_t parser_exec_command(parser_state_t *new_state, parser_words_t *words, parser_cmd_explicit_t *cmd);

#ifdef __cplusplus
Expand Down
5 changes: 5 additions & 0 deletions uCNC/src/core/planner.c
Original file line number Diff line number Diff line change
Expand Up @@ -659,3 +659,8 @@ bool planner_get_overflows(uint8_t *overflows)
planner_ovr_counter--;
return false;
}

uint8_t planner_get_buffer_freeblocks()
{
return PLANNER_BUFFER_SIZE - planner_data_blocks;
}
2 changes: 2 additions & 0 deletions uCNC/src/core/planner.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ extern "C"

bool planner_get_overflows(uint8_t *overflows);

uint8_t planner_get_buffer_freeblocks();

#ifdef __cplusplus
}
#endif
Expand Down
8 changes: 5 additions & 3 deletions uCNC/src/interface/grbl_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ extern "C"
#ifndef EMULATE_GRBL_STARTUP
#define MSG_STARTUP_START "uCNC "
#define MSG_STARTUP_END " ['$' for help]"
#define MSG_STARTUP __romstr__(STR_EOL MSG_STARTUP_START CNC_VERSION MSG_STARTUP_END STR_EOL)
#else
#define MSG_STARTUP_START "Grbl "
#define MSG_STARTUP_END " ['$' for uCNC help]"
#define MSG_STARTUP __romstr__(STR_EOL "Grbl 1.1h ['$' for help]" STR_EOL)
#endif
#define MSG_STARTUP __romstr__(MSG_STARTUP_START CNC_VERSION MSG_STARTUP_END STR_EOL)

#define MSG_HELP __romstr__("[HLP:$$ $# $G $I $N $x=val $Nx=line $J=line $C $X $H ~ ! ? ctrl-x]" STR_EOL)

// Non query feedback messages
Expand Down Expand Up @@ -186,13 +186,15 @@ extern "C"
#define MSG_STATUS_CHECK __romstr__("Check")

#define MSG_STATUS_MPOS __romstr__("|MPos:")
#define MSG_STATUS_WPOS __romstr__("|WPos:")
#define MSG_STATUS_FS __romstr__("|FS:")
#define MSG_STATUS_F __romstr__("|F:")
#define MSG_STATUS_WCO __romstr__("|WCO:")
#define MSG_STATUS_OVR __romstr__("|Ov:")
#define MSG_STATUS_TOOL __romstr__("|A:")
#define MSG_STATUS_LINE __romstr__("|Ln:")
#define MSG_STATUS_PIN __romstr__("|Pn:")
#define MSG_STATUS_BUF __romstr__("|Buf:")

//#define MSG_INT "%d"
//#define MSG_FLT "%0.3f"
Expand Down
25 changes: 20 additions & 5 deletions uCNC/src/interface/protocol.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
/*
Name: protocol.h
Name: protocol.c
Description: µCNC implementation of a Grbl compatible send-response protocol
Copyright: Copyright (c) João Martins
Author: João Martins
Date: 19/09/2019
µCNC 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. Please see <http://www.gnu.org/licenses/>
µCNC is distributed WITHOUT ANY WARRANTY;
Also without the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
Expand Down Expand Up @@ -254,7 +252,16 @@ void protocol_send_status(void)
}
}

serial_print_str(MSG_STATUS_MPOS);
if ((g_settings.status_report_mask & 1))
{
serial_print_str(MSG_STATUS_MPOS);
}
else
{
serial_print_str(MSG_STATUS_WPOS);
parser_machine_to_work(axis);
}

serial_print_fltarr(axis, AXIS_COUNT);

#if TOOL_COUNT > 0
Expand Down Expand Up @@ -330,6 +337,14 @@ void protocol_send_status(void)

protocol_send_status_tail();

if ((g_settings.status_report_mask & 2))
{
serial_print_str(MSG_STATUS_BUF);
serial_print_int((uint32_t)planner_get_buffer_freeblocks());
serial_putc(',');
serial_print_int((uint32_t)serial_get_rx_freebytes());
}

serial_putc('>');
procotol_send_newline();
#ifdef ECHO_CMD
Expand Down Expand Up @@ -824,4 +839,4 @@ void protocol_send_cnc_info(void)
protocol_busy = false;
#endif
}
#endif
#endif
Loading

0 comments on commit e599acb

Please sign in to comment.