Skip to content

Commit

Permalink
added grbl state function to cnc
Browse files Browse the repository at this point in the history
  • Loading branch information
Paciente8159 committed Jan 18, 2024
1 parent 568133e commit d0e65da
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 92 deletions.
56 changes: 56 additions & 0 deletions uCNC/src/cnc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1052,3 +1052,59 @@ void cnc_run_startup_blocks(void)
// reset streams
serial_stream_change(NULL);
}

uint8_t cnc_get_state(void)
{
// state check must obay this order
if (cnc_has_alarm())
{
return GRBL_STATE_ALARM;
}
else if (mc_get_checkmode())
{
return GRBL_STATE_CHECKMODE;
}
else
{
uint8_t state = cnc_get_exec_state(EXEC_ALLACTIVE);

if (state & (EXEC_UNHOMED | EXEC_LIMITS))
{
return (!cnc_get_exec_state(EXEC_HOMING)) ? GRBL_STATE_ALARM : GRBL_STATE_HOMING;
}

#if ASSERT_PIN(SAFETY_DOOR)
if (state & EXEC_DOOR)
{
if (CHECKFLAG(io_get_controls(), SAFETY_DOOR_MASK))
{
return (cnc_get_exec_state(EXEC_RUN)) ? GRBL_STATE_DOOR_2 : GRBL_STATE_DOOR_1;
}

return (cnc_get_exec_state(EXEC_RUN)) ? GRBL_STATE_DOOR_3 : GRBL_STATE_DOOR_0;
}
#endif

if (state & EXEC_HOMING)
{
return GRBL_STATE_HOMING;
}

if (state & EXEC_HOLD)
{
return (cnc_get_exec_state(EXEC_RUN)) ? GRBL_STATE_HOLD_1 : GRBL_STATE_HOLD_0;
}

if (state & EXEC_JOG)
{
return GRBL_STATE_JOG;
}

if (state & EXEC_RUN)
{
return GRBL_STATE_RUN;
}
}

return GRBL_STATE_IDLE;
}
40 changes: 29 additions & 11 deletions uCNC/src/cnc.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,23 @@ extern "C"

/**
* Flags and state changes
*
*
* EXEC_KILL
* Set by cnc_alarm.
* Cleared by reset or unlock depending on the the alarm priority. Cannot be cleared if ESTOP is pressed.
*
* Cleared by reset or unlock depending on the the alarm priority. Cannot be cleared if ESTOP is pressed.
*
* EXEC_LIMITS
* Set when at a transition of a limit switch from inactive to the active state.
* Set when at a transition of a limit switch from inactive to the active state.
* Cleared by reset or unlock. Not affected by the limit switch state.
*
*
* EXEC_UNHOMED
* Set when the interpolator is abruptly stopped causing the position to be lost.
* Cleared by homing or unlock.
*
*
* EXEC_DOOR
* Set with when the safety door pin is active or the safety door command is called.
* Cleared by cycle resume, unlock or reset. If the door is opened it will remain active
*
*
*/
// current cnc states (multiple can be active/overlapped at the same time)
#define EXEC_IDLE 0 // All flags cleared
Expand All @@ -87,6 +87,23 @@ extern "C"
#define EXEC_GCODE_LOCKED (EXEC_ALARM | EXEC_DOOR | EXEC_HOMING | EXEC_JOG) // Gcode is locked by an alarm or any special motion state
#define EXEC_ALLACTIVE 255 // All states

// this is a new list of states that can be used more easilly to get the current "Grbl" state
#define GRBL_STATE_CODE(C, M) ((C << 3) | M)
#define GRBL_STATE_CODE_BASE(C) (C & ~0x07)
#define GRBL_STATE_CODE_MANTISSA(C) (C & 0x07)
#define GRBL_STATE_ALARM GRBL_STATE_CODE(7, 0)
#define GRBL_STATE_CHECKMODE GRBL_STATE_CODE(6, 0)
#define GRBL_STATE_DOOR_0 GRBL_STATE_CODE(5, 0)
#define GRBL_STATE_DOOR_1 GRBL_STATE_CODE(5, 1)
#define GRBL_STATE_DOOR_2 GRBL_STATE_CODE(5, 2)
#define GRBL_STATE_DOOR_3 GRBL_STATE_CODE(5, 3)
#define GRBL_STATE_HOMING GRBL_STATE_CODE(4, 0)
#define GRBL_STATE_HOLD_0 GRBL_STATE_CODE(3, 0)
#define GRBL_STATE_HOLD_1 GRBL_STATE_CODE(3, 1)
#define GRBL_STATE_JOG GRBL_STATE_CODE(2, 0)
#define GRBL_STATE_RUN GRBL_STATE_CODE(1, 0)
#define GRBL_STATE_IDLE GRBL_STATE_CODE(0, 0)

