2026-10-21 15:19:07 +09:00
|
|
|
"""Turn Lumi's head (body HTTP API on the arm/body controller).
|
|
|
|
|
|
|
|
|
|
python robot/head.py # print lift, waist, head yaw, head pitch
|
|
|
|
|
python robot/head.py 10 # head yaw to +10 deg (slow), other joints unchanged
|
|
|
|
|
python robot/head.py 10 5 # head yaw +10 deg, head pitch +5 deg
|
|
|
|
|
python robot/head.py 30 --vel 60 # try another speed (speed ratio %, 1-100)
|
|
|
|
|
"""
|
|
|
|
|
import json
|
|
|
|
|
import sys
|
|
|
|
|
import threading
|
2026-10-30 20:55:05 +09:00
|
|
|
import time
|
2026-10-21 15:19:07 +09:00
|
|
|
import urllib.request
|
|
|
|
|
|
|
|
|
|
BODY = "http://192.168.10.90:5000/api/extaxis"
|
|
|
|
|
YAW_LIMIT = 60.0 # deg; we keep the head well inside its +-180 range
|
2026-10-30 20:55:05 +09:00
|
|
|
STILL_S = 0.1 # s between two position reads that must agree before a move is sent
|
|
|
|
|
TURN_TIMEOUT_S = 8.0 # s a head turn may take (estimate, to measure); a move the controller never reported
|
|
|
|
|
# done held every other body move 30 s and Lumi stopped listening (2026-10-30)
|
2026-10-21 15:19:07 +09:00
|
|
|
PITCH_RANGE = (-5.0, 35.0) # deg, hardware range
|
2026-10-23 18:30:40 +09:00
|
|
|
HEAD_REST_PITCH = PITCH_RANGE[0] # deg; the head's rest: its lowest, looking down (+pitch = up), so sitting people
|
|
|
|
|
# and near things are in view. Every "head back / ahead" uses it; yaw rests at 0
|
2026-10-21 15:19:07 +09:00
|
|
|
VEL = 40 # speed ratio %, 1-100 (was 15: too slow to follow a visitor)
|
|
|
|
|
ACC = 40 # acceleration ratio %, 1-100
|
2026-10-26 18:54:30 +09:00
|
|
|
# Temporary: the head camera's cable is too short, any lift above the bottom pulls it out, so the lift stays at 0 and
|
|
|
|
|
# Lumi searches by driving and turning. Back to 250 mm (the body API allows 300) when the longer cable is in.
|
|
|
|
|
LIFT_MAX_MM = 0.0
|
2026-10-21 15:19:07 +09:00
|
|
|
# [lift mm, waist, head yaw, head pitch] limits of the body API (docs/03), the lift held to LIFT_MAX_MM. The controller
|
|
|
|
|
# drops a whole moveto when any value is outside, and its own encoders read a little past the ends (pitch -5.003 at
|
|
|
|
|
# the bottom stop).
|
|
|
|
|
AXIS_LIMITS = ((0.0, LIFT_MAX_MM), (-140.0, 140.0), (-180.0, 180.0), (-5.0, 35.0))
|
|
|
|
|
|
|
|
|
|
_one = threading.Lock() # one moveto at a time: overlapping calls hang the body API
|
|
|
|
|
owners = () # threads that own the body now (tasks/follow.py); empty: anyone may move it
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _call(path, body=None, timeout=2):
|
|
|
|
|
data = None if body is None else json.dumps(body).encode()
|
|
|
|
|
req = urllib.request.Request(f"{BODY}/{path}", data=data, headers={"Content-Type": "application/json"})
|
|
|
|
|
with urllib.request.urlopen(req, timeout=timeout) as r:
|
|
|
|
|
return json.loads(r.read())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def moveto(pos, vel=VEL, acc=ACC, timeout=30):
|
|
|
|
|
"""Move all four body joints; 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`)."""
|
|
|
|
|
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:
|
2026-10-30 20:55:05 +09:00
|
|
|
end = time.time() + timeout
|
|
|
|
|
joints = _call("status")
|
|
|
|
|
while 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
|
|
|
|
|
break
|
|
|
|
|
if not all(j["enable"] for j in joints): # off after a controller power cycle; moveto needs them on
|
2026-10-21 15:19:07 +09:00
|
|
|
_call("enable", {"enable": 1})
|
2026-10-30 20:55:05 +09:00
|
|
|
return _call("moveto", {"pos": pos, "vel": vel, "acc": acc}, timeout=max(1.0, end - time.time()))
|
2026-10-21 15:19:07 +09:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def status():
|
|
|
|
|
"""[lift mm, waist deg, head yaw deg, head pitch deg]"""
|
|
|
|
|
return [j["pos"] for j in _call("status")]
|
|
|
|
|
|
|
|
|
|
|
2026-10-30 18:22:47 +09:00
|
|
|
def turn(yaw, pitch=None, vel=VEL, acc=ACC, limit=YAW_LIMIT):
|
|
|
|
|
"""Move the head, yaw within +-`limit`; blocks until the motion ends. Returns the controller's reply."""
|
2026-10-21 15:19:07 +09:00
|
|
|
lift, waist, _, cur_pitch = status()
|
2026-10-30 18:22:47 +09:00
|
|
|
yaw = max(-limit, min(limit, yaw))
|
2026-10-21 15:19:07 +09:00
|
|
|
pitch = cur_pitch if pitch is None else max(PITCH_RANGE[0], min(PITCH_RANGE[1], pitch))
|
2026-10-30 20:55:05 +09:00
|
|
|
return moveto([lift, waist, yaw, pitch], vel, acc, timeout=TURN_TIMEOUT_S)
|
2026-10-21 15:19:07 +09:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
args = sys.argv[1:]
|
|
|
|
|
vel = VEL
|
|
|
|
|
if "--vel" in args:
|
|
|
|
|
i = args.index("--vel")
|
|
|
|
|
vel = float(args[i + 1])
|
|
|
|
|
del args[i:i + 2]
|
|
|
|
|
if not args:
|
|
|
|
|
print(dict(zip(["lift_mm", "waist", "head_yaw", "head_pitch"], (round(p, 2) for p in status()))))
|
|
|
|
|
else:
|
|
|
|
|
print(turn(float(args[0]), float(args[1]) if len(args) > 1 else None, vel))
|
|
|
|
|
print("now:", [round(p, 2) for p in status()])
|