diff --git a/src/emc/usr_intf/emcrsh.cc b/src/emc/usr_intf/emcrsh.cc index 409c267f0b5..a21b3bc26e5 100644 --- a/src/emc/usr_intf/emcrsh.cc +++ b/src/emc/usr_intf/emcrsh.cc @@ -68,6 +68,9 @@ constexpr auto JOINT_WAIT_HOMED_SLEEP = 100ms; // Sleep time between checks // up and reflect a requested state change in the status #define WAIT_TASK_STATE_CHANGE_MAX 25 +// Maximum wait time for task to receive a command +#define WAIT_TASK_RECEIVED_TIMEOUT 10.0 + typedef enum { cmdUnknown, cmdHello, cmdSet, cmdGet, cmdQuit, cmdShutdown, cmdHelp } cmdType; typedef enum { @@ -908,6 +911,23 @@ static cmdResponseType doWaitHeartbeat(connectionRecType &ctx, int periods, doub return rtError; } +// Task may be busy for a while before it gets to a command (the interpreter +// syncs the parameter file to disk on a state change) and its status, +// heartbeat included, stands still until then. emcTimeout is 0 (infinite) +// in linuxcncrsh, so bound the wait. +static cmdResponseType waitTaskReceived(connectionRecType &ctx, const std::string &cmd) +{ + double saved = emcTimeout; + emcTimeout = WAIT_TASK_RECEIVED_TIMEOUT; + int rv = emcCommandWaitReceived(); + emcTimeout = saved; + if (rv) { + errornl(ctx, fmt::format("Task did not receive '{}' within {:.1f} seconds", cmd, WAIT_TASK_RECEIVED_TIMEOUT)); + return rtError; + } + return rtOk; +} + static cmdResponseType setJointWaitHomed(connectionRecType &ctx) { // SET JOINT_WAIT_HOMED [joint] [timeout] @@ -1009,6 +1029,8 @@ static cmdResponseType setMachine(connectionRecType &ctx) return rtError; break; } + if (rtOk != waitTaskReceived(ctx, fmt::format("SET MACHINE {}", onOff(machon)))) + return rtError; // Setting machine on/off takes a moment // We need to wait for the motion controller to catch up // and set/clear the motion enable flag internally. @@ -1052,6 +1074,8 @@ static cmdResponseType setEStop(connectionRecType &ctx) break; } + if (rtOk != waitTaskReceived(ctx, fmt::format("SET ESTOP {}", onOff(estopon)))) + return rtError; // Setting/clearing ESTOP takes a moment for(unsigned toc = 0; toc < WAIT_TASK_STATE_CHANGE_MAX; toc++) { // Wait for the motion control to run @@ -1445,6 +1469,8 @@ static cmdResponseType setJointHome(connectionRecType &ctx) return rtError; if (joint >= 0) { + if (rtOk != waitTaskReceived(ctx, fmt::format("SET JOINT_HOME {}", joint))) + return rtError; // The motion controller needs a servo-thread run to raise the // homing flag. Wait for it so that a following command, like // 'SET JOINT_WAIT_HOMED', can rely on the state being visible.