From 43ceba5693269718f7fc90ed066b66334162c907 Mon Sep 17 00:00:00 2001 From: Luca Toniolo <10792599+grandixximo@users.noreply.github.com> Date: Sat, 5 Dec 2026 17:57:35 +1100 Subject: [PATCH] interp: take the G52/G92 offset from status on abort The interpreter reads ahead of the machine. When a program was aborted after the read ahead had passed a G52 or G92 the machine never reached, the interpreter kept that offset while status, and so the DRO, still had the old one, and the next move went to the read ahead's offset (#2908). DISABLE_G92_PERSISTENCE did not clear the offset on an abort either, only at startup and at M2/M30. On abort the interpreter now takes the offset the machine has from status, as it does the coordinate system, and tells canon so status and interpreter agree. With DISABLE_G92_PERSISTENCE it clears the offset, as M2 does. A new canon call, GET_EXTERNAL_G92_OFFSET, reads it. tests/statbuffer-g5x-abort sets G52 X7 from MDI, aborts a program whose read ahead has passed a G52 X3, and checks the DRO, #5211 and where G0 X0 goes; it runs again with DISABLE_G92_PERSISTENCE = 1 and expects 0. Its fixed sleeps are now waits on status. Without the fix it ends at 3 in both runs. The docs say the offset follows an abort, and overview.adoc no longer has the persistence of #5211-#5219 backwards. --- docs/src/config/ini-config.adoc | 4 +- docs/src/gcode/coordinates.adoc | 14 +-- docs/src/gcode/overview.adoc | 2 +- include/nml_intf/canon.hh | 5 + src/emc/rs274ngc/gcodemodule.cc | 1 + src/emc/rs274ngc/rs274ngc_pre.cc | 43 +++++++++ src/emc/sai/saicanon.cc | 5 + src/emc/task/emccanon.cc | 10 ++ tests/interp/compile/use-rs274.cc | 1 + tests/statbuffer-g5x-abort/nopersist.ini | 117 +++++++++++++++++++++++ tests/statbuffer-g5x-abort/test-ui.py | 44 +++++++-- tests/statbuffer-g5x-abort/test.ngc | 1 + tests/statbuffer-g5x-abort/test.sh | 6 +- 13 files changed, 235 insertions(+), 18 deletions(-) create mode 100644 tests/statbuffer-g5x-abort/nopersist.ini diff --git a/docs/src/config/ini-config.adoc b/docs/src/config/ini-config.adoc index e8aabf083d6..bd32ed55445 100644 --- a/docs/src/config/ini-config.adoc +++ b/docs/src/config/ini-config.adoc @@ -632,8 +632,8 @@ The maximum number of `USER_M_PATH` directories is defined at compile time (typ: Preserve case in O-word names within comments if set, enables reading of mixed-case HAL items in structured comments like `(debug, #<_hal[MixedCaseItem])`. * `OWORD_WARNONLY = 0` (Default: 0) + Warn rather than error in case of errors in O-word subroutines. -* `DISABLE_G92_PERSISTENCE = 0` (bool, Default: 0) - Allow to clear the G92 offset automatically when config start-up. +* `DISABLE_G92_PERSISTENCE = 0` (bool, Default: 0) + + When set the shared G52/G92 offset is cleared at startup, by M2 and M30, and by an abort. * `DISABLE_AUTO_G54 = 0` (bool, Default: 0) + When set M2 and M30 will no longer automatically reset the active WCS to 'G54'. * `RETAIN_WORK_PLANE = 0` (bool, Default: 0) + diff --git a/docs/src/gcode/coordinates.adoc b/docs/src/gcode/coordinates.adoc index ffe321c982e..c134879fccc 100644 --- a/docs/src/gcode/coordinates.adoc +++ b/docs/src/gcode/coordinates.adoc @@ -381,11 +381,13 @@ program, the best practice is to issue a G92.1 at the end of any G code files where you use G92 offsets. When a program is aborted during processing that has 'G92' offsets in -effect a startup will cause them to become active again. As a -safeguard, always have your preamble to set the environment as you -expect it. Additionally, 'G92' persistence may be disabled by setting -'DISABLE_G92_PERSISTENCE = 1' in the '[RS274NGC]' section of the -INI file. +effect, they stay in effect, and a startup will cause them to become +active again. The offset kept is the one the machine had when the +program stopped, not one set by a line the interpreter had read ahead +to. As a safeguard, always have your preamble to set the environment +as you expect it. Additionally, 'G92' persistence may be disabled by +setting 'DISABLE_G92_PERSISTENCE = 1' in the '[RS274NGC]' section of +the INI file, which also clears the offsets on an abort. [[sec:g92-g52-interaction-cautions]] === G92 and G52 Interaction Cautions @@ -393,7 +395,7 @@ INI file. 'G52' and 'G92' share the same offset registers. Unless 'G92' persistence is disabled in the INI file (see <>), -'G52' offsets will also persist after machine reset, +'G52' offsets will also persist after machine reset, an abort, 'M02' or 'M30'. Beware that a 'G52' offset in effect during a program abort may result in unintended offsets when the next program is run. See <> above. diff --git a/docs/src/gcode/overview.adoc b/docs/src/gcode/overview.adoc index f2bfecf17b2..d1f59fc674b 100644 --- a/docs/src/gcode/overview.adoc +++ b/docs/src/gcode/overview.adoc @@ -324,7 +324,7 @@ example '##2' means the value of the parameter whose index is the 'DISABLE_G92_PERSISTENCE = 1' in the '[RS274NGC]' section of the INI file. * '5211-5219' - Shared "G52" and "G92" offset for X, Y, Z, A, B, C, U, - V & W. Volatile by default; persistent if + V & W. Persistent by default; volatile if 'DISABLE_G92_PERSISTENCE = 1' in the '[RS274NGC]' section of the INI file. * '5220' - Coordinate System number 1 - 9 for G54 - G59.3. Persistent. diff --git a/include/nml_intf/canon.hh b/include/nml_intf/canon.hh index a89772c224f..aa8ea91e014 100644 --- a/include/nml_intf/canon.hh +++ b/include/nml_intf/canon.hh @@ -915,6 +915,11 @@ extern int GET_EXTERNAL_HAL_VALUE(const char *name, double *value); interpreter has, -1 when there is no machine to ask (sai, preview). */ extern int GET_EXTERNAL_G68_FRAME(double origin[3], double rotation[9]); +/* The G92/G52 offset the machine last executed, X Y Z A B C U V W in + program units: 1 with offset filled in, -1 when there is no machine to + ask (sai, preview). */ +extern int GET_EXTERNAL_G92_OFFSET(double offset[9]); + // Returns the current motion path-following tolerance extern double GET_EXTERNAL_MOTION_CONTROL_TOLERANCE(); diff --git a/src/emc/rs274ngc/gcodemodule.cc b/src/emc/rs274ngc/gcodemodule.cc index 4bee697bfaa..fd778ded4e2 100644 --- a/src/emc/rs274ngc/gcodemodule.cc +++ b/src/emc/rs274ngc/gcodemodule.cc @@ -1012,6 +1012,7 @@ int GET_EXTERNAL_KINS_TYPE_FLAGS(int ktype) { return kins_type_flags[ktype]; } int GET_EXTERNAL_G68_FRAME(double * /*origin*/, double * /*rotation*/) { return -1; } +int GET_EXTERNAL_G92_OFFSET(double * /*offset*/) { return -1; } // the kind of transform the machine runs, from a canon that watches the // status buffer; one that cannot answer has no machine, and the joints diff --git a/src/emc/rs274ngc/rs274ngc_pre.cc b/src/emc/rs274ngc/rs274ngc_pre.cc index e76326b3e00..ba23cacae08 100644 --- a/src/emc/rs274ngc/rs274ngc_pre.cc +++ b/src/emc/rs274ngc/rs274ngc_pre.cc @@ -2658,6 +2658,49 @@ int Interp::on_abort(int reason, const char *message) reset(); _setup.mdi_interrupt = false; + // the G92/G52 offset: the read ahead may have set one the machine never + // got, so the interpreter takes the one status has, as it does the + // coordinate system. DISABLE_G92_PERSISTENCE clears it, as M2 does. + // Canon is told either way, since the message that would have brought + // status level with the interpreter died with the queue. This comes + // ahead of the plane, which checks the offset under it against status. + { + double *const off[9] = { + &_setup.axis_offset_x, &_setup.axis_offset_y, &_setup.axis_offset_z, + &_setup.AA_axis_offset, &_setup.BB_axis_offset, &_setup.CC_axis_offset, + &_setup.u_axis_offset, &_setup.v_axis_offset, &_setup.w_axis_offset }; + double *const cur[9] = { + &_setup.current_x, &_setup.current_y, &_setup.current_z, + &_setup.AA_current, &_setup.BB_current, &_setup.CC_current, + &_setup.u_current, &_setup.v_current, &_setup.w_current }; + double want[9]; + bool have = GET_EXTERNAL_G92_OFFSET(want) > 0; + bool changed = false, any = false; + + if (_setup.disable_g92_persistence) { + for (int i = 0; i < 9; i++) { want[i] = 0.0; } + have = true; + } + if (have) { + for (int i = 0; i < 9; i++) { + if (*off[i] != want[i]) { changed = true; } + if (want[i] != 0.0) { any = true; } + *cur[i] += *off[i] - want[i]; + *off[i] = want[i]; + } + // an unchanged offset keeps its parameters, so a G92.2 still + // has the values G92.3 brings back + if (changed || _setup.disable_g92_persistence) { + _setup.parameters[5210] = any ? 1.0 : 0.0; + for (int i = 0; i < 9; i++) { + _setup.parameters[5211 + i] = PROGRAM_TO_USER_AX(i, want[i]); + } + } + } + SET_G92_OFFSET(*off[0], *off[1], *off[2], *off[3], *off[4], *off[5], + *off[6], *off[7], *off[8]); + } + // the tilted work plane goes before the abort routine runs, so that // routine can change coordinate systems as it likes. Canon is told // even when the read ahead had already cancelled it, since the message diff --git a/src/emc/sai/saicanon.cc b/src/emc/sai/saicanon.cc index d06775a120a..ea3147a2ba8 100644 --- a/src/emc/sai/saicanon.cc +++ b/src/emc/sai/saicanon.cc @@ -840,6 +840,11 @@ extern int GET_EXTERNAL_G68_FRAME(double * /*origin*/, double * /*rotation*/) return -1; } +extern int GET_EXTERNAL_G92_OFFSET(double * /*offset*/) +{ + return -1; +} + extern bool GET_EXTERNAL_KINEMATICS_IDENTITY() { return true; diff --git a/src/emc/task/emccanon.cc b/src/emc/task/emccanon.cc index ec11bca6d00..98dffb5122c 100644 --- a/src/emc/task/emccanon.cc +++ b/src/emc/task/emccanon.cc @@ -4329,6 +4329,16 @@ int GET_EXTERNAL_G68_FRAME(double origin[3], double rotation[9]) return 1; } +int GET_EXTERNAL_G92_OFFSET(double offset[9]) +{ + // status follows the executed commands, canon the read ahead + const EmcPose &p = emcStatus->task.g92_offset; + const double ext[9] = { p.tran.x, p.tran.y, p.tran.z, p.a, p.b, p.c, p.u, p.v, p.w }; + + for (int i = 0; i < 9; i++) { offset[i] = TO_PROG_AX(i, FROM_EXT_AX(i, ext[i])); } + return 1; +} + bool GET_EXTERNAL_KINEMATICS_IDENTITY() { return emcStatus->motion.traj.kinematics_type == KINEMATICS_IDENTITY; diff --git a/tests/interp/compile/use-rs274.cc b/tests/interp/compile/use-rs274.cc index a3fee1dd9c0..621d20b57cf 100644 --- a/tests/interp/compile/use-rs274.cc +++ b/tests/interp/compile/use-rs274.cc @@ -254,6 +254,7 @@ void *GET_EXTERNAL_KINEMATICS(const struct KinematicsUserOps **ops, const char * } int GET_EXTERNAL_HAL_VALUE(const char *name, double *value) { return -ENOENT; } int GET_EXTERNAL_G68_FRAME(double origin[3], double rotation[9]) { return -1; } +int GET_EXTERNAL_G92_OFFSET(double offset[9]) { return -1; } void SET_G68_FRAME(double x, double y, double z, const double rotation[9], int active) {} void HOME_CYCLE(void) {} void HOME_CYCLE_JOINT(int joint) {} diff --git a/tests/statbuffer-g5x-abort/nopersist.ini b/tests/statbuffer-g5x-abort/nopersist.ini new file mode 100644 index 00000000000..09881a78244 --- /dev/null +++ b/tests/statbuffer-g5x-abort/nopersist.ini @@ -0,0 +1,117 @@ +# This config file was created 2016-06-17 10:27:05.976381 by the update_ini script +# The original config files may be found in the /data/s/linuxcnc-dev/tests/statbuffer-g5x-abort/test.old directory + +[EMC] +# The version string for this INI file. +VERSION = 1.1 + +DEBUG = 0x0 + +[DISPLAY] +DISPLAY = ./test-ui.py + +[FILTER] +#No Content + +[RS274NGC] +PARAMETER_FILE = sim.var +DISABLE_G92_PERSISTENCE = 1 +USER_M_PATH = ./subs + +[EMCMOT] +EMCMOT = motmod +COMM_TIMEOUT = 4.0 +BASE_PERIOD = 0 +SERVO_PERIOD = 1000000 + +[TASK] +TASK = milltask +CYCLE_TIME = 0.001 + +[HAL] +HALUI = halui +HALFILE = core_sim.hal + +[HALUI] +#No Content + +[TRAJ] +NO_FORCE_HOMING=1 +AXES = 3 +COORDINATES = X Y Z +HOME = 0 0 0 +LINEAR_UNITS = inch +ANGULAR_UNITS = degree +DEFAULT_LINEAR_VELOCITY = 1.2 +MAX_LINEAR_VELOCITY = 4 + +[EMCIO] +TOOL_TABLE = simpockets.tbl +TOOL_CHANGE_QUILL_UP = 1 +RANDOM_TOOLCHANGER = 0 + + +[KINS] +KINEMATICS = trivkins +#This is a best-guess at the number of joints, it should be checked +JOINTS = 3 + +[AXIS_X] +MIN_LIMIT = -40.0 +MAX_LIMIT = 40.0 +MAX_VELOCITY = 4 +MAX_ACCELERATION = 1000.0 + +[JOINT_0] + +TYPE = LINEAR +HOME = 0.000 +MAX_VELOCITY = 4 +MAX_ACCELERATION = 1000.0 +BACKLASH = 0.000 +INPUT_SCALE = 4000 +OUTPUT_SCALE = 1.000 +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 = 4 +MAX_ACCELERATION = 1000.0 + +[JOINT_1] + +TYPE = LINEAR +HOME = 0.000 +MAX_VELOCITY = 4 +MAX_ACCELERATION = 1000.0 +BACKLASH = 0.000 +INPUT_SCALE = 4000 +OUTPUT_SCALE = 1.000 +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 = 4 +MAX_ACCELERATION = 1000.0 + +[JOINT_2] + +TYPE = LINEAR +HOME = 0.0 +MAX_VELOCITY = 4 +MAX_ACCELERATION = 1000.0 +BACKLASH = 0.000 +INPUT_SCALE = 4000 +OUTPUT_SCALE = 1.000 +MIN_LIMIT = -40.0 +MAX_LIMIT = 40.0 +FERROR = 0.050 +MIN_FERROR = 0.010 diff --git a/tests/statbuffer-g5x-abort/test-ui.py b/tests/statbuffer-g5x-abort/test-ui.py index e219b3861d8..4afa80cd707 100755 --- a/tests/statbuffer-g5x-abort/test-ui.py +++ b/tests/statbuffer-g5x-abort/test-ui.py @@ -15,6 +15,22 @@ retval = 0 +# the G52 set from MDI below survives the abort unless the INI clears it +ini = linuxcnc.ini(os.environ['INI_FILE_NAME']) +persist = int(ini.find('RS274NGC', 'DISABLE_G92_PERSISTENCE') or 0) == 0 +g52 = 7.0 if persist else 0.0 +print("DISABLE_G92_PERSISTENCE = %d" % (0 if persist else 1)) + +def wait_for(cond, what, timeout=30): + end = time.time() + timeout + while time.time() < end: + s.poll() + if cond(): + return + time.sleep(0.01) + print("timed out waiting for %s: motion_line %d read_line %d" % (what, s.motion_line, s.read_line)) + sys.exit(1) + c = linuxcnc.command() s = linuxcnc.stat() @@ -32,16 +48,18 @@ c.mdi('G10 L2 P3 X0 Y0 Z-6 (Set G56)') c.mdi('G54 (Use G54)') c.mdi('G0 X0 Y0 Z0 (Move to 0,0,0)') +c.mdi('G52 X7 (the offset the machine has when the program runs)') c.wait_complete() -# FIXME: this is lame -time.sleep(1) - c.mode(linuxcnc.MODE_AUTO) c.auto(linuxcnc.AUTO_RUN, 0) -# FIXME: this is lame -time.sleep(1) +# abort in the slow move, once the read ahead has passed the G52 X3; +# status counts lines from 0 +lines = open('test.ngc').read().splitlines() +slow = [i for i, l in enumerate(lines) if l.startswith('G1 ')][0] +buster = [i for i, l in enumerate(lines) if l.startswith('M66')][0] +wait_for(lambda: s.motion_line == slow and s.read_line >= buster, "the slow move") c.abort() @@ -62,8 +80,8 @@ c.wait_complete() s.poll() -if math.fabs(s.position[0]) > 0.000001: - print("'G0 X0 Y0 Z0' took us to X=%.6f, should be 0" % s.position[0]) +if math.fabs(s.position[0] - g52) > 0.000001: + print("'G0 X0 Y0 Z0' took us to X=%.6f, should be %g" % (s.position[0], g52)) retval = 1 if math.fabs(s.position[1]) > 0.000001: @@ -98,6 +116,18 @@ print("g5x_offset.z is %.6f, should be -5" % s.g5x_offset[2]) retval = 1 +# the G52 X3 the read ahead reached never ran +if math.fabs(s.g92_offset[0] - g52) > 0.000001: + print("g92_offset.x is %.6f, should be %g" % (s.g92_offset[0], g52)) + retval = 1 + +c.mdi('(debug,#5211)') +c.wait_complete() +err = e.poll() +if not err or math.fabs(float(err[1]) - g52) > 0.000001: + print("#5211 is %s, should be %g" % (err and err[1], g52)) + retval = 1 + if retval == 0: print("Everything ok!") diff --git a/tests/statbuffer-g5x-abort/test.ngc b/tests/statbuffer-g5x-abort/test.ngc index 667abb57e2b..dd770efad85 100644 --- a/tests/statbuffer-g5x-abort/test.ngc +++ b/tests/statbuffer-g5x-abort/test.ngc @@ -10,6 +10,7 @@ G1 X5 Y-5 F1 ; 3. Interp will read ahead: set G56 offset and throw a queue buster; ; execution will never reach this point G56 +G52 X3 M66 P0 L0 (queue buster) % diff --git a/tests/statbuffer-g5x-abort/test.sh b/tests/statbuffer-g5x-abort/test.sh index 52db72cd70d..91906b933bc 100755 --- a/tests/statbuffer-g5x-abort/test.sh +++ b/tests/statbuffer-g5x-abort/test.sh @@ -1,4 +1,6 @@ #!/bin/bash - +set -e +rm -f sim.var sim.var.bak linuxcnc -r test.ini - +linuxcnc -r nopersist.ini +rm -f sim.var sim.var.bak