Arm moves only along checked poses; any marker name of the active map

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-21 16:54:22 +09:00
co-authored by Claude Opus 5.5
parent 775b32bc52
commit a50fdc9c0d
19 changed files with 880 additions and 277 deletions
+140
View File
@@ -0,0 +1,140 @@
"""Self-check of the pose graph (manipulation/arm_route.py) with a fake arm and toy kinematics (no robot):
python tests/test_arm_route.py"""
import tempfile
from pathlib import Path
import numpy as np
from manipulation import arm_route, skills
from robot import arm, gripper, motion
from tasks import fetch
from vision import head_cam
fetch.STATE_FILE = Path(tempfile.mkdtemp()) / "lumi_task.json" # never the real /dev/shm file
POSES = {"look": [0.0] * 6, "carry": [20.0, 0, 0, 0, 0, 0], "give": [40.0, 0, 0, 0, 0, 0],
"wave_start": [0.0, -20, 0, 0, 0, 0], "wave": [0.0, -20, 20, 0, 0, 0], "ready": [0.0, 0, -20, 0, 0, 0],
"tuck": [-20.0, 0, 0, 0, 0, 0]}
FAR = np.array([0.0, 0.0, 5000.0]) # the head far away: nothing near it
head = {"centre": FAR}
motion.poses = lambda: dict(POSES)
head_cam.head_centre = lambda: head["centre"]
gripper.state = lambda: {"holding": False, "ai": [160]}
arm_route.log = fetch.log = lambda *a: None
class FakeArm:
"""Joints in deg; toy forward kinematics: flange at 30 mm per degree of J1 / J2 / J3, never turned."""
def __init__(self, joints):
self.j, self.sent, self.programs = list(joints), [], []
def __call__(self, name, **a):
if name == "get_robot_state":
return {"power": 1, "enable": 1, "errorCode": "0"}
if name == "get_program_state":
return {"programState": "idle", "errorCode": "0"}
if name == "get_joint_pos":
return {"joint_pos": list(self.j), "errorCode": "0"}
if name == "kine_forward":
j = a["jointPosition"]
return {"cartPosition": [30 * j[0], 30 * j[1], 30 * j[2]] + list(skills.GRASP_RPY), "errorCode": "0"}
if name == "get_data":
return {"joint_actual_position": list(self.j), "inpos": True}
if name == "joint_move":
self.sent.append(list(a["jointPosition"]))
self.j = list(a["jointPosition"])
return {"errorCode": "0"}
if name in ("get_loaded_program", "load_program", "play_program"):
self.programs.append(name)
return {"errorCode": "0", "programName": ""}
raise AssertionError(f"unexpected arm command {name}")
def use(j):
arm.cmd = FakeArm(j)
return arm.cmd
def refused(reason, fn, *a, **k):
try:
fn(*a, **k)
except arm_route.RouteError as e:
assert e.args == reason, e.args
return
raise AssertionError(f"moved, expected {reason}")
edges = {frozenset(e) for e in arm_route.EDGES}
# ---------- every pair of poses: along the graph's edges, one joint move each ----------
for a in POSES:
for b in POSES:
fake = use(POSES[a])
route = arm_route.goto(b)
assert [a] + route == arm_route._path(a, b, POSES) and fake.sent == [POSES[n] for n in route], (a, b, route)
assert all(frozenset(p) in edges for p in zip([a] + route, route)), (a, b, route)
assert arm_route.where(fake.j) == b
use(POSES["give"])
assert arm_route.goto("look") == ["carry", "look"] # after giving: give -> carry -> look
assert arm_route.goto("give") == ["carry", "give"] # to give: look -> carry -> give
use(POSES["wave"])
assert arm_route.goto("tuck") == ["wave_start", "look", "tuck"] # fold: through look
# ---------- after a wave the arm stays up; the next pick goes wave -> wave_start -> look first ----------
fake = use(POSES["wave"])
skills._prepare()
assert fake.sent == [POSES["wave_start"], POSES["look"]], fake.sent
fake = use(POSES["wave"])
fetch._pose("look")
assert fake.sent == [POSES["wave_start"], POSES["look"]], fake.sent
# ---------- an unknown pose near a known one: a checked joint move to it, then on ----------
near = [3.0, 2.0, -4.0, 5.0, 0.0, 0.0] # 5 deg from look (free drive)
assert arm_route.where(near) is None
fake = use(near)
assert arm_route.goto("carry") == ["look", "carry"] and fake.sent == [POSES["look"], POSES["carry"]], fake.sent
fake = use([20.5, 0, 0, 0, 0, 0]) # within EXACT_DEG of carry: straight on
assert arm_route.goto("give") == ["give"]
# ---------- far from every pose, or a move that passes the head: refused, nothing sent ----------
fake = use([60.0, 60.0, 0, 0, 0, 0])
refused(arm_route.UNKNOWN, arm_route.goto, "look")
try:
fetch._pose("look")
raise AssertionError("fetch moved from an unknown pose")
except fetch.FetchError as e:
assert e.args == arm_route.UNKNOWN
assert fake.sent == []
fake = use([60.0, 60.0, 0, 0, 0, 0]) # a task's own way back from the table: straight
assert arm_route.goto("look", leaf=True) == ["look"]
head["centre"] = np.array([300.0, 0.0, 0.0]) # on the way look -> carry (flange x 0 -> 600)
for start in (POSES["look"], near, POSES["give"]):
fake = use(start)
refused(arm_route.NEAR_HEAD, arm_route.goto, "give" if start != POSES["give"] else "look")
assert fake.sent == [], fake.sent
fake = use(POSES["look"])
assert arm_route.goto("wave") == ["wave_start", "wave"] # another edge: clear of it
head["centre"] = None # the head not measured: nothing moves
refused(arm_route.UNSURE, arm_route.goto, "look")
head["centre"] = FAR
POSES["give"] = [40.0, 0, 0, 0, 125.0, 0] # beyond J5's limit
fake = use(POSES["look"])
refused(arm_route.BLOCKED, arm_route.goto, "give")
assert fake.sent == []
POSES["give"] = [40.0, 0, 0, 0, 0, 0]
del POSES["wave_start"]
refused(arm_route.NOT_TAUGHT, arm_route.goto, "wave") # no way to wave without wave_start
refused(arm_route.NOT_TAUGHT, arm_route.goto, "nowhere")
POSES["wave_start"] = [0.0, -20, 0, 0, 0, 0]
# ---------- Cobo π programs only from look; hand_shaking never ----------
fake = use(POSES["carry"])
refused(arm_route.NOT_AT_LOOK, arm_route.start_program, "arm_reach.jks")
fake = use(near)
refused(arm_route.NOT_AT_LOOK, arm_route.start_program, "arm_reach.jks")
fake = use(POSES["look"])
refused(arm_route.RETIRED, arm_route.start_program, "hand_shaking.jks")
assert fake.programs == []
arm_route.start_program("arm_reach.jks")
assert "play_program" in fake.programs
print("arm route ok")
+28 -26
View File
@@ -233,17 +233,15 @@ except fetch.FetchError:
st = run(fetch.place)
assert st["status"] == "done" and not st["holding"] and ("place",) in calls and poses()[-1] == "look", (st, calls)
# ---------- arm speed: empty hand faster, with the object slow ----------
# ---------- arm speed: every move along the pose graph at arm_route.SPEED, empty hand or holding ----------
speeds = []
real_go = fetch.motion.go
fetch.motion.go = lambda name, speed="slow", *a, **k: (speeds.append((name, speed)), calls.append(("pose", name)))
arm.update(j=[5.0] * 6)
fetch._pose("look")
fetch.motion.poses = lambda: {"tuck": [0] * 6, "ready": [0] * 6, "carry": [3] * 6, "look": [0] * 6}
arm.update(j=[0.5] * 6)
fetch._pose("carry")
tilt["rpy"] = up_rpy
fetch._pose_holding("carry")
assert speeds == [("look", skills.EMPTY_SPEED), ("carry", skills.HOLDING_SPEED)], speeds
assert skills.EMPTY_SPEED != skills.HOLDING_SPEED
fetch._pose("carry", holding=True)
assert speeds == [("carry", fetch.arm_route.SPEED)] * 2 and fetch.arm_route.SPEED == "slow", speeds
fetch.motion.go = real_go
# ---------- a drive that turns on the spot without getting anywhere is cancelled ----------
@@ -753,9 +751,11 @@ fetch.GIVE_WAIT_S = 0.3
reset()
st = run(fetch.start, ok)
assert st["status"] == "holding", st
calls.clear()
st = run(fetch.give) # nobody says thank you: kept
assert st["status"] == "holding" and st["holding"] and ("open",) not in calls, (st, calls)
assert calls.count(("line", "straight")) == 2 and "ready" not in poses() # carry -> give -> carry, straight
assert calls.count(("line", "straight")) == 4 and poses() == ["carry", "give", "carry", "look"], calls # look ->
# carry -> give -> carry -> look, straight
fetch.GIVE_WAIT_S = 5.0
threading.Timer(0.2, fetch.release_now).start() # "thank you" / Release
st = run(fetch.give)
@@ -792,32 +792,34 @@ assert ("cancel",) in calls
tilted = calib.rpy_matrix(30, 0, 0) @ calib.rpy_matrix(*up_rpy) # turned 30 deg about base x
ry = math.degrees(math.asin(-tilted[2, 0]))
tilt["rpy"] = [math.degrees(math.atan2(tilted[2, 1], tilted[2, 2])), ry, math.degrees(math.atan2(tilted[1, 0], tilted[0, 0]))]
assert abs(fetch.tilt_along("carry") - 30) < 0.5, fetch.tilt_along("carry")
arm.update(j=[0.5] * 6) # at look: the route is look -> carry
calls.clear()
try:
fetch._pose_holding("carry")
fetch._pose("carry", holding=True)
raise AssertionError("tilted move accepted")
except fetch.FetchError:
assert not calls
except fetch.FetchError as e:
assert not calls and e.args == fetch.arm_route.SPILL, (calls, e.args)
tilt["rpy"] = up_rpy
assert fetch.tilt_along("carry") < 1e-6
fetch._pose("carry", holding=True)
assert calls == [("pose", "carry")], calls
# ---------- height check: a pose move must not take the gripper down to the table ----------
z_along = {"look": -500.0} # flange z at the end of the move; it starts at 0
skills._ask = lambda name, **a: {"errorCode": "0", "cartPosition": [0, 0, z_along.get(name_of(a), 0.0)] + up_rpy}
name_of = lambda a: "look" if a["jointPosition"][0] > 0 else None
fetch.motion.poses = lambda: {"tuck": [0] * 6, "ready": [0] * 6, "carry": [3] * 6, "look": [10.0] + [0] * 5}
# ---------- height check: a pose move must not take the gripper down to the table on the way ----------
skills._ask = lambda name, **a: {"errorCode": "0", "cartPosition": [0, 0, -500.0 if 0 < a["jointPosition"][0] < 10 else 0.0]
+ up_rpy}
fetch.motion.poses = lambda: {"look": [0] * 6, "carry": [10.0] + [0] * 5}
arm.update(j=[0.0] * 6)
calls.clear()
try:
fetch._pose("look")
fetch._pose("carry")
raise AssertionError("move down to the table accepted")
except fetch.FetchError:
assert not calls
arm.update(j=[-5.0] * 6)
fetch._pose("ready")
assert calls == [("pose", "ready")], calls
fetch._pose("ready") # there already: no move
assert calls == [("pose", "ready")], calls
except fetch.FetchError as e:
assert not calls and e.args == fetch.arm_route.BLOCKED, (calls, e.args)
skills._ask = lambda name, **a: {"errorCode": "0", "cartPosition": [0, 0, 0] + up_rpy}
fetch._pose("carry")
assert calls == [("pose", "carry")], calls
fetch._pose("carry") # there already: no move
assert calls == [("pose", "carry")], calls
fetch.motion.poses = lambda: {"tuck": [0] * 6, "ready": [0] * 6, "carry": [3] * 6, "look": [0] * 6}
# ---------- place spots: the pick spot only while the base stands where it picked ----------
seen_table = {"spots": None}
skills.on_table = lambda spots, table: (seen_table.update(spots=list(spots), table=table), [s for s in spots if s[1] >= 680])[1]
+118
View File
@@ -0,0 +1,118 @@
"""Self-check of marker names as people say them on any map (assistant/wording.py map_names, brain, steps, the
assistant's live list), with fake marker lists for the office and a conference map, no robot, no LLM:
python tests/test_map_names.py"""
import types
from assistant import brain, lumi, steps, wording
from robot import agv
lumi.LOG_FILE = "/dev/null"
OFFICE = ["home", "dock2", "exit", "final"] + [f"p{i}" for i in range(1, 13)]
CONF = ["home", "start", "end", "init", "final", "middle", "table_front", "stage", "booth_2"]
# ---------- said -> written, with the conference map's names ----------
NORM = {
"go to table front": "go to table_front", "go to the Table-Front": "go to the table_front",
"go to tablefront": "go to table_front", "테이블 프론트로 가": "table_front로 가", "테이블 프런트로": "table_front로",
"go to booth two": "go to booth_2", "go to Booth 2": "go to booth_2", "go to booth-2": "go to booth_2",
"부스 투로 가": "booth_2로 가", "스타트로 가": "start로 가", "엔드에서 컵 가져와": "end에서 컵 가져와",
"미들로 가": "middle로 가", "스테이지로 가": "stage로 가", "이닛으로 가": "init으로 가", "홈으로 가": "home으로 가",
"go to the stagee": "go to the stage", "go to the middel": "go to the middle", # whisper slips, one name
"go to the table fronts": "go to the table_front", "from the stagee to end": "from the stage to end",
# not a marker: unchanged
"테이블에 놔": "테이블에 놔", "restart the song": "restart the song", "스타트업": "스타트업", "go to point": "go to point",
"go to booth 3": "go to booth 3", "go to the left": "go to the left",
}
bad = {t: (wording.norm(t, CONF), w) for t, w in NORM.items() if wording.norm(t, CONF) != w}
assert not bad, bad
# ---------- go to / come back to: the quick rules ----------
GO = {"go to table front": "table_front", "테이블 프론트로 가": "table_front", "go to booth two": "booth_2",
"부스 투로 가 줘": "booth_2", "루미야 스타트로 가": "start", "go to the end": "end", "엔드로 가": "end",
"go back to start": "start", "come back to the stage": "stage", "drive to Middle": "middle", "이닛으로 가": "init",
"파이널로 가": "final", "go to the stagee": "stage", "go home": "home"}
wrong = {t: brain.intent(t, CONF) for t, w in GO.items() if brain.intent(t, CONF) != ("go_to", w)}
assert not wrong, wrong
for t, w in {"go to p3": "p3", "피쓰리로 가": "p3", "go to final": "final", "도크 투로 가": "dock2", "go to 10p point": "p10",
"엑시트로 가": "exit"}.items(): # the office map's names keep working
assert brain.intent(t, OFFICE) == ("go_to", w), (t, brain.intent(t, OFFICE))
for t in ("go to p3", "go to the kitchen", "go to start"): # not on that map: never driven
assert brain.intent(t, OFFICE if t == "go to start" else CONF) is None, t
# ---------- unknown, ambiguous: what Lumi says ----------
en, ko = brain.place_problem("go to p12", CONF)
assert en.startswith("I don't have a place called p12 on this map. I know: ") and "table front" in en, en
assert "p12라는 곳이 없어요" in ko, ko
TWO = CONF + ["booth_1"]
en, ko = brain.place_problem("go to booth", TWO)
assert en.startswith("Did you mean booth ") and "booth 1" in en and "booth 2" in en, en
assert brain.intent("go to booth", TWO) is None and brain.intent("go to booth two", TWO) == ("go_to", "booth_2")
for t in ("go to stage", "go to sleep", "go to the left", "go to the kitchen"): # the LLM answers the others
assert brain.place_problem(t, CONF) is None, t
said = brain.Brain._action("ACTION: go_to kitchen", "go to the kitchen", CONF)
assert said["action"] == "none" and said["say"].startswith("I don't have a place called kitchen"), said
assert brain.Brain._action("ACTION: go_to table_front", "go to the table front", CONF)["target"] == "table_front"
# ---------- missions, fetch to / from, place at a marker ----------
STEPS = {
"go to start, then come back": [{"do": "go", "to": "start"}, {"do": "go", "to": "here"}],
"스타트로 갔다가 돌아와": [{"do": "go", "to": "start"}, {"do": "go", "to": "here"}],
"go to middle and wait 10 seconds then go to end":
[{"do": "go", "to": "middle"}, {"do": "wait", "s": 10.0}, {"do": "go", "to": "end"}],
"미들로 가서 10초 기다렸다가 엔드로 가": [{"do": "go", "to": "middle"}, {"do": "wait", "s": 10.0}, {"do": "go", "to": "end"}],
"go to Table-Front then 홈으로 가": [{"do": "go", "to": "table_front"}, {"do": "go", "to": "home"}],
"go to the stagee and come back": [{"do": "go", "to": "stage"}, {"do": "go", "to": "here"}],
"go stage": [{"do": "go", "to": "stage"}],
"take the white cup to booth two and put it on the table": [
{"do": "pick", "object": "cup", "colour": "white"}, {"do": "go", "to": "booth_2"},
{"do": "place", "on": {"name": "dining table", "colour": None, "side": None}}],
"부스 투 테이블에 놔": [{"do": "go", "to": "booth_2"},
{"do": "place", "on": {"name": "dining table", "colour": None, "side": None}}],
"컵 들고 테이블 프론트로 가서 놔": [{"do": "pick", "object": "cup", "colour": None}, {"do": "go", "to": "table_front"},
{"do": "place", "on": None}],
}
for t, want in STEPS.items():
got, unread, _ = steps.read(t, CONF, llm=False)
assert got == want and not unread, (t, got, unread)
assert steps.read("go to booth and come back", TWO, llm=False)[1] == ["go to booth"] # ambiguous: not guessed
assert lumi.read_mission("go to start, then come back", CONF)[0][0] == {"do": "go", "to": "start"}
FETCH = {"bring the cup from start to table front": ({"marker": "start"}, {"marker": "table_front"}),
"스타트에서 컵 가져와서 부스 투에 놔": ({"marker": "start"}, {"marker": "booth_2"}),
"bring me the cup from the stagee": ({"marker": "stage"}, "here")}
for t, (src, dest) in FETCH.items():
task, _ = brain.parse_fetch(t, CONF)
assert (task["source"], task["dest"]) == (src, dest), (t, task)
assert brain.parse_fetch("bring the cup from the kitchen", CONF)[0]["source"] is None
# ---------- the assistant's list follows the map ----------
maps = {"now": OFFICE}
agv.markers = lambda: list(maps["now"])
assert agv.known_markers(fresh=True) == OFFICE
maps["now"] = CONF # the owner switched the map
assert agv.known_markers() == OFFICE # read again only after MARKERS_FRESH_S ...
agv._known["t"] -= agv.MARKERS_FRESH_S + 1
assert agv.known_markers() == CONF # ... or on a miss (fresh=True)
def offline():
raise ConnectionError("AGV closed the connection")
agv.markers = offline
assert agv.known_markers(fresh=True) == CONF # the base not answering: the last list
agv.markers = lambda: list(maps["now"])
L = lumi.Lumi.__new__(lumi.Lumi)
assert L.markers == CONF
spoken, trips = [], []
lumi.mouth.say = lambda text, **k: spoken.append(text)
L.lang, L.keep_listening, L.mission_running = "en", lambda: None, lambda: False
L.base_off, L.arm_off, L.start_trip = lambda *a: False, lambda: False, trips.append
L.modes, L.brain, L.current, L.voice_until = types.SimpleNamespace(drive=True), types.SimpleNamespace(history=[]), None, 0
assert L.quick_command("go to p12") and spoken[-1].startswith("I don't have a place called p12") and not trips
maps["now"] = OFFICE # a miss reads the list again: p12 is on this map
assert L.quick_command("go to p12") and trips == ["p12"]
maps["now"] = CONF
agv.known_markers(fresh=True)
assert L.quick_command("go to table front") and trips[-1] == "table_front" and spoken[-1] == "Okay, I'm going to table front."
print("map names ok")
+17 -9
View File
@@ -8,7 +8,7 @@ from pathlib import Path
import numpy as np
from assistant import lumi
from manipulation import skills, wave
from manipulation import arm_route, skills, wave
from robot import agv, arm, gripper, motion
from tasks import fetch
from vision import head_cam
@@ -25,7 +25,7 @@ hand = {"holding": False}
gripper.state = lambda: {"holding": hand["holding"]}
base = {"move_status": "idle"}
agv.status = lambda: dict(base)
wave.log = lambda *a: None
wave.log = arm_route.log = lambda *a: None
class FakeArm:
@@ -67,17 +67,20 @@ def refused(reason):
raise AssertionError(f"waved, expected {reason}")
# ---------- from look: along the route, wave, back the same way; every move clear of the head ----------
# ---------- from look: up along the graph, wave, and the arm stays up; every move clear of the head ----------
a = use(FakeArm(LOOK))
started = []
wave.wave(on_start=lambda: started.append(len(a.sent)))
assert started == [0] # said before the first move
top = POSES["wave"]
up, down = top[:4] + [top[4] + wave.WAVE_DEG] + top[5:], top[:4] + [top[4] - wave.WAVE_DEG] + top[5:]
assert a.sent == [POSES["wave_start"], top] + [up, down] * wave.WAVE_SWINGS + [top, POSES["wave_start"], LOOK], a.sent
assert a.sent == [POSES["wave_start"], top] + [up, down] * wave.WAVE_SWINGS + [top], a.sent
for j0, j1 in zip([LOOK] + a.sent, a.sent):
assert wave.move_clearance(j0, j1, HEAD) >= wave.HEAD_CLEAR_MM, (j0, j1)
assert wave.move_clearance(LOOK, top, HEAD) < wave.HEAD_CLEAR_MM # straight up from look would pass the head
assert arm_route.profile(j0, j1, HEAD)[0] >= arm_route.HEAD_CLEAR_MM, (j0, j1)
assert arm_route.profile(LOOK, top, HEAD)[0] < arm_route.HEAD_CLEAR_MM # straight up from look would pass the head
a.sent.clear()
wave.wave() # asked again while up: only the swings
assert a.sent == [top] + [up, down] * wave.WAVE_SWINGS + [top], a.sent
# ---------- a taught pose whose way passes the head: refused before anything moves ----------
POSES["wave_start"] = [-15.0, 0.0, 40.0, 0.0, 0.0, 0.0]
@@ -99,11 +102,16 @@ refused(wave.HOLDING)
skills.hand_sees = lambda: "unknown"
assert a.sent == []
# ---------- an unknown start (after free drive: the wrist the other way round), carry, give, base, power ----------
# ---------- from carry (empty hand): down to look first, then up ----------
POSES["carry"] = [30.0, 0.0, 0.0, 0.0, 0.0, 0.0]
a = use(FakeArm(POSES["carry"]))
wave.wave()
assert a.sent[:3] == [LOOK, POSES["wave_start"], top], a.sent
del POSES["carry"]
# ---------- an unknown start (after free drive: the wrist the other way round), base, power ----------
a = use(FakeArm([-249.1, -90.9, -29.1, 14.6, -83.8, -33.0]))
refused(wave.UNKNOWN_POSE)
use(FakeArm([-280.6, -86.13, -2.58, -173.3, 85.86, 119.9])) # carry
refused(wave.UNKNOWN_POSE)
use(FakeArm(LOOK, enable=False))
refused(wave.OFF)
base["move_status"] = "running"