Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
Phone portrait shows every joint slider at once in a collapsible tray under the model, in place of the one-joint lever #346
Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.
quill/arm:quill/276-phone-portrait-shows-every-joint-slider → main 1 commit fork
8 files
changed +191
−785
Software/README.md +14 −25
| @@ -611,34 +611,23 @@ that is a sheet with tabs at the bottom edge: **Chat**, **Queue**, | ||
| 611 | 611 | starts) and **Admin** (the owner: the room, the safe zone, the trims, |
| 612 | 612 | metrics and 3D calibration). |
| 613 | 613 | |
| 614 | - Controls, phone upright: a row of chips (J1 base, J2 shoulder, J3 elbow, | |
| 615 | J4 wrist, J5 roll, Grip) picks one joint. One tall lever drives it: push | |
| 616 | up or down, and the further you push the faster it goes. Let go and it | |
| 617 | stops. ▲/▼ beside the lever nudge one step per tap and jog while held. | |
| 618 | Under them come the poses, Hold, Record and the rest. This layout is for | |
| 619 | screens up to 640 px wide. Wider screens, a tablet | |
| 620 | held upright included, get the sliders. From a keyboard, arrows on the chips pick | |
| 621 | the joint and ↑/↓ on the lever step it. | |
| 614 | - Controls, phone upright: every joint and the gripper at once, in a tray | |
| 615 | under the 3D model. Each is one compact row: its name, its slider, and | |
| 616 | the value. The rail shows where you asked the joint to go (a blue pill) | |
| 617 | and where the arm really is (a thin line). Tap the tray's **Joints** | |
| 618 | line to fold it, and the poses, Hold, Record and the rest below it, to | |
| 619 | that one line so the model fills the screen. Tap it again to open | |
| 620 | it. The page remembers which you chose (`src/ui/drive.js`). **Stop** | |
| 621 | stays in the strip either way. This layout is for screens up to 600 px | |
| 622 | wide. Wider screens, a tablet held upright included, get the full rows | |
| 623 | with hold-to-jog −/+. | |
| 622 | 624 | - Controls, phone on its side: the joint sliders and ±-buttons drive every |
| 623 | 625 | joint, the gripper included; **Hide** tucks the sheet away to show the |
| 624 | 626 | picture, and **Show** brings it back. |
| 625 | - How touch driving works (`src/ui/drive.js`): a finger's deflection is a | |
| 626 | speed, with a dead band in the middle, never a position. Each finger is | |
| 627 | tracked by its own pointer, so two thumbs drive two sets of joints | |
| 628 | independently. The instant a finger lifts, its joints' targets are sent | |
| 629 | as they stand, without waiting for the next frame (`src/ui/frames.js` | |
| 630 | sends whatever is still pending with it). The arm's governor | |
| 631 | settles on them. The same happens on pointercancel, on a lost capture, | |
| 632 | when the window loses focus, when the page is hidden, and when driving | |
| 633 | stops being allowed: your turn ends, the arm is stopped, or it is | |
| 634 | reconnecting (`canDrive` in `src/ui/drive.js`). Frames go out | |
| 635 | at 25 Hz. A target never runs more than 0.6 s ahead of where the arm | |
| 636 | last said it was, so a slow link or a speed cap never queues up motion. | |
| 637 | The server's limits clamp every frame as before. Android buzzes on a | |
| 638 | press and at the end of a joint's range. Every press also shows on | |
| 639 | screen. The screen stays awake during your turn (Screen Wake Lock), and | |
| 640 | the status line shows the round trip to the arm once it passes 250 ms. | |
| 641 | On a phone the strip carries one stop button, the E-stop. | |
| 627 | - While a slider is held, its frames go out through `src/ui/frames.js`, | |
| 628 | and the server's limits clamp every one. The screen stays awake during | |
| 629 | your turn (Screen Wake Lock), and the status line shows the round trip | |
| 630 | to the arm once it passes 250 ms. | |
| 642 | 631 | - Controls, wider screens: the joint sliders with hold-to-jog −/+, the poses, |
| 643 | 632 | and the rest. The server checks every frame against the zone. |
| 644 | 633 | - Chat: the page follows the visual viewport. The keyboard shrinks the |
Software/so101/src/portrait-layout.test.ts +64
| @@ -188,4 +188,68 @@ describe("a phone held upright", () => { | ||
| 188 | 188 | .filter((s) => targets.some((t) => t.test(s.split(/[\s>+~]+/).pop()!))); |
| 189 | 189 | expect(offenders).toEqual([]); |
| 190 | 190 | }); |
| 191 | ||
| 192 | describe("390x844, your turn: the joint tray under the model (#276)", () => { | |
| 193 | const size = SIZES[0]!; | |
| 194 | const tap = 44; // --tap: one compact row, and the tray's one line | |
| 195 | const sheetFor = (body: number) => { | |
| 196 | const pad = tracks(decl(all, ".tab", "padding")!).map((t) => px(t, size.w, {})); | |
| 197 | return Math.min(body + pad[0]! + pad[2]!, (parseFloat(decl(mobile, 'html #shell.docked[data-panel="drive"].mayDrive #sheet', "max-height")!) * size.h) / 100); | |
| 198 | }; | |
| 199 | const boxes = (sheet: number) => { | |
| 200 | const areas = ["view", "strip", "model", "sheet", "dock"]; | |
| 201 | const rows = decl(mobile, 'html #shell.docked[data-panel="drive"].mayDrive', "grid-template-rows")!; | |
| 202 | return layout(rows, areas, size, { strip: STRIP, sheet }, { "--cam": decl(mobile, "#shell", "--cam")! }).box; | |
| 203 | }; | |
| 204 | ||
| 205 | test("the one-joint lever is gone; every joint and the gripper is a row of the tray, each with a slider and both marks", () => { | |
| 206 | expect(html).not.toMatch(/id="(touch|lever|chips|nudgeUp|nudgeDown)"/); | |
| 207 | expect(html).toMatch(/<section id="tray"[^>]*>\s*<button type="button" id="trayToggle" aria-controls="joints" aria-expanded="true">[\s\S]*?<\/button>\s*<div id="joints"><\/div>\s*<\/section>/); | |
| 208 | expect(html).toContain('bindTray($("trayToggle"), $("tray")'); | |
| 209 | // render() builds one row per joint the arm reports, the gripper among them: name, rail with its slider, value, and the marks. | |
| 210 | const render = /function render\(d\) \{[\s\S]*?\n \}\n/.exec(html)?.[0] ?? ""; | |
| 211 | expect(render).toContain("for (const j of d.joints)"); | |
| 212 | expect(render).toMatch(/<span class="rail"><input type="range"/); | |
| 213 | expect(render).toContain('<span class="pos">'); | |
| 214 | expect(render).toContain("marks(el, want, j.position)"); | |
| 215 | expect(render).toContain("joints.append(div)"); | |
| 216 | }); | |
| 217 | ||
| 218 | test("no phone-portrait rule hides the rows, their sliders or STOP; only a shut tray folds the rows", () => { | |
| 219 | const targets = [/#joints\b/, /^\.j$/, /\.rail\b/, /input\[type=range\]/, /#tray\b/, /#stop\b/, /#strip\b/]; | |
| 220 | const offenders = all | |
| 221 | .filter((r) => (r.media === "" ? false : portrait(r.media)) && hides(r.body)) | |
| 222 | .flatMap((r) => r.selector.split(",").map((s) => s.trim())) | |
| 223 | .filter((s) => !s.startsWith("#tray.shut ")) | |
| 224 | .filter((s) => targets.some((t) => t.test(s.split(/[\s>+~]+/).pop()!))); | |
| 225 | expect(offenders).toEqual([]); | |
| 226 | }); | |
| 227 | ||
| 228 | test("open: all six rows at once, one compact line each, fit the tray with the model still above them", () => { | |
| 229 | expect(decl(mobile, "#trayToggle", "display")).toBe("flex"); | |
| 230 | expect(decl(mobile, "#joints", "grid-template-columns")).toBe("100%"); | |
| 231 | expect(decl(mobile, ".j", "grid-template-areas")).toBe('"name slide val"'); | |
| 232 | expect(decl(mobile, ".j", "min-height")).toBe("var(--tap)"); | |
| 233 | expect(decl(mobile, ".j .jog", "display")).toBe("none"); | |
| 234 | const gap = px(decl(all, "#tray", "gap")!, size.w, {}); | |
| 235 | const body = tap + gap + 6 * tap; | |
| 236 | const sheet = sheetFor(body); | |
| 237 | expect(sheet).toBe(body + 28); // nothing clipped: every slider shows without scrolling | |
| 238 | const box = boxes(sheet); | |
| 239 | expect(box.sheet!.top).toBeCloseTo(box.model!.bottom, 3); // under the model | |
| 240 | expect(box.model!.height).toBeGreaterThan(120); | |
| 241 | expect(box.strip!.height).toBe(STRIP); // STOP's row | |
| 242 | }); | |
| 243 | ||
| 244 | test("shut: the tray is one line, the controls under it fold too, and the model takes the room; STOP stays", () => { | |
| 245 | expect(decl(mobile, "#tray.shut #joints", "display")).toBe("none"); | |
| 246 | expect(decl(mobile, "#tray.shut ~ *", "display")).toBe("none"); | |
| 247 | const open = boxes(sheetFor(tap + px(decl(all, "#tray", "gap")!, size.w, {}) + 6 * tap)); | |
| 248 | const shut = boxes(sheetFor(tap)); | |
| 249 | expect(shut.sheet!.height).toBe(tap + 28); | |
| 250 | expect(shut.model!.height).toBeGreaterThan(open.model!.height + 5 * tap); | |
| 251 | expect(shut.strip!.height).toBe(STRIP); | |
| 252 | expect(shut.dock!.bottom).toBeCloseTo(size.h, 3); | |
| 253 | }); | |
| 254 | }); | |
| 191 | 255 | }); |
Software/so101/src/server.test.ts +4 −4
| @@ -218,7 +218,7 @@ describe("viewer page", () => { | ||
| 218 | 218 | expect(page).toMatch(/#tabs \{[^}]*var\(--pill\)/); |
| 219 | 219 | }); |
| 220 | 220 | |
| 221 | test("serves the touch-driving modules, and the phone layout keeps the browser's gestures off the controls", async () => { | |
| 221 | test("serves the driving modules, and the phone layout keeps the browser's gestures off the controls", async () => { | |
| 222 | 222 | const page = await (await fetch(`${base}/`)).text(); |
| 223 | 223 | for (const name of ["drive", "frames"]) { |
| 224 | 224 | const path = new RegExp(`/ui/[0-9a-f]{12}/${name}\\.js`).exec(page)?.[0]; |
| @@ -227,12 +227,12 @@ describe("viewer page", () => { | ||
| 227 | 227 | expect(r.status).toBe(200); |
| 228 | 228 | expect(r.headers.get("cache-control")).toContain("immutable"); |
| 229 | 229 | } |
| 230 | // The one-joint lever shows only on a phone held upright, so its rules live in the phone's stylesheet. | |
| 230 | // The one-joint lever, its chips and its nudges are gone: phone portrait has every joint's slider in the tray (#276). | |
| 231 | 231 | const tabsCss = await (await fetch(`${base}${/\/ui\/[0-9a-f]{12}\/tabs\.css/.exec(page)![0]}`)).text(); |
| 232 | expect(tabsCss).toMatch(/#lever \{[^}]*touch-action: none/); | |
| 232 | expect(page).not.toMatch(/id="(touch|chips|one|lever|leverKnob|nudgeUp|nudgeDown)"/); | |
| 233 | expect(tabsCss).not.toMatch(/#(lever|chips|one)\b/); | |
| 233 | 234 | // The thumb sticks and the reach joystick are gone: no element, rule or script for them. |
| 234 | 235 | expect(page).not.toMatch(/id="(sticks|stickL|stickR|reach|pad|swap|grips)"|reach\.js|reachStep|\.stick\b/); |
| 235 | expect(tabsCss).toMatch(/#chips button small \{[^}]*white-space: nowrap/); // a long label never wraps and shifts the row | |
| 236 | 236 | // One STOP in every layout: one stop control in the markup, and no layout hides it. Resume is its own control. |
| 237 | 237 | expect(page.match(/<button[^>]*data-stop/g)?.length).toBe(1); |
| 238 | 238 | expect(page).not.toMatch(/#stop \{[^}]*display: none/); |
Software/so101/src/ui/drive.js +29 −341
| @@ -1,356 +1,44 @@ | ||
| 1 | 1 | /** |
| 2 | * Touch driving: a finger's deflection is a joint's velocity, never its position. Plain ESM, served to the page at | |
| 3 | * /ui/ and imported by the tests. The wire stays what /follow always took, a target in ticks per joint: each held | |
| 4 | * joint's target runs ahead at the deflection's share of the jog speed, and the moment the last finger on it lifts | |
| 5 | * the target stops where it is and goes out at once. That frame is the zero: the arm's governor settles on it. | |
| 6 | * The server's limits still clamp every frame; this only keeps the page from asking for more. | |
| 2 | * The joint tray: on a phone held upright, every joint's slider (the gripper's too) sits in one tray under the model, | |
| 3 | * and the tray folds to one line so the model can have the room. Plain ESM, served to the page at /ui/ and imported by | |
| 4 | * the tests. The look is ui/mobile.css's, keyed on .shut; this keeps the state, the button's aria-expanded, and the | |
| 5 | * person's choice across visits. Outside phone portrait the button is hidden and the rows always show. | |
| 7 | 6 | */ |
| 8 | 7 | |
| 9 | /** The tick direction that raises the gripper, swings the base right, rolls the wrist or opens the jaw (from the URDF). */ | |
| 10 | export const UP = /** @type {Record<number, number>} */ ({ 1: 1, 2: -1, 3: -1, 4: -1, 5: 1, 6: 1 }); | |
| 8 | /** Where the choice is kept. */ | |
| 9 | export const KEY = "so101.tray"; | |
| 11 | 10 | |
| 12 | 11 | /** |
| 13 | * A raw deflection (-1..1) through a dead band and a gentle curve: nothing near the middle, fine control | |
| 14 | * a little way out, full speed at the edge. | |
| 15 | * @param {number} v | |
| 16 | * @param {number} [dead] | |
| 12 | * Whether a stored value means open: anything but "shut", so a first visit (nothing stored) opens the tray. | |
| 13 | * @param {unknown} stored | |
| 17 | 14 | */ |
| 18 | export function shape(v, dead = 0.12) { | |
| 19 | if (!Number.isFinite(v)) return 0; | |
| 20 | const a = Math.min(1, Math.abs(v)); | |
| 21 | if (a <= dead) return 0; | |
| 22 | return Math.sign(v) * ((a - dead) / (1 - dead)) ** 1.5; | |
| 23 | } | |
| 24 | ||
| 25 | /** @typedef {Record<number, { min: number, max: number }>} Limits */ | |
| 26 | /** @typedef {Record<number, number>} Frame */ | |
| 27 | ||
| 28 | /** | |
| 29 | * Whether a finger may drive now: it is this person's turn, the arm is not stopped and not reconnecting, and its | |
| 30 | * limits are known. The page asks before a press starts and on every status poll, letting go of whatever is held | |
| 31 | * the moment the answer turns false. | |
| 32 | * @param {{ mayDrive: boolean, estop?: unknown, held?: unknown, reconnecting?: unknown, limits?: Limits | null }} state | |
| 33 | */ | |
| 34 | export function canDrive({ mayDrive, estop, held, reconnecting, limits }) { | |
| 35 | return Boolean(mayDrive) && !estop && !held && !reconnecting && Object.keys(limits ?? {}).length > 0; | |
| 15 | export function opened(stored) { | |
| 16 | return stored !== "shut"; | |
| 36 | 17 | } |
| 37 | 18 | |
| 38 | 19 | /** |
| 39 | * The velocity integrator. Each holder (a pointer, a key) holds some joints at some deflections; a joint's velocity is | |
| 40 | * the sum of its holders', capped at 1. `step` advances the held joints and returns their targets; `release` drops a | |
| 41 | * holder and returns the targets of the joints it was moving, which is what stops them. | |
| 42 | */ | |
| 43 | export class Drive { | |
| 44 | /** @param {{ margin?: number, lead?: number }} [options] */ | |
| 45 | constructor(options = {}) { | |
| 46 | /** Ticks kept clear of each end of a joint's range, as the page's sliders always did. */ | |
| 47 | this.margin = options.margin ?? 30; | |
| 48 | /** How far a target may run ahead of where the arm last said it was: a slow arm (a speed cap) never builds up a backlog. */ | |
| 49 | this.lead = options.lead ?? 0.6; | |
| 50 | /** @type {Map<string, Map<number, number>>} holder → joint → deflection */ | |
| 51 | this.holders = new Map(); | |
| 52 | /** @type {Map<number, number>} the targets of joints being driven, in fractional ticks */ | |
| 53 | this.targets = new Map(); | |
| 54 | /** @type {Map<number, number>} where the arm last said each joint was */ | |
| 55 | this.present = new Map(); | |
| 56 | } | |
| 57 | ||
| 58 | /** Where the arm is. A joint nobody holds starts from here when it is next held. @param {Iterable<readonly [number, number]>} positions */ | |
| 59 | seed(positions) { | |
| 60 | for (const [id, p] of positions) if (Number.isFinite(p)) this.present.set(id, p); | |
| 61 | } | |
| 62 | ||
| 63 | /** Whether any joint is held off the middle. */ | |
| 64 | get moving() { | |
| 65 | for (const axes of this.holders.values()) for (const v of axes.values()) if (v !== 0) return true; | |
| 66 | return false; | |
| 67 | } | |
| 68 | ||
| 69 | /** The joints held, by anyone. */ | |
| 70 | held() { | |
| 71 | const out = new Set(); | |
| 72 | for (const axes of this.holders.values()) for (const id of axes.keys()) out.add(id); | |
| 73 | return out; | |
| 74 | } | |
| 75 | ||
| 76 | /** | |
| 77 | * A holder's joints and deflections, replacing what it held before; a joint it lets go of this way stops like a release. | |
| 78 | * @param {string} holder | |
| 79 | * @param {Record<number, number>} axes joint id → deflection, -1..1, already shaped | |
| 80 | * @returns {Frame | null} targets for joints that stopped, to send at once | |
| 81 | */ | |
| 82 | hold(holder, axes) { | |
| 83 | const before = this.holders.get(holder); | |
| 84 | const next = new Map(); | |
| 85 | for (const [k, v] of Object.entries(axes)) { | |
| 86 | const id = Number(k); | |
| 87 | if (!Number.isInteger(id) || !Number.isFinite(v)) continue; | |
| 88 | if (!this.targets.has(id)) { | |
| 89 | const p = this.present.get(id); | |
| 90 | if (p === undefined) continue; // no position yet: nothing to run ahead of | |
| 91 | this.targets.set(id, p); | |
| 92 | } | |
| 93 | next.set(id, Math.max(-1, Math.min(1, v))); | |
| 94 | } | |
| 95 | this.holders.set(holder, next); | |
| 96 | const dropped = before ? [...before.keys()].filter((id) => !next.has(id)) : []; | |
| 97 | return this.(dropped); | |
| 98 | } | |
| 99 | ||
| 100 | /** @param {string} holder @returns {Frame | null} the zero: the targets of the joints this holder moved, now still */ | |
| 101 | release(holder) { | |
| 102 | const axes = this.holders.get(holder); | |
| 103 | if (!axes) return null; | |
| 104 | this.holders.delete(holder); | |
| 105 | return this.([...axes.keys()]); | |
| 106 | } | |
| 107 | ||
| 108 | /** Every holder at once: the window lost focus, the page was hidden. @returns {Frame | null} */ | |
| 109 | releaseAll() { | |
| 110 | const ids = [...this.held()]; | |
| 111 | this.holders.clear(); | |
| 112 | return this.(ids); | |
| 113 | } | |
| 114 | ||
| 115 | /** | |
| 116 | * One small step of one joint, for a tap on − or +. | |
| 117 | * @param {number} id @param {number} ticks @param {Limits} limits | |
| 118 | * @returns {Frame | null} | |
| 119 | */ | |
| 120 | nudge(id, ticks, limits) { | |
| 121 | const from = this.targets.get(id) ?? this.present.get(id); | |
| 122 | if (from === undefined || !limits[id]) return null; | |
| 123 | const t = this.(id, from + ticks, limits); | |
| 124 | if (!this.held().has(id)) this.targets.delete(id); else this.targets.set(id, t); | |
| 125 | this.present.set(id, t); // the next nudge goes on from here, not from a stale reading | |
| 126 | return { [id]: Math.round(t) }; | |
| 127 | } | |
| 128 | ||
| 129 | /** | |
| 130 | * Advance every held joint by `dt` seconds at up to `speed` ticks/s. | |
| 131 | * @param {number} dt @param {number} speed @param {Limits} limits | |
| 132 | * @returns {{ frame: Frame | null, stops: number[] }} the targets to send, and joints that just met an end of their range | |
| 133 | */ | |
| 134 | step(dt, speed, limits) { | |
| 135 | /** @type {Map<number, number>} */ | |
| 136 | const v = new Map(); | |
| 137 | for (const axes of this.holders.values()) for (const [id, d] of axes) v.set(id, (v.get(id) ?? 0) + d); | |
| 138 | /** @type {Frame} */ | |
| 139 | const frame = {}; | |
| 140 | const stops = []; | |
| 141 | let any = false; | |
| 142 | for (const [id, sum] of v) { | |
| 143 | const t0 = this.targets.get(id); | |
| 144 | if (t0 === undefined || !limits[id]) continue; | |
| 145 | const vel = Math.max(-1, Math.min(1, sum)); | |
| 146 | if (vel === 0) continue; | |
| 147 | const want = t0 + vel * speed * dt; | |
| 148 | let t = this.(id, want, limits); | |
| 149 | const ended = t !== want; | |
| 150 | // Never more than `lead` seconds of travel ahead of the arm: a slow arm (a speed cap) is waited for, not queued | |
| 151 | // up behind. Only further travel is held back; a target never steps back toward a stale reading. | |
| 152 | const p = this.present.get(id); | |
| 153 | if (p !== undefined) { | |
| 154 | const room = Math.max(speed * this.lead, 20); | |
| 155 | t = vel > 0 ? Math.min(t, Math.max(t0, p + room)) : Math.max(t, Math.min(t0, p - room)); | |
| 156 | } | |
| 157 | if (t === t0) continue; // nowhere to go: nothing to send | |
| 158 | if (ended) stops.push(id); | |
| 159 | this.targets.set(id, t); | |
| 160 | frame[id] = Math.round(t); | |
| 161 | any = true; | |
| 162 | } | |
| 163 | return { frame: any ? frame : null, stops }; | |
| 164 | } | |
| 165 | ||
| 166 | /** @param {number} id @param {number} t @param {Limits} limits */ | |
| 167 | (id, t, limits) { | |
| 168 | const l = limits[id]; | |
| 169 | if (!l) return t; | |
| 170 | return Math.max(l.min + this.margin, Math.min(l.max - this.margin, t)); | |
| 171 | } | |
| 172 | ||
| 173 | /** @param {number[]} ids */ | |
| 174 | (ids) { | |
| 175 | const still = this.held(); | |
| 176 | /** @type {Frame} */ | |
| 177 | const frame = {}; | |
| 178 | let any = false; | |
| 179 | for (const id of ids) { | |
| 180 | if (still.has(id)) continue; // another finger still drives it | |
| 181 | const t = this.targets.get(id); | |
| 182 | this.targets.delete(id); | |
| 183 | if (t === undefined) continue; | |
| 184 | frame[id] = Math.round(t); | |
| 185 | this.present.set(id, t); // until the arm says otherwise, it is where it was told to stop | |
| 186 | any = true; | |
| 187 | } | |
| 188 | return any ? frame : null; | |
| 189 | } | |
| 190 | } | |
| 191 | ||
| 192 | /** | |
| 193 | * The page's driver: a Drive, a clock at the send rate while anything is held, and the ways a press ends. | |
| 194 | * `send` takes frames at the clock's rate (the page may coalesce them); `flush` must put a frame on the wire now, | |
| 195 | * because it is the zero; `end` says nothing is held any more. | |
| 196 | */ | |
| 197 | export class Driver { | |
| 198 | /** | |
| 199 | * @param {{ | |
| 200 | * send: (frame: Frame) => void, | |
| 201 | * flush: (frame: Frame) => void, | |
| 202 | * end: () => void, | |
| 203 | * begin?: () => boolean | void, | |
| 204 | * limits: () => Limits, | |
| 205 | * speed: () => number, | |
| 206 | * hz?: number, | |
| 207 | * onStop?: (id: number) => void, | |
| 208 | * timers?: { every: (fn: () => void, ms: number) => unknown, cancel: (handle: unknown) => void, now: () => number }, | |
| 209 | * }} options | |
| 210 | */ | |
| 211 | constructor(options) { | |
| 212 | this.o = options; | |
| 213 | this.drive = new Drive(); | |
| 214 | this.hz = Math.max(20, Math.min(30, options.hz ?? 25)); | |
| 215 | this.timers = options.timers ?? { every: (fn, ms) => setInterval(fn, ms), cancel: (h) => clearInterval(/** @type {any} */ (h)), now: () => performance.now() }; | |
| 216 | /** @type {unknown} */ | |
| 217 | this.clock = null; | |
| 218 | this.last = 0; | |
| 219 | } | |
| 220 | ||
| 221 | /** Whether a finger (or a key) is down on anything. */ | |
| 222 | get active() { | |
| 223 | return this.clock !== null; | |
| 224 | } | |
| 225 | ||
| 226 | /** @param {Iterable<readonly [number, number]>} positions */ | |
| 227 | seed(positions) { | |
| 228 | this.drive.seed(positions); | |
| 229 | } | |
| 230 | ||
| 231 | /** @param {string} holder @param {Record<number, number>} axes @returns {boolean} false when driving is refused */ | |
| 232 | hold(holder, axes) { | |
| 233 | if (!this.clock) { | |
| 234 | if (this.o.begin?.() === false) return false; | |
| 235 | this.last = this.timers.now(); | |
| 236 | this.clock = this.timers.every(() => this.tick(), 1000 / this.hz); | |
| 237 | } | |
| 238 | const stopped = this.drive.hold(holder, axes); | |
| 239 | if (stopped) this.o.flush(stopped); | |
| 240 | return true; | |
| 241 | } | |
| 242 | ||
| 243 | /** A holder lets go: its joints' zero goes out now, not at the next tick. @param {string} holder */ | |
| 244 | release(holder) { | |
| 245 | const frame = this.drive.release(holder); | |
| 246 | if (frame) this.o.flush(frame); | |
| 247 | if (this.clock && this.drive.holders.size === 0) this.(); | |
| 248 | } | |
| 249 | ||
| 250 | /** Everything lets go at once: the window lost focus, the page was hidden. */ | |
| 251 | releaseAll() { | |
| 252 | const frame = this.drive.releaseAll(); | |
| 253 | if (frame) this.o.flush(frame); | |
| 254 | if (this.clock) this.(); | |
| 255 | } | |
| 256 | ||
| 257 | /** One step of one joint, for a tap. @param {number} id @param {number} ticks signed */ | |
| 258 | nudge(id, ticks) { | |
| 259 | if (this.o.begin?.() === false) return; | |
| 260 | const frame = this.drive.nudge(id, ticks, this.o.limits()); | |
| 261 | if (frame) this.o.flush(frame); | |
| 262 | if (!this.clock) this.o.end(); | |
| 263 | } | |
| 264 | ||
| 265 | tick() { | |
| 266 | const now = this.timers.now(); | |
| 267 | const dt = Math.min(0.1, Math.max(0, (now - this.last) / 1000)); // a late tick never turns into a jump | |
| 268 | this.last = now; | |
| 269 | const { frame, stops } = this.drive.step(dt, this.o.speed(), this.o.limits()); | |
| 270 | if (frame) this.o.send(frame); | |
| 271 | for (const id of stops) this.o.onStop?.(id); | |
| 272 | } | |
| 273 | ||
| 274 | () { | |
| 275 | this.timers.cancel(this.clock); | |
| 276 | this.clock = null; | |
| 277 | this.o.end(); | |
| 278 | } | |
| 279 | ||
| 280 | /** | |
| 281 | * Every way a hold can end without a pointerup: the window loses focus, the page is hidden or put away. | |
| 282 | * @param {EventTarget} win @param {EventTarget & { hidden?: boolean }} doc | |
| 283 | */ | |
| 284 | watch(win, doc) { | |
| 285 | const all = () => this.releaseAll(); | |
| 286 | win.addEventListener("blur", all); | |
| 287 | win.addEventListener("pagehide", all); | |
| 288 | doc.addEventListener("visibilitychange", () => { if (doc.hidden) all(); }); | |
| 289 | } | |
| 290 | } | |
| 291 | ||
| 292 | /** | |
| 293 | * @typedef {EventTarget & { | |
| 294 | * setPointerCapture?: (id: number) => void, | |
| 295 | * getBoundingClientRect: () => { left: number, top: number, width: number, height: number }, | |
| 296 | * classList: { toggle: (name: string, on?: boolean) => unknown }, | |
| 297 | * }} Surface | |
| 298 | * @typedef {Event & { pointerId: number, clientX: number, clientY: number }} Pointer | |
| 20 | * @typedef {{ setAttribute: (name: string, value: string) => void, addEventListener: (type: string, fn: () => void) => void }} TrayButton | |
| 21 | * @typedef {{ classList: { toggle: (name: string, on?: boolean) => unknown } }} TrayBox | |
| 22 | * @typedef {{ getItem: (key: string) => string | null, setItem: (key: string, value: string) => void }} Store | |
| 299 | 23 | */ |
| 300 | 24 | |
| 301 | 25 | /** |
| 302 | * One finger on a surface, tracked by its pointerId: `place` maps where it is to joint deflections, every way the | |
| 303 | * finger can leave ends the hold, and a second finger on the same surface is ignored until the first one lifts. | |
| 304 | * Two surfaces are two holders, so two thumbs drive two sets of joints independently. | |
| 305 | * @param {Surface} el | |
| 306 | * @param {Driver} driver | |
| 307 | * @param {string} holder | |
| 308 | * @param {(x: number, y: number) => Record<number, number> | null} place x, y from -1 to 1 across the surface, y up | |
| 309 | * @param {{ show?: (x: number, y: number) => void, haptic?: () => void }} [view] | |
| 310 | * @returns {{ end: () => void }} | |
| 26 | * Wire the tray's one-line button to the tray: a tap opens or shuts it, and the button says which it is. | |
| 27 | * @param {TrayButton} button | |
| 28 | * @param {TrayBox} tray | |
| 29 | * @param {Store | null} [store] localStorage, or nothing (private mode, the tests) | |
| 30 | * @returns {{ readonly open: boolean, set: (open: boolean) => void }} | |
| 311 | 31 | */ |
| 312 | export function bindSurface(el, driver, holder, place, view = {}) { | |
| 313 | /** @type {number | null} */ | |
| 314 | let pointer = null; | |
| 315 | /** @param {Pointer} e */ | |
| 316 | const at = (e) => { | |
| 317 | const r = el.getBoundingClientRect(); | |
| 318 | const x = r.width ? ((e.clientX - r.left) / r.width) * 2 - 1 : 0; | |
| 319 | const y = r.height ? 1 - ((e.clientY - r.top) / r.height) * 2 : 0; | |
| 320 | return [Math.max(-1, Math.min(1, x)), Math.max(-1, Math.min(1, y))]; | |
| 32 | export function bindTray(button, tray, store = null) { | |
| 33 | let open = true; | |
| 34 | try { open = opened(store?.getItem(KEY)); } catch { /* storage refused: open */ } | |
| 35 | const set = (/** @type {boolean} */ on) => { | |
| 36 | open = Boolean(on); | |
| 37 | tray.classList.toggle("shut", !open); | |
| 38 | button.setAttribute("aria-expanded", String(open)); | |
| 39 | try { store?.setItem(KEY, open ? "open" : "shut"); } catch { /* storage refused: it lasts this visit */ } | |
| 321 | 40 | }; |
| 322 | /** @param {Pointer} e */ | |
| 323 | const apply = (e) => { | |
| 324 | const [x = 0, y = 0] = at(e); | |
| 325 | view.show?.(x, y); | |
| 326 | const axes = place(x, y); | |
| 327 | if (!axes) return false; | |
| 328 | return driver.hold(holder, axes); | |
| 329 | }; | |
| 330 | const end = () => { | |
| 331 | if (pointer === null) return; | |
| 332 | pointer = null; | |
| 333 | el.classList.toggle("held", false); | |
| 334 | view.show?.(0, 0); | |
| 335 | driver.release(holder); | |
| 336 | }; | |
| 337 | el.addEventListener("pointerdown", (ev) => { | |
| 338 | const e = /** @type {Pointer} */ (ev); | |
| 339 | if (pointer !== null) return; | |
| 340 | e.preventDefault(); | |
| 341 | pointer = e.pointerId; | |
| 342 | try { el.setPointerCapture?.(e.pointerId); } catch { /* a synthetic pointer */ } | |
| 343 | el.classList.toggle("held", true); | |
| 344 | view.haptic?.(); | |
| 345 | if (apply(e) === false) end(); | |
| 346 | }); | |
| 347 | el.addEventListener("pointermove", (ev) => { | |
| 348 | const e = /** @type {Pointer} */ (ev); | |
| 349 | if (e.pointerId === pointer) apply(e); | |
| 350 | }); | |
| 351 | for (const type of ["pointerup", "pointercancel", "lostpointercapture"]) { | |
| 352 | el.addEventListener(type, (ev) => { if (/** @type {Pointer} */ (ev).pointerId === pointer) end(); }); | |
| 353 | } | |
| 354 | el.addEventListener("contextmenu", (e) => e.preventDefault()); | |
| 355 | return { end }; | |
| 41 | set(open); | |
| 42 | button.addEventListener("click", () => set(!open)); | |
| 43 | return { get open() { return open; }, set }; | |
| 356 | 44 | } |
Software/so101/src/ui/drive.test.ts +50 −285
| @@ -1,293 +1,58 @@ | ||
| 1 | 1 | import { describe, expect, test } from "bun:test"; |
| 2 | import { bindSurface, canDrive, Drive, Driver, shape } from "./drive.js"; | |
| 2 | import { bindTray, KEY, opened } from "./drive.js"; | |
| 3 | 3 | |
| 4 | const LIMITS = { 1: { min: 1000, max: 3000 }, 2: { min: 1000, max: 3000 }, 3: { min: 1000, max: 3000 }, 4: { min: 1000, max: 3000 }, 6: { min: 2000, max: 3500 } }; | |
| 5 | const AT = [[1, 2000], [2, 2000], [3, 2000], [4, 2000], [6, 2500]] as const; | |
| 6 | ||
| 7 | /** A driver on a hand-wound clock that records what goes on the wire, and when. */ | |
| 8 | function rig() { | |
| 9 | const wire: Array<{ kind: "send" | "flush" | "end"; frame?: Record<number, number>; at: number }> = []; | |
| 10 | let now = 0; | |
| 11 | let tick: (() => void) | null = null; | |
| 12 | const driver = new Driver({ | |
| 13 | send: (frame) => wire.push({ kind: "send", frame, at: now }), | |
| 14 | flush: (frame) => wire.push({ kind: "flush", frame, at: now }), | |
| 15 | end: () => wire.push({ kind: "end", at: now }), | |
| 16 | limits: () => LIMITS, | |
| 17 | speed: () => 300, | |
| 18 | timers: { every: (fn) => ((tick = fn), 1), cancel: () => (tick = null), now: () => now }, | |
| 19 | }); | |
| 20 | driver.seed(AT); | |
| 21 | /** Advance the clock by one 40 ms frame and run the tick, when one is running. */ | |
| 22 | const frame = () => { | |
| 23 | now += 40; | |
| 24 | tick?.(); | |
| 25 | }; | |
| 26 | return { driver, wire, frame, running: () => tick !== null }; | |
| 4 | /** A button built on EventTarget like the page's, recording its attributes. */ | |
| 5 | function button() { | |
| 6 | const b = Object.assign(new EventTarget(), { attrs: {} as Record<string, string> }); | |
| 7 | return Object.assign(b, { setAttribute: (n: string, v: string) => void (b.attrs[n] = v) }); | |
| 27 | 8 | } |
| 28 | 9 | |
| 29 | /** A surface 100 px square at the origin, built on EventTarget like the page's elements. */ | |
| 30 | function surface() { | |
| 31 | const el = Object.assign(new EventTarget(), { | |
| 32 | captured: [] as number[], | |
| 33 | held: false, | |
| 34 | setPointerCapture(id: number) { this.captured.push(id); }, | |
| 35 | getBoundingClientRect: () => ({ left: 0, top: 0, width: 100, height: 100 }), | |
| 36 | classList: { toggle: (_: string, on?: boolean) => (el.held = Boolean(on)) }, | |
| 37 | }); | |
| 38 | return el; | |
| 10 | /** A tray whose classList is a set. */ | |
| 11 | function tray() { | |
| 12 | const classes = new Set<string>(); | |
| 13 | return { classes, classList: { toggle: (name: string, on?: boolean) => ((on ?? !classes.has(name)) ? classes.add(name) : classes.delete(name)) } }; | |
| 39 | 14 | } |
| 40 | const pointer = (type: string, pointerId: number, clientX = 50, clientY = 50) => Object.assign(new Event(type, { cancelable: true }), { pointerId, clientX, clientY }); | |
| 41 | 15 | |
| 42 | describe("the stick's shape", () => { | |
| 43 | test("a dead band in the middle, the full speed at the edge, never more", () => { | |
| 44 | expect(shape(0.05)).toBe(0); | |
| 45 | expect(shape(-0.12)).toBe(0); | |
| 46 | expect(shape(1)).toBe(1); | |
| 47 | expect(shape(-3)).toBe(-1); | |
| 48 | expect(shape(Number.NaN)).toBe(0); | |
| 49 | expect(shape(0.5)).toBeGreaterThan(0); | |
| 50 | expect(shape(0.5)).toBeLessThan(0.5); // fine control a little way out | |
| 51 | expect(shape(-0.5)).toBe(-shape(0.5)); | |
| 52 | }); | |
| 53 | }); | |
| 54 | ||
| 55 | describe("the velocity integrator", () => { | |
| 56 | test("a held joint runs ahead at the deflection's share of the speed, from where the arm is", () => { | |
| 57 | const d = new Drive(); | |
| 58 | d.seed(AT); | |
| 59 | d.hold("a", { 1: 0.5 }); | |
| 60 | expect(d.step(0.1, 300, LIMITS).frame).toEqual({ 1: 2015 }); | |
| 61 | expect(d.step(0.1, 300, LIMITS).frame).toEqual({ 1: 2030 }); | |
| 62 | }); | |
| 63 | ||
| 64 | test("the target stops at the range's margin and says so once", () => { | |
| 65 | const d = new Drive(); | |
| 66 | d.seed([[1, 2960]]); | |
| 67 | d.hold("a", { 1: 1 }); | |
| 68 | expect(d.step(0.1, 300, LIMITS)).toEqual({ frame: { 1: 2970 }, stops: [1] }); | |
| 69 | expect(d.step(0.1, 300, LIMITS)).toEqual({ frame: null, stops: [] }); | |
| 70 | }); | |
| 71 | ||
| 72 | test("never runs more than the lead ahead of an arm that lags, and never steps back toward a stale reading", () => { | |
| 73 | const d = new Drive({ lead: 0.5 }); | |
| 74 | d.seed([[1, 2000]]); | |
| 75 | d.hold("a", { 1: 1 }); | |
| 76 | for (let i = 0; i < 20; i++) d.step(0.1, 300, LIMITS); | |
| 77 | expect(d.targets.get(1)).toBe(2150); // 0.5 s of 300 ticks/s ahead of where the arm said it was | |
| 78 | d.seed([[1, 1900]]); // a reading older than the target | |
| 79 | expect(d.step(0.1, 300, LIMITS).frame).toBeNull(); | |
| 80 | expect(d.targets.get(1)).toBe(2150); | |
| 81 | }); | |
| 82 | ||
| 83 | test("a joint without a position is not driven", () => { | |
| 84 | const d = new Drive(); | |
| 85 | d.hold("a", { 5: 1 }); | |
| 86 | expect(d.step(0.1, 300, LIMITS).frame).toBeNull(); | |
| 87 | expect(d.release("a")).toBeNull(); | |
| 88 | }); | |
| 89 | ||
| 90 | test("a tap nudges from the last target, inside the range", () => { | |
| 91 | const d = new Drive(); | |
| 92 | d.seed([[6, 3450]]); | |
| 93 | expect(d.nudge(6, 20, LIMITS)).toEqual({ 6: 3470 }); | |
| 94 | expect(d.nudge(6, 20, LIMITS)).toEqual({ 6: 3470 }); | |
| 95 | expect(d.nudge(6, -20, LIMITS)).toEqual({ 6: 3450 }); | |
| 96 | }); | |
| 97 | }); | |
| 98 | ||
| 99 | describe("the driver", () => { | |
| 100 | test("lifting the finger sends zero within one frame: the target as it stands, flushed at once, then nothing", () => { | |
| 101 | const { driver, wire, frame, running } = rig(); | |
| 102 | driver.hold("lever", { 2: 1 }); | |
| 103 | frame(); frame(); | |
| 104 | const last = wire.at(-1)!; | |
| 105 | expect(last.kind).toBe("send"); | |
| 106 | const sentAt = wire.length; | |
| 107 | driver.release("lever"); | |
| 108 | // Before any further tick of the clock: the zero is already on the wire. | |
| 109 | expect(wire.slice(sentAt)).toEqual([{ kind: "flush", frame: last.frame, at: last.at }, { kind: "end", at: last.at }]); | |
| 110 | expect(running()).toBe(false); | |
| 111 | frame(); frame(); | |
| 112 | expect(wire.length).toBe(sentAt + 2); // and nothing after it | |
| 113 | }); | |
| 114 | ||
| 115 | test("two thumbs drive independent joints: lifting one stops only its joints", () => { | |
| 116 | const { driver, wire, frame } = rig(); | |
| 117 | driver.hold("left", { 1: 1, 2: 0 }); | |
| 118 | driver.hold("right", { 3: -1 }); | |
| 119 | frame(); | |
| 120 | expect(wire.at(-1)).toEqual({ kind: "send", frame: { 1: 2012, 3: 1988 }, at: 40 }); | |
| 121 | driver.release("left"); | |
| 122 | expect(wire.at(-1)).toEqual({ kind: "flush", frame: { 1: 2012, 2: 2000 }, at: 40 }); | |
| 123 | frame(); | |
| 124 | expect(wire.at(-1)).toEqual({ kind: "send", frame: { 3: 1976 }, at: 80 }); // the right thumb goes on | |
| 125 | driver.release("right"); | |
| 126 | expect(wire.slice(-2)).toEqual([{ kind: "flush", frame: { 3: 1976 }, at: 80 }, { kind: "end", at: 80 }]); | |
| 127 | }); | |
| 128 | ||
| 129 | test("two fingers on one joint: it stops only when both have lifted", () => { | |
| 130 | const { driver, wire, frame } = rig(); | |
| 131 | driver.hold("lever", { 4: 1 }); | |
| 132 | driver.hold("plus", { 4: 1 }); | |
| 133 | frame(); | |
| 134 | driver.release("plus"); | |
| 135 | expect(wire.at(-1)!.kind).toBe("send"); | |
| 136 | frame(); | |
| 137 | expect(wire.at(-1)).toEqual({ kind: "send", frame: { 4: 2024 }, at: 80 }); | |
| 138 | driver.release("lever"); | |
| 139 | expect(wire.at(-1)!.kind).toBe("end"); | |
| 140 | }); | |
| 141 | ||
| 142 | test("blur, pagehide and a hidden page stop every joint at once", () => { | |
| 143 | for (const how of ["blur", "pagehide", "hidden"] as const) { | |
| 144 | const { driver, wire, frame, running } = rig(); | |
| 145 | const win = new EventTarget(), doc = Object.assign(new EventTarget(), { hidden: false }); | |
| 146 | driver.watch(win, doc); | |
| 147 | driver.hold("left", { 1: 1 }); | |
| 148 | driver.hold("right", { 3: 1 }); | |
| 149 | frame(); | |
| 150 | if (how === "hidden") { doc.hidden = true; doc.dispatchEvent(new Event("visibilitychange")); } | |
| 151 | else win.dispatchEvent(new Event(how)); | |
| 152 | expect(wire.slice(-2)).toEqual([{ kind: "flush", frame: { 1: 2012, 3: 2012 }, at: 40 }, { kind: "end", at: 40 }]); | |
| 153 | expect(running()).toBe(false); | |
| 154 | } | |
| 155 | }); | |
| 156 | ||
| 157 | test("a hidden page that was not driving sends nothing", () => { | |
| 158 | const { driver, wire } = rig(); | |
| 159 | const win = new EventTarget(), doc = Object.assign(new EventTarget(), { hidden: true }); | |
| 160 | driver.watch(win, doc); | |
| 161 | doc.dispatchEvent(new Event("visibilitychange")); | |
| 162 | win.dispatchEvent(new Event("blur")); | |
| 163 | expect(wire).toEqual([]); | |
| 164 | }); | |
| 165 | ||
| 166 | test("refused by the page (not your turn), a press starts nothing", () => { | |
| 167 | const wire: unknown[] = []; | |
| 168 | const driver = new Driver({ send: (f) => wire.push(f), flush: (f) => wire.push(f), end: () => wire.push("end"), begin: () => false, limits: () => LIMITS, speed: () => 300 }); | |
| 169 | driver.seed(AT); | |
| 170 | expect(driver.hold("x", { 1: 1 })).toBe(false); | |
| 171 | expect(driver.active).toBe(false); | |
| 172 | driver.nudge(1, 20); | |
| 173 | expect(wire).toEqual([]); | |
| 174 | }); | |
| 175 | ||
| 176 | test("a tap nudges and ends at once", () => { | |
| 177 | const { driver, wire } = rig(); | |
| 178 | driver.nudge(6, -20); | |
| 179 | expect(wire).toEqual([{ kind: "flush", frame: { 6: 2480 }, at: 0 }, { kind: "end", at: 0 }]); | |
| 180 | }); | |
| 181 | }); | |
| 182 | ||
| 183 | describe("a surface under a finger", () => { | |
| 184 | const lever = (y: number) => ({ 2: y }); | |
| 185 | ||
| 186 | test("pointerup sends the zero before the next frame, and the surface lets go", () => { | |
| 187 | const { driver, wire, frame } = rig(); | |
| 188 | const el = surface(); | |
| 189 | bindSurface(el, driver, "lever", (_, y) => lever(y)); | |
| 190 | el.dispatchEvent(pointer("pointerdown", 7, 50, 0)); // the top edge: full speed up | |
| 191 | expect(el.captured).toEqual([7]); | |
| 192 | expect(el.held).toBe(true); | |
| 193 | frame(); | |
| 194 | expect(wire.at(-1)).toEqual({ kind: "send", frame: { 2: 2012 }, at: 40 }); | |
| 195 | el.dispatchEvent(pointer("pointerup", 7)); | |
| 196 | expect(wire.slice(-2)).toEqual([{ kind: "flush", frame: { 2: 2012 }, at: 40 }, { kind: "end", at: 40 }]); | |
| 197 | expect(el.held).toBe(false); | |
| 198 | }); | |
| 199 | ||
| 200 | for (const type of ["pointercancel", "lostpointercapture"]) { | |
| 201 | test(`${type} ends the hold like a lift`, () => { | |
| 202 | const { driver, wire, frame, running } = rig(); | |
| 203 | const el = surface(); | |
| 204 | bindSurface(el, driver, "lever", (_, y) => lever(y)); | |
| 205 | el.dispatchEvent(pointer("pointerdown", 3, 50, 100)); | |
| 206 | frame(); | |
| 207 | el.dispatchEvent(pointer(type, 3)); | |
| 208 | expect(wire.at(-1)!.kind).toBe("end"); | |
| 209 | expect(running()).toBe(false); | |
| 210 | }); | |
| 211 | } | |
| 212 | ||
| 213 | test("a second finger on the same surface neither steers it nor ends it", () => { | |
| 214 | const { driver, wire, frame } = rig(); | |
| 215 | const el = surface(); | |
| 216 | bindSurface(el, driver, "lever", (_, y) => lever(y)); | |
| 217 | el.dispatchEvent(pointer("pointerdown", 1, 50, 0)); | |
| 218 | el.dispatchEvent(pointer("pointerdown", 2, 50, 100)); | |
| 219 | el.dispatchEvent(pointer("pointermove", 2, 50, 100)); | |
| 220 | el.dispatchEvent(pointer("pointerup", 2)); | |
| 221 | frame(); | |
| 222 | expect(wire.at(-1)).toEqual({ kind: "send", frame: { 2: 2012 }, at: 40 }); | |
| 223 | }); | |
| 224 | ||
| 225 | test("two sticks under two thumbs, each with its own pointer, drive their own joints", () => { | |
| 226 | const { driver, wire, frame } = rig(); | |
| 227 | const left = surface(), right = surface(); | |
| 228 | bindSurface(left, driver, "left", (x, y) => ({ 1: x, 2: y })); | |
| 229 | bindSurface(right, driver, "right", (x, y) => ({ 4: x, 3: y })); | |
| 230 | left.dispatchEvent(pointer("pointerdown", 11, 100, 50)); // full right | |
| 231 | right.dispatchEvent(pointer("pointerdown", 12, 50, 100)); // full down | |
| 232 | frame(); | |
| 233 | expect(wire.at(-1)).toEqual({ kind: "send", frame: { 1: 2012, 3: 1988 }, at: 40 }); | |
| 234 | right.dispatchEvent(pointer("pointerup", 11)); // the other thumb's id on this stick: ignored | |
| 235 | left.dispatchEvent(pointer("pointerup", 11)); | |
| 236 | expect(wire.at(-1)).toEqual({ kind: "flush", frame: { 1: 2012, 2: 2000 }, at: 40 }); | |
| 237 | frame(); | |
| 238 | expect(wire.at(-1)).toEqual({ kind: "send", frame: { 3: 1976 }, at: 80 }); | |
| 239 | }); | |
| 240 | ||
| 241 | test("a press the page refuses lets go of the surface at once", () => { | |
| 242 | const driver = new Driver({ send: () => {}, flush: () => {}, end: () => {}, begin: () => false, limits: () => LIMITS, speed: () => 300 }); | |
| 243 | const el = surface(); | |
| 244 | bindSurface(el, driver, "lever", (_, y) => lever(y)); | |
| 245 | el.dispatchEvent(pointer("pointerdown", 1, 50, 0)); | |
| 246 | expect(el.held).toBe(false); | |
| 247 | }); | |
| 248 | ||
| 249 | test("the arm drops while a thumb is held: the poll's releaseAll flushes the zero, and the still-pressed stick sends nothing more", () => { | |
| 250 | const wire: Array<{ kind: string; frame?: Record<number, number> }> = []; | |
| 251 | let tick: (() => void) | null = null, now = 0; | |
| 252 | const state = { mayDrive: true, estop: false, reconnecting: false, limits: LIMITS }; | |
| 253 | const driver = new Driver({ | |
| 254 | send: (frame) => wire.push({ kind: "send", frame }), | |
| 255 | flush: (frame) => wire.push({ kind: "flush", frame }), | |
| 256 | end: () => wire.push({ kind: "end" }), | |
| 257 | begin: () => canDrive(state), | |
| 258 | limits: () => LIMITS, | |
| 259 | speed: () => 300, | |
| 260 | timers: { every: (fn) => ((tick = fn), 1), cancel: () => (tick = null), now: () => now }, | |
| 261 | }); | |
| 262 | driver.seed(AT); | |
| 263 | const stick = surface(); | |
| 264 | bindSurface(stick, driver, "left", (x, y) => ({ 1: x, 2: y })); | |
| 265 | stick.dispatchEvent(pointer("pointerdown", 7, 100, 50)); | |
| 266 | now += 40; tick!(); | |
| 267 | expect(wire.at(-1)).toEqual({ kind: "send", frame: { 1: 2012 } }); | |
| 268 | // The status poll: reconnecting, so driving is refused and everything held lets go. | |
| 269 | state.reconnecting = true; | |
| 270 | if (!canDrive(state)) driver.releaseAll(); | |
| 271 | expect(wire.slice(-2)).toEqual([{ kind: "flush", frame: { 1: 2012, 2: 2000 } }, { kind: "end" }]); | |
| 272 | expect(tick).toBeNull(); | |
| 273 | const after = wire.length; | |
| 274 | stick.dispatchEvent(pointer("pointermove", 7, 100, 0)); // the thumb is still down and moving | |
| 275 | expect(wire.length).toBe(after); | |
| 276 | expect(tick).toBeNull(); | |
| 277 | }); | |
| 278 | }); | |
| 279 | ||
| 280 | describe("canDrive", () => { | |
| 281 | const ok = { mayDrive: true, estop: false, reconnecting: false, limits: LIMITS }; | |
| 282 | test("drives on your turn, with the arm up and its limits known", () => { | |
| 283 | expect(canDrive(ok)).toBe(true); | |
| 284 | }); | |
| 285 | test("refuses when it is not your turn, the arm is stopped, the arm is reconnecting, or no limits have come", () => { | |
| 286 | expect(canDrive({ ...ok, mayDrive: false })).toBe(false); | |
| 287 | expect(canDrive({ ...ok, estop: true })).toBe(false); | |
| 288 | expect(canDrive({ ...ok, held: { by: "ann", at: "2026-10-20T22:05:00Z" } })).toBe(false); // STOP, until someone resumes | |
| 289 | expect(canDrive({ ...ok, reconnecting: true })).toBe(false); | |
| 290 | expect(canDrive({ ...ok, limits: {} })).toBe(false); | |
| 291 | expect(canDrive({ ...ok, limits: null })).toBe(false); | |
| 16 | function store(initial: Record<string, string> = {}) { | |
| 17 | const data = { ...initial }; | |
| 18 | return { data, getItem: (k: string) => data[k] ?? null, setItem: (k: string, v: string) => void (data[k] = v) }; | |
| 19 | } | |
| 20 | ||
| 21 | describe("the joint tray", () => { | |
| 22 | test("opens on a first visit, and only a stored 'shut' starts it shut", () => { | |
| 23 | expect(opened(null)).toBe(true); | |
| 24 | expect(opened("open")).toBe(true); | |
| 25 | expect(opened("shut")).toBe(false); | |
| 26 | }); | |
| 27 | ||
| 28 | test("a tap shuts it to one line and the next opens it, the button saying which", () => { | |
| 29 | const b = button(), t = tray(), s = store(); | |
| 30 | const view = bindTray(b, t, s); | |
| 31 | expect(view.open).toBe(true); | |
| 32 | expect(t.classes.has("shut")).toBe(false); | |
| 33 | expect(b.attrs["aria-expanded"]).toBe("true"); | |
| 34 | b.dispatchEvent(new Event("click")); | |
| 35 | expect(view.open).toBe(false); | |
| 36 | expect(t.classes.has("shut")).toBe(true); | |
| 37 | expect(b.attrs["aria-expanded"]).toBe("false"); | |
| 38 | expect(s.data[KEY]).toBe("shut"); | |
| 39 | b.dispatchEvent(new Event("click")); | |
| 40 | expect(view.open).toBe(true); | |
| 41 | expect(t.classes.has("shut")).toBe(false); | |
| 42 | expect(b.attrs["aria-expanded"]).toBe("true"); | |
| 43 | expect(s.data[KEY]).toBe("open"); | |
| 44 | }); | |
| 45 | ||
| 46 | test("the choice outlasts the visit, and a refusing store leaves it working", () => { | |
| 47 | const t = tray(); | |
| 48 | expect(bindTray(button(), t, store({ [KEY]: "shut" })).open).toBe(false); | |
| 49 | expect(t.classes.has("shut")).toBe(true); | |
| 50 | const refusing = { getItem: () => { throw new Error("denied"); }, setItem: () => { throw new Error("denied"); } }; | |
| 51 | const b = button(); | |
| 52 | const view = bindTray(b, tray(), refusing); | |
| 53 | expect(view.open).toBe(true); | |
| 54 | b.dispatchEvent(new Event("click")); | |
| 55 | expect(view.open).toBe(false); | |
| 56 | expect(bindTray(button(), tray()).open).toBe(true); // no store at all | |
| 292 | 57 | }); |
| 293 | 58 | }); |
Software/so101/src/ui/mobile.css +12
| @@ -27,4 +27,16 @@ | ||
| 27 | 27 | since the strip already says who drives. */ |
| 28 | 28 | #top .info, #driverTag { display: none; } |
| 29 | 29 | #armLine { display: flex; } |
| 30 | /* The joint tray (src/ui/drive.js), under the model on your turn: every joint and the gripper at once, one compact row | |
| 31 | each (name, slider, value) with the intent and actual marks on its rail. Its button is the one line it folds to, and | |
| 32 | folded the controls under it fold too, so the model takes the room; STOP stays in the strip either way. */ | |
| 33 | #trayToggle { display: flex; align-items: center; justify-content: space-between; width: 100%; padding: 0 12px; font-weight: 650; } | |
| 34 | #trayToggle span::after { content: "Hide"; color: var(); font-size: 13px; font-weight: 500; } | |
| 35 | #trayToggle[aria-expanded="false"] span::after { content: "Show"; } | |
| 36 | #tray.shut #joints, #tray.shut ~ * { display: none; } | |
| 37 | #joints { grid-template-columns: 100%; gap: 0; } | |
| 38 | .j { grid-template-columns: 4.6em minmax(0, 1fr) 4.5ch; grid-template-areas: "name slide val"; min-height: var(); padding: 0 4px; } | |
| 39 | .j b .long, .j .jog, .j small { display: none; } | |
| 40 | .j b .short { display: inline; } | |
| 41 | .j .v { justify-content: flex-end; } | |
| 30 | 42 | } |
Software/so101/src/ui/tabs.css +1 −19
| @@ -1,6 +1,5 @@ | ||
| 1 | 1 | /* The phone-portrait bottom tab bar: one panel at a time. Hidden on desktop and in landscape. STOP stays in the strip. |
| 2 | Every bar rule keys on .docked, which the page sets once the bar is drawn: until then the sheet's own tabs are the navigation. | |
| 3 | Last, the touch lever, which shows only on a phone held upright. */ | |
| 2 | Every bar rule keys on .docked, which the page sets once the bar is drawn: until then the sheet's own tabs are the navigation. */ | |
| 4 | 3 | #dock { display: none; } |
| 5 | 4 | @media (max-width: 640px) and (orientation: portrait) { |
| 6 | 5 | html #shell.docked { grid-template-rows: minmax(0, var()) auto minmax(0, 1fr) calc(56px + var()); grid-template-areas: "view" "strip" "sheet" "dock"; } |
| @@ -13,21 +12,4 @@ | ||
| 13 | 12 | html #shell.docked[data-panel="drive"]:not(.mayDrive) #sheet { display: none; } |
| 14 | 13 | html #shell.docked:not([data-panel="drive"]) { grid-template-rows: 0 auto minmax(0, 1fr) calc(56px + var()); } |
| 15 | 14 | #shell.docked:not([data-panel="drive"]) #app { display: none; } |
| 16 | /* Touch driving (src/ui/drive.js), shown only here: pick a joint and push one big lever; let go and it stops. */ | |
| 17 | #chips { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 4px; } | |
| 18 | #chips button { min-height: 50px; padding: 0 2px; border-radius: 10px; display: grid; place-content: center; gap: 1px; line-height: 1.1; } | |
| 19 | #chips button b { font-size: 15px; } #chips button small { font-size: 10.5px; color: var(); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } | |
| 20 | #chips button[aria-checked=true] { background: var(); border-color: var(); color: var(); } #chips button[aria-checked=true] small { color: inherit; opacity: .85; } | |
| 21 | #one { display: grid; grid-template-columns: minmax(0, 1fr) 84px minmax(0, 1fr); gap: 12px; height: clamp(210px, 36svh, 360px); } | |
| 22 | #one .nudges { display: grid; grid-template-rows: 1fr 1fr; gap: 10px; } | |
| 23 | #one .nudges button { display: grid; place-content: center; gap: 2px; font-size: 22px; line-height: 1; touch-action: none; -webkit-user-select: none; user-select: none; } | |
| 24 | #one .nudges small { font-size: 12px; color: var(); } | |
| 25 | #lever { position: relative; border-radius: 42px; background: var(); border: 1px solid var(); touch-action: none; -webkit-user-select: none; user-select: none; overflow: hidden; cursor: ns-resize; } | |
| 26 | #lever::before { content: ""; position: absolute; left: 16px; right: 16px; top: 50%; height: 2px; margin-top: -1px; background: var(); } | |
| 27 | #leverAt { position: absolute; left: 0; bottom: 0; width: 5px; height: 100%; background: var(); transform-origin: bottom; transform: scaleY(var(, .5)); } | |
| 28 | #leverKnob { position: absolute; left: 7px; right: 7px; top: 50%; height: 68px; margin-top: -34px; border-radius: 34px; background: var(); color: var(); display: grid; place-content: center; font-size: 22px; box-shadow: 0 2px 10px rgba(0,0,0,.3); transition: transform .14s ease-out; pointer-events: none; } | |
| 29 | #lever.held { border-color: var(); background: var(); } #lever.held #leverKnob { transition: none; } | |
| 30 | #one .about { display: grid; align-content: center; gap: 4px; min-width: 0; } | |
| 31 | #oneName { font-size: 20px; font-weight: 700; } #onePos { font-size: 28px; font-weight: 650; font-variant-numeric: tabular-nums; } | |
| 32 | #oneHint { color: var(); font-size: 12px; } | |
| 33 | 15 | } |
Software/so101/src/viewer.html +17 −111
| @@ -168,9 +168,8 @@ | ||
| 168 | 168 | .pose { display: inline-flex; gap: 4px; align-items: center; } |
| 169 | 169 | #readout { color: var(); font-size: 13px; display: flex; gap: 14px; flex-wrap: wrap; } #readout b { color: var(); font-weight: 550; } |
| 170 | 170 | |
| 171 | /* touch driving (src/ui/drive.js): on a phone held upright, pick a joint and push one big lever; let go and it stops (ui/tabs.css) */ | |
| 172 | #touch { display: none; gap: 10px; } | |
| 173 | #lever.end { box-shadow: 0 0 0 3px var(); } | |
| 171 | /* the joint tray (src/ui/drive.js): its one-line button shows only on a phone held upright (ui/mobile.css) */ | |
| 172 | #tray { display: grid; gap: 6px; } #trayToggle { display: none; } .j b .short { display: none; } | |
| 174 | 173 | #lag { color: #f2cc60; font-variant-numeric: tabular-nums; } |
| 175 | 174 | #chat .earlier { display: block; margin: 2px auto 8px; font-size: 13px; } |
| 176 | 175 | |
| @@ -252,7 +251,6 @@ | ||
| 252 | 251 | #msg { bottom: 52px; } |
| 253 | 252 | .tab { padding-top: 8px; } |
| 254 | 253 | } |
| 255 | @media (max-width: 640px) and (orientation: portrait) { #touch { display: grid; } #joints { display: none ; } } | |
| 256 | 254 | /* One STOP in every layout; it holds until Resume. Sideways it is fixed top-right (the strip's blur sits on ::before |
| 257 | 255 | so it does not pin STOP to the strip). */ |
| 258 | 256 | @media (orientation: landscape) and (max-height: 500px) { |
| @@ -317,16 +315,11 @@ | ||
| 317 | 315 | <p class="note">Plug an SO-101 leader into this computer or Android phone. The 3D model mirrors it while you wait; switch Teleop on and, on your turn, the arm follows it live. Hiding this tab or unplugging the leader stops the arm where it is.</p> |
| 318 | 316 | </div> |
| 319 | 317 | <div class="tab" id="tab-controls" role="tabpanel" aria-labelledby="t-controls" hidden> |
| 320 | <section id="touch" aria-label="Drive one joint"> | |
| 321 | <div id="chips" role="radiogroup" aria-label="Joint"></div> | |
| 322 | <div id="one"> | |
| 323 | <div class="nudges"><button type="button" data-nudge="1" id="nudgeUp">▲<small></small></button><button type="button" data-nudge="-1" id="nudgeDown">▼<small></small></button></div> | |
| 324 | <div id="lever" role="slider" tabindex="0" aria-orientation="vertical" aria-valuemin="-100" aria-valuemax="100" aria-valuenow="0"><span id="leverAt"></span><span id="leverKnob" aria-hidden="true">⇕</span></div> | |
| 325 | <div class="about"><span id="oneName"></span><span id="onePos">—</span><span id="oneHint">Push the lever: further is faster. Let go and it stops.</span></div> | |
| 326 | </div> | |
| 327 | </section> | |
| 328 | 318 | <p id="why" role="status"></p> |
| 329 | <div id="joints"></div> | |
| 319 | <section id="tray" aria-label="Joints"> | |
| 320 | <button type="button" id="trayToggle" aria-controls="joints" aria-expanded="true">Joints<span aria-hidden="true"></span></button> | |
| 321 | <div id="joints"></div> | |
| 322 | </section> | |
| 330 | 323 | <div class="row" id="poses"></div> |
| 331 | 324 | <div class="row" id="drive"> |
| 332 | 325 | <label class="inline">jog <select id="rate"><option value="100">slow</option><option value="300" selected>medium</option><option value="800">fast</option></select></label> |
| @@ -480,7 +473,7 @@ | ||
| 480 | 473 | </div> |
| 481 | 474 | <script type="module"> |
| 482 | 475 | import { cameraPlan, QUALITIES, samePlan } from "/ui/__VERSION__/camera-plan.js"; |
| 483 | import { bindSurface, canDrive, Driver, shape, UP } from "/ui/__VERSION__/drive.js"; | |
| 476 | import { bindTray } from "/ui/__VERSION__/drive.js"; | |
| 484 | 477 | import { Frames } from "/ui/__VERSION__/frames.js"; |
| 485 | 478 | import { createRoom } from "/ui/__VERSION__/lobby-panel.js"; |
| 486 | 479 | |
| @@ -516,7 +509,6 @@ | ||
| 516 | 509 | $("sheetToggle").textContent = bare ? "Show" : "Hide"; $("sheetToggle").setAttribute("aria-expanded", String(!bare)); |
| 517 | 510 | }; |
| 518 | 511 | $("sheetToggle").onclick = () => setBare(!shell.classList.contains("bare")); |
| 519 | const sideways = matchMedia("(orientation: landscape) and (max-height: 500px)"); | |
| 520 | 512 | let tab = "chat"; |
| 521 | 513 | /** The Machine tab's panel, once it is loaded (below); here, because leaving any tab closes it. */ |
| 522 | 514 | const machine = { panel: null }; |
| @@ -868,7 +860,6 @@ | ||
| 868 | 860 | // STOP: drop what this page would still send, then the server halts and holds the arm until Resume. |
| 869 | 861 | async function stop() { |
| 870 | 862 | stream.halt(); |
| 871 | driver.releaseAll(); | |
| 872 | 863 | stopLeaderDrive("stopped"); |
| 873 | 864 | const r = await api("/stop", {}); |
| 874 | 865 | msg(r.ok ? `STOPPED${r.stopped ? ` ${r.stopped}` : ""}${r.saved ? `; saved recording ${r.saved}` : ""}: the arm holds until you resume` : r.error, true); |
| @@ -1201,93 +1192,9 @@ | ||
| 1201 | 1192 | } |
| 1202 | 1193 | } |
| 1203 | 1194 | |
| 1204 | // ---------------------------------------------------------------- touch driving (src/ui/drive.js) | |
| 1205 | // Upright: a chip picks the joint, the lever's deflection is its speed, ▲ and ▼ nudge it and jog when held. | |
| 1206 | const driver = new Driver({ | |
| 1207 | send: (f) => stream.send(f), | |
| 1208 | flush: (f) => stream.flush(f), | |
| 1209 | end: () => stream.end(), | |
| 1210 | begin: () => { | |
| 1211 | if (!canDrive({ mayDrive: mayDrive(), estop: status?.estop, held: status?.held, reconnecting: status?.reconnecting, limits })) return false; | |
| 1212 | stream.open(); | |
| 1213 | return true; | |
| 1214 | }, | |
| 1215 | limits: () => limits, | |
| 1216 | speed: () => Number($("rate").value) || 300, | |
| 1217 | onStop: (id) => { | |
| 1218 | navigator.vibrate?.(30); | |
| 1219 | for (const e of [$("lever")]) { e.classList.add("end"); setTimeout(() => e.classList.remove("end"), 400); } | |
| 1220 | msg(`${LABEL[id]?.[1] ?? id} is at the end of its range`); | |
| 1221 | }, | |
| 1222 | }); | |
| 1223 | driver.watch(window, document); | |
| 1224 | // Turning the phone swaps the controls under the thumbs: whatever was held stops. | |
| 1225 | sideways.addEventListener("change", () => driver.releaseAll()); | |
| 1226 | const tap = () => navigator.vibrate?.(8); | |
| 1227 | const LABEL = { 1: ["J1", "Base"], 2: ["J2", "Shoulder"], 3: ["J3", "Elbow"], 4: ["J4", "Wrist"], 5: ["J5", "Roll"], 6: ["Grip", "Gripper"] }; | |
| 1228 | const ENDS = { 1: ["right", "left"], 2: ["up", "down"], 3: ["up", "down"], 4: ["up", "down"], 5: ["roll +", "roll −"], 6: ["open", "close"] }; | |
| 1229 | let picked = (() => { try { const n = Number(localStorage.getItem("so101.joint")); return LABEL[n] ? n : 2; } catch { return 2; } })(); | |
| 1230 | const lever = $("lever"); | |
| 1231 | function pickJoint(id) { | |
| 1232 | if (driver.drive.holders.has("lever")) driver.hold("lever", { [id]: 0 }); // the joint that was moving stops; the finger now drives this one | |
| 1233 | picked = id; | |
| 1234 | try { localStorage.setItem("so101.joint", String(id)); } catch {} | |
| 1235 | for (const b of $("chips").children) { const on = Number(b.dataset.id) === id; b.setAttribute("aria-checked", String(on)); b.tabIndex = on ? 0 : -1; } | |
| 1236 | $("oneName").textContent = LABEL[id][1]; | |
| 1237 | $("nudgeUp").querySelector("small").textContent = ENDS[id][0]; $("nudgeDown").querySelector("small").textContent = ENDS[id][1]; | |
| 1238 | $("nudgeUp").setAttribute("aria-label", `${LABEL[id][1]} a step ${ENDS[id][0]}`); $("nudgeDown").setAttribute("aria-label", `${LABEL[id][1]} a step ${ENDS[id][1]}`); | |
| 1239 | lever.setAttribute("aria-label", `${LABEL[id][1]}: push ${ENDS[id][0]} or ${ENDS[id][1]}, let go to stop`); | |
| 1240 | if (status) renderTouch(status); | |
| 1241 | } | |
| 1242 | $("chips").replaceChildren(...Object.entries(LABEL).map(([id, [short, long]]) => { | |
| 1243 | const b = el("button", { type: "button", onclick: () => { tap(); pickJoint(Number(id)); } }, el("b", { textContent: short }), el("small", { textContent: long })); | |
| 1244 | b.dataset.id = id; b.setAttribute("role", "radio"); b.setAttribute("aria-label", `${short} ${long}`); | |
| 1245 | return b; | |
| 1246 | })); | |
| 1247 | $("chips").addEventListener("keydown", (e) => { | |
| 1248 | const d = e.key === "ArrowRight" || e.key === "ArrowDown" ? 1 : e.key === "ArrowLeft" || e.key === "ArrowUp" ? -1 : 0; | |
| 1249 | if (!d) return; | |
| 1250 | e.preventDefault(); pickJoint(((picked - 1 + d + 6) % 6) + 1); $("chips").children[picked - 1]?.focus(); | |
| 1251 | }); | |
| 1252 | pickJoint(picked); | |
| 1253 | /** Where the picked joint is: degrees from the middle of its range, the gripper as how far open; the green bar on the lever likewise. */ | |
| 1254 | function renderTouch(d) { | |
| 1255 | const j = d.joints.find((x) => x.id === picked), l = limits[picked]; | |
| 1256 | if (!j || j.position == null || !l) { $("onePos").textContent = "—"; return; } | |
| 1257 | const share = (j.position - l.min) / (l.max - l.min); | |
| 1258 | const up = UP[picked] > 0 ? share : 1 - share; // the bar fills toward the lever's top end | |
| 1259 | lever.style.setProperty("--at", String(Math.max(0, Math.min(1, up)).toFixed(3))); | |
| 1260 | $("onePos").textContent = picked === 6 ? `${Math.round(share * 100)}% open` : `${(((j.position - mid(picked)) * 360) / 4096).toFixed(0)}°`; | |
| 1261 | } | |
| 1262 | bindSurface(lever, driver, "lever", (_, y) => ({ [picked]: shape(y) * UP[picked] }), { | |
| 1263 | haptic: tap, | |
| 1264 | show: (_, y) => { | |
| 1265 | $("leverKnob").style.transform = `translateY(${(-y * (lever.clientHeight / 2 - 38)).toFixed(1)}px)`; | |
| 1266 | lever.setAttribute("aria-valuenow", String(Math.round(y * 100))); | |
| 1267 | }, | |
| 1268 | }); | |
| 1269 | // ▲ ▼: a tap is one step, a hold jogs at half speed; arrow keys on the lever step too. | |
| 1270 | const step = () => Math.max(5, Math.round((Number($("rate").value) || 300) / 15)); | |
| 1271 | for (const b of [$("nudgeUp"), $("nudgeDown")]) { | |
| 1272 | let pointer = null, timer = null; | |
| 1273 | const dir = () => Number(b.dataset.nudge) * UP[picked]; | |
| 1274 | b.addEventListener("pointerdown", (e) => { | |
| 1275 | if (pointer !== null) return; | |
| 1276 | e.preventDefault(); pointer = e.pointerId; | |
| 1277 | try { b.setPointerCapture(e.pointerId); } catch {} | |
| 1278 | tap(); b.classList.add("held"); | |
| 1279 | driver.nudge(picked, dir() * step()); | |
| 1280 | timer = setTimeout(() => { timer = null; if (pointer !== null) driver.hold("nudge", { [picked]: dir() * 0.5 }); }, 350); | |
| 1281 | }); | |
| 1282 | const up = (e) => { if (e.pointerId !== pointer) return; pointer = null; clearTimeout(timer); b.classList.remove("held"); driver.release("nudge"); }; | |
| 1283 | for (const ev of ["pointerup", "pointercancel", "lostpointercapture"]) b.addEventListener(ev, up); | |
| 1284 | b.addEventListener("keydown", (e) => { if (e.key === "Enter" || e.key === " ") { e.preventDefault(); e.stopPropagation(); driver.nudge(picked, dir() * step()); } }); | |
| 1285 | b.addEventListener("contextmenu", (e) => e.preventDefault()); | |
| 1286 | } | |
| 1287 | lever.addEventListener("keydown", (e) => { | |
| 1288 | const d = e.key === "ArrowUp" ? 1 : e.key === "ArrowDown" ? -1 : 0; | |
| 1289 | if (d) { e.preventDefault(); driver.nudge(picked, d * UP[picked] * step()); } | |
| 1290 | }); | |
| 1195 | // ---------------------------------------------------------------- the joint tray (src/ui/drive.js) | |
| 1196 | // Upright: every joint's row sits in a tray under the model that folds to one line; elsewhere the rows always show. | |
| 1197 | bindTray($("trayToggle"), $("tray"), (() => { try { return localStorage; } catch { return null; } })()); | |
| 1291 | 1198 | |
| 1292 | 1199 | // Keep the screen on while it is your turn: a phone that dims mid-drive is a hand off the controls. |
| 1293 | 1200 | let wakeLock = null, waking = false, wantAwake = false; |
| @@ -1400,9 +1307,8 @@ | ||
| 1400 | 1307 | shell.classList.toggle("mayDrive", mayDrive()); |
| 1401 | 1308 | $("t-admin").hidden = !owner; |
| 1402 | 1309 | $("t-chat").hidden = $("t-queue").hidden = !relayed; |
| 1403 | for (const id of ["joints", "poses", "drive", "touch"]) $(id).inert = !can; | |
| 1310 | for (const id of ["joints", "poses", "drive"]) $(id).inert = !can; | |
| 1404 | 1311 | shell.classList.toggle("driving", can); |
| 1405 | if (!can) driver.releaseAll(); | |
| 1406 | 1312 | wake(can); |
| 1407 | 1313 | $("stop").disabled = !(mayDrive() || owner); |
| 1408 | 1314 | $("stop").title = $("stop").disabled ? "Stop: the driver and the admins can press it" : "Stop (space or Esc): halt everything, hold with torque, until someone resumes"; |
| @@ -1456,6 +1362,8 @@ | ||
| 1456 | 1362 | // ---------------------------------------------------------------- live status, sliders and jog |
| 1457 | 1363 | const joints = $("joints"); |
| 1458 | 1364 | const built = new Map(); |
| 1365 | /** A phone's compact row has room for a word, not "shoulder_pan". */ | |
| 1366 | const SHORT = { 1: "Base", 2: "Shoulder", 3: "Elbow", 4: "Wrist", 5: "Roll", 6: "Grip" }; | |
| 1459 | 1367 | let sliding = null; |
| 1460 | 1368 | // Lazy: the first view's budget is spent. |
| 1461 | 1369 | let intent = { set() {}, release() {}, clear() {}, target: () => null }, whyNot = () => null, marks = () => {}; |
| @@ -1466,9 +1374,10 @@ | ||
| 1466 | 1374 | if (!el) { |
| 1467 | 1375 | const lim = limits[j.id] ?? { min: 0, max: 4095 }; |
| 1468 | 1376 | const div = document.createElement("div"); div.className = "j"; |
| 1469 | div.innerHTML = `<b></b><div class="v"><span class="pos"></span><small class="ld"></small><small class="t"></small></div> | |
| 1377 | div.innerHTML = `<b><span class="long"></span><span class="short" aria-hidden="true"></span></b><div class="v"><span class="pos"></span><small class="ld"></small><small class="t"></small></div> | |
| 1470 | 1378 | <div class="slide"><span class="jog"><button data-d="-1" aria-label="less">−</button></span><span class="rail"><input type="range" min="${lim.min + 30}" max="${lim.max - 30}" step="1"></span><span class="jog"><button data-d="1" aria-label="more">+</button></span></div>`; |
| 1471 | div.querySelector("b").textContent = j.joint ?? String(j.id); | |
| 1379 | div.querySelector(".long").textContent = j.joint ?? String(j.id); | |
| 1380 | div.querySelector(".short").textContent = SHORT[j.id] ?? j.joint ?? String(j.id); | |
| 1472 | 1381 | const range = div.querySelector("input"); |
| 1473 | 1382 | range.setAttribute("aria-label", j.joint ?? `joint ${j.id}`); |
| 1474 | 1383 | range.addEventListener("pointerdown", () => { sliding = j.id; stream.open(); }); |
| @@ -1537,10 +1446,7 @@ | ||
| 1537 | 1446 | { |
| 1538 | 1447 | const was = $("why").textContent; |
| 1539 | 1448 | status = d; |
| 1540 | driver.seed(d.joints.filter((j) => j.position != null).map((j) => [j.id, j.position])); | |
| 1541 | 1449 | if ((explainLock() ?? "") !== was) applyLock(); |
| 1542 | // Stopped, reconnecting or no longer this person's turn: a held thumb lets go, and its zero goes out now. | |
| 1543 | if (!canDrive({ mayDrive: mayDrive(), estop: d.estop, held: d.held, reconnecting: d.reconnecting, limits })) driver.releaseAll(); | |
| 1544 | 1450 | // The round trip to the arm, which every command also makes: shown once it is long enough to feel. |
| 1545 | 1451 | const lag = [...rtts].sort((a, b) => a - b)[rtts.length >> 1] ?? 0; |
| 1546 | 1452 | $("lag").hidden = rtts.length < 3 || lag <= 250; $("lag").textContent = `· ${Math.round(lag)} ms lag`; |
| @@ -1554,7 +1460,7 @@ | ||
| 1554 | 1460 | $("stop").classList.toggle("on", Boolean(d.estop || d.held)); $("stop").textContent = d.estop || d.held ? "Stopped" : "Stop"; |
| 1555 | 1461 | $("record").textContent = d.driving === "record" ? "■ Stop recording" : "● Record"; |
| 1556 | 1462 | for (const id of ["hold", "releaseBtn", "play"]) $(id).disabled = d.driving !== null; |
| 1557 | if (tab === "controls") { render(d); renderTouch(d); } | |
| 1463 | if (tab === "controls") render(d); | |
| 1558 | 1464 | banner(d); applyModel(d); |
| 1559 | 1465 | $("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>`; |
| 1560 | 1466 | } |
new above
offline