From cec933c1ac1cc185c87cf6ae37f383988ee9f788 Mon Sep 17 00:00:00 2001 From: Cutshion Date: Wed, 11 Nov 2026 12:00:26 +0900 Subject: [PATCH] Fix greeting task reliability Co-Authored-By: Claude Opus 5.5 --- assistant/lumi.py | 4 ++-- tasks/wave_greet.py | 45 +++++++++++++++++++++++++++++--------- tests/test_mission_stop.py | 14 ++++++++++++ 3 files changed, 51 insertions(+), 12 deletions(-) diff --git a/assistant/lumi.py b/assistant/lumi.py index 03ec99e..e2fa53e 100644 --- a/assistant/lumi.py +++ b/assistant/lumi.py @@ -518,6 +518,7 @@ MODES_FILE = "/dev/shm/lumi_modes.json" # written by api/server.py: {"greeter" CMD_FILE = "/dev/shm/lumi_cmd.json" # written by api/server.py: {"id", "cmd": go | go_point | cancel | calibrate # | fetch | fetch_give | fetch_place | fetch_stop | fetch_carry | fetch_release | fetch_steps # | fetch_pause | fetch_resume} +STARTED = time.time() # commands written before this program started are old and skipped CALIB_FILE = "/dev/shm/lumi_calib.json" # progress of a calibration session, for the dashboard BUSY_FILE = "/dev/shm/lumi_busy.json" # what runs here now, for api/server.py robot_free(): {"busy": skill | # follow | drive | wave | null} (fetch tasks are in lumi_task.json) @@ -2502,9 +2503,8 @@ class Lumi: return if d.get("id") == self.cmd_id: return - first = self.cmd_id is None self.cmd_id = d.get("id") - if first and time.time() - d.get("time", 0) > 5: # an old command from before this start + if d.get("time", 0) < STARTED: # from before this start (a busy step can read a new one late) return cmd = d.get("cmd") log(f"dashboard: {cmd} {d.get('marker') or d.get('point') or d.get('markers') or ''}") diff --git a/tasks/wave_greet.py b/tasks/wave_greet.py index f44c54e..a8245a6 100644 --- a/tasks/wave_greet.py +++ b/tasks/wave_greet.py @@ -32,10 +32,12 @@ from vision import camview, pose, reid, waving CAMERAS = ("head", "torso") # both watched: the head may look away while the person is in the torso camera's view GREET_MIN_M, GREET_M = 1.0, 5.0 # m; people waving nearer or further away are not answered (owner) -PASSER_M = 3.0 # m; a patrol's wave for people passing (passer_wave) waves only at people this near +PASSER_M = GREET_M # m; a patrol's wave for people passing (passer_wave) waves only at people this near, and + # only when nobody is within IDLE_CLEAR_M (owner: just over 1 m is enough) SHOULDER_M = 0.36 # m between the shoulder keypoints: a waver's distance from their width in the image when # depth has none there (far, or a colour-only camera); estimate (recordings 0.32-0.40) -HOLD_S = 40.0 # s the waist stays on the last one who waved, then back to its rest, the hand kept up +HOLD_S = 15.0 # s the waist stays on the last one who waved, then back to its rest, the hand kept up and + # the idle swings at once (owner: every 15 s on the charger, also among many people) GONE_S = 10.0 # s a greeted person may be out of both cameras' view and still be known (no new intro) SWING_POLL_S = 0.2 # s between looks at the controller's queue while swinging SWING_S = 1.5 # s one swing takes (measured 1.5-1.6 at "swing"); with the queue reading, at most one swing @@ -45,6 +47,8 @@ NEW_WAVE_S = 5.0 # s without any waving after which a wave is greete JOKE_AFTER_S = 10.0 # s of waving before the first joke ... JOKE_EVERY_S = 12.0 # ... and between jokes WAIST_MIN_DEG = 10.0 # deg; a person nearer the front than this: only the head turns +WAIST_MAX_DEG = 30.0 # deg from the front the waist turns to a waving person at most, the head covers the rest + # (owner: it faces the front, a little toward them is fine); estimate WAIST_FOLLOW_DEG = 20.0 # deg the waving person may move off the waist's front before it turns to them again ... FOLLOW_EVERY_S = 3.0 # ... at most this often (between swings) WAIST_PARTS = (1.0, 0.75, 0.5, 0.25) # a blocked waist turn is tried again this much of the way (beside a wall) @@ -67,6 +71,8 @@ RAISE_WAIT_S = 10.0 # s a drive waits for someone right by the arm to s # (else it drives in look); estimate IDLE_RETRY_S = 1.0 # s between two asks whether Lumi is idle (it asks the base whether it charges) CLOSE_FRESH_S = 1.5 # s a camera service detection counts for the too-close check +LINK_RETRY_S = 3.0 # s before trying again when the arm controller or the base did not answer (the controller + # now and then closes the connection, the base refuses while it loads a map): not a stop _seen = {c: {"t": 0.0, "wavers": []} for c in CAMERAS} # wavers: [(bearing deg from the waist's front, + left; # distance m)] in that camera, nearest first @@ -303,14 +309,19 @@ def _settle(): def _face(bearing): """Waist turned to the person (they face each other), the head staying on them; only the head when they are - nearly in front. A turn whose sweep is not clear is tried part of the way (WAIST_PARTS); the head covers the - rest.""" + nearly in front. The waist stays within WAIST_MAX_DEG of the front; a turn whose sweep is not clear is tried part + of the way (WAIST_PARTS); the head covers the rest.""" from tasks import fetch fetch._stop.clear() # no task runs: an old stop must not end this turn + tried = set() for part in WAIST_PARTS: if abs(bearing * part) < WAIST_MIN_DEG: break - if fetch._waist_to(fetch._waist_turn() + bearing * part, in_place=True, look=bearing): + want = round(max(-WAIST_MAX_DEG, min(WAIST_MAX_DEG, fetch._waist_turn() + bearing * part)), 1) + if want in tried: # cut to WAIST_MAX_DEG like a longer part before it + continue + tried.add(want) + if fetch._waist_to(want, in_place=True, look=bearing): return _head_to(bearing) @@ -416,6 +427,11 @@ def _nearest(m): return min(near)[1] if near else None +def passer_due(): + """Someone passing to wave at: a person within PASSER_M, nobody within IDLE_CLEAR_M (lumi.Patrol stops for it).""" + return _nearest(PASSER_M) is not None and not _someone_within(IDLE_CLEAR_M) + + def passer_wave(): """A drive with the hand up (lumi.Patrol, the base stopped): the head to the one waving, else the nearest person within PASSER_M, and IDLE_SWINGS swings, waited for, then the head ahead again; none when nobody is within PASSER_M @@ -553,7 +569,7 @@ def step(now): st["up"] = st["held"] = False elif now - st["last_wave"] > HOLD_S: # nobody waves: the hand stays up (owner), the waist to its rest _rest_waist() - st["held"], st["idle_at"], st["held_at"] = True, now, 0.0 + st["held"], st["idle_at"], st["held_at"] = True, now - IDLE_WAVE_EVERY_S, 0.0 # swings due at once def _stopped(e): @@ -573,10 +589,19 @@ def _run(): log(f"arm not read at start: {e!r}") while True: time.sleep(0.05) - try: - step(time.time()) - except Exception as e: - _stopped(e) + _tick(time.time()) + + +def _tick(now): + """One step; a stop or a failure of the arm switches the greeting off, no answer from a device does not.""" + try: + step(now) + except OSError as e: # ConnectionError too: the switch stays on + log(f"no answer ({e!r}), trying again in {LINK_RETRY_S:.0f} s") + _busy.clear() + time.sleep(LINK_RETRY_S) + except Exception as e: + _stopped(e) def up(): diff --git a/tests/test_mission_stop.py b/tests/test_mission_stop.py index a426cd4..f1d7a96 100644 --- a/tests/test_mission_stop.py +++ b/tests/test_mission_stop.py @@ -122,6 +122,20 @@ with os.fdopen(fd, "w") as f: L.poll_commands() os.remove(lumi.CMD_FILE) assert "stop" in done and run["status"] == "stopped" and L.mission_hold is None, done +# the first command since the start, read 30 s late by a busy conversation step: still done; one from before the +# start: skipped (2026-11-10: "go home" after boot was dropped as old) +for written, taken in ((-30, True), (-90, False)): + done.clear() + L = halted(fresh()) + L.cmd_id, started = None, lumi.STARTED + lumi.STARTED = time.time() - 60 + fd, lumi.CMD_FILE = tempfile.mkstemp(suffix=".json") + with os.fdopen(fd, "w") as f: + json.dump({"id": 2, "time": time.time() + written, "cmd": "cancel"}, f) + L.poll_commands() + os.remove(lumi.CMD_FILE) + lumi.STARTED = started + assert ("stop" in done) == taken, (written, done) # 19:47 the dashboard's stop while following (no task runs): following ends too, not only the task from tasks import follow ended, real = [], (follow.stop, fetch.stop)