Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
The thumb sticks and the reach joystick are gone from every layout, with their code and tests #328
Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.
smith/arm:smith/278-the-thumb-sticks-and-the-reach-joystick → main 4 commits fork
7 files
changed +34
−329
Software/README.md +9 −18
| @@ -614,17 +614,12 @@ metrics and 3D calibration). | ||
| 614 | 614 | up or down, and the further you push the faster it goes. Let go and it |
| 615 | 615 | stops. ▲/▼ beside the lever nudge one step per tap and jog while held. |
| 616 | 616 | Under them come the poses, Hold, Record and the rest. This layout is for |
| 617 | screens up to 640 px wide. A tablet held upright gets the joystick and | |
| 618 | sliders of the wider screens. From a keyboard, arrows on the chips pick | |
| 617 | screens up to 640 px wide. Wider screens, a tablet | |
| 618 | held upright included, get the sliders. From a keyboard, arrows on the chips pick | |
| 619 | 619 | the joint and ↑/↓ on the lever step it. |
| 620 | - Controls, phone on its side: the sheet tucks away when your turn starts, | |
| 621 | and two thumb sticks sit over the picture. The left stick moves J1 | |
| 622 | sideways and J2 up/down. The right stick moves J4 sideways and J3 up/down, | |
| 623 | and **Wrist** swaps it to J5 roll and the gripper. **Open** and **Close** | |
| 624 | between the sticks are press-and-hold. **Show** brings the sheet back. | |
| 625 | The sticks and the grip buttons take touch and a mouse only. With a | |
| 626 | keyboard or a screen reader, **Show** brings back the sheet, and its joint | |
| 627 | sliders and ±-buttons drive every joint, the gripper included. | |
| 620 | - Controls, phone on its side: the joint sliders and ±-buttons drive every | |
| 621 | joint, the gripper included; **Hide** tucks the sheet away to show the | |
| 622 | picture, and **Show** brings it back. | |
| 628 | 623 | - How touch driving works (`src/ui/drive.js`): a finger's deflection is a |
| 629 | 624 | speed, with a dead band in the middle, never a position. Each finger is |
| 630 | 625 | tracked by its own pointer, so two thumbs drive two sets of joints |
| @@ -642,12 +637,8 @@ metrics and 3D calibration). | ||
| 642 | 637 | screen. The screen stays awake during your turn (Screen Wake Lock), and |
| 643 | 638 | the status line shows the round trip to the arm once it passes 250 ms. |
| 644 | 639 | On a phone the strip carries one stop button, the E-stop. |
| 645 | - Controls, wider screens: a joystick moves the gripper. Up reaches out, | |
| 646 | sideways swings around the base, and ▲/▼ raise and lower it. The page | |
| 647 | solves that onto pan, lift and elbow against the URDF | |
| 648 | (`src/ui/reach.js`), and the server checks every frame against the zone | |
| 649 | as usual. Below the joystick come the joint sliders with hold-to-jog −/+, | |
| 650 | the poses, and the rest. | |
| 640 | - Controls, wider screens: the joint sliders with hold-to-jog −/+, the poses, | |
| 641 | and the rest. The server checks every frame against the zone. | |
| 651 | 642 | - Chat: the page follows the visual viewport. The keyboard shrinks the |
| 652 | 643 | sheet and the view, and never covers the box or the last message. |
| 653 | 644 | Long-press a message (or right-click it) to delete it, or mute or ban its |
| @@ -661,8 +652,8 @@ metrics and 3D calibration). | ||
| 661 | 652 | the page follows the system's light or dark mode, and nothing moves for |
| 662 | 653 | people who ask for reduced motion. The bottom-right corner belongs to the |
| 663 | 654 | forge pill, so no control sits under it. |
| 664 | - Mobile data: the first view needs no three.js. The model and the | |
| 665 | joystick arrive after it. The relay gzips the page, three.js, the URDF | |
| 655 | - Mobile data: the first view needs no three.js. The model arrives | |
| 656 | after it. The relay gzips the page, three.js, the URDF | |
| 666 | 657 | and the meshes (`src/compress.ts`). The page's modules are cached for |
| 667 | 658 | good under a name that changes with their content. On a slow link, a |
| 668 | 659 | slow `/status` round trip or the browser's Save-Data, the camera drops |
Software/so101/src/landscape-layout.test.ts +7 −11
| @@ -83,15 +83,6 @@ describe("a phone on its side", () => { | ||
| 83 | 83 | expect(sideways(css, "#strip::before")["backdrop-filter"]).toContain("blur"); |
| 84 | 84 | }); |
| 85 | 85 | |
| 86 | test("the joint sticks overlay the lower corners, inside the insets, while driving with the sheet away", () => { | |
| 87 | expect(sideways(css, "#shell.driving.bare #sticks").display).toBe("block"); | |
| 88 | const stick = sideways(css, ".stick"); | |
| 89 | expect(stick.position).toBe("absolute"); | |
| 90 | expect(stick.bottom).toContain("var(--safe-b)"); | |
| 91 | expect(sideways(css, "#stickL").left).toContain("var(--safe-l)"); | |
| 92 | expect(sideways(css, "#stickR").right).toContain("var(--safe-r)"); | |
| 93 | }); | |
| 94 | ||
| 95 | 86 | test("the strip and the view buttons leave STOP's corner free", () => { |
| 96 | 87 | expect(sideways(css, "#shell.bare #strip")["max-width"]).toContain("var(--stop-w)"); |
| 97 | 88 | const top = sideways(css, "#shell.driving.bare #top"); |
| @@ -100,9 +91,14 @@ describe("a phone on its side", () => { | ||
| 100 | 91 | expect(sideways(css, "#sheet")["padding-top"]).toContain("var(--safe-t)"); |
| 101 | 92 | }); |
| 102 | 93 | |
| 103 | test("a turn that starts on its side puts the sheet away, so the sticks come up", () => { | |
| 94 | test("a turn that starts on its side leaves the sheet up: the sheet is all there is to drive with", () => { | |
| 104 | 95 | const js = [...html.matchAll(/<script\b[^>]*>([\s\S]*?)<\/script>/gi)].map((m) => m[1]!).join("\n"); |
| 105 | expect(js).toMatch(/if \(can && !shell\.classList\.contains\("driving"\) && sideways\.matches\) setBare\(true\);\s*shell\.classList\.toggle\("driving", can\);/); | |
| 96 | expect(js).not.toMatch(/sideways\.matches\) setBare\(true\)/); | |
| 97 | }); | |
| 98 | ||
| 99 | test("the served page balances its div tags before the first script", () => { | |
| 100 | const body = html.slice(0, html.indexOf("<script")); | |
| 101 | expect((body.match(/<div\b/g) ?? []).length).toBe((body.match(/<\/div>/g) ?? []).length); | |
| 106 | 102 | }); |
| 107 | 103 | }); |
| 108 | 104 |
Software/so101/src/relay.test.ts +3 −3
| @@ -161,17 +161,17 @@ describe("relay", () => { | ||
| 161 | 161 | expect(page.headers.get("content-encoding")).toBe("gzip"); |
| 162 | 162 | expect(page.headers.get("x-content-type-options")).toBe("nosniff"); |
| 163 | 163 | const html = gunzipSync(Buffer.from(await page.arrayBuffer())).toString(); |
| 164 | const module = /\/ui\/[0-9a-f]{12}\/reach\.js/.exec(html)?.[0]; | |
| 164 | const module = /\/ui\/[0-9a-f]{12}\/camera-plan\.js/.exec(html)?.[0]; | |
| 165 | 165 | expect(module).toBeDefined(); |
| 166 | 166 | const js = await raw(module!); |
| 167 | 167 | expect(js.status).toBe(200); |
| 168 | 168 | expect(js.headers.get("cache-control")).toContain("immutable"); |
| 169 | 169 | expect(js.headers.get("content-encoding")).toBe("gzip"); |
| 170 | expect(gunzipSync(Buffer.from(await js.arrayBuffer())).toString()).toContain("export function reachStep"); | |
| 170 | expect(gunzipSync(Buffer.from(await js.arrayBuffer())).toString()).toContain("export function cameraPlan"); | |
| 171 | 171 | // A client that asks for nothing compressed gets the plain bytes. |
| 172 | 172 | const plain = await fetch(`${base}${module}`, { headers: { "accept-encoding": "identity" }, decompress: false } as RequestInit); |
| 173 | 173 | expect(plain.headers.get("content-encoding")).toBeNull(); |
| 174 | expect(await plain.text()).toContain("export function reachStep"); | |
| 174 | expect(await plain.text()).toContain("export function cameraPlan"); | |
| 175 | 175 | }); |
| 176 | 176 | |
| 177 | 177 | test("set: status, poses and /follow go to the arm server; the camera's bytes come through", async () => { |
Software/so101/src/server.test.ts +4 −4
| @@ -163,7 +163,7 @@ describe("viewer page", () => { | ||
| 163 | 163 | } |
| 164 | 164 | expect(bytes).toBeLessThan(52_000); |
| 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", "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); | |
| 166 | for (const lazy of ["three", "/leader-map.js", "leader-calib.js", "calibration.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 | 168 | expect(script).toMatch(/import\("\/ui\/[0-9a-f]{12}\/leader-calib\.js"\)/); |
| 169 | 169 | expect(script).toMatch(/import\("\/ui\/[0-9a-f]{12}\/calibration\.js"\)/); |
| @@ -183,7 +183,7 @@ describe("viewer page", () => { | ||
| 183 | 183 | const version = path!.split("/")[2]; |
| 184 | 184 | const css = await fetch(`${base}/ui/${version}/tabs.css`); |
| 185 | 185 | expect([css.status, css.headers.get("content-type")]).toEqual([200, "text/css"]); |
| 186 | expect((await fetch(`${base}/ui/${version}/reach.test.ts`)).status).toBe(404); | |
| 186 | expect((await fetch(`${base}/ui/${version}/drive.test.ts`)).status).toBe(404); | |
| 187 | 187 | expect((await fetch(`${base}/ui/${version}/nothing.js`)).status).toBe(404); |
| 188 | 188 | expect((await fetch(`${base}/ui/${version}/..%2Fserver.ts`)).status).toBe(404); |
| 189 | 189 | }); |
| @@ -230,8 +230,8 @@ describe("viewer page", () => { | ||
| 230 | 230 | // The one-joint lever shows only on a phone held upright, so its rules live in the phone's stylesheet. |
| 231 | 231 | const tabsCss = await (await fetch(`${base}${/\/ui\/[0-9a-f]{12}\/tabs\.css/.exec(page)![0]}`)).text(); |
| 232 | 232 | expect(tabsCss).toMatch(/#lever \{[^}]*touch-action: none/); |
| 233 | expect(page).toMatch(/\.stick \{[^}]*touch-action: none/); | |
| 234 | expect(page).toMatch(/#grips button \{[^}]*min-height: (7[2-9]|[89]\d)px/); | |
| 233 | // The thumb sticks and the reach joystick are gone: no element, rule or script for them. | |
| 234 | expect(page).not.toMatch(/id="(sticks|stickL|stickR|reach|pad|swap|grips)"|reach\.js|reachStep|\.stick\b/); | |
| 235 | 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); |
Software/so101/src/ui/reach.js deleted −79
| @@ -1,79 +0,0 @@ | ||
| 1 | /** | |
| 2 | * The touch joystick's arithmetic: move the gripper, not a joint. Plain ESM, served to the page at /ui/ and | |
| 3 | * imported by the tests. The page owns the arm's geometry (the URDF in three.js) and hands it in as `tip`: | |
| 4 | * joint angles in, the gripper's position in the base frame (metres, z up) out. | |
| 5 | */ | |
| 6 | ||
| 7 | /** | |
| 8 | * The joystick and the height buttons as a displacement of the gripper, in the base frame. | |
| 9 | * Up on the pad reaches away from the base, sideways swings around it; `lift` is -1, 0 or 1. | |
| 10 | * `tip` is where the gripper is now; the step is `speed` m/s for `dt` seconds at full deflection. | |
| 11 | * @param {{ x: number, y: number, lift: number }} stick | |
| 12 | * @param {number[]} tip | |
| 13 | * @param {number} speed | |
| 14 | * @param {number} dt | |
| 15 | * @returns {number[]} | |
| 16 | */ | |
| 17 | export function stickToDelta(stick, tip, speed, dt) { | |
| 18 | const [tx = 0, ty = 0] = tip; | |
| 19 | const r = Math.hypot(tx, ty); | |
| 20 | // Straight above the base there is no "away": take the base's own forward, +x. | |
| 21 | const fx = r > 1e-3 ? tx / r : 1; | |
| 22 | const fy = r > 1e-3 ? ty / r : 0; | |
| 23 | const x = clampUnit(stick.x), y = clampUnit(stick.y), lift = clampUnit(stick.lift); | |
| 24 | const step = speed * dt; | |
| 25 | // Sideways is the forward direction turned a quarter to the right, seen from above. | |
| 26 | return [(fx * y + fy * x) * step, (fy * y - fx * x) * step, lift * step]; | |
| 27 | } | |
| 28 | ||
| 29 | /** | |
| 30 | * One damped-least-squares step: the joint changes that move the tip by `delta`. | |
| 31 | * The Jacobian comes from `tip` by finite differences, so any chain the page can evaluate works; | |
| 32 | * the damping keeps the step small near a stretched-out or folded-up arm instead of letting it whip. | |
| 33 | * @param {(angles: number[]) => number[]} tip | |
| 34 | * @param {number[]} angles | |
| 35 | * @param {number[]} delta | |
| 36 | * @param {{ h?: number, damping?: number, maxStep?: number }} [options] | |
| 37 | * @returns {number[]} | |
| 38 | */ | |
| 39 | export function reachStep(tip, angles, delta, options = {}) { | |
| 40 | const h = options.h ?? 1e-4, damping = options.damping ?? 0.02, maxStep = options.maxStep ?? 0.08; | |
| 41 | const here = tip(angles); | |
| 42 | const n = angles.length; | |
| 43 | /** @type {number[][]} J[row][joint] */ | |
| 44 | const J = [[], [], []]; | |
| 45 | for (let j = 0; j < n; j++) { | |
| 46 | const moved = angles.slice(); | |
| 47 | moved[j] = (moved[j] ?? 0) + h; | |
| 48 | const there = tip(moved); | |
| 49 | for (let r = 0; r < 3; r++) /** @type {number[]} */ (J[r])[j] = ((there[r] ?? 0) - (here[r] ?? 0)) / h; | |
| 50 | } | |
| 51 | // dq = Jᵀ (J Jᵀ + λ² I)⁻¹ delta | |
| 52 | const A = [0, 1, 2].map((r) => [0, 1, 2].map((c) => dot(/** @type {number[]} */ (J[r]), /** @type {number[]} */ (J[c])) + (r === c ? damping * damping : 0))); | |
| 53 | const w = solve3(A, delta); | |
| 54 | const dq = angles.map((_, j) => w.reduce((s, wr, r) => s + wr * (/** @type {number[]} */ (J[r])[j] ?? 0), 0)); | |
| 55 | // Never more than maxStep radians on any joint in one step. | |
| 56 | const biggest = Math.max(...dq.map(Math.abs)); | |
| 57 | return biggest > maxStep ? dq.map((d) => (d * maxStep) / biggest) : dq; | |
| 58 | } | |
| 59 | ||
| 60 | /** @param {number} v */ | |
| 61 | const clampUnit = (v) => (Number.isFinite(v) ? Math.max(-1, Math.min(1, v)) : 0); | |
| 62 | /** @param {number[]} a @param {number[]} b */ | |
| 63 | const dot = (a, b) => a.reduce((s, v, i) => s + v * (b[i] ?? 0), 0); | |
| 64 | ||
| 65 | /** | |
| 66 | * Cramer's rule for a 3×3 system; a singular matrix gives no motion. | |
| 67 | * @param {number[][]} A | |
| 68 | * @param {number[]} b | |
| 69 | */ | |
| 70 | function solve3(A, b) { | |
| 71 | /** @param {number[][]} m */ | |
| 72 | const det = (m) => { | |
| 73 | const [[a = 0, b = 0, c = 0] = [], [d = 0, e = 0, f = 0] = [], [g = 0, h = 0, i = 0] = []] = m; | |
| 74 | return a * (e * i - f * h) - b * (d * i - f * g) + c * (d * h - e * g); | |
| 75 | }; | |
| 76 | const d = det(A); | |
| 77 | if (!Number.isFinite(d) || Math.abs(d) < 1e-12) return [0, 0, 0]; | |
| 78 | return [0, 1, 2].map((c) => det(A.map((row, r) => row.map((v, k) => (k === c ? b[r] ?? 0 : v)))) / d); | |
| 79 | } |
Software/so101/src/ui/reach.test.ts deleted −58
| @@ -1,58 +0,0 @@ | ||
| 1 | import { describe, expect, test } from "bun:test"; | |
| 2 | import { reachStep, stickToDelta } from "./reach.js"; | |
| 3 | ||
| 4 | // A yaw joint and a two-link planar arm on top of it: the SO-101's pan, shoulder lift and elbow, roughly. | |
| 5 | const L1 = 0.116, L2 = 0.135; | |
| 6 | const tip = ([pan = 0, lift = 0, elbow = 0]: number[]) => { | |
| 7 | const reach = L1 * Math.cos(lift) + L2 * Math.cos(lift + elbow); | |
| 8 | const z = L1 * Math.sin(lift) + L2 * Math.sin(lift + elbow); | |
| 9 | return [reach * Math.cos(pan), reach * Math.sin(pan), z]; | |
| 10 | }; | |
| 11 | const distance = (a: number[], b: number[]) => Math.hypot(...a.map((v, i) => v - b[i]!)); | |
| 12 | ||
| 13 | describe("the joystick as a displacement", () => { | |
| 14 | test("up on the pad reaches away from the base, whichever way the arm faces", () => { | |
| 15 | const [dx, dy, dz] = stickToDelta({ x: 0, y: 1, lift: 0 }, [0, 0.2, 0.1], 0.05, 0.1); | |
| 16 | expect(dx).toBeCloseTo(0); | |
| 17 | expect(dy).toBeCloseTo(0.005); | |
| 18 | expect(dz).toBe(0); | |
| 19 | }); | |
| 20 | ||
| 21 | test("right on the pad swings clockwise seen from above, and lift is straight up", () => { | |
| 22 | const [dx, dy, dz] = stickToDelta({ x: 1, y: 0, lift: 1 }, [0.2, 0, 0.1], 0.05, 0.1); | |
| 23 | expect(dx).toBeCloseTo(0); | |
| 24 | expect(dy).toBeCloseTo(-0.005); | |
| 25 | expect(dz).toBeCloseTo(0.005); | |
| 26 | }); | |
| 27 | ||
| 28 | test("a deflection past the edge or a broken number is clamped, never amplified", () => { | |
| 29 | const [dx, dy, dz] = stickToDelta({ x: 0, y: 9, lift: Number.NaN }, [0.2, 0, 0], 0.05, 0.1); | |
| 30 | expect(dx).toBeCloseTo(0.005); | |
| 31 | expect(dy).toBeCloseTo(0); | |
| 32 | expect(dz).toBe(0); | |
| 33 | }); | |
| 34 | }); | |
| 35 | ||
| 36 | describe("a reach step", () => { | |
| 37 | test("moves the tip the way it was asked, and the error shrinks step by step", () => { | |
| 38 | let angles = [0.3, 0.6, -1.2]; | |
| 39 | const start = tip(angles); | |
| 40 | const goal = [start[0]! + 0.03, start[1]! - 0.02, start[2]! + 0.02]; | |
| 41 | let before = distance(start, goal); | |
| 42 | for (let i = 0; i < 20; i++) { | |
| 43 | const here = tip(angles); | |
| 44 | const delta = goal.map((g, k) => g - here[k]!); | |
| 45 | angles = angles.map((a, k) => a + reachStep(tip, angles, delta)[k]!); | |
| 46 | const after = distance(tip(angles), goal); | |
| 47 | expect(after).toBeLessThanOrEqual(before + 1e-9); | |
| 48 | before = after; | |
| 49 | } | |
| 50 | expect(before).toBeLessThan(0.001); | |
| 51 | }); | |
| 52 | ||
| 53 | test("a stretched-out arm asked to reach further barely moves instead of whipping", () => { | |
| 54 | const angles = [0, 0, 0]; // straight out: no joint can take the tip further away | |
| 55 | const dq = reachStep(tip, angles, [0.05, 0, 0]); | |
| 56 | for (const d of dq) expect(Math.abs(d)).toBeLessThanOrEqual(0.08); | |
| 57 | }); | |
| 58 | }); |
Software/so101/src/viewer.html +11 −156
| @@ -156,14 +156,6 @@ | ||
| 156 | 156 | #forgeSignin { color: var(); font-size: 13px; align-self: flex-start; } |
| 157 | 157 | |
| 158 | 158 | /* controls */ |
| 159 | #reach { display: grid; grid-template-columns: auto 1fr; gap: 12px; align-items: center; } | |
| 160 | #pad { position: relative; width: min(46vw, 180px); aspect-ratio: 1; border-radius: 50%; background: radial-gradient(circle, var() 0 58%, var() 59%); border: 1px solid var(); touch-action: none; -webkit-user-select: none; user-select: none; } | |
| 161 | #pad::before, #pad::after { content: ""; position: absolute; background: var(); left: 50%; top: 12%; bottom: 12%; width: 1px; } #pad::after { left: 12%; right: 12%; top: 50%; bottom: auto; width: auto; height: 1px; } | |
| 162 | #knob { position: absolute; left: 50%; top: 50%; width: 38%; aspect-ratio: 1; margin: -19% 0 0 -19%; border-radius: 50%; background: var(); box-shadow: 0 2px 8px rgba(0,0,0,.35); transition: transform .12s ease-out; pointer-events: none; z-index: 1; } | |
| 163 | #pad.held #knob { transition: none; } | |
| 164 | #reach .side { display: grid; gap: 8px; } | |
| 165 | #reach .side button { width: 100%; touch-action: none; -webkit-user-select: none; user-select: none; } | |
| 166 | #reach .hint { grid-column: 1 / -1; margin: 0; color: var(); font-size: 12px; } | |
| 167 | 159 | #joints { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 6px 14px; } |
| 168 | 160 | .j { display: grid; grid-template-columns: 1fr auto; grid-template-areas: "name val" "slide slide"; align-items: center; column-gap: 6px; padding: 4px 6px; border-radius: 10px; } |
| 169 | 161 | .j.jam { background: var(); } .j.drag { background: var(); } |
| @@ -178,22 +170,9 @@ | ||
| 178 | 170 | |
| 179 | 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) */ |
| 180 | 172 | #touch { display: none; gap: 10px; } |
| 181 | #lever.end, .stick.end { box-shadow: 0 0 0 3px var(); } | |
| 173 | #lever.end { box-shadow: 0 0 0 3px var(); } | |
| 182 | 174 | #lag { color: #f2cc60; font-variant-numeric: tabular-nums; } |
| 183 | 175 | #chat .earlier { display: block; margin: 2px auto 8px; font-size: 13px; } |
| 184 | /* on a phone on its side: two thumb sticks over the picture, the gripper between them */ | |
| 185 | #sticks { display: none; position: absolute; inset: 0; z-index: 6; pointer-events: none; : min(40vh, 164px); } | |
| 186 | #sticks > * { pointer-events: auto; } | |
| 187 | .stick { position: absolute; bottom: calc(14px + var()); width: var(); height: var(); border-radius: 50%; background: radial-gradient(circle, rgba(255,255,255,.08) 0 54%, rgba(255,255,255,.03) 55%); border: 1px solid rgba(255,255,255,.24); touch-action: none; -webkit-user-select: none; user-select: none; } | |
| 188 | #stickL { left: calc(18px + var()); } #stickR { right: calc(var() + var()); } | |
| 189 | .stick .knob { position: absolute; left: 50%; top: 50%; width: 42%; height: 42%; margin: -21% 0 0 -21%; border-radius: 50%; background: rgba(56,139,253,.8); box-shadow: 0 2px 10px rgba(0,0,0,.45); transition: transform .14s ease-out; pointer-events: none; } | |
| 190 | .stick.held { border-color: #388bfd; } .stick.held .knob { background: #388bfd; transition: none; } | |
| 191 | .stick .cap { position: absolute; left: -20px; right: -20px; bottom: 100%; margin-bottom: 4px; text-align: center; font-size: 11px; color: #e8eaed; text-shadow: 0 1px 2px #000; pointer-events: none; white-space: nowrap; } | |
| 192 | #sticks button { background: rgba(0,0,0,.55); border-color: rgba(255,255,255,.24); color: #e8eaed; touch-action: none; -webkit-user-select: none; user-select: none; } | |
| 193 | #sticks button.held { background: #388bfd; border-color: #388bfd; } | |
| 194 | #swap { position: absolute; right: calc(var() + var()); bottom: calc(var() + 40px + var()); min-height: 40px; font-size: 13px; } | |
| 195 | #grips { position: absolute; left: 50%; bottom: calc(14px + var()); transform: translateX(-50%); display: flex; gap: 10px; } | |
| 196 | #grips button { min-width: 80px; min-height: 72px; font-weight: 650; } | |
| 197 | 176 | |
| 198 | 177 | /* admin */ |
| 199 | 178 | .card { display: grid; gap: 10px; padding: 12px; border: 1px solid var(); border-radius: 12px; background: var(); } |
| @@ -272,15 +251,13 @@ | ||
| 272 | 251 | #banner { top: 64px; } |
| 273 | 252 | #msg { bottom: 52px; } |
| 274 | 253 | .tab { padding-top: 8px; } |
| 275 | #pad { width: min(30vh, 150px); } | |
| 276 | 254 | } |
| 277 | @media (max-width: 640px) and (orientation: portrait) { #touch { display: grid; } #reach, #joints { display: none ; } } | |
| 255 | @media (max-width: 640px) and (orientation: portrait) { #touch { display: grid; } #joints { display: none ; } } | |
| 278 | 256 | /* One STOP in every layout; it holds until Resume. Sideways it is fixed top-right (the strip's blur sits on ::before |
| 279 | 257 | so it does not pin STOP to the strip). */ |
| 280 | 258 | @media (orientation: landscape) and (max-height: 500px) { |
| 281 | 259 | #stop { position: fixed; z-index: 9; top: calc(var(, 0px) + 6px + var()); right: calc(6px + var()); width: var(); } |
| 282 | 260 | #sheet { padding-top: calc(68px + var()); } |
| 283 | #shell.driving.bare #sticks { display: block; } | |
| 284 | 261 | #shell.driving.bare #top { top: calc(6px + var()); bottom: auto; left: auto; right: calc(var() + 14px + var()); max-width: calc(42% - var() - 8px); justify-content: flex-end; } |
| 285 | 262 | #shell.driving.bare #strip { max-width: calc(58% - 18px - var()); } |
| 286 | 263 | #shell.driving.bare #msg { bottom: auto; top: 110px; } |
| @@ -306,12 +283,6 @@ | ||
| 306 | 283 | <div id="banner" role="alert"></div> |
| 307 | 284 | <div id="msg" role="status"></div> |
| 308 | 285 | <div id="refusal" role="status"></div> |
| 309 | <div id="sticks" role="group" aria-label="Thumb sticks"> | |
| 310 | <div class="stick" id="stickL" role="group" aria-label="Left stick: sideways turns the base, up and down lifts the shoulder"><span class="cap">J1 base ↔ · J2 shoulder ↕</span><span class="knob"></span></div> | |
| 311 | <div class="stick" id="stickR" role="group" aria-label="Right stick"><span class="cap" id="capR"></span><span class="knob"></span></div> | |
| 312 | <button id="swap" type="button" aria-pressed="false">Wrist</button> | |
| 313 | <div id="grips" role="group" aria-label="Gripper"><button id="gripOpen" type="button">Open</button><button id="gripClose" type="button">Close</button></div> | |
| 314 | </div> | |
| 315 | 286 | </div> |
| 316 | 287 | |
| 317 | 288 | <section id="strip" aria-label="Who drives" aria-live="polite"> |
| @@ -354,16 +325,6 @@ | ||
| 354 | 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> |
| 355 | 326 | </div> |
| 356 | 327 | </section> |
| 357 | <div id="reach" hidden> | |
| 358 | <div id="pad" role="application" aria-label="Move the gripper: up reaches out, down pulls in, sideways swings around the base"><span id="knob"></span></div> | |
| 359 | <div class="side"> | |
| 360 | <button data-lift="1" aria-label="Gripper up">▲ Up</button> | |
| 361 | <button data-lift="-1" aria-label="Gripper down">▼ Down</button> | |
| 362 | <button data-grip="1">Open</button> | |
| 363 | <button data-grip="-1">Close</button> | |
| 364 | </div> | |
| 365 | <p class="hint">Hold the pad to move the gripper: up reaches out, sideways swings around the base. The jog speed sets how fast.</p> | |
| 366 | </div> | |
| 367 | 328 | <p id="why" role="status"></p> |
| 368 | 329 | <div id="joints"></div> |
| 369 | 330 | <div class="row" id="poses"></div> |
| @@ -814,7 +775,7 @@ | ||
| 814 | 775 | $("v3").onclick = () => pick("pane3"); |
| 815 | 776 | |
| 816 | 777 | // ---------------------------------------------------------------- the stream: the page as a leader |
| 817 | // Opened while a slider, a jog button, the joystick or a model joint is held; frames go at most every 33 ms; closed shortly after letting go. | |
| 778 | // Opened while a slider, a jog button or a model joint is held; frames go at most every 33 ms; closed shortly after letting go. | |
| 818 | 779 | // Stamped targets (src/ui/frames.js): while the socket opens only the newest waits, and goes only if still fresh. |
| 819 | 780 | const stream = { |
| 820 | 781 | ws: null, closeTimer: null, halted: false, waiting: null, |
| @@ -908,7 +869,6 @@ | ||
| 908 | 869 | async function stop() { |
| 909 | 870 | stream.halt(); |
| 910 | 871 | driver.releaseAll(); |
| 911 | reachEnd(); | |
| 912 | 872 | stopLeaderDrive("stopped"); |
| 913 | 873 | const r = await api("/stop", {}); |
| 914 | 874 | msg(r.ok ? `STOPPED${r.stopped ? ` ${r.stopped}` : ""}${r.saved ? `; saved recording ${r.saved}` : ""}: the arm holds until you resume` : r.error, true); |
| @@ -1170,10 +1130,10 @@ | ||
| 1170 | 1130 | function load3D() { |
| 1171 | 1131 | if (!modelInfo?.ok) return Promise.resolve(false); |
| 1172 | 1132 | return (model3D ??= (async () => { |
| 1173 | const [three, { STLLoader }, { default: URDFManipulator }, reach, plane] = await Promise.all([ | |
| 1174 | import("three"), import("three/examples/jsm/loaders/STLLoader.js"), import("urdf-loader/manipulator"), import("/ui/__VERSION__/reach.js"), import("/ui/__VERSION__/table.js"), | |
| 1133 | const [three, { STLLoader }, { default: URDFManipulator }, plane] = await Promise.all([ | |
| 1134 | import("three"), import("three/examples/jsm/loaders/STLLoader.js"), import("urdf-loader/manipulator"), import("/ui/__VERSION__/table.js"), | |
| 1175 | 1135 | ]); |
| 1176 | THREE = three; reachMath = reach; tablePlaneZ = plane.tablePlaneZ; | |
| 1136 | THREE = three; tablePlaneZ = plane.tablePlaneZ; | |
| 1177 | 1137 | if (!customElements.get("urdf-viewer")) customElements.define("urdf-viewer", URDFManipulator); |
| 1178 | 1138 | viewer.loadMeshFunc = (path, manager, material, done) => { |
| 1179 | 1139 | new STLLoader(manager).load(path, (geometry) => done(new THREE.Mesh(geometry, material ?? new THREE.MeshPhongMaterial({ color: 0x9aa4ae, shininess: 30 }))), undefined, (err) => done(null, err)); |
| @@ -1204,7 +1164,6 @@ | ||
| 1204 | 1164 | viewer.controls.target.set(0.08, 0.1, 0); |
| 1205 | 1165 | viewer.controls.update(); |
| 1206 | 1166 | viewer.recenter(); |
| 1207 | $("reach").hidden = !tipLink(); | |
| 1208 | 1167 | }); |
| 1209 | 1168 | viewer.addEventListener("manipulate-start", (e) => { dragging = e.detail; stream.open(); }); |
| 1210 | 1169 | viewer.addEventListener("manipulate-end", () => { const id = IDS[dragging]; if (id) intent.release(id, performance.now()); dragging = null; stream.end(); }); |
| @@ -1228,7 +1187,6 @@ | ||
| 1228 | 1187 | for (const j of leaderMirrored() ? [] : d.joints) { |
| 1229 | 1188 | const name = NAMES[j.id]; |
| 1230 | 1189 | if (!name || j.position == null || name === dragging || !limits[j.id]) continue; |
| 1231 | if (reachState.timer && j.id <= 3) continue; // the joystick is ahead of the arm; snapping back would make it stutter | |
| 1232 | 1190 | const want = intent.target(j.id, j.position, performance.now()); |
| 1233 | 1191 | viewer.setJointValue(name, toRad(j.id, want ?? j.position)); |
| 1234 | 1192 | } |
| @@ -1243,86 +1201,6 @@ | ||
| 1243 | 1201 | } |
| 1244 | 1202 | } |
| 1245 | 1203 | |
| 1246 | // ---------------------------------------------------------------- the joystick: move the gripper, not a joint | |
| 1247 | // The pad's deflection becomes a displacement of the gripper (src/ui/reach.js), solved onto pan, lift and elbow. | |
| 1248 | let reachMath = null; | |
| 1249 | const REACH = [1, 2, 3]; | |
| 1250 | const SPEED = { 100: 0.02, 300: 0.05, 800: 0.1 }; // m/s at full deflection, by jog speed | |
| 1251 | const reachState = { stick: { x: 0, y: 0, lift: 0 }, angles: null, timer: null, holders: 0 }; | |
| 1252 | const tipLink = () => viewer.robot?.links?.gripper_frame_link ?? viewer.robot?.links?.gripper_link ?? null; | |
| 1253 | /** Where the gripper would be with pan, lift and elbow at `angles`, in the arm's base frame: the model is posed, read and put back. */ | |
| 1254 | function tipAt(angles) { | |
| 1255 | const robot = viewer.robot, link = tipLink(); | |
| 1256 | const saved = REACH.map((id) => robot.joints[NAMES[id]].jointValue[0]); | |
| 1257 | REACH.forEach((id, i) => robot.joints[NAMES[id]].setJointValue(angles[i])); | |
| 1258 | robot.updateMatrixWorld(true); | |
| 1259 | const p = robot.worldToLocal(link.getWorldPosition(new THREE.Vector3())); | |
| 1260 | REACH.forEach((id, i) => robot.joints[NAMES[id]].setJointValue(saved[i])); | |
| 1261 | robot.updateMatrixWorld(true); | |
| 1262 | return [p.x, p.y, p.z]; | |
| 1263 | } | |
| 1264 | function reachBegin() { | |
| 1265 | if (reachState.timer) { reachState.holders++; return; } | |
| 1266 | if (!viewer.robot || !reachMath || !tipLink()) return msg("the 3D model is still loading", true); | |
| 1267 | const at = REACH.map((id) => status?.joints.find((j) => j.id === id)?.position); | |
| 1268 | if (at.some((p) => p == null) || REACH.some((id) => !limits[id])) return msg("no position for the arm yet", true); | |
| 1269 | reachState.angles = REACH.map((id, i) => toRad(id, at[i])); | |
| 1270 | reachState.holders = 1; | |
| 1271 | stream.open(); | |
| 1272 | reachState.timer = setInterval(() => { | |
| 1273 | const { stick } = reachState; | |
| 1274 | if (!stick.x && !stick.y && !stick.lift) return; | |
| 1275 | const delta = reachMath.stickToDelta(stick, tipAt(reachState.angles), SPEED[$("rate").value] ?? 0.05, 0.05); | |
| 1276 | const dq = reachMath.reachStep(tipAt, reachState.angles, delta); | |
| 1277 | const frame = {}; | |
| 1278 | REACH.forEach((id, i) => { frame[id] = clampTicks(id, toTicks(id, reachState.angles[i] + dq[i])); }); | |
| 1279 | reachState.angles = REACH.map((id) => toRad(id, frame[id])); // a joint at its limit stays there | |
| 1280 | REACH.forEach((id, i) => viewer.setJointValue(NAMES[id], reachState.angles[i])); | |
| 1281 | stream.send(frame); | |
| 1282 | }, 50); | |
| 1283 | } | |
| 1284 | function reachEnd(all = true) { | |
| 1285 | if (!reachState.timer) return; | |
| 1286 | if (!all && --reachState.holders > 0) return; | |
| 1287 | clearInterval(reachState.timer); reachState.timer = null; reachState.holders = 0; | |
| 1288 | reachState.stick = { x: 0, y: 0, lift: 0 }; | |
| 1289 | stream.end(); | |
| 1290 | } | |
| 1291 | { | |
| 1292 | const pad = $("pad"), knob = $("knob"); | |
| 1293 | let pointer = null; | |
| 1294 | const place = (e) => { | |
| 1295 | const r = pad.getBoundingClientRect(), radius = r.width / 2; | |
| 1296 | let x = (e.clientX - r.left - radius) / radius, y = -(e.clientY - r.top - radius) / radius; | |
| 1297 | const length = Math.hypot(x, y); | |
| 1298 | if (length > 1) { x /= length; y /= length; } | |
| 1299 | reachState.stick.x = Math.abs(x) < 0.12 ? 0 : x; reachState.stick.y = Math.abs(y) < 0.12 ? 0 : y; // a dead zone around the middle | |
| 1300 | knob.style.transform = `translate(${x * radius * 0.62}px, ${-y * radius * 0.62}px)`; | |
| 1301 | }; | |
| 1302 | const release = () => { | |
| 1303 | if (pointer === null) return; | |
| 1304 | pointer = null; pad.classList.remove("held"); knob.style.transform = ""; | |
| 1305 | reachState.stick.x = reachState.stick.y = 0; reachEnd(false); | |
| 1306 | }; | |
| 1307 | pad.addEventListener("pointerdown", (e) => { | |
| 1308 | if (pointer !== null) return; | |
| 1309 | e.preventDefault(); pointer = e.pointerId; | |
| 1310 | try { pad.setPointerCapture(e.pointerId); } catch {} | |
| 1311 | pad.classList.add("held"); place(e); reachBegin(); | |
| 1312 | }); | |
| 1313 | pad.addEventListener("pointermove", (e) => { if (e.pointerId === pointer) place(e); }); | |
| 1314 | for (const ev of ["pointerup", "pointercancel", "lostpointercapture"]) pad.addEventListener(ev, (e) => { if (e.pointerId === pointer) release(); }); | |
| 1315 | addEventListener("blur", release); | |
| 1316 | for (const b of document.querySelectorAll("#reach [data-lift]")) { | |
| 1317 | let held = false; | |
| 1318 | const up = () => { if (!held) return; held = false; reachState.stick.lift = 0; reachEnd(false); }; | |
| 1319 | b.addEventListener("pointerdown", (e) => { e.preventDefault(); if (held) return; held = true; try { b.setPointerCapture(e.pointerId); } catch {} reachState.stick.lift = Number(b.dataset.lift); reachBegin(); }); | |
| 1320 | for (const ev of ["pointerup", "pointercancel", "lostpointercapture"]) b.addEventListener(ev, up); | |
| 1321 | b.addEventListener("contextmenu", (e) => e.preventDefault()); | |
| 1322 | } | |
| 1323 | for (const b of document.querySelectorAll("#reach [data-grip]")) jog(b, 6, () => Number(b.dataset.grip)); | |
| 1324 | } | |
| 1325 | ||
| 1326 | 1204 | // ---------------------------------------------------------------- touch driving (src/ui/drive.js) |
| 1327 | 1205 | // Upright: a chip picks the joint, the lever's deflection is its speed, ▲ and ▼ nudge it and jog when held. |
| 1328 | 1206 | const driver = new Driver({ |
| @@ -1338,13 +1216,13 @@ | ||
| 1338 | 1216 | speed: () => Number($("rate").value) || 300, |
| 1339 | 1217 | onStop: (id) => { |
| 1340 | 1218 | navigator.vibrate?.(30); |
| 1341 | for (const e of [$("lever"), $("stickL"), $("stickR")]) { e.classList.add("end"); setTimeout(() => e.classList.remove("end"), 400); } | |
| 1219 | for (const e of [$("lever")]) { e.classList.add("end"); setTimeout(() => e.classList.remove("end"), 400); } | |
| 1342 | 1220 | msg(`${LABEL[id]?.[1] ?? id} is at the end of its range`); |
| 1343 | 1221 | }, |
| 1344 | 1222 | }); |
| 1345 | 1223 | driver.watch(window, document); |
| 1346 | // Turning the phone swaps the controls under the thumbs: whatever was held stops, and on its side the sticks come up. | |
| 1347 | sideways.addEventListener("change", () => { driver.releaseAll(); if (sideways.matches && shell.classList.contains("driving")) setBare(true); }); | |
| 1224 | // Turning the phone swaps the controls under the thumbs: whatever was held stops. | |
| 1225 | sideways.addEventListener("change", () => driver.releaseAll()); | |
| 1348 | 1226 | const tap = () => navigator.vibrate?.(8); |
| 1349 | 1227 | const LABEL = { 1: ["J1", "Base"], 2: ["J2", "Shoulder"], 3: ["J3", "Elbow"], 4: ["J4", "Wrist"], 5: ["J5", "Roll"], 6: ["Grip", "Gripper"] }; |
| 1350 | 1228 | const ENDS = { 1: ["right", "left"], 2: ["up", "down"], 3: ["up", "down"], 4: ["up", "down"], 5: ["roll +", "roll −"], 6: ["open", "close"] }; |
| @@ -1410,25 +1288,6 @@ | ||
| 1410 | 1288 | const d = e.key === "ArrowUp" ? 1 : e.key === "ArrowDown" ? -1 : 0; |
| 1411 | 1289 | if (d) { e.preventDefault(); driver.nudge(picked, d * UP[picked] * step()); } |
| 1412 | 1290 | }); |
| 1413 | // The sticks: a disc, so a corner is no faster than an edge; each stick has its own finger. | |
| 1414 | const disc = (x, y) => { const r = Math.hypot(x, y); return r > 1 ? [x / r, y / r] : [x, y]; }; | |
| 1415 | const knob = (stick) => (x, y) => { | |
| 1416 | const [dx, dy] = disc(x, y), r = stick.clientWidth * 0.3; | |
| 1417 | stick.querySelector(".knob").style.transform = `translate(${(dx * r).toFixed(1)}px, ${(-dy * r).toFixed(1)}px)`; | |
| 1418 | }; | |
| 1419 | bindSurface($("stickL"), driver, "left", (x, y) => { const [dx, dy] = disc(x, y); return { 1: shape(dx) * UP[1], 2: shape(dy) * UP[2] }; }, { show: knob($("stickL")), haptic: tap }); | |
| 1420 | let wrist = false; | |
| 1421 | const right = () => (wrist ? { x: 5, y: 6 } : { x: 4, y: 3 }); | |
| 1422 | const rightStick = bindSurface($("stickR"), driver, "right", (x, y) => { const [dx, dy] = disc(x, y), m = right(); return { [m.x]: shape(dx) * UP[m.x], [m.y]: shape(dy) * UP[m.y] }; }, { show: knob($("stickR")), haptic: tap }); | |
| 1423 | const labelRight = () => { | |
| 1424 | $("capR").textContent = wrist ? "J5 roll ↔ · grip ↕" : "J4 wrist ↔ · J3 elbow ↕"; | |
| 1425 | $("stickR").setAttribute("aria-label", wrist ? "Right stick: sideways rolls the wrist, up opens the gripper" : "Right stick: sideways bends the wrist, up and down lifts the elbow"); | |
| 1426 | $("swap").textContent = wrist ? "Arm" : "Wrist"; $("swap").setAttribute("aria-pressed", String(wrist)); | |
| 1427 | }; | |
| 1428 | $("swap").onclick = () => { rightStick.end(); wrist = !wrist; tap(); labelRight(); }; | |
| 1429 | labelRight(); | |
| 1430 | bindSurface($("gripOpen"), driver, "open", () => ({ 6: UP[6] }), { haptic: tap }); | |
| 1431 | bindSurface($("gripClose"), driver, "close", () => ({ 6: -UP[6] }), { haptic: tap }); | |
| 1432 | 1291 | |
| 1433 | 1292 | // Keep the screen on while it is your turn: a phone that dims mid-drive is a hand off the controls. |
| 1434 | 1293 | let wakeLock = null, waking = false, wantAwake = false; |
| @@ -1541,9 +1400,7 @@ | ||
| 1541 | 1400 | shell.classList.toggle("mayDrive", mayDrive()); |
| 1542 | 1401 | $("t-admin").hidden = !owner; |
| 1543 | 1402 | $("t-chat").hidden = $("t-queue").hidden = !relayed; |
| 1544 | for (const id of ["joints", "poses", "drive", "reach", "touch", "sticks"]) $(id).inert = !can; | |
| 1545 | // A turn that starts sideways shows the sticks at once. | |
| 1546 | if (can && !shell.classList.contains("driving") && sideways.matches) setBare(true); | |
| 1403 | for (const id of ["joints", "poses", "drive", "touch"]) $(id).inert = !can; | |
| 1547 | 1404 | shell.classList.toggle("driving", can); |
| 1548 | 1405 | if (!can) driver.releaseAll(); |
| 1549 | 1406 | wake(can); |
| @@ -1552,14 +1409,12 @@ | ||
| 1552 | 1409 | // The server takes settings, the safety limits among them, from admins only. |
| 1553 | 1410 | for (const id of ["zoneRow", "trims", "safety", "jointLimits"]) $(id).inert = !owner; |
| 1554 | 1411 | viewer.toggleAttribute("disable-dragging", !can); |
| 1555 | if (!can) reachEnd(); | |
| 1556 | 1412 | teleop?.update(); // the turn ended: teleop stops at once (the server closes it too; the page need not wait for that) |
| 1557 | 1413 | // Your turn just came: open the controls. It just ended: back to the chat. |
| 1558 | 1414 | if (mayDrive() !== drove) { |
| 1559 | 1415 | drove = mayDrive(); |
| 1560 | 1416 | if (relayed && phone.matches && dock) dock.go(drove ? "drive" : dock.panel === "drive" ? "chat" : dock.panel, true); |
| 1561 | 1417 | else if (relayed) showTab(drove ? "controls" : "chat"); |
| 1562 | if (drove && sideways.matches) setBare(true); // on its side, a turn starts on the thumb sticks | |
| 1563 | 1418 | } |
| 1564 | 1419 | else if ($(`t-${tab}`).hidden) showTab(tab); |
| 1565 | 1420 | drawDock(); |
| @@ -1745,7 +1600,7 @@ | ||
| 1745 | 1600 | if (early) applyStatus(early); |
| 1746 | 1601 | poll(); |
| 1747 | 1602 | if (relayed) pollLink(); |
| 1748 | // The model is fetched once the first view is up, whether or not it is on screen: the joystick needs it. | |
| 1603 | // The model is fetched once the first view is up, whether or not it is on screen. | |
| 1749 | 1604 | if (modelInfo.ok) (window.requestIdleCallback ?? setTimeout)(() => load3D(), { timeout: 3000 }); |
| 1750 | 1605 | })(); |
| 1751 | 1606 | </script> |
new above
offline