diff --git a/src/emc/nml_intf/interpl.hh b/src/emc/nml_intf/interpl.hh index a2a211d0410..b8f9112da6e 100644 --- a/src/emc/nml_intf/interpl.hh +++ b/src/emc/nml_intf/interpl.hh @@ -64,6 +64,9 @@ class InterpList void clear(); + /// Drop messages from the back until at most n remain. + void truncate(int n); + /// Number the next append() gives its message; numbers only grow. uint64_t next_number() const { return next_append; } @@ -175,6 +178,20 @@ void InterpList::append(InterpList &&other) other.linked_list.clear(); } +template +void InterpList::truncate(int n) +{ + if (n < 0) + n = 0; + if (static_cast(n) >= linked_list.size()) + return; + if (tracing()) { + printf("InterpList(%p)::truncate(%d): discarding %zu items\n", + static_cast(this), n, linked_list.size() - n); + } + linked_list.erase(linked_list.begin() + n, linked_list.end()); +} + template void InterpList::print() const { diff --git a/src/emc/task/emccanon.cc b/src/emc/task/emccanon.cc index ec11bca6d00..93c92f12b9e 100644 --- a/src/emc/task/emccanon.cc +++ b/src/emc/task/emccanon.cc @@ -1364,13 +1364,23 @@ struct pt { static std::vector chained_points; +// Of the program line being executed, for emcCanonLineFailed(): +static size_t line_held = 0; // points held from the lines before it +static int line_queued = 0; // length of interp_list with their moves + static void drop_segments(void) { chained_points.clear(); + line_held = 0; } static void flush_segments(void) { if(chained_points.empty()) return; + // the line being executed flushes the moves from before it, and has + // queued nothing itself + const bool flushes_lines_before = line_held == chained_points.size() + && line_queued == interp_list.len(); + struct pt &pos = chained_points.back(); double x = pos.x, y = pos.y, z = pos.z; @@ -1436,6 +1446,9 @@ static void flush_segments(void) { } canonUpdateEndPoint(x, y, z, a, b, c, u, v, w); + if (flushes_lines_before) { + line_queued = interp_list.len(); + } drop_segments(); } @@ -1516,6 +1529,22 @@ void FINISH() { flush_segments(); } +void emcCanonLineStart() { + line_held = chained_points.size(); + line_queued = interp_list.len(); +} + +void emcCanonLineFailed() { + interp_list.truncate(line_queued); + chained_points.erase(chained_points.begin() + std::min(line_held, chained_points.size()), + chained_points.end()); + flush_segments(); +} + +void emcCanonDropHeld() { + drop_segments(); +} + void ON_RESET() { drop_segments(); } diff --git a/src/emc/task/emctask.cc b/src/emc/task/emctask.cc index e6ba142600c..8650db7f4d2 100644 --- a/src/emc/task/emctask.cc +++ b/src/emc/task/emctask.cc @@ -801,8 +801,11 @@ int emcTaskUpdate(EMC_TASK_STAT * stat) return 0; } +bool interp_error_pending = false; + int emcAbortCleanup(EMC_ABORT reason, const char *message) { + interp_error_pending = false; abortCleanupFirst = interp_list.next_number(); int status = interp.on_abort((int)reason,message); abortCleanupEnd = interp_list.next_number(); diff --git a/src/emc/task/emctaskmain.cc b/src/emc/task/emctaskmain.cc index 8efaa87c61d..9a31e8cebaa 100644 --- a/src/emc/task/emctaskmain.cc +++ b/src/emc/task/emctaskmain.cc @@ -577,8 +577,21 @@ void readahead_reading(void) This if() actually evaluates to if (readRetval != INTERP_OK)... *** Need to look at all calls to things that return INTERP_xxx values! *** MGS */ - if (readRetval > INTERP_MIN_ERROR - || readRetval == INTERP_ENDFILE + if (readRetval > INTERP_MIN_ERROR) { + // Bad line: read no further, let the good lines + // already queued run, then abort (readahead_waiting). + // So do the moves canon holds back. In a run from + // line all of them are of skipped lines: drop them. + if (programStartLine != 0) { + emcCanonDropHeld(); + interp_list.clear(); + } else { + FINISH(); + } + interp_error_pending = true; + emcStatus->task.interpState = + EMC_TASK_INTERP::WAITING; + } else if (readRetval == INTERP_ENDFILE || readRetval == INTERP_EXIT || readRetval == INTERP_EXECUTE_FINISH) { /* emcTaskPlanRead retval != INTERP_OK @@ -597,17 +610,20 @@ void readahead_reading(void) emcTaskPlanCommand((char *) &emcStatus->task. command); // and execute it + emcCanonLineStart(); execRetval = emcTaskPlanExecute(NULL); // line number may need update after // returns from subprograms in external // files emcStatus->task.readLine = emcTaskPlanLine(); if (execRetval > INTERP_MIN_ERROR) { + // Same as a read error: drop whatever the bad + // line itself queued, keep the good lines before + // it, and abort once they have run. + emcCanonLineFailed(); + interp_error_pending = true; emcStatus->task.interpState = EMC_TASK_INTERP::WAITING; - interp_list.clear(); - emcAbortCleanup(EMC_ABORT::INTERPRETER_ERROR, - "interpreter error"); } else if (execRetval == -1 || execRetval == INTERP_EXIT ) { emcStatus->task.interpState = @@ -780,6 +796,26 @@ void readahead_waiting(void) emcStatus->io.status == RCS_STATUS::DONE) // finished { + if (interp_error_pending) { + // Not traj.queue alone: a copy read before the controller + // ran after the last move was sent still shows an empty + // queue. And a dwell or an input wait shows in execState. + if (emcStatus->motion.status != RCS_STATUS::DONE || + emcStatus->task.execState != EMC_TASK_EXEC::DONE) { + return; + } + // The lines before an interpreter error have run. Stop the + // program the way an abort does (spindles, IO), so it does not + // sit at the bad line with the spindle still on. + emcTaskAbort(); + emcIoAbort(EMC_ABORT::INTERPRETER_ERROR); + for (int s = 0; s < emcStatus->motion.traj.spindles; s++) + emcSpindleAbort(s); + mdi_execute_abort(); + emcAbortCleanup(EMC_ABORT::INTERPRETER_ERROR, + "interpreter error"); + return; + } int was_open = taskplanopen; if (was_open) { emcTaskPlanClose(); diff --git a/src/emc/task/task.hh b/src/emc/task/task.hh index c1fd9496c1e..bcdbe983d7c 100644 --- a/src/emc/task/task.hh +++ b/src/emc/task/task.hh @@ -21,6 +21,16 @@ extern std::unique_ptr emcTaskCommand; extern int stepping; +// An interpreter error is waiting for the good lines queued before it to +// run out; task aborts once they have. Any abort clears it. +extern bool interp_error_pending; +// Canon holds feed moves back to join them. Task says when a program line +// starts to execute, and if it fails, that canon is to drop what the line +// queued or held, and queue the moves held from the lines before it. +// emcCanonDropHeld() is for a run from line: the lines are skipped. +extern void emcCanonLineStart(); +extern void emcCanonLineFailed(); +extern void emcCanonDropHeld(); extern int steppingWait; extern int emcTaskQueueCommand(std::unique_ptr &&cmd); extern int emcTaskOnce(const char *inifile, EMC_IO_STAT &emcioStatus); diff --git a/tests/interp-error-abort/.gitignore b/tests/interp-error-abort/.gitignore new file mode 100644 index 00000000000..849937d0988 --- /dev/null +++ b/tests/interp-error-abort/.gitignore @@ -0,0 +1,2 @@ +isub.ngc +test.ngc diff --git a/tests/interp-error-abort/README b/tests/interp-error-abort/README new file mode 100644 index 00000000000..a1b0dac911d --- /dev/null +++ b/tests/interp-error-abort/README @@ -0,0 +1,15 @@ +The lines before an interpreter error run, then task aborts (#2978). + +Each program ends in a line that fails in the interpreter, as it is read +or as it is executed. The test fails if the machine does not reach the +last point before that line, if the spindle stays on, or if a dwell +before it is cut short. + +Canon holds feed moves back to join them, so the last one is not in +interp_list yet when the error is found. In a run from line those held +back belong to skipped lines, and must not run. + +This uses the real motion controller, with motion-controller before +motion-command-handler and a 50 ms period as in +tests/stale-motion-status/late-controller, so that a status read in +between still shows an empty queue. diff --git a/tests/interp-error-abort/checkresult b/tests/interp-error-abort/checkresult new file mode 100755 index 00000000000..acfd40f1b32 --- /dev/null +++ b/tests/interp-error-abort/checkresult @@ -0,0 +1,3 @@ +#!/bin/sh +# Test passes if test-ui.py returns successfully +exit 0 diff --git a/tests/interp-error-abort/late.hal b/tests/interp-error-abort/late.hal new file mode 100644 index 00000000000..104b629402f --- /dev/null +++ b/tests/interp-error-abort/late.hal @@ -0,0 +1,5 @@ +# Run the controller before the command handler, so that motion +# acknowledges a command a whole servo period before the controller +# acts on it. +delf motion-command-handler servo-thread +addf motion-command-handler servo-thread 2 diff --git a/tests/interp-error-abort/simpockets.tbl b/tests/interp-error-abort/simpockets.tbl new file mode 100644 index 00000000000..0bdf35afecc --- /dev/null +++ b/tests/interp-error-abort/simpockets.tbl @@ -0,0 +1 @@ +T1 P1 Z0.0 D0.0 ; diff --git a/tests/interp-error-abort/test-ui.py b/tests/interp-error-abort/test-ui.py new file mode 100755 index 00000000000..aefc166c838 --- /dev/null +++ b/tests/interp-error-abort/test-ui.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 + +import linuxcnc +import sys +import time + +c = linuxcnc.command() +s = linuxcnc.stat() +e = linuxcnc.error_channel() + +failures = [] + + +def finish(): + for msg in failures: + sys.stderr.write("error: " + msg + chr(10)) + sys.exit(1 if failures else 0) + + +def wait(what, done, timeout=60): + start = time.time() + while True: + s.poll() + if done(): + return + if time.time() - start > timeout: + failures.append("timeout waiting for " + what) + finish() + time.sleep(0.01) + + +def drain(): + errors = [] + while True: + err = e.poll() + if not err: + return errors + if "Unexpected realtime delay" not in err[1]: + errors.append(err[1]) + + +def start(lines, from_line=0): + """home the axes to X0 Y0, then run a program of these lines""" + c.mode(linuxcnc.MODE_MDI) + c.wait_complete(60) + c.mdi("G20 G90 G64 G80 G0 X0 Y0 Z0") + c.wait_complete(60) + wait("X0", lambda: abs(s.position[0]) < 1e-6 and abs(s.position[1]) < 1e-6) + drain() + with open("test.ngc", "w") as f: + f.write("\n".join(lines) + "\n") + c.mode(linuxcnc.MODE_AUTO) + c.wait_complete(60) + c.program_open("test.ngc") + c.auto(linuxcnc.AUTO_RUN, from_line) + + +def check(name, lines, x, y=0, min_time=0, from_line=0): + """run the lines: task must report an error and end with the interpreter + idle, the spindle off and the machine at x, y""" + start(lines, from_line) + errors = [] + + def reported(): + errors.extend(drain()) + return errors + wait("the error of " + name, reported) + reported_at = time.time() + wait("the end of " + name, + lambda: s.interp_state == linuxcnc.INTERP_IDLE) + took = time.time() - reported_at + if abs(s.position[0] - x) > 1e-6 or abs(s.position[1] - y) > 1e-6: + failures.append("%s: ended at X%.4f Y%.4f, before X%g Y%g" % ( + name, s.position[0], s.position[1], x, y)) + time.sleep(0.5) + s.poll() + if abs(s.position[0] - x) > 1e-6 or abs(s.position[1] - y) > 1e-6: + failures.append("%s: at X%.4f Y%.4f, not X%g Y%g" % ( + name, s.position[0], s.position[1], x, y)) + if s.spindle[0]["enabled"]: + failures.append("%s: the spindle is on" % name) + c.abort() + c.wait_complete(60) + if took < min_time: + failures.append("%s: ended %.2f s after the error, not %g s" % ( + name, took, min_time)) + + +c.state(linuxcnc.STATE_ESTOP_RESET) +c.wait_complete(60) +c.state(linuxcnc.STATE_ON) +c.wait_complete(60) +for joint in range(3): + c.home(joint) + wait("joint %d to home" % joint, lambda: s.homed[joint] + and not any(j["homing"] for j in s.joint[:3])) + +read_error = "G4" +exec_error = "G83 X0 Y0 Z-1 R0 Q0" +arc_error = "G2 X9 Y0 I0.1 J0" +for kind, bad in (("read", read_error), ("exec", exec_error), + # its F word flushes the move canon holds back + ("exec, F word", exec_error + " F20"), + # nothing before the error flushes it + ("exec, arc", arc_error)): + # slow moves: an abort stops each joint first, a period for each, and a + # fast move is over before the abort gets to it + for name, good, x, y in ( + ("G0", ["G0 X1"], 1, 0), + ("G1", ["G1 X1 F30"], 1, 0), + # in line: canon holds both back as one move + ("joined G1", ["G1 X0.5 F30", "G1 X1"], 1, 0), + ("corner G1", ["G1 X1 F30", "G1 Y1"], 1, 1), + ("dwell, G1", ["G4 P0.2", "G1 X1 F30"], 1, 0)): + check(kind + " error after " + name, + ["G20 G90 G64 P0.01", "M3 S100"] + good + [bad, "G0 X3", "M2"], + x, y) + # the dwell runs to its end + check(kind + " error after a dwell", + ["G20 G90", "M3 S100", "G0 X1", "G4 P2", bad, "G0 X3", "M2"], + 1, 0, min_time=1.5) + check(kind + " error, no move before it", + ["G20 G90", "M3 S100", bad, "G0 X3", "M2"], 0) + + if kind not in ("read", "exec"): + continue + # Run from line 7: lines 1 to 6 are skipped, so the G1 canon holds back + # when the error is found must not run. + check(kind + " error in the lines skipped", + ["G20 G90 G64 P0.01", "F300", "G1 X1", bad, "G4 P0", "G4 P0", + "G0 X3", "M2"], 0, from_line=7) + # the same in a subroutine, where task does not clear what is queued + check(kind + " error in a subroutine skipped", + ["o100 sub", bad, "o100 endsub", "G20 G90 G64 P0.01", "F300", + "G1 X1", "o100 call", "G4 P0", "G4 P0", "G0 X3", "M2"], + 0, from_line=10) + check(kind + " error after a move in a subroutine skipped", + ["o100 sub", "G1 X1", bad, "o100 endsub", "G20 G90 G64 P0.01", + "F300", "o100 call", "G4 P0", "G4 P0", "G0 X3", "M2"], + 0, from_line=10) + check(kind + " error after a G0 in a subroutine skipped", + ["o100 sub", "G0 X1", bad, "o100 endsub", "G20 G90 G64 P0.01", + "F300", "o100 call", "G4 P0", "G4 P0", "G0 X3", "M2"], + 0, from_line=10) + + # in a file of its own, the line numbers are past the line to run from + for name, move in (("G0", "G0 X1"), ("G1", "G1 X1 F300")): + with open("isub.ngc", "w") as f: + f.write("\n".join(["o sub"] + ["G4 P0"] * 20 + + [move, bad, "o endsub", "M2"]) + "\n") + check("%s error after %s in a file skipped" % (kind, name), + ["G20 G90 G64 P0.01", "o call", "G4 P0", "G4 P0", + "G0 X3", "M2"], 0, from_line=5) + +# The pending error goes with an abort: the next program runs to its end. +start(["G20 G90", "G1 X1 F10", read_error, "M2"]) +wait("the move", lambda: s.position[0] > 0.05) +c.abort() +c.wait_complete(60) +start(["G20 G90", "G1 X2 F300", "M2"]) +time.sleep(0.5) +wait("the program", lambda: s.interp_state == linuxcnc.INTERP_IDLE) +if abs(s.position[0] - 2) > 1e-6: + failures.append("after an abort: at X%.4f, not X2" % s.position[0]) +failures += ["after an abort: " + err for err in drain()] + +finish() diff --git a/tests/interp-error-abort/test.ini b/tests/interp-error-abort/test.ini new file mode 100644 index 00000000000..37d0a53f161 --- /dev/null +++ b/tests/interp-error-abort/test.ini @@ -0,0 +1,86 @@ +[EMC] +VERSION = 1.1 +DEBUG = 0x0 + +[DISPLAY] +DISPLAY = ./test-ui.py + +[TASK] +TASK = milltask +CYCLE_TIME = 0.001 + +[RS274NGC] +PARAMETER_FILE = sim.var + +[EMCMOT] +EMCMOT = motmod +COMM_TIMEOUT = 4.0 +BASE_PERIOD = 0 +SERVO_PERIOD = 50000000 + +[EMCIO] +TOOL_TABLE = simpockets.tbl + +[HAL] +HALFILE = LIB:core_sim.hal +HALFILE = late.hal + +[TRAJ] +NO_FORCE_HOMING = 1 +COORDINATES = X Y Z +LINEAR_UNITS = inch +ANGULAR_UNITS = degree +DEFAULT_LINEAR_VELOCITY = 10 +MAX_LINEAR_VELOCITY = 100 + +[KINS] +KINEMATICS = trivkins +JOINTS = 3 + +[AXIS_X] +MIN_LIMIT = -40.0 +MAX_LIMIT = 40.0 +MAX_VELOCITY = 100 +MAX_ACCELERATION = 10000.0 + +[JOINT_0] +TYPE = LINEAR +HOME = 0.0 +MAX_VELOCITY = 100 +MAX_ACCELERATION = 10000.0 +MIN_LIMIT = -40.0 +MAX_LIMIT = 40.0 +FERROR = 0.050 +MIN_FERROR = 0.010 + +[AXIS_Y] +MIN_LIMIT = -40.0 +MAX_LIMIT = 40.0 +MAX_VELOCITY = 100 +MAX_ACCELERATION = 10000.0 + +[JOINT_1] +TYPE = LINEAR +HOME = 0.0 +MAX_VELOCITY = 100 +MAX_ACCELERATION = 10000.0 +MIN_LIMIT = -40.0 +MAX_LIMIT = 40.0 +FERROR = 0.050 +MIN_FERROR = 0.010 + +[AXIS_Z] +MIN_LIMIT = -40.0 +MAX_LIMIT = 40.0 +MAX_VELOCITY = 100 +MAX_ACCELERATION = 10000.0 + +[JOINT_2] +TYPE = LINEAR +HOME = 0.0 +MAX_VELOCITY = 100 +MAX_ACCELERATION = 10000.0 +MIN_LIMIT = -40.0 +MAX_LIMIT = 40.0 +FERROR = 0.050 +MIN_FERROR = 0.010 diff --git a/tests/interp-error-abort/test.sh b/tests/interp-error-abort/test.sh new file mode 100755 index 00000000000..1f7aa8c808a --- /dev/null +++ b/tests/interp-error-abort/test.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e + +linuxcnc -r test.ini diff --git a/tests/motion-logger/basic/exec-error.ngc b/tests/motion-logger/basic/exec-error.ngc new file mode 100644 index 00000000000..a5b248bf610 --- /dev/null +++ b/tests/motion-logger/basic/exec-error.ngc @@ -0,0 +1,8 @@ +; An execute error (G83 with Q0) is handled the same way as a read error: +; the lines before it run, then task aborts and stops the spindle. +G1 X0 Y0 F100 +G1 X10 Y10 +M3 S1000 +G83 X1 Y1 Z-1 R1 Q0 +G1 X100 Y53 +M2 diff --git a/tests/motion-logger/basic/expected.exec-error b/tests/motion-logger/basic/expected.exec-error new file mode 100644 index 00000000000..29628152761 --- /dev/null +++ b/tests/motion-logger/basic/expected.exec-error @@ -0,0 +1,22 @@ +SET_LINE x=10, y=10, z=0, a=0, b=0, c=0, u=0, v=0, w=0, id=4, motion_type=2, vel=1.66667, ini_maxvel=5.65685, acc=1414.21, turn=-1 +SPINDLE_ON speed=0.000000, css_factor=0.000000, xoffset=0.000000 +SPINDLE_ON speed=1000.000000, css_factor=0.000000, xoffset=0.000000 +JOG_ABORT joint=0 +JOG_ABORT joint=1 +JOG_ABORT joint=2 +JOG_ABORT joint=3 +JOG_ABORT joint=4 +JOG_ABORT joint=5 +JOG_ABORT joint=6 +JOG_ABORT joint=7 +JOG_ABORT joint=8 +JOG_ABORT joint=9 +JOG_ABORT joint=10 +JOG_ABORT joint=11 +JOG_ABORT joint=12 +JOG_ABORT joint=13 +JOG_ABORT joint=14 +JOG_ABORT joint=15 +ABORT +SPINDLE_OFF +SS_ENABLE diff --git a/tests/motion-logger/basic/expected.g4-no-p b/tests/motion-logger/basic/expected.g4-no-p new file mode 100644 index 00000000000..f36cfd2646c --- /dev/null +++ b/tests/motion-logger/basic/expected.g4-no-p @@ -0,0 +1,22 @@ +SET_LINE x=10, y=10, z=0, a=0, b=0, c=0, u=10, v=10, w=0, id=4, motion_type=2, vel=1.66667, ini_maxvel=5.65685, acc=1414.21, turn=-1 +SPINDLE_ON speed=0.000000, css_factor=0.000000, xoffset=0.000000 +SPINDLE_ON speed=1000.000000, css_factor=0.000000, xoffset=0.000000 +JOG_ABORT joint=0 +JOG_ABORT joint=1 +JOG_ABORT joint=2 +JOG_ABORT joint=3 +JOG_ABORT joint=4 +JOG_ABORT joint=5 +JOG_ABORT joint=6 +JOG_ABORT joint=7 +JOG_ABORT joint=8 +JOG_ABORT joint=9 +JOG_ABORT joint=10 +JOG_ABORT joint=11 +JOG_ABORT joint=12 +JOG_ABORT joint=13 +JOG_ABORT joint=14 +JOG_ABORT joint=15 +ABORT +SPINDLE_OFF +SS_ENABLE diff --git a/tests/motion-logger/basic/g4-no-p.ngc b/tests/motion-logger/basic/g4-no-p.ngc new file mode 100644 index 00000000000..a914c33e6b5 --- /dev/null +++ b/tests/motion-logger/basic/g4-no-p.ngc @@ -0,0 +1,8 @@ +; #2978: G4 with no P is a read error. The lines before it run, then task +; aborts: the spindle the M3 started is stopped, not left running. +G1 X0 Y0 U0 V0 F100 +G1 X10 Y10 U10 V10 +M3 S1000 +G4 X2 +G1 X100 Y53 +M2