|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Joints 0 and 2 share one input for home and limit switches. With |
| 3 | +# HOME_IGNORE_LIMITS and HOME_IS_SHARED on both, homing one joint must |
| 4 | +# not trip the other's limit; once homed the limits are live again. |
| 5 | + |
| 6 | +import linuxcnc |
| 7 | +import hal |
| 8 | + |
| 9 | +import subprocess |
| 10 | +import sys |
| 11 | +import time |
| 12 | + |
| 13 | +failures = [] |
| 14 | + |
| 15 | + |
| 16 | +def fail(msg): |
| 17 | + print("FAIL " + msg) |
| 18 | + failures.append(msg) |
| 19 | + |
| 20 | + |
| 21 | +def wait_for(cond, timeout): |
| 22 | + t0 = time.time() |
| 23 | + while time.time() - t0 < timeout: |
| 24 | + s.poll() |
| 25 | + if cond(): |
| 26 | + return True |
| 27 | + time.sleep(0.01) |
| 28 | + s.poll() |
| 29 | + return cond() |
| 30 | + |
| 31 | + |
| 32 | +def errors(): |
| 33 | + out = [] |
| 34 | + while True: |
| 35 | + m = e.poll() |
| 36 | + if not m: |
| 37 | + return out |
| 38 | + print("linuxcnc error: %s" % m[1]) |
| 39 | + out.append(m[1]) |
| 40 | + |
| 41 | + |
| 42 | +def sets(name, value): |
| 43 | + subprocess.run(["halcmd", "sets", name, str(value)], check=True) |
| 44 | + |
| 45 | + |
| 46 | +def home(joint, joints, label): |
| 47 | + errors() |
| 48 | + c.teleop_enable(0) |
| 49 | + c.wait_complete() |
| 50 | + c.home(joint) |
| 51 | + c.wait_complete() |
| 52 | + ok = wait_for(lambda: all(s.homed[j] for j in joints) |
| 53 | + and not any(s.joint[j]["homing"] for j in joints) |
| 54 | + or s.task_state != linuxcnc.STATE_ON, 30.0) |
| 55 | + msgs = errors() |
| 56 | + if not ok or s.task_state != linuxcnc.STATE_ON or msgs: |
| 57 | + fail("%s: homed=%s on=%s" % (label, s.homed[:3], s.task_state == linuxcnc.STATE_ON)) |
| 58 | + else: |
| 59 | + print("%-24s homed" % label) |
| 60 | + |
| 61 | + |
| 62 | +h = hal.component("test-ui") |
| 63 | +h.ready() |
| 64 | +c = linuxcnc.command() |
| 65 | +s = linuxcnc.stat() |
| 66 | +e = linuxcnc.error_channel() |
| 67 | + |
| 68 | +c.state(linuxcnc.STATE_ESTOP_RESET) |
| 69 | +c.wait_complete() |
| 70 | +c.state(linuxcnc.STATE_ON) |
| 71 | +c.wait_complete() |
| 72 | +c.mode(linuxcnc.MODE_MANUAL) |
| 73 | +c.wait_complete() |
| 74 | + |
| 75 | +# Home all, joint 2 first then joint 0: each presses the switch while |
| 76 | +# the other stands still. |
| 77 | +home(-1, [0, 1, 2], "home all") |
| 78 | + |
| 79 | +# Homed: the limit is live again. |
| 80 | +errors() |
| 81 | +sets("xswpos", 1) |
| 82 | +if not wait_for(lambda: s.task_state != linuxcnc.STATE_ON, 5.0): |
| 83 | + fail("limit after homing: machine still on") |
| 84 | +elif not any("limit switch error" in m for m in errors()): |
| 85 | + fail("limit after homing: no limit switch error") |
| 86 | +else: |
| 87 | + print("%-24s tripped" % "limit after homing") |
| 88 | + |
| 89 | +if failures: |
| 90 | + print("%d failure(s):" % len(failures)) |
| 91 | + for msg in failures: |
| 92 | + print(" " + msg) |
| 93 | + sys.exit(1) |
| 94 | +print("success") |
| 95 | +sys.exit(0) |
0 commit comments