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

Migrated from github.com/Trevato/SO-ARM100

workbenchthis machine

The live arm never flaps to the simulation on one slow answer: keep the connection, retry, and say "reconnecting" #59

Merged opened this in answer to

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

Merged

quill/arm:quill/48-the-live-arm-never-flaps-to-the-simulati → main 1 commit fork

  • Merged. quill/48-the-live-arm-never-flaps-to-the-simulati was merged into main as 5ae001d.

Review

Sign in to review this change.
10 files changed +689 −114
Software/README.md +23 −10
@@ -133,8 +133,9 @@ The API, all JSON (the full list with bodies is at the top of
133133 (explicit `{"targets": {"3": 2048}}`), `/grip`, `/torque`, `/stop`,
134134 `/estop`, `/estop/clear`, `/record/start`, `/record/stop`,
135135 `/recording/delete`, `/play`;
136 `PUT /calibration/camera|handeye`; and the WebSocket `/follow` taking
137 `{"1": ticks, ...}` frames. One thing drives the arm at a time. Every move
136 `PUT /calibration/camera|handeye`; the WebSocket `/follow` taking
137 `{"1": ticks, ...}` frames; and the read-only WebSocket `/watch`, which sends
138 the `/status` body every 250 ms, one bus read shared by every watcher. One thing drives the arm at a time. Every move
138139 goes through the joint supervisor; `/stop` ends a move, a playback, a
139140 recording, or a stream, and the arm holds. The stream is clamped to the
140141 joint limits, eases over to the first frame, drops jumps no hand makes, is
@@ -217,16 +218,28 @@ destination, or can't reach it itself, the top bar and `/link` give its
217218 reason, e.g. **the forge's egress proxy answered 502: 100.89.95.68:8101 did
218219 not answer**. That means the forge's machine has no route to the Pi, which
219220 is not something the app can fix. Every `/status` says which arm answered:
220 `"source": "live <host:port>"` or `"source": "simulated: <why>"`.
221 `"source": "live <host:port>"`, `"source": "reconnecting to the arm: <why>"`
222 or `"source": "simulated: <why>"`.
221223
222224 With `ARM_SERVER` unset, the app runs the simulated arm and says
223 **Simulated**. When it is set but the server doesn't answer `/status` within
224 1.5 s, the top bar says **Simulated · \<host\> unreachable (why)**, and the
225 page runs on the simulation instead of waiting on the dead link. The app
226 checks again every 5 s and switches back by itself. The page reloads on
227 either switch, because the other arm has its own limits and poses. A move
228 that was in flight to the real arm when the link dropped comes back as an
229 error; it is not replayed on the simulation. Changes (`POST`, `PUT`, the
225 **Simulated**. With it set, the app holds one WebSocket to the arm server's
226 `/watch` (`src/arm-link.ts`), which pushes its status four times a second, and
227 every viewer's `/status` is answered from that. The Pi is asked once, however
228 many people are watching. An arm server from before `/watch` is polled over
229 HTTP instead. One slow or dropped answer changes nothing. The link only counts
230 as dropped when the connection closes, or brings no status for 2 s.
231
232 A dropped link is never swapped for the simulation. The top bar says
233 **Reconnecting to the arm…**, the page keeps showing the last known pose, and
234 the app retries after 0.5, 1, 2, then every 4 s. While it reconnects, driving
235 is paused: commands come back as 503, and a stream's frames are not passed on.
236 The arm server's dead-man sees the silence and holds the arm. Stop and the
237 emergency stop are still sent. When the arm answers again the page carries on
238 without reloading. Only after 60 s down does the app run the page on the
239 simulation, and then the top bar says **OFFLINE · simulated arm** in red, with
240 a banner saying that this is not the real arm. The retries go on, and the page
241 reloads onto the real arm when it comes back. A move that was in flight to the
242 real arm when the link dropped comes back as an error; it is not replayed. Changes (`POST`, `PUT`, the
230243 stream) are only accepted from the app's own page, so another site can't
231244 drive the arm through it.
232245
Software/so101/bench/fake-serve.ts +3 −2
@@ -4,7 +4,8 @@
44 * Goals become positions at once, so every move "arrives"; poses and recordings go to a temp dir.
55 * PORT, HOST and FORGE_APP_DATA override the port, the 127.0.0.1 bind and the temp dir (how the forge runs it).
66 * ARM_SERVER=ws://<host>:8101 puts a real arm's `so101 serve` behind the same page: the relay in src/relay.ts
7 * sends the arm API, /follow and the cameras there while it answers, and to the simulation when it does not.
7 * sends the arm API, /follow and the cameras there. A dropped link says "reconnecting" and retries (src/arm-link.ts);
8 * only after 60 s down does the page go over to the simulation, labelled offline.
89 * The owner lists the cameras on /admin/camera-list (kept in cameras.json under the data dir); the first one comes from ARM_SERVER.
910 * Anyone may watch; only the person whose turn it is may drive (src/lobby.ts). People join with a name picked here
1011 * (src/accounts.ts, kept in accounts.sqlite); the forge signs who its own signed-in people are with FORGE_APP_IDENTITY_KEY.
@@ -60,4 +61,4 @@ const cameras = new CameraHub({ file: join(dir, "cameras.json"), seed: remote ?
6061 const leaders = new LeaderCalibrations({ file: join(dir, "leader-calibrations.json") });
6162 const relay = await startRelay({ host, port, simulation: `http://127.0.0.1:${simulation.port}`, remote, problem, lobby, cameras, leaders });
6263 const { mode, host: armHost } = relay.link();
63 console.log(`arm on http://${host}:${relay.port} (${mode === "live" ? `live arm ${armHost}` : "simulated"}; data in ${dir}${local ? "; local: every visitor drives as the owner" : ""})`);
64 console.log(`arm on http://${host}:${relay.port} (${mode === "simulated" ? "simulated" : `arm ${armHost}: ${mode}`}; data in ${dir}${local ? "; local: every visitor drives as the owner" : ""})`);
Software/so101/src/admin-relay.test.ts +3 −2
@@ -76,7 +76,7 @@ test("the arm server set on /admin is the one the relay uses, at once; a bad one
7676 expect(await bad.text()).not.toContain("<script>x");
7777 await fetch(`${base}/admin`, form(owner, { action: "save", armServer: "http://127.0.0.1:1" }));
7878 expect(relay!.link().host).toBe("127.0.0.1:1");
79 expect(relay!.link().mode).toBe("simulated");
79 expect(relay!.link().mode).toBe("reconnecting"); // a named arm server that does not answer is retried, not swapped for the simulation
8080 await fetch(`${base}/admin`, form(owner, { action: "save", armServer: "" }));
8181 expect(relay!.link().host).toBeNull();
8282 });
@@ -97,7 +97,8 @@ test("reading /link never changes anything; the arm server switches when the set
9797 const base = await boot();
9898 const owner = as("trevato", "admin");
9999 await fetch(`${base}/admin`, form(owner, { action: "save", armServer: "http://127.0.0.1:1" }));
100 const after = await (await fetch(`${base}/link`)).json();
100 await new Promise((r) => setTimeout(r, 200)); // the first attempt at the new address is over: what /link says now is settled
101 const after = (await (await fetch(`${base}/link`)).json()) as { host: string };
101102 expect(after.host).toBe("127.0.0.1:1");
102103 expect(await (await fetch(`${base}/link`)).json()).toEqual(after);
103104 });
Software/so101/src/relay.test.ts +96 −19
@@ -5,7 +5,7 @@ import { connect, createServer as createTcpServer, type Server as TcpServer } fr
55 import { tmpdir } from "node:os";
66 import { join } from "node:path";
77 import { gunzipSync } from "node:zlib";
8 import WebSocket from "ws";
8 import WebSocket, { WebSocketServer } from "ws";
99 import { FeetechBus } from "./bus.ts";
1010 import { FakeBus } from "./fake-bus.ts";
1111 import { OFFLINE_FRAME } from "./offline-frame.ts";
@@ -20,7 +20,7 @@ const listening = (server: Server | TcpServer) => new Promise<number>((r) => ser
2020 /** An arm server on a fake bus; `ids` tells the simulation (six joints) and the "real arm" (three) apart. */
2121 async function arm(ids: number[]): Promise<RunningServer> {
2222 const fake = new FakeBus(ids);
23 return startServer(new FeetechBus(fake), { host: "127.0.0.1", port: 0, ids, posesFile: join(mkdtempSync(join(tmpdir(), "so101-relay-")), "poses.json"), watchdogMs: 60 });
23 return startServer(new FeetechBus(fake), { host: "127.0.0.1", port: 0, ids, posesFile: join(mkdtempSync(join(tmpdir(), "so101-relay-")), "poses.json"), watchdogMs: 60, watchEveryMs: 10 });
2424 }
2525 const remoteAt = (port: number, camera?: number): Remote => parseRemote({ ARM_SERVER: `ws://127.0.0.1:${port}`, ...(camera ? { ARM_CAMERA_1: `http://127.0.0.1:${camera}/stream` } : {}) })!;
2626 const JPEG = Buffer.from([0xff, 0xd8, 0x4a, 0x50, 0x45, 0x47, 0xff, 0xd9]);
@@ -48,6 +48,9 @@ const joints = async (base: string) => ((await (await fetch(`${base}/status`)).j
4848 const source = async (base: string) => ((await (await fetch(`${base}/status`)).json()) as { source: string }).source;
4949 const link = async (base: string) => (await (await fetch(`${base}/link`)).json()) as { mode: string; host: string | null; problem: string | null };
5050
51 /** The link's timings at 1/100: retries after 5–40 ms, offline after 600 ms (60 s). */
52 const FAST = { backoffMs: [5, 10, 20, 40], staleMs: 60, offlineAfterMs: 600, connectTimeoutMs: 50, pollEveryMs: 10 };
53
5154 let simulation: RunningServer;
5255 let simBase: string;
5356 beforeAll(async () => {
@@ -198,19 +201,25 @@ describe("relay", () => {
198201 expect(refused.status).toBe(403);
199202 });
200203
201 test("unreachable: the simulation answers within 3 s and the page is told why", async () => {
204 test("unreachable from the start: reconnecting, never the simulation, until 60 s pass (at 1/100 time)", async () => {
202205 hang = createTcpServer(() => {}); // accepts and never answers
203206 const port = await listening(hang);
204207 const started = Date.now();
205 relay = await startRelay({ port: 0, simulation: simBase, remote: remoteAt(port) });
208 relay = await startRelay({ port: 0, simulation: simBase, remote: remoteAt(port), link: FAST });
206209 const base = `http://127.0.0.1:${relay.port}`;
207 expect(await joints(base)).toBe(6);
208 const l = await link(base);
209210 expect(Date.now() - started).toBeLessThan(3000);
210 expect(l.mode).toBe("simulated");
211 const l = await link(base);
212 expect(l.mode).toBe("reconnecting");
211213 expect(l.host).toBe(`127.0.0.1:${port}`);
212214 expect(l.problem).toContain("unreachable");
213 expect(await source(base)).toBe(`simulated: ${l.problem}`);
215 const s = await fetch(`${base}/status`);
216 expect(s.status).toBe(503);
217 expect(await s.json()).toMatchObject({ reconnecting: true, source: expect.stringContaining(`reconnecting to the arm: 127.0.0.1:${port}`) });
218 expect((await fetch(`${base}/move`, { method: "POST", body: '{"targets":{"1":2000}}' })).status).toBe(503);
219 await sleep(650);
220 expect((await link(base)).mode).toBe("offline");
221 expect(await joints(base)).toBe(6);
222 expect(await source(base)).toStartWith("simulated: offline, ");
214223 });
215224
216225 test("the forge's egress proxy cannot reach the arm: its reason is the one the page and /status give", async () => {
@@ -218,9 +227,9 @@ describe("relay", () => {
218227 relay = await startRelay({ port: 0, simulation: simBase, remote: remoteAt(await listening(camera)) });
219228 const base = `http://127.0.0.1:${relay.port}`;
220229 const l = await link(base);
230 expect(l.mode).toBe("reconnecting");
221231 expect(l.problem).toContain("the forge's egress proxy answered 502: 100.89.95.68:8101 did not answer");
222 expect(await source(base)).toBe(`simulated: ${l.problem}`);
223 expect(await joints(base)).toBe(6);
232 expect(await source(base)).toEndWith(l.problem!);
224233 });
225234
226235 test("inside a cell, /follow reaches the arm server through the egress proxy", async () => {
@@ -253,7 +262,8 @@ describe("relay", () => {
253262 const ws = new WebSocket(`ws://127.0.0.1:${relay.port}/follow`);
254263 await new Promise((r, j) => (ws.once("open", r), ws.once("error", j)));
255264 await sleep(50);
256 expect(tunnelled).toEqual([`127.0.0.1:${remote.port}`]);
265 // Both connections to the arm go through it: the link's /watch, and the stream.
266 expect(tunnelled).toEqual([`127.0.0.1:${remote.port}`, `127.0.0.1:${remote.port}`]);
257267 expect(((await (await fetch(`http://127.0.0.1:${remote.port}/status`)).json()) as { driving: string | null }).driving).toBe("stream");
258268 ws.close();
259269 } finally {
@@ -264,20 +274,87 @@ describe("relay", () => {
264274 }
265275 });
266276
267 test("an arm server that goes away mid-session falls back on the next request, /follow included", async () => {
277 test("one connection to the arm server: every viewer's /status comes from it, and one slow answer keeps it live", async () => {
268278 remote = await arm([1, 2, 6]);
269 relay = await startRelay({ port: 0, simulation: simBase, remote: remoteAt(remote.port), probeEveryMs: 60_000 });
279 relay = await startRelay({ port: 0, simulation: simBase, remote: remoteAt(remote.port), link: FAST });
280 const base = `http://127.0.0.1:${relay.port}`;
281 const asked: number[] = [];
282 for (let i = 0; i < 20; i++) {
283 asked.push(await joints(base));
284 await sleep(5);
285 }
286 expect(asked.every((n) => n === 3)).toBe(true);
287 expect((await link(base)).mode).toBe("live");
288 });
289
290 test("an arm server that goes away: reconnecting with the last pose, driving suspended, then live again on the same arm", async () => {
291 remote = await arm([1, 2, 6]);
292 const port = remote.port;
293 // A stopped server closes the watch at once; the patience for a quiet one is long here, so a move's busy bus is no drop.
294 relay = await startRelay({ port: 0, simulation: simBase, remote: remoteAt(port), link: { ...FAST, staleMs: 1000 } });
270295 const base = `http://127.0.0.1:${relay.port}`;
271296 expect(await joints(base)).toBe(3);
272297 await shut(remote);
273298 remote = undefined;
274 const started = Date.now();
275 expect(await joints(base)).toBe(6);
276 expect(Date.now() - started).toBeLessThan(3000);
277 expect((await link(base)).mode).toBe("simulated");
299 await sleep(80);
300 expect((await link(base)).mode).toBe("reconnecting");
301 const s = await fetch(`${base}/status`);
302 expect(s.status).toBe(200);
303 const body = (await s.json()) as { joints: unknown[]; reconnecting: boolean; source: string };
304 expect(body.joints.length).toBe(3); // the last known pose, not the simulation's six joints
305 expect(body.reconnecting).toBe(true);
306 expect(body.source).toStartWith(`reconnecting to the arm: 127.0.0.1:${port}`);
307 const move = await fetch(`${base}/move`, { method: "POST", body: '{"targets":{"1":2000}}' });
308 expect(move.status).toBe(503);
309 expect(((await move.json()) as { error: string }).error).toContain("reconnecting to the arm");
278310 const ws = new WebSocket(`ws://127.0.0.1:${relay.port}/follow`);
279 await new Promise((r, j) => (ws.once("open", r), ws.once("error", j)));
280 ws.close();
311 const closed = await new Promise<{ code: number; why: string }>((r) => ws.on("close", (code, why) => r({ code, why: String(why) })));
312 expect(closed).toEqual({ code: 1013, why: "reconnecting to the arm…" });
313
314 remote = await startServer(new FeetechBus(new FakeBus([1, 2, 6])), { host: "127.0.0.1", port, ids: [1, 2, 6], posesFile: join(mkdtempSync(join(tmpdir(), "so101-relay-")), "poses.json"), watchEveryMs: 10 });
315 await sleep(120);
316 expect((await link(base)).mode).toBe("live");
317 expect(await source(base)).toBe(`live 127.0.0.1:${port}`);
318 expect((await fetch(`${base}/move`, { method: "POST", body: '{"targets":{"1":2000}}' })).status).toBe(200);
319 });
320
321 test("a stream on the real arm goes quiet while the link reconnects, and flows again once it is back", async () => {
322 // An arm server whose /watch can be silenced while its /follow stays up: what the relay passes on is counted.
323 let watching = true;
324 const got: string[] = [];
325 const wss = new WebSocketServer({ noServer: true });
326 camera = createServer((_req, res) => res.writeHead(404).end());
327 camera.on("upgrade", (req, socket, head) =>
328 wss.handleUpgrade(req, socket, head, (ws) => {
329 if (req.url === "/follow") ws.on("message", (d) => got.push(String(d)));
330 }),
331 );
332 const pump = setInterval(() => watching && wss.clients.forEach((c) => c.send('{"joints":[]}')), 10);
333 try {
334 relay = await startRelay({ port: 0, simulation: simBase, remote: remoteAt(await listening(camera)), link: FAST });
335 const ws = new WebSocket(`ws://127.0.0.1:${relay.port}/follow`);
336 await new Promise((r, j) => (ws.once("open", r), ws.once("error", j)));
337 ws.send('{"1": 2050}');
338 await sleep(30);
339 expect(got).toEqual(['{"1": 2050}']);
340 watching = false;
341 await sleep(100);
342 expect(relay.link().mode).toBe("reconnecting");
343 ws.send('{"1": 2060}');
344 await sleep(30);
345 expect(got).toEqual(['{"1": 2050}']); // the arm server's dead-man sees silence, and holds
346 expect(ws.readyState).toBe(WebSocket.OPEN);
347 watching = true;
348 await sleep(80);
349 expect(relay.link().mode).toBe("live");
350 ws.send('{"1": 2070}');
351 await sleep(30);
352 expect(got).toEqual(['{"1": 2050}', '{"1": 2070}']);
353 ws.close();
354 } finally {
355 clearInterval(pump);
356 for (const c of wss.clients) c.terminate();
357 }
281358 });
282359
283360 test("a bad setting is reported and the arm is simulated", async () => {
Software/so101/src/relay.ts +85 −69
@@ -2,8 +2,11 @@
22 * The hosted app's front door: one origin for the browser, whichever arm stands behind it.
33 *
44 * With ARM_SERVER unset, everything goes to the simulated arm. With it set (ws://<host>:8101, http://…, https://…),
5 * the arm API, the WebSocket /follow and the cameras go to that `so101 serve`, as long as it answers; when it does
6 * not, the relay says so on /link and answers from the simulation instead, so the page never hangs on a dead link.
5 * the arm API, the WebSocket /follow and the cameras go to that `so101 serve`. The relay holds one connection to it
6 * (src/arm-link.ts) and answers every /status from it. When that connection drops, the relay never jumps to the
7 * simulation: it says "reconnecting" and keeps showing the last known pose while it retries. Driving is suspended
8 * (commands are refused and stream frames are not passed on, so the arm server's dead-man holds the arm); a stop
9 * still goes through. Only after 60 s down does the page go over to the simulation, labelled offline.
710 * The page itself, the 3D model and the vendored scripts always come from here.
811 * Inside a forge cell the only way out is the egress proxy (src/egress.ts): fetch finds it on its own, the WebSocket
912 * to the arm server is told it, and when the proxy refuses or cannot dial, its reason is the one /link and /status give.
@@ -22,8 +25,10 @@
2225 * POST /join {"name": "…"}: become someone here, no forge account needed (src/accounts.ts); the answer
2326 * sets the HttpOnly session cookie and names you: {"you": {"id", "name", "owner"}}
2427 * GET /admin, POST /admin the owner's page: pause, end a turn, take control, the settings, the log
25 * GET /link {"mode": "live"|"simulated", "host": "<host:port>"|null, "problem": "…"|null}
26 * GET /status the arm's own /status, and which arm said it: "source": "live <host:port>" or "simulated: <why>"
28 * GET /link {"mode": "live"|"reconnecting"|"offline"|"simulated", "host": "<host:port>"|null, "problem": "…"|null}
29 * GET /status the arm's own /status, and which arm said it: "source": "live <host:port>", "reconnecting to the arm:
30 * <why>" (the last known status, with "reconnecting": true and "seenAt"; 503 before
31 * there is one), or "simulated: <why>" ("simulated: offline, <why>" after 60 s down)
2732 * GET /cameras the listed cameras: [{"id", "name", "online"}]
2833 * GET /camera/<id>/<stream|state|snapshot> one listed camera, one upstream connection shared by all its viewers (src/cameras.ts, src/camera-feed.ts);
2934 * the "camera offline" picture when its source is down
@@ -39,6 +44,7 @@ import type { Duplex } from "node:stream";
3944 import { Readable } from "node:stream";
4045 import { pipeline } from "node:stream/promises";
4146 import WebSocket, { WebSocketServer } from "ws";
47 import { ArmLink, type LinkTimings } from "./arm-link.ts";
4248 import { acceptsGzip, GzipCache, worthCompressing } from "./compress.ts";
4349 import { AccountError, sessionCookie, visitor } from "./accounts.ts";
4450 import { AdminError, changeFromForm } from "./admin.ts";
@@ -64,7 +70,8 @@ export interface Remote {
6470 }
6571
6672 export interface Link {
67 mode: "live" | "simulated";
73 /** "simulated" only when no arm server is named; "offline" when one is, and has been gone 60 s. */
74 mode: "live" | "reconnecting" | "offline" | "simulated";
6875 /** The arm server the setting names, live or not. */
6976 host: string | null;
7077 /** Why a configured arm server is not in use right now. */
@@ -81,11 +88,11 @@ export interface RelayOptions {
8188 cameras?: CameraHub;
8289 /** A setting that could not be used (shown on /link while simulating). */
8390 problem?: string | null;
84 /** How long the arm server has to answer /status before it counts as unreachable. */
85 probeTimeoutMs?: number;
86 /** How often the arm server is checked, whichever way the last check went. */
87 probeEveryMs?: number;
88 /** How long a read through to the arm server may wait for its answer before the simulation answers instead. */
91 /** The link's retries and its patience (src/arm-link.ts). */
92 link?: Partial<LinkTimings>;
93 /** How often the admin's arm server setting is looked at again. */
94 followEveryMs?: number;
95 /** How long a read through to the arm server may wait for its answer. */
8996 readTimeoutMs?: number;
9097 /** Who may command the arm; without one, anyone who reaches the relay may (a run on one's own machine). */
9198 lobby?: Lobby | null;
@@ -120,6 +127,9 @@ const LEADER = "/leader/calibration";
120127 const ADMIN_ONLY = new Set(["POST /estop/clear", "PUT /settings"]);
121128 /** The driver's, and any admin's at any time. */
122129 const ADMIN_TOO = new Set(["POST /stop", "POST /estop"]);
130 /** What still goes to the arm server while the link is reconnecting: stopping it. */
131 const STOPS = new Set(["POST /stop", "POST /estop"]);
132 const RECONNECTING = "reconnecting to the arm";
123133 /** The close code that tells the arm server its driver was lost, not done: it parks the arm. */
124134 export const LOST_CLOSE = 4001;
125135 const MAX_JOIN_BODY = 1024;
@@ -189,8 +199,7 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> {
189199 let named = "";
190200 const simulation = options.simulation.replace(/\/+$/, "");
191201 const simulationWs = simulation.replace(/^http/, "ws");
192 const probeTimeoutMs = options.probeTimeoutMs ?? 1500;
193 const probeEveryMs = options.probeEveryMs ?? 5000;
202 const followEveryMs = options.followEveryMs ?? 5000;
194203 const readTimeoutMs = options.readTimeoutMs ?? 2500;
195204 const lobby = options.lobby ?? null;
196205 const heartbeatMs = options.heartbeatMs ?? 1000;
@@ -198,51 +207,48 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> {
198207 const leaders = options.leaders ?? new LeaderCalibrations();
199208
200209 // ------------------------------------------------------------------ the link
201 let live = false;
210 let armLink: ArmLink | null = null;
202211 let problem: string | null = options.problem ?? null;
203 /** Requests in flight to the arm server: cut when it is found gone, so nothing waits on a dead link. */
212 /** Requests in flight to the arm server: cut when the link drops, so nothing waits on a dead one. */
204213 const inFlight = new Set<AbortController>();
205 const up = () => {
206 if (!live && remote) console.log(`arm server ${remote.host}: live`);
207 live = true;
208 problem = null;
209 };
210 const down = (why: string) => {
214 const connect = () => {
215 armLink?.close();
216 armLink = null;
211217 if (!remote) return;
212 if (live || problem === null) console.error(`arm server ${remote.host}: ${why}; simulating`);
213 live = false;
214 problem = `${remote.host} unreachable (${why})`;
215 for (const c of inFlight) c.abort(new Error(why));
218 const host = remote.host;
219 armLink = new ArmLink({
220 remote,
221 open: (url, timeoutMs) => open(url, timeoutMs),
222 timings: options.link,
223 onChange: (state, previous) => {
224 if (state === "live") console.log(`arm server ${host}: live`);
225 else console.error(`arm server ${host}: ${state === "offline" ? "offline; simulating" : "reconnecting"} (${armLink?.problem})`);
226 if (previous === "live") for (const c of inFlight) c.abort(new Error(armLink?.problem ?? "the link dropped"));
227 },
228 });
216229 };
217 let probing: Promise<void> | null = null;
218 const probe = () =>
219 (probing ??= (async () => {
220 try {
221 const r = await fetch(`${remote!.http}/status`, { signal: AbortSignal.timeout(probeTimeoutMs), cache: "no-store" });
222 if (r.ok) {
223 await r.body?.cancel();
224 up();
225 } else down(await whyNot(r, "/status"));
226 } catch (err) {
227 down((err as Error)?.name === "TimeoutError" ? `no answer in ${probeTimeoutMs} ms` : reason(err));
228 } finally {
229 probing = null;
230 }
231 })());
232 /** Follow the admin's arm server setting: a change cuts what is in flight and probes the new address at once. */
230 /** Follow the admin's arm server setting: a change cuts what is in flight and connects to the new address at once. */
233231 const follow = () => {
234232 if (!lobby) return;
235233 const address = lobby.admin.settings.armServer;
236234 if (address === named) return;
237235 named = address;
238236 remote = address ? parseRemote({ ARM_SERVER: address }) : environment;
239 live = false;
240237 problem = null;
241238 for (const c of inFlight) c.abort(new Error("the arm server was changed"));
242 if (remote) void probe();
239 connect();
240 };
241 const mode = (): Link["mode"] => (remote && armLink ? armLink.state : "simulated");
242 /** Whether the arm server, not the simulation, is the arm right now: live, or only briefly gone. */
243 const onArm = () => mode() === "live" || mode() === "reconnecting";
244 const link = (): Link => ({ mode: mode(), host: remote?.host ?? null, problem: armLink ? armLink.problem : problem });
245 const source = () => {
246 const m = mode();
247 if (m === "live") return `live ${remote!.host}`;
248 if (m === "reconnecting") return `${RECONNECTING}: ${armLink!.problem}`;
249 if (m === "offline") return `simulated: offline, ${armLink!.problem}`;
250 return `simulated: ${problem ?? "ARM_SERVER is not set"}`;
243251 };
244 const link = (): Link => ({ mode: live ? "live" : "simulated", host: remote?.host ?? null, problem });
245 const source = () => (live && remote ? `live ${remote.host}` : `simulated: ${problem ?? "ARM_SERVER is not set"}`);
246252
247253 // ------------------------------------------------------------------ HTTP
248254 const json = (res: ServerResponse, status: number, body: unknown) => {
@@ -303,18 +309,18 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> {
303309 }
304310 };
305311
306 /** The arm's /status, from the live arm while it answers and the simulation otherwise, saying which it is. */
312 /**
313 * The arm's /status, saying which arm it is: from the link while it is live, the last one it had while reconnecting,
314 * and from the simulation otherwise.
315 */
307316 const status = async (res: ServerResponse, path: string) => {
308 let answer: Response | undefined;
309 if (live && remote) {
310 try {
311 answer = await fetch(remote.http + path, { signal: AbortSignal.timeout(readTimeoutMs), cache: "no-store" });
312 if (!answer.ok) down(await whyNot(answer, "/status"));
313 } catch (err) {
314 down((err as Error)?.name === "TimeoutError" ? `no answer in ${readTimeoutMs} ms` : reason(err));
315 }
317 if (onArm()) {
318 const last = armLink!.last;
319 const reconnecting = mode() === "reconnecting";
320 if (!last) return json(res, 503, { error: `${RECONNECTING}…`, source: source(), reconnecting });
321 return json(res, 200, { ...last, source: source(), reconnecting, ...(reconnecting ? { seenAt: new Date(armLink!.lastAt).toISOString() } : {}) });
316322 }
317 if (!live || !answer?.ok) answer = await fetch(simulation + path, { cache: "no-store" });
323 const answer = await fetch(simulation + path, { cache: "no-store" });
318324 const body = (await answer.json()) as Record<string, unknown>;
319325 return json(res, answer.status, { ...body, source: source() });
320326 };
@@ -533,14 +539,15 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> {
533539 // Who stopped or cleared the arm is the relay's to say, never the page's; every other command is held to the admin's speed cap.
534540 const raw = reading || route === "POST /estop" || route === "POST /estop/clear" ? undefined : await readBody(req);
535541 const body = reading ? undefined : !raw ? Buffer.from(JSON.stringify({ by })) : lobby ? capBody(url.pathname, raw, lobby.maxSpeedPercent) : raw;
536 if (LOCAL.test(url.pathname) || !live || !remote) return await pass(res, await ask(simulation + path, req, body, null, false));
542 if (LOCAL.test(url.pathname) || !onArm() || !remote) return await pass(res, await ask(simulation + path, req, body, null, false));
543 if (mode() === "reconnecting" && !reading && !STOPS.has(route)) {
544 return json(res, 503, { error: `${RECONNECTING} at ${remote.host}…; driving resumes when it answers`, reconnecting: true });
545 }
537546 try {
538 // A read must answer fast or fall back; a move or a playback answers when the arm arrives, and is only cut when the link is.
547 // A read must answer fast; a move or a playback answers when the arm arrives, and is only cut when the link drops.
539548 return await pass(res, await ask(remote.http + path, req, body, reading ? readTimeoutMs : null, true));
540549 } catch (err) {
541 down(reason(err));
542 if (!reading) return json(res, 502, { error: `the arm server at ${remote.host} did not answer (${reason(err)}); the page is now on the simulation` });
543 return await pass(res, await ask(simulation + path, req, body, null, false));
550 return json(res, mode() === "reconnecting" ? 503 : 502, { error: `the arm server at ${remote.host} did not answer (${reason(err)})`, reconnecting: mode() === "reconnecting" });
544551 }
545552 } catch (err) {
546553 if (!res.headersSent) json(res, 502, { error: reason(err) });
@@ -564,17 +571,21 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> {
564571 clearTimeout(timer);
565572 resolve(ws);
566573 });
567 ws.once("error", (err) => {
574 ws.on("error", (err) => {
568575 clearTimeout(timer);
569 reject(err);
576 reject(err); // the first one settles it; a later one (Bun's client can send two) must still find a listener
570577 });
571578 });
572579 /** Close codes a server may send on; the rest (1005, 1006, 1015) only describe a connection that died. */
573580 const sendable = (code: number) => (code >= 1000 && code <= 1003) || (code >= 1007 && code <= 1014) || (code >= 3000 && code <= 4999);
574581 /** Each open /follow stream and whose it is, so a turn that ends can close it. */
575582 const streams = new Map<WebSocket, string>();
576 /** `base` is the arm server's HTTP side, asked where the joints are so the speed cap starts from there. */
583 /**
584 * `base` is the arm server's HTTP side, asked where the joints are so the speed cap starts from there. On the real arm,
585 * frames are not passed on while the link reconnects: the stream goes quiet, and the arm server's dead-man holds the arm.
586 */
577587 const bridge = (browser: WebSocket, arm: WebSocket, base: string, driver: string | null) => {
588 const real = base !== simulation;
578589 if (driver !== null) {
579590 streams.set(browser, driver);
580591 browser.on("close", () => streams.delete(browser));
@@ -606,7 +617,7 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> {
606617 if (lobby && !lobby.turns.isDriver(driver) && !lobby.local) return;
607618 lobby?.activity(driver);
608619 }
609 if (arm.readyState !== WebSocket.OPEN) return;
620 if (arm.readyState !== WebSocket.OPEN || (real && mode() !== "live")) return;
610621 const percent = lobby?.maxSpeedPercent ?? 100;
611622 if (percent >= 100) {
612623 if (lobby) cap.cap(String(data), percent); // keeps each joint's last target, so a cap set mid-stream starts from there
@@ -651,12 +662,15 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> {
651662 void (async () => {
652663 let arm: WebSocket;
653664 let base: string;
665 if (remote && mode() === "reconnecting") {
666 return wss.handleUpgrade(req, socket, head, (browser) => browser.close(1013, `${RECONNECTING}…`));
667 }
654668 try {
655 if (!remote || !live) throw null;
669 if (!remote || mode() !== "live") throw null;
656670 arm = await open(`${remote.ws}/follow`, readTimeoutMs);
657671 base = remote.http;
658672 } catch (err) {
659 if (err) down(reason(err));
673 if (err) return wss.handleUpgrade(req, socket, head, (browser) => browser.close(1013, `the arm server at ${remote!.host} did not take the stream (${reason(err)})`));
660674 try {
661675 arm = await open(`${simulationWs}/follow`, readTimeoutMs);
662676 base = simulation;
@@ -674,7 +688,7 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> {
674688 const settingsChanged = lobby?.onSettingsChange(follow);
675689 const turnEnded = lobby?.onTurnEnd((who) => {
676690 for (const [browser, id] of streams) if (id === who.id) browser.close(1000, "your turn ended");
677 const base = remote && live ? remote.http : simulation;
691 const base = remote && onArm() ? remote.http : simulation;
678692 fetch(`${base}/stop`, { method: "POST", signal: AbortSignal.timeout(readTimeoutMs) }).then(
679693 (r) => r.body?.cancel(),
680694 (err: unknown) => console.error(`stop after ${who.name}'s turn: ${reason(err)}`),
@@ -682,9 +696,10 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> {
682696 });
683697
684698 // ------------------------------------------------------------------ start
685 follow();
686 if (remote) await probe(); // bounded by the probe timeout: the first page already knows which arm it has
687 const timer = remote || lobby ? setInterval(() => (follow(), remote && void probe()), probeEveryMs) : undefined;
699 if (lobby) follow();
700 if (!armLink) connect();
701 await (armLink as ArmLink | null)?.ready; // bounded by the first attempt's timeout: the first page already knows which arm it has
702 const timer = lobby ? setInterval(follow, followEveryMs) : undefined;
688703 timer?.unref?.();
689704 await new Promise<void>((resolve) => http.listen(options.port ?? 8102, options.host ?? "127.0.0.1", resolve));
690705 const address = http.address();
@@ -696,6 +711,7 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> {
696711 clearInterval(timer);
697712 settingsChanged?.();
698713 turnEnded?.();
714 armLink?.close();
699715 if (!options.cameras) cameras.close();
700716 for (const c of inFlight) c.abort();
701717 for (const client of wss.clients) client.terminate();
Software/so101/src/server.test.ts +24
@@ -80,6 +80,30 @@ describe("so101 serve", () => {
8080 const p = await fetch(`${base}/pose/go`, { method: "POST", body: JSON.stringify({ name: "nope" }) });
8181 expect(p.status).toBe(404); // no poses file at all: nothing saved yet
8282 });
83 test("/watch pushes the status to every watcher without taking the arm; any other path is not a WebSocket", async () => {
84 const watch = () =>
85 new Promise<{ ws: WebSocket; first: { joints: Array<{ id: number }>; driving: unknown } }>((resolve, reject) => {
86 const ws = new WebSocket(`${base.replace("http", "ws")}/watch`);
87 ws.once("message", (d) => resolve({ ws, first: JSON.parse(String(d)) }));
88 ws.on("error", reject);
89 });
90 const [a, b] = await Promise.all([watch(), watch()]);
91 expect(a.first.joints.map((j) => j.id)).toEqual([1, 2, 6]);
92 expect(b.first.driving).toBeNull();
93 let more = 0;
94 a.ws.on("message", () => more++);
95 await sleep(600); // pushed every 250 ms
96 expect(more).toBeGreaterThanOrEqual(1);
97 a.ws.close();
98 b.ws.close();
99 const refused = await new Promise<string>((resolve) => {
100 const ws = new WebSocket(`${base.replace("http", "ws")}/nope`);
101 ws.on("open", () => resolve("open"));
102 ws.on("error", () => resolve("refused"));
103 ws.on("close", () => resolve("refused"));
104 });
105 expect(refused).toBe("refused");
106 });
83107 });
84108
85109 describe("leader mapping of a whole frame", () => {
Software/so101/src/server.ts +46 −2
@@ -32,6 +32,8 @@
3232 * GET /app/<file> the modules the page imports itself (the leader arm over Web Serial), as JavaScript
3333 * GET /printables the sheets to print for the vision quest (board, cube markers, gripper marker); GET /printables/<file>
3434 * WS /follow client sends {"1": ticks, "2": ticks, ...} frames; the server drives the arm
35 * WS /watch read-only: the server sends the /status body every watchEveryMs, one bus read shared by every watcher;
36 * the hosted app's relay keeps one open, so viewers see the arm without each of them asking the Pi
3537 *
3638 * One thing drives the arm at a time. Every target is pulled inside the joint limits and every motion held to the speed and
3739 * acceleration limits (src/safety.ts); a stream that goes silent holds, one whose connection is lost parks. Every target, whether a move or a stream frame, is run through
@@ -43,6 +45,7 @@
4345 import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync, statSync, unlinkSync, writeFileSync } from "node:fs";
4446 import { createServer, type IncomingMessage, type ServerResponse } from "node:http";
4547 import { dirname, extname, join, resolve } from "node:path";
48 import type { Duplex } from "node:stream";
4649 import { createHash } from "node:crypto";
4750 import { fileURLToPath } from "node:url";
4851 import { WebSocketServer, type WebSocket } from "ws";
@@ -81,6 +84,8 @@ export interface ServerOptions {
8184 easeSpeed?: number;
8285 /** How often the stream's jam watch samples position and load, ms. */
8386 jamSampleMs?: number;
87 /** How often each /watch connection is sent the status, ms. */
88 watchEveryMs?: number;
8489 }
8590
8691 export interface RunningServer {
@@ -892,7 +897,7 @@ export async function startServer(bus: FeetechBus, options: ServerOptions = {}):
892897 // Frames are merged down to the command rate; each joint's target is pulled inside its bounds, judged by the zone,
893898 // and handed to the governor, which moves the goals toward it within the speed and acceleration limits.
894899 // Silence makes the arm hold (the dead-man); a connection that is lost rather than closed makes it park.
895 const wss = new WebSocketServer({ server: http, path: "/follow", maxPayload: 4096 });
900 const wss = new WebSocketServer({ noServer: true, maxPayload: 4096 });
896901 wss.on("connection", (ws: WebSocket) => {
897902 if (estop) {
898903 ws.close(ESTOP_CLOSE, stopped(estop));
@@ -1142,6 +1147,43 @@ export async function startServer(bus: FeetechBus, options: ServerOptions = {}):
11421147 });
11431148 });
11441149
1150 // ------------------------------------------------------------------ the watch
1151 // Read-only: the status, pushed. One timer reads the bus while anyone watches; a watcher's messages are ignored.
1152 const watchers = new WebSocketServer({ noServer: true, maxPayload: 1024 });
1153 const watchEveryMs = options.watchEveryMs ?? 250;
1154 let watching: ReturnType<typeof setInterval> | undefined;
1155 let reading = false;
1156 const broadcast = async () => {
1157 if (reading) return; // a slow bus read is not stacked behind another: the next tick asks again
1158 reading = true;
1159 try {
1160 const text = JSON.stringify(await status());
1161 for (const w of watchers.clients) if (w.readyState === w.OPEN) w.send(text);
1162 } catch (err) {
1163 console.error(`watch: ${err instanceof Error ? err.message : String(err)}`);
1164 } finally {
1165 reading = false;
1166 }
1167 };
1168 watchers.on("connection", (ws: WebSocket) => {
1169 watching ??= setInterval(() => void broadcast(), watchEveryMs);
1170 void broadcast();
1171 ws.on("error", () => ws.terminate());
1172 ws.on("close", () => {
1173 if (watchers.clients.size > 0 || !watching) return;
1174 clearInterval(watching);
1175 watching = undefined;
1176 });
1177 });
1178
1179 http.on("upgrade", (req: IncomingMessage, socket: Duplex, head: Buffer) => {
1180 const path = new URL(req.url ?? "/", "http://x").pathname;
1181 const to = path === "/follow" ? wss : path === "/watch" ? watchers : null;
1182 if (!to) return void socket.end("HTTP/1.1 404 Not Found\r\nconnection: close\r\ncontent-length: 0\r\n\r\n");
1183 socket.on("error", () => socket.destroy());
1184 to.handleUpgrade(req, socket, head, (ws) => to.emit("connection", ws, req));
1185 });
1186
11451187 await new Promise<void>((resolve) => http.listen(options.port ?? 8101, options.host ?? "0.0.0.0", resolve));
11461188 const address = http.address();
11471189 const port = typeof address === "object" && address ? address.port : options.port ?? 8101;
@@ -1149,8 +1191,10 @@ export async function startServer(bus: FeetechBus, options: ServerOptions = {}):
11491191 port,
11501192 close: () =>
11511193 new Promise<void>((resolve) => {
1152 for (const client of wss.clients) client.terminate();
1194 clearInterval(watching);
1195 for (const client of [...wss.clients, ...watchers.clients]) client.terminate();
11531196 wss.close();
1197 watchers.close();
11541198 http.close(() => resolve());
11551199 http.closeAllConnections(); // the viewer keeps idle keep-alive connections open; close() alone would wait for them
11561200 }),
Software/so101/src/viewer.html +22 −10
@@ -72,8 +72,8 @@
7272 #top button { background: rgba(0,0,0,.6); border-color: rgba(255,255,255,.18); color: #e8eaed; min-height: 36px; padding: 0 10px; border-radius: 8px; font-size: 13px; }
7373 #top .seg { display: inline-flex; flex-wrap: wrap; gap: 2px; } #top .seg button { max-width: 40vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } #top .seg button.on { background: #388bfd; border-color: #388bfd; color: #fff; }
7474 #top .info { display: inline-flex; gap: 6px; align-items: center; background: rgba(0,0,0,.6); padding: 0 10px; min-height: 36px; border-radius: 8px; max-width: 100%; }
75 #link { color: #9aa4ae; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: min(36ch, 46vw); } #link.on { color: #3fb950; } #link.warn { color: #d29922; }
76 .dot { width: 8px; height: 8px; border-radius: 50%; background: #f85149; display: inline-block; flex: none; } .dot.on { background: #3fb950; }
75 #link { color: #9aa4ae; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: min(36ch, 46vw); } #link.on { color: #3fb950; } #link.warn { color: #d29922; } #link.bad { color: #f85149; font-weight: 700; }
76 .dot { width: 8px; height: 8px; border-radius: 50%; background: #f85149; display: inline-block; flex: none; } .dot.on { background: #3fb950; } .dot.warn { background: #d29922; }
7777 #banner { position: absolute; top: 50px; left: 50%; transform: translateX(-50%); z-index: 5; width: max-content; max-width: min(640px, calc(100% - 16px)); padding: 8px 12px; border-radius: 8px; font-size: 13px; background: rgba(0,0,0,.8); border: 1px solid #30363d; display: none; }
7878 #banner.bad { display: block; border-color: #f85149; color: #ffb3ad; } #banner.warn { display: block; border-color: #d29922; color: #f2cc60; }
7979 #msg { position: absolute; left: calc(8px + var(--safe-l)); right: calc(8px + var(--safe-r)); bottom: 8px; z-index: 6; margin: 0 auto; width: max-content; max-width: calc(100% - 16px); padding: 8px 12px; border-radius: 8px; font-size: 13px; background: rgba(13,17,23,.9); color: #e8eaed; border: 1px solid #30363d; }
@@ -514,20 +514,27 @@
514514 // ---------------------------------------------------------------- which arm
515515 // Behind the hosted app's relay (/link answers), the arm is a live `so101 serve` or the simulation, and the cameras come through the relay.
516516 const relayed = await fetch("/link", { cache: "no-store" }).then((r) => r.ok, () => false);
517 let linkMode = null;
517 // A real arm that drops is "reconnecting": the same arm, its last pose kept, driving paused. Only after a minute down
518 // does the relay hand the page the simulation, and then it says OFFLINE in red.
519 let linkMode = null, linkNow = null;
520 const realArm = (mode) => mode === "live" || mode === "reconnecting";
518521 async function pollLink() {
519522 const l = await api("/link").catch(() => null);
520523 if (l?.ok) {
524 linkNow = l.mode;
521525 const el = $("link");
522526 el.hidden = false;
523 el.textContent = l.mode === "live" ? `Live arm · ${l.host}` : l.problem ? `Simulated · ${l.problem}` : "Simulated";
527 el.textContent = l.mode === "live" ? `Live arm · ${l.host}` : l.mode === "reconnecting" ? `Reconnecting to the arm… · ${l.host}`
528 : l.mode === "offline" ? `OFFLINE · simulated arm · ${l.problem}` : l.problem ? `Simulated · ${l.problem}` : "Simulated";
524529 el.title = el.textContent;
525 el.className = l.mode === "live" ? "on" : l.problem ? "warn" : "";
530 el.className = l.mode === "live" ? "on" : l.mode === "offline" ? "bad" : l.mode === "reconnecting" || l.problem ? "warn" : "";
526531 // Another arm has other limits, poses and joints: start the page over on it, once nothing on the page is driving.
527 if (linkMode !== null && l.mode !== linkMode && !stream.ws && !leader) return location.reload();
532 // So does a page that opened while reconnecting and never got the arm's limits.
533 const other = linkMode !== null && realArm(l.mode) !== realArm(linkMode);
534 if ((other || (l.mode === "live" && !limits?.ok)) && linkMode !== null && !stream.ws && !leader) return location.reload();
528535 if (linkMode === null) linkMode = l.mode;
529536 }
530 setTimeout(pollLink, 3000);
537 setTimeout(pollLink, l?.mode === "live" ? 3000 : 1000);
531538 }
532539
533540 // ---------------------------------------------------------------- cameras
@@ -1573,6 +1580,8 @@
15731580 if (isOwner()) b.append(el("button", { textContent: "Clear the stop", className: "danger", onclick: clearEStop }));
15741581 return;
15751582 }
1583 if (d.reconnecting) { b.className = "warn"; b.textContent = `Reconnecting to the arm… showing where it was at ${new Date(d.seenAt).toLocaleTimeString()}. Driving is paused, and the arm holds where it is.`; return; }
1584 if (typeof d.source === "string" && d.source.startsWith("simulated: offline")) { b.className = "bad"; b.textContent = "The arm has been unreachable for over a minute. This is the SIMULATION, not the real arm."; return; }
15761585 if (d.jammed?.length) { b.className = "bad"; b.textContent = `jam: ${d.jammed.map((id) => NAMES[id] ?? id).join(", ")} pushed against something and stopped — back the leader off`; }
15771586 else if (d.blocked) { b.className = d.zone === "enforce" ? "bad" : "warn"; b.textContent = `${d.zone === "enforce" ? "held at the boundary" : "warning"}: ${say(d.blocked)}`; }
15781587 else if (d.violation) { b.className = "warn"; b.textContent = `the arm is ${say(d.violation)} (zone: ${d.zone})`; }
@@ -1587,15 +1596,18 @@
15871596 rtts.push(performance.now() - started); if (rtts.length > 10) rtts.shift(); // the link's round trip, for the camera plan
15881597 status = d;
15891598 if (Boolean(d.estop) !== wasStopped) applyLock();
1590 $("dot").className = "dot on";
1591 $("state").textContent = d.driving === null ? "idle" : d.driving === "record" ? `recording ${d.recording}` : leaderStreaming() ? `leader · ${leader.frames} frames` : d.driving;
1599 $("dot").className = d.reconnecting ? "dot warn" : "dot on";
1600 $("state").textContent = d.reconnecting ? "reconnecting…" : d.driving === null ? "idle" : d.driving === "record" ? `recording ${d.recording}` : leaderStreaming() ? `leader · ${leader.frames} frames` : d.driving;
15921601 $("estop").classList.toggle("on", Boolean(d.estop)); $("estop").textContent = d.estop ? "Stopped" : "Stop";
15931602 $("record").textContent = d.driving === "record" ? "■ Stop recording" : "● Record";
15941603 for (const id of ["hold", "releaseBtn", "play"]) $(id).disabled = d.driving !== null;
15951604 if (tab === "controls") render(d);
15961605 banner(d); applyModel(d);
15971606 $("readout").innerHTML = `<span>lowest part <b>${Number.isFinite(d.lowestMm) ? d.lowestMm : "—"} mm</b> above the table</span><span>zone <b>${d.zone === "enforce" || d.zone === "warn" || d.zone === "off" ? d.zone : "—"}</b></span>`;
1598 } catch { $("dot").className = "dot"; $("state").textContent = "no arm"; }
1607 } catch {
1608 const reconnecting = linkNow === "reconnecting";
1609 $("dot").className = reconnecting ? "dot warn" : "dot"; $("state").textContent = reconnecting ? "reconnecting…" : "no arm";
1610 }
15991611 // Hidden, the page keeps the arm's state fresh at a walk: nobody is looking, and a phone's battery and data matter.
16001612 setTimeout(poll, document.hidden ? 2000 : 200);
16011613 }

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs