diff --git a/data/arm_poses.json b/data/arm_poses.json index 166d76a..8e13e12 100644 --- a/data/arm_poses.json +++ b/data/arm_poses.json @@ -30,5 +30,29 @@ 199.97, 81.4, 158.35 + ], + "wave_out": [ + -239.15, + -89.88, + 0.5, + -160.11, + 77.47, + -201.64 + ], + "wave_start": [ + -178.86, + -77.26, + -2.49, + -175.06, + 38.21, + -219.73 + ], + "wave": [ + -178.86, + -77.27, + -2.52, + -175.06, + 59.89, + -219.73 ] } \ No newline at end of file diff --git a/manipulation/arm_route.py b/manipulation/arm_route.py index 7ab527e..871cecf 100644 --- a/manipulation/arm_route.py +++ b/manipulation/arm_route.py @@ -1,7 +1,7 @@ """The arm moves between named poses only along a graph of taught poses, every joint move checked before anything moves. - give - carry - look - wave_start - wave + give - carry - look - wave_out - wave_start - wave | \\ ready tuck @@ -31,8 +31,8 @@ from robot import arm, gripper, motion from vision import calib, head_cam HOME = "look" # every arm action starts here -EDGES = (("give", "carry"), ("carry", "look"), ("look", "wave_start"), ("wave_start", "wave"), - ("look", "ready"), ("look", "tuck")) # the only joint moves between named poses (both ways) +EDGES = (("give", "carry"), ("carry", "look"), ("look", "wave_out"), ("wave_out", "wave_start"), + ("wave_start", "wave"), ("look", "ready"), ("look", "tuck")) # the only joint moves between named poses (both ways) SPEED = "slow" # motion.JOINT_SPEED of every move along the graph AT_POSE_DEG = 3.0 # deg; the arm is at a pose when every joint is this close (where, programs) EXACT_DEG = 1.0 # deg; further than this from the nearest pose a route first goes to it diff --git a/manipulation/wave.py b/manipulation/wave.py index 588db20..4ccbf78 100644 --- a/manipulation/wave.py +++ b/manipulation/wave.py @@ -1,27 +1,27 @@ """Wave the hand when someone asked for it. A greeting never moves the arm. -The arm goes up along the pose graph (manipulation/arm_route.py: from `look` to `wave_start`, the hand away from the -head, then `wave`, raised), swings one wrist joint there and STAYS at `wave`: the next arm task takes it back down -the same way (wave -> wave_start -> look). Before anything moves, every joint move is checked (arm_route.check_moves: -limits, travel, the gripper clear of the head, no dip). Refused, with the reason in English and Korean: the arm off or -busy, the base driving, a task running, something in the hand, the arm at no known pose (after free drive), a pose not -taught, a move too near the head. +The arm goes up along the pose graph (manipulation/arm_route.py: from `look` to `wave_out`, the hand away from the +head, then `wave_start`, the hand above the head), swings between the taught poses `wave_start` and `wave` (J5 +21.7 +deg) SWINGS times and STAYS at `wave_start`: the next arm task takes it back down the same way (wave_start -> wave_out +-> look). Before anything moves, every joint move is checked (arm_route.check_moves: limits, travel, the gripper clear +of the head, no dip). Refused, with the reason in English and Korean: the arm off or busy, the base driving, a task +running, something in the hand, the arm at no known pose (after free drive), a pose not taught, a move too near the +head. python manipulation/wave.py check # plan and check from where the arm is now; moves nothing python manipulation/wave.py run # wave (slow; the owner at the STOP) -Teach the route first, arm moved by hand in Cobo π: python robot/motion.py teach wave_start / teach wave +Teach the poses first, arm moved by hand in Cobo π: python robot/motion.py teach wave_out / wave_start / wave """ import sys from manipulation import arm_route, skills from robot import agv, arm, gripper, motion -TOP = "wave" # the raised pose (arm_route.EDGES: look - wave_start - wave) -WAVE_JOINT = 4 # joint index swung at the raised pose (4 = J5); check on the first slow run -WAVE_DEG = 15.0 # deg each way from the raised pose -WAVE_SWINGS = 2 # times there and back -WAVE_SPEED = arm_route.SPEED # motion.JOINT_SPEED; the route and the swings +START = "wave_start" # hand above the head; the wave starts and ends here +SWING = "wave" # START with J5 +21.7 deg (taught 2026-10-21) +SWINGS = 3 # times START -> SWING -> START +SWING_SPEED = "slow" # motion.JOINT_SPEED of the swings; the way up is arm_route.SPEED # Why Lumi does not wave, (English, Korean) OFF = ("Please enable my arm first.", "먼저 제 팔을 켜 주세요.") @@ -71,12 +71,12 @@ def plan(): raise WaveError(*HOLDING) now = motion.joints() try: - route = arm_route.plan(TOP, now=now) # up along the graph ([] when it is up already) - start = route[-1][1] if route else now - top = motion.nearest(motion.poses()[TOP], start) - i = WAVE_JOINT - swings = [top[:i] + [top[i] + s * WAVE_DEG] + top[i + 1:] for _ in range(WAVE_SWINGS) for s in (1, -1)] - seq = [j for _, j in route] + arm_route.check_moves(start, ([] if route else [top]) + swings + [top]) + route = arm_route.plan(START, now=now) # up along the graph ([] when it is there already) + p = motion.poses() + if SWING not in p: + raise arm_route.RouteError(*arm_route.NOT_TAUGHT) + seq = [j for _, j in route] + arm_route.check_moves(route[-1][1] if route else now, + [p[SWING], p[START]] * SWINGS) except arm_route.RouteError as e: raise WaveError(*{arm_route.UNKNOWN: UNKNOWN_POSE, arm_route.NOT_TAUGHT: NOT_TAUGHT, arm_route.BLOCKED: NOT_TAUGHT, arm_route.NEAR_HEAD: NEAR_HEAD}.get(e.args, UNSURE)) from e @@ -84,7 +84,7 @@ def plan(): def wave(on_start=None): - """Plan, check and wave; the arm stays at TOP. `on_start()` is called once the plan is checked. + """Plan, check and wave; the arm stays at START. `on_start()` is called once the plan is checked. Raises WaveError (refused) or motion.MotionError (stopped / failed while moving: the arm stays where it stopped).""" try: seq = plan() @@ -94,8 +94,9 @@ def wave(on_start=None): log(f"wave: {len(seq)} joint moves, checked") if on_start: on_start() - for t in seq: - motion.move_joints(t, WAVE_SPEED) + up = len(seq) - 2 * SWINGS # the moves before are the way up along the graph + for k, t in enumerate(seq): + motion.move_joints(t, arm_route.SPEED if k < up else SWING_SPEED) if __name__ == "__main__": diff --git a/tests/test_arm_route.py b/tests/test_arm_route.py index eef2c17..f0378e8 100644 --- a/tests/test_arm_route.py +++ b/tests/test_arm_route.py @@ -12,7 +12,8 @@ from vision import head_cam fetch.STATE_FILE = Path(tempfile.mkdtemp()) / "lumi_task.json" # never the real /dev/shm file POSES = {"look": [0.0] * 6, "carry": [20.0, 0, 0, 0, 0, 0], "give": [40.0, 0, 0, 0, 0, 0], - "wave_start": [0.0, -20, 0, 0, 0, 0], "wave": [0.0, -20, 20, 0, 0, 0], "ready": [0.0, 0, -20, 0, 0, 0], + "wave_out": [0.0, -20, 0, 0, 0, 0], "wave_start": [0.0, -20, 20, 0, 0, 0], + "wave": [0.0, -20, 20, 0, 20, 0], "ready": [0.0, 0, -20, 0, 0, 0], "tuck": [-20.0, 0, 0, 0, 0, 0]} FAR = np.array([0.0, 0.0, 5000.0]) # the head far away: nothing near it head = {"centre": FAR} @@ -78,15 +79,18 @@ use(POSES["give"]) assert arm_route.goto("look") == ["carry", "look"] # after giving: give -> carry -> look assert arm_route.goto("give") == ["carry", "give"] # to give: look -> carry -> give use(POSES["wave"]) -assert arm_route.goto("tuck") == ["wave_start", "look", "tuck"] # fold: through look +assert arm_route.goto("tuck") == ["wave_start", "wave_out", "look", "tuck"] # fold: through look +use(POSES["look"]) +assert arm_route.goto("wave_start") == ["wave_out", "wave_start"] # up: never look -> wave_start directly +assert {"look", "wave_start"} not in [set(e) for e in arm_route.EDGES] -# ---------- after a wave the arm stays up; the next pick goes wave -> wave_start -> look first ---------- -fake = use(POSES["wave"]) +# ---------- after a wave the arm stays at wave_start; the next pick goes down through wave_out first ---------- +fake = use(POSES["wave_start"]) skills._prepare() -assert fake.sent == [POSES["wave_start"], POSES["look"]], fake.sent -fake = use(POSES["wave"]) +assert fake.sent == [POSES["wave_out"], POSES["look"]], fake.sent +fake = use(POSES["wave_start"]) fetch._pose("look") -assert fake.sent == [POSES["wave_start"], POSES["look"]], fake.sent +assert fake.sent == [POSES["wave_out"], POSES["look"]], fake.sent # ---------- an unknown pose near a known one: a checked joint move to it, then on ---------- near = [3.0, 2.0, -4.0, 5.0, 0.0, 0.0] # 5 deg from look (free drive) @@ -113,7 +117,7 @@ for start in (POSES["look"], near, POSES["give"]): refused(arm_route.NEAR_HEAD, arm_route.goto, "give" if start != POSES["give"] else "look") assert fake.sent == [], fake.sent fake = use(POSES["look"]) -assert arm_route.goto("wave") == ["wave_start", "wave"] # another edge: clear of it +assert arm_route.goto("wave") == ["wave_out", "wave_start", "wave"] # other edges: clear of it head["centre"] = None # the head not measured: nothing moves refused(arm_route.UNSURE, arm_route.goto, "look") head["centre"] = FAR @@ -125,7 +129,7 @@ POSES["give"] = [40.0, 0, 0, 0, 0, 0] del POSES["wave_start"] refused(arm_route.NOT_TAUGHT, arm_route.goto, "wave") # no way to wave without wave_start refused(arm_route.NOT_TAUGHT, arm_route.goto, "nowhere") -POSES["wave_start"] = [0.0, -20, 0, 0, 0, 0] +POSES["wave_start"] = [0.0, -20, 20, 0, 0, 0] # ---------- Cobo π programs only from look; hand_shaking never ---------- fake = use(POSES["carry"]) diff --git a/tests/test_wave.py b/tests/test_wave.py index 503d06c..1545775 100644 --- a/tests/test_wave.py +++ b/tests/test_wave.py @@ -16,7 +16,8 @@ from vision import head_cam fetch.STATE_FILE = Path(tempfile.mkdtemp()) / "lumi_task.json" # never the real /dev/shm file lumi.BUSY_FILE = str(fetch.STATE_FILE.parent / "lumi_busy.json") # nor the real busy file LOOK = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0] -POSES = {"look": LOOK, "wave_start": [-50.0, 0.0, 0.0, 0.0, 0.0, 0.0], "wave": [-50.0, 0.0, 60.0, 0.0, 30.0, 0.0]} +POSES = {"look": LOOK, "wave_out": [-50.0, 0.0, 0.0, 0.0, 0.0, 0.0], "wave_start": [-50.0, 0.0, 60.0, 0.0, 0.0, 0.0], + "wave": [-50.0, 0.0, 60.0, 0.0, 21.7, 0.0]} HEAD = np.array([-100.0, -200.0, 700.0]) # mm, the head's turning centre in the toy arm motion.poses = lambda: dict(POSES) head_cam.head_centre = lambda: HEAD @@ -67,27 +68,36 @@ def refused(reason): raise AssertionError(f"waved, expected {reason}") -# ---------- from look: up along the graph, wave, and the arm stays up; every move clear of the head ---------- +# ---------- from look: up through wave_out, 3 swings, the arm stays at wave_start; every move clear of the head ------ a = use(FakeArm(LOOK)) started = [] wave.wave(on_start=lambda: started.append(len(a.sent))) assert started == [0] # said before the first move -top = POSES["wave"] -up, down = top[:4] + [top[4] + wave.WAVE_DEG] + top[5:], top[:4] + [top[4] - wave.WAVE_DEG] + top[5:] -assert a.sent == [POSES["wave_start"], top] + [up, down] * wave.WAVE_SWINGS + [top], a.sent +top, swing = POSES["wave_start"], POSES["wave"] +assert wave.SWINGS == 3 +assert a.sent == [POSES["wave_out"], top] + [swing, top] * 3, a.sent for j0, j1 in zip([LOOK] + a.sent, a.sent): assert arm_route.profile(j0, j1, HEAD)[0] >= arm_route.HEAD_CLEAR_MM, (j0, j1) assert arm_route.profile(LOOK, top, HEAD)[0] < arm_route.HEAD_CLEAR_MM # straight up from look would pass the head +assert {"look", "wave_start"} not in [set(e) for e in arm_route.EDGES] # so there is no such edge a.sent.clear() wave.wave() # asked again while up: only the swings -assert a.sent == [top] + [up, down] * wave.WAVE_SWINGS + [top], a.sent +assert a.sent == [swing, top] * 3, a.sent +a.sent.clear() +a.j = list(swing) # stopped at wave: back to wave_start, then swings +wave.wave() +assert a.sent == [top] + [swing, top] * 3, a.sent +a.j = list(top) +a.sent.clear() +assert arm_route.goto("look") == ["wave_out", "look"] # the way down +assert a.sent == [POSES["wave_out"], LOOK], a.sent # ---------- a taught pose whose way passes the head: refused before anything moves ---------- -POSES["wave_start"] = [-15.0, 0.0, 40.0, 0.0, 0.0, 0.0] +POSES["wave_out"] = [-15.0, 0.0, 40.0, 0.0, 0.0, 0.0] a = use(FakeArm(LOOK)) refused(wave.NEAR_HEAD) assert a.sent == [] -POSES["wave_start"] = [-50.0, 0.0, 0.0, 0.0, 0.0, 0.0] +POSES["wave_out"] = [-50.0, 0.0, 0.0, 0.0, 0.0, 0.0] # ---------- holding something: refused (task state, or AI_1 when the camera can't tell) ---------- a = use(FakeArm(LOOK)) @@ -106,7 +116,7 @@ assert a.sent == [] POSES["carry"] = [30.0, 0.0, 0.0, 0.0, 0.0, 0.0] a = use(FakeArm(POSES["carry"])) wave.wave() -assert a.sent[:3] == [LOOK, POSES["wave_start"], top], a.sent +assert a.sent[:4] == [LOOK, POSES["wave_out"], top, swing], a.sent del POSES["carry"] # ---------- an unknown start (after free drive: the wrist the other way round), base, power ---------- @@ -123,7 +133,10 @@ refused(wave.BUSY) fetch.STATE_FILE.unlink() del POSES["wave"] refused(wave.NOT_TAUGHT) -POSES["wave"] = top +POSES["wave"] = swing +del POSES["wave_out"] +refused(wave.NOT_TAUGHT) +POSES["wave_out"] = [-50.0, 0.0, 0.0, 0.0, 0.0, 0.0] assert a.sent == [] # ---------- the assistant: a greeting sends no arm command; a wave asked while holding is refused aloud ----------