From 417bfcade7668a838844d8bfe57a99479ba66329 Mon Sep 17 00:00:00 2001 From: Cutshion Date: Wed, 21 Oct 2026 18:19:17 +0900 Subject: [PATCH] Wave swings back to back, gentler joint acceleration Co-Authored-By: Claude Opus 5.5 --- manipulation/wave.py | 4 ++-- robot/motion.py | 45 ++++++++++++++++++++++++++++++----------- tests/test_arm_route.py | 25 ++++++++++++++++++++--- tests/test_wave.py | 6 +++--- 4 files changed, 60 insertions(+), 20 deletions(-) diff --git a/manipulation/wave.py b/manipulation/wave.py index 165ee30..be38b72 100644 --- a/manipulation/wave.py +++ b/manipulation/wave.py @@ -3,7 +3,7 @@ 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` (J3 −14.7°) SWINGS times and STAYS at `wave_start`: the next arm task takes it back down the same way (wave_start -> wave_out --> look). The way up goes like arm_route.goto, then the swings; each move to a stop (motion.move_path). Before +-> look). The way up goes like arm_route.goto, each move to a stop; the swings run back to back. 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 @@ -100,7 +100,7 @@ def wave(on_start=None): if route and motion._stopped.wait(motion.SETTLE_S): # stand at START first, as between any two moves raise motion.MotionError("stopped") t0 = time.time() - motion.move_path([(t, SWING_SPEED) for t in swings]) + motion.move_path([(t, SWING_SPEED) for t in swings], queued=True) # back to back, no stop between swings log(f"wave: {SWINGS} swings after {time.time() - t0:.1f} s") diff --git a/robot/motion.py b/robot/motion.py index ba3d72d..da35a5f 100644 --- a/robot/motion.py +++ b/robot/motion.py @@ -28,7 +28,9 @@ LIMITS = [360, 125, 130, 360, 120, 360] # deg, ± factory range per LIMIT_MARGIN = 5.0 # deg kept away from every limit JOINT_SPEED = {"slow": 10.0, "normal": 25.0, "route": 40.0} # deg/s; route: empty-hand pose-graph moves (estimate) LINEAR_SPEED = {"slow": 20.0, "normal": 50.0} # mm/s -ACCEL_FACTOR = 3.0 # accel = speed x this (per s) +ACCEL_FACTOR = 3.0 # straight moves: accel = speed x this (per s) +JOINT_ACCEL_FACTOR = 1.5 # joint moves: accel = speed x this (per s); estimate: x 3 + # stopped at each pose with a jolt that shook the robot MAX_FLIP = 90.0 # deg; an IK answer turning J1 / J4 / J6 more than this is refused MAX_TRAVEL = 200.0 # deg; a joint move turning any joint further is refused FULL_TURN = (0, 3, 5) # J1, J4, J6: ±360 joints; a target and target ± 360 are the same pose @@ -107,9 +109,19 @@ def _ready(): raise MotionError("a Cobo π program is running") -def _wait(arrived, limit_s): +def _queue_empty(): + """The controller has no motion command left to run (get_motion_state; an odd reply counts as not empty).""" + try: + r = arm.cmd("get_motion_state") + except (ConnectionError, OSError): + return False + return r.get("queue") == 0 and r.get("active_queue", 0) == 0 + + +def _wait(arrived, limit_s, queued=False): """Wait while the arm moves, at most limit_s. Big moves run far slower than the speed asked (SLOW_DEG_S), so the - limit is generous; a move that stops short is caught by STILL_S instead.""" + limit is generous; a move that stops short is caught by STILL_S instead. `queued`: several moves were sent at + once, so arrived also needs the controller's queue empty (the swings pass their last pose before the end).""" t0 = last_move = last_seen = time.time() last = None while time.time() - t0 < limit_s: @@ -128,7 +140,7 @@ def _wait(arrived, limit_s): last_seen = time.time() if d.get("protective_stop") or d.get("emergency_stop"): raise MotionError("the controller stopped the arm (protective / emergency stop)") - if d.get("inpos") and arrived(d): + if d.get("inpos") and arrived(d) and (not queued or _queue_empty()): return d j = d["joint_actual_position"] if last is None or max(abs(a - b) for a, b in zip(j, last)) > STILL_DEG: @@ -157,10 +169,12 @@ def move_joints(target, speed="slow", wait=True): return move_path([(target, speed)], wait) -def move_path(moves, wait=True): +def move_path(moves, wait=True, queued=False): """Joint moves [(joints, speed)], all checked before the first is sent, then one at a time: each waits until the - arm is there (inpos, every joint within AT_JOINT_DEG) and stands SETTLE_S before the next is sent. stop() ends - it between moves too. Returns the joints reached (wait=False: the last move is not waited for, its target).""" + arm is there (inpos, every joint within AT_JOINT_DEG) and stands SETTLE_S before the next is sent. `queued`: all + sent at once instead, the controller runs them back to back (docs/04 §10; the wave's swings) and only the end is + waited for. stop() ends it at any point. Returns the joints reached (wait=False: the last move is not waited for, + its target).""" for t, _ in moves: check(t) _ready() @@ -173,21 +187,28 @@ def move_path(moves, wait=True): seq.append((t, speed, max(abs(a - b) for a, b in zip(t, prev)))) prev = t _stopped.clear() - d = None + d, limit = None, 0.0 for k, (t, speed, deg) in enumerate(seq): - if k and _stopped.wait(SETTLE_S): + if k and not queued and _stopped.wait(SETTLE_S): raise MotionError("stopped") deg_s = JOINT_SPEED[speed] - r = arm.cmd("joint_move", relFlag=0, jointPosition=list(t), speed=deg_s, accel=deg_s * ACCEL_FACTOR) + r = arm.cmd("joint_move", relFlag=0, jointPosition=list(t), speed=deg_s, accel=deg_s * JOINT_ACCEL_FACTOR) if _stopped.is_set(): # STOP pressed while sending: stop again, after this move stop() raise MotionError("stopped") if r.get("errorCode") != "0": + if queued and k: + stop() # the moves already queued must not run on alone raise MotionError(f"joint_move refused: {r.get('errorMsg') or r}") - if not wait and k == len(seq) - 1: + last = k == len(seq) - 1 + limit += max(3 * deg / deg_s, deg / SLOW_DEG_S) + if queued and not last: + continue + if not wait and last: return list(t) d = _wait(lambda d, t=t: max(abs(a - b) for a, b in zip(d["joint_actual_position"], t)) < AT_JOINT_DEG, - max(3 * deg / deg_s, deg / SLOW_DEG_S) + EXTRA_S) + limit + EXTRA_S, queued=queued and len(seq) > 1) + limit = 0.0 return d["joint_actual_position"] diff --git a/tests/test_arm_route.py b/tests/test_arm_route.py index 841b720..2524e9e 100644 --- a/tests/test_arm_route.py +++ b/tests/test_arm_route.py @@ -160,10 +160,12 @@ class QueueArm(FakeArm): self.on_poll() if self.queue: self.j = self.queue.pop(0) - return {"joint_actual_position": list(self.j), "inpos": not self.queue} + return {"joint_actual_position": list(self.j), "inpos": getattr(self, "inpos_always", False) or not self.queue} if name == "stop_program": self.queue.clear() return {"errorCode": "0"} + if name == "get_motion_state": + return {"queue": len(self.queue), "active_queue": len(self.queue), "errorCode": "0"} return super().__call__(name, **a) def moves(self): @@ -178,7 +180,7 @@ fake = arm.cmd = QueueArm(POSES["look"]) assert arm_route.goto("wave_start") == ["wave_out", "wave_start"] and fake.j == POSES["wave_start"] route = motion.JOINT_SPEED[arm_route.SPEED] keys = {"relFlag", "jointPosition", "speed", "accel"} # no tol: never blended -assert fake.moves() == [(route, route * motion.ACCEL_FACTOR, keys)] * 2, fake.moves() +assert fake.moves() == [(route, route * motion.JOINT_ACCEL_FACTOR, keys)] * 2, fake.moves() assert order(fake) == ["joint_move", "get_data", "joint_move", "get_data"], order(fake) arrived = [e for e in fake.log if e[0] == "get_data"][0][4] second = [e for e in fake.log if e[0] == "joint_move"][1][4] @@ -187,7 +189,7 @@ fake = arm.cmd = QueueArm(POSES["look"]) arm_route.goto("wave_start", holding=True) hold = motion.JOINT_SPEED[arm_route.HOLD_SPEED] assert order(fake) == ["joint_move", "get_data", "joint_move", "get_data"], order(fake) -assert fake.moves() == [(hold, hold * motion.ACCEL_FACTOR, keys)] * 2 and hold < route, fake.moves() +assert fake.moves() == [(hold, hold * motion.JOINT_ACCEL_FACTOR, keys)] * 2 and hold < route, fake.moves() # STOP while a move is sent, while the arm runs it, or while it stands between two: stopped, nothing more sent fake = arm.cmd = QueueArm(POSES["look"], on_move=lambda k: k == 1 and motion.stop()) @@ -215,6 +217,23 @@ except motion.MotionError as e: motion.SETTLE_S = 0.05 +# queued (the wave's swings): all sent before any poll, the wait ends only at the end with the queue empty, though +# the arm passes the last pose before (inpos there too) +a_, b_ = POSES["wave_start"], POSES["wave"] +fake = arm.cmd = QueueArm(a_) +fake.inpos_always = True +motion.move_path([(b_, "normal"), (a_, "normal"), (b_, "normal"), (a_, "normal")], queued=True) +assert order(fake)[:5] == ["joint_move"] * 4 + ["get_data"] and fake.queue == [] and fake.j == a_, order(fake) +assert order(fake).count("get_data") >= 4, order(fake) +fake = arm.cmd = QueueArm(a_, on_move=lambda k: k == 2 and motion.stop()) # STOP while they are sent +try: + motion.move_path([(b_, "normal"), (a_, "normal"), (b_, "normal"), (a_, "normal")], queued=True) + raise AssertionError("went on after STOP") +except motion.MotionError as e: + names = [e_[0] for e_ in fake.log] + assert str(e) == "stopped" and names[-1] == "stop_program" and fake.queue == [] and "get_data" not in names, names + + class SlowArm(QueueArm): """Runs the queued moves at `deg_s` (wall clock), far below the speed asked; `stall_at`: stops there for good.""" diff --git a/tests/test_wave.py b/tests/test_wave.py index 86ef96d..78459ee 100644 --- a/tests/test_wave.py +++ b/tests/test_wave.py @@ -90,14 +90,14 @@ a.sent.clear() a.j = list(swing) # stopped at wave: back to wave_start, then swings wave.wave() assert a.sent == [top] + [swing, top] * wave.SWINGS, a.sent -names = [] # every move to a stop, then the next; the way up +names = [] # the way up as goto, move by move; then the swings arm.cmd = lambda name, **k: (names.append((name, k.get("speed"), "tol" in k)), a(name, **k))[1] # as goto a.j = list(LOOK) wave.wave() sent = [e for e in names if e[0] in ("joint_move", "get_data")] route, swing_s = motion.JOINT_SPEED[arm_route.SPEED], motion.JOINT_SPEED[wave.SWING_SPEED] -assert sent == [x for v in [route] * 2 + [swing_s] * 2 * wave.SWINGS - for x in (("joint_move", v, False), ("get_data", None, False))], sent +assert sent == [("joint_move", route, False), ("get_data", None, False)] * 2 + \ + [("joint_move", swing_s, False)] * 2 * wave.SWINGS + [("get_data", None, False)], sent # swings back to back assert wave.SWING_SPEED == "normal" arm.cmd = a a.j = list(top)