Follow me: steady 10 Hz control with prediction between sightings, one kept-open base connection, head tracks on its own thread, the followed person kept by position and clothes; Lumi: no fate error, cut-off sentences joined, no promise without its action, song bank only on song words
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+178
-73
@@ -1,11 +1,16 @@
|
||||
"""Follow a person with the base: "follow me" ... "stop" (lumi.py follow_me).
|
||||
|
||||
The person is a track of the assistant's Vision; `see()` gives (distance m, angle deg + left, age s) or None once the
|
||||
track is gone, `people()` [(see, distance, angle, age)] of everyone in view: a track lost is swapped for a fresh one
|
||||
where the person was last (re-lock). The head turns toward the person (only this task moves the body meanwhile:
|
||||
head.owners), the base turns after it, so they stay in view when they walk to the side or behind. The base is driven with joy_control (one command lasts 0.5 s: a stuck loop stops the base by itself),
|
||||
the arm in look and the waist at the front as for every fetch drive. Every command is checked first: where the base
|
||||
and the arm would be BRAKE_S later must be clear (lidar + map, agv.clearance).
|
||||
The person is a track of the assistant's Vision: `see()` gives (distance m, angle deg + left, age s, clothes signature)
|
||||
or None once the track is gone, `people()` [(see, distance, angle, age, look)] of everyone in view. Every sighting is
|
||||
put on the map (base pose + distance, angle) with a smoothed walking velocity, so between sightings the base drives
|
||||
toward where the person is now, not where they were seen (no stop-and-go when frames are missed). A sighting far from
|
||||
that place is someone else (a passer-by who took the track over); a lost track is swapped for a fresh one near that
|
||||
place whose clothes are not clearly different (re-lock). The head turns toward the person in its own thread (only
|
||||
this task moves the body meanwhile: head.owners), the base turns after it, so they stay in view when they walk to the
|
||||
side or behind. 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 and the waist at the front as for every fetch drive. 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.
|
||||
|
||||
stop() from any thread: the event is set and the base is sent 0 at once
|
||||
"""
|
||||
@@ -19,27 +24,39 @@ import numpy as np
|
||||
|
||||
from robot import agv, head, motion
|
||||
from tasks import fetch
|
||||
from vision import reid
|
||||
|
||||
FOLLOW_GAP_M = 1.0 # m; keep this far from the person (camera distance); nearer: stand still
|
||||
FOLLOW_MAX_MS = 0.15 # m/s; forward speed cap (estimate, tune live)
|
||||
SPEED_GAIN = 0.8 # m/s per m the person is further than FOLLOW_GAP_M
|
||||
ACCEL_MS2 = 0.4 # m/s^2; speeding up at most this fast (slowing down at once)
|
||||
TURN_GAIN = 1.0 # rad/s per rad the person is off to the side
|
||||
TURN_MAX_RS = 0.5 # rad/s; turn speed cap (joy_control takes up to 1.0)
|
||||
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)
|
||||
FOLLOW_MAX_MS = 0.3 # m/s; forward cap, first live run slow (joy_control clamps at 0.5, docs/05); raise to 0.5 after it
|
||||
SPEED_GAIN = 0.8 # m/s per m the person is further than FOLLOW_GAP_M, on top of their walking speed
|
||||
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_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 = 30.0 # deg; the person further aside: turn on the spot first, no forward
|
||||
STALE_S = 0.5 # s; a sighting older than this: stand still and wait for a new one
|
||||
LOST_S = 2.0 # s without a sighting -> "I lost you" (lumi.Vision drops a track after its own LOST_S, 2 s)
|
||||
TURN_ONLY_DEG = 35.0 # deg; the person further aside: turn on the spot first, no forward
|
||||
STALE_S = 0.4 # s; a sighting older than this is not new (Vision gives one per head camera frame, 15/s)
|
||||
COAST_S = 1.0 # s after the last sighting the base drives on toward where they should be; then it slows
|
||||
LOST_S = 3.0 # s without a sighting -> "I lost you" (lumi.Vision drops a track after its own LOST_S, 2 s)
|
||||
VEL_TAU_S = 0.5 # s; time constant of the walking velocity filter (depth jumps a few cm per frame)
|
||||
WALK_MAX_MS = 2.0 # m/s; a faster walking estimate is noise
|
||||
JUMP_M = 0.5 # m; a sighting this far from where the person should be is someone else (estimate) ...
|
||||
GATE_GROW_MS = 0.7 # ... plus this much per s unseen (they may have turned): re-lock radius (estimate)
|
||||
SWITCH_M = 0.15 # m; another track must be this much nearer to where they should be to be taken (estimate)
|
||||
RELOCK_LOOK = 0.6 # reid.same below this: clearly other clothes, not taken (same person 0.66-0.91, reid.py)
|
||||
BRAKE_S = 0.5 # s; the base and the arm must be clear where this command would take them by then
|
||||
BLOCKED_END_S = 5.0 # s standing in front of an obstacle before giving up
|
||||
ARM_CLEAR_M = fetch.ARM_CLEAR_M # m; arm points kept this far from anything the lidar / map shows
|
||||
BASE_CLEAR_M = fetch.BASE_HALF_M + fetch.CLEAR_M # m; base centre kept this far, unless it gets further away
|
||||
TICK_S = 0.1 # s between commands (joy_control wants > 2 Hz)
|
||||
TICK_S = 0.1 # s between commands, steady (joy_control wants > 2 Hz; a tick takes ~10-30 ms)
|
||||
LOG_S = 1.0 # s between "follow: v ... " lines (what the next live run is read from)
|
||||
JOIN_S = 2.0 # s a new "follow me" waits for the old loop to end
|
||||
RELOCK_DEG = 25.0 # deg; a fresh track this near the lost person's last angle is taken as them (estimate)
|
||||
RELOCK_M = 0.8 # m; ... and this near their last distance (estimate)
|
||||
HEAD_YAW_MAX = 90.0 # deg; the head turns toward the person up to this far (+ left; body API allows 180)
|
||||
HEAD_YAW_MAX = 120.0 # deg; the head turns toward the person up to this far (+ left; body API allows 180)
|
||||
HEAD_PITCH = 10.0 # deg; head pitch while following, a little up toward a standing person (as "look at me")
|
||||
HEAD_STEP_DEG = 5.0 # deg; a new head move only when the person is this far from where the head was sent
|
||||
HEAD_STEP_DEG = 3.0 # deg; a new head move only when the target is this far from where the head was sent
|
||||
HEAD_LEAD_S = 0.3 # s; the head is sent where the person will be this much later (a moveto takes time)
|
||||
HEAD_VEL = 50 # speed ratio %; the head is at its full speed from 50 (audio/sing.py, measured)
|
||||
HEAD_TIMEOUT_S = 5 # s; one head move (90 deg at ~40 deg/s takes ~2.5 s)
|
||||
|
||||
@@ -85,34 +102,39 @@ def stop():
|
||||
return was
|
||||
|
||||
|
||||
def speeds(distance, angle_deg, v_last, dt=TICK_S):
|
||||
"""(v m/s, w rad/s) toward a person `distance` m away at `angle_deg` (+ left); never forward inside the gap."""
|
||||
if distance <= FOLLOW_GAP_M:
|
||||
return 0.0, 0.0 # close enough: stand still (no turn: the arm sweeps)
|
||||
def speeds(distance, angle_deg, v_last, w_last=0.0, walk=0.0, go=True, dt=TICK_S):
|
||||
"""(v m/s, w rad/s) toward a person `distance` m away at `angle_deg` (+ left), walking `walk` m/s away: their
|
||||
speed plus SPEED_GAIN per m beyond the gap, never faster than lets the base stop at the gap, changed smoothly.
|
||||
Never forward inside the gap; nothing at all inside NEAR_STOP_M. go=False: only slow down (and turn)."""
|
||||
if distance < NEAR_STOP_M:
|
||||
return 0.0, 0.0
|
||||
a = math.radians(angle_deg)
|
||||
w = 0.0 if abs(angle_deg) < TURN_DEAD_DEG else max(-TURN_MAX_RS, min(TURN_MAX_RS, TURN_GAIN * a))
|
||||
v = 0.0 if abs(angle_deg) > TURN_ONLY_DEG else min(FOLLOW_MAX_MS, SPEED_GAIN * (distance - FOLLOW_GAP_M),
|
||||
v_last + ACCEL_MS2 * dt)
|
||||
w_want = 0.0 if abs(angle_deg) < TURN_DEAD_DEG else max(-TURN_MAX_RS, min(TURN_MAX_RS, 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:
|
||||
return 0.0, w # at the gap: no forward now
|
||||
v_want = 0.0 if not go or abs(angle_deg) > TURN_ONLY_DEG else \
|
||||
min(FOLLOW_MAX_MS, max(0.0, walk) + SPEED_GAIN * room, math.sqrt(2 * DECEL_MS2 * room))
|
||||
v = min(v_last + ACCEL_MS2 * dt, max(v_last - DECEL_MS2 * dt, v_want))
|
||||
return max(0.0, v), w
|
||||
|
||||
|
||||
def blocked(v, w, arm_pts):
|
||||
"""True when the base or the arm would come too close to something within BRAKE_S at (v, w)."""
|
||||
x, y, th = agv.pose()
|
||||
def blocked(v, w, arm_pts, pose=None):
|
||||
"""Why the base or the arm would come too close to something within BRAKE_S at (v, w), None when clear."""
|
||||
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:
|
||||
ahead = agv.clearance(x2, y2)
|
||||
if ahead < BASE_CLEAR_M and ahead < agv.clearance(x, y):
|
||||
fetch.log(f"follow: an obstacle {ahead:.2f} m from the base's way")
|
||||
return True
|
||||
return f"an obstacle {ahead:.2f} m from the base's way"
|
||||
c, s = math.cos(th2), math.sin(th2)
|
||||
for p in arm_pts:
|
||||
px, py = x2 + c * p[0] - s * p[1], y2 + s * p[0] + c * p[1]
|
||||
if agv.clearance(px, py) < ARM_CLEAR_M:
|
||||
fetch.log(f"follow: my arm would hit something at ({px:.2f}, {py:.2f})")
|
||||
return True
|
||||
return False
|
||||
return f"my arm would hit something at ({px:.2f}, {py:.2f})"
|
||||
return None
|
||||
|
||||
|
||||
def ready():
|
||||
@@ -150,27 +172,78 @@ def prepare():
|
||||
return [np.asarray(p, float) for p in fetch.arm_points()]
|
||||
|
||||
|
||||
def _relock(people, last):
|
||||
"""The see() of the fresh track nearest to where the followed person was last (distance, angle), None when
|
||||
nobody is near. ponytail: by position only, appearance (reid) would tell two people apart."""
|
||||
d0, a0 = last
|
||||
near = [(abs(a - a0), see) for see, d, a, age in people()
|
||||
if age < STALE_S and abs(a - a0) <= RELOCK_DEG and abs(d - d0) <= RELOCK_M]
|
||||
return min(near, key=lambda n: n[0], default=(0, None))[1]
|
||||
def _to_map(pose, d, a):
|
||||
"""Map point (x, y) of a sighting d m away at a deg (+ left) from the base at pose (the camera ~ the base centre)."""
|
||||
x, y, th = pose
|
||||
return np.array([x + d * math.cos(th + math.radians(a)), y + d * math.sin(th + math.radians(a))])
|
||||
|
||||
|
||||
def _head_follow(target, done, lift, waist):
|
||||
def _rel(pose, p):
|
||||
"""(distance m, angle deg + left) of the map point p from the base at pose."""
|
||||
x, y, th = pose
|
||||
a = math.degrees(math.atan2(p[1] - y, p[0] - x) - th)
|
||||
return math.hypot(p[0] - x, p[1] - y), (a + 180.0) % 360.0 - 180.0
|
||||
|
||||
|
||||
class Person:
|
||||
"""Where the followed person is on the map: the last sighting (p, t) and a smoothed walking velocity."""
|
||||
|
||||
def __init__(self):
|
||||
self.p, self.t, self.vel = None, None, np.zeros(2)
|
||||
|
||||
def seen(self, p, t):
|
||||
if self.p is not None:
|
||||
if t <= self.t + 1e-3:
|
||||
return # the same sighting read again
|
||||
k = (t - self.t) / (VEL_TAU_S + t - self.t)
|
||||
vel = (1 - k) * self.vel + k * (p - self.p) / (t - self.t)
|
||||
speed = float(np.linalg.norm(vel))
|
||||
self.vel = vel if speed <= WALK_MAX_MS else vel * WALK_MAX_MS / speed
|
||||
self.p, self.t = p, t
|
||||
|
||||
def at(self, t):
|
||||
"""Where they should be at t (the walk is carried on for COAST_S at most)."""
|
||||
return self.p + self.vel * min(max(t - self.t, 0.0), COAST_S)
|
||||
|
||||
|
||||
def _fresh(got):
|
||||
return got is not None and got[2] < STALE_S
|
||||
|
||||
|
||||
def _pick(see, got, people, person, pose, sig, now):
|
||||
"""(see, sighting) of the followed person this tick: of their own track's sighting and every fresh one in view the
|
||||
nearest to where they should be, within JUMP_M (growing while unseen) and not in clearly other clothes. A track
|
||||
taken over by a passer-by jumps away from that place, the person's own new track is near it. Their own track is
|
||||
kept unless another is SWITCH_M nearer (the same track also comes in people()). Sighting None: none."""
|
||||
cands = [(see, got)] if _fresh(got) else []
|
||||
cands += [(s, tuple(g)) for s, *g in (people() if people else ()) if _fresh(g)]
|
||||
if person.p is None: # nothing to compare with yet: their own track
|
||||
return see, (got if _fresh(got) else None)
|
||||
gate = JUMP_M + GATE_GROW_MS * min(max(now - person.t - STALE_S, 0.0), LOST_S)
|
||||
best = (float("inf"), see, None)
|
||||
for s, g in cands:
|
||||
e = float(np.linalg.norm(_to_map(pose, *g[:2]) - person.at(now - g[2])))
|
||||
if e > gate or sig is not None and g[3] is not None and reid.same(sig, g[3]) < RELOCK_LOOK:
|
||||
continue
|
||||
e -= SWITCH_M if s is see else 0.0
|
||||
if e < best[0]:
|
||||
best = (e, s, g)
|
||||
return best[1:]
|
||||
|
||||
|
||||
def _head_follow(target, done, lift, waist, stats):
|
||||
"""Turn the head to target[0] (yaw, pitch) whenever it is HEAD_STEP_DEG from where it was sent last. One moveto
|
||||
at a time, from this thread only: overlapping calls hang the body API."""
|
||||
at a time, from this thread only: overlapping calls hang the body API. The drive loop never waits for it."""
|
||||
sent = None
|
||||
while not done.is_set():
|
||||
t = target[0]
|
||||
if t is None or sent is not None and max(abs(t[0] - sent[0]), abs(t[1] - sent[1])) < HEAD_STEP_DEG:
|
||||
done.wait(TICK_S)
|
||||
done.wait(TICK_S / 2)
|
||||
continue
|
||||
try:
|
||||
t0 = time.time()
|
||||
head.moveto([lift, waist, *t], vel=HEAD_VEL, acc=HEAD_VEL, timeout=HEAD_TIMEOUT_S)
|
||||
sent = t
|
||||
sent, stats["head"], stats["head_ms"] = t, t[0], round(1000 * (time.time() - t0))
|
||||
except (ConnectionError, OSError, ValueError, KeyError) as e:
|
||||
fetch.log(f"follow: head error {e!r}")
|
||||
done.wait(1.0)
|
||||
@@ -178,54 +251,86 @@ def _head_follow(target, done, lift, waist):
|
||||
|
||||
def _run(see, say, people):
|
||||
"""The loop: until stop(), lost, or something in the way. Any failure ends with the base at 0 and a reason said."""
|
||||
v, turning = 0.0, False
|
||||
done, target = threading.Event(), [None]
|
||||
v = w = 0.0
|
||||
done, target, stats = threading.Event(), [None], {"head": None, "head_ms": None}
|
||||
head.owners = (threading.current_thread(),)
|
||||
person = Person()
|
||||
try:
|
||||
got = see() # where they are now: prepare() may turn the waist
|
||||
sig = got[3] if got else None
|
||||
if _fresh(got):
|
||||
person.seen(_to_map(agv.pose(), *got[:2]), time.time() - got[2])
|
||||
arm_pts = prepare()
|
||||
lift, waist = head.status()[:2]
|
||||
head_thread = threading.Thread(target=_head_follow, args=(target, done, lift, waist), daemon=True,
|
||||
head_thread = threading.Thread(target=_head_follow, args=(target, done, lift, waist, stats), daemon=True,
|
||||
name="follow head")
|
||||
head.owners += (head_thread,)
|
||||
head_thread.start()
|
||||
seen_t, last = time.time(), None
|
||||
start_t = next_t = time.time() # lost counts from here: prepare() takes a few seconds
|
||||
turning, blocked_t, hold_v, log_t, odd_t, log_pose = False, None, 0.0, 0.0, 0.0, agv.pose()
|
||||
while not _stop.is_set():
|
||||
got = see()
|
||||
now = time.time()
|
||||
if (got is None or got[2] >= STALE_S) and last and people:
|
||||
new = _relock(people, last)
|
||||
if new is not None:
|
||||
fetch.log(f"follow: re-locked onto the person at {last[1]:+.0f} deg")
|
||||
see, got = new, new()
|
||||
if got is not None and got[2] < STALE_S:
|
||||
seen_t, last = now - got[2], got[:2]
|
||||
cmd = speeds(got[0], got[1], v)
|
||||
target[0] = (max(-HEAD_YAW_MAX, min(HEAD_YAW_MAX, got[1])), HEAD_PITCH)
|
||||
else: # stale or gone (Vision drops a track unseen for 2 s)
|
||||
if now - seen_t >= LOST_S:
|
||||
fetch.log("follow: lost the person")
|
||||
fetch._say(say, *LOST)
|
||||
return
|
||||
cmd = (0.0, 0.0)
|
||||
pose = agv.pose()
|
||||
mine, was = see(), see
|
||||
see, got = _pick(see, mine, people, person, pose, 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:
|
||||
odd_t = now
|
||||
fetch.log(f"follow: a sighting at {mine[0]:.2f} m {mine[1]:+.0f} deg is not them (someone else)")
|
||||
if _fresh(got):
|
||||
p = _to_map(pose, *got[:2])
|
||||
person.seen(p, now - got[2])
|
||||
near = [float(np.linalg.norm(_to_map(pose, *o[1:3]) - p)) for o in people() if _fresh(o[1:])] \
|
||||
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 now - max(person.t or 0.0, start_t) >= LOST_S:
|
||||
fetch.log("follow: lost the person")
|
||||
fetch._say(say, *LOST)
|
||||
return
|
||||
if person.p is None: # not seen yet (only before the first sighting)
|
||||
cmd, d, a, walk = (0.0, 0.0), 0.0, 0.0, 0.0
|
||||
else:
|
||||
d, a = _rel(pose, person.at(now))
|
||||
yaw = _rel(pose, person.at(now + HEAD_LEAD_S))[1]
|
||||
target[0] = (max(-HEAD_YAW_MAX, min(HEAD_YAW_MAX, yaw)), HEAD_PITCH)
|
||||
walk = float(person.vel @ np.array([math.cos(pose[2]), math.sin(pose[2])]))
|
||||
cmd = speeds(d, a, v, w, walk, go=now - person.t <= COAST_S, dt=TICK_S)
|
||||
spot = cmd[0] == 0.0 and cmd[1] != 0.0 # a turn on the spot (the person aside or behind)
|
||||
if spot and not turning:
|
||||
hit = fetch.turn_hits(got[1]) # the arm's whole sweep toward them
|
||||
hit = fetch.turn_hits(a) # the arm's whole sweep toward them
|
||||
if hit is not None:
|
||||
fetch.log(f"follow: turning {got[1]:+.0f} deg my arm would hit something at "
|
||||
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
|
||||
turning = spot
|
||||
if cmd != (0.0, 0.0) and blocked(*cmd, arm_pts):
|
||||
fetch._say(say, "Something is in my way, so I stopped.", "앞이 막혀서 멈췄어요.")
|
||||
return
|
||||
# stopped by something: on again only when the speed it was stopped at is clear (no creeping up to it)
|
||||
why = cmd != (0.0, 0.0) and blocked(max(cmd[0], hold_v) if cmd[0] else 0.0, cmd[1], arm_pts, pose)
|
||||
if why:
|
||||
if blocked_t is None:
|
||||
fetch.log(f"follow: standing, {why}")
|
||||
blocked_t, hold_v, cmd = blocked_t or now, max(hold_v, cmd[0]), (0.0, 0.0)
|
||||
if now - blocked_t >= BLOCKED_END_S:
|
||||
fetch._say(say, "Something is in my way, so I stopped.", "앞이 막혀서 멈췄어요.")
|
||||
return
|
||||
elif cmd != (0.0, 0.0):
|
||||
blocked_t, hold_v = None, 0.0
|
||||
with _send:
|
||||
if _stop.is_set():
|
||||
return
|
||||
_joy(*cmd)
|
||||
v = cmd[0]
|
||||
_stop.wait(TICK_S)
|
||||
v, w = cmd
|
||||
if now - log_t >= LOG_S: # driven: what the base really did since the last line (pose)
|
||||
driven = math.hypot(pose[0] - log_pose[0], pose[1] - log_pose[1]) / max(now - log_t, 1e-3)
|
||||
log_t, log_pose = now, pose
|
||||
fetch.log(f"follow: v {v:.2f} w {w:+.2f} driven {driven:.2f} m/s | person {d:.2f} m {a:+.0f} deg, seen "
|
||||
f"{now - (person.t or now):.2f} s ago, walking {walk:+.2f} m/s | head "
|
||||
f"{stats['head'] or 0:+.0f} deg ({stats['head_ms']} ms) | tick {1000 * (time.time() - now):.0f} ms")
|
||||
next_t = max(next_t + TICK_S, time.time()) # a steady rate; a slow tick is not caught up
|
||||
_stop.wait(next_t - time.time())
|
||||
except fetch.Stopped:
|
||||
pass
|
||||
except fetch.FetchError as e:
|
||||
|
||||
Reference in New Issue
Block a user