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

Merged opened this

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

Merged

smith/arm:smith/164-a-refused-move-shows-its-reason-on-the-p → main 2 commits fork 1 thread · 1 unresolved

  • Merged. smith/164-a-refused-move-shows-its-reason-on-the-p was merged into main as 90b0d9b.

Review

Sign in to review this change.
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 @@
7878 #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; }
7979 #banner.bad { display: block; border-color: #f85149; color: #ffb3ad; } #banner.warn { display: block; border-color: #d29922; color: #f2cc60; }
8080 #msg { position: absolute; left: calc(8px + var(--safe-l)); right: calc(8px + var(--safe-r)); bottom: 8px; z-index: 6; margin: 0 auto; width: max-content; max-width: calc(100% - 16px); padding: 8px 12px; border-radius: 8px; font-size: 13px; background: rgba(13,17,23,.9); color: #e8eaed; border: 1px solid #30363d; }
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; }
8182 #msg:empty { display: none; } #msg.bad { border-color: #f85149; color: #ffb3ad; }
8283
8384 /* the status strip: who drives, your place, time left, the turn's action and the E-stop */
@@ -308,6 +309,7 @@
308309 <div id="driverTag" aria-hidden="true"></div>
309310 <div id="banner" role="alert"></div>
310311 <div id="msg" role="status"></div>
312 <div id="refusal" role="status"></div>
311313 <div id="sticks" role="group" aria-label="Thumb sticks">
312314 <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>
313315 <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 @@
526528 $("msg").textContent = text; $("msg").className = bad ? "bad" : "";
527529 clearTimeout(msgTimer); if (text) msgTimer = setTimeout(() => ($("msg").textContent = ""), bad ? 9000 : 5000);
528530 };
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 = ""; };

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.

529534 const api = async (path, body, method = body === undefined ? "GET" : "POST") => {
530535 const r = await fetch(path, { method, body: body === undefined ? undefined : JSON.stringify(body), cache: "no-store" });
531536 const data = await r.json().catch(() => ({}));
@@ -868,6 +873,7 @@
868873 async function goPose(name) {
869874 msg(`going to ${name}…`);
870875 const r = await api("/pose/go", { name });
876 if (r.reached) accepted(); else if (r.violation || String(r.error).startsWith("refused:")) refused(r);
871877 msg(r.reached ? `reached ${name}${r.warning ? ` — ${r.warning}` : ""}` : `${r.error}`, !r.reached);
872878 }
873879 // STOP: drop what this page would still send, then the server halts and holds the arm until Resume.
@@ -958,6 +964,7 @@
958964 play.onclick = async () => {
959965 $("playDlg").close(); msg(`playing ${name}…`);
960966 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);
961968 msg(r.ok ? `played ${name}: ${r.played} frames; holding` : r.error, !r.ok);
962969 };
963970 const x = document.createElement("button"); x.className = "x"; x.textContent = "×";
@@ -1265,6 +1272,10 @@
12651272 const want = intent.target(j.id, j.position, performance.now());
12661273 viewer.setJointValue(name, toRad(j.id, want ?? j.position));
12671274 }
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" : "";
12681279 if (marker) {
12691280 const v = d.blocked ?? d.violation;
12701281 marker.visible = Boolean(v);
@@ -1654,16 +1665,17 @@
16541665 el.range.disabled = locked; for (const b of el.buttons) b.disabled = locked;
16551666 }
16561667 }
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 };
16571676 function banner(d) {
16581677 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;
16671679 if (d.estop) {
16681680 b.dataset.held = "";
16691681 b.className = "stop";

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs