From 9b239c8b8999120dcc12c2abec661e1a220da7e6 Mon Sep 17 00:00:00 2001 From: Cutshion Date: Fri, 30 Oct 2026 20:55:04 +0900 Subject: [PATCH] Watch both cameras for waves and keep the body facing a waver Co-Authored-By: Claude Opus 5.5 --- docs/TUNING.md | 6 ++++- tasks/wave_greet.py | 53 ++++++++++++++++++++++++++-------------- tests/test_wave_greet.py | 11 ++++++++- 3 files changed, 49 insertions(+), 21 deletions(-) diff --git a/docs/TUNING.md b/docs/TUNING.md index 18d8e97..39c971b 100644 --- a/docs/TUNING.md +++ b/docs/TUNING.md @@ -60,7 +60,10 @@ Every value here is a constant at the top of its file, with a comment on what it | `tasks/follow.py` | `FOLLOW_MAX_MS`, `SPEED_GAIN` | 0.5 m/s, 0.8 /s | Follow speed cap (joy_control's own clamp, [05_AGV_BASE.md](05_AGV_BASE.md); the base's `max_speed_linear` is raised to it while following); speed = their walking speed + this per m beyond the gap | | `tasks/follow.py` | `ACCEL_MS2`, `DECEL_MS2` | 0.5, 0.8 m/s² | Speeding up / slowing down limits; never faster than lets the base stop at the gap with `DECEL_MS2` after `REACT_S`. Estimates | | `tasks/follow.py` | `REACT_S` | 0.3 s | From the person stopping to the base braking (frame, track, tick, command); with `DECEL_MS2` it sets the follow distance while walking (~1.3 m at 0.5 m/s). Estimate | -| `tasks/follow.py` | `TURN_GAIN`, `TURN_MAX_RS`, `TURN_ACCEL_RS2`, `TURN_DEAD_DEG`, `TURN_ONLY_DEG` | 1.5, 0.6 rad/s, 2.0 rad/s², 5°, 35° | Turning toward the person: gain, cap (the base's `max_speed_angular` is raised to it while following), change limit, no turn within, no forward beyond (turn first) | +| `tasks/follow.py` | `TURN_GAIN`, `TURN_MAX_RS`, `TURN_ACCEL_RS2`, `TURN_DEAD_DEG`, `TURN_ONLY_DEG` | 1.5, 1.0 rad/s, 2.0 rad/s², 5°, 35° | Turning toward the person: gain, cap (joy_control's own clamp, [05_AGV_BASE.md](05_AGV_BASE.md); the base's `max_speed_angular` is raised to it while following), change limit, no turn within, no forward beyond (turn first) | +| `tasks/follow.py` | `FOCUS_FULL_DEG`, `FOCUS_EDGE_DEG`, `HEAD_DEG_S` | 5°, 25°, 40 °/s | The base turns at full speed while the head's gaze is within `FOCUS_FULL_DEG` behind the person, slower down to a stop at `FOCUS_EDGE_DEG` (only when the turn takes them further from the gaze), so the head camera keeps them in view; `HEAD_DEG_S` is the head's top speed (audio/sing.py), used for where a head swinging through the front meets a person coming round. Estimates | +| `tasks/follow.py` | `ROUND_MAX_DEG` | 225° | The arm's sweep blocked toward the person: the base turns the other way round when that is at most this far | +| `tasks/follow.py` | `SHIFT_STEP_M`, `SHIFT_MAX_M`, `SHIFT_MS` | 0.1 m, 0.4 m, 0.10 m/s (`fetch.MOVE_SPEED`) | Blocked both ways on the spot: straight moves forward / back in these steps up to `SHIFT_MAX_M` are tried (the way clear for the base and the arm, the turn from its end clear, the person still `FOLLOW_GAP_M` away; away from them first), the shortest is driven at `SHIFT_MS` and the turn made from there. Estimate | | `tasks/follow.py` | `STALE_S`, `COAST_S`, `LOST_S` | 0.4 s, 1.0 s, 3 s | A sighting older than `STALE_S` is not new; unseen, the base drives on toward where they should be for `COAST_S`, then slows; "I lost you" after `LOST_S` | | `tasks/follow.py` | `VEL_TAU_S`, `WALK_MAX_MS` | 0.5 s, 2 m/s | Walking-velocity filter time constant; faster estimates are noise | | `tasks/follow.py` | `JUMP_M`, `GATE_GROW_MS`, `SWITCH_M`, `RELOCK_LOOK` | 0.5 m, 0.7 m/s, 0.15 m, 0.6 | Keeping the person: a sighting further than `JUMP_M` (+ `GATE_GROW_MS` per s unseen) from where they should be is someone else; another track must be `SWITCH_M` nearer to be taken; clothes `reid.same` below `RELOCK_LOOK` never taken. Estimates | @@ -200,6 +203,7 @@ Every value here is a constant at the top of its file, with a comment on what it | `tasks/wave_greet.py` | `NEW_WAVE_S` | 5 s | No waving this long: the next wave gets a new greeting line | | `tasks/wave_greet.py` | `JOKE_AFTER_S`, `JOKE_EVERY_S` | 10 s, 12 s | Waving on and on: the first joke after this long, then one every this long | | `tasks/wave_greet.py` | `WAIST_MIN_DEG` | 10° | A person nearer the front than this: only the head turns, not the waist | +| `tasks/wave_greet.py` | `WAIST_FOLLOW_DEG`, `FOLLOW_EVERY_S` | 20°, 3 s | The waving person moved this far off the waist's front: the waist turns to them again between swings, at most this often | | `tasks/wave_greet.py` | `WAIST_PARTS` | 1, 0.75, 0.5, 0.25 | A waist turn whose sweep is blocked is tried again this much of the way; the head covers the rest | | `tasks/wave_greet.py` | `REFUSED_S` | 30 s | After a refused wave (arm off, busy, holding) the next try waits this long | | `vision/waving.py` | `WINDOW_S`, `MIN_FLIPS` | 2 s, 2 | A wave: the hand's sideways motion changes direction this often within this long. Shorter window = quicker start and stop, slow waves missed | diff --git a/tasks/wave_greet.py b/tasks/wave_greet.py index 7c93098..cfddfd7 100644 --- a/tasks/wave_greet.py +++ b/tasks/wave_greet.py @@ -1,7 +1,7 @@ """Wave back at people who wave at Lumi: the dashboard switch "Wave greeting". Greeter / Patrol are not needed; while either runs the assistant owns the robot and this waits. Runs in the API process (api/server.py starts it). -Someone waving within GREET_M of the head camera (vision/waving.py): the head turns to them and the arm goes up at +Someone waving within GREET_M of either camera (vision/waving.py; the head camera may look elsewhere): the head turns to them and the arm goes up at once (manipulation/wave.py checks and plans the way, look -> wave_out -> wave_start, at the route speed), Lumi says a greeting (tasks/wave_lines.py), swings once even when they stopped meanwhile, then turns the waist to face them (fetch._waist_to, its sweep checked; the head stays on them). It swings while they wave, stops when they stop and @@ -25,7 +25,7 @@ from robot import head, motion from tasks import wave_lines from vision import camview, pose, waving -CAM = "head" +CAMERAS = ("head", "torso") # both watched: the head may look away while the person is in the torso camera's view GREET_M = 3.0 # m; people waving further away are not answered HOLD_S = 60.0 # s the hand stays up after the last wave seen, then down to look FRESH_S = 0.5 # s a waving person seen counts as waving now @@ -33,6 +33,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_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) HEAD_MIN_DEG = 8.0 # deg the person may move off the head's direction before the head follows DOWN_SPEED = "normal" # motion.JOINT_SPEED of the way down (owner: 2x the first "slow") @@ -45,7 +47,8 @@ HEAD_TIMEOUT_S = 5.0 # s a head move may take; the body API now and then # holds the body lock all that time (the waist turn waited 30 s behind it) CLOSE_FRESH_S = 1.5 # s a camera service detection counts for the too-close check -_seen = {"t": 0.0, "wavers": []} # wavers: [(bearing deg from the waist's front, + left; distance m)], nearest first +_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 _busy = threading.Event() # the arm is up or moving for a greeting _speaking = threading.Lock() _on = _free = _off = None # set by start() @@ -60,10 +63,11 @@ def busy(): # ---------- seeing ---------- -def _head_yaw(): - """Head yaw (deg) the camera service saw its newest frame at.""" +def _head_yaw(cam="head"): + """Yaw (deg) of a camera from the waist's front at its newest frame: the head's yaw for the head camera, 0 for the + torso camera (it looks along the waist).""" try: - return float(json.loads(camview.det_path(CAM).read_text()).get("head_yaw") or 0.0) + return float(json.loads(camview.det_path(cam).read_text()).get("head_yaw") or 0.0) except (FileNotFoundError, ValueError): return 0.0 @@ -78,17 +82,17 @@ def _dist(depth, at): return float(np.median(v)) / 1000 if v.size >= 5 else None -def _watch(): - """Every head camera frame while the switch is on: who waves, where (_seen).""" +def _watch(cam): + """Every frame of camera `cam` while the switch is on: who waves, where (_seen[cam]).""" watcher, last, intr = waving.Watcher(), 0.0, None while True: if not _on(): watcher, intr = waving.Watcher(), None time.sleep(0.5) continue - camview.want(CAM) + camview.want(cam) try: - m = camview.raw_path(CAM).stat().st_mtime + m = camview.raw_path(cam).stat().st_mtime except FileNotFoundError: time.sleep(0.2) continue @@ -96,11 +100,11 @@ def _watch(): time.sleep(0.01) continue last = m - img = cv2.imread(str(camview.raw_path(CAM))) - intr = intr or camview.intrinsics(CAM) + img = cv2.imread(str(camview.raw_path(cam))) + intr = intr or camview.intrinsics(cam) if img is None or intr is None: continue - depth, yaw = camview.depth(CAM), _head_yaw() + depth, yaw = camview.depth(cam), _head_yaw(cam) gray = cv2.cvtColor(cv2.resize(img, None, fx=waving.SCALE, fy=waving.SCALE), cv2.COLOR_BGR2GRAY) wavers = [] for p, is_waving in watcher.update(m, gray, pose.detect(img)): @@ -111,12 +115,13 @@ def _watch(): d = _dist(depth, at) if d is not None and d <= GREET_M: wavers.append((yaw - math.degrees(math.atan((at[0] - intr["cx"]) / intr["fx"])), d)) - _seen.update(t=time.time(), wavers=sorted(wavers, key=lambda w: w[1])) + _seen[cam].update(t=time.time(), wavers=sorted(wavers, key=lambda w: w[1])) def _waver(): - """(bearing, distance) of the nearest person waving now, None when nobody waves.""" - return _seen["wavers"][0] if _seen["wavers"] and time.time() - _seen["t"] < FRESH_S else None + """(bearing, distance) of the nearest person waving now in either camera, None when nobody waves.""" + now = [w for s in _seen.values() if time.time() - s["t"] < FRESH_S for w in s["wavers"]] + return min(now, key=lambda w: w[1], default=None) def _too_close(): @@ -247,7 +252,7 @@ def _down(): _st = {"up": False, "last_wave": 0.0, "lang": 0, "next_joke": 0.0, "free": False, "free_at": 0.0, "refused_until": 0.0, - "waist_out": False} # waist_out: the way back to the front was blocked, tried again later + "waist_out": False, "follow_at": 0.0} # waist_out: the way back to the front was blocked, tried again later def step(now): @@ -289,7 +294,11 @@ def step(now): elif now > st["next_joke"]: _say(wave_lines.LONG, st["lang"]) st["next_joke"] = now + JOKE_EVERY_S - _head_to(w[0]) + if abs(w[0]) > WAIST_FOLLOW_DEG and now > st["follow_at"]: + st["follow_at"] = now + FOLLOW_EVERY_S + _face(w[0]) # they moved: the waist turns to them again, between two swings + else: + _head_to(w[0]) _swing() elif now - st["last_wave"] > HOLD_S: _down() @@ -306,6 +315,11 @@ def _stopped(e): def _run(): + try: # the hand left up (an API restart mid-wave): it comes down after HOLD_S + if arm_route.where() in (wave.START, wave.SWING): + _st.update(up=True, last_wave=time.time()) + except Exception as e: + log(f"arm not read at start: {e!r}") while True: time.sleep(0.05) try: @@ -318,5 +332,6 @@ def start(on, free, off): """`on()`: the switch is on; `free()`: nothing else has the robot; `off()`: turn the switch off.""" global _on, _free, _off _on, _free, _off = on, free, off - threading.Thread(target=_watch, daemon=True).start() + for cam in CAMERAS: + threading.Thread(target=_watch, args=(cam,), daemon=True).start() threading.Thread(target=_run, daemon=True).start() diff --git a/tests/test_wave_greet.py b/tests/test_wave_greet.py index b20a009..71e02ac 100644 --- a/tests/test_wave_greet.py +++ b/tests/test_wave_greet.py @@ -72,7 +72,7 @@ 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) + 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) switch.update(on=True, free=True) plan_error["e"] = None @@ -92,6 +92,15 @@ run(100.5, 100.0 + g.JOKE_AFTER_S + 1) assert done.count("swing") >= 10 and "up route" not in done, done 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"] = (-40.0, 2.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) +assert done[0] == "waist -40 look -40.0" and done.count("waist -40 look -40.0") == 1, done +waver["w"] = (30.0, 2.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()