"""Self-check of the wave greeting (tasks/wave_greet.py) with a fake arm, head, waist and speech, no robot, and of the waving detector (vision/waving.py) on the recordings of vision/wave_record.py when they are here (tests/data/waves, local only: people in the pictures): python tests/test_wave_greet.py""" import json from pathlib import Path import time import no_robot # noqa: F401 any connection to the robot fails the test from manipulation import arm_route, wave from robot import head, motion from tasks import fetch, wave_greet as g from vision import waving done = [] # what the robot did, in order arm = {"at": "look"} waver = {"w": None} # (bearing, distance, look, face) of the person waving now A, B = [1.0, 0.0, 1.0, 0.0], [0.0, 1.0, 0.0, 1.0] # clothes signatures (vision/reid.py) of two people def W(bearing, look=A): return (bearing, 2.0, look, None) plan_error = {"e": None} POSES = {"look": [0.0] * 6, "wave_out": [1.0] * 6, "wave_start": [2.0] * 6, "wave": [3.0] * 6} def plan(): if plan_error["e"]: raise plan_error["e"] return ([("wave_out", POSES["wave_out"]), ("wave_start", POSES["wave_start"])] if arm["at"] == "look" else [], []) def move_path(moves, wait=True, queued=False): if moves[-1][0] == POSES["wave_start"] and len(moves) == 2 and queued: done.append("swing") else: done.append(f"up {moves[-1][1]}") arm["at"] = "wave_start" def route_move(seq, speed=None, holding=False): done.append(f"down {speed}") arm["at"] = "look" wave.plan = plan motion.poses = lambda: dict(POSES) motion.move_path = move_path arm_route.where = lambda now=None: arm["at"] arm_route.plan = lambda target, holding=False, leaf=False, now=None: [("wave_out", None), ("look", None)] arm_route.move = route_move head.status = lambda: [0.0, 0.0, 0.0, 10.0] head.moveto = lambda pos, **k: done.append(f"head {pos[2]:+.0f}" + (" rest" if pos[3] == head.HEAD_REST_PITCH else "") + (" timeout" if k.get("timeout") != g.HEAD_TIMEOUT_S else "")) fetch._waist_turn = lambda: 0.0 fetch._waist_to = lambda rel, in_place=False, look=None: done.append(f"waist {rel:+.0f} look {look}") or True g._head_yaw = lambda: 0.0 g._too_close = lambda: False g._waver = lambda: waver["w"] said = [] g._say = lambda lines, lang: said.append(lines[0][lang]) switch = {"on": True, "free": True} g._on, g._free = (lambda: switch["on"]), (lambda: switch["free"]) g._off = lambda: switch.update(on=False) g._bg = lambda fn, *args: fn(*args) # the background parts run at once here queue = {"n": 0} # moves the fake controller has queued g.arm.cmd = lambda name, **k: {"queue": queue["n"]} if name == "get_motion_state" else {} motion._wait = lambda *a, **k: done.append("settle") def run(t0, t1, dt=0.5): t = t0 while t <= t1: g._tick(t) t += dt def fresh(): done.clear() said.clear() arm["at"] = "look" g._st.update(up=False, last_wave=0.0, next_joke=0.0, free_at=0.0, refused_until=0.0, waist_out=False, follow_at=0.0, swing_at=0.0, swing_end=0.0) g._met.known.clear() switch.update(on=True, free=True) plan_error["e"] = None # someone waves 30 deg on the left at 2 m: head to them, a greeting, arm up at the route speed, GREET_SWINGS swings, waist to them fresh() waver["w"] = W(30.0) run(100.0, 100.0) assert done == ["head +30", "up route"] + ["swing"] * g.GREET_SWINGS + ["waist +30 look 30.0"], done assert len(said) == 1 and said[0] in sum(g.wave_lines.INTRO, ()), said # someone new: Lumi introduces itself assert g.busy() # they keep waving: swings, and a joke after JOKE_AFTER_S, then not again before JOKE_EVERY_S done.clear() said.clear() run(100.5, 100.0 + g.JOKE_AFTER_S + 1) assert done.count("swing") >= g.JOKE_AFTER_S / g.SWING_S - 1 and "up route" not in done, done # one per SWING_S assert len(said) == 1 and said[0] in sum(g.wave_lines.LONG, ()), said # they move to the other side while waving: the waist turns to them again between swings, not more often than # FOLLOW_EVERY_S done.clear() waver["w"] = W(-40.0) g._st["follow_at"] = 0.0 # (the fake waver stays at +30 after each turn: turned again so far) run(111.5, 111.5 + g.FOLLOW_EVERY_S - 0.5) # (swings end first; the waist turns WAIST_MAX_DEG at most, the head the rest) assert done[:2] == ["settle", "waist -30 look -40.0"] and done.count("waist -30 look -40.0") == 1, done waver["w"] = W(30.0) # never more than one swing queued ahead, even if the controller's queue always read empty g._st["swing_end"], g._st["swing_at"] = 0.0, 0.0 done.clear() for _ in range(10): g._swing_on(1000.0) assert done.count("swing") == 1, done g._st["swing_end"] -= g.SWING_S # it ended: the next is sent g._swing_on(1000.0) assert done.count("swing") == 2, done g._st["swing_end"] = 0.0 # they stop: the hand stays up, no swings; waving again: swings again, no new greeting within NEW_WAVE_S waver["w"] = None done.clear() run(112.0, 115.0) assert done == [], done waver["w"] = W(30.0) said.clear() run(115.5, 116.0) assert done.count("swing") == 1 and said == [], (done, said) # nobody waves for HOLD_S: the waist to the front, the hand stays up (held); not before waver["w"] = None done.clear() run(116.5, 116.0 + g.HOLD_S - 1) assert done == [], done run(116.0 + g.HOLD_S + 0.5, 116.0 + g.HOLD_S + 0.5) assert done == ["waist +0 look None"], done assert not g.busy() and arm["at"] == "wave_start" and g._st["up"] and g._st["held"] # the waist's way back is blocked: the hand stays up there, held fresh() waver["w"] = W(30.0) run(150.0, 150.0) waver["w"] = None blocked = {"n": 1} fetch._waist_to = lambda rel, in_place=False, look=None: done.append(f"waist {rel:+.0f}") or ( blocked.update(n=blocked["n"] - 1) or False if blocked["n"] > 0 else True) done.clear() run(150.5 + g.HOLD_S, 150.5 + g.HOLD_S + g.REFUSED_S * 2.5) assert done == ["waist +0"] and g._st["held"] and g._st["waist_out"], done fetch._waist_to = lambda rel, in_place=False, look=None: done.append(f"waist {rel:+.0f} look {look}") or True # beside a wall the waist turn (cut to WAIST_MAX_DEG) is blocked: it turns as far as its sweep is clear, the head on # the person; the same cut turn is not tried twice fresh() waver["w"] = W(-66.0) fetch._waist_to = lambda rel, in_place=False, look=None: done.append(f"waist {rel:+.0f} look {look}") or abs(rel) < 20 run(160.0, 160.0) assert done[-2:] == ["waist -30 look -66.0", "waist -16 look -66.0"], done fetch._waist_to = lambda rel, in_place=False, look=None: done.append(f"waist {rel:+.0f} look {look}") or True # a wave nearly in front: only the head turns, the waist stays fresh() waver["w"] = W(5.0) run(200.0, 200.0) assert done == ["up route"] + ["swing"] * g.GREET_SWINGS, done # the head looks there already (within HEAD_MIN_DEG) # the switch is turned off with the hand up: down at once switch["on"] = False done.clear() run(200.5, 200.5) assert done[:2] == ["waist +0 look None", "down normal"], done # the arm stops (STOP, a person too close) during a swing: switched off, nothing moves after it fresh() waver["w"] = W(0.0) run(300.0, 300.0) def stopped(*a, **k): raise motion.MotionError("stopped") motion.move_path = stopped run(300.5, 300.5) assert switch["on"] is False and not g.busy() and said[-1] in ("I stopped my arm.", "팔을 멈췄어요."), said motion.move_path = move_path done.clear() run(301.0, 310.0) assert done == [], done # the arm refused (off, busy, something held): nothing moves, tried again after REFUSED_S only fresh() plan_error["e"] = wave.WaveError(*wave.OFF) run(400.0, 400.0 + g.REFUSED_S - 1) assert done == [] and switch["on"], done plan_error["e"] = None run(400.0 + g.REFUSED_S + 0.5, 400.0 + g.REFUSED_S + 0.5) assert "up route" in done, done # the assistant or a task has the robot: nothing moves fresh() switch["free"] = False run(500.0, 505.0) assert done == [] and not g.busy(), done # a drive starts (lumi.waist_home) with the hand up mid-swing: the swings end, then the arm down before the drive fresh() waver["w"] = W(0.0) run(550.0, 550.0) g._st["swing_end"] = 1e12 # a swing still queued switch["free"] = False run(550.5, 550.5) # the drive took the robot: the wave greeting lets go done.clear() g._rest = lambda: 86.0 # on the charger the waist rests turned; a drive still wants the front g.lower() assert done == ["settle", "waist +0 look None", "down normal", "head +0 rest"] and arm["at"] == "look", done arm["at"] = "wave_start" # nobody waves any more on the charger: down to the waist's rest there done.clear() g._down() assert done[:2] == ["waist +86 look None", "down normal"], done g._rest = lambda: 0.0 done.clear() g.lower() # the hand is down: nothing moves assert done == [], done # Welcome (owner, 2026-11-16): switched on, the hand goes up to wave_start at once and stays, no swings yet, no words; # someone right by the arm: later. At home (_home) a new person 0.8..6 m: the head to them and IDLE_SWINGS swings, each # person once. Keep alive (_idle: Welcome, on the charger, nobody talked for KEEP_QUIET_S): KEEP_QUIET_S after the last # wave at people, IDLE_SWINGS swings every IDLE_WAVE_EVERY_S toward the nearest person at any distance, else the front. fresh() waver["w"] = None g._welcome, g._home, g._idle, g._rest_arm = (lambda: True), (lambda: False), (lambda: False), (lambda: "look") g._someone_within = lambda m: True run(690.0, 690.0) assert done == [], done g._someone_within = lambda m: False run(691.0, 691.0) assert done == ["up route"] and g._st["up"] and g._st["held"] and said == [], (done, said) run(691.5, 700.0) assert done == ["up route"], done # held up, still: nobody new, keep alive not allowed g._home = lambda: True person, marked = {"p": (25.0, 3.0, None, None)}, [] real_new, real_mark = g._new_passer, g._mark_passed g._new_passer = lambda now=None: person["p"] g._mark_passed = lambda now=None: marked.append(1) or person.update(p=None) done.clear() run(701.0, 702.0) assert done[0] == "head +25" and done.count("swing") == g.IDLE_SWINGS and marked == [1] and said == [], done done.clear() run(702.5, 710.0) assert "swing" not in done, done # the same person: not again g._idle = lambda: True nearest = {"b": None} real_any, g._nearest_any = g._nearest_any, (lambda now=None: nearest["b"]) run(710.5, 701.0 + g.KEEP_QUIET_S - 1) assert "swing" not in done, done # not before KEEP_QUIET_S after the last wave at people run(701.0 + g.KEEP_QUIET_S + 0.5, 701.0 + g.KEEP_QUIET_S + 1.5) assert done.count("swing") == g.IDLE_SWINGS, done # nobody seen: to the front done.clear() t = 701.0 + g.KEEP_QUIET_S + 2 nearest["b"] = -40.0 # someone far away on the right run(t, t + g.IDLE_WAVE_EVERY_S - 3) assert done == [], done # not before IDLE_WAVE_EVERY_S run(t + g.IDLE_WAVE_EVERY_S, t + g.IDLE_WAVE_EVERY_S + 1) assert done[0] == "head -40" and done.count("swing") == g.IDLE_SWINGS, done done.clear() talk = {"on": True} # someone talks to Lumi: no swings, the hand stays up, still g._idle = lambda: not talk["on"] g._home = lambda: not talk["on"] run(1000.0, 1100.0) assert done == [] and g._st["up"], done g.lower() # a drive: down to the front first assert "down normal" in done and not g._st["up"] and not g._st["held"], done done.clear() g._welcome = lambda: False # Welcome off: nothing goes up by itself on the charger g._idle = g._home = lambda: True run(1200.0, 1300.0) assert done == [], done g._welcome, g._home, g._idle, g._rest_arm = (lambda: False), (lambda: False), (lambda: False), g.arm_route.rest_pose g._new_passer, g._mark_passed, g._nearest_any = real_new, real_mark, real_any # keep alive on the way (Wave on, lumi.Patrol): due KEEP_QUIET_S after the last swing of any kind, not with someone by # the arm; IDLE_SWINGS swings toward the nearest person at any distance, else the front, only with the hand up g._st["any_wave"] = 2000.0 assert not g.keep_alive_due(2000.0 + g.KEEP_QUIET_S - 1) assert g.keep_alive_due(2000.0 + g.KEEP_QUIET_S) g._someone_within = lambda m: True assert not g.keep_alive_due(2000.0 + g.KEEP_QUIET_S) g._someone_within = lambda m: False g.note_quiet_start(3000.0) assert not g.keep_alive_due(3000.0 + 1) and g.keep_alive_due(3000.0 + g.KEEP_QUIET_S) real_any, g._nearest_any = g._nearest_any, (lambda now=None: 15.0) arm["at"] = "look" done.clear() assert not g.keep_alive_wave() and done == [] # the hand is not up arm["at"] = "wave_start" assert g.keep_alive_wave() and done.count("swing") == g.IDLE_SWINGS, done time.sleep(0.2) assert "head +15" in done and done[-1] == "head +0" and g._st["any_wave"] > 3000.0, done g._nearest_any = real_any arm["at"] = "look" done.clear() # Lumi introduces itself once per person: the same person waving again (after NEW_WAVE_S) gets a hello, someone # else an introduction, and the first person too once they were out of view for GONE_S fresh() waver["w"] = W(0.0) run(600.0, 600.0) waver["w"] = None run(600.5, 600.0 + g.NEW_WAVE_S + 1) waver["w"] = W(0.0) run(607.0, 607.0) waver["w"] = None run(607.5, 607.0 + g.NEW_WAVE_S + 1) waver["w"] = W(0.0, B) run(614.0, 614.0) assert [l in sum(g.wave_lines.INTRO, ()) for l in said] == [True, False, True], said assert said[1] in sum(g.wave_lines.GREET, ()), said for k in g._met.known: k.last_seen -= g.GONE_S + 1 # both left the view waver["w"] = None run(614.5, 614.0 + g.NEW_WAVE_S + 1) waver["w"] = W(0.0) run(621.0, 621.0) assert said[-1] in sum(g.wave_lines.INTRO, ()), said # a drive's wave for people passing (Wave on): only with the hand up, a new person PASSER_MIN_M..PASSER_M away, # nobody by the arm; each person once (_mark_passed after the swings, owner 2026-11-16) near = {"m": None} marked = [] waver["w"] = None g._someone_within = lambda m: near["m"] is not None and near["m"] < m real_new, g._new_passer = g._new_passer, lambda now=None: (20.0, near["m"], None, None) \ if near["m"] is not None and g.PASSER_MIN_M <= near["m"] <= g.PASSER_M else None real_mark, g._mark_passed = g._mark_passed, lambda now=None: marked.append(1) assert (g.PASSER_MIN_M, g.PASSER_M, g.IDLE_CLEAR_M) == (0.8, 6.0, 0.8) saved, arm["at"] = arm["at"], "wave_start" done.clear() assert not g.passer_wave() and not done # nobody there assert not g.passer_due() near["m"] = 0.5 assert not g.passer_wave() and not done and not g.passer_due() # someone right by the arm: no stop, no wave near["m"] = 0.75 assert not g.passer_due() # within IDLE_CLEAR_M 0.8: no stop near["m"] = 0.9 assert g.passer_due() # just over 0.8 m: a stop and a wave near["m"] = g.PASSER_M + 0.5 assert not g.passer_due() # too far near["m"] = 2.0 assert g.passer_wave() and done.count("swing") == g.IDLE_SWINGS and marked == [1] # remembered as waved at time.sleep(0.2) # the head turns in the background assert "head +20" in done and done[-1] == "head +0" # to them, then ahead again for the drive arm["at"], near["m"] = "look", 2.0 assert not g.passer_wave() # the hand is not up arm["at"] = saved done.clear() g._mark_passed = real_mark g._new_passer = real_new # the module's own again # each person once: someone waved at is not new again while in view, and new again after GONE_S out of view import numpy as np # noqa: E402 from vision import reid # noqa: E402 img = np.zeros((200, 200, 3), np.uint8) img[:, :100] = (200, 30, 30) img[:, 100:] = (30, 200, 30) red, green = reid.look(img, (0, 0, 100, 200)), reid.look(img, (100, 0, 200, 200)) g._passed.known = [] now = 1000.0 g._seen["head"].update(t=now, people=[(10.0, 2.0, red, None), (-30.0, 7.5, green, None)]) g._seen["torso"].update(t=0.0, people=[]) p = g._new_passer(now) assert p is not None and p[1] == 2.0, p # the one at 7.5 m is too far g._mark_passed(now) assert g._new_passer(now + 0.1) is None # waved at: not new again g._seen["head"].update(t=now + 3, people=[(5.0, 3.0, red, None), (0.0, 4.0, green, None)]) p = g._new_passer(now + 3) assert p is not None and p[1] == 4.0, p # the other one came near: new g._mark_passed(now + 3) g._seen["head"].update(t=now + 3 + g.GONE_S + 1, people=[(5.0, 3.0, red, None)]) assert g._new_passer(now + 3 + g.GONE_S + 1) is not None # gone longer than GONE_S: waved at again g._seen["head"].update(t=0.0, people=[]) g._passed.known = [] # the hand held up, someone waves for a moment and stops: GREET_SWINGS swings all the same, then no more fresh() arm["at"] = "wave_start" g._st.update(up=True, held=True, idle_at=1e9) waver["w"] = W(10.0) run(400.0, 400.0) waver["w"] = None run(400.5, 420.0) assert done.count("swing") == g.GREET_SWINGS and g._st["owed"] == 0, done arm["at"] = "look" done.clear() # on the charger with Welcome, the waving over: after HOLD_S the waist back to the front, the hand held; keep alive # swings once KEEP_QUIET_S passed since that wave fresh() arm["at"] = "wave_start" g._st.update(up=True, held=False, last_wave=600.0, people_wave=0.0, idle_at=0.0) real_idle, g._idle = g._idle, (lambda: True) real_any, g._nearest_any = g._nearest_any, (lambda now=None: None) # never the live camera files run(600.0 + g.HOLD_S + 0.5, 600.0 + g.HOLD_S + 2.0) assert done == ["waist +0 look None"] and g._st["held"], done run(600.0 + g.KEEP_QUIET_S + 0.5, 600.0 + g.KEEP_QUIET_S + 2.0) assert done[1:] == ["swing"] * g.IDLE_SWINGS, done g._idle, g._nearest_any = real_idle, real_any arm["at"] = "look" done.clear() # the base or the arm controller does not answer for a moment: tried again, the switch stays on; a stop switches it off g.LINK_RETRY_S = 0.0 real_step = g.step def no_answer(now): raise ConnectionRefusedError(111, "Connection refused") g.step = no_answer switch["on"] = True g._tick(500.0) assert switch["on"], switch def stopped_step(now): raise g.motion.MotionError("stopped") g.step = stopped_step g._tick(501.0) assert not switch["on"], switch g.step = real_step switch["on"] = True said.clear() done.clear() # a hand at the head (scratching, a phone at the ear) is not a raised hand; the same hand held out to the side is import numpy as np # noqa: E402 import cv2 # noqa: E402 kp = np.zeros((17, 3)) for name, xy in (("nose", (640, 200)), ("left_eye", (650, 190)), ("right_eye", (630, 190)), ("left_shoulder", (700, 320)), ("right_shoulder", (580, 320)), ("right_elbow", (560, 260)), ("right_wrist", (600, 210))): kp[g.pose.K[name]] = (*xy, 1.0) assert waving._hand(kp) is None # palm at (620, 185): beside the eye kp[g.pose.K["right_wrist"]] = (480, 200, 1.0) kp[g.pose.K["right_elbow"]] = (520, 280, 1.0) assert waving._hand(kp) is not None # palm at (460, 160): out to the side # the detector on the recordings: every wave found, nothing else ever counts as waving DATA = Path(__file__).parent / "data" / "waves" if DATA.exists(): for f in sorted(p for p in DATA.iterdir() if p.is_dir()): r = waving.run_recording(f) seen = [any(w) for _, w in r] if f.name.startswith("no_"): assert not any(seen), f.name else: first = next(t - r[0][0] for (t, _), s in zip(r, seen) if s) assert first < 2.0, (f.name, first) # found within 2 s if f.name != "pause_2m": # (it stops waving half way) assert sum(seen) > 0.75 * len(seen), (f.name, sum(seen), len(seen)) for name in ("right_3m", "left_3m"): # the same waves shrunk to 10 m: still found w, n, hit, k = waving.Watcher(), 0, 0, 3 / 10 for line in open(DATA / f"{name}.jsonl"): d = json.loads(line) g = cv2.imread(str(DATA / name / f"{d['n']:04d}.jpg"), cv2.IMREAD_GRAYSCALE) far = np.full_like(g, 128) small = cv2.resize(g, None, fx=k, fy=k, interpolation=cv2.INTER_AREA) far[:small.shape[0], :small.shape[1]] = small n += 1 hit += any(v for _, v in w.update(d["t"], far, [{**p, "kp": np.array(p["kp"]) * [k, k, 1]} for p in d["people"]])) assert hit > 0.75 * n, (name, hit, n) r = waving.run_recording(DATA / "pause_2m") # stopped at ~6 s: no longer waving 2.5 s later assert not any(any(w) for t, w in r if t - r[0][0] > 8.5) else: print("(no recordings here: detector not checked)") print("wave greeting: ok")