Files
Lumi/tests/test_reach.py
T

213 lines
12 KiB
Python

"""Self-check of the arm's reach planning with the offline kinematics (robot/kinematics.py), no robot:
python tests/test_reach.py
Cups the arm reaches from where Lumi stands are planned, from any grasp turn and IK seed; cups beside it are reached
by a waist turn first; cups out of reach of every waist turn still go to the base move / asking. The positions of the
live cases are from logs/api.log (2026-10-2x): the one picked, and the ones refused then."""
import math
import numpy as np
from robot import kinematics, motion
from manipulation import arm_route, skills
from tasks import fetch
# ---------- the offline kinematics: the controller's own answers ----------
SAMPLES = ( # joints, the flange pose the controller reported (calibration
([87.074, -80.539, -21.134, 0.608, -113.653, -47.948], # samples of the torso and the head camera; the samples
[47.777, 320.575, -20.839, 86.777, 45.757, -57.042]), # themselves stay on the Thor)
([69.322, -78.233, -38.969, -5.131, -100.011, -69.562],
[6.08, 270.561, -123.566, 77.877, 51.571, -62.655]),
([101.531, -72.825, 6.01, 5.321, -102.275, -53.297],
[174.924, 414.109, 93.127, 72.719, 64.842, -115.343]),
([-272.27, -100.091, -8.001, 234.453, 83.508, 134.594],
[-9.389, 400.198, 107.353, 31.332, 18.372, -113.47]),
([-260.075, -97.103, -25.513, 228.746, 69.866, 129.55],
[-66.288, 354.894, 170.458, 37.788, 29.511, -109.309]),
(skills.TEACH["joints"], skills.TEACH["flange"])) # the taught grasp
for j, p in SAMPLES:
T = kinematics.fk(j)
R = skills.calib.rpy_matrix(*p[3:])
assert np.linalg.norm(T[:3, 3] - p[:3]) < 0.5 and np.degrees(np.arccos(min(1.0, (np.trace(T[:3, :3].T @ R) - 1) / 2))) < 0.1, j
HEAD = np.array([-92.97, -19.87, 356.12]) # the head's turning centre (vision/head_cam.py fit 2026-10-19)
arm_route.head_cam.head_centre = lambda: HEAD
for name, j in motion.poses().items(): # IK from a seed 8 deg off: the same flange pose
p = kinematics.pose(j)
s = kinematics.ik(p, [a + 8.0 for a in j])
assert s is not None and np.allclose(kinematics.pose(s)[:3], p[:3], atol=0.05), name
assert kinematics.ask("kine_inverse", jointPosition=[0.0] * 6, cartPosition=[3000.0, 0, 0, 0, 0, 0])["errorCode"] != "0"
# ---------- picks from look, offline IK, every check fetch uses (carry line, head, joint limits) ----------
skills._ask = kinematics.ask
LOOK = motion.poses()["look"]
skills._joints = lambda: list(LOOK)
lines = []
fetch.log = skills.log = arm_route.log = lambda *a: lines.append(" ".join(str(x) for x in a))
cup = lambda x, y, top=-260.0: {"name": "cup", "colour": "grey", "x": float(x), "y": float(y), "top_mm": top,
"width_mm": 76.0, "seen": 8, "spread": 1.0}
reach = lambda o: skills.reachable_pick(o, accept=fetch._carry_ok)
trusted = lambda o: skills.reachable_pick(o, accept=fetch._carry_ok, max_yaw=skills.TRUSTED_YAW_DEG) # fetch's first try
plan = reach(cup(-96, 598)) # picked live 2026-10-2x 15:39 (J5 +20, no turn)
assert plan and abs(skills.yaw_of(plan[0][1][1])) < 1, plan
for xy in ((-171, 697), (-395, 629), (-480, 652)): # refused live from where it stood: still out of reach
assert reach(cup(*xy)) is None, xy # of the arm alone at waist 0 (base moved then)
# 2026-10-26 18:40 at lift 0: (260, 722) top -186 was planned turned -30 deg, J5 +26 (missed live, as every turn
# over 15 deg): offline the same; within the trusted turns there is none from here (the waist or the base first)
plan = reach(cup(260, 722, -186))
assert plan and round(skills.yaw_of(plan[0][1][1])) == -30 and 20 < plan[1][4] < 30, plan
assert skills.reachable_pick(cup(260, 722, -186), accept=fetch._carry_ok, max_yaw=skills.TRUSTED_YAW_DEG) is None
# 17:57 / 18:45: (-104, 694) and (-99, 738) top -186: every straight line to carry refused (a joint jumps); straight up
# and one joint move to carry takes the cup there instead (upright, clear of the head)
def line_ok(w, ja): # the straight line alone (fetch._carry_ok also takes carry_up)
try:
fetch._line_rows(kinematics.pose(motion.poses()["carry"]), "to carry", start=(w[0][1], ja))
return True
except fetch.FetchError:
return False
for xy in ((-104, 694), (-99, 738)):
assert skills.reachable_pick(cup(*xy, -186), accept=line_ok) is None, xy
plan = skills.reachable_pick(cup(*xy, -186), accept=lambda w, ja: skills.carry_up(w[0][1], ja) is not None)
assert plan, xy
via, carry = skills.carry_up(plan[0][0][1], plan[1])
assert via[2] > plan[0][0][1][2] and np.allclose(kinematics.pose(carry)[:3], kinematics.pose(motion.poses()["carry"])[:3], atol=0.1)
plan = skills.reachable_pick(cup(-104, 694, -186), accept=lambda w, ja: skills.carry_up(w[0][1], ja) is not None,
max_yaw=skills.TRUSTED_YAW_DEG)
assert plan and abs(skills.yaw_of(plan[0][1][1])) <= 15.5, plan
# a held IK answer with J4 one turn round (the same pose) is not a joint-limit or flip refusal
real_ask = skills._ask
def turned_j4(name, **a):
r = real_ask(name, **a)
if name == "kine_inverse" and r["errorCode"] == "0":
r["jointPosition"][3] += 360.0 if r["jointPosition"][3] < 0 else -360.0
return r
skills._ask = turned_j4
assert reach(cup(-96, 598))
skills._ask = real_ask
# the joint move out to above the cup is checked against the head: a head on the way refuses it
real_centre = arm_route.head_cam.head_centre
start, ja = list(LOOK), plan[1]
assert skills.move_ok(start, ja)
mid = kinematics.pose([(a + b) / 2 for a, b in zip(start, motion.nearest(ja, start))])
arm_route.head_cam.head_centre = lambda: np.array(mid[:3])
assert not skills.move_ok(start, ja)
arm_route.head_cam.head_centre = lambda: None # the head's place unknown: no move
assert not skills.move_ok(start, ja)
arm_route.head_cam.head_centre = real_centre
# ---------- waist turns: the arm reaches round the column ----------
C = arm_route.head_cam.head_centre()[:2]
body = {"waist": -20.31} # the waist of measure-axis = the front
fetch.head.status = lambda: [75.0, body["waist"], 0.0, -5.0]
around = lambda r, b: cup(C[0] - r * math.sin(math.radians(b)), C[1] + r * math.cos(math.radians(b)))
o = cup(-300, 400)
t = fetch.turned(o, 30.0) # turned about the column: same distance from it
assert np.isclose(np.hypot(*(np.subtract((t["x"], t["y"]), C))), np.hypot(*(np.subtract((o["x"], o["y"]), C))))
assert t["x"] > o["x"] # waist left: a thing on the left comes ahead
assert np.allclose([fetch.turned(t, -30.0)[k] for k in "xy"], [o[k] for k in "xy"])
for r, b in ((600, 60), (600, -60), (550, 90), (600, -100), (730, 7), (720, 25)): # beside it and the live ones
o = around(r, b)
assert trusted(o) is None, (r, b) # not from here ...
turns = fetch.waist_for(o)
assert turns and any(trusted(fetch.turned(o, d)) for d in turns), (r, b, turns) # ... after a waist turn
assert abs(turns[0] - (b - math.degrees(math.atan2(-(fetch.PICK_ZONE_CENTRE[0] - C[0]),
fetch.PICK_ZONE_CENTRE[1] - C[1])))) < 1e-6
for r, b in ((1000, 0), (950, 40)): # too far for any waist turn: the base has to move
o = around(r, b)
assert all(trusted(fetch.turned(o, d)) is None for d in fetch.waist_for(o)), (r, b)
# the live cases of 2026-10-26 with trusted turns only: from here, after a waist turn, or the base moves (none)
LIVE = (((262, 774, -189), "base"), ((267, 763, -188), "base"), ((260, 722, -186), "base"), # missed at -45 / -30
((300, 621, -191), "waist"), ((-99, 738, -181), "waist"), # missed at -30 / carry line jumped at -30
((-141, 869, -186), "base"), ((-117, 960, -186), "base"), # across the table / far
((-104, 694, -186), "here"), ((-73, 588, -186), "here"), ((-96, 598, -260), "here")) # carried / picked
for (x, y, top), how in LIVE:
o = cup(x, y, top)
got = "here" if trusted(o) else "waist" if any(trusted(fetch.turned(o, d)) for d in fetch.waist_for(o)) else "base"
assert got == how, ((x, y, top), got, how)
body["waist"] = 100.0 # the waist near its limit (140): no turn past it
assert all(body["waist"] + d <= 140.0 for d in fetch.waist_for(around(600, 80)))
body["waist"] = -20.31
# ---------- _reach: a waist turn before any base move; a cup no turn reaches still moves the base ----------
calls = []
fetch._step = lambda name, say=None, *a: calls.append(("step", name))
fetch._pose = lambda name, *a, **k: None
fetch._charging = lambda: False
fetch._other_here = lambda task, obj: None
fetch.arm_yaw = lambda: -95.5
fetch.remember = lambda obj, yaw: {}
fetch.agv.pose = lambda: (0.0, 0.0, 0.0)
def waist_to(rel):
calls.append(("waist", round(rel)))
body["waist"] = -20.31 + rel
return True
fetch._waist_to = waist_to
scene = {"cup": None} # the cup where it stands, in the arm frame at waist 0
skills.look = lambda *a, **k: [fetch.turned(scene["cup"], body["waist"] + 20.31)]
class Moved(Exception):
pass
def approach(obj, yaw, tried, **kw):
calls.append(("base",))
raise Moved()
fetch._approach = approach
task = {"object": "cup", "colour": None}
scene["cup"] = around(600, 70) # on the left: the waist turns, no base move
got = fetch._reach(task, scene["cup"], None)
assert got and ("base",) not in calls and calls[0] == ("step", "turning my waist to reach it"), calls
assert any(c[0] == "waist" for c in calls) and got[0]["x"] == fetch.turned(scene["cup"], body["waist"] + 20.31)["x"]
calls.clear()
body["waist"] = -20.31
scene["cup"] = around(1000, 0) # too far: no waist turn reaches it, the base moves
try:
fetch._reach(task, scene["cup"], None)
raise AssertionError("no base move")
except Moved:
pass
assert ("base",) in calls and not any(c[0] == "waist" for c in calls), calls
calls.clear()
scene["cup"] = around(600, -70) # the cup was moved away while the waist turned
skills.look = lambda *a, **k: []
assert fetch._waist_reach(task, scene["cup"], None) is None and any(c[0] == "waist" for c in calls)
# ---------- put down: where no joint move from look keeps the cup upright, a straight line to above the spot does
skills._held.clear()
skills._held.update(top_mm=-300.0, yaw=0.0, tips=False, below=40.0)
opts = list(skills.place_options(200.0, 500.0, -300.0, list(LOOK)))
assert opts and all(w[0][0] == "above place (straight)" for w, _ in opts), [w[0][0] for w, _ in opts]
way, ja = opts[0]
assert np.allclose(kinematics.pose(ja)[:3], way[0][1][:3], atol=0.05)
moved = []
real_lin, real_jm, real_place = skills.motion.move_linear, skills.motion.move_joints, skills._do_place
skills.motion.move_linear = lambda **k: moved.append(("line", k["target"]))
skills.motion.move_joints = lambda j, s: moved.append(("joints", j))
skills._do_place = lambda w: moved.append(("down",))
skills.place_turned(way, ja)
assert moved[0] == ("line", way[0][1]) and moved[1] == ("down",), moved
skills.motion.move_linear, skills.motion.move_joints, skills._do_place = real_lin, real_jm, real_place
# ---------- the controller confirms only the chosen plan (a stand-in behind arm.cmd here) ----------
asked, shift = [], {"mm": 0.0}
def controller(name, **a):
asked.append(name)
r = kinematics.ask(name, **a)
if name == "kine_forward":
r["cartPosition"][0] += shift["mm"]
return r
real_cmd = skills.arm.cmd
skills.arm.cmd = skills._ARM_CMD = controller
skills._ask = skills._ASK # planning offline, only confirm() asks the "controller"
skills.home_joints = lambda: list(LOOK)
assert reach(cup(-96, 598)) and sorted(asked) == ["kine_forward", "kine_inverse"], asked
shift["mm"] = 5.0 # the controller disagrees with the offline model: no plan
assert reach(cup(-96, 598)) is None and any("the controller puts" in l for l in lines)
skills.arm.cmd = skills._ARM_CMD = real_cmd
print("reach ok")