Files
Lumi/tests/test_reach_live.py
T

176 lines
11 KiB
Python

"""The live sequence of 2026-10-23 15:21-15:23, replayed with fakes: "give me a hand" / "손 줘" with an empty hand
are arm reach (handshake words are a wave) (the rules, not the LLM's "I can't shake hands"), and a reach whose body move hangs ends at once on
STOP, so the next "give me your hand" starts instead of "I'm busy with another task". And 2026-10-23 17:40-17:43:
"close your hand" on nothing is not holding (the camera saw the person's hand by the held-out arm), "there is nothing
in your hand" checks the hand again, and "okay, thank you" / a timeout folds the held-out arm. No robot.
.venv/bin/python tests/test_reach_live.py
"""
import tempfile
import threading
import time
from pathlib import Path
import no_robot # noqa: F401 any connection to the robot fails the test
from assistant import brain
from tasks import fetch
MARKERS = ["home", "p1"]
REACH = ["Let's give me a hand.", "Let's give me your hand.", "Give me your hand.", "give me a hand", "손 줘",
"손 내밀어", "루미야 손 줘", "Lumi, 손 줘", "hand 줘", "손 좀 줘"]
WAVE = ["shake hands with me", "let's shake hands", "shake my hand", "shake hand", "can you shake hand", "shake",
"give me a handshake", "악수하자", "악수해 줘", "악수해요", "악수 한번 해요", "나랑 악수할래?"] # owner 2026-10-23: a wave
NOT_REACH = ["give me a hand with this", "can you lend me a hand?", "give me a hand to lift this", "I shook hands with him",
"don't shake hands", "악수하지 마", "어제 악수했어", "the handshake was nice", "give me a break"]
bad = [(t, brain.intent(t, MARKERS)) for t in REACH if brain.intent(t, MARKERS) != ("arm", "reach")]
bad += [(t, brain.intent(t, MARKERS)) for t in NOT_REACH if brain.intent(t, MARKERS) == ("arm", "reach")]
bad += [(t, brain.intent(t, MARKERS)) for t in WAVE if brain.intent(t, MARKERS) != ("wave", "")]
bad += [(t, brain.intent(t, MARKERS)) for t in NOT_REACH if brain.intent(t, MARKERS) == ("wave", "")]
bad += [(t, "held rule") for t in REACH if brain.hand_request(t, False, None, MARKERS)] # empty hand: never a give
assert not bad, bad
# ---------- the stuck reach: the body API does not answer a head move ----------
tmp = Path(tempfile.mkdtemp())
fetch.STATE_FILE = tmp / "lumi_task.json"
hung = threading.Event() # never set: the body API's moveto hangs
fetch.head.moveto = lambda pos, **kw: hung.wait(30)
fetch.head.status = lambda: [0.0, 0.0, -22.0, 0.0]
fetch.camview.want = lambda name: None
fetch.skills.cancel = fetch.agv.cancel = lambda: None
fetch.motion.poses = lambda: {"give": [0] * 6, "look": [0] * 6}
fetch.head_ahead = lambda: None
moved = []
fetch._turn = lambda deg, **kw: (fetch._head_to(-30.0, 0.0), True)[1] # the turn's head check (head_hits) hangs
fetch._pose = lambda name: moved.append(name)
fetch._person_point = lambda: ((0.0, 600.0, 0.0), 100.0)
fetch._reach_to_hand = lambda *a: moved.append("hand")
fetch._sweep_blocked = lambda *a, **k: False # never the real base's pose and map (it failed at the charger)
fetch._route_points = lambda seq: []
real_route = (fetch.arm_route.plan, fetch.arm_route.move)
fetch.arm_route.plan = lambda target, holding=False, leaf=False, now=None: [(target, [0] * 6)]
fetch.arm_route.move = lambda seq, speed=None, holding=False: moved.append("route " + seq[-1][0])
fetch.reach(-22.7)
time.sleep(0.3)
assert fetch._busy() and fetch.state()["status"] == "running"
try:
fetch.reach(-22.0) # said again while it really runs: busy is right
raise AssertionError("a second reach started")
except fetch.FetchError as e:
assert "busy" in e.args[0]
t = time.time()
fetch.end() # "stop" / a new command
assert time.time() - t < 1.0 and not fetch._busy(), "stop waited for the hung body call"
assert fetch.state()["status"] == "stopped" and moved == ["route give"] # the arm went with the hung waist turn
moved.clear()
hung.set() # the body answers again
fetch.reach(0.0) # the next "give me your hand": starts, not "busy"
fetch._thread.join(2)
assert moved == ["give", "hand"] and fetch.state()["status"] == "done", (moved, fetch.state()["status"])
fetch.arm_route.plan, fetch.arm_route.move = real_route
# ---------- closed on nothing is empty, whatever the camera sees ----------
grip = {"ai": [4153.0, 489.0], "holding": False} # the live close of 17:40:39
fetch.gripper.state = lambda: dict(grip)
fetch.skills.hand_sees = lambda: "object" # the person's hand by the held-out fingers
assert fetch._in_hand() is False
fetch._set(status="done", holding=True) # the stale "holding" of 17:41
assert fetch.check_hand() is False and not fetch.state()["holding"]
grip.update(ai=[3500.0, 489.0], holding=True) # closed on a cup: held
fetch.skills.hand_sees = lambda: "unknown"
assert fetch._in_hand() is True
# ---------- the person says the hand is empty; "okay, thank you" / a timeout folds a held-out arm ----------
from assistant import lumi
for t in ["There is nothing in your hand.", "your hand is empty", "you're not holding anything", "손에 아무것도 없어",
"손에 아무것도 없잖아요", "손 비었어"]:
assert lumi.HAND_EMPTY_SAID.search(t), t
for t in ["is there anything in your hand?", "what's in your hand", "give me your hand"]:
assert not lumi.HAND_EMPTY_SAID.search(t), t
L = lumi.Lumi.__new__(lumi.Lumi)
folds, status, stops = [], {"now": "done"}, []
L.fetch_status = lambda: status["now"]
L.arm_pose = lambda target: folds.append(target) or True
real_state, real_stop = fetch.state, fetch.stop
fetch.state, fetch.stop = lambda: {"step": "reaching out"}, lambda: stops.append(1)
for text in ["Okay, thank you.", "okay thanks", "고마워", "됐어", "what's the weather?", "go home"]: # any next words
folds.clear()
L.reached_at = time.time()
assert L.reach_done(text) and folds == ["fold"], (text, folds)
L.reached_at, folds[:] = time.time(), []
assert not L.reach_done() and not folds # nothing said yet: the hand stays out
L.reached_at = time.time() - lumi.REACH_HOLD_S - 1 # nobody said anything: the timeout folds
assert L.reach_done() and folds == ["fold"]
status["now"] = "running" # still on its way after REACH_HOLD_S: no timeout
L.reached_at, folds[:] = time.time() - lumi.REACH_HOLD_S - 1, []
assert not L.reach_done() and not folds
status["now"] = "done" # the hand got there only now: the hold counts
fetch.state = lambda: {"step": "", "since": time.time()} # from then, not from the order
assert not L.reach_done() and not folds
fetch.state = lambda: {"step": "reaching out"}
status["now"] = "running" # still going toward their hand: stopped, folded
L.reached_at, folds[:] = time.time(), []
assert L.reach_done("okay thanks") and folds == ["fold"] and stops, (folds, stops)
status["now"] = "failed" # the reach failed (a timeout, 2026-10-30): folded
L.reached_at, folds[:] = time.time(), []
assert L.reach_done("thanks") and folds == ["fold"]
status["now"] = "stopped" # a stop word: the arm stays where it is
L.reached_at, folds[:] = time.time(), []
assert not L.reach_done("thanks") and not folds and not L.reached_at
L.reached_at = 0.0
status["now"] = "done"
assert not L.reach_done("thanks") and not folds # no reach out: "thanks" moves nothing
fetch.state, fetch.stop = real_state, real_stop
# an arm left half way (a reach stopped by a lost state reply) and the waist turned: an order to drive folds the empty
# arm along the checked route, then the waist comes to the front; a conversation's end or a held thing never folds it
# (live 2026-10-28 19:08-19:13: every "come here" said "I can't turn back to the front")
route, waist = [], {"turn": 26.0}
real = (fetch._waist_turn, fetch.state, fetch._at_pose, fetch._waist_to, lumi.arm_route.plan, lumi.arm_route.move)
fetch._waist_turn = lambda: waist["turn"]
fetch._at_pose = lambda name: bool(route)
fetch._waist_to = lambda deg, in_place=False, look=None: waist.update(turn=deg) or True
lumi.arm_route.plan = lambda target, holding=False: [(target, [0] * 6)]
lumi.arm_route.move = lambda seq, speed=None, holding=False: route.append(seq[-1][0])
fetch.state = lambda: {"status": "failed", "holding": False}
assert not lumi.waist_home("the conversation is over") and not route and waist["turn"] == 26.0
assert lumi.waist_home("coming to a person", fold=True) and route == ["look"] and waist["turn"] == 0.0
# something in the hand (live 2026-10-29: "come here" drove with the cup held out in give): to look first along the
# straight held lines (fetch.drive_ready), also with the waist at the front; a conversation's end moves nothing
route.clear(), waist.update(turn=26.0)
fetch.state = lambda: {"status": "stopped", "holding": True}
real_ready = fetch.drive_ready
fetch.drive_ready = lambda: route.append("held look")
assert not lumi.waist_home("the conversation is over") and not route
assert lumi.waist_home("coming to a person", fold=True) and route == ["held look"] and waist["turn"] == 0.0
route.clear()
assert lumi.waist_home("driving", fold=True) and route == ["held look"] # the waist at the front already
fetch.drive_ready = lambda: (_ for _ in ()).throw(fetch.FetchError("I can't move my arm there safely."))
route.clear(), waist.update(turn=26.0)
assert not lumi.waist_home("coming to a person", fold=True) and not route and waist["turn"] == 26.0 # no drive
fetch.drive_ready = real_ready
(fetch._waist_turn, fetch.state, fetch._at_pose, fetch._waist_to, lumi.arm_route.plan, lumi.arm_route.move) = real
# fetch.drive_ready itself: nothing held -> no arm move; held -> the held lines to look (_drive_pose(True)); a task
# running -> refused (it moves the arm itself)
real = (fetch.state, fetch._drive_pose, fetch._busy)
moves = []
fetch._drive_pose = lambda holding=False: moves.append(holding)
fetch._busy = lambda: False
fetch.state = lambda: {"status": "done", "holding": False}
fetch.drive_ready()
assert moves == []
fetch.state = lambda: {"status": "stopped", "holding": True}
fetch._stop.set() # an old stop (the hand-over ended) does not end it
fetch.drive_ready()
assert moves == [True] and not fetch._stop.is_set()
fetch._busy = lambda: True
try:
fetch.drive_ready()
raise AssertionError("drove the arm while a task runs")
except fetch.FetchError:
pass
(fetch.state, fetch._drive_pose, fetch._busy) = real
print(f"reach live ok ({len(REACH) + len(NOT_REACH)} wordings, stop during a hung body move, held state, auto fold)")