// creates a set of helper masks used to configure the controller
#define ESTOP_MASK 1
#define SAFETY_DOOR_MASK 2
Expand All @@ -98,14 +115,14 @@ extern "C"
/**
* Basic step and dir IO masks
* STEPS DIRS and LIMITS can be combined to form MULTI AXIS/LIMITS combinations
*
*
* Usually (depends on the kinematic) STEP0 is assigned to AXIS X, STEP1 is assigned to AXIS Y, etc..
* But STEP0 can be formed by multiple STEPPERS (for example STEPPER0, STEPPER5, STEPPER6 and STEPPER7)
*
*
* STEP0_MASK can then be formed by a combinations of stepper IO masks like this
*
*
* #define STEP0_MASK (STEPPER0_IO_MASK | STEPPER5_IO_MASK | STEPPER6_IO_MASK | STEPPER7_IO_MASK)
*
*
* For auto-squaring LIMITS should also match this STEPx mask by merging all combined limits to form a multi-switch limit
* **/
#define STEP_UNDEF_IO_MASK 0
Expand Down Expand Up @@ -161,6 +178,7 @@ extern "C"
void cnc_home(void);
void cnc_alarm(int8_t code);
bool cnc_has_alarm(void);
uint8_t cnc_get_state(void);
uint8_t cnc_get_alarm(void);
void cnc_stop(void);
uint8_t cnc_unlock(bool force);
Expand Down
108 changes: 27 additions & 81 deletions uCNC/src/interface/protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,92 +221,38 @@ void protocol_send_status(void)
uint8_t controls = io_get_controls();
uint8_t limits = io_get_limits();
bool probe = io_get_probe();
uint8_t state = cnc_get_exec_state(0xFF);
uint8_t filter = 0x80;
while (!(state & filter) && filter)
{
filter >>= 1;
}

state &= filter;
uint8_t state = cnc_get_state();

serial_putc('<');
if (cnc_has_alarm())

switch (GRBL_STATE_CODE_BASE(state))
{
case GRBL_STATE_ALARM:
protocol_send_string(MSG_STATUS_ALARM);
}
else if (mc_get_checkmode())
{
break;
case GRBL_STATE_CHECKMODE:
protocol_send_string(MSG_STATUS_CHECK);
}
else
{
switch (state)
{
#if ASSERT_PIN(SAFETY_DOOR)
case EXEC_DOOR:
protocol_send_string(MSG_STATUS_DOOR);
serial_putc(':');
if (CHECKFLAG(controls, SAFETY_DOOR_MASK))
{
if (cnc_get_exec_state(EXEC_RUN))
{
serial_putc('2');
}
else
{
serial_putc('1');
}
}
else
{
if (cnc_get_exec_state(EXEC_RUN))
{
serial_putc('3');
}
else
{
serial_putc('0');
}
}
break;
#endif
case EXEC_UNHOMED:
case EXEC_LIMITS:
if (!cnc_get_exec_state(EXEC_HOMING))
{
protocol_send_string(MSG_STATUS_ALARM);
}
else
{
protocol_send_string(MSG_STATUS_HOME);
}
break;
case EXEC_HOLD:
protocol_send_string(MSG_STATUS_HOLD);
serial_putc(':');
if (cnc_get_exec_state(EXEC_RUN))
{
serial_putc('1');
}
else
{
serial_putc('0');
}
break;
case EXEC_HOMING:
protocol_send_string(MSG_STATUS_HOME);
break;
case EXEC_JOG:
protocol_send_string(MSG_STATUS_JOG);
break;
case EXEC_RUN:
protocol_send_string(MSG_STATUS_RUN);
break;
default:
protocol_send_string(MSG_STATUS_IDLE);
break;
}
break;
case GRBL_STATE_DOOR_0:
protocol_send_string(MSG_STATUS_DOOR);
serial_putc('0' + GRBL_STATE_CODE_MANTISSA(state));
break;
case GRBL_STATE_HOMING:
protocol_send_string(MSG_STATUS_HOME);
break;
case GRBL_STATE_HOLD_0:
protocol_send_string(MSG_STATUS_HOLD);
serial_putc('0' + GRBL_STATE_CODE_MANTISSA(state));
break;
case GRBL_STATE_JOG:
protocol_send_string(MSG_STATUS_JOG);
break;
case GRBL_STATE_RUN:
protocol_send_string(MSG_STATUS_RUN);
break;
default:
protocol_send_string(MSG_STATUS_IDLE);
break;
}

if ((g_settings.status_report_mask & 1))
Expand Down

0 comments on commit d0e65da

Please sign in to comment.