From f8c83717795eb809661a1762aa86218abc64f8d5 Mon Sep 17 00:00:00 2001 From: Joseph Coffland Date: Sat, 27 Aug 2016 14:18:30 -0700 Subject: [PATCH] Reintroduced machine cycle state --- src/estop.c | 4 +-- src/homing.c | 7 +++--- src/machine.c | 58 ++++++++++++++++++++++++++++++++++++-------- src/machine.h | 21 +++++++++++++--- src/plan/calibrate.c | 10 ++++---- src/plan/exec.c | 17 +++++-------- src/plan/jog.c | 15 +++++++----- src/probing.c | 5 ++-- src/status.h | 2 +- src/varcb.c | 20 +++------------ src/vars.def | 1 + 11 files changed, 99 insertions(+), 61 deletions(-) diff --git a/src/estop.c b/src/estop.c index 96a416b..e9edd66 100644 --- a/src/estop.c +++ b/src/estop.c @@ -73,7 +73,7 @@ void estop_init() { switch_set_callback(SW_ESTOP, _switch_callback); - if (estop.triggered) mach_set_state(STATE_ESTOP); + if (estop.triggered) mach_set_state(STATE_ESTOPPED); // Fault signal if (estop.triggered) OUTSET_PIN(FAULT_PIN); // High @@ -95,7 +95,7 @@ void estop_trigger(estop_reason_t reason) { mach_spindle_estop(); // Set machine state - mach_set_state(STATE_ESTOP); + mach_set_state(STATE_ESTOPPED); // Set axes not homed mach_set_not_homed(); diff --git a/src/homing.c b/src/homing.c index 70222c0..c1d517d 100644 --- a/src/homing.c +++ b/src/homing.c @@ -177,6 +177,7 @@ static void _homing_finalize_exit() { mach.gm.feed_rate = hm.saved_feed_rate; mach_set_motion_mode(MOTION_MODE_CANCEL_MOTION_MODE); mach_cycle_end(); + mach_set_cycle(CYCLE_MACHINING); } @@ -342,7 +343,7 @@ static void _homing_axis_start(int8_t axis) { bool mach_is_homing() { - return mach_get_state() == STATE_HOMING; + return mach_get_cycle() == CYCLE_HOMING; } @@ -371,7 +372,7 @@ void mach_homing_cycle_start() { hm.axis = -1; // set to retrieve initial axis hm.func = _homing_axis_start; // bind initial processing function - mach_set_state(STATE_HOMING); + mach_set_cycle(CYCLE_HOMING); mach.homing_state = HOMING_NOT_HOMED; } @@ -384,6 +385,6 @@ void mach_homing_cycle_start_no_set() { /// Main loop callback for running the homing cycle void mach_homing_callback() { - if (mach_get_state() != STATE_HOMING || mach_get_runtime_busy()) return; + if (mach_get_cycle() != CYCLE_HOMING || mach_get_runtime_busy()) return; hm.func(hm.axis); // execute the current homing move } diff --git a/src/machine.c b/src/machine.c index b637ab9..8b583b9 100644 --- a/src/machine.c +++ b/src/machine.c @@ -183,6 +183,7 @@ machine_t mach = { }, ._state = STATE_IDLE, + ._cycle = CYCLE_MACHINING, // State .gm = {.motion_mode = MOTION_MODE_CANCEL_MOTION_MODE}, @@ -203,14 +204,29 @@ static void _exec_absolute_origin(float *value, float *flag); uint32_t mach_get_line() {return mach.gm.line;} -bool mach_is_running() { - switch (mach_get_state()) { - case STATE_RUNNING: - case STATE_HOMING: - case STATE_PROBING: - return true; - default: return false; +PGM_P mach_get_state_pgmstr(machState_t state) { + switch (state) { + case STATE_IDLE: return PSTR("idle"); + case STATE_ESTOPPED: return PSTR("estopped"); + case STATE_RUNNING: return PSTR("running"); + case STATE_STOPPING: return PSTR("stopping"); + case STATE_HOLDING: return PSTR("holding"); } + + return PSTR("invalid"); +} + + +PGM_P mach_get_cycle_pgmstr(machCycle_t cycle) { + switch (cycle) { + case CYCLE_MACHINING: return PSTR("machining"); + case CYCLE_HOMING: return PSTR("homing"); + case CYCLE_PROBING: return PSTR("probing"); + case CYCLE_CALIBRATING: return PSTR("calibrating"); + case CYCLE_JOGGING: return PSTR("jogging"); + } + + return PSTR("invalid"); } @@ -231,12 +247,35 @@ float mach_get_feed_rate() {return mach.gm.feed_rate;} void mach_set_state(machState_t state) { if (mach._state == state) return; // No change - if (mach._state == STATE_ESTOP) return; // Can't leave EStop state + if (mach._state == STATE_ESTOPPED) return; // Can't leave EStop state mach._state = state; report_request(); } +void mach_set_cycle(machCycle_t cycle) { + if (mach._cycle == cycle) return; // No change + + if (mach._state != STATE_IDLE) { + STATUS_ERROR(STAT_INTERNAL_ERROR, "Cannot transition to %S while %S", + mach_get_cycle_pgmstr(cycle), + mach_get_state_pgmstr(mach._state)); + return; + } + + if (mach._cycle != CYCLE_MACHINING && cycle != CYCLE_MACHINING) { + STATUS_ERROR(STAT_INTERNAL_ERROR, + "Cannot transition to cycle %S while in %S", + mach_get_cycle_pgmstr(cycle), + mach_get_cycle_pgmstr(mach._cycle)); + return; + } + + mach._cycle = cycle; + report_request(); +} + + void mach_set_motion_mode(machMotionMode_t motion_mode) { mach.gm.motion_mode = motion_mode; } @@ -1126,7 +1165,7 @@ void mach_feedhold_callback() { if (mach.feedhold_requested) { mach.feedhold_requested = false; - if (mach_is_running()) { + if (mach_get_state() == STATE_RUNNING) { mach_set_state(STATE_STOPPING); mach.hold_state = FEEDHOLD_SYNC; // invokes hold from aline execution } @@ -1228,7 +1267,6 @@ stat_t mach_queue_flush() { /// Do a cycle start right now void mach_cycle_start() { - // don't (re)start homing, probe or other canned cycles if (mach_get_state() == STATE_IDLE) mach_set_state(STATE_RUNNING); } diff --git a/src/machine.h b/src/machine.h index a9b2502..f9c2a0f 100644 --- a/src/machine.h +++ b/src/machine.h @@ -32,6 +32,8 @@ #include "config.h" #include "status.h" +#include + #include #include @@ -44,15 +46,22 @@ typedef enum { STATE_IDLE, - STATE_ESTOP, + STATE_ESTOPPED, STATE_RUNNING, - STATE_HOMING, - STATE_PROBING, STATE_STOPPING, STATE_HOLDING, } machState_t; +typedef enum { + CYCLE_MACHINING, + CYCLE_HOMING, + CYCLE_PROBING, + CYCLE_CALIBRATING, + CYCLE_JOGGING, +} machCycle_t; + + typedef enum { // feedhold_state machine FEEDHOLD_OFF, // no feedhold in effect FEEDHOLD_SYNC, // start hold - sync to latest aline segment @@ -348,6 +357,7 @@ typedef struct { // struct to manage mach globals and cycles AxisConfig_t a[AXES]; machState_t _state; + machCycle_t _cycle; machFeedholdState_t hold_state; // hold: feedhold sub-state machine machHomingState_t homing_state; // home: homing cycle sub-state machine bool homed[AXES]; // individual axis homing flags @@ -373,7 +383,7 @@ extern machine_t mach; // machine controller singleton // Model state getters and setters uint32_t mach_get_line(); inline machState_t mach_get_state() {return mach._state;} -bool mach_is_running(); +inline machCycle_t mach_get_cycle() {return mach._cycle;} machFeedholdState_t mach_get_hold_state(); machHomingState_t mach_get_homing_state(); machMotionMode_t mach_get_motion_mode(); @@ -389,6 +399,9 @@ bool mach_get_runtime_busy(); float mach_get_feed_rate(); void mach_set_state(machState_t state); +void mach_set_cycle(machCycle_t cycle); +PGM_P mach_get_state_pgmstr(machState_t state); +PGM_P mach_get_cycle_pgmstr(machCycle_t cycle); void mach_set_motion_mode(machMotionMode_t motion_mode); void mach_set_spindle_mode(machSpindleMode_t spindle_mode); void mach_set_spindle_speed_parameter(float speed); diff --git a/src/plan/calibrate.c b/src/plan/calibrate.c index b1833c8..289e8ba 100644 --- a/src/plan/calibrate.c +++ b/src/plan/calibrate.c @@ -59,7 +59,6 @@ enum { typedef struct { - bool busy; bool stall_valid; bool stalled; bool reverse; @@ -112,7 +111,7 @@ static stat_t _exec_calibrate(mpBuf_t *bf) { tmc2660_set_stallguard_threshold(cal.motor, 63); mp_free_run_buffer(); // Release buffer - cal.busy = false; + mach_set_cycle(CYCLE_MACHINING); return STAT_OK; } else { @@ -145,7 +144,7 @@ static stat_t _exec_calibrate(mpBuf_t *bf) { } -bool calibrate_busy() {return cal.busy;} +bool calibrate_busy() {return mach_get_cycle() == CYCLE_CALIBRATING;} void calibrate_set_stallguard(int motor, uint16_t sg) { @@ -164,7 +163,8 @@ void calibrate_set_stallguard(int motor, uint16_t sg) { uint8_t command_calibrate(int argc, char *argv[]) { - if (cal.busy) return 0; + if (mach_get_cycle() != CYCLE_MACHINING || mach_get_state() != STATE_IDLE) + return 0; mpBuf_t *bf = mp_get_write_buffer(); if (!bf) { @@ -174,7 +174,7 @@ uint8_t command_calibrate(int argc, char *argv[]) { // Start memset(&cal, 0, sizeof(cal)); - cal.busy = true; + mach_set_cycle(CYCLE_CALIBRATING); cal.motor = 1; bf->bf_func = _exec_calibrate; // register callback diff --git a/src/plan/exec.c b/src/plan/exec.c index 4460580..e87bb5d 100644 --- a/src/plan/exec.c +++ b/src/plan/exec.c @@ -796,20 +796,15 @@ stat_t mp_exec_aline(mpBuf_t *bf) { } -/// Dequeues buffer, executes move continuations and manages run buffers. +/// Dequeues buffer, executes move callback and enters RUNNING state stat_t mp_exec_move() { - mpBuf_t *bf = mp_get_run_buffer(); - - if (!bf) return STAT_NOOP; // nothing's running if (estop_triggered()) return STAT_MACHINE_ALARMED; - // Manage state transitions - // Cycle auto-start for lines only - if (bf->move_type == MOVE_TYPE_ALINE && mach_get_state() == STATE_IDLE) - mach_set_state(STATE_RUNNING); + mpBuf_t *bf = mp_get_run_buffer(); + if (!bf) return STAT_NOOP; // nothing running + if (!bf->bf_func) return CM_ALARM(STAT_INTERNAL_ERROR); - if (!bf->bf_func) - return CM_ALARM(STAT_INTERNAL_ERROR); // never supposed to get here + if (mach_get_state() == STATE_IDLE) mach_set_state(STATE_RUNNING); - return bf->bf_func(bf); // run the move callback on the planner buffer + return bf->bf_func(bf); // move callback } diff --git a/src/plan/jog.c b/src/plan/jog.c index 265402f..ca97884 100644 --- a/src/plan/jog.c +++ b/src/plan/jog.c @@ -43,7 +43,6 @@ typedef struct { - bool running; bool writing; float next_velocity[AXES]; float target_velocity[AXES]; @@ -96,7 +95,7 @@ static stat_t _exec_jog(mpBuf_t *bf) { // Release buffer mp_free_run_buffer(); - jr.running = false; + mach_set_cycle(CYCLE_MACHINING); return STAT_NOOP; } @@ -113,10 +112,14 @@ static stat_t _exec_jog(mpBuf_t *bf) { } -bool mp_jog_busy() {return jr.running;} +bool mp_jog_busy() {return mach_get_cycle() == CYCLE_JOGGING;} uint8_t command_jog(int argc, char *argv[]) { + if (!mp_jog_busy() && + (mach_get_state() != STATE_IDLE || mach_get_cycle() != CYCLE_MACHINING)) + return STAT_NOOP; + float velocity[AXES]; for (int axis = 0; axis < AXES; axis++) @@ -124,14 +127,14 @@ uint8_t command_jog(int argc, char *argv[]) { else velocity[axis] = 0; // Reset - if (!jr.running) memset(&jr, 0, sizeof(jr)); + if (mp_jog_busy()) memset(&jr, 0, sizeof(jr)); jr.writing = true; for (int axis = 0; axis < AXES; axis++) jr.next_velocity[axis] = velocity[axis]; jr.writing = false; - if (!jr.running) { + if (!mp_jog_busy()) { // Should always be at least one free buffer mpBuf_t *bf = mp_get_write_buffer(); if (!bf) { @@ -140,7 +143,7 @@ uint8_t command_jog(int argc, char *argv[]) { } // Start - jr.running = true; + mach_set_cycle(CYCLE_JOGGING); bf->bf_func = _exec_jog; // register callback mp_commit_write_buffer(-1, MOVE_TYPE_COMMAND); } diff --git a/src/probing.c b/src/probing.c index a7dd584..e79ee52 100644 --- a/src/probing.c +++ b/src/probing.c @@ -89,6 +89,7 @@ static void _probe_restore_settings() { // update the model with actual position mach_set_motion_mode(MOTION_MODE_CANCEL_MOTION_MODE); mach_cycle_end(); + mach_set_cycle(CYCLE_MACHINING); } @@ -140,7 +141,7 @@ static void _probing_init() { // it is an error for the limit or homing switches to fire, or for some other // configuration error. mach.probe_state = PROBE_FAILED; - mach_set_state(STATE_PROBING); + mach_set_cycle(CYCLE_PROBING); // initialize the axes - save the jerk settings & switch to the jerk_homing // settings @@ -175,7 +176,7 @@ static void _probing_init() { bool mach_is_probing() { - return mach_get_state() == STATE_PROBING || mach.probe_state == PROBE_WAITING; + return mach_get_cycle() == CYCLE_PROBING || mach.probe_state == PROBE_WAITING; } diff --git a/src/status.h b/src/status.h index e2e7df9..24f222b 100644 --- a/src/status.h +++ b/src/status.h @@ -77,5 +77,5 @@ void status_help(); #define STATUS_WARNING(MSG, ...) \ STATUS_MESSAGE(STAT_LEVEL_WARNING, STAT_OK, MSG, ##__VA_ARGS__) -#define STATUS_ERROR(MSG, CODE, ...) \ +#define STATUS_ERROR(CODE, MSG, ...) \ STATUS_MESSAGE(STAT_LEVEL_ERROR, CODE, MSG, ##__VA_ARGS__) diff --git a/src/varcb.c b/src/varcb.c index 4a559b6..488d091 100644 --- a/src/varcb.c +++ b/src/varcb.c @@ -27,11 +27,10 @@ \******************************************************************************/ #include "usart.h" +#include "machine.h" #include "plan/planner.h" #include "plan/buffer.h" -#include - float get_position(int index) {return mp_get_runtime_absolute_position(index);} float get_velocity() {return mp_get_runtime_velocity();} @@ -39,18 +38,5 @@ bool get_echo() {return usart_is_set(USART_ECHO);} void set_echo(bool value) {return usart_set(USART_ECHO, value);} uint16_t get_queue() {return mp_get_planner_buffer_room();} int32_t get_line() {return mr.ms.line;} - - -PGM_P get_state() { - switch (mach_get_state()) { - case STATE_IDLE: return PSTR("idle"); - case STATE_ESTOP: return PSTR("estop"); - case STATE_RUNNING: return PSTR("running"); - case STATE_HOMING: return PSTR("homing"); - case STATE_PROBING: return PSTR("probing"); - case STATE_STOPPING: return PSTR("stopping"); - case STATE_HOLDING: return PSTR("holding"); - } - - return PSTR("invalid"); -} +PGM_P get_state() {return mach_get_state_pgmstr(mach_get_state());} +PGM_P get_cycle() {return mach_get_cycle_pgmstr(mach_get_cycle());} diff --git a/src/vars.def b/src/vars.def index cd9b0e2..e65bf14 100644 --- a/src/vars.def +++ b/src/vars.def @@ -100,3 +100,4 @@ VAR(estop_reason, "er", pstring, 0, 0, 0, "Emergency stop reason") VAR(line, "ln", int32_t, 0, 0, 0, "Last GCode line executed") VAR(queue, "q", uint16_t, 0, 0, 0, "Space in planner queue") VAR(state, "s", pstring, 0, 0, 0, "Machine state") +VAR(cycle, "c", pstring, 0, 0, 0, "Current machine cycle") -- 2.27.0