From 41f7bd5aca4db0c125870a36bc5189078af2e026 Mon Sep 17 00:00:00 2001 From: Cutshion Date: Fri, 23 Oct 2026 16:02:27 +0900 Subject: [PATCH] Follow keeps up at walking pace (0.5 m/s, slow near the person, faster when the gap grows); short moves end a running follow, drive without pauses and say honestly when the wheels did not move; a STOP ends a waiting body move at once Co-Authored-By: Claude Opus 5.5 --- docs/TUNING.md | 6 +- tasks/fetch.py | 132 ++++++++++++++++++++++++++++++++---------- tasks/follow.py | 23 ++++++-- tests/test_driving.py | 77 +++++++++++++++++++++++- tests/test_fetch.py | 3 + tests/test_follow.py | 37 ++++++++++++ 6 files changed, 235 insertions(+), 43 deletions(-) diff --git a/docs/TUNING.md b/docs/TUNING.md index 7fec142..66589a1 100644 --- a/docs/TUNING.md +++ b/docs/TUNING.md @@ -48,7 +48,8 @@ Every value here is a constant at the top of its file, with a comment on what it | `assistant/lumi.py` | `FOLLOW_VOICE_DEG`, `FOLLOW_SORRY_S` | 35°, 20 s | While following, a voice this near the followed person's angle is theirs (estimate); the "I'm following someone" line at most this often | | `tasks/follow.py` | `FOLLOW_GAP_M`, `NEAR_STOP_M` | 1.0 m, 0.8 m | "Follow me": camera distance kept to the person (nearer: no forward); nearer than `NEAR_STOP_M` no turn either. Estimate | | `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)); 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`. Estimates | +| `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, change limit, no turn within, no forward beyond (turn first) | | `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 | @@ -110,7 +111,8 @@ Every value here is a constant at the top of its file, with a comment on what it | `assistant/lumi.py` | `REACH_FRONT_DEG` | 20° | "Reach out to me": a person further to the side is turned to first (Patrol) or asked to stand in front (Greeter). Estimate | | `tasks/fetch.py` | `POINT_STEPS_MM` | 0, ±50, ±100 mm | "Put it here" with pointing: spot grid around the pointed point | | `tasks/fetch.py` | `RECEIVE_DI_S` | 0.5 s | `receive`: the finger sensor (DI_1) must see something this long before Lumi asks the person to let go (the hand closes on their "okay") | -| `tasks/fetch.py` | `MOVE_SEGMENT_M` | 0.25 m | A mission `move` is driven in pieces this long; the way of the base and the arm is checked again before each | +| `tasks/fetch.py` | `MOVE_SEGMENT_M` | 0.25 m | A mission `move` is driven in pieces this long; the way of the base and the arm is checked again before each (also what the base rolls on meanwhile, `JOY_HOLD_S` 0.5 s); the base does not stop between pieces | +| `tasks/fetch.py` | `MOVED_MIN_PART`, `MOVED_CHECK_M` | 0.5, 0.1 m | A move whose base pose went less than this part of the distance driven by time stops and says the wheels did not move (checked from 0.1 m on). Estimate | ## Placing diff --git a/tasks/fetch.py b/tasks/fetch.py index f32702b..85d6fed 100644 --- a/tasks/fetch.py +++ b/tasks/fetch.py @@ -151,6 +151,12 @@ RECEIVE_WAIT_S = 30.0 # s; receive: nothing put in the hand by then -> th RECEIVE_DI_S = 0.5 # s; DI_1 (finger sensor) sees something this long -> Lumi asks the person to let go; # the hand closes only on their "okay" / Release (never on fingers still in it) MOVE_SEGMENT_M = 0.25 # m; a mission move is driven in pieces this long, the way (base and arm) checked before each + # (the base rolls on while the next piece is checked: no stop between pieces) +JOY_HOLD_S = 0.5 # s; one joy_control command lasts this long, then the base stops by itself (docs/05) +MOVED_MIN_PART = 0.5 # a move whose base pose went less than this part of the distance driven by time: the base + # did not follow the commands (a second joy_control sender, 2026-10-23) -> stop, say so + # (estimate; pose jitter and lag are a few cm) +MOVED_CHECK_M = 0.1 # m; ... checked once this much is driven by time (shorter moves are within the jitter) WAIT_POLL_S = 0.05 # s between looks at stop / go on / an instruction while a mission waits (a wait step) LOG_LINES = 40 POINT_STEPS_MM = (0, -50, 50, -100, 100) # "put it here" with pointing: spots on this grid around the point (estimate) @@ -513,7 +519,7 @@ def _lift_to(mm): return _go_on() log(f"lift {lift:.0f} -> {mm:.0f} mm") - head.moveto([mm, waist, yaw, pitch], vel=LIFT_VEL, acc=20, timeout=90) + _body_move([mm, waist, yaw, pitch], vel=LIFT_VEL, acc=20, timeout=90) skills.moved() _go_on() @@ -576,6 +582,27 @@ def depth_hits(deg, camera="torso", T=None): return None +def _body_move(pos, **kw): + """head.moveto that a STOP does not wait for: the body API answers only when the motion ends, and a hung call took + its full timeout (2026-10-23: two 30 s timeouts in a row kept a stopped reach "busy" for a minute). The call ends in + its own thread (the body finishes the move anyway); a STOP raises Stopped at once.""" + out = {} + + def run(): + try: + out["reply"] = head.moveto(pos, **kw) + except Exception as e: + out["error"] = e + t = threading.Thread(target=run, daemon=True) + t.start() + while t.is_alive(): + if _stop.wait(0.1): + raise Stopped() + if "error" in out: + raise out["error"] + return out.get("reply") + + def _head_to(yaw, pitch): """Turn the head (lift and waist stay) unless it is there; its frames are then read from after the move (skills.moved).""" @@ -583,7 +610,7 @@ def _head_to(yaw, pitch): camview.want("head") if abs(y0 - yaw) < 1.0 and abs(p0 - pitch) < 1.0: return - head.moveto([lift, waist, yaw, pitch]) + _body_move([lift, waist, yaw, pitch]) skills.moved("head") @@ -648,7 +675,7 @@ def _waist_to(rel): return False lift, waist, yaw, pitch = head.status() log(f"waist {waist:.0f} -> {waist + d:.0f} deg") - head.moveto([lift, waist + d, yaw, pitch], vel=WAIST_VEL, acc=20, timeout=60) + _body_move([lift, waist + d, yaw, pitch], vel=WAIST_VEL, acc=20, timeout=60) skills.moved() _go_on() return True @@ -1068,27 +1095,36 @@ def set_speed(factor): return factor -def _straight_clear(dist, speed=None): +def _straight_clear(dist, speed=None, stop=True): """Drive straight by `dist` m (+ forward), only as far as the base keeps BASE_HALF_M + CLEAR_M from every obstacle the sensors and the map show (checked every PROBE_STEP_M before it starts; getting further from one is fine). - Returns the distance driven (m).""" + Returns the distance driven (m). stop=False: the base is left rolling (_straight) when the way is clear also + rolling_lead() further, the most it rolls on while the next piece is checked.""" _waist_front() x, y, th = agv.pose() sign = 1.0 if dist > 0 else -1.0 - free, last = 0.0, agv.clearance(x, y) - for k in range(1, math.ceil(abs(dist) / PROBE_STEP_M - 1e-9) + 1): - at = min(k * PROBE_STEP_M, abs(dist)) # every PROBE_STEP_M, and the end itself (a 1 cm move too) + reach = abs(dist) + (0.0 if stop else rolling_lead(speed)) + free, last, blocked = 0.0, agv.clearance(x, y), False + for k in range(1, math.ceil(reach / PROBE_STEP_M - 1e-9) + 1): + at = min(k * PROBE_STEP_M, reach) # every PROBE_STEP_M, and the end itself (a 1 cm move too) c = agv.clearance(x + sign * at * math.cos(th), y + sign * at * math.sin(th)) if c < BASE_HALF_M + CLEAR_M and c < last: log(f"an obstacle {c:.2f} m from the way after {free:.2f} m") + blocked = True break free, last = at, c + free = min(free, abs(dist)) if free > 0: log(f"straight {sign * free:+.2f} m") - free = _straight(sign * free, speed or AXIS_SPEED) + free = _straight(sign * free, speed or AXIS_SPEED, stop=stop or blocked) return free +def rolling_lead(speed): + """How far (m) the base can roll on at `speed` (m/s or a function) after its last joy_control command.""" + return (speed() if callable(speed) else speed or AXIS_SPEED) * JOY_HOLD_S + + def _approach(obj, yaw, tried, goal=PICK_ZONE_CENTRE, holding=False): """One base move that should bring `obj` (x, y in the arm frame) to `goal`: turn on the spot and drive straight when the way is short and free; else navigation (around obstacles) to a stand-off point on a side of it not in @@ -1814,6 +1850,11 @@ def _new_run(): def _launch(fn, *a): global _thread + from tasks import follow # imported here: follow imports this module + if follow.running(): # its joy_control commands would fight this task's (2026-10-23: + log("follow: ended for a task") # "go back 1 m" while following did not move and said done) + follow.stop() + follow._thread.join(follow.JOIN_S) def run(): fn(*a) @@ -2229,26 +2270,40 @@ def _move(direction, m, holding): _step(f"driving {m:g} m {'back' if direction == 'back' else 'forward'}") speed = (lambda: AXIS_SPEED) if m < SHORT_MOVE_M else (lambda: MOVE_SPEED * speed_factor) # "faster" while on it done = 0.0 - while m - done > 1e-3: - _go_on() - piece = min(MOVE_SEGMENT_M, m - done) - hit = line_hits(sign * piece) - if hit is not None: - raise FetchError(f"My arm would hit something: I stopped after {done:.2f} m of {m:g}.", - f"팔이 부딪힐 것 같아서 {done:.2f}미터에서 멈췄어요.") - cut = _pause.is_set - got = _straight_clear(sign * piece, speed) - done += got - if cut(): # someone stopped me: wait, then the rest of the way, checked again + x0, y0, th0 = agv.pose() + try: + while m - done > 1e-3: _go_on() - continue - if holding and not gripper.state()["holding"]: - skills._held.clear() - _set(holding=False) - raise FetchError("I dropped it.", "떨어뜨렸어요.") - if got < piece - 1e-3: - raise FetchError(f"Something is in my way: I stopped after {done:.2f} m of {m:g}.", - f"앞에 뭔가 있어서 {done:.2f}미터에서 멈췄어요.") + piece = MOVE_SEGMENT_M if m - done > MOVE_SEGMENT_M + rolling_lead(speed) else m - done + last = piece == m - done # (no last piece shorter than what rolls on during a check) + hit = line_hits(sign * (piece + (0.0 if last else rolling_lead(speed)))) + if hit is not None: + raise FetchError(f"My arm would hit something: I stopped after {done:.2f} m of {m:g}.", + f"팔이 부딪힐 것 같아서 {done:.2f}미터에서 멈췄어요.") + cut = _pause.is_set + got = _straight_clear(sign * piece, speed, stop=last) + done += got + x, y, _ = agv.pose() + real = sign * ((x - x0) * math.cos(th0) + (y - y0) * math.sin(th0)) + if done >= MOVED_CHECK_M and real < MOVED_MIN_PART * done: + log(f"the base went {real:.2f} m of {done:.2f} m driven") + raise FetchError(f"My wheels did not move as they should, so I stopped after {max(0.0, real):.2f} m " + f"of {m:g}.", f"바퀴가 제대로 움직이지 않아서 {max(0.0, real):.2f}미터에서 멈췄어요.") + if cut(): # someone stopped me: wait, then the rest of the way, checked again + _go_on() + continue + if holding and not gripper.state()["holding"]: + skills._held.clear() + _set(holding=False) + raise FetchError("I dropped it.", "떨어뜨렸어요.") + if got < piece - 1e-3: + raise FetchError(f"Something is in my way: I stopped after {done:.2f} m of {m:g}.", + f"앞에 뭔가 있어서 {done:.2f}미터에서 멈췄어요.") + finally: + if _rolling["v"]: # left rolling between pieces: an error or STOP stops it now + _rolling["v"] = 0.0 + agv.request("/api/joy_control?linear_velocity=0&angular_velocity=0") + skills.moved(after=COAST_S) def _reach_to_hand(p, short=RECEIVE_SHORT_MM): @@ -2474,24 +2529,37 @@ def end(timeout=5.0): # ---------- measuring the arm's yaw on the base ---------- -def _straight(dist_m, speed=None): +_rolling = {"v": 0.0, "t": 0.0, "sent": 0.0} # a straight drive left rolling (stop=False): its signed speed (m/s), + # the end of its counted time and when its last command went out + + +def _straight(dist_m, speed=None, stop=True): """Drive straight by dist_m (+ forward) at `speed` m/s (AXIS_SPEED; a function: read again every 0.1 s, so a speed change applies at once) with joy_control (no turning), then stop. The last command lasts only the time left (no overshoot of up to 0.1 s). Stopped() on STOP; a pause stops it early. Returns the distance driven (m, - by time).""" + by time). stop=False (_move's pieces): when it got all the way the base is left rolling at its speed; the next + _straight the same way counts what it rolled meanwhile (at most JOY_HOLD_S) as driven.""" if stay_put: raise FetchError(*NEED_PATROL) rate = speed if callable(speed) else (lambda: speed or AXIS_SPEED) done, t = 0.0, time.time() + if _rolling["v"] * dist_m > 0: # rolled on since the last piece's time ended, until its command ran out + done = min(abs(dist_m), abs(_rolling["v"]) * max(0.0, min(t, _rolling["sent"] + JOY_HOLD_S) - _rolling["t"])) + _rolling["v"], v, sent = 0.0, 0.0, t try: while abs(dist_m) - done > 1e-4 and not _stop.is_set() and not _pause.is_set(): - v = rate() + v, sent = rate(), time.time() agv.request(f"/api/joy_control?linear_velocity={v if dist_m > 0 else -v:.3f}&angular_velocity=0") time.sleep(max(0.0, min(0.1, (abs(dist_m) - done) / v - (time.time() - t)))) now = time.time() done, t = done + v * (now - t), now finally: - agv.request("/api/joy_control?linear_velocity=0&angular_velocity=0") + if stop or abs(dist_m) - done > 1e-4 or not v: # cut short (STOP, pause, error): stopped now + agv.request("/api/joy_control?linear_velocity=0&angular_velocity=0") + else: + _rolling.update(v=v if dist_m > 0 else -v, t=t, sent=sent) + if _rolling["v"]: + return min(abs(dist_m), done) skills.moved(after=COAST_S) # frames from after the base has coasted to a stop if _stop.is_set(): raise Stopped() diff --git a/tasks/follow.py b/tasks/follow.py index bca5b65..3b39047 100644 --- a/tasks/follow.py +++ b/tasks/follow.py @@ -28,8 +28,12 @@ from vision import 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) -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 +FOLLOW_MAX_MS = 0.5 # m/s; forward cap = joy_control's own clamp (docs/05). At 0.3 a person walking 0.6-0.75 m/s + # got 4 m ahead (2026-10-23 15:06): the cap, not the gain, was the lag +SPEED_GAIN = 0.8 # m/s per m the person is further than FOLLOW_GAP_M, on top of their walking speed (signed: + # coming back toward the base slows it). A standing person: 0.4 m/s 0.5 m beyond the gap +REACT_S = 0.3 # s; from a person stopping to the base braking (frame, track, tick, command); the speed is + # kept low enough to stop at the gap after this plus braking at DECEL_MS2 (estimate) 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 @@ -74,7 +78,7 @@ SEEN_MAX_S = 1.5 # s; an older sighting does not tell where they are STOP_WORDS = re.compile(r"\b(stop|halt|freeze|enough|wait|hold on|stay(?! with)|don'?t (move|follow)|no more|" - r"that'?s (enough|far enough|it))\b|멈춰|그만|정지|스톱|거기 ?(있어|서|계세요|있으세요)|기다려|" + r"that'?s (enough|far enough|it))\b|멈춰|그만|정지|스톱|스탑|거기 ?(있어|서|계세요|있으세요)|기다려|" r"여기 ?(있어|서)|따라오지 ?마|됐어|^\s*서[\s,!.]*$", re.I) # end following (only while it runs) @@ -111,8 +115,9 @@ def stop(): 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. + """(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).""" if distance < NEAR_STOP_M: return 0.0, 0.0 @@ -123,11 +128,17 @@ def speeds(distance, angle_deg, v_last, w_last=0.0, walk=0.0, go=True, dt=TICK_S 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)) + min(FOLLOW_MAX_MS, walk + SPEED_GAIN * room, _brake_speed(room)) v = min(v_last + ACCEL_MS2 * dt, max(v_last - DECEL_MS2 * dt, v_want)) return max(0.0, v), w +def _brake_speed(room): + """Fastest speed (m/s) from which the base still stops within `room` m: REACT_S at it, then DECEL_MS2 braking.""" + dr = DECEL_MS2 * REACT_S + return math.sqrt(dr * dr + 2 * DECEL_MS2 * room) - dr + + 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() diff --git a/tests/test_driving.py b/tests/test_driving.py index 3267485..0dd658b 100644 --- a/tests/test_driving.py +++ b/tests/test_driving.py @@ -1,6 +1,7 @@ """Self-check of distances and speed by voice (assistant/driving.py, brain.intent's moves, fetch's straight move and speed) with a fake base, no robot: python tests/test_driving.py""" import tempfile +import threading import time from pathlib import Path @@ -118,13 +119,33 @@ assert driving.say_speed(1, 1.2, True) == "Okay, a bit faster: 120 percent of my assert driving.say_speed(1, fetch.SPEED_MAX, False) == "I'm already at my top speed." # ---------- a 1 cm move is driven, and the last joy_control command lasts only the time left ---------- -pose = [0.0, 0.0, 0.0] -fetch.agv.pose = lambda: tuple(pose) +# a fake base (heading 0): its x follows the joy_control speed, each command for JOY_HOLD_S; no wheels: it stays +base = {"x": 0.0, "v": 0.0, "t": time.time(), "wheels": True} + + +def roll(): + now = time.time() + dt, base["t"] = now - base["t"], now + if base["wheels"]: + base["x"] += base["v"] * min(dt, fetch.JOY_HOLD_S) + if dt > fetch.JOY_HOLD_S: + base["v"] = 0.0 + + +def joy_request(cmd, timeout=3.0): + if "joy_control" in cmd: + roll() + base["v"] = float(cmd.split("linear_velocity=")[1].split("&")[0]) + joy.append((time.time(), cmd)) + return {} + + +fetch.agv.pose = lambda: (roll(), (base["x"], 0.0, 0.0))[1] fetch.agv.clearance = lambda x, y: 5.0 fetch._waist_front = lambda: None real_moved, skills.moved = skills.moved, lambda after=0.0: None joy = [] -fetch.agv.request = lambda cmd, timeout=3.0: joy.append((time.time(), cmd)) or {} +fetch.agv.request = joy_request for dist in (0.01, 0.03, 0.07, 0.37): joy.clear() t0 = time.time() @@ -150,7 +171,57 @@ for m, v in ((0.01, "-0.050"), (0.3, "-0.120")): vel = {c.split("linear_velocity=")[1].split("&")[0] for _, c in joy} - {"0"} assert vel == {v}, (m, vel) fetch.speed_factor = 1.0 + +# _move rolls on between its pieces (no stop-and-go), the time spent checking counted as driven: 0.8 m is 0.8 m +STOP = "linear_velocity=0&angular_velocity=0" +fetch.MOVE_SPEED = 0.5 +real_line_hits = fetch.line_hits +fetch.line_hits = lambda d: time.sleep(0.15) # a slow check of the arm's way +joy.clear() +x0 = base["x"] +fetch._move("back", 0.8, holding=False) +time.sleep(fetch.JOY_HOLD_S + 0.05) +roll() +stops = [k for k, (_, c) in enumerate(joy) if c.endswith(STOP)] +assert stops == [len(joy) - 1], stops # one stop: at the end +assert abs((x0 - base["x"]) - 0.8) < 0.03, x0 - base["x"] +assert fetch._rolling["v"] == 0.0 + +# the base does not follow the commands (2026-10-23: a follow loop sent its own): said, stopped, never "done" +base["wheels"] = False +joy.clear() +try: + fetch._move("back", 1.0, holding=False) + raise AssertionError("a move the base did not drive ended as done") +except fetch.FetchError as e: + assert "did not move" in e.args[0] and "0.00 m of 1" in e.args[0], e.args +assert joy[-1][1].endswith(STOP) and fetch._rolling["v"] == 0.0, joy[-3:] +base["wheels"] = True + +# something steps into the way after the first piece: stops at once (also while rolling), says how far it got +clear_at = {"x": base["x"] - 0.3} +fetch.agv.clearance = lambda x, y: 5.0 if x > clear_at["x"] else 0.1 +joy.clear() +try: + fetch._move("back", 1.0, holding=False) + raise AssertionError("drove into an obstacle") +except fetch.FetchError as e: + assert "in my way" in e.args[0], e.args +time.sleep(fetch.JOY_HOLD_S + 0.05) +assert base["x"] > clear_at["x"] and joy[-1][1].endswith(STOP), (base, clear_at) # stopped before it +fetch.agv.clearance = lambda x, y: 5.0 +fetch.line_hits = real_line_hits fetch.agv.request, skills.moved = real_request, real_moved + +# a task (a move by voice) ends a follow that runs first: two joy_control senders must never fight +from tasks import follow +follow._stop.clear() +follow._thread = threading.Thread(target=follow._stop.wait, daemon=True) +follow._thread.start() +follow._joy = lambda v, w: None +fetch._launch(lambda: None) +assert not follow.running() and follow._stop.is_set() +fetch._thread.join(1.0) # "again" / "a bit more" right after a turn (2026-10-21) _turn = driving.last_command([("turn left", "ACTION: turn left 90")]) assert _turn == ("turn", "left 90"), _turn diff --git a/tests/test_fetch.py b/tests/test_fetch.py index 396e2ea..7f332f1 100644 --- a/tests/test_fetch.py +++ b/tests/test_fetch.py @@ -135,6 +135,7 @@ fetch.agv.request = lambda cmd, **kw: (calls.append(("joy", cmd.split("linear_ve room = {"clear": lambda x, y: 1.0, "probe": lambda x, y: (float("inf"), float("inf"))} fetch.agv.probe = lambda x, y: room["probe"](x, y) fetch.AXIS_SPEED, fetch.COAST_S = 100.0, 0.0 +fetch.MOVED_CHECK_M = float("inf") # this fake base never moves its pose (the check: test_driving.py) arm = {"j": [5.0] * 6} # the fake arm's joints: away from every pose fetch.motion.go = lambda name, *a, **k: (calls.append(("pose", name)), arm.update(j=list(fetch.motion.poses()[name]))) fetch.motion.stop = lambda: calls.append(("arm stop",)) @@ -1000,7 +1001,9 @@ for bad in ([P(do="give")], [P(do="place", on=None)], [P(do="go", to="p9")], [P( assert not any(c[0] in ("go", "go_point", "joy", "pose") for c in calls), calls # a move is driven in pieces, each checked again; back on the charger is refused reset() +hold, fetch.JOY_HOLD_S = fetch.JOY_HOLD_S, 0.0005 # at this fake 100 m/s a real hold would roll the whole way st = run(fetch.run_steps, [P(do="move", dir="forward", m=0.6)], say) +fetch.JOY_HOLD_S = hold assert st["status"] == "done" and [c for c in calls if c[0] == "joy"].count(("joy", "100.000")) == 3, calls # 0.25+0.25+0.1 fetch.agv.status = lambda: {"move_status": "succeeded", "charge_state": True} st = run(fetch.run_steps, [P(do="move", dir="back", m=0.5)], say) diff --git a/tests/test_follow.py b/tests/test_follow.py index 3ec74af..3370b65 100644 --- a/tests/test_follow.py +++ b/tests/test_follow.py @@ -592,4 +592,41 @@ L.follow_me = lambda: called.append(1) L.mission_running = lambda: False L.brain, L.markers = types.SimpleNamespace(history=[]), M assert L.quick_command("follow me") and called +# the speed law (live 2026-10-23: capped at 0.3 m/s the base fell 4 m behind a person walking 0.6-0.75 m/s) +def settle(d, walk, v=0.0, n=60): + for _ in range(n): + v = follow.speeds(d, 0.0, v, 0.0, walk, dt=0.1)[0] + return v + + +assert follow.FOLLOW_MAX_MS == 0.5 # joy_control's own clamp (docs/05) +assert settle(1.8, 0.7) == follow.FOLLOW_MAX_MS # far behind a walker: full speed +assert abs(settle(1.5, 0.0) - 0.4) < 1e-6 # standing 0.5 m beyond the gap: gentle +assert settle(1.1, 0.0) < 0.1 and settle(1.0, 0.5) == 0.0 # near the gap: slow; at it: none +assert settle(1.3, -0.4, v=0.4) == 0.0 # coming back toward the base: slows +vs = [settle(1.0 + r, 0.3) for r in (0.1, 0.3, 0.6, 1.0)] +assert vs == sorted(vs) and vs[-1] == follow.FOLLOW_MAX_MS, vs # faster the further behind +v, prev = 0.0, [] # speeds up and eases back gradually +for d in [3.0] * 20 + [1.2] * 20: + prev.append(v) + v = follow.speeds(d, 0.0, v, 0.0, 0.0, dt=0.1)[0] +steps = [b - a for a, b in zip(prev, prev[1:])] +assert max(steps) <= follow.ACCEL_MS2 * 0.1 + 1e-9 and min(steps) >= -follow.DECEL_MS2 * 0.1 - 1e-9, steps + +# never into the person: they walk at 0.5 m/s and stop dead; the base sees them REACT_S late (walking estimate +# filtered as Person does) and must still stand before NEAR_STOP_M +gap, v, walk, t, seen = follow.FOLLOW_GAP_M + 0.1, 0.0, 0.0, 0.0, [] +for k in range(300): # 0.1 s ticks + t = k * 0.1 + pw = 0.5 if t < 15.0 else 0.0 + seen.append((gap, pw)) + lag = seen[max(0, len(seen) - 1 - round(follow.REACT_S / 0.1))] + walk += (lag[1] - walk) * 0.1 / (follow.VEL_TAU_S + 0.1) + v = follow.speeds(lag[0], 0.0, v, 0.0, walk, dt=0.1)[0] + gap += (pw - v) * 0.1 + assert gap > follow.NEAR_STOP_M, (t, gap, v) + if 10.0 < t < 15.0: + assert v > 0.45, (t, v) # keeps up with a 0.5 m/s walk +assert v == 0.0 and gap < follow.FOLLOW_GAP_M + 0.2, (v, gap) + print("all follow checks passed")