// Checks of the web app's logic in Node, with the page's own WebAssembly physics: // teleop keys and the base speed loop, scripted picks in the tabletop scene, grasps of objects turned // every way, go-to and pick of every object in the tabletop scene without the base touching furniture, // go-to / pick / place in the kitchen, the kitchen's household tasks (their BDDL problems, and the // scripted demonstrator doing five of them), and that reading contacts does not leak WebAssembly memory. // Exits non-zero if a check fails. // // usage: node tools/check_sim.mjs import { readFileSync } from "node:fs"; import { join } from "node:path"; import { ASSETS, loadScene } from "./node_env.mjs"; import { Episode, Household } from "../src/tasks/household.js"; import { solve } from "../src/tasks/solver.js"; import { XLeRobotTeleop } from "../src/robots/xlerobot.js"; import { approach, objectInfo, pick, place, runSkill } from "../src/robots/skills.js"; import { ContactWatch } from "../src/robots/nav.js"; let failed = 0; const check = (ok, what) => { console.log(`${ok ? "PASS" : "FAIL"}: ${what}`); if (!ok) failed++; }; // Teleop and base, on the empty floor. { const { sim, robot } = await loadScene("basic"); const control = () => robot.update(); sim.reset(() => robot.home()); sim.run(0.5, control); const teleop = new XLeRobotTeleop(robot); const p0 = robot.pose(); teleop.down.add("KeyW"); sim.run(2.0, (dt) => (teleop.tick(dt), robot.update())); teleop.down.clear(); sim.run(0.5, (dt) => (teleop.tick(dt), robot.update())); const p1 = robot.pose(); const moved = (p1[0] - p0[0]) * Math.cos(p0[2]) + (p1[1] - p0[1]) * Math.sin(p0[2]); check(moved > 0.3, `W drives forward (${moved.toFixed(2)} m in 2 s)`); const y0 = robot.armPositions("left")[1]; teleop.down.add("Digit8"); sim.run(0.5, (dt) => (teleop.tick(dt), robot.update())); teleop.down.clear(); check(Math.abs(robot.armPositions("left")[1] - y0) > 0.05, "8 moves the left arm"); robot.setBaseVelocity(0.2, 0); sim.run(3, control); const [v] = robot.baseVelocity(); check(Math.abs(v - 0.2) < 0.04, `base speed loop holds 0.2 m/s (${v.toFixed(3)})`); robot.stopBase(); } // Scripted picks in the tabletop scene, with the robot in front of the long table. { const { sim, robot } = await loadScene("tabletop", "xlerobot", { objects: "robot" }); const control = () => robot.update(); sim.reset(() => robot.home()); sim.run(1.5, control); const settled = sim.snapshot(); let ok = 0; const cases = [["box1", 0.25], ["sphere2", 0.05], ["cylinder1", 0.25], ["sphere1", 0.35], ["box1", 0.0], ["sphere2", 0.3], ["cylinder1", 0.5], ["sphere1", 0.55]]; for (const [name, x] of cases) { sim.restore(settled); robot.home(); robot.spawn(x, -0.09, -Math.PI / 2); robot.snapJoints(); sim.forward(); sim.run(0.5, control); if (runSkill(sim, robot, pick(robot, sim.id("body", name))).ok) ok++; } check(ok >= cases.length - 1, `tabletop scripted picks ${ok}/${cases.length}`); } // Grasps that follow how an object lies: a block, a banana and a mug turned to different yaws, in // front of the robot at the tabletop's back counter (the other objects moved out of the way). { const { sim, robot } = await loadScene("tabletop"); const control = () => robot.update(); sim.reset(() => { robot.home(); robot.snapJoints(); }); sim.run(1.0, control); const settled = sim.snapshot(); const { model } = sim; let ok = 0; let n = 0; for (const [name, yaws] of [["cube", [0, 30, 60]], ["banana", [0, 50, 100]], ["mug", [0, 150]]]) { const id = sim.id("body", name); const qa = model.jnt_qposadr[model.body_jntadr[id]]; const z = settled.qpos[qa + 2]; const q0 = Array.from(settled.qpos.subarray(qa + 3, qa + 7)); for (const deg of yaws) { sim.restore(settled); robot.home(); robot.spawn(0, -0.3 + 0.022 + 0.196, -Math.PI / 2); robot.apply(); robot.snapJoints(); sim.forward(); sim.freeBodies([robot.root]).forEach((o, k) => { if (o.id !== id) sim.data.qpos.set([3 + 0.3 * k, 3, 0.1, 1, 0, 0, 0], model.jnt_qposadr[model.body_jntadr[o.id]]); }); const p = robot.toWorld([0.27, robot.arms.left.axis[1], 0]); const a = (deg * Math.PI) / 180; const [w, x, y, zq] = [Math.cos(a / 2), 0, 0, Math.sin(a / 2)]; // the yaw on top of how it rests const q = [w * q0[0] - zq * q0[3], w * q0[1] - zq * q0[2], w * q0[2] + zq * q0[1], w * q0[3] + zq * q0[0]]; sim.data.qpos.set([p[0], p[1], z, ...q], qa); sim.forward(); sim.run(0.5, control); n++; if (runSkill(sim, robot, pick(robot, id, { side: "left" })).ok) ok++; } } check(ok >= n - 1, `grasps across objects' narrow sides, turned every way: ${ok}/${n}`); } // Tabletop: from the start, go to each object and pick it (the bowl and plate: go to them). The base // must never touch the counters. { const { sim, robot } = await loadScene("tabletop"); const control = () => robot.update(); sim.reset(() => { robot.home(); robot.snapJoints(); }); sim.run(1.0, control); const settled = sim.snapshot(); const watch = new ContactWatch(sim, robot.root); let reached = 0; let picked = 0; let touched = 0; const names = sim.freeBodies([robot.root]).map((o) => o.name); const containers = ["bowl", "plate"]; for (const name of names) { sim.restore(settled); robot.home(); robot.snapJoints(); sim.forward(); const id = sim.id("body", name); let phase = "go"; const onTick = () => { const c = watch.check(); // a finger on the worktop while grasping is picking; the base on furniture is not if (c && (phase === "go" || /chassis|wheel/.test(c.robot))) touched++; }; const a = runSkill(sim, robot, approach(robot, id), { maxSeconds: 60, onTick }); if (a.ok) reached++; phase = "pick"; if (a.ok && !containers.includes(name) && runSkill(sim, robot, pick(robot, id), { maxSeconds: 30, onTick }).ok) picked++; if (!a.ok) console.log(` ${name}: ${a.message}`); } check(reached === names.length, `tabletop: went to ${reached}/${names.length} objects`); check(picked >= names.length - containers.length - 1, `tabletop: picked ${picked}/${names.length - containers.length}`); check(touched === 0, `tabletop: the base never touched furniture (${(touched * sim.controlDt).toFixed(2)} s of contact)`); } // Kitchen: go to the orange, pick it, put it in the bowl. { const { sim, robot } = await loadScene("kitchen"); const control = () => robot.update(); sim.reset(() => { robot.home(); robot.snapJoints(); }); sim.run(1.0, control); const settled = sim.snapshot(); // The can is behind the island from the start: around it, without touching it. const watch = new ContactWatch(sim, robot.root); let touched = 0; const can = sim.id("body", "can"); const toCan = runSkill(sim, robot, approach(robot, can), { maxSeconds: 60, onTick: () => (watch.check() ? touched++ : 0) }); const gotCan = toCan.ok && runSkill(sim, robot, pick(robot, can)).ok; check(gotCan && touched === 0, `kitchen: around the island to the can and pick it (${toCan.message}; ${touched} ticks touching)`); sim.restore(settled); robot.home(); robot.snapJoints(); sim.forward(); const orange = sim.id("body", "orange"); const bowl = sim.id("body", "bowl"); const a = runSkill(sim, robot, approach(robot, orange), { maxSeconds: 40 }); check(a.ok, `kitchen: go to the orange (${a.message})`); const p = runSkill(sim, robot, pick(robot, orange)); check(p.ok, `kitchen: pick the orange (${p.message})`); const b = robot.toRobot(sim.bodyPos(bowl)); const info = objectInfo(sim, bowl); runSkill(sim, robot, place(robot, p.side, [b[0], b[1], robot.toRobot([0, 0, info.bottom])[2] + 0.012], orange)); sim.run(0.5, control); const o = sim.bodyPos(orange); const c = sim.bodyPos(bowl); check(Math.hypot(o[0] - c[0], o[1] - c[1]) < 0.055, "kitchen: the orange ends in the bowl"); } // Household tasks: every BDDL problem parses and grounds; the demonstrator does five of them; and the // contact checks the base makes every control period do not grow the WebAssembly heap. { const { sim, robot } = await loadScene("kitchen"); sim.reset(() => { robot.home(); robot.snapJoints(); }); sim.run(1.0, () => robot.update()); const settled = sim.snapshot(); const dir = join(ASSETS, "environments", "kitchen", "tasks"); const registry = JSON.parse(readFileSync(join(dir, "index.json"), "utf8")); const problems = Object.fromEntries(registry.tasks.map((t) => [t.file, readFileSync(join(dir, t.file), "utf8")])); const household = new Household(sim, robot, registry, problems); const literals = household.tasks.reduce((n, t) => n + t.goals.length, 0); check(household.tasks.length === registry.tasks.length && literals >= 20, `tasks: ${household.tasks.length}/${registry.tasks.length} BDDL problems, ${literals} goal conditions`); // a switch, things carried to a shelf, the fridge door and the island drawer opened and shut, the // microwave's door pushed shut for (const [id, seed] of [["turning_on_radio", 0], ["putting_away_groceries", 0], ["getting_the_juice", 0], ["putting_away_the_cube", 0], ["closing_the_microwave", 0]]) { const task = household.task(id); household.setup(task, seed, settled); const episode = new Episode(household, task, seed); runSkill(sim, robot, solve(household, episode), { maxSeconds: task.timeLimit, onTick: () => { household.update(); episode.tick(sim.controlDt); }, }); const r = episode.report(); check(r.success, `tasks: the demonstrator does ${id} #${seed} (${r.score} in ${r.time_s} s)`); } const watch = new ContactWatch(sim, robot.root); const heap = () => sim.data.qpos.buffer.byteLength; const before = heap(); for (let i = 0; i < 3000; i++) watch.check(); check(heap() === before, `memory: 3000 contact checks leave the WebAssembly heap as it was (${(before / 1e6).toFixed(0)} MB)`); } console.log(failed ? `${failed} check(s) failed` : "all checks passed"); process.exit(failed ? 1 : 0);