diff --git a/sim/sorting/assets/minicobo/link_a1.stl b/sim/sorting/assets/minicobo/link_a1.stl new file mode 100644 index 0000000..294a527 Binary files /dev/null and b/sim/sorting/assets/minicobo/link_a1.stl differ diff --git a/sim/sorting/assets/minicobo/link_a2.stl b/sim/sorting/assets/minicobo/link_a2.stl new file mode 100644 index 0000000..e63d1fd Binary files /dev/null and b/sim/sorting/assets/minicobo/link_a2.stl differ diff --git a/sim/sorting/assets/minicobo/link_a3.stl b/sim/sorting/assets/minicobo/link_a3.stl new file mode 100644 index 0000000..25f851f Binary files /dev/null and b/sim/sorting/assets/minicobo/link_a3.stl differ diff --git a/sim/sorting/assets/minicobo/link_a4.stl b/sim/sorting/assets/minicobo/link_a4.stl new file mode 100644 index 0000000..4aba3f3 Binary files /dev/null and b/sim/sorting/assets/minicobo/link_a4.stl differ diff --git a/sim/sorting/assets/minicobo/link_a5.stl b/sim/sorting/assets/minicobo/link_a5.stl new file mode 100644 index 0000000..e7916d1 Binary files /dev/null and b/sim/sorting/assets/minicobo/link_a5.stl differ diff --git a/sim/sorting/assets/minicobo/link_a6.stl b/sim/sorting/assets/minicobo/link_a6.stl new file mode 100644 index 0000000..5ce0e38 Binary files /dev/null and b/sim/sorting/assets/minicobo/link_a6.stl differ diff --git a/sim/sorting/build.py b/sim/sorting/build.py new file mode 100644 index 0000000..8923c1a --- /dev/null +++ b/sim/sorting/build.py @@ -0,0 +1,379 @@ +"""Build the colour sorting scene (sorting.xml): Lumi's arm with the Zimmer gripper and its straight fingers, the +torso and head it must keep clear of, the table, the tray of balls and the two boxes. + +Everything is placed in the arm controller's base frame (Cobo pi, the frame of moveL and kine_forward), in metres, +z up, Lumi's front along +y. That is the frame the robot's own code and its calibrations use, so poses, camera +readings and taught points carry over without conversion. + +- Arm joints: the fitted kinematics (data/arm_kinematics.json, robot/kinematics.py): each joint's frame + and zero corrected to the controller's own answers (0.2 mm). A joint's qpos is the controller's angle in radians. +- Arm meshes: JAKA's MiniCobo STEP (sim/sorting/assets/minicobo, the same files the Lumi app draws), placed with the + app's link chain (Lumi-app src/lib/armFrame.ts, fitted to the controller within ~1 mm). +- Torso, lift, head, base: the vendor URDF meshes, placed from the arm through the URDF chain at the sorting pose + (lift LIFT_M, waist WAIST_RAD, head straight). +- Torso camera: the hand-eye calibration (data/camera_extrinsics.json), fovy from the factory intrinsics. +- Gripper and fingers: measured by the owner 2026-11-25 (docs/gripper, docs/10_GRIPPER.md); lengths read from photos + are +-5 mm and the fingers' direction around the flange is a guess (see GRIP_YAW_DEG) until the grasp point is + taught on the robot. + + .venv-sim/bin/python sim/sorting/build.py # writes sim/sorting/sorting.xml +""" +import json +import math +import re +from pathlib import Path + +import mujoco +import numpy as np + +HERE = Path(__file__).resolve().parent +SIM = HERE.parent +REPO = SIM.parent +KIN_FILE = REPO / "data/arm_kinematics.json" +CAM_FILE = REPO / "data/camera_extrinsics.json" +OUT = HERE / "sorting.xml" + +# ---------- the robot's pose during sorting ---------- +LIFT_M = 0.0 # the lift at its lowest: at the proven picks' 75 mm the arm cannot hold its fingers straight + # down at a ball's centre on the table (sim/sorting/scripted_demo.py probe, 2026-11-25) +WAIST_RAD = 0.0 # waist at the front; only turns the base under the table here (approximate) +HEAD_PITCH_RAD = 0.0 # head straight + +# ---------- the app's arm chain (Lumi-app src/lib/armFrame.ts): glTF axes, each link turns about its y ---------- +APP_CHAIN = [((0, 0, 0), (0, 0, 0, 1)), + ((0, 0.156, 0.0165), (0.5, 0.5, 0.5, 0.5)), + ((0.21, -0.029, 0), (0, 0, 0, 1)), + ((0.1303, 0.0185, 0), (-0.5, -0.5, -0.5, 0.5)), + ((0, 0.0802, -0.0187), (0.5, -0.5, -0.5, 0.5)), + ((-0.1233, 0.01805, 0), (0.5, -0.5, 0.5, 0.5))] +APP_MOUNT_R = np.array([[0, 1, 0], [0, 0, -1], [-1, 0, 0]], float) # model arm frame -> controller frame +APP_MOUNT_T = np.array([30.4, -0.4, 0.1]) / 1000.0 +GLTF_FROM_URDF = np.array([[1, 0, 0], [0, 0, 1], [0, -1, 0]], float) # a URDF (z up) vector in glTF axes (y up) + +# ---------- gripper (gripper frame G: origin at the flange face, z along the flange's z away from the arm, x the way +# the fingers run out, y the way the jaws move), metres ---------- +GRIP_YAW_DEG = -43.3 # x_G in the flange frame: the old C-hooks' grasp point lay this way (tcp_offset_flange + # 85.6, -80.6 mm in grasp_teach.json); the new fingers sit on the same mount blocks. A GUESS + # until the grasp point is taught with a ball +ADAPTER = (0.040, 0.035, 0.004) # half sizes; the plate on the flange +BODY = (0.032, 0.030, 0.046) # Zimmer GEP2013 body, half sizes; its lower face 100 mm from the flange +BODY_Z = 0.008 + BODY[2] +JAW_STROKE = 0.013 # per jaw (datasheet; measured 61 open / 35 closed inner gap) +GAP_CLOSED = 0.035 # inner gap between the gripping faces, closed (owner's tape) +FINGER_T = 0.0075 # finger thickness across the gripping direction, pad included +PAD_T = 0.003 # the rubber pad's share of it (guess) +ARM_X = (-0.035, 0.145) # the finger arm along x_G: back edge of the mount plate to the tip +ARM_Z = (0.155, 0.170) # the arm's top and lower edge below the flange face (photos) +PAD_X = (0.095, 0.145) # the pads at the tip end, ~50 mm +PLATE_Z = (0.100, ARM_Z[0]) # the vertical mount plate from the Zimmer body down to the arm +BLOCK = (0.020, 0.030) # steel mount block: half length along x_G, height below the body +TCP = (0.5 * sum(PAD_X), 0.0, 0.5 * sum(ARM_Z)) # grasp point: between the pad centres (to be taught) +GRIP_FORCE = 90.0 # N, switch level 1 +FINGER_MASS = 0.06 # kg per finger, a guess (to weigh) + +# ---------- scene (controller frame, m) ---------- +TABLE_Z = -0.403 - (LIFT_M - 0.075) # table top: -403 mm at lift 75 mm (tasks/fetch.py); the arm rises with the lift +FLOOR_Z = -1.051 - (LIFT_M - 0.075) # under the wheels (URDF chain): a 0.65 m table +TRAY_C = (0.150, 0.550) # tray centre: where the hand reaches the table with its fingers straight + # down (x 0..0.50, y 0.30..0.65 at lift 0; reach map 2026-11-25), not + # the cups' pick centre (-0.10, 0.58) +TRAY_OUT = (0.280, 0.190, 0.025) # outer size; long side across Lumi's front +TRAY_WALL, TRAY_FLOOR = 0.005, 0.004 +BOX_OUT = (0.180, 0.260, 0.080) # S_Half box without its lid, short side facing the tray +BOX_WALL = 0.003 +BOX_GAP = 0.050 # between the tray and each box: room for the fingers +BALL_R, BALL_MASS = 0.02125, 0.008 # foam golf practice ball 42.5 mm, 8 g +BALL_GRID = (0.090, 0.075) # ball spacing along x and y: the hand reaches the table with its jaws + # turned about 45 deg, so the fingers need room on the diagonals too +COLOURS = {"red": "0.86 0.12 0.12 1", "yellow": "0.97 0.80 0.10 1"} +BOXES = {"red": -1, "yellow": +1} # which side of the tray (along x) each colour's box stands + + +def quat_mat(q): + x, y, z, w = q + return np.array([[1 - 2 * (y * y + z * z), 2 * (x * y - z * w), 2 * (x * z + y * w)], + [2 * (x * y + z * w), 1 - 2 * (x * x + z * z), 2 * (y * z - x * w)], + [2 * (x * z - y * w), 2 * (y * z + x * w), 1 - 2 * (x * x + y * y)]]) + + +def rot(axis, a): + c, s = math.cos(a), math.sin(a) + if axis == "y": + return np.array([[c, 0, s], [0, 1, 0], [-s, 0, c]]) + return np.array([[c, -s, 0], [s, c, 0], [0, 0, 1]]) + + +def hmat(R, t): + T = np.eye(4) + T[:3, :3], T[:3, 3] = R, t + return T + + +def mj_quat(R): + q = np.zeros(4) + mujoco.mju_mat2Quat(q, np.asarray(R, float).flatten()) + return q + + +def fmt(v, n=6): + return " ".join(f"{x:.{n}f}".rstrip("0").rstrip(".") if abs(x) > 1e-12 else "0" for x in v) + + +def pose_attr(T): + return f'pos="{fmt(T[:3, 3])}" quat="{fmt(mj_quat(T[:3, :3]), 8)}"' + + +# ---------- arm ---------- +def fitted(): + d = json.loads(KIN_FILE.read_text()) + mm = lambda A: hmat(np.array(A)[:3, :3], np.array(A)[:3, 3] / 1000.0) + return mm(d["base"]), [mm(F) for F in d["joints"]], d["offsets_rad"] + + +def fit_chain(q, model): + """Each link's frame in the controller frame (m) after its joint, as robot/kinematics.py chain().""" + base, frames, offsets = model + T, out = base, [] + for F, a, off in zip(frames, q, offsets): + T = T @ F @ hmat(rot("z", a + off), np.zeros(3)) + out.append(T) + return out + + +def app_chain(q): + """Each link's frame in the controller frame (m) as the app draws it, in URDF link axes (the STL's own).""" + R, p, out = np.eye(3), np.zeros(3), [] + for (t, qq), a in zip(APP_CHAIN, q): + p = p + R @ np.array(t) + R = R @ quat_mat(qq) @ rot("y", a) + out.append(hmat(APP_MOUNT_R @ R @ GLTF_FROM_URDF, APP_MOUNT_R @ p + APP_MOUNT_T)) + return out + + +def mesh_offsets(model): + """Each MiniCobo mesh's place in its fitted link body: constant, averaged over random poses; worst spread kept.""" + rng = np.random.default_rng(0) + rows = [] + for _ in range(40): + q = rng.uniform(-math.pi, math.pi, 6) + rows.append([np.linalg.inv(f) @ a for f, a in zip(fit_chain(q, model), app_chain(q))]) + out, spread = [], [] + for i in range(6): + Ms = np.array([r[i] for r in rows]) + t = Ms[:, :3, 3].mean(axis=0) + U, _, Vt = np.linalg.svd(Ms[:, :3, :3].mean(axis=0)) + out.append(hmat(U @ Vt, t)) + spread.append(1000 * np.linalg.norm(Ms[:, :3, 3] - t, axis=1).max()) + return out, spread + + +# ---------- torso from the old full model ---------- +def torso(): + """{body: 4x4 in link_2's frame} of base_link, link_1 ... link_4 at the sorting pose, and their geoms.""" + m = mujoco.MjModel.from_xml_path(str(SIM / "lumi.xml")) + d = mujoco.MjData(m) + for name, v in (("l_1", LIFT_M), ("l_2", WAIST_RAD), ("l_3", 0.0), ("l_4", HEAD_PITCH_RAD)): + d.qpos[m.jnt_qposadr[m.joint(name).id]] = v + mujoco.mj_kinematics(m, d) + W = lambda b: hmat(d.xmat[m.body(b).id].reshape(3, 3), d.xpos[m.body(b).id]) + inv2 = np.linalg.inv(W("link_2")) + names = ["base_link", "wheel_l", "wheel_r", "link_1", "link_2", "link_3", "link_4"] + meshfile = dict(re.findall(r'' + + +def open_box(prefix, centre, out, wall, floor, rgba): + """An open box: floor and four walls (centre: x, y of the middle, z of the bottom).""" + x, y, z = centre + sx, sy, sz = (v / 2 for v in out) + g = [box_geom(f"{prefix}_floor", (x, y, z + floor / 2), (sx, sy, floor / 2), rgba)] + h = (sz - floor / 2) + zc = z + floor + h - floor / 2 + g += [box_geom(f"{prefix}_wall_x0", (x - sx + wall / 2, y, zc), (wall / 2, sy, h), rgba), + box_geom(f"{prefix}_wall_x1", (x + sx - wall / 2, y, zc), (wall / 2, sy, h), rgba), + box_geom(f"{prefix}_wall_y0", (x, y - sy + wall / 2, zc), (sx, wall / 2, h), rgba), + box_geom(f"{prefix}_wall_y1", (x, y + sy - wall / 2, zc), (sx, wall / 2, h), rgba)] + return g + + +def gripper_xml(): + G = rot("z", math.radians(GRIP_YAW_DEG)) + L = [] + L.append(f'') + L.append(f' ') + L.append(f' ') + L.append(f' ') + L.append(f' ') + for side, s in (("l", +1), ("r", -1)): + y_in = s * GAP_CLOSED / 2 # gripping face, closed + L.append(f' ') + L.append(f' ') + L.append(f' ') + bz0, bz1 = 2 * BODY[2] + 0.008, 2 * BODY[2] + 0.008 + BLOCK[1] + L.append(f' ') + yc = s * (abs(y_in) + PAD_T + (FINGER_T - PAD_T) / 2) + L.append(f' ') + L.append(f' ') + yp = s * (abs(y_in) + PAD_T / 2) + L.append(f' ') + L.append(' ') + L.append('') + return L + + +def build(): + model = fitted() + offsets, spread = mesh_offsets(model) + base, frames, joint_offsets = model + tgeoms, meshfile = torso() + T2 = link2_in_controller(model) + cam = json.loads(CAM_FILE.read_text()) + Tc = np.array(cam["T_base_cam"]) + cam_R, cam_t = Tc[:3, :3], Tc[:3, 3] / 1000.0 + fovy = 2 * math.degrees(math.atan(360.0 / 608.4)) # 1280x720, fy 608.4 (06_CAMERAS_AND_VISION.md) + limits = [360, 125, 130, 360, 120, 360] # deg, controller joint limits (01_HARDWARE.md) + + x = [] + x.append('') + x.append(' ') + x.append(' ') + x.append(' ') + OUT.write_text("\n".join(x) + "\n") + print(f"wrote {OUT.relative_to(REPO)}") + print("mesh placement spread over 40 poses (mm):", " ".join(f"a{i + 1} {s:.2f}" for i, s in enumerate(spread))) + + +if __name__ == "__main__": + build() diff --git a/sim/sorting/sorting.xml b/sim/sorting/sorting.xml new file mode 100644 index 0000000..48c162b --- /dev/null +++ b/sim/sorting/sorting.xml @@ -0,0 +1,176 @@ + + + +