Improve smooth jog
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+132
-32
@@ -1,11 +1,16 @@
|
||||
"""Hold-to-move ("jog") control for the dashboard: base, head, waist, lift and arm joints.
|
||||
"""Hold-to-move ("jog") control for the app and the dashboard: base, head, waist, lift, arm joints and the hand.
|
||||
|
||||
The page sends POST /jog about every 150 ms while a button is held; each call keeps that axis moving
|
||||
for HOLD_S more. When the calls stop (button released, page closed, network gone) the axis stops by
|
||||
itself within one small step. Nothing here runs a long motion that has to be cancelled:
|
||||
itself:
|
||||
- base: /api/joy_control, each command lasts 0.5 s on the AGV anyway
|
||||
- head / waist / lift: short moveto steps (the body /stop call does not stop a moveto)
|
||||
- arm: short relative joint_move steps over TCP 10001
|
||||
- arm joints and the hand (tcp_x / tcp_y / tcp_z): one long move per hold (no step pauses), stopped with
|
||||
stop_program the moment the hold ends. A move covers at most ARM_SEG_S / ARM_SEG_DEG (TCP_SEG_MM): if the
|
||||
controller drops the stop for a moment the arm still ends there; a longer hold starts the next one right away.
|
||||
- head / waist / lift: the body controller cannot stop a moveto (docs/08), so these stay steps, sized to
|
||||
about BODY_SEG_S each from the speed measured on the last steps and sent back to back (no settle wait between
|
||||
two of our own steps). A release ends at most one step later.
|
||||
The arm's speeds follow the owner's arm speed % (motion.speed_percent, the app's slider), as every arm move does.
|
||||
"""
|
||||
import threading
|
||||
import time
|
||||
@@ -13,18 +18,52 @@ import time
|
||||
from robot import agv
|
||||
from robot import arm
|
||||
from robot import head
|
||||
from robot import motion
|
||||
|
||||
HOLD_S = 0.35 # s an axis keeps moving after the last /jog call
|
||||
|
||||
# axis: (index in the body status, step per move, low limit, high limit)
|
||||
# axis: (index in the body status, first step, low limit, high limit)
|
||||
BODY_AXES = {"lift": (0, 10.0, 0.0, head.LIFT_MAX_MM), "waist": (1, 3.0, -90.0, 90.0),
|
||||
"head_yaw": (2, 3.0, -180.0, 180.0), "head_pitch": (3, 2.0, -5.0, 35.0)}
|
||||
BODY_SEG_S = 0.4 # s one body step should take (sized from the measured speed)
|
||||
BODY_STEP_RANGE = (0.5, 8.0) # x the first step: the smallest and largest step
|
||||
BODY_ACC = 80 # acceleration ratio % of a body step (was 60: shorter ramps, smaller dips between steps)
|
||||
ARM_AXES = {f"j{i}": i - 1 for i in range(1, 7)}
|
||||
ARM_STEP = 2.0 # deg per step
|
||||
ARM_SPEED = 20.0 # deg/s at speed 100 %
|
||||
TCP_AXES = {"tcp_x": 0, "tcp_y": 1, "tcp_z": 2}
|
||||
ARM_SPEED = 20.0 # deg/s at speed 1 and arm speed 100 %
|
||||
ARM_SEG_S, ARM_SEG_DEG = 2.0, 30.0 # one arm-joint move: at most this long and this far
|
||||
TCP_SPEED = 40.0 # mm/s at speed 1 and arm speed 100 %
|
||||
TCP_SEG_S, TCP_SEG_MM = 2.0, 60.0 # one hand move: at most this long and this far
|
||||
TCP_TRY_MM = (1.0, 0.5, 0.25, 0.1) # x the planned length, tried in turn when IK refuses the longer one
|
||||
NEXT_EARLY_S = 0.1 # s before a move should end that the next one of a long hold is sent
|
||||
BASE_V = 0.3 # m/s at speed 100 %
|
||||
BASE_W = 0.6 # rad/s at speed 100 %
|
||||
|
||||
HAND_AXES = {**{a: ("joint", i) for a, i in ARM_AXES.items()}, **{a: ("tcp", i) for a, i in TCP_AXES.items()}}
|
||||
|
||||
|
||||
def arm_deg_s(speed):
|
||||
return min(motion.JOINT_MAX_DEG_S, ARM_SPEED * speed * motion.speed_percent() / 100)
|
||||
|
||||
|
||||
def tcp_mm_s(speed):
|
||||
return min(motion.LINEAR_MAX_MM_S, TCP_SPEED * speed * motion.speed_percent() / 100)
|
||||
|
||||
|
||||
def joint_target(now, index, direction, deg_s):
|
||||
"""Where one joint move of a hold goes: ARM_SEG_S / ARM_SEG_DEG on, kept inside the joint's limit.
|
||||
None at the limit."""
|
||||
lim = motion.LIMITS[index] - motion.LIMIT_MARGIN
|
||||
t = list(now)
|
||||
t[index] = max(-lim, min(lim, now[index] + direction * min(ARM_SEG_DEG, deg_s * ARM_SEG_S)))
|
||||
return t if abs(t[index] - now[index]) > 0.2 else None
|
||||
|
||||
|
||||
def body_step(first, rate):
|
||||
"""One body step for `rate` units/s measured (None: not yet), BODY_SEG_S long, within BODY_STEP_RANGE."""
|
||||
lo, hi = (first * k for k in BODY_STEP_RANGE)
|
||||
return first if not rate else max(lo, min(hi, rate * BODY_SEG_S))
|
||||
|
||||
|
||||
class Jogger:
|
||||
def __init__(self):
|
||||
@@ -32,16 +71,17 @@ class Jogger:
|
||||
self.cmd = {} # axis -> (direction, speed 0..1, deadline)
|
||||
self.threads = {}
|
||||
self.last_error = None
|
||||
self.body_rate = {} # (axis, vel) -> units/s measured on the last steps
|
||||
|
||||
def hold(self, axis, direction, speed=0.5):
|
||||
if axis not in BODY_AXES and axis not in ARM_AXES and axis not in ("base_drive", "base_turn"):
|
||||
if axis not in BODY_AXES and axis not in HAND_AXES and axis not in ("base_drive", "base_turn"):
|
||||
raise ValueError(f"unknown axis {axis}")
|
||||
group = "base" if axis.startswith("base") else "arm" if axis in ARM_AXES else "body"
|
||||
group = "base" if axis.startswith("base") else "arm" if axis in HAND_AXES else "body"
|
||||
with self.lock:
|
||||
if group == "base": # drive and turn may be held together
|
||||
self.cmd = {a: c for a, c in self.cmd.items() if a.startswith("base")}
|
||||
else: # one body / arm axis at a time
|
||||
self.cmd = {a: c for a, c in self.cmd.items() if not (a in BODY_AXES or a in ARM_AXES)}
|
||||
self.cmd = {a: c for a, c in self.cmd.items() if not (a in BODY_AXES or a in HAND_AXES)}
|
||||
self.cmd[axis] = (1 if direction > 0 else -1, max(0.05, min(1.0, speed)), time.time() + HOLD_S)
|
||||
t = self.threads.get(group)
|
||||
if t is None or not t.is_alive():
|
||||
@@ -77,44 +117,104 @@ class Jogger:
|
||||
time.sleep(0.1)
|
||||
|
||||
def _run_body(self):
|
||||
last = None # (axis, target) of the step just ended: the next starts there
|
||||
while True:
|
||||
active = self._active(BODY_AXES)
|
||||
if not active:
|
||||
return
|
||||
axis, (direction, speed, _) = next(iter(active.items()))
|
||||
index, step, low, high = BODY_AXES[axis]
|
||||
index, first, low, high = BODY_AXES[axis]
|
||||
vel = 20 + 60 * speed
|
||||
try:
|
||||
pos = head.status()
|
||||
if last and last[0] == axis and abs(pos[index] - last[1]) < 1.0:
|
||||
pos[index] = last[1] # read a hair short of the step's end: go on from where it was sent
|
||||
step = body_step(first, self.body_rate.get((axis, vel)))
|
||||
target = max(low, min(high, pos[index] + direction * step))
|
||||
if abs(target - pos[index]) < 0.05: # at the limit
|
||||
last = None
|
||||
time.sleep(0.1)
|
||||
continue
|
||||
pos[index] = target
|
||||
head.moveto(pos, 20 + 60 * speed, 60, timeout=10)
|
||||
start, pos[index] = pos[index], target
|
||||
t0 = time.time()
|
||||
head.moveto(pos, vel, BODY_ACC, timeout=10, settle=not (last and last[0] == axis))
|
||||
dt = time.time() - t0
|
||||
if dt > 0.05:
|
||||
rate = abs(target - start) / dt
|
||||
old = self.body_rate.get((axis, vel))
|
||||
self.body_rate[(axis, vel)] = rate if old is None else (old + rate) / 2
|
||||
last = (axis, target)
|
||||
except Exception as e:
|
||||
self.last_error = f"{axis}: {e}"
|
||||
last = None
|
||||
time.sleep(0.3)
|
||||
|
||||
def _run_arm(self):
|
||||
while True:
|
||||
active = self._active(ARM_AXES)
|
||||
if not active:
|
||||
return
|
||||
axis, (direction, speed, _) = next(iter(active.items()))
|
||||
delta = [0.0] * 6
|
||||
delta[ARM_AXES[axis]] = direction * ARM_STEP
|
||||
deg_s = ARM_SPEED * speed
|
||||
def _halt_arm(self):
|
||||
try:
|
||||
arm.cmd("stop_program")
|
||||
except Exception as e:
|
||||
self.last_error = f"arm stop: {e}"
|
||||
|
||||
def _start_arm(self, axis, direction, speed):
|
||||
"""Send one move of a hold; returns when it should end (time), None when it cannot go on (a limit)."""
|
||||
kind, index = HAND_AXES[axis]
|
||||
motion._ready()
|
||||
if kind == "joint":
|
||||
deg_s = arm_deg_s(speed)
|
||||
now = motion.joints()
|
||||
target = joint_target(now, index, direction, deg_s)
|
||||
if target is None:
|
||||
return None
|
||||
moved = max(abs(a - b) for a, b in zip(target, now))
|
||||
r = arm.cmd("joint_move", relFlag=0, jointPosition=target, speed=deg_s, accel=deg_s * 3)
|
||||
if r.get("errorCode") != "0":
|
||||
raise motion.MotionError(r.get("errorMsg") or str(r))
|
||||
return time.time() + moved / deg_s + 1 / 3
|
||||
mm_s = tcp_mm_s(speed)
|
||||
length = min(TCP_SEG_MM, mm_s * TCP_SEG_S)
|
||||
err = None
|
||||
for k in TCP_TRY_MM:
|
||||
delta = [0.0, 0.0, 0.0]
|
||||
delta[index] = direction * length * k
|
||||
try:
|
||||
r = arm.cmd("joint_move", relFlag=1, jointPosition=delta, speed=deg_s, accel=deg_s * 3)
|
||||
if r.get("errorCode") != "0":
|
||||
self.last_error = f"{axis}: {r.get('errorMsg') or r}"
|
||||
time.sleep(0.3)
|
||||
continue
|
||||
except Exception as e:
|
||||
self.last_error = f"{axis}: {e}"
|
||||
time.sleep(0.3)
|
||||
continue
|
||||
time.sleep(ARM_STEP / deg_s + 0.05) # let this step finish before the next
|
||||
motion.move_linear(delta=delta, wait=False, mm_s=mm_s)
|
||||
return time.time() + length * k / mm_s + 1 / 3
|
||||
except motion.MotionError as e:
|
||||
err = e
|
||||
raise err
|
||||
|
||||
def _run_arm(self):
|
||||
moving, ends = None, 0.0 # (axis, direction, speed) of the move running now
|
||||
try:
|
||||
while True:
|
||||
active = self._active(HAND_AXES)
|
||||
if active:
|
||||
axis, (direction, speed, _) = next(iter(active.items()))
|
||||
want = (axis, direction, speed)
|
||||
else:
|
||||
want = None
|
||||
if moving and want != moving:
|
||||
self._halt_arm() # released, or another axis / direction / speed
|
||||
moving = None
|
||||
if want is None:
|
||||
return
|
||||
if moving is None or time.time() >= ends - NEXT_EARLY_S:
|
||||
try:
|
||||
end = self._start_arm(*want)
|
||||
except Exception as e:
|
||||
self.last_error = f"{want[0]}: {e}"
|
||||
moving = None
|
||||
time.sleep(0.3)
|
||||
continue
|
||||
if end is None: # at the joint's limit: nothing sent
|
||||
moving = None
|
||||
time.sleep(0.1)
|
||||
continue
|
||||
moving, ends = want, end
|
||||
time.sleep(0.03)
|
||||
finally:
|
||||
if moving:
|
||||
self._halt_arm()
|
||||
|
||||
|
||||
jogger = Jogger()
|
||||
|
||||
+3
-3
@@ -46,17 +46,17 @@ def _call(path, body=None, timeout=2):
|
||||
return json.loads(r.read())
|
||||
|
||||
|
||||
def moveto(pos, vel=VEL, acc=ACC, timeout=30):
|
||||
def moveto(pos, vel=VEL, acc=ACC, timeout=30, settle=True):
|
||||
"""Move all four body joints (the waist from the front); every value is clamped into AXIS_LIMITS. Blocks until the
|
||||
motion ends (and while another thread's moveto runs). None, nothing sent, when other threads own the body
|
||||
(`owners`)."""
|
||||
(`owners`). `settle` False: skip the wait for the body to stand still (the caller's own move just returned)."""
|
||||
if owners and threading.current_thread() not in owners:
|
||||
return None # ponytail: skipped silently, the owner turns it back
|
||||
pos = [max(lo, min(hi, float(p))) for p, (lo, hi) in zip(pos, AXIS_LIMITS)]
|
||||
with _one:
|
||||
end = time.time() + timeout
|
||||
joints = _call("status")
|
||||
while time.time() < end: # another process's move still runs: a moveto sent meanwhile
|
||||
while settle and time.time() < end: # another process's move still runs: a moveto sent meanwhile
|
||||
time.sleep(STILL_S) # is dropped and its call never answers (checked 2026-10-30:
|
||||
before, joints = joints, _call("status") # 20 s timeout, no motion). "vel" reads 0 while moving, so
|
||||
if all(abs(a["pos"] - b["pos"]) < 0.05 for a, b in zip(before, joints)): # the positions tell
|
||||
|
||||
Reference in New Issue
Block a user