|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | +Regression test: a partial G28.3 Pn on NON-IDENTITY kinematics must abort |
| 5 | +cleanly instead of stranding the machine. |
| 6 | +
|
| 7 | +Motion refuses to (re-)enter TELEOP or COORD while any joint is unhomed, but |
| 8 | +only when the kinematics are not identity -- switch_to_teleop_mode() (motion.c) |
| 9 | +and the EMCMOT_COORD case (command.c) both gate on |
| 10 | +"kinType != KINEMATICS_IDENTITY && !get_allhomed()". |
| 11 | +
|
| 12 | +The G28.2/G28.3 sequencing dips motion into FREE (do_homing() only advances |
| 13 | +there) and restores the previous trajectory mode afterwards. Restoring that |
| 14 | +mode unconditionally is wrong: a per-joint G28.3 Pn succeeds for its own joint |
| 15 | +while leaving the machine as a whole unreferenced, so motion rejects the |
| 16 | +restore, task still reports DONE, and the machine is stranded in FREE with the |
| 17 | +GUI's mode controls greyed out -- recoverable only by cycling the controller. |
| 18 | +Reported on real hardware (Mesa 7I95T gantry) in PR #4172, where it surfaced as |
| 19 | +"all joints must be homed before going into coordinated mode" followed by a |
| 20 | +dead UI needing F2. |
| 21 | +
|
| 22 | +Note this needs NO_FORCE_HOMING=1. With the default 0, an earlier branch |
| 23 | +catches the partial-unhome case first and this path is never reached -- which |
| 24 | +is exactly why the trivkins `sequencing` test (NO_FORCE_HOMING unset, identity |
| 25 | +kins) passes both before and after the fix. |
| 26 | +
|
| 27 | +The witness is deliberately M64 P0 rather than a move: a digital output needs |
| 28 | +no coordinated motion, so it still executes with the machine stuck in FREE. |
| 29 | +If it ends up set, the program continued past the G28.3 -- the bug. |
| 30 | +""" |
| 31 | + |
| 32 | +import linuxcnc |
| 33 | +import hal |
| 34 | + |
| 35 | +import os |
| 36 | +import sys |
| 37 | +import time |
| 38 | + |
| 39 | +h = hal.component("python-ui") |
| 40 | +h.ready() |
| 41 | + |
| 42 | +c = linuxcnc.command() |
| 43 | +s = linuxcnc.stat() |
| 44 | +e = linuxcnc.error_channel() |
| 45 | + |
| 46 | + |
| 47 | +def poll(): |
| 48 | + s.poll() |
| 49 | + |
| 50 | + |
| 51 | +def fail(msg): |
| 52 | + print("FAIL: " + msg) |
| 53 | + sys.exit(1) |
| 54 | + |
| 55 | + |
| 56 | +def near(a, b, tol=0.001): |
| 57 | + return abs(a - b) < tol |
| 58 | + |
| 59 | + |
| 60 | +def wait_idle(timeout=10.0): |
| 61 | + t0 = time.time() |
| 62 | + while time.time() - t0 < timeout: |
| 63 | + poll() |
| 64 | + if s.interp_state == linuxcnc.INTERP_IDLE: |
| 65 | + return True |
| 66 | + time.sleep(0.01) |
| 67 | + return False |
| 68 | + |
| 69 | + |
| 70 | +def wait_homed(expected, timeout=10.0): |
| 71 | + t0 = time.time() |
| 72 | + while time.time() - t0 < timeout: |
| 73 | + poll() |
| 74 | + if list(s.homed[:3]) == expected: |
| 75 | + return True |
| 76 | + time.sleep(0.01) |
| 77 | + return False |
| 78 | + |
| 79 | + |
| 80 | +def drain_errors(): |
| 81 | + msgs = [] |
| 82 | + while True: |
| 83 | + err = e.poll() |
| 84 | + if not err: |
| 85 | + return msgs |
| 86 | + msgs.append(err[1]) |
| 87 | + |
| 88 | + |
| 89 | +c.state(linuxcnc.STATE_ESTOP_RESET) |
| 90 | +c.state(linuxcnc.STATE_ON) |
| 91 | +c.home(0) |
| 92 | +c.home(1) |
| 93 | +c.home(2) |
| 94 | +if not wait_homed([1, 1, 1]): |
| 95 | + fail("initial home-all did not home all joints: {}".format(list(s.homed[:3]))) |
| 96 | + |
| 97 | +# Confirm the machine really is in a coordinated mode and can move, so that the |
| 98 | +# restore this test is about has something meaningful to restore to. |
| 99 | +c.mode(linuxcnc.MODE_MDI) |
| 100 | +c.mdi("G0 X1") |
| 101 | +if not wait_idle(): |
| 102 | + fail("setup MDI move did not settle") |
| 103 | +poll() |
| 104 | +if not near(s.position[0], 1.0): |
| 105 | + fail("setup MDI move did not run (X at {}) -- machine not in a usable coordinated mode".format(s.position[0])) |
| 106 | +drain_errors() |
| 107 | + |
| 108 | +# Run the program: G28.3 P0 (partial unhome) then M64 P0 (the witness). |
| 109 | +c.mode(linuxcnc.MODE_AUTO) |
| 110 | +c.program_open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "test.ngc")) |
| 111 | +c.auto(linuxcnc.AUTO_RUN, 0) |
| 112 | +if not wait_idle(): |
| 113 | + # This is the unfixed behaviour: task reported DONE for the G28.3, motion |
| 114 | + # silently refused to take TELEOP/COORD back, and the interpreter now sits |
| 115 | + # forever on a line it can never run. A timeout here IS the wedge. |
| 116 | + poll() |
| 117 | + fail( |
| 118 | + "interpreter never returned to idle after the G28.3 -- the machine is " |
| 119 | + "wedged: task accepted the partial unhome and tried to restore a " |
| 120 | + "coordinated mode that motion cannot grant while unreferenced " |
| 121 | + "(motion_mode={}, homed={}, dout0={})".format( |
| 122 | + s.motion_mode, list(s.homed[:3]), s.dout[0]) |
| 123 | + ) |
| 124 | +time.sleep(0.3) |
| 125 | +poll() |
| 126 | + |
| 127 | +if list(s.homed[:3]) == [1, 1, 1]: |
| 128 | + fail("G28.3 P0 did not actually unhome joint 0: {}".format(list(s.homed[:3]))) |
| 129 | + |
| 130 | +if s.dout[0]: |
| 131 | + fail( |
| 132 | + "program continued past a G28.3 that left the machine partially homed " |
| 133 | + "(digital-out 0 got set): the mode restore was attempted and silently " |
| 134 | + "refused by motion, leaving the machine stranded in FREE while task " |
| 135 | + "reported DONE" |
| 136 | + ) |
| 137 | +print("PASS: a partial G28.3 Pn aborts the program on non-identity kinematics") |
| 138 | + |
| 139 | +if s.motion_mode != linuxcnc.TRAJ_MODE_FREE: |
| 140 | + fail("expected to be left in FREE after the aborted G28.3, got motion_mode={}".format(s.motion_mode)) |
| 141 | +print("PASS: machine is left in FREE, the only mode it may legally sit in unreferenced") |
| 142 | + |
| 143 | +msgs = drain_errors() |
| 144 | +if not msgs: |
| 145 | + fail("the aborted G28.3 reported no error at all -- the operator gets no explanation") |
| 146 | +print("PASS: an operator error was reported ({!r})".format(msgs[0][:70])) |
| 147 | + |
| 148 | +# The whole point of failing cleanly rather than wedging: ordinary recovery |
| 149 | +# must work, with no controller restart. |
| 150 | +c.mode(linuxcnc.MODE_MANUAL) |
| 151 | +c.home(0) |
| 152 | +if not wait_homed([1, 1, 1]): |
| 153 | + fail("could not re-home joint 0 after the aborted G28.3 -- machine is wedged") |
| 154 | + |
| 155 | +c.mode(linuxcnc.MODE_MDI) |
| 156 | +c.mdi("G0 X2") |
| 157 | +if not wait_idle(): |
| 158 | + fail("post-recovery MDI move did not settle") |
| 159 | +poll() |
| 160 | +if not near(s.position[0], 2.0): |
| 161 | + fail("coordinated motion did not come back after re-homing (X stayed at {})".format(s.position[0])) |
| 162 | +print("PASS: re-homing recovers coordinated motion without cycling the controller") |
| 163 | + |
| 164 | +print("done! it all worked") |
| 165 | +sys.exit(0) |
0 commit comments