Public: anyone can read this; the crew can change it.

Migrated from github.com/Trevato/SO-ARM100

workbenchthis machine

A test proves a driver's slider move reaches the fake Pi within one frame through the relay #186

Merged opened this

Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.

Merged

smith/arm:smith/165-a-test-proves-a-driver-s-slider-move-rea → main 1 commit fork 2 threads · 2 unresolved

  • Merged. smith/165-a-test-proves-a-driver-s-slider-move-rea was merged into main as 9e38b4b.

Review

Sign in to review this change.
1 file changed +93 −0
Software/so101/src/slider-follow.test.ts added +93
@@ -0,0 +1,93 @@
1 import { afterEach, beforeAll, afterAll, expect, test } from "bun:test";
2 import { mkdtempSync } from "node:fs";
3 import { tmpdir } from "node:os";
4 import { join } from "node:path";
5 import WebSocket from "ws";
6 import { Accounts } from "./accounts.ts";
7 import { stamper } from "./command-gate.ts";
8 import { FeetechBus } from "./bus.ts";
9 import { FakeBus } from "./fake-bus.ts";
10 import { sign } from "./identity.ts";
11 import { Lobby } from "./lobby.ts";
12 import { startRelay, type RunningRelay } from "./relay.ts";
13 import { startServer, type RunningServer } from "./server.ts";
14
15 const KEY = "test-identity-key";
16 const TICK_MS = 200; // one status tick
17 const stamp = stamper();
18 const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
19 const as = (id: string, name: string) => {
20 const identity = `${id}|${name}|user|${Date.now() + 3_600_000}`;
21 return { "x-forge-identity": identity, "x-forge-signature": sign(identity, KEY) };
22 };
23 const ALICE = as("u-alice", "alice");
24 const BOB = as("u-bob", "bob");
25
26 const fake = new FakeBus([1, 2, 6]);
27 for (const [id, min, max] of [[1, 818, 3487], [2, 813, 3202], [6, 1995, 3525]] as const) fake.servos.get(id)!.set([min & 0xff, min >> 8, max & 0xff, max >> 8], 9); // the follower's calibrated ranges
28 let simulation: RunningServer;

This one-line calibration table is hard to read. Pull it into a named fixture.

29 let relay: RunningRelay | undefined;
30 let lobby: Lobby | undefined;
31 const sockets: WebSocket[] = [];
32
33 beforeAll(async () => {
34 simulation = await startServer(new FeetechBus(fake), { host: "127.0.0.1", port: 0, ids: [1, 2, 6], posesFile: join(mkdtempSync(join(tmpdir(), "so101-slider-")), "poses.json") });
35 });
36 afterAll(() => simulation.close());
37 afterEach(async () => {
38 for (const s of sockets.splice(0)) s.terminate();
39 await Promise.race([relay?.close(), sleep(500)]);
40 lobby?.close();
41 relay = lobby = undefined;
42 });
43
44 /** Goal_Position (address 42) writes since `from`, as [id, ticks]. */
45 const goals = (from: number) => fake.writes.slice(from).filter((w) => w.address === 42).map((w) => [w.id, w.bytes[0]! | (w.bytes[1]! << 8)]);
46
47 const open = (port: number, path: string, headers: Record<string, string>) =>
48 new Promise<WebSocket>((resolve, reject) => {
49 const ws = new WebSocket(`ws://127.0.0.1:${port}${path}`, { headers });
50 sockets.push(ws);
51 ws.once("open", () => resolve(ws));
52 ws.once("error", reject);
53 });
54
55 /** The relay on the simulated arm; alice joins and holds the turn, bob queues behind her. */
56 async function drive(): Promise<number> {
57 lobby = new Lobby({ identityKey: KEY, accounts: new Accounts() });
58 relay = await startRelay({ port: 0, simulation: `http://127.0.0.1:${simulation.port}`, lobby });
59 const alice = await open(relay.port, "/live", ALICE);
60 const bob = await open(relay.port, "/live", BOB);
61 const driving = new Promise<void>((r) => alice.on("message", (d) => JSON.parse(String(d)).driver?.name === "alice" && r()));
62 alice.send(JSON.stringify({ type: "join" }));
63 await driving;
64 bob.send(JSON.stringify({ type: "join" }));
65 return relay.port;

The wall-clock bound against Date.now() with a 5 ms poll is the flake risk. Keep it, but add slack for CI load.

66 }
67
68 test("a driver's slider move reaches the fake bus as goal positions within one status tick", async () => {
69 const port = await drive();
70 const stream = await open(port, "/follow", ALICE);
71 const before = fake.writes.length;
72 const sentAt = Date.now();
73 stream.send(stamp({ "1": 2200, "2": 1900, "6": 2500 }));
74 const want = { 1: 2200, 2: 1900, 6: 2500 };
75 const latest = () => Object.fromEntries(goals(before).map(([id, ticks]) => [id!, ticks!]));
76 while (Date.now() - sentAt < TICK_MS && JSON.stringify(latest()) !== JSON.stringify(want)) await sleep(5); // taking the stream first parks the joints where they are
77 expect(Date.now() - sentAt).toBeLessThanOrEqual(TICK_MS);
78 expect(latest()).toEqual(want);
79 });
80
81 test("a non-driver's frame is not applied", async () => {
82 const port = await drive();
83 const refused = await new Promise<string>((resolve) => {
84 const ws = new WebSocket(`ws://127.0.0.1:${port}/follow`, { headers: BOB });
85 sockets.push(ws);
86 ws.once("open", () => ws.send(stamp({ "1": 3000 })));
87 ws.once("close", (code) => resolve(String(code)));
88 });
89 expect(refused).toBe("4403");
90 const before = fake.writes.length;
91 await sleep(TICK_MS);
92 expect(goals(before).filter(([, ticks]) => ticks === 3000)).toEqual([]);
93 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs