Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
Phone portrait: the video is pinned on top with its 16:9 box reserved, and one status strip sits under it (from main at 1142ee6 or later, forge.json untouched) #187
Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.
quill/arm:quill/176-phone-portrait-the-video-is-pinned-on-to → main 6 commits fork
11 files
changed +329
−126
Software/README.md +5 −1
| @@ -538,7 +538,11 @@ the app's own page. | ||
| 538 | 538 | **On a phone.** The page is laid out for one thumb, phone first. The view |
| 539 | 539 | (camera or 3D) sits on top at full width. Under it a status strip shows who |
| 540 | 540 | drives, your place in the line, the time left, the turn's one button, and |
| 541 | the red **Stop**. Stop is there whenever you drive, and for the owner. Below | |
| 541 | the red **Stop**. Stop is there whenever you drive, and for the owner. Held | |
| 542 | upright (up to 600 px wide), the view is pinned to the top in a 16:9 box kept | |
| 543 | free before the first frame arrives, so nothing jumps when the picture | |
| 544 | does. The strip under it is the one status line: the arm live or | |
| 545 | reconnecting moves there from over the picture (`src/ui/mobile.css`). Below | |
| 542 | 546 | that is a sheet with tabs at the bottom edge: **Chat**, **Queue**, |
| 543 | 547 | **Controls** (only on your turn, and it opens by itself when your turn |
| 544 | 548 | starts) and **Admin** (the owner: the room, the safe zone, the trims, |
Software/so101/src/server.test.ts +22 −1
| @@ -163,8 +163,9 @@ describe("viewer page", () => { | ||
| 163 | 163 | } |
| 164 | 164 | expect(bytes).toBeLessThan(51_200); |
| 165 | 165 | // three.js, the leader's mapping, the calibration panel and the Machine panel are imported where they are used, not up front. |
| 166 | for (const lazy of ["three", "/leader-map.js", "calibration.js", "reach.js", "machine-panel.js", "machine.js", "metrics-panel.js"]) expect([...upfront].some((p) => p === lazy || p.endsWith(`/${lazy}`))).toBe(false); | |
| 166 | for (const lazy of ["three", "/leader-map.js", "leader-calib.js", "calibration.js", "reach.js", "machine-panel.js", "machine.js", "metrics-panel.js"]) expect([...upfront].some((p) => p === lazy || p.endsWith(`/${lazy}`))).toBe(false); | |
| 167 | 167 | expect(script).toContain('import("/leader-map.js")'); |
| 168 | expect(script).toMatch(/import\("\/ui\/[0-9a-f]{12}\/leader-calib\.js"\)/); | |
| 168 | 169 | expect(script).toMatch(/import\("\/ui\/[0-9a-f]{12}\/calibration\.js"\)/); |
| 169 | 170 | expect(script).toMatch(/import\("\/ui\/[0-9a-f]{12}\/machine-panel\.js"\)/); |
| 170 | 171 | expect(script).toMatch(/import\("\/ui\/[0-9a-f]{12}\/metrics-panel\.js"\)/); |
| @@ -187,6 +188,26 @@ describe("viewer page", () => { | ||
| 187 | 188 | expect((await fetch(`${base}/ui/${version}/..%2Fserver.ts`)).status).toBe(404); |
| 188 | 189 | }); |
| 189 | 190 | |
| 191 | test("links the phone-portrait stylesheet and serves it as CSS: the video pinned in a reserved 16:9 box, one status strip", async () => { | |
| 192 | const page = await (await fetch(`${base}/`)).text(); | |
| 193 | const link = /<link rel="stylesheet" href="(\/ui\/[0-9a-f]{12}\/mobile\.css)" media="\(max-width: 600px\) and \(orientation: portrait\)">/.exec(page); | |
| 194 | expect(link).not.toBeNull(); | |
| 195 | // Its rules have the inline ones' specificity, so only a later place in the page lets them win. | |
| 196 | expect(link!.index).toBeGreaterThan(page.lastIndexOf("</style>")); | |
| 197 | const r = await fetch(`${base}${link![1]}`); | |
| 198 | expect(r.status).toBe(200); | |
| 199 | expect(r.headers.get("content-type")).toBe("text/css"); | |
| 200 | expect(r.headers.get("cache-control")).toContain("immutable"); | |
| 201 | const css = await r.text(); | |
| 202 | expect(css).toMatch(/#app \{[^}]*position: sticky; top: 0;[^}]*aspect-ratio: 16 \/ 9/); | |
| 203 | expect(css).toContain("env(safe-area-inset-top"); | |
| 204 | expect(css).toContain("html #shell.docked { grid-template-rows: auto auto"); // the tab bar keeps the reserved 16:9 view | |
| 205 | expect(css).toMatch(/#top \.info, #driverTag \{ display: none; \}/); // the overlay's status lines give way to the strip's | |
| 206 | expect(page).toContain("viewport-fit=cover"); | |
| 207 | expect(page).toMatch(/<section id="strip"[\s\S]*id="armLine"[\s\S]*<\/section>/); | |
| 208 | expect(page).toMatch(/#armLine \{ display: none;/); // the desktop keeps its overlay and never shows the strip's copy | |
| 209 | }); | |
| 210 | ||
| 190 | 211 | test("is laid out for a phone: tabs under the thumb, an E-stop beside the status, room for the forge pill", async () => { |
| 191 | 212 | const page = await (await fetch(`${base}/`)).text(); |
| 192 | 213 | for (const tab of ["chat", "queue", "controls", "admin"]) expect(page).toContain(`data-tab="${tab}"`); |
Software/so101/src/server.ts +1 −1
| @@ -653,7 +653,7 @@ export async function startServer(bus: FeetechBus, options: ServerOptions = {}): | ||
| 653 | 653 | // ------------------------------------------------------------------ routes |
| 654 | 654 | const viewerPath = join(here, "viewer.html"); |
| 655 | 655 | const uiDir = join(here, "ui"); |
| 656 | /** The page's own modules, under a name that changes with their content: cached for good, and a deploy is seen at once. */ | |
| 656 | /** The page's own modules and its phone stylesheet, under a name that changes with their content: cached for good, and a deploy is seen at once. */ | |
| 657 | 657 | const uiVersion = () => { |
| 658 | 658 | const hash = createHash("sha256"); |
| 659 | 659 | for (const f of readdirSync(uiDir).filter(isUiModule).sort()) hash.update(f).update(readFileSync(join(uiDir, f))); |
Software/so101/src/ui/leader-calib.js added +128
| @@ -0,0 +1,128 @@ | ||
| 1 | // @ts-check | |
| 2 | /** | |
| 3 | * Calibrating a leader arm on the page: once per driver, the leader in the follower's middle pose, then each joint at | |
| 4 | * each end the model shows; kept on the server under the driver's sign-in. Moved out of viewer.html and fetched the | |
| 5 | * first time someone calibrates, so a first visit never pays for it. The pure helpers are tested in ./leader-calib.test.ts. | |
| 6 | */ | |
| 7 | ||
| 8 | export const LABELS = { 1: "Base rotation", 2: "Shoulder", 3: "Elbow", 4: "Wrist bend", 5: "Wrist roll", 6: "Gripper trigger" }; | |
| 9 | export const LEADER_IDS = [1, 2, 3, 4, 5, 6]; | |
| 10 | /** @typedef {"centre" | "check" | { id: number, end: "low" | "high" }} Step */ | |
| 11 | /** @type {Step[]} */ | |
| 12 | export const STEPS = ["centre", ...LEADER_IDS.flatMap((id) => [{ id, end: /** @type {const} */ ("low") }, { id, end: /** @type {const} */ ("high") }]), "check"]; | |
| 13 | /** @param {number} id */ | |
| 14 | const label = (id) => /** @type {Record<number, string>} */ (LABELS)[id] ?? `joint ${id}`; | |
| 15 | ||
| 16 | /** | |
| 17 | * Follower ticks the model shows for a step: every joint in the middle, the one being taught at its end. | |
| 18 | * @param {Step | undefined} step @param {Record<string, { min: number, max: number }>} limits @returns {Record<string, number>} | |
| 19 | */ | |
| 20 | export const previewOf = (step, limits) => Object.fromEntries(Object.entries(limits).map(([k, l]) => { | |
| 21 | const id = Number(k); | |
| 22 | if (typeof step !== "object" || step.id !== id) return [k, (l.min + l.max) / 2]; | |
| 23 | return [k, step.end === "low" ? l.min + (id === 6 ? 0 : 30) : l.max - (id === 6 ? 0 : 30)]; | |
| 24 | })); | |
| 25 | ||
| 26 | /** | |
| 27 | * The calibration so far, in the shape the server keeps: only joints with both ends, the gripper's centre halfway. | |
| 28 | * @param {Record<number, { low?: number, centre?: number, high?: number }>} joints | |
| 29 | */ | |
| 30 | export const draftOf = (joints) => ({ joints: Object.fromEntries(LEADER_IDS.filter((id) => joints[id]?.high !== undefined).map((id) => { | |
| 31 | const j = /** @type {{ low: number, centre: number, high: number }} */ (joints[id]); | |
| 32 | return [id, id === 6 ? { low: j.low, high: j.high, centre: Math.round((j.low + j.high) / 2) } : { low: j.low, centre: j.centre, high: j.high }]; | |
| 33 | })) }); | |
| 34 | ||
| 35 | /** What the card says at step `i`. @param {number} i */ | |
| 36 | export function stepText(i) { | |
| 37 | const step = STEPS[i]; | |
| 38 | if (step === "centre") return "Nothing moves the real arm while you calibrate. Hold the leader in the pose the 3D model shows: every joint in the middle of its travel, the gripper half open. Then press Capture."; | |
| 39 | if (step === "check" || !step) return "Move the leader: the model follows through your calibration. If a joint turns the wrong way, flip it. Save keeps it with your name, for every computer you drive from."; | |
| 40 | return `${label(step.id)} (${i} of 12): move it to the ${step.id === 6 ? (step.end === "low" ? "closed" : "open") : "end"} the model shows, as far as it goes, and press Capture.`; | |
| 41 | } | |
| 42 | ||
| 43 | /** | |
| 44 | * Open the card. The page hands in what it owns; `onClose` runs once, after the card is gone. | |
| 45 | * Returns the open card: `reading(pose)` for each read of the leader, `close(note)` to end it. | |
| 46 | * @param {{ | |
| 47 | * $: (id: string) => any, el: (tag: string, props?: object, ...children: any[]) => any, | |
| 48 | * api: (path: string, body?: unknown, method?: string) => Promise<any>, msg: (text: string, bad?: boolean) => void, | |
| 49 | * leader: () => any, limits: () => Record<string, { min: number, max: number }>, leaderMap: any, | |
| 50 | * showOnModel: (ticks: Record<string, number>) => void, had: boolean, onClose: (note?: string) => void, | |
| 51 | * }} page | |
| 52 | */ | |
| 53 | export function openLeaderCalib(page) { | |
| 54 | const { $, el } = page; | |
| 55 | /** @type {{ step: number, joints: Record<number, { low?: number, centre?: number, high?: number }>, reading: Map<number, number>, error: string | null }} */ | |
| 56 | const c = { step: 0, joints: {}, reading: new Map(page.leader()?.pose ?? []), error: null }; | |
| 57 | let open = true; | |
| 58 | const draft = () => draftOf(c.joints); | |
| 59 | const short = (/** @type {number} */ id) => `${label(id).split(" ")[0]} ${c.reading.get(id) ?? "–"}`; | |
| 60 | ||
| 61 | function render() { | |
| 62 | const step = STEPS[c.step]; | |
| 63 | const t = typeof step === "object" ? step : null; | |
| 64 | $("lcErr").textContent = c.error ?? ""; | |
| 65 | $("lcBack").hidden = c.step < 1; | |
| 66 | $("lcFlips").hidden = step !== "check"; | |
| 67 | $("lcNext").textContent = step === "check" ? "Save" : "Capture"; | |
| 68 | $("lcStep").textContent = stepText(c.step); | |
| 69 | $("lcRead").replaceChildren(...LEADER_IDS.map((id) => el("span", { className: t?.id === id ? "on" : "", textContent: short(id) }))); | |
| 70 | if (step === "check") { | |
| 71 | $("lcFlips").replaceChildren(...LEADER_IDS.flatMap((id) => { | |
| 72 | const j = /** @type {{ low: number, centre: number, high: number }} */ (c.joints[id]); | |
| 73 | return [el("span", { textContent: label(id) }), el("small", { textContent: `${j.low} · ${id === 6 ? "–" : j.centre} · ${j.high}${j.low > j.high ? " (reversed)" : ""}` }), | |
| 74 | el("button", { textContent: "Flip", title: "swap this joint's ends", onclick: () => { [j.low, j.high] = [j.high, j.low]; render(); } })]; | |
| 75 | })); | |
| 76 | } | |
| 77 | if (step !== "check") page.showOnModel(previewOf(step, page.limits())); | |
| 78 | } | |
| 79 | /** @param {string} [note] */ | |
| 80 | function close(note) { | |
| 81 | if (!open) return; | |
| 82 | open = false; | |
| 83 | $("leaderDlg").close(); | |
| 84 | page.onClose(note); | |
| 85 | } | |
| 86 | async function next() { | |
| 87 | const step = STEPS[c.step]; | |
| 88 | c.error = null; | |
| 89 | if (step === "check") { | |
| 90 | const r = await page.api("/leader/calibration", draft(), "PUT"); | |
| 91 | if (!open) return; | |
| 92 | if (!r.ok) { c.error = r.error ?? "the calibration was not saved"; return render(); } | |
| 93 | const leader = page.leader(); | |
| 94 | if (leader) leader.calibration = draft(); | |
| 95 | return close("leader calibrated and saved; the leader now maps through it"); | |
| 96 | } | |
| 97 | const missing = LEADER_IDS.filter((id) => !c.reading.has(id)); | |
| 98 | if (missing.length) { c.error = `no reading from leader servo ${missing.join(", ")}: is every joint plugged in?`; return render(); } | |
| 99 | if (step === "centre") for (const id of LEADER_IDS) c.joints[id] = { centre: c.reading.get(id) }; | |
| 100 | else if (step) { | |
| 101 | const j = /** @type {{ low?: number, centre?: number, high?: number }} */ (c.joints[step.id]); | |
| 102 | j[step.end] = c.reading.get(step.id); | |
| 103 | if (step.end === "high") { | |
| 104 | const one = /** @type {{ low: number, centre: number, high: number }} */ (j); | |
| 105 | try { page.leaderMap.parseCalibration({ joints: { [step.id]: step.id === 6 ? { low: one.low, high: one.high, centre: Math.round((one.low + one.high) / 2) } : j } }); } | |
| 106 | catch (err) { delete j.low; delete j.high; c.step--; c.error = `${String(/** @type {any} */ (err)?.message).replace(/^joint \d+: /, "")}. Try ${label(step.id).toLowerCase()} again.`; return render(); } | |
| 107 | } | |
| 108 | } | |
| 109 | c.step++; | |
| 110 | render(); | |
| 111 | } | |
| 112 | $("lcNext").onclick = () => void next(); | |
| 113 | $("lcBack").onclick = () => { if (open && c.step > 0) { c.step--; c.error = null; render(); } }; | |
| 114 | $("lcCancel").onclick = () => close(page.had ? "calibration cancelled; your saved one still applies" : "calibration cancelled"); | |
| 115 | $("leaderDlg").oncancel = (/** @type {Event} */ e) => { e.preventDefault(); close("calibration cancelled"); }; | |
| 116 | $("leaderDlg").show(); | |
| 117 | render(); | |
| 118 | ||
| 119 | return { | |
| 120 | close, | |
| 121 | /** Each read of the connected leader while calibrating: the numbers in the card, and in the last step the model through the draft. @param {Iterable<[number, number]>} pose */ | |
| 122 | reading(pose) { | |
| 123 | for (const [id, t] of pose) c.reading.set(id, t); | |
| 124 | if (STEPS[c.step] === "check") page.showOnModel(page.leaderMap.mapLeaderFrame(c.reading, draft(), page.limits())); | |
| 125 | for (const [i, span] of [...$("lcRead").children].entries()) span.textContent = short(/** @type {number} */ (LEADER_IDS[i])); | |
| 126 | }, | |
| 127 | }; | |
| 128 | } |
Software/so101/src/ui/leader-calib.test.ts added +80
| @@ -0,0 +1,80 @@ | ||
| 1 | import { describe, expect, test } from "bun:test"; | |
| 2 | import * as leaderMap from "../leader-map.js"; | |
| 3 | import { draftOf, openLeaderCalib, previewOf, STEPS, stepText } from "./leader-calib.js"; | |
| 4 | ||
| 5 | const limits = { 1: { min: 1000, max: 3000 }, 6: { min: 2000, max: 2800 } }; | |
| 6 | ||
| 7 | /** Just enough of the page's DOM for the card: elements by id, children as plain objects. */ | |
| 8 | function page(over: Record<string, unknown> = {}) { | |
| 9 | const els: Record<string, any> = {}; | |
| 10 | const $ = (id: string) => (els[id] ??= { hidden: false, textContent: "", children: [] as any[], replaceChildren(...c: any[]) { this.children = c; }, show() { this.open = true; }, close() { this.open = false; } }); | |
| 11 | const el = (tag: string, props = {}) => ({ tag, ...props }); | |
| 12 | const calls: { path: string; body: any; method?: string }[] = [], closed: (string | undefined)[] = [], shown: Record<string, number>[] = []; | |
| 13 | const leader: any = { pose: [] }; | |
| 14 | const p = { | |
| 15 | $, el, msg: () => {}, leaderMap, had: false, leader: () => leader, limits: () => limits, | |
| 16 | api: async (path: string, body: any, method?: string) => { calls.push({ path, body, method }); return { ok: true }; }, | |
| 17 | showOnModel: (t: Record<string, number>) => shown.push(t), onClose: (note?: string) => closed.push(note), ...over, | |
| 18 | }; | |
| 19 | return { p, $, calls, closed, shown, leader }; | |
| 20 | } | |
| 21 | const tick = () => new Promise((r) => setTimeout(r, 0)); | |
| 22 | ||
| 23 | describe("calibrating a leader arm", () => { | |
| 24 | test("the model shows every joint in the middle, the one being taught at its end", () => { | |
| 25 | expect(previewOf("centre", limits)).toEqual({ 1: 2000, 6: 2400 }); | |
| 26 | expect(previewOf({ id: 1, end: "low" }, limits)).toEqual({ 1: 1030, 6: 2400 }); | |
| 27 | expect(previewOf({ id: 6, end: "high" }, limits)).toEqual({ 1: 2000, 6: 2800 }); | |
| 28 | }); | |
| 29 | ||
| 30 | test("the draft keeps only joints with both ends, the gripper's centre halfway", () => { | |
| 31 | expect(draftOf({ 1: { low: 10, centre: 20, high: 30 }, 2: { centre: 5 }, 6: { low: 100, high: 200 } })).toEqual({ joints: { 1: { low: 10, centre: 20, high: 30 }, 6: { low: 100, high: 200, centre: 150 } } }); | |
| 32 | }); | |
| 33 | ||
| 34 | test("the card says what to do at each of its 14 steps", () => { | |
| 35 | expect(STEPS).toHaveLength(14); | |
| 36 | expect(stepText(0)).toContain("Hold the leader in the pose the 3D model shows"); | |
| 37 | expect(stepText(1)).toBe("Base rotation (1 of 12): move it to the end the model shows, as far as it goes, and press Capture."); | |
| 38 | expect(stepText(12)).toContain("Gripper trigger (12 of 12): move it to the open the model shows"); | |
| 39 | expect(stepText(13)).toContain("Save keeps it with your name"); | |
| 40 | }); | |
| 41 | ||
| 42 | test("capture, every joint at both ends, then save: one PUT and the leader maps through it", async () => { | |
| 43 | const { p, $, calls, closed, leader } = page(); | |
| 44 | const card = openLeaderCalib(p); | |
| 45 | expect($("leaderDlg").open).toBe(true); | |
| 46 | const all = (t: number) => card.reading([1, 2, 3, 4, 5, 6].map((id) => [id, t] as [number, number])); | |
| 47 | all(2048); $("lcNext").onclick(); await tick(); | |
| 48 | for (let id = 1; id <= 6; id++) { | |
| 49 | card.reading([[id, 1000]]); $("lcNext").onclick(); await tick(); | |
| 50 | card.reading([[id, 3000]]); $("lcNext").onclick(); await tick(); | |
| 51 | } | |
| 52 | expect($("lcNext").textContent).toBe("Save"); | |
| 53 | expect($("lcFlips").hidden).toBe(false); | |
| 54 | $("lcNext").onclick(); await tick(); | |
| 55 | expect(calls).toHaveLength(1); | |
| 56 | expect(calls[0]!.method).toBe("PUT"); | |
| 57 | expect(calls[0]!.body.joints[1]).toEqual({ low: 1000, centre: 2048, high: 3000 }); | |
| 58 | expect(calls[0]!.body.joints[6]).toEqual({ low: 1000, high: 3000, centre: 2000 }); | |
| 59 | expect(leader.calibration).toEqual(calls[0]!.body); | |
| 60 | expect(closed).toEqual(["leader calibrated and saved; the leader now maps through it"]); | |
| 61 | expect($("leaderDlg").open).toBe(false); | |
| 62 | }); | |
| 63 | ||
| 64 | test("a servo that never answered is named, and nothing is captured", async () => { | |
| 65 | const { p, $ } = page(); | |
| 66 | const card = openLeaderCalib(p); | |
| 67 | card.reading([[1, 2000], [2, 2000]]); | |
| 68 | $("lcNext").onclick(); await tick(); | |
| 69 | expect($("lcErr").textContent).toBe("no reading from leader servo 3, 4, 5, 6: is every joint plugged in?"); | |
| 70 | expect($("lcBack").hidden).toBe(true); | |
| 71 | }); | |
| 72 | ||
| 73 | test("cancelling closes once, and says the saved calibration still applies when there is one", () => { | |
| 74 | const { p, $, closed } = page({ had: true }); | |
| 75 | const card = openLeaderCalib(p); | |
| 76 | $("lcCancel").onclick(); | |
| 77 | card.close(); | |
| 78 | expect(closed).toEqual(["calibration cancelled; your saved one still applies"]); | |
| 79 | }); | |
| 80 | }); |
Software/so101/src/ui/lobby-panel.js +33 −22
| @@ -27,17 +27,19 @@ export function queueEntry(el, x, mine) { | ||
| 27 | 27 | */ |
| 28 | 28 | export function createRoom(page) { |
| 29 | 29 | const { $, el, relayed, msg } = page; |
| 30 | const doc = /** @type {any} */ (globalThis).document, nav = /** @type {any} */ (globalThis).navigator; | |
| 30 | // the type check has no DOM (as ./machine-panel.js) | |
| 31 | const win = /** @type {any} */ (globalThis), doc = win.document, nav = win.navigator, location = win.location; | |
| 32 | /** @type {{ ws: WebSocket | null, hello: any, state: any, mutedList: any[], bannedList: any[], crew?: any, [k: string]: any }} */ | |
| 31 | 33 | const lobby = { ws: null, hello: null, state: null, offset: 0, retry: 500, mutedList: [], bannedList: [], admin: null, statusSeen: false }; |
| 32 | 34 | function mayDrive() { return !relayed || (lobby.hello !== null && (!lobby.hello.signin || (lobby.hello.you !== null && lobby.state?.driver?.id === lobby.hello.you.id))); } |
| 33 | 35 | function isOwner() { return !relayed || Boolean(lobby.hello?.you?.owner); } |
| 34 | const say = (m) => { if (lobby.ws?.readyState === 1) lobby.ws.send(JSON.stringify(m)); }; | |
| 36 | const say = (/** @type {Record<string, unknown>} */ m) => { if (lobby.ws?.readyState === 1) lobby.ws.send(JSON.stringify(m)); }; | |
| 35 | 37 | const serverNow = () => Date.now() + lobby.offset; |
| 36 | 38 | function tickClock() { |
| 37 | 39 | const d = lobby.state?.driver, c = $("clock"), me = lobby.hello?.you; |
| 38 | 40 | if (!relayed) return void (c.textContent = "On this machine: no sign-in, every visitor drives."); |
| 39 | 41 | if (!lobby.state || !lobby.hello?.signin) return void (c.textContent = lobby.hello && !lobby.hello.signin ? "Local: no sign-in, every visitor drives." : ""); |
| 40 | const mine = me ? lobby.state.queue.find((x) => x.id === me.id) : null; | |
| 42 | const mine = me ? lobby.state.queue.find((/** @type {any} */ x) => x.id === me.id) : null; | |
| 41 | 43 | const place = mine ? ` · you're ${ordinal(mine.place)} in line, ≤ ${Math.max(1, Math.ceil(mine.waitMs / 60000))} min` : ""; |
| 42 | 44 | if (!d) $("ring").hidden = true; |
| 43 | 45 | if (!d) { c.className = ""; c.textContent = lobby.state.paused ? `No new turns until the owner resumes${place || "; you can still join the line"}.` : "Free: take control to drive."; return; } |
| @@ -48,7 +50,7 @@ export function createRoom(page) { | ||
| 48 | 50 | if (d.readyBy && me && d.id === me.id) return void (c.textContent = `Tap Ready within ${Math.max(0, Math.ceil((d.readyBy - now) / 1000))} s or the turn passes on`); |
| 49 | 51 | c.textContent = idleSoon ? `Idle: the turn ends in ${Math.max(0, Math.ceil(idle / 1000))} s unless the arm moves` : `${minutes(d.endsAt - now)} left${place}`; |
| 50 | 52 | } |
| 51 | /** The ring is the driver's own: time left on the turn, in the seconds around it. */ | |
| 53 | /** The ring is the driver's own: time left on the turn, in the seconds around it. @param {any} d @param {any} me @param {number} now */ | |
| 52 | 54 | function tickRing(d, me, now) { |
| 53 | 55 | const ring = $("ring"), mine = Boolean(me && d.id === me.id), st = lobby.state; |
| 54 | 56 | ring.hidden = !mine; |
| @@ -59,21 +61,23 @@ export function createRoom(page) { | ||
| 59 | 61 | $("ringN").textContent = left >= 60000 ? `${Math.ceil(left / 60000)}m` : `${Math.ceil(left / 1000)}`; |
| 60 | 62 | } |
| 61 | 63 | setInterval(tickClock, 250); |
| 62 | $("quiet").onchange = (e) => { try { localStorage.setItem("so101.quiet", e.target.checked ? "1" : "0"); } catch {} }; | |
| 64 | $("quiet").onchange = (/** @type {any} */ e) => { try { localStorage.setItem("so101.quiet", e.target.checked ? "1" : "0"); } catch {} }; | |
| 63 | 65 | /** "You're next": a tone and a buzz (Android) and an amber strip; no web push, so only while the page is open. */ |
| 64 | 66 | let wasNext = false; |
| 65 | 67 | const quiet = () => { try { return localStorage.getItem("so101.quiet") === "1"; } catch { return false; } }; |
| 68 | /** @param {boolean} next */ | |
| 66 | 69 | function alertNext(next) { |
| 67 | 70 | $("strip").classList.toggle("next", next); |
| 68 | 71 | if (next && !wasNext) { |
| 69 | 72 | nav.vibrate?.([200, 100, 200]); |
| 70 | 73 | if (!quiet()) try { |
| 71 | const ctx = new (window.AudioContext || window.webkitAudioContext)(), o = ctx.createOscillator(), g = ctx.createGain(); | |
| 74 | const ctx = new (win.AudioContext || win.webkitAudioContext)(), o = ctx.createOscillator(), g = ctx.createGain(); | |
| 72 | 75 | o.frequency.value = 880; g.gain.value = 0.08; o.connect(g); g.connect(ctx.destination); o.start(); o.stop(ctx.currentTime + 0.25); o.onended = () => ctx.close(); |
| 73 | 76 | } catch { /* autoplay refused: the amber strip is the alert */ } |
| 74 | 77 | } |
| 75 | 78 | wasNext = next; |
| 76 | 79 | } |
| 80 | /** @param {string} textContent @param {string} className @param {() => void} onclick */ | |
| 77 | 81 | const button = (textContent, className, onclick) => el("button", { textContent, className, type: "button", onclick }); |
| 78 | 82 | /** The one thing to do about the turn, short enough for the strip. */ |
| 79 | 83 | function turnButtons() { |
| @@ -82,7 +86,7 @@ export function createRoom(page) { | ||
| 82 | 86 | if (!me) return [button("Join to drive", "go", () => { page.showTab("queue"); $("joinName").focus(); })]; |
| 83 | 87 | if (lobby.hello.banned) return []; // banned: watching only |
| 84 | 88 | if (d?.id === me.id) return [...(d.readyBy ? [button("Ready", "go ready", () => say({ type: "ready" }))] : []), ...(!st.queue.length && !d.readyBy ? [button("+30 s", "", () => say({ type: "extend" }))] : []), button("Hand back", "", () => say({ type: "leave" }))]; |
| 85 | if (st.queue.some((x) => x.id === me.id)) return [button("Leave", "", () => say({ type: "leave" }))]; | |
| 89 | if (st.queue.some((/** @type {any} */ x) => x.id === me.id)) return [button("Leave", "", () => say({ type: "leave" }))]; | |
| 86 | 90 | return [button(me.owner && d ? "Take over" : d || (st.paused && !me.owner) ? "Join queue" : "Take control", "go", () => say({ type: "join" }))]; |
| 87 | 91 | } |
| 88 | 92 | function renderTurn() { |
| @@ -90,7 +94,7 @@ export function createRoom(page) { | ||
| 90 | 94 | if (!st || !lobby.hello) return; |
| 91 | 95 | const d = st.driver; |
| 92 | 96 | $("driverLine").className = st.paused ? "paused" : d ? "" : "none"; |
| 93 | const mine = me ? st.queue.find((x) => x.id === me.id) : null, driving = Boolean(me && d?.id === me.id); | |
| 97 | const mine = me ? st.queue.find((/** @type {any} */ x) => x.id === me.id) : null, driving = Boolean(me && d?.id === me.id); | |
| 94 | 98 | // The strip in words: watching, queued, next, your turn. |
| 95 | 99 | const wait = Math.max(1, Math.ceil((st.queue.length ? st.queue.at(-1).waitMs + st.turnMs : d ? Math.max(0, d.endsAt - st.now) : 0) / 60000)); |
| 96 | 100 | $("driverLine").textContent = (st.paused ? "Paused for maintenance" + (d ? " · " : "") : "") + |
| @@ -110,7 +114,7 @@ export function createRoom(page) { | ||
| 110 | 114 | if (!joining) $("joinErr").textContent = ""; |
| 111 | 115 | $("forgeSignin").href = `/__forge/signin?to=${encodeURIComponent(location.pathname + location.search)}`; |
| 112 | 116 | $("waiting").textContent = st.queue.length ? String(st.queue.length) : ""; |
| 113 | $("queue").replaceChildren(...st.queue.map((x) => { | |
| 117 | $("queue").replaceChildren(...st.queue.map((/** @type {any} */ x) => { | |
| 114 | 118 | const mine = me && x.id === me.id; |
| 115 | 119 | const li = queueEntry(el, x, Boolean(mine)); |
| 116 | 120 | if (me?.owner) { const b = button("×", "x", () => say({ type: "end", id: x.id })); b.setAttribute("aria-label", `take ${x.name} out of the line`); li.append(b); } |
| @@ -131,7 +135,7 @@ export function createRoom(page) { | ||
| 131 | 135 | tickClock(); |
| 132 | 136 | page.applyLock(); |
| 133 | 137 | } |
| 134 | /** The owner's view of the crew moderator: on/off, the rules, lines it hid, people it suggests muting. */ | |
| 138 | /** The owner's view of the crew moderator: on/off, the rules, lines it hid, people it suggests muting. @param {any} me */ | |
| 135 | 139 | function renderCrew(me) { |
| 136 | 140 | const c = lobby.crew; |
| 137 | 141 | const box = $("crewLine"); |
| @@ -139,30 +143,31 @@ export function createRoom(page) { | ||
| 139 | 143 | if (box.contains(doc.activeElement) && doc.activeElement.matches("textarea, input")) return; // do not drop what is being typed |
| 140 | 144 | const perMinute = el("input", { type: "number", min: 1, max: 600, step: 1, value: c.settings.maxPerMinute, id: "crewMax", inputMode: "numeric" }); |
| 141 | 145 | const rules = el("textarea", { value: c.settings.rules, rows: 3, maxLength: 2000, ariaLabel: "Room rules for the crew moderator" }); |
| 142 | const item = (text, ...buttons) => el("div", { className: "item" }, el("span", { textContent: text }), ...buttons); | |
| 146 | const item = (/** @type {string} */ text, /** @type {any[]} */ ...buttons) => el("div", { className: "item" }, el("span", { textContent: text }), ...buttons); | |
| 143 | 147 | box.replaceChildren( |
| 144 | 148 | item(c.available ? (c.settings.enabled ? "On: every chat line is checked." : "Off.") : "No model reachable: nothing is checked.", |
| 145 | 149 | button(c.settings.enabled ? "Turn off" : "Turn on", "", () => say({ type: "crew", settings: { enabled: !c.settings.enabled } }))), |
| 146 | 150 | el("div", { className: "item" }, el("label", { htmlFor: "crewMax", textContent: "Most lines checked per minute" }), perMinute, |
| 147 | 151 | button("Save", "", () => say({ type: "crew", settings: { maxPerMinute: Number(perMinute.value) } }))), |
| 148 | 152 | rules, el("div", { className: "row" }, button("Save rules", "", () => say({ type: "crew", settings: { rules: rules.value } }))), |
| 149 | ...c.hidden.map((h) => item(`Hidden: ${h.message.from?.name}: ${h.message.text} (${h.reason})`, button("Restore", "", () => say({ type: "restore", id: h.message.id })))), | |
| 150 | ...c.suggestions.map((x) => item(`Suggests muting ${x.message.from?.name}: ${x.message.text} (${x.reason})`, button("Mute", "danger", () => say({ type: "mute", id: x.message.from.id, on: true })), button("Dismiss", "", () => say({ type: "dismiss", id: x.message.id }))))); | |
| 153 | ...c.hidden.map((/** @type {any} */ h) => item(`Hidden: ${h.message.from?.name}: ${h.message.text} (${h.reason})`, button("Restore", "", () => say({ type: "restore", id: h.message.id })))), | |
| 154 | ...c.suggestions.map((/** @type {any} */ x) => item(`Suggests muting ${x.message.from?.name}: ${x.message.text} (${x.reason})`, button("Mute", "danger", () => say({ type: "mute", id: x.message.from.id, on: true })), button("Dismiss", "", () => say({ type: "dismiss", id: x.message.id }))))); | |
| 151 | 155 | } |
| 152 | 156 | /** Join: one field, one button. The answer sets the session cookie; the room is reopened to hear who you are now. */ |
| 153 | $("joinForm").onsubmit = async (e) => { | |
| 157 | $("joinForm").onsubmit = async (/** @type {any} */ e) => { | |
| 154 | 158 | e.preventDefault(); |
| 155 | 159 | const button = $("joinForm").querySelector("button"), err = $("joinErr"); |
| 156 | 160 | button.disabled = true; |
| 157 | 161 | try { |
| 158 | 162 | const r = await fetch("/join", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ name: $("joinName").value.trim() }) }); |
| 159 | const body = await r.json().catch(() => ({})); | |
| 163 | const body = /** @type {any} */ (await r.json().catch(() => ({}))); | |
| 160 | 164 | err.textContent = r.ok || (r.status === 409 && body.you) ? "" : body.error ?? `could not join (${r.status})`; |
| 161 | 165 | if (!err.textContent) { $("joinName").value = ""; lobby.ws?.close(); } |
| 162 | 166 | } catch { err.textContent = "could not reach the app; try again"; } |
| 163 | 167 | button.disabled = false; |
| 164 | 168 | }; |
| 165 | 169 | const chatBox = $("chat"); |
| 170 | /** @param {any} m */ | |
| 166 | 171 | function chatLine(m) { |
| 167 | 172 | const me = lobby.hello?.you; |
| 168 | 173 | const line = el("div", { className: m.from ? `line${me && m.from.id === me.id ? " me" : ""}` : "line sys", title: new Date(m.at).toLocaleTimeString() }); |
| @@ -180,13 +185,16 @@ export function createRoom(page) { | ||
| 180 | 185 | earlier.disabled = true; |
| 181 | 186 | say({ type: "history", before: Number(first.dataset.id) }); |
| 182 | 187 | }; |
| 188 | /** @param {boolean} more */ | |
| 183 | 189 | function showEarlier(more) { earlier.disabled = false; if (more) chatBox.prepend(earlier); else earlier.remove(); } |
| 190 | /** @param {any[]} lines @param {boolean} more */ | |
| 184 | 191 | function addOlder(lines, more) { |
| 185 | 192 | const below = chatBox.scrollHeight - chatBox.scrollTop; // the reader stays on the line they were reading |
| 186 | 193 | chatBox.prepend(...lines.map(chatLine)); |
| 187 | 194 | chatBox.scrollTop = chatBox.scrollHeight - below; |
| 188 | 195 | showEarlier(more); |
| 189 | 196 | } |
| 197 | /** @param {any[]} lines */ | |
| 190 | 198 | function addChat(lines, replace = false) { |
| 191 | 199 | const atEnd = chatBox.scrollHeight - chatBox.scrollTop - chatBox.clientHeight < 40; |
| 192 | 200 | if (replace) chatBox.replaceChildren(); |
| @@ -197,8 +205,11 @@ export function createRoom(page) { | ||
| 197 | 205 | } |
| 198 | 206 | // Moderation on a phone: long-press a line (or right-click it, or Enter on it) for delete and mute. |
| 199 | 207 | { |
| 200 | let press = null, held = null; | |
| 201 | const open = (line) => { | |
| 208 | /** @type {any} */ | |
| 209 | let press = null; | |
| 210 | /** @type {any} */ | |
| 211 | let held = null; | |
| 212 | const open = (/** @type {any} */ line) => { | |
| 202 | 213 | if (!lobby.hello?.you?.owner || !line?.dataset.from) return; |
| 203 | 214 | held = line; line.classList.add("held"); |
| 204 | 215 | $("modTitle").textContent = `${line.dataset.name} said`; |
| @@ -209,15 +220,15 @@ export function createRoom(page) { | ||
| 209 | 220 | $("modDlg").showModal(); |
| 210 | 221 | nav.vibrate?.(10); |
| 211 | 222 | }; |
| 212 | chatBox.addEventListener("pointerdown", (e) => { | |
| 223 | chatBox.addEventListener("pointerdown", (/** @type {any} */ e) => { | |
| 213 | 224 | const line = e.target.closest(".line"); if (!line?.dataset.from || !lobby.hello?.you?.owner) return; |
| 214 | 225 | press = { x: e.clientX, y: e.clientY, timer: setTimeout(() => { press = null; open(line); }, 480) }; |
| 215 | 226 | }); |
| 216 | 227 | const cancel = () => { if (press) { clearTimeout(press.timer); press = null; } }; |
| 217 | chatBox.addEventListener("pointermove", (e) => { if (press && Math.hypot(e.clientX - press.x, e.clientY - press.y) > 10) cancel(); }); | |
| 228 | chatBox.addEventListener("pointermove", (/** @type {any} */ e) => { if (press && Math.hypot(e.clientX - press.x, e.clientY - press.y) > 10) cancel(); }); | |
| 218 | 229 | for (const ev of ["pointerup", "pointercancel", "scroll"]) chatBox.addEventListener(ev, cancel); |
| 219 | chatBox.addEventListener("contextmenu", (e) => { const line = e.target.closest(".line"); if (line?.dataset.from && lobby.hello?.you?.owner) { e.preventDefault(); cancel(); open(line); } }); | |
| 220 | chatBox.addEventListener("keydown", (e) => { if (e.key === "Enter" && e.target.classList?.contains("line")) open(e.target); }); | |
| 230 | chatBox.addEventListener("contextmenu", (/** @type {any} */ e) => { const line = e.target.closest(".line"); if (line?.dataset.from && lobby.hello?.you?.owner) { e.preventDefault(); cancel(); open(line); } }); | |
| 231 | chatBox.addEventListener("keydown", (/** @type {any} */ e) => { if (e.key === "Enter" && e.target.classList?.contains("line")) open(e.target); }); | |
| 221 | 232 | $("modDlg").addEventListener("close", () => { |
| 222 | 233 | const line = held; held = null; line?.classList.remove("held"); |
| 223 | 234 | if (!line) return; |
| @@ -257,7 +268,7 @@ export function createRoom(page) { | ||
| 257 | 268 | lobby.retry = Math.min(lobby.retry * 2, 8000); |
| 258 | 269 | }; |
| 259 | 270 | } |
| 260 | $("say").onsubmit = (e) => { | |
| 271 | $("say").onsubmit = (/** @type {any} */ e) => { | |
| 261 | 272 | e.preventDefault(); |
| 262 | 273 | const text = $("sayText").value.trim(); |
| 263 | 274 | if (!text) return; |
Software/so101/src/ui/mobile.css added +18
| @@ -0,0 +1,18 @@ | ||
| 1 | /* A phone held upright: the video pinned on top, full-bleed, its 16:9 box reserved before the first frame arrives so | |
| 2 | nothing under it moves; directly under it one status strip (who drives and the time left, your place in line, the | |
| 3 | arm live or reconnecting); the sheet of tabs below. viewer.html links this only for this media query, so the | |
| 4 | desktop and landscape layouts never read it. */ | |
| 5 | @media (max-width: 600px) and (orientation: portrait) { | |
| 6 | #shell { grid-template-rows: auto auto minmax(0, 1fr); } | |
| 7 | /* With the tab bar (ui/tabs.css, linked earlier): the same 16:9 view on top; its per-panel rules still hide or fill it. */ | |
| 8 | html #shell.docked { grid-template-rows: auto auto minmax(0, 1fr) calc(56px + var()); } | |
| 9 | /* The notch: a black band the height of the top inset, outside the 16:9 box (content-box keeps the ratio on the picture). */ | |
| 10 | #app { position: sticky; top: 0; box-sizing: content-box; aspect-ratio: 16 / 9; border-top: env(safe-area-inset-top, 0px) solid #000; } | |
| 11 | /* The keyboard is up: the view gives way to the chat, as it does on every layout. */ | |
| 12 | #shell.typing { grid-template-rows: minmax(0, var()) auto minmax(0, 1fr); } | |
| 13 | #shell.typing #app { aspect-ratio: auto; } | |
| 14 | /* One status line: the arm's state moves from over the video into the strip, and the driver's name over the video goes, | |
| 15 | since the strip already says who drives. */ | |
| 16 | #top .info, #driverTag { display: none; } | |
| 17 | #armLine { display: flex; } | |
| 18 | } |
Software/so101/src/ui/tabs.js +14 −7
| @@ -1,30 +1,36 @@ | ||
| 1 | // @ts-check | |
| 1 | 2 | // The phone's bottom tab bar: which tabs a viewer has and which one a hash names. STOP is in no tab. |
| 3 | /** @type {Record<string, string>} */ | |
| 2 | 4 | export const SHEET_TAB = { drive: "controls", queue: "queue", chat: "chat", mod: "admin", machine: "machine" }; |
| 3 | 5 | |
| 4 | 6 | /** Queue and Chat need a lobby; Mod is for admins; Machine is for everyone, as in the sheet. */ |
| 5 | 7 | export const tabsFor = ({ mod = false, lobby = true } = {}) => |
| 6 | ["drive", ...(lobby ? ["queue", "chat"] : []), ...(mod ? ["mod"] : []), "machine"].map((id) => ({ id, label: id[0].toUpperCase() + id.slice(1) })); | |
| 8 | ["drive", ...(lobby ? ["queue", "chat"] : []), ...(mod ? ["mod"] : []), "machine"].map((id) => ({ id, label: id.charAt(0).toUpperCase() + id.slice(1) })); | |
| 7 | 9 | |
| 8 | /** The tab the hash names, or Drive. */ | |
| 10 | /** The tab the hash names, or Drive. @param {unknown} hash @param {{ id: string }[]} tabs */ | |
| 9 | 11 | export const tabFromHash = (hash, tabs = tabsFor()) => { |
| 10 | 12 | const name = String(hash ?? "").replace(/^#/, "").toLowerCase(); |
| 11 | 13 | return tabs.some((t) => t.id === name) ? name : "drive"; |
| 12 | 14 | }; |
| 13 | 15 | |
| 16 | /** @param {string} id */ | |
| 14 | 17 | export const hashFor = (id) => `#${id}`; |
| 15 | 18 | |
| 16 | /** The tab an arrow key moves to from the current one (wrapping), or null for any other key. */ | |
| 19 | /** The tab an arrow key moves to from the current one (wrapping), or null for any other key. @param {string[]} ids @param {string} current @param {string} key @returns {string | null} */ | |
| 17 | 20 | export const arrow = (ids, current, key) => { |
| 18 | const step = { ArrowRight: 1, ArrowLeft: -1 }[key]; | |
| 19 | return step && ids.length ? ids[(ids.indexOf(current) + step + ids.length) % ids.length] : null; | |
| 21 | const step = /** @type {Record<string, number>} */ ({ ArrowRight: 1, ArrowLeft: -1 })[key]; | |
| 22 | return step && ids.length ? ids[(ids.indexOf(current) + step + ids.length) % ids.length] ?? null : null; | |
| 20 | 23 | }; |
| 21 | 24 | |
| 22 | 25 | /** |
| 23 | 26 | * The bar on the page: draws the tabs into `dock` as roving-tabindex buttons, each controlling its sheet panel (which it then labels), |
| 24 | 27 | * and shows the panel the hash names through `showTab`. `who()` says { mod, lobby }. |
| 28 | * @param {any} dock @param {any} shell @param {(tab: string | undefined) => void} showTab @param {() => { mod?: boolean, lobby?: boolean }} who | |
| 25 | 29 | */ |
| 26 | 30 | export function mountDock(dock, shell, showTab, who) { |
| 31 | const win = /** @type {any} */ (globalThis), document = win.document, history = win.history, location = win.location; | |
| 27 | 32 | const me = { panel: "drive", sync, go }; |
| 33 | /** @param {string} name */ | |
| 28 | 34 | function go(name, write = false) { |
| 29 | 35 | me.panel = tabFromHash(hashFor(name), tabsFor(who())); |
| 30 | 36 | shell.dataset.panel = me.panel; |
| @@ -32,6 +38,7 @@ export function mountDock(dock, shell, showTab, who) { | ||
| 32 | 38 | showTab(SHEET_TAB[me.panel]); |
| 33 | 39 | for (const b of dock.children) { const on = b.dataset.panel === me.panel; b.setAttribute("aria-selected", String(on)); b.tabIndex = on ? 0 : -1; } |
| 34 | 40 | } |
| 41 | /** @param {boolean} [force] */ | |
| 35 | 42 | function sync(force) { |
| 36 | 43 | const tabs = tabsFor(who()), ids = tabs.map((t) => t.id).join(); |
| 37 | 44 | if (!force && dock.dataset.ids === ids) return; |
| @@ -46,8 +53,8 @@ export function mountDock(dock, shell, showTab, who) { | ||
| 46 | 53 | shell.classList.add("docked"); |
| 47 | 54 | go(location.hash); |
| 48 | 55 | } |
| 49 | dock.addEventListener("keydown", (e) => { | |
| 50 | const n = arrow([...dock.children].map((b) => b.dataset.panel), me.panel, e.key); | |
| 56 | dock.addEventListener("keydown", (/** @type {any} */ e) => { | |
| 57 | const n = arrow([...dock.children].map((/** @type {any} */ b) => b.dataset.panel), me.panel, e.key); | |
| 51 | 58 | if (n) { go(n, true); document.getElementById(`d-${n}`).focus(); } |
| 52 | 59 | }); |
| 53 | 60 | return me; |
Software/so101/src/ui/video.js +3 −3
| @@ -40,7 +40,7 @@ export function openFeed(options) { | ||
| 40 | 40 | if (w) { w.onmessage = w.onclose = w.onerror = null; try { w.close(); } catch { /* already gone */ } } |
| 41 | 41 | }; |
| 42 | 42 | /** Replace the socket, after `ms`. */ |
| 43 | const again = (ms) => { | |
| 43 | const again = (/** @type {number} */ ms) => { | |
| 44 | 44 | stop(); |
| 45 | 45 | if (!closed) watchdog = after(connect, ms); |
| 46 | 46 | }; |
| @@ -50,7 +50,7 @@ export function openFeed(options) { | ||
| 50 | 50 | let w; |
| 51 | 51 | try { w = open(url); } catch { return again(RETRY_MS); } |
| 52 | 52 | ws = w; |
| 53 | w.binaryType = "blob"; | |
| 53 | w.binaryType = /** @type {any} */ ("blob"); // Bun's types know only arraybuffer and nodebuffer; browsers default to blob anyway | |
| 54 | 54 | w.onmessage = (e) => { |
| 55 | 55 | if (ws !== w || !(e.data instanceof Blob)) return; |
| 56 | 56 | cancel(watchdog); watchdog = after(() => again(0), QUIET_MS); |
| @@ -81,7 +81,7 @@ export function drawFrame(cam, frame, env) { | ||
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | /** Whether a socket-drawn camera is live: a frame lately, and the relay not saying it is offline. @param {{ lastFrame?: number, online?: boolean | null }} cam */ |
| 84 | export const isLive = (cam, now) => now - (cam.lastFrame ?? -Infinity) < QUIET_MS + 500 && cam.online !== false; | |
| 84 | export const isLive = (cam, /** @type {number} */ now) => now - (cam.lastFrame ?? -Infinity) < QUIET_MS + 500 && cam.online !== false; | |
| 85 | 85 | |
| 86 | 86 | /** |
| 87 | 87 | * Start a camera's socket, unless the page moved on while this module was loading: `epoch` is the camera's count when |
Software/so101/src/ui/video.test.ts +1 −1
| @@ -113,7 +113,7 @@ describe("drawing frames in the page", () => { | ||
| 113 | 113 | const { e } = env(); |
| 114 | 114 | const cam: any = { id: 2, epoch: 4, feed: null, img: { src: "" } }; |
| 115 | 115 | const sockets: any[] = []; |
| 116 | (globalThis as any).WebSocket = class { constructor(public url: string) { sockets.push(this); } close() {} }; | |
| 116 | (globalThis as any).WebSocket = class { url: string; constructor(url: string) { this.url = url; sockets.push(this); } close() {} }; | |
| 117 | 117 | attach(cam, 4, e); |
| 118 | 118 | expect(sockets[0].url).toBe("wss://h/camera/2/ws"); |
| 119 | 119 | sockets[0].onmessage({ data: new Blob(["x"]) }); |
Software/so101/src/viewer.html +24 −90
| @@ -90,6 +90,7 @@ | ||
| 90 | 90 | #driverLine { font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } #driverLine.none { color: var(); font-weight: 500; } |
| 91 | 91 | #clock { color: var(); font-size: 13px; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } #clock.idle { color: var(); } |
| 92 | 92 | #capLine { color: var(); font-size: 12px; } #capLine:empty { display: none; } |
| 93 | #armLine { display: none; gap: 6px; align-items: center; color: var(); font-size: 12px; white-space: nowrap; } #armText { min-width: 0; overflow: hidden; text-overflow: ellipsis; } /* shown on a phone held upright (ui/mobile.css) */ | |
| 93 | 94 | #driverLine.paused { color: var(); } |
| 94 | 95 | #turnActions { display: flex; gap: 6px; } #turnActions:empty { display: none; } |
| 95 | 96 | .estop { flex: none; min-width: 72px; min-height: 56px; border: 0; border-radius: 12px; background: #d1242f; color: #fff; font-weight: 800; letter-spacing: .08em; text-transform: uppercase; box-shadow: 0 0 0 3px rgba(209,36,47,.25), 0 2px 8px rgba(0,0,0,.35); } |
| @@ -296,6 +297,7 @@ | ||
| 296 | 297 | @media (max-width: 420px) { #top .label { display: none; } #link { max-width: 30vw; } } |
| 297 | 298 | @media (prefers-reduced-motion: reduce) { *, *::before, *::after { transition: none ; animation: none ; scroll-behavior: auto ; } } |
| 298 | 299 | </style> |
| 300 | <link rel="stylesheet" href="/ui/__VERSION__/mobile.css" media="(max-width: 600px) and (orientation: portrait)"> | |
| 299 | 301 | </head> |
| 300 | 302 | <body> |
| 301 | 303 | <div id="shell"> |
| @@ -323,7 +325,7 @@ | ||
| 323 | 325 | |
| 324 | 326 | <section id="strip" aria-label="Who drives" aria-live="polite"> |
| 325 | 327 | <div id="ring" hidden aria-hidden="true"><span id="ringN"></span></div> |
| 326 | <div class="who"><div id="driverLine" class="none">Connecting…</div><div id="clock" aria-live="off"></div><div id="capLine"></div></div> | |
| 328 | <div class="who"><div id="driverLine" class="none">Connecting…</div><div id="clock" aria-live="off"></div><div id="capLine"></div><div id="armLine" aria-live="off"><span class="dot" id="armDot"></span><span id="armText"></span></div></div> | |
| 327 | 329 | <div id="turnActions"></div> |
| 328 | 330 | <button id="sheetToggle" hidden aria-controls="sheet" aria-expanded="true">Hide</button> |
| 329 | 331 | <button id="stop" class="estop" data-stop title="Stop (space or Esc): halt everything, hold with torque, until someone resumes" aria-label="Stop the arm">Stop</button> |
| @@ -1100,7 +1102,7 @@ | ||
| 1100 | 1102 | l.arm.follow((pose) => { |
| 1101 | 1103 | l.pose = pose; |
| 1102 | 1104 | l.targets = l.arm.targets(pose, limits, l.calibration); |
| 1103 | if (calib) return calibReading(pose); | |
| 1105 | if (calib) return calib.reading(pose); | |
| 1104 | 1106 | teleop.pose(l.targets); |
| 1105 | 1107 | if (frame++ % 4 === 0) renderTracks(); // the leader is read at 60 Hz; the tracks need no more than 15 |
| 1106 | 1108 | renderLeader(); |
| @@ -1109,7 +1111,7 @@ | ||
| 1109 | 1111 | async function disconnectLeader(reason) { |
| 1110 | 1112 | const l = leader; if (!l) return; |
| 1111 | 1113 | teleop?.setLeader(false, reason ?? "the leader was disconnected"); // first: the stream closes before anything else, and the arm holds |
| 1112 | closeCalib(); | |
| 1114 | calib?.close(); | |
| 1113 | 1115 | leader = null; |
| 1114 | 1116 | l.stop.abort(); |
| 1115 | 1117 | await l.arm.close().catch(() => {}); |
| @@ -1129,103 +1131,27 @@ | ||
| 1129 | 1131 | } |
| 1130 | 1132 | teleop.set(true); |
| 1131 | 1133 | } |
| 1132 | // ---------------------------------------------------------------- leader calibration (src/leader-map.js) | |
| 1133 | // Once per driver: the leader in the follower's middle pose, then each joint at each end the model shows; kept on the server under the driver's sign-in. | |
| 1134 | const LABELS = { 1: "Base rotation", 2: "Shoulder", 3: "Elbow", 4: "Wrist bend", 5: "Wrist roll", 6: "Gripper trigger" }; | |
| 1135 | const LEADER_IDS = [1, 2, 3, 4, 5, 6]; | |
| 1136 | const STEPS = ["centre", ...LEADER_IDS.flatMap((id) => [{ id, end: "low" }, { id, end: "high" }]), "check"]; | |
| 1137 | /** Follower ticks the model shows for a step: every joint in the middle, the one being taught at its end. */ | |
| 1138 | const previewOf = (step) => Object.fromEntries(Object.keys(limits).map((k) => { | |
| 1139 | const id = Number(k); | |
| 1140 | if (typeof step !== "object" || step.id !== id) return [id, id === 6 ? limits[id].min + (limits[id].max - limits[id].min) / 2 : mid(id)]; | |
| 1141 | return [id, step.end === "low" ? limits[id].min + (id === 6 ? 0 : 30) : limits[id].max - (id === 6 ? 0 : 30)]; | |
| 1142 | })); | |
| 1134 | // ---------------------------------------------------------------- leader calibration (src/ui/leader-calib.js, src/leader-map.js) | |
| 1135 | // Both fetched the first time someone calibrates a leader: nobody else needs them. | |
| 1143 | 1136 | const showOnModel = (ticks) => { if (viewer.robot) for (const [id, t] of Object.entries(ticks)) if (NAMES[id] && limits[id]) viewer.setJointValue(NAMES[id], toRad(Number(id), t)); }; |
| 1144 | const draft = () => ({ joints: Object.fromEntries(LEADER_IDS.filter((id) => calib.joints[id]?.high !== undefined).map((id) => { | |
| 1145 | const j = calib.joints[id]; | |
| 1146 | return [id, id === 6 ? { low: j.low, high: j.high, centre: Math.round((j.low + j.high) / 2) } : { low: j.low, centre: j.centre, high: j.high }]; | |
| 1147 | })) }); | |
| 1148 | function renderCalib() { | |
| 1149 | const step = STEPS[calib.step]; | |
| 1150 | const t = typeof step === "object" ? step : null; | |
| 1151 | $("lcErr").textContent = calib.error ?? ""; | |
| 1152 | $("lcBack").hidden = calib.step < 1; | |
| 1153 | $("lcFlips").hidden = step !== "check"; | |
| 1154 | $("lcNext").textContent = step === "check" ? "Save" : "Capture"; | |
| 1155 | $("lcStep").textContent = | |
| 1156 | step === "centre" ? "Nothing moves the real arm while you calibrate. Hold the leader in the pose the 3D model shows: every joint in the middle of its travel, the gripper half open. Then press Capture." | |
| 1157 | : step === "check" ? "Move the leader: the model follows through your calibration. If a joint turns the wrong way, flip it. Save keeps it with your name, for every computer you drive from." | |
| 1158 | : `${LABELS[t.id]} (${calib.step} of 12): move it to the ${t.id === 6 ? (t.end === "low" ? "closed" : "open") : "end"} the model shows, as far as it goes, and press Capture.`; | |
| 1159 | $("lcRead").replaceChildren(...LEADER_IDS.map((id) => el("span", { className: t?.id === id ? "on" : "", textContent: `${LABELS[id].split(" ")[0]} ${calib.reading.get(id) ?? "–"}` }))); | |
| 1160 | if (step === "check") { | |
| 1161 | $("lcFlips").replaceChildren(...LEADER_IDS.flatMap((id) => { | |
| 1162 | const j = calib.joints[id]; | |
| 1163 | return [el("span", { textContent: LABELS[id] }), el("small", { textContent: `${j.low} · ${id === 6 ? "–" : j.centre} · ${j.high}${j.low > j.high ? " (reversed)" : ""}` }), | |
| 1164 | el("button", { textContent: "Flip", title: "swap this joint's ends", onclick: () => { [j.low, j.high] = [j.high, j.low]; renderCalib(); } })]; | |
| 1165 | })); | |
| 1166 | } | |
| 1167 | if (step !== "check") showOnModel(previewOf(step)); | |
| 1168 | } | |
| 1169 | /** src/leader-map.js, fetched the first time someone calibrates a leader: nobody else needs it. */ | |
| 1170 | let leaderMap = null; | |
| 1171 | 1137 | async function openCalib() { |
| 1172 | 1138 | if (!leader) return; |
| 1173 | try { leaderMap ??= await import("/leader-map.js"); } catch { return msg("the calibration did not load; try again", true); } | |
| 1139 | let leaderMap, card; | |
| 1140 | try { [leaderMap, card] = await Promise.all([import("/leader-map.js"), import("/ui/__VERSION__/leader-calib.js")]); } catch { return msg("the calibration did not load; try again", true); } | |
| 1174 | 1141 | if (!Object.keys(limits).length) return msg("the arm's limits have not loaded yet", true); |
| 1175 | 1142 | const mine = await api("/leader/calibration").catch(() => null); |
| 1176 | 1143 | if (mine?.status === 401) return msg("join to calibrate your leader: the calibration is kept with your name", true); |
| 1177 | if (!leader) return; | |
| 1144 | if (!leader || calib) return; | |
| 1178 | 1145 | stopLeaderDrive(); |
| 1179 | 1146 | if (!(await load3D())) return msg("the 3D model is needed to show the poses to copy, and it did not load", true); |
| 1180 | 1147 | if (!$("pane3").classList.contains("on")) pick("pane3"); |
| 1181 | calib = { step: 0, joints: {}, reading: new Map(leader.pose ?? []), error: null, had: Boolean(mine?.joints) }; | |
| 1182 | $("leaderDlg").show(); | |
| 1183 | renderCalib(); | |
| 1148 | calib = card.openLeaderCalib({ $, el, api, msg, leaderMap, showOnModel, leader: () => leader, limits: () => limits, had: Boolean(mine?.joints), onClose: (note) => { | |
| 1149 | calib = null; | |
| 1150 | if (status) applyModel(status); | |
| 1151 | renderLeader(); | |
| 1152 | if (note) msg(note); | |
| 1153 | } }); | |
| 1184 | 1154 | } |
| 1185 | function closeCalib(note) { | |
| 1186 | if (!calib) return; | |
| 1187 | calib = null; | |
| 1188 | $("leaderDlg").close(); | |
| 1189 | if (status) applyModel(status); | |
| 1190 | renderLeader(); | |
| 1191 | if (note) msg(note); | |
| 1192 | } | |
| 1193 | /** Each read of the connected leader while calibrating: the numbers in the card, and in the last step the model through the draft. */ | |
| 1194 | function calibReading(pose) { | |
| 1195 | const c = calib; | |
| 1196 | for (const [id, t] of pose) c.reading.set(id, t); | |
| 1197 | if (STEPS[c.step] === "check") showOnModel(leaderMap.mapLeaderFrame(c.reading, draft(), limits)); | |
| 1198 | for (const [i, span] of [...$("lcRead").children].entries()) span.textContent = `${LABELS[LEADER_IDS[i]].split(" ")[0]} ${c.reading.get(LEADER_IDS[i]) ?? "–"}`; | |
| 1199 | } | |
| 1200 | async function nextCalib() { | |
| 1201 | const c = calib; if (!c) return; | |
| 1202 | const step = STEPS[c.step]; | |
| 1203 | c.error = null; | |
| 1204 | if (step === "check") { | |
| 1205 | const r = await api("/leader/calibration", draft(), "PUT"); | |
| 1206 | if (!r.ok) { c.error = r.error ?? "the calibration was not saved"; return renderCalib(); } | |
| 1207 | if (leader) leader.calibration = draft(); | |
| 1208 | return closeCalib("leader calibrated and saved; the leader now maps through it"); | |
| 1209 | } | |
| 1210 | const missing = LEADER_IDS.filter((id) => !c.reading.has(id)); | |
| 1211 | if (missing.length) { c.error = `no reading from leader servo ${missing.join(", ")}: is every joint plugged in?`; return renderCalib(); } | |
| 1212 | if (step === "centre") for (const id of LEADER_IDS) c.joints[id] = { centre: c.reading.get(id) }; | |
| 1213 | else { | |
| 1214 | const j = c.joints[step.id]; | |
| 1215 | j[step.end] = c.reading.get(step.id); | |
| 1216 | if (step.end === "high") { | |
| 1217 | try { leaderMap.parseCalibration({ joints: { [step.id]: step.id === 6 ? { low: j.low, high: j.high, centre: Math.round((j.low + j.high) / 2) } : j } }); } | |
| 1218 | catch (err) { delete j.low; delete j.high; c.step--; c.error = `${err.message.replace(/^joint \d+: /, "")}. Try ${LABELS[step.id].toLowerCase()} again.`; return renderCalib(); } | |
| 1219 | } | |
| 1220 | } | |
| 1221 | c.step++; | |
| 1222 | renderCalib(); | |
| 1223 | } | |
| 1224 | $("lcNext").onclick = () => void nextCalib(); | |
| 1225 | $("lcBack").onclick = () => { if (calib && calib.step > 0) { calib.step--; calib.error = null; renderCalib(); } }; | |
| 1226 | $("lcCancel").onclick = () => void closeCalib(calib?.had ? "calibration cancelled; your saved one still applies" : "calibration cancelled"); | |
| 1227 | $("leaderDlg").addEventListener("cancel", (e) => { e.preventDefault(); void closeCalib("calibration cancelled"); }); | |
| 1228 | ||
| 1229 | 1155 | $("leaderCal").onclick = () => (calib ? undefined : void openCalib()); |
| 1230 | 1156 | |
| 1231 | 1157 | function stopLeaderDrive(note) { |
| @@ -1776,6 +1702,14 @@ | ||
| 1776 | 1702 | $("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>`; |
| 1777 | 1703 | } |
| 1778 | 1704 | } |
| 1705 | /** On a phone held upright the strip says the arm's state, not the video (ui/mobile.css): the words the overlay holds, mirrored as they change. */ | |
| 1706 | const info = document.querySelector("#top .info"); | |
| 1707 | const mirrorArm = () => { | |
| 1708 | $("armDot").className = $("dot").className; | |
| 1709 | $("armText").textContent = [$("link").hidden ? "" : $("link").textContent, $("state").textContent, $("live").textContent && `camera ${$("live").textContent}`, $("lag").hidden ? "" : $("lag").textContent.replace(/^· /, "")].filter(Boolean).join(" · "); | |
| 1710 | }; | |
| 1711 | new MutationObserver(mirrorArm).observe(info, { subtree: true, childList: true, characterData: true, attributes: true, attributeFilter: ["class", "hidden"] }); | |
| 1712 | mirrorArm(); | |
| 1779 | 1713 | function noStatus() { |
| 1780 | 1714 | const reconnecting = linkNow === "reconnecting"; |
| 1781 | 1715 | $("dot").className = reconnecting ? "dot warn" : "dot"; $("state").textContent = reconnecting ? "reconnecting…" : "no arm"; |
new above
offline