Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/emc/nml_intf/interpl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down Expand Up @@ -175,6 +178,20 @@ void InterpList<Msg>::append(InterpList &&other)
other.linked_list.clear();
}

template <class Msg>
void InterpList<Msg>::truncate(int n)
{
if (n < 0)
n = 0;
if (static_cast<size_t>(n) >= linked_list.size())
return;
if (tracing()) {
printf("InterpList(%p)::truncate(%d): discarding %zu items\n",
static_cast<const void *>(this), n, linked_list.size() - n);
}
linked_list.erase(linked_list.begin() + n, linked_list.end());
}

template <class Msg>
void InterpList<Msg>::print() const
{
Expand Down
29 changes: 29 additions & 0 deletions src/emc/task/emccanon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1364,13 +1364,23 @@ struct pt {

static std::vector<struct pt> 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;
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}
Expand Down
3 changes: 3 additions & 0 deletions src/emc/task/emctask.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
46 changes: 41 additions & 5 deletions src/emc/task/emctaskmain.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 =
Expand Down Expand Up @@ -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();
Expand Down
10 changes: 10 additions & 0 deletions src/emc/task/task.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@

extern std::unique_ptr<NMLmsg> 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<NMLmsg> &&cmd);
extern int emcTaskOnce(const char *inifile, EMC_IO_STAT &emcioStatus);
Expand Down
2 changes: 2 additions & 0 deletions tests/interp-error-abort/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
isub.ngc
test.ngc
15 changes: 15 additions & 0 deletions tests/interp-error-abort/README
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 3 additions & 0 deletions tests/interp-error-abort/checkresult
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
# Test passes if test-ui.py returns successfully
exit 0
5 changes: 5 additions & 0 deletions tests/interp-error-abort/late.hal
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions tests/interp-error-abort/simpockets.tbl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
T1 P1 Z0.0 D0.0 ;
167 changes: 167 additions & 0 deletions tests/interp-error-abort/test-ui.py
Original file line number Diff line number Diff line change
@@ -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<isub> sub"] + ["G4 P0"] * 20
+ [move, bad, "o<isub> endsub", "M2"]) + "\n")
check("%s error after %s in a file skipped" % (kind, name),
["G20 G90 G64 P0.01", "o<isub> 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()
Loading