Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
A refused move shows its reason on the page and in the Machine panel - never silent #180
Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.
smith/arm:smith/164-a-refused-move-shows-its-reason-on-the-p → main 2 commits fork 1 thread · 1 unresolved
3 files
changed +61
−8
Software/so101/src/ui/refusal.js added +23
| @@ -0,0 +1,23 @@ | ||
| 1 | // @ts-check | |
| 2 | /** | |
| 3 | * The one line a driver sees when the arm server refuses a move: the server's own reason (src/server.ts answers | |
| 4 | * `{ error: "refused: …", violation }`), or a generic line when the body is anything else. Text only: the page puts | |
| 5 | * it in with textContent, and nothing here builds markup. | |
| 6 | */ | |
| 7 | ||
| 8 | const MAX = 160; | |
| 9 | const GENERIC = "blocked: the arm would not make that move"; | |
| 10 | ||
| 11 | /** | |
| 12 | * @param {unknown} body what the refused request answered (any shape) | |
| 13 | * @returns {string} | |
| 14 | */ | |
| 15 | export function refusalLine(body) { | |
| 16 | const error = body && typeof body === "object" ? /** @type {{ error?: unknown }} */ (body).error : undefined; | |
| 17 | if (typeof error !== "string") return GENERIC; | |
| 18 | // eslint-disable-next-line no-control-regex | |
| 19 | const reason = error.replace(/[\u0000-\u001f\u007f]+/g, " ").replace(/^refused:\s*/i, "").trim(); | |
| 20 | if (!reason) return GENERIC; | |
| 21 | const line = `blocked: ${reason}`; | |
| 22 | return line.length > MAX ? `${line.slice(0, MAX - 1)}…` : line; | |
| 23 | } |
Software/so101/src/ui/refusal.test.ts added +18
| @@ -0,0 +1,18 @@ | ||
| 1 | import { describe, expect, test } from "bun:test"; | |
| 2 | import { refusalLine } from "./refusal.js"; | |
| 3 | ||
| 4 | describe("a refusal as one line", () => { | |
| 5 | test("the server's reason, without its 'refused:' prefix", () => { | |
| 6 | expect(refusalLine({ reached: false, error: "refused: gripper would be 28 mm below the table (plus the 5 mm margin)" })).toBe("blocked: gripper would be 28 mm below the table (plus the 5 mm margin)"); | |
| 7 | }); | |
| 8 | ||
| 9 | test("unknown shapes give the generic line", () => { | |
| 10 | for (const body of [null, undefined, "no", 7, [], {}, { error: 3 }, { error: " " }, { error: "refused:" }]) expect(refusalLine(body)).toBe("blocked: the arm would not make that move"); | |
| 11 | }); | |
| 12 | ||
| 13 | test("HTML stays text, control characters and length are bounded", () => { | |
| 14 | expect(refusalLine({ error: "<img src=x onerror=alert(1)>" })).toBe("blocked: <img src=x onerror=alert(1)>"); | |
| 15 | expect(refusalLine({ error: "a\nb\u0007c" })).toBe("blocked: a b c"); | |
| 16 | expect(refusalLine({ error: "x".repeat(500) }).length).toBe(160); | |
| 17 | }); | |
| 18 | }); |
Software/so101/src/viewer.html +20 −8
| @@ -78,6 +78,7 @@ | ||
| 78 | 78 | #banner { position: absolute; top: 50px; left: 50%; transform: translateX(-50%); z-index: 5; width: max-content; max-width: min(640px, calc(100% - 16px)); padding: 8px 12px; border-radius: 8px; font-size: 13px; background: rgba(0,0,0,.8); border: 1px solid #30363d; display: none; } |
| 79 | 79 | #banner.bad { display: block; border-color: #f85149; color: #ffb3ad; } #banner.warn { display: block; border-color: #d29922; color: #f2cc60; } |
| 80 | 80 | #msg { position: absolute; left: calc(8px + var()); right: calc(8px + var()); bottom: 8px; z-index: 6; margin: 0 auto; width: max-content; max-width: calc(100% - 16px); padding: 8px 12px; border-radius: 8px; font-size: 13px; background: rgba(13,17,23,.9); color: #e8eaed; border: 1px solid #30363d; } |
| 81 | #refusal { position: absolute; left: 8px; right: 8px; bottom: 52px; z-index: 6; margin: 0 auto; width: max-content; max-width: calc(100% - 16px); padding: 6px 12px; border-radius: 8px; font-size: 13px; background: rgba(13,17,23,.9); border: 1px solid #f85149; color: #ffb3ad; } #refusal:empty { display: none; } | |
| 81 | 82 | #msg:empty { display: none; } #msg.bad { border-color: #f85149; color: #ffb3ad; } |
| 82 | 83 | |
| 83 | 84 | /* the status strip: who drives, your place, time left, the turn's action and the E-stop */ |
| @@ -308,6 +309,7 @@ | ||
| 308 | 309 | <div id="driverTag" aria-hidden="true"></div> |
| 309 | 310 | <div id="banner" role="alert"></div> |
| 310 | 311 | <div id="msg" role="status"></div> |
| 312 | <div id="refusal" role="status"></div> | |
| 311 | 313 | <div id="sticks" role="group" aria-label="Thumb sticks"> |
| 312 | 314 | <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> |
| 313 | 315 | <div class="stick" id="stickR" role="group" aria-label="Right stick"><span class="cap" id="capR"></span><span class="knob"></span></div> |
| @@ -526,6 +528,9 @@ | ||
| 526 | 528 | $("msg").textContent = text; $("msg").className = bad ? "bad" : ""; |
| 527 | 529 | clearTimeout(msgTimer); if (text) msgTimer = setTimeout(() => ($("msg").textContent = ""), bad ? 9000 : 5000); |
| 528 | 530 | }; |
| 531 | // Why a move was refused, until the next accepted one. | |
| 532 | const refused = async (body) => { $("refusal").textContent = (await import("/ui/__VERSION__/refusal.js")).refusalLine(body); }; | |
| 533 | const accepted = () => { $("refusal").textContent = ""; }; | |
| 529 | 534 | const api = async (path, body, method = body === undefined ? "GET" : "POST") => { |
| 530 | 535 | const r = await fetch(path, { method, body: body === undefined ? undefined : JSON.stringify(body), cache: "no-store" }); |
| 531 | 536 | const data = await r.json().catch(() => ({})); |
| @@ -868,6 +873,7 @@ | ||
| 868 | 873 | async function goPose(name) { |
| 869 | 874 | msg(`going to ${name}…`); |
| 870 | 875 | const r = await api("/pose/go", { name }); |
| 876 | if (r.reached) accepted(); else if (r.violation || String(r.error).startsWith("refused:")) refused(r); | |
| 871 | 877 | msg(r.reached ? `reached ${name}${r.warning ? ` — ${r.warning}` : ""}` : `${r.error}`, !r.reached); |
| 872 | 878 | } |
| 873 | 879 | // STOP: drop what this page would still send, then the server halts and holds the arm until Resume. |
| @@ -958,6 +964,7 @@ | ||
| 958 | 964 | play.onclick = async () => { |
| 959 | 965 | $("playDlg").close(); msg(`playing ${name}…`); |
| 960 | 966 | const r = await api("/play", { name, rate: Number($("playRate").value) }); |
| 967 | if (r.ok) accepted(); else if (r.violation || String(r.error).startsWith("refused:")) refused(r); | |
| 961 | 968 | msg(r.ok ? `played ${name}: ${r.played} frames; holding` : r.error, !r.ok); |
| 962 | 969 | }; |
| 963 | 970 | const x = document.createElement("button"); x.className = "x"; x.textContent = "×"; |
| @@ -1265,6 +1272,10 @@ | ||
| 1265 | 1272 | const want = intent.target(j.id, j.position, performance.now()); |
| 1266 | 1273 | viewer.setJointValue(name, toRad(j.id, want ?? j.position)); |
| 1267 | 1274 | } |
| 1275 | // A stream frame the zone refuses shows as the status's `blocked` in enforce mode; the next clear status ends it. | |
| 1276 | if (d.blocked && d.zone === "enforce") refused({ error: `${sayViolation(d.blocked)}; back the leader off` }); | |
| 1277 | else if (!d.blocked && $("refusal").dataset.stream) accepted(); | |
| 1278 | $("refusal").dataset.stream = d.blocked && d.zone === "enforce" ? "1" : ""; | |
| 1268 | 1279 | if (marker) { |
| 1269 | 1280 | const v = d.blocked ?? d.violation; |
| 1270 | 1281 | marker.visible = Boolean(v); |
| @@ -1654,16 +1665,17 @@ | ||
| 1654 | 1665 | el.range.disabled = locked; for (const b of el.buttons) b.disabled = locked; |
| 1655 | 1666 | } |
| 1656 | 1667 | } |
| 1668 | // The depth includes the margin: say where the part really is, and how much of that is margin. | |
| 1669 | const sayViolation = (v) => { | |
| 1670 | const part = v.link.replace(/_link$/, "").replace(/_so101_v1$/, ""); | |
| 1671 | const marginMm = v.what === "table" ? settings?.floorMarginMm ?? 0 : settings?.baseMarginMm ?? 0; | |
| 1672 | const beyond = v.depth * 1000 - marginMm; | |
| 1673 | if (beyond > 0) return `${part} is ${beyond.toFixed(0)} mm ${v.what === "table" ? "below the table" : "inside the base"} (plus the ${marginMm} mm margin)`; | |
| 1674 | return `${part} is ${(-beyond).toFixed(0)} mm ${v.what === "table" ? "above the table" : "from the base"}, inside the ${marginMm} mm margin`; | |
| 1675 | }; | |
| 1657 | 1676 | function banner(d) { |
| 1658 | 1677 | const b = $("banner"); |
| 1659 | // The depth includes the margin: say where the part really is, and how much of that is margin. | |
| 1660 | const say = (v) => { | |
| 1661 | const part = v.link.replace(/_link$/, "").replace(/_so101_v1$/, ""); | |
| 1662 | const marginMm = v.what === "table" ? settings?.floorMarginMm ?? 0 : settings?.baseMarginMm ?? 0; | |
| 1663 | const beyond = v.depth * 1000 - marginMm; | |
| 1664 | if (beyond > 0) return `${part} is ${beyond.toFixed(0)} mm ${v.what === "table" ? "below the table" : "inside the base"} (plus the ${marginMm} mm margin)`; | |
| 1665 | return `${part} is ${(-beyond).toFixed(0)} mm ${v.what === "table" ? "above the table" : "from the base"}, inside the ${marginMm} mm margin`; | |
| 1666 | }; | |
| 1678 | const say = sayViolation; | |
| 1667 | 1679 | if (d.estop) { |
| 1668 | 1680 | b.dataset.held = ""; |
| 1669 | 1681 | b.className = "stop"; |
new above
offline
Unawaited dynamic import with no catch: a network blip means no reason is shown at all. Add .catch(() => { $("refusal").textContent = "blocked: the arm would not make that move"; }). Not blocking.