// The mission: a fictional MacroSwan playroom (4.6 x 4.0 m, line anchors in its corners at ~2.5 m, inside the range of // random rooms the world model was trained on), furniture the visitor moves, toys the visitor drops, and a toy hamper. Furniture reaches the planner ONLY as costs, as in // cablepilot/roommap/costs.py: clearance of spheres down the robot's pole and of points along the four lines, from // boxes. The phases are a slim version of cablepilot/tasks/pick_place.py. import { rollout as wmRollout } from "./model.js"; import { MPPI, PLANNER, rng } from "./mppi.js"; export const ROOM = { name: "MacroSwan playroom (fictional)", size: [4.6, 4.0], // walls: x in [-2.3, 2.3], y in [-2.0, 2.0] mounts: [[2.22, 1.92, 2.52], [-2.22, 1.92, 2.48], [-2.22, -1.92, 2.55], [2.22, -1.92, 2.50]], // corners, CCW hamper: [-1.2, -0.95], parking: [1.15, 0.85], floor: { extent: 5.0 }, }; export const HAMPER = { half: 0.2, height: 0.40 }; // a laundry hamper's opening (clear_playroom.yaml) export const DEFAULT_FURNITURE = [ // boxes {x, y, hx, hy, h} { id: "shelf", label: "Toy shelf", x: 0.55, y: 1.72, hx: 0.6, hy: 0.18, h: 1.2 }, { id: "table", label: "Play table", x: 0.95, y: -0.85, hx: 0.35, hy: 0.35, h: 0.55 }, { id: "armchair", label: "Armchair", x: -1.55, y: 0.85, hx: 0.4, hy: 0.4, h: 0.9 }, ]; export const DEFAULT_TOYS = [[-0.4, 0.6], [0.3, 0.45], [-0.2, -0.3], [0.5, 0.0], [-0.6, -0.9]]; const DG = -0.5147; // grasp point below the gantry along the pole (stringman.yaml body numbers) const SPHERES = [ // [d along the pole, radius] (roommap/costs.robot_spheres): gantry, pole, gripper, fingers [-0.037, 0.085], [-0.09, 0.012], [-0.17, 0.012], [-0.25, 0.012], [-0.329, 0.012], [-0.3907, 0.075], [-0.4547, 0.045]]; const W = { goal: 4, swing: 0.05, swingCarry: 0.3, walls: 5, floor: 5, furniture: 5, lines: 5, height: 5 }; const P = { huber: 0.05, tw0: 0.3, terminal: 3, wallMargin: 0.25, robotClear: 0.03, lineClear: 0.07, lineN: 14, lineSkip: 0.3, zMax: 2.2, lead: 0.45, toyZ: 0.02, aboveTop: 0.30, carryZ: HAMPER.height + 0.14, carryHighZ: HAMPER.height + 0.30 }; // ------------------------------------------------------------------------------------------- geometry const poly = (() => { // the mount polygon, CCW, for "inside the room" distance const c = ROOM.mounts.reduce((a, m) => [a[0] + m[0] / 4, a[1] + m[1] / 4], [0, 0]); return ROOM.mounts.map(m => [m[0], m[1]]).sort((a, b) => Math.atan2(a[1] - c[1], a[0] - c[0]) - Math.atan2(b[1] - c[1], b[0] - c[0])); })(); export function insideDistance(x, y) { let best = Infinity; for (let i = 0; i < poly.length; i++) { const a = poly[i], b = poly[(i + 1) % poly.length], ex = b[0] - a[0], ey = b[1] - a[1], l = Math.hypot(ex, ey); best = Math.min(best, ((x - a[0]) * -ey + (y - a[1]) * ex) / l); } return best; } export const REACH_MARGIN = 0.35; function boxSdf(b, x, y, z) { // box from the floor to height h const qx = Math.abs(x - b.x) - b.hx, qy = Math.abs(y - b.y) - b.hy, qz = Math.abs(z - b.h / 2) - b.h / 2; const ox = Math.max(qx, 0), oy = Math.max(qy, 0), oz = Math.max(qz, 0); return Math.hypot(ox, oy, oz) + Math.min(Math.max(qx, qy, qz), 0); } function sdf(boxes, x, y, z) { let d = 9; for (const b of boxes) d = Math.min(d, boxSdf(b, x, y, z)); return d; } function hamperBoxes() { // the hamper's four walls (runner.obstacles does the same for the bin) const [bx, by] = ROOM.hamper, i = HAMPER.half, t = 0.02, h = HAMPER.height; return [{ x: bx + i + t / 2, y: by, hx: t / 2, hy: i + t, h }, { x: bx - i - t / 2, y: by, hx: t / 2, hy: i + t, h }, { x: bx, y: by + i + t / 2, hx: i, hy: t / 2, h }, { x: bx, y: by - i - t / 2, hx: i, hy: t / 2, h }]; } export function axis(x, o = 0) { // pole axis (up) from the swing const sx = x[o + 6], sy = x[o + 7], sz = Math.sqrt(Math.max(1e-6, 1 - sx * sx - sy * sy)); return [sx, sy, sz]; } export function grasp(x, o = 0) { const a = axis(x, o); return [x[o] + DG * a[0], x[o + 1] + DG * a[1], x[o + 2] + DG * a[2]]; } const huber = (d, k) => (d < k ? 0.5 * d * d / k : d - 0.5 * k); // ------------------------------------------------------------------------------------------- where the hand can reach the floor /** Why the hand cannot pick at (x, y) from the floor, or null. Checked with the gantry straight above the toy (grasp * point at toy height): the hand must clear the furniture, and every line from its mount must clear it too. The * last one is the cable robot's own constraint: furniture casts a "line shadow" on the floor. */ export function reachWhy(furniture, x, y) { if (insideDistance(x, y) < REACH_MARGIN) return { code: "wall", text: "too close to a wall: the lines cannot pull the hand there" }; if (Math.hypot(x - ROOM.hamper[0], y - ROOM.hamper[1]) < HAMPER.half + 0.15) return { code: "hamper", text: "that is the hamper" }; if (sdf(furniture, x, y, 0.01) < 0.10) return { code: "furniture", text: "under or against furniture: the hand cannot get down there" }; const pz = P.toyZ - DG; for (const M of ROOM.mounts) { const dx = x - M[0], dy = y - M[1], dz = pz - M[2], L = Math.hypot(dx, dy, dz); for (let i = 0; i <= 24; i++) { const f = P.lineSkip / L + (1 - P.lineSkip / L - 0.08 / L) * i / 24; if (sdf(furniture, M[0] + f * dx, M[1] + f * dy, M[2] + f * dz) < P.lineClear * 0.6) return { code: "shadow", text: "in a line shadow: a cable to that spot would cut through furniture" }; } } return null; } /** n x n grid over the 5 m floor image: 0 reachable, 1 wall, 2 furniture/hamper, 3 line shadow (row 0 = +y edge). */ export function reachMap(furniture, n = 100) { const e = ROOM.floor.extent, out = new Uint8Array(n * n), code = { wall: 1, hamper: 2, furniture: 2, shadow: 3 }; for (let r = 0; r < n; r++) for (let c = 0; c < n; c++) { const x = ((c + 0.5) / n - 0.5) * e, y = (0.5 - (r + 0.5) / n) * e, w = reachWhy(furniture, x, y); out[r * n + c] = w ? code[w.code] : 0; } return out; } // ------------------------------------------------------------------------------------------- the mission export const PHASES = ["above", "descend", "close", "lift", "carry", "lower", "release", "park", "idle"]; const LABEL = { above: "flying over the toy", descend: "reaching down", close: "closing the fingers", lift: "lifting", carry: "carrying to the hamper", lower: "lowering into the hamper", release: "dropping it in", park: "parking", idle: "waiting for toys" }; export class Mission { constructor(model, seed = 0) { this.m = model; this.seed = seed; this.furniture = DEFAULT_FURNITURE.map(f => ({ ...f })); this.toys = DEFAULT_TOYS.map(([x, y], i) => ({ id: i + 1, x, y, z: P.toyZ, state: "floor" })); this.nextId = this.toys.length + 1; this.reset(); } reset() { const x = new Float32Array(14); x[0] = ROOM.parking[0]; x[1] = ROOM.parking[1]; x[2] = this.parkZ() - DG; this.x = x; this.cmd = [0, 0, 0, 0, 0]; this.hist = Array.from({ length: this.m.H + 1 }, () => Float32Array.from(x)); this.chist = Array.from({ length: this.m.H }, () => this.cmd.slice()); this.mppi = new MPPI(PLANNER, this.seed); this.t = 0; this.phase = "idle"; this.phaseT = 0; this.target = null; this.held = null; this.stats = { plans: 0, planMs: 0, delivered: 0, contacts: 0, events: [] }; for (const t of this.toys) if (t.state !== "floor") { t.state = "floor"; t.z = P.toyZ; } this.noise = rng(this.seed + 7); } // ---- edits from the page setFurniture(list) { this.furniture = list.map(f => ({ ...f })); } addToy(x, y) { const why = this.placeable(x, y); if (why) return { ok: false, why: why.text }; this.toys.push({ id: this.nextId++, x, y, z: P.toyZ, state: "floor" }); return { ok: true }; } removeToy(id) { if (this.target && this.target.id === id) return; this.toys = this.toys.filter(t => t.id !== id); } placeable(x, y) { return reachWhy(this.furniture, x, y); } reachMap(n = 100) { return reachMap(this.furniture, n); } // ---- what the planner should do now /** On long moves the planner chases a carrot at most P.lead ahead of the hand, not the far target (pick_place's carry * funnel does the same job): far goals make it sprint, and a sprinting cable robot swings. */ goal() { const g = this.rawGoal(); if (!["above", "carry", "park"].includes(this.phase)) return g; const G = grasp(this.x), dx = g[0] - G[0], dy = g[1] - G[1], d = Math.hypot(dx, dy); if (d <= P.lead) return g; return [G[0] + dx / d * P.lead, G[1] + dy / d * P.lead, g[2]]; } rawGoal() { const t = this.target, hz = this.maxTop() + P.aboveTop; switch (this.phase) { case "above": return [t.x, t.y, Math.max(0.35, Math.min(hz, 0.55))]; case "descend": case "close": return [t.x, t.y, P.toyZ]; case "lift": return [t.x, t.y, Math.max(0.45, Math.min(hz, 0.7))]; case "carry": return [ROOM.hamper[0], ROOM.hamper[1], P.carryHighZ]; case "lower": case "release": return [ROOM.hamper[0], ROOM.hamper[1], P.carryZ]; default: return [ROOM.parking[0], ROOM.parking[1], this.parkZ()]; } } parkZ() { return Math.max(0.8, this.maxTop() + 0.35); } maxTop() { return this.furniture.reduce((a, f) => Math.max(a, f.h), HAMPER.height); } fixed() { const close = ["close", "lift", "carry", "lower"].includes(this.phase) ? 1 : 0; return { 3: 0, 4: close }; } // ---- the task's cost on imagined futures (S x T x 14) cost(states, S) { const T = PLANNER.horizon, goal = this.goal(), boxes = this.furniture.concat(hamperBoxes()); const zcap = this.maxTop() + P.lineClear, ws = ["carry", "lower", "release"].includes(this.phase) ? W.swingCarry : W.swing; const J = new Float64Array(S), terms = { goal: 0, furniture: 0, lines: 0, walls: 0, swing: 0, floor: 0 }; const sw2 = (2 * Math.PI / 180) ** 2, nearFloor = this.phase === "descend" || this.phase === "close"; for (let s = 0; s < S; s++) { let j = 0; for (let k = 0; k < T; k++) { const o = (s * T + k) * 14, a = axis(states, o), px = states[o], py = states[o + 1], pz = states[o + 2]; const gx = px + DG * a[0], gy = py + DG * a[1], gz = pz + DG * a[2]; let tw = P.tw0 + (1 - P.tw0) * k / (T - 1); if (k === T - 1) tw *= P.terminal; const cg = W.goal * huber(Math.hypot(gx - goal[0], gy - goal[1], gz - goal[2]), P.huber) / P.huber * tw; const csw = ws * (states[o + 8] ** 2 + states[o + 9] ** 2) / sw2; const cw = W.walls * (Math.max(0, P.wallMargin - insideDistance(gx, gy)) / 0.02) ** 2; const cf = W.floor * (Math.max(0, -0.005 - gz) / 0.01) ** 2 + W.height * (Math.max(0, pz - P.zMax) / 0.02) ** 2; // furniture: spheres down the pole (the hand may touch the floor near a toy, not the furniture) let cr = 0; for (const [d, r] of SPHERES) { const c = sdf(boxes, px + d * a[0], py + d * a[1], pz + d * a[2]); const v = Math.max(0, P.robotClear + r - c) / 0.02; cr += v * v; } const cpad = sdf(boxes, gx, gy, gz); cr += (Math.max(0, (nearFloor ? 0.005 : 0.02) - cpad) / 0.02) ** 2; // the four lines, below the tallest thing in the room only let cl = 0; for (const M of ROOM.mounts) { const dx = px - M[0], dy = py - M[1], dz = pz - M[2], L = Math.hypot(dx, dy, dz); let f0 = Math.min(0.9, P.lineSkip / L); const f1 = 1 - 0.08 / L; f0 = Math.max(f0, Math.min(0.95, Math.max(0, (M[2] - zcap) / Math.max(1e-3, -dz)))); for (let i = 0; i < P.lineN; i++) { const f = f0 + (f1 - f0) * i / (P.lineN - 1); const c = sdf(boxes, M[0] + f * dx, M[1] + f * dy, M[2] + f * dz); const v = Math.max(0, P.lineClear - c) / 0.02; cl += v * v; } } cr *= W.furniture; cl *= W.lines; j += cg + csw + cw + cf + cr + cl; if (s === 0) { terms.goal += cg; terms.furniture += cr; terms.lines += cl; terms.walls += cw; terms.swing += csw; terms.floor += cf; } } J[s] = j; } return { J, terms }; } // ---- one planner tick (0.2 s): plan, then the plant moves 2 model steps, then events step() { if (this.phase === "idle" || this.phase === "park") this.pickNext(); const M = ROOM.mounts, m = this.m; const rollFn = (plans, S) => wmRollout(m, M, this.hist, this.chist, plans, S, PLANNER.horizon, 2); const { action, info } = this.mppi.plan(rollFn, (st, _p, S) => this.cost(st, S), this.cmd, this.fixed()); this.stats.plans++; this.stats.planMs += info.ms.total; // the robot: simulated by the same world model, one 0.1 s step at a time, plus noise on the swing and the gantry // (in cablepilot the robot is the MuJoCo twin; the model tracks it to ~2 cm over 2 s, test 1) const two = Float32Array.from([...action, ...action]); const nxt = wmRollout(m, M, this.hist, this.chist, two, 1, 2, 1); for (let k = 0; k < 2; k++) { const xk = Float32Array.from(nxt.subarray(k * 14, (k + 1) * 14)); for (let i = 0; i < 2; i++) xk[8 + i] += 0.004 * this.noise.n(); for (let i = 0; i < 3; i++) xk[3 + i] += 0.002 * this.noise.n(); this.hist.push(xk); this.hist.shift(); this.chist.push(action.slice()); this.chist.shift(); } this.x = Float32Array.from(this.hist[this.hist.length - 1]); this.cmd = action; this.t += 0.2; this.phaseT += 0.2; const G = grasp(this.x); if (this.held) { this.held.x = G[0]; this.held.y = G[1]; this.held.z = G[2] - 0.02; } // contact check on the TRUE state (the score; the planner never sees it) const boxes = this.furniture.concat(hamperBoxes()); let touch = false; for (const [d, r] of SPHERES) { const a = axis(this.x); if (sdf(boxes, this.x[0] + d * a[0], this.x[1] + d * a[1], this.x[2] + d * a[2]) < r) touch = true; } if (touch) this.stats.contacts++; this.events(G); return this.frame(info); } pickNext() { const floor = this.toys.filter(t => t.state === "floor" && !this.placeable(t.x, t.y)); if (!floor.length) { if (this.phase !== "park" && this.phase !== "idle") this.go("park"); else if (this.phase === "idle") this.phase = "park"; return; } const G = grasp(this.x); floor.sort((a, b) => Math.hypot(a.x - G[0], a.y - G[1]) - Math.hypot(b.x - G[0], b.y - G[1])); this.target = floor[0]; this.target.state = "target"; this.go("above"); } go(ph, why) { this.phase = ph; this.phaseT = 0; if (why) { this.stats.events.push({ t: +this.t.toFixed(1), what: why }); if (this.stats.events.length > 30) this.stats.events.shift(); } } events(G) { const g = this.rawGoal(), v = Math.hypot(this.x[3], this.x[4], this.x[5]); const dxy = Math.hypot(G[0] - g[0], G[1] - g[1]), dz = Math.abs(G[2] - g[2]); const swing = Math.hypot(this.x[8], this.x[9]) * 180 / Math.PI; const t = this.target; if (t && this.placeable(t.x, t.y) && !this.held) { // the visitor moved furniture onto it t.state = "floor"; this.target = null; this.go("idle", `toy ${t.id} skipped: ${this.placeable(t.x, t.y).text}`); return; } switch (this.phase) { case "above": if (dxy < 0.05 && dz < 0.08) this.go("descend", `over toy ${t.id}`); break; case "descend": if (dxy < 0.025 && dz < 0.02 && v < 0.06) this.go("close", `reached toy ${t.id}`); break; case "close": if (this.x[12] > 0.85) { if (Math.hypot(G[0] - t.x, G[1] - t.y) < 0.04) { this.held = t; t.state = "held"; this.go("lift", `grabbed toy ${t.id}`); } else this.go("above", `missed toy ${t.id}, trying again`); } break; case "lift": if (dz < 0.05) this.go("carry", "lifted"); break; case "carry": if (dxy < 0.06 && dz < 0.08 && swing < 6) this.go("lower", "over the hamper"); break; case "lower": if (dxy < 0.04 && dz < 0.03 && swing < 5) this.go("release", "inside the rim"); break; case "release": if (this.x[12] < 0.2) { const h = this.held; h.state = "delivered"; h.x = ROOM.hamper[0] + (Math.random() - 0.5) * 0.2; h.y = ROOM.hamper[1] + (Math.random() - 0.5) * 0.2; h.z = 0.05; this.held = null; this.target = null; this.stats.delivered++; this.go("idle", `toy ${h.id} in the hamper (${this.t.toFixed(0)} s)`); } break; case "park": if (dxy < 0.08) this.go("idle"); break; } if (this.phase !== "park" && this.phase !== "idle" && this.phaseT > 45 && t) { t.state = "floor"; if (this.held) { this.held = null; t.z = P.toyZ; } this.target = null; this.go("idle", `gave up on toy ${t.id} after 45 s`); } } frame(info) { const T = PLANNER.horizon, gpath = st => { const out = []; for (let k = 0; k < T; k++) out.push(grasp(st, k * 14)); return out; }; return { t: this.t, x: Array.from(this.x), cmd: this.cmd, phase: this.phase, label: LABEL[this.phase], goal: this.goal(), target: this.target ? this.target.id : null, nominal: info ? gpath(info.nominal) : [], rollouts: info ? info.rollouts.map(gpath) : [], ess: info ? info.ess : 0, ms: info ? info.ms : null, terms: info ? info.terms : null, toys: this.toys.map(t => ({ ...t })), furniture: this.furniture, stats: { ...this.stats, events: this.stats.events.slice(-8), msPerPlan: this.stats.planMs / Math.max(1, this.stats.plans) }, }; } }