Files
Lumi/robot/motion.py
T

265 lines
12 KiB
Python
Raw Normal View History

2026-10-21 15:19:07 +09:00
"""Checked arm motion over the controller's TCP command port (10001, JSON, no SDK): named poses, joint moves,
straight-line TCP moves, stop.
Every move is checked before it is sent: joint limits with a margin, and for linear moves an IK solution
(the controller's kine_inverse) that does not swing J1 / J4 / J6 around. Moves wait until the arm is there,
and return early with an error when STOP was pressed or the controller stopped the arm.
python robot/motion.py # joints, TCP, named poses
python manipulation/arm_route.py goto look # to a named pose: only along the pose graph, checked
2026-10-21 15:19:07 +09:00
python robot/motion.py teach ready # save the arm's current joints as a named pose (move it in Cobo π first)
python robot/motion.py line 0 0 30 # straight TCP move by dx dy dz mm (base frame), slow
python robot/motion.py stop
Poses taught with `teach` are kept in arm_poses.json next to this file.
"""
import json
import sys
import threading
import time
from pathlib import Path
from robot import arm
ROOT = Path(__file__).resolve().parents[1] # the project folder
POSES_FILE = ROOT / "data" / "arm_poses.json"
TUCK = [179.3, -88.6, -13.9, -15.8, -14.5, -121.3] # deg, arm folded beside the column (fits doorways)
LIMITS = [360, 125, 130, 360, 120, 360] # deg, ± factory range per joint (docs/01_HARDWARE.md)
LIMIT_MARGIN = 5.0 # deg kept away from every limit
JOINT_SPEED = {"slow": 10.0, "normal": 25.0, "route": 40.0} # deg/s; route: empty-hand pose-graph moves (estimate)
JOINT_ACCEL = {"slow": 30.0, "normal": 75.0, "route": 200.0} # deg/s2 (vendor: keep under 720); route: estimate
2026-10-21 15:19:07 +09:00
LINEAR_SPEED = {"slow": 20.0, "normal": 50.0} # mm/s
ACCEL_FACTOR = 3.0 # linear moves: accel = speed x this (per s)
2026-10-21 15:19:07 +09:00
MAX_FLIP = 90.0 # deg; an IK answer turning J1 / J4 / J6 more than this is refused
MAX_TRAVEL = 200.0 # deg; a joint move turning any joint further is refused
FULL_TURN = (0, 3, 5) # J1, J4, J6: ±360 joints; a target and target ± 360 are the same pose
AT_JOINT_DEG = 0.3 # arrived when every joint is this close ...
AT_TCP_MM = 1.0 # ... or the TCP this close (linear moves)
POLL_S = 0.1
EXTRA_S = 10.0 # s allowed on top of 3x the planned move time
STILL_S = 2.0 # s without joint motion (and not there) -> the move ended short
STILL_DEG = 0.05 # joint change that counts as motion
BLIND_S = 1.0 # s without a valid state reply while moving -> stop the arm
_stopped = threading.Event() # set by stop(): a waiting move gives up
class MotionError(RuntimeError):
pass
def _read(name, key):
"""A value read from the controller. It now and then answers with another command's reply (get_data or
get_robot_state to get_joint_pos, 2026-10-16): ask again."""
for _ in range(arm.RETRIES):
r = arm.cmd(name)
if key in r:
return r[key]
time.sleep(arm.RETRY_S)
raise MotionError(f"{name} failed: {r.get('errorMsg') or r.get('cmdName') or r}")
def joints():
return _read("get_joint_pos", "joint_pos")
def tcp():
return _read("get_tcp_pos", "tcp_pos")
def poses():
"""Named poses: tuck plus everything taught."""
try:
taught = json.loads(POSES_FILE.read_text())
except FileNotFoundError:
taught = {}
return {"tuck": TUCK, **taught}
def teach(name):
"""Save the current joints as pose `name`."""
if name == "tuck":
raise MotionError("tuck is fixed in motion.py")
check(j := [round(v, 2) for v in joints()])
taught = {k: v for k, v in poses().items() if k != "tuck"}
taught[name] = j
POSES_FILE.write_text(json.dumps(taught, indent=1))
return j
def check(target):
"""Raise MotionError if a joint target is out of range (with LIMIT_MARGIN)."""
if len(target) != 6:
raise MotionError(f"need 6 joints, got {len(target)}")
for i, (v, lim) in enumerate(zip(target, LIMITS)):
if abs(v) > lim - LIMIT_MARGIN:
raise MotionError(f"J{i + 1} {v:.1f} deg is beyond ±{lim - LIMIT_MARGIN:.0f}")
def _ready():
st = arm.cmd("get_robot_state")
if not (st.get("power") and st.get("enable")):
raise MotionError("arm is not powered / enabled")
if arm.program_state() != "idle":
raise MotionError("a Cobo π program is running")
def _wait(arrived, planned_s):
"""Wait while the arm moves. Big joint moves run slower than speed / travel suggests (measured: 157 deg
of J6 at 10 deg/s took well over 21 s), so the limit is generous; a move that stops short is caught by
STILL_S instead."""
t0 = last_move = last_seen = time.time()
last = None
while time.time() - t0 < 3 * planned_s + EXTRA_S:
if _stopped.is_set():
raise MotionError("stopped")
try:
d = arm.cmd("get_data")
except (ConnectionError, OSError):
d = {}
if "joint_actual_position" not in d: # the controller now and then answers a poll with nothing
if time.time() - last_seen > BLIND_S:
stop()
raise MotionError("lost the arm state while moving - stopped")
time.sleep(POLL_S)
continue
last_seen = time.time()
if d.get("protective_stop") or d.get("emergency_stop"):
raise MotionError("the controller stopped the arm (protective / emergency stop)")
if d.get("inpos") and arrived(d):
return d
j = d["joint_actual_position"]
if last is None or max(abs(a - b) for a, b in zip(j, last)) > STILL_DEG:
last, last_move = j, time.time()
elif time.time() - last_move > STILL_S and time.time() - t0 > STILL_S + 1:
raise MotionError("the arm stopped before the target")
time.sleep(POLL_S)
stop()
raise MotionError(f"not there after {time.time() - t0:.0f} s - stopped")
def nearest(target, start):
"""For the ±360 joints use target or target ± 360, whichever is closest to where the joint is now:
the same pose, without spinning the arm a full turn around the column."""
out = list(target)
for i in FULL_TURN:
lim = LIMITS[i] - LIMIT_MARGIN
options = [v for v in (target[i] - 360, target[i], target[i] + 360) if abs(v) <= lim]
out[i] = min(options, key=lambda v: abs(v - start[i]))
return out
def move_joints(target, speed="slow", wait=True):
"""Joint move to absolute angles (deg). Returns the joints reached (or the target if wait=False)."""
return move_path([(target, speed, 0.0)], wait)
def move_path(moves, wait=True):
"""Joint moves [(joints, speed, tol)] sent one after another without waiting between them: the controller queues
them and starts each when the one before ends (docs/04 §10). `tol` is the controller's transition radius into the
next move (it blends through that via-point instead of stopping); the last move always ends exactly. All moves
are checked before the first is sent. Waits for the last one; stop() ends the queue. Returns the joints reached
(or the last target if wait=False)."""
for t, _, _ in moves:
check(t)
2026-10-21 15:19:07 +09:00
_ready()
prev, planned, seq = joints(), 0.0, []
for t, speed, tol in moves:
t = nearest(t, prev)
for i, (a, b) in enumerate(zip(t, prev)):
if abs(a - b) > MAX_TRAVEL:
raise MotionError(f"J{i + 1} would turn {a - b:+.0f} deg - refused")
planned += max(abs(a - b) for a, b in zip(t, prev)) / JOINT_SPEED[speed]
seq.append((t, speed, tol))
prev = t
2026-10-21 15:19:07 +09:00
_stopped.clear()
for k, (t, speed, tol) in enumerate(seq):
extra = {"tol": tol} if tol > 0 and k < len(seq) - 1 else {} # no tol key at all on an exact move
r = arm.cmd("joint_move", relFlag=0, jointPosition=list(t), speed=JOINT_SPEED[speed],
accel=JOINT_ACCEL[speed], **extra)
if _stopped.is_set(): # STOP pressed while sending: stop again, after this move is queued
stop()
raise MotionError("stopped")
if r.get("errorCode") != "0":
if k:
stop() # the moves already queued must not run on alone
raise MotionError(f"joint_move refused: {r.get('errorMsg') or r}")
target = seq[-1][0]
2026-10-21 15:19:07 +09:00
if not wait:
return list(target)
d = _wait(lambda d: max(abs(a - b) for a, b in zip(d["joint_actual_position"], target)) < AT_JOINT_DEG, planned)
2026-10-21 15:19:07 +09:00
return d["joint_actual_position"]
def go(name, speed="slow", wait=True):
p = poses()
if name not in p:
raise MotionError(f"no pose {name!r} (known: {', '.join(p)})")
return move_joints(p[name], speed, wait)
def move_linear(target=None, delta=None, speed="slow", wait=True, path_checked=False):
"""Straight TCP move to `target` [x, y, z mm, rx, ry, rz deg] or by `delta` (dx, dy, dz mm), base frame.
The end pose is checked with the controller's IK first. `path_checked`: the caller checked the whole line in
small steps (fetch._line_rows), so a big J1 / J4 / J6 turn over it is a real one, not another IK branch."""
now_j, now_p = joints(), tcp()
if target is None:
target = [now_p[i] + (delta[i] if i < len(delta) else 0.0) for i in range(3)] + list(now_p[3:])
for _ in range(3): # the controller now and then answers "0" without the joints: ask again
ik = arm.cmd("kine_inverse", jointPosition=now_j, cartPosition=list(target))
if "jointPosition" in ik or ik.get("errorCode") not in ("0", None):
break
time.sleep(0.2)
if "jointPosition" not in ik:
raise MotionError(f"no IK solution for {[round(v, 1) for v in target]}: {ik.get('errorMsg') or ik}")
end_j = ik["jointPosition"][:6] # some answers carry a 7th (external axis) value
check(end_j)
for i in () if path_checked else (0, 3, 5):
if abs(end_j[i] - now_j[i]) > MAX_FLIP:
raise MotionError(f"J{i + 1} would turn {end_j[i] - now_j[i]:+.0f} deg - refused")
_ready()
mm_s = LINEAR_SPEED[speed]
dist = sum((a - b) ** 2 for a, b in zip(target[:3], now_p[:3])) ** 0.5
_stopped.clear()
r = arm.cmd("moveL", relFlag=0, cartPosition=list(target), speed=mm_s, accel=mm_s * ACCEL_FACTOR)
if r.get("errorCode") != "0":
raise MotionError(f"moveL refused: {r.get('errorMsg') or r}")
if not wait:
return list(target)
d = _wait(lambda d: sum((a - b) ** 2 for a, b in zip(d["actual_position"][:3], target[:3])) ** 0.5 < AT_TCP_MM,
dist / mm_s)
return d["actual_position"]
def stop():
"""Stop any arm motion (also a running Cobo π program)."""
_stopped.set()
for _ in range(2): # the controller stops the arm but sometimes closes the connection without a reply
try:
return arm.cmd("stop_program")
except ConnectionError:
pass
return {"sent": True, "reply": None}
if __name__ == "__main__":
a = sys.argv[1:]
try:
if not a:
print("joints", [round(v, 2) for v in joints()])
print("tcp ", [round(v, 1) for v in tcp()])
for k, v in poses().items():
print(f"pose {k:8s}", v)
elif a[0] == "teach":
print(a[1], teach(a[1]))
elif a[0] == "line":
print(move_linear(delta=[float(v) for v in a[1:4]]))
elif a[0] == "stop":
print(stop())
else:
sys.exit(__doc__)
except KeyboardInterrupt:
print(stop())
except MotionError as e:
sys.exit(f"refused / failed: {e}")