Keep head moves from hanging, check a shut hand with the camera and follow round tight spots

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-30 20:55:05 +09:00
co-authored by Claude Opus 5.5
parent 9b239c8b89
commit 8ec328f9da
18 changed files with 458 additions and 79 deletions
+82 -17
View File
@@ -50,6 +50,9 @@ LOOK_POSE = "look" # drives, turns, looks and waits here, also with an
# the torso view, too near for depth (ready if not taught). carry is only passed through after a
# pick: in carry the hand is ~0.3 m before the base's front, where its 3D camera seems to see
# it as an obstacle (every drive in carry stalled; the same drives in look went, 2026-10-16)
CHECK_POSE = "carry" # a fully shut hand is looked at here (look_at_hand): the torso camera sees the place between
# the fingers ~445 mm away (from the joints and the calibration; in look it is above the view,
# in give a person's hand can be there). Not yet tried live: the fingers may hide it
MAX_TILT_DEG = skills.MAX_TILT_DEG # deg; the held cup's axis may lean this far from vertical on a straight move
LINE_POINTS = 20 # points IK- and tilt-checked on a straight move with an object in the hand ...
LINE_STEP_DEG = 25.0 # ... no joint may turn more than this between two of them (one IK branch all the way;
@@ -785,14 +788,15 @@ def head_hits(deg, pts=None):
return depth_hits(deg, "head", T, pts)
def turn_hits(deg, pts=None):
def turn_hits(deg, pts=None, pose=None):
"""The map point (x, y) where the arm would come closer than ARM_CLEAR_M to something the lidar or the map shows
while the base turns by `deg`, checked every SWEEP_STEP_DEG along the way; None when the way is clear. The base
itself turns in its own footprint; navigation does not know about the arm sticking out. `pts`: as depth_hits."""
itself turns in its own footprint; navigation does not know about the arm sticking out. `pts`: as depth_hits.
`pose`: the base's (x, y, th) to turn from, else where it is."""
if arm_yaw() is None:
return None
k0, pts = (1, arm_points()) if pts is None else (0, [to_base(p[:2], arm_yaw(), arm_xy()) for p in pts])
x, y, th = agv.pose()
x, y, th = pose or agv.pose()
n = max(1, math.ceil(abs(deg) / SWEEP_STEP_DEG))
for k in range(k0, n + 1):
R = _rot(math.degrees(th) + deg * k / n)
@@ -2625,29 +2629,89 @@ def _busy():
# ---------- public ----------
def _in_hand():
"""Is something in the hand, whatever the state file says? The torso depth camera first (skills.hand_sees: it
sees the place between the fingers); when it can't tell, the gripper's AI_1. AI_1 alone was wrong once: a cup
pulled out of the closed fingers left it reading "open" (2026-10-16). Fingers fully shut (AI_1 above
gripper.EMPTY_AI) hold nothing, whatever the camera sees: with the arm held out it saw the person's hand there and
"closed on nothing" became "holding" (2026-10-23 17:40)."""
"""Is something in the hand, whatever the state file says? AI_1 stopped between gripper.CLOSED_AI and EMPTY_AI:
held. Fully shut (above EMPTY_AI): a thing narrower than the belly's closed gap sits in the hooks with AI_1 reading
as on nothing (live 2026-10-30: a cup put in the hand read 4143), so the torso camera decides, but only from
CHECK_POSE (look_at_hand); anywhere else it is held only when close_hand closed on a thing the camera saw between
the open fingers ("narrow"): with the arm held out the camera saw the person's hand there after a close and
"closed on nothing" became "holding" (2026-10-23 17:40). Open: the torso camera first (skills.hand_sees), "unknown"
= empty; AI_1 alone was wrong once: a cup pulled out of the closed fingers left it reading "open" (2026-10-16)."""
try:
g = gripper.state()
ai = bool(g["holding"])
if g.get("ai", [0.0])[0] > gripper.EMPTY_AI:
return False
if g["holding"]:
return True
shut = g.get("ai", [0.0])[0] > gripper.EMPTY_AI
except (ConnectionError, OSError, KeyError):
ai = None
shut = None
narrow = bool(_state["holding"] and skills._held.get("narrow"))
try:
if shut and not (CHECK_POSE in motion.poses() and _at_pose(CHECK_POSE)):
return narrow
seen = skills.hand_sees()
except (skills.SkillError, ConnectionError, OSError, KeyError):
except (skills.SkillError, motion.MotionError, ConnectionError, OSError, KeyError):
seen = "unknown"
if seen == "unknown":
return bool(ai)
if ai is not None and ai != (seen == "object"):
log(f"hand: the camera sees it {seen}, AI_1 says {'holding' if ai else 'empty'}: the camera is believed")
return narrow if shut else False
if seen == "object" and shut is False:
log("hand: the camera sees it object, AI_1 says empty: the camera is believed")
if shut:
log(f"hand: fully shut, the camera sees it {seen} from {CHECK_POSE}")
if seen == "object":
skills._held["narrow"] = True # held while shut: away from CHECK_POSE too
return seen == "object"
def look_at_hand():
"""check_hand before an order that needs something in the hand (put down, give, bring) or when told what is in
it: a fully shut hand first goes slowly (holding) along the checked route to CHECK_POSE, so the camera can tell."""
_to_check_pose()
return check_hand()
def _to_check_pose():
"""A fully shut hand to CHECK_POSE (look_at_hand), not while a task runs."""
if _busy() or CHECK_POSE not in motion.poses():
return
try:
shut = gripper.state().get("ai", [0.0])[0] > gripper.EMPTY_AI
except (ConnectionError, OSError, KeyError):
shut = False
if shut:
try:
if not _at_pose(CHECK_POSE):
log(f"hand: fully shut, the arm to {CHECK_POSE} to look at it")
arm_route.goto(CHECK_POSE, holding=True)
except (arm_route.RouteError, motion.MotionError, ConnectionError, OSError) as e:
log(f"hand: can't look at it from {CHECK_POSE}: {e}")
def close_hand():
"""The voice "close your hand": the camera looks between the open fingers first; closed fully on a thing it saw
there (narrower than the belly's closed gap), it is held all the same ("narrow", named by vision.scene when the
person's hand held it, else "something"); closed fully on nothing seen: nothing is held. Returns gripper.close()'s
state. A hand already closed is closed again and nothing else changes."""
_adopt()
try:
before = None if gripper.state()["closed"] else skills.hand_sees()
except (skills.SkillError, ConnectionError, OSError, KeyError):
before = "unknown"
st = gripper.close()
if before is None:
return st
shut = st.get("ai", [0.0])[0] > gripper.EMPTY_AI
with _lock:
if shut and before == "object" or st.get("holding") and not _state["holding"]:
thing = _scene("object_in_hand") or {}
log(f"hand: closed on {thing.get('name') or 'something'} (camera before: {before}, AI_1 "
f"{st.get('ai', [0.0])[0]:.0f})")
skills._held.clear()
skills._held.update(received=True, narrow=shut)
held_by_skill({"object": thing.get("name"), "colour": thing.get("colour")})
elif shut and _state["holding"]:
_forget_held()
return st
def check_hand():
"""Bring the state in line with the hand (_in_hand) while no task runs: forget a held object that is gone, take
over one that is there. Returns whether something is held. The dashboard's command dialog asks this."""
@@ -2709,7 +2773,7 @@ def _hold_again():
put down needs. Live 2026-10-26 18:53:00 one hand read said empty with the cup in it; after that every reply said
"something" and put down had no height. ponytail: the last forgotten thing is assumed; a thing put in the hand
without receive would get its name."""
if not skills._held and _last_held.get("held"):
if not set(skills._held) - {"narrow"} and _last_held.get("held"):
skills._held.update(_last_held["held"])
_set(holding=True, held_object=_state.get("held_object") or _last_held.get("held_object"))
@@ -2748,6 +2812,7 @@ END_WORDS = {"go": ("I'm here.", "도착했어요."), "place": PUT_DOWN_WORDS, "
def _finish(fn, say, end=None):
_adopt()
_to_check_pose() # a fully shut hand: looked at (_in_hand) before it is refused
with _lock:
if _busy():
raise FetchError("I'm busy with another task.")
+113 -21
View File
@@ -12,10 +12,14 @@ turns further out: following starts with it where it is (turned toward the perso
as the base comes round (unwind). Nothing waits for another. The base is driven with joy_control every TICK_S (one
command lasts 0.5 s: a stuck loop stops the base by itself), the arm in look; forward only with the person near ahead
and the waist near the front. Every command is checked first: where the base and the arm would be BRAKE_S later must be clear
(lidar + map, agv.clearance); when not, the base stands, and after BLOCKED_END_S it gives up.
(lidar + map, agv.clearance); when not, the base stands, and after BLOCKED_END_S it gives up. A turn on the spot whose
arm sweep is blocked goes the other way round, or from a spot a short straight move away (_room_to_turn); it never
turns faster than the head keeps the person in view (focus), and a head that can't follow them past its +-180 swings
through the front to meet them (meet) while the torso camera watches (torso_people).
stop() from any thread: the event is set and the base is sent 0 at once
"""
import json
import math
import re
import threading
@@ -26,7 +30,7 @@ import numpy as np
from robot import agv, head, motion
from tasks import fetch
from vision import reid
from vision import camview, reid
FOLLOW_GAP_M = 1.0 # m; keep this far from the person (camera distance); nearer: no forward
NEAR_STOP_M = 0.8 # m; nearer than this no turn either: the arm would swing close to them (estimate)
@@ -39,7 +43,8 @@ REACT_S = 0.3 # s; from a person stopping to the base braking (fra
ACCEL_MS2 = 0.5 # m/s^2; speeding up at most this fast (estimate, tune live)
DECEL_MS2 = 0.8 # m/s^2; slowing down this fast, and never faster than lets it stop at the gap (estimate)
TURN_GAIN = 1.5 # rad/s per rad the person is off to the side
TURN_MAX_RS = 0.6 # rad/s; turn speed cap (joy_control takes up to 1.0; the arm tips sweep 0.6 m out)
TURN_MAX_RS = 1.0 # rad/s; turn speed cap = joy_control's own clamp (docs/05; the arm tips sweep 0.6 m out,
# 0.6 m/s); slower while the head can't keep the person in view (FOCUS_*)
TURN_ACCEL_RS2 = 2.0 # rad/s^2; turn speed changes at most this fast (estimate)
TURN_DEAD_DEG = 5.0 # deg; the person this near the middle: no turn
TURN_ONLY_DEG = 35.0 # deg; the person further aside: turn on the spot first, no forward
@@ -62,6 +67,15 @@ JOIN_S = 2.0 # s a new "follow me" waits for the old loop to end
BEHIND_DEG = 180.0 # deg; the person's angle from the base is kept within +-this (right behind: one side)
ROUND_MAX_DEG = 225.0 # deg; the arm blocked turning toward them, the base turns the other way round when that is
# at most this far (the person within 45 deg of right behind, beside a wall)
SHIFT_STEP_M = 0.1 # m; blocked both ways on the spot: straight moves this long, then twice ... tried (each
SHIFT_MAX_M = 0.4 # ... way clear and the turn from its end clear), up to this (estimate; between fetch's
# NEAR_BACK_M 0.3 back-off and STRAIGHT_MAX_M 0.6 straight drive)
SHIFT_MS = fetch.MOVE_SPEED # m/s; that move's speed (a mission's straight move, 0.10)
FOCUS_FULL_DEG = 5.0 # deg; the base turns at full speed while the head's gaze is at most this far behind the
FOCUS_EDGE_DEG = 25.0 # person, slower and slower up to this (the head camera sees +-47, vision/person.py; the
# rest is the person's own width and the braking at TURN_ACCEL_RS2). Estimates
HEAD_DEG_S = 40.0 # deg/s; the head's top speed (audio/sing.py, measured): where a swing through the front
# meets a person coming round
HEAD_REL_MAX = 30.0 # deg; a turned waist unwinds until the person is this far from the base heading, the
# head looks for the rest (it never turns further out while following, unwind())
WAIST_FOLLOW_MAX = 90.0 # deg from the front; a waist turned further unwinds to this first (estimate)
@@ -81,7 +95,7 @@ HEAD_LEAD_MAX_S = 1.5 # much later at least, or as long as the last head m
# the base turning 0.6 rad/s goes 35-45 deg round in one move, which left the head off them
HEAD_VEL = 100 # speed and acceleration ratio %, the body API's maximum: the head's speed tops out from 50
# (~40 deg/s, audio/sing.py, measured at 100 / 100); the waist's moves keep WAIST_FOLLOW_VEL
HEAD_TIMEOUT_S = 5 # s; one head move (90 deg at ~40 deg/s takes ~2.5 s)
HEAD_TIMEOUT_S = 10 # s; one head move (a swing through the front, up to 360 deg at HEAD_DEG_S, takes ~9 s)
CHARGER = ("I'm on the charger, I can't follow you from here.", "충전 중이라 따라갈 수 없어요.")
ARM_BUSY = ("A program is running on my arm, so I can't follow you.", "팔에서 프로그램이 실행 중이라 따라갈 수 없어요.")
@@ -133,15 +147,16 @@ def stop():
return was
def speeds(distance, angle_deg, v_last, w_last=0.0, walk=0.0, go=True, dt=TICK_S):
def speeds(distance, angle_deg, v_last, w_last=0.0, walk=0.0, go=True, dt=TICK_S, w_max=TURN_MAX_RS):
"""(v m/s, w rad/s) toward a person `distance` m away at `angle_deg` (+ left), walking `walk` m/s away (- toward
the base): their speed plus SPEED_GAIN per m beyond the gap, never faster than lets the base stop at the gap,
changed smoothly (ACCEL_MS2 / DECEL_MS2).
Never forward inside the gap; nothing at all inside NEAR_STOP_M. go=False: only slow down (and turn)."""
Never forward inside the gap; nothing at all inside NEAR_STOP_M. go=False: only slow down (and turn). w_max: the
turn's cap now (focus())."""
if distance < NEAR_STOP_M:
return 0.0, 0.0
a = math.radians(angle_deg)
w_want = 0.0 if abs(angle_deg) < TURN_DEAD_DEG else max(-TURN_MAX_RS, min(TURN_MAX_RS, TURN_GAIN * a))
w_want = 0.0 if abs(angle_deg) < TURN_DEAD_DEG else max(-w_max, min(w_max, TURN_GAIN * a))
w = w_last + max(-TURN_ACCEL_RS2 * dt, min(TURN_ACCEL_RS2 * dt, w_want - w_last))
room = distance - FOLLOW_GAP_M
if room <= 0:
@@ -163,7 +178,7 @@ def blocked(v, w, arm_pts, pose=None):
x, y, th = pose or agv.pose()
th2 = th + w * BRAKE_S
x2, y2 = x + v * BRAKE_S * math.cos(th), y + v * BRAKE_S * math.sin(th)
if v > 0:
if v: # forward, or back (room to turn, _room_to_turn)
ahead = agv.clearance(x2, y2)
if ahead < BASE_CLEAR_M and ahead < agv.clearance(x, y):
return f"an obstacle {ahead:.2f} m from the base's way"
@@ -297,6 +312,64 @@ def unwind(w, b):
return 0.0 if goal * w <= 0.0 else math.copysign(min(abs(goal), abs(w)), w)
def focus(a, gaze):
"""The base's turn cap (rad/s) toward a person a deg from its heading while the head (waist + head) looks at gaze
deg: full while the gaze is within FOCUS_FULL_DEG of them, down to 0 at FOCUS_EDGE_DEG, but only when the turn takes
them further from the gaze (the head is further round than they are); turning them toward the gaze is never held.
The base turns ~57 deg/s at TURN_MAX_RS, the head ~40 (HEAD_DEG_S): unchecked it outran the head."""
off = (a - gaze + 180.0) % 360.0 - 180.0
if off * a >= 0.0 or abs(off) <= FOCUS_FULL_DEG:
return TURN_MAX_RS
return TURN_MAX_RS * max(0.0, (FOCUS_EDGE_DEG - abs(off)) / (FOCUS_EDGE_DEG - FOCUS_FULL_DEG))
def meet(gaze, a, side):
"""Where (deg from the base heading) the head, looking at gaze, meets the person a deg round (on `side`'s half, so
past +-180) while the base turns round that way at TURN_MAX_RS: the head can't follow them across its +-180, so it
swings through the front at HEAD_DEG_S toward them as they come round; never past the front, where they end."""
rate = -side * math.degrees(TURN_MAX_RS) # deg/s their angle changes (toward the front)
closing = math.copysign(HEAD_DEG_S, a - gaze) - rate
if (a - gaze) * closing <= 0.0:
return 0.0
m = a + rate * (a - gaze) / closing
return side * max(0.0, side * m)
def _room_to_turn(pose, p, turned, pts):
"""Signed length (m, + forward) of the shortest straight move, up to SHIFT_MAX_M, after which the base can turn on
the spot toward the person at the map point p one way round or the other (the arm's sweep clear, fetch.turn_hits),
with the way there clear for the base and the arm (blocked(), every fetch.PROBE_STEP_M) and the person still
FOLLOW_GAP_M away; at each length away from them first. None when there is none."""
x, y, th = pose
there = lambda s: (x + s * math.cos(th), y + s * math.sin(th), th)
for k in range(1, round(SHIFT_MAX_M / SHIFT_STEP_M) + 1):
for s in sorted((k * SHIFT_STEP_M, -k * SHIFT_STEP_M), key=lambda s: -_rel(there(s), p)[0]):
d, a = _rel(there(s), p)
if d < FOLLOW_GAP_M:
continue
n = math.ceil(abs(s) / fetch.PROBE_STEP_M - 1e-9)
# blocked() looks BRAKE_S ahead at speed v: v = s / BRAKE_S checks the point s m along
if any(blocked(s * i / n / BRAKE_S, 0.0, pts, pose) for i in range(1, n + 1)):
continue
for deg in (a, a - math.copysign(360.0, a)):
if abs(deg) <= ROUND_MAX_DEG and fetch.turn_hits(deg - turned, pose=there(s)) is None:
return s
return None
def torso_people():
"""The torso camera's fresh people [(distance m, waist-frame angle deg, age s, None)]: it turns with the waist and
sees ahead while the head swings off the person (the assistant's tracks come from the head camera only)."""
camview.want("torso")
try:
d = json.loads(camview.det_path("torso").read_text())
age = time.time() - d["time"]
return [(p["distance_m"], p["angle_deg"], age, None) for p in d["people"]
if p["distance_m"] is not None and age < STALE_S]
except (FileNotFoundError, ValueError, KeyError, TypeError):
return []
def _head_follow(target, done, lift, front, w, waist_free, stats):
"""Turn the head to the person at target[0] (deg from the base heading, pitch), the waist back toward the front
with it. Every move sends the head straight to them (up to +-BEHIND_DEG on the waist, never held back) and the waist
@@ -385,16 +458,20 @@ def _run(see, say, people):
turning, blocked_t, hold_v, log_t, odd_t, log_pose = False, None, 0.0, 0.0, 0.0, agv.pose()
side = 0.0 # +-1: turning round the other way (one way blocked), until
# the person is ahead again; 0: the near way
while not _stop.is_set():
shift, shifted = None, False # (pose, m): a straight move to where it can turn; made once
while not _stop.is_set(): # for a turn
now = time.time()
pose = agv.pose()
try:
turned = head.status()[1] - front
_, waist, yaw = head.status()[:3]
turned, gaze = waist - front, waist - front + yaw
except (ConnectionError, OSError, KeyError):
pass # the last reading: the body API may be busy
seen_from = _seen_from(pose, turned)
mine, was = see(), see
see, got = _pick(see, mine, people, person, seen_from, sig, now)
# the head off them (swinging round): the torso camera watches (no clothes: it only keeps where they are)
torso = [] if _fresh(mine) else [(see, *g) for g in torso_people()]
see, got = _pick(see, mine, lambda: (people() if people else []) + torso, person, seen_from, sig, now)
if see is not was:
fetch.log(f"follow: re-locked onto the person at {got[0]:.2f} m {got[1]:+.0f} deg")
elif got is None and _fresh(mine) and now - odd_t > LOG_S:
@@ -408,7 +485,7 @@ def _run(see, say, people):
if people else [0.0]
if got[3] is not None and all(e > JUMP_M or e < 0.05 for e in near): # alone (or themselves):
sig = reid.blend(sig, got[3]) # these clothes are theirs
if side:
if side or shift:
start_t = now # out of view while it turns round: lost counts after
if now - max(person.t or 0.0, start_t) >= LOST_S:
fetch.log("follow: lost the person")
@@ -429,13 +506,24 @@ def _run(see, say, people):
pose[2] + w * lead) # the base when the head gets there (v, w: this command)
yaw = _rel(then, person.at(now + lead))[1]
yaw = ref + (yaw - ref + 180.0) % 360.0 - 180.0
if side and abs(yaw) > 90.0: # the head ahead (not round past its +-180): the person
yaw = 0.0 # comes into view as the base turns
if side: # the head can't follow them round past its +-180: it
yaw = meet(gaze, a, side) # swings through the front to meet them coming round
target[0] = (max(-BEHIND_DEG, min(BEHIND_DEG, yaw)), HEAD_PITCH)
walk = float(person.vel @ np.array([math.cos(pose[2]), math.sin(pose[2])]))
go = now - person.t <= COAST_S and abs(turned) < WAIST_DRIVE_DEG # forward with the waist ahead
cmd = speeds(d, a, v, w, walk, go=go, dt=TICK_S)
spot = cmd[0] == 0.0 and cmd[1] != 0.0 # a turn on the spot (the person aside or behind)
cap = TURN_MAX_RS if side else focus(a, gaze) # never turning them out of the head's view
cmd = speeds(d, a, v, w, walk, go=go, dt=TICK_S, w_max=cap)
pts = [fetch._rot(turned) @ p for p in arm_pts] # the arm turned with the waist
if shift: # straight to where it can turn, then the turn checked again
(x0, y0, th0), m = shift
gone = (pose[0] - x0) * math.cos(th0) + (pose[1] - y0) * math.sin(th0)
if gone * math.copysign(1.0, m) >= abs(m) - 0.01:
shift, turning = None, False
else:
cmd = (math.copysign(min(SHIFT_MS, abs(v) + ACCEL_MS2 * TICK_S), m), 0.0)
spot = not shift and cmd[0] == 0.0 and cmd[1] != 0.0 # a turn on the spot (the person aside or behind)
if not spot and not shift and not turning:
shifted = False
if spot and not turning:
hit = fetch.turn_hits(a - turned) # the arm's whole sweep toward them (it is turned already)
other = a - math.copysign(360.0, a) # the other way round (right behind: about as far)
@@ -448,15 +536,19 @@ def _run(see, say, people):
elif hit is not None:
fetch.log(f"follow: turning {a:+.0f} deg my arm would hit something at "
f"({hit[0]:.2f}, {hit[1]:.2f})")
fetch._say(say, "My arm can't turn here, something is in its way.",
"팔이 부딪힐 수 있어서 돌 수 없어요.")
return
m = None if shifted else _room_to_turn(pose, person.at(now), turned, pts)
if m is None:
fetch._say(say, "My arm can't turn here, something is in its way.",
"팔이 부딪힐 수 있어서 돌 수 없어요.")
return
fetch.log(f"follow: no turn either way here; driving {m:+.2f} m to turn from there")
shift, shifted, spot = (pose, m), True, False
cmd = (0.0, 0.0)
turning = spot
if side and abs(a) < TURN_ONLY_DEG:
side = 0.0 # ahead again: angles go on from the head's target
# stopped by something: on again only when the speed it was stopped at is clear (no creeping up to it)
pts = [fetch._rot(turned) @ p for p in arm_pts] # the arm turned with the waist
why = cmd != (0.0, 0.0) and blocked(max(cmd[0], hold_v) if cmd[0] else 0.0, cmd[1], pts, pose)
why = cmd != (0.0, 0.0) and blocked(max(cmd[0], hold_v) if cmd[0] > 0 else cmd[0], cmd[1], pts, pose)
if why:
if blocked_t is None:
fetch.log(f"follow: standing, {why}")