fc5ee4f2c6
Add declarative robot packages, shared simulation lifecycle and Microban ONNX/PD walking with independent head control. Unify comic UI and chase camera; default robot packages to a 4.5m arena with real collision walls. Include regression tests, asset provenance/licenses and verification evidence.
182 lines
6.3 KiB
JavaScript
182 lines
6.3 KiB
JavaScript
import { test, expect } from "@playwright/test";
|
||
const boot = async (page, world) => {
|
||
if (world)
|
||
await page.route("**/robots/microban/robot.json*", async (route) => {
|
||
const response = await route.fetch(),
|
||
config = await response.json();
|
||
config.presentation.world = world;
|
||
await route.fulfill({ response, json: config });
|
||
});
|
||
await page.goto("/");
|
||
await expect(
|
||
page.getByRole("button", { name: "Pause", exact: true }),
|
||
).toBeEnabled();
|
||
await page.getByLabel("Robot", { exact: true }).selectOption("microban");
|
||
await expect
|
||
.poll(() => page.evaluate(() => window.__store.getState().robotId))
|
||
.toBe("microban");
|
||
await expect(
|
||
page.getByRole("button", { name: "Pause", exact: true }),
|
||
).toBeEnabled();
|
||
};
|
||
for (const mode of [
|
||
{ id: "bounded", forward: 8, back: 8, seconds: 70 },
|
||
{
|
||
id: "unbounded-diagnostic",
|
||
forward: 20,
|
||
back: 10,
|
||
seconds: 84,
|
||
world: "flat",
|
||
},
|
||
])
|
||
test(`three ${mode.seconds}s physics trials (${mode.id}): stand, forward, back, turn, and parallel head`, async ({
|
||
page,
|
||
}, info) => {
|
||
await boot(page, mode.world);
|
||
await page.evaluate(() => window.rl.pause());
|
||
const report = await page.evaluate(async (mode) => {
|
||
const results = [],
|
||
r = window.rl;
|
||
const pose = () => {
|
||
const [w, x, y, z] = r.data.qpos.slice(3, 7);
|
||
return {
|
||
time: r.data.time,
|
||
pos: Array.from(r.data.qpos.slice(0, 3)),
|
||
yaw: Math.atan2(2 * (w * z + x * y), 1 - 2 * (y * y + z * z)),
|
||
head: r.data.qpos[r.model.jnt("head").qposadr],
|
||
};
|
||
};
|
||
for (let trial = 0; trial < 3; trial++) {
|
||
await r.resetSim();
|
||
const segments = [];
|
||
for (const [name, seconds, vx, wz] of [
|
||
["stand", 10, 0, 0],
|
||
["forward", mode.forward, 0.15, 0],
|
||
["rest", 3, 0, 0],
|
||
["backward", mode.back, -0.1, 0],
|
||
["rest", 3, 0, 0],
|
||
["left", 10, 0, 0.6],
|
||
["rest", 3, 0, 0],
|
||
["right", 10, 0, -0.6],
|
||
["rest", 3, 0, 0],
|
||
["head_walk", 10, 0.15, 0],
|
||
["head_stop", 2, 0.15, 0],
|
||
]) {
|
||
const before = pose();
|
||
r.command.set([vx, 0, wz]);
|
||
if (name.startsWith("head_")) {
|
||
const previous = Array.from(r.lastAction);
|
||
r.toggle("head");
|
||
if (previous.some((v, i) => v !== r.lastAction[i]))
|
||
throw new Error("head toggle mutated policy history");
|
||
}
|
||
let yaw = 0,
|
||
previous = before.yaw,
|
||
minHead = Infinity,
|
||
maxHead = -Infinity,
|
||
minHeight = Infinity;
|
||
for (let i = 0; i < seconds * 10; i++) {
|
||
await r.step(5);
|
||
const p = pose();
|
||
let delta = p.yaw - previous;
|
||
while (delta > Math.PI) delta -= 2 * Math.PI;
|
||
while (delta < -Math.PI) delta += 2 * Math.PI;
|
||
yaw += delta;
|
||
previous = p.yaw;
|
||
minHead = Math.min(minHead, p.head);
|
||
maxHead = Math.max(maxHead, p.head);
|
||
minHeight = Math.min(minHeight, p.pos[2]);
|
||
if (r.fallen)
|
||
throw new Error(`Fallen trial ${trial} ${name} at ${p.time}`);
|
||
if (
|
||
mode.id === "bounded" &&
|
||
Math.max(Math.abs(p.pos[0]), Math.abs(p.pos[1])) >
|
||
r.world.boundary.half_extent - 0.05
|
||
)
|
||
throw new Error(
|
||
`Bounded gait trial reached a wall: ${name} ${p.pos}`,
|
||
);
|
||
}
|
||
const after = pose(),
|
||
dx = after.pos[0] - before.pos[0],
|
||
dy = after.pos[1] - before.pos[1];
|
||
segments.push({
|
||
name,
|
||
before,
|
||
after,
|
||
localDisplacement:
|
||
dx * Math.cos(before.yaw) + dy * Math.sin(before.yaw),
|
||
yaw,
|
||
minHead,
|
||
maxHead,
|
||
minHeight,
|
||
});
|
||
}
|
||
results.push({ trial, segments, actuation: r.actuationStats });
|
||
}
|
||
return results;
|
||
}, mode);
|
||
for (const trial of report) {
|
||
const find = (name) => trial.segments.find((s) => s.name === name);
|
||
expect(find("forward").localDisplacement).toBeGreaterThan(0.5);
|
||
expect(find("backward").localDisplacement).toBeLessThan(-0.2);
|
||
expect(find("left").yaw).toBeGreaterThan(1);
|
||
expect(find("right").yaw).toBeLessThan(-1);
|
||
expect(
|
||
find("head_walk").maxHead - find("head_walk").minHead,
|
||
).toBeGreaterThan(1);
|
||
expect(Math.abs(find("head_stop").after.head)).toBeLessThan(0.05);
|
||
expect(trial.actuation.longestSaturatedSeconds).toBeLessThan(0.5);
|
||
}
|
||
await info.attach(`microban-${mode.id}-three-trials.json`, {
|
||
body: JSON.stringify(report, null, 2),
|
||
contentType: "application/json",
|
||
});
|
||
console.log(
|
||
`Microban: 3 × ${mode.seconds}s ${mode.id}, no falls/resets, correct motion and independent head.`,
|
||
);
|
||
});
|
||
test("keyboard H toggles head, fallen Space resets, and real-time control runs near 50Hz", async ({
|
||
page,
|
||
}, info) => {
|
||
await boot(page);
|
||
await page
|
||
.locator("canvas")
|
||
.first()
|
||
.click({ position: { x: 700, y: 600 } });
|
||
await page.keyboard.press("h");
|
||
expect(
|
||
await page.evaluate(() => window.rl.skills.overlays.get("head").enabled),
|
||
).toBe(true);
|
||
const first = await page.evaluate(() => ({
|
||
sim: window.rl.data.time,
|
||
wall: performance.now(),
|
||
}));
|
||
await page.waitForTimeout(5000);
|
||
const second = await page.evaluate(() => ({
|
||
sim: window.rl.data.time,
|
||
wall: performance.now(),
|
||
fallen: window.rl.fallen,
|
||
}));
|
||
const ratio = (second.sim - first.sim) / ((second.wall - first.wall) / 1000);
|
||
expect(second.fallen).toBe(false);
|
||
expect(ratio).toBeGreaterThan(0.85);
|
||
expect(ratio).toBeLessThan(1.15);
|
||
await info.attach("real-time.json", {
|
||
body: JSON.stringify({ first, second, ratio }),
|
||
contentType: "application/json",
|
||
});
|
||
await page.evaluate(() => {
|
||
const r = window.rl;
|
||
r.data.qpos[3] = Math.SQRT1_2;
|
||
r.data.qpos[4] = Math.SQRT1_2;
|
||
r.data.qpos[5] = 0;
|
||
r.data.qpos[6] = 0;
|
||
r.mujoco.mj_forward(r.model, r.data);
|
||
});
|
||
await expect.poll(() => page.evaluate(() => window.rl.fallen)).toBe(true);
|
||
await expect(page.getByRole("status")).toContainText("Robot fallen");
|
||
await page.keyboard.press("Space");
|
||
await expect.poll(() => page.evaluate(() => window.rl.fallen)).toBe(false);
|
||
});
|