Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
bench/limits-search.ts <url> runs the limits search per joint against the fake bus or a given arm server and prints a before/after table (lands once main is green) #538
Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.
smith/arm:smith/513-bench-limits-search-ts-url-runs-the-limi → main 1 commit fork
3 files
changed +199
−0
Software/so101/README.md +4
| @@ -48,3 +48,7 @@ The owner can test a joint's P (the position gain, 1–64) on the real arm, with | ||
| 48 | 48 | 3. keeps the new P only if p95 tracking error and overshoot are both no worse and one is better; otherwise, and on any failure or STOP, writes the old P back. |
| 49 | 49 | |
| 50 | 50 | Nobody can drive while it runs, and it will not start while someone does (409; 423 on an E-stop or hold). Only one run at a time. Progress shows in the Servos section (reload the page) and with `/tune` alone, which also gives the last result (`GET /tuning`). Every result is stored with the deployed commit and listed newest first in the Precision panel (joint, P from→to, error and overshoot before→after, kept or put back) and as `tunings` in `GET /precision`. All of it is the owner's: anyone else gets 404. |
| 51 | ||
| 52 | ## Faster limits | |
| 53 | ||
| 54 | A person runs this at the arm while nobody drives: `bun bench/limits-search.ts http://<arm>:8101` (add `--joint 2` for one joint; with no URL it runs on the fake bus). From today's 90 °/s and 360 °/s² it steps each joint's speed and acceleration up within the safety ranges (the gripper is skipped), sweeps at each step, and keeps the last step that did not make p95 error or overshoot worse, writing them through `PUT /settings`. It prints its command line, then a table of limits and p95 error, overshoot and time to arrive, before → after. It refuses to start while anyone drives or a STOP or E-stop is in force, and if one appears mid-run it stops and puts the starting limits back. |
Software/so101/bench/limits-search.test.ts added +31
| @@ -0,0 +1,31 @@ | ||
| 1 | import { expect, test } from "bun:test"; | |
| 2 | import { fakeArm } from "./precision.ts"; | |
| 3 | import { run, table } from "./limits-search.ts"; | |
| 4 | ||
| 5 | test("on the fake bus a joint's search ends in a table, and the arm's settings end at the 'after' limits", async () => { | |
| 6 | const arm = await fakeArm(); | |
| 7 | try { | |
| 8 | const found = await run(arm.url, [1], 300); | |
| 9 | const md = table(found); | |
| 10 | expect(md.split("\n")).toHaveLength(3); | |
| 11 | expect(md).toContain("| shoulder_pan | 90 → "); | |
| 12 | const to = found[0]!.search.to; | |
| 13 | const s = (await (await fetch(`${arm.url}/settings`)).json()) as { jointSpeedDegS: Record<string, number>; jointAccelDegS2: Record<string, number> }; | |
| 14 | expect(s.jointSpeedDegS.shoulder_pan).toBe(to.speedDegS); | |
| 15 | expect(s.jointAccelDegS2.shoulder_pan).toBe(to.accelDegS2); | |
| 16 | } finally { | |
| 17 | await arm.close(); | |
| 18 | } | |
| 19 | }, 60_000); | |
| 20 | ||
| 21 | test("it refuses to start while someone drives", async () => { | |
| 22 | const arm = await fakeArm(); | |
| 23 | try { | |
| 24 | const ws = new (await import("ws")).default(`${arm.url.replace("http", "ws")}/follow`); | |
| 25 | await new Promise((r) => ws.once("open", r)); | |
| 26 | await expect(run(arm.url, [1], 50)).rejects.toThrow("is driving the arm"); | |
| 27 | ws.close(1000); | |
| 28 | } finally { | |
| 29 | await arm.close(); | |
| 30 | } | |
| 31 | }, 10_000); |
Software/so101/bench/limits-search.ts added +164
| @@ -0,0 +1,164 @@ | ||
| 1 | /** | |
| 2 | * Finds faster speed and acceleration limits per joint, and prints a before/after table. | |
| 3 | * bun bench/limits-search.ts on the fake bus with the real Governor, as bench/limits-sweep.ts does | |
| 4 | * bun bench/limits-search.ts <url> [--joint N] on a given arm server (http://host:8101), every joint but the gripper, or joint N | |
| 5 | * A person runs it at the arm while nobody drives. It refuses to start unless /status shows nobody driving, no STOP and | |
| 6 | * no E-stop; if one appears mid-run it stops at once and puts the starting limits back (searchLimits does). | |
| 7 | * The limits are read and written through PUT /settings (jointSpeedDegS / jointAccelDegS2). It starts at today's | |
| 8 | * 90 °/s and 360 °/s² and steps up within the ranges of src/safety.ts, keeping the last step that did not get worse. | |
| 9 | */ | |
| 10 | import WebSocket from "ws"; | |
| 11 | import { searchLimits, type Limits, type LimitsSearch } from "../src/limits-search.ts"; | |
| 12 | import { JOINTS } from "../src/sts3215.ts"; | |
| 13 | import { RESEND_MS } from "../src/tuning-sweep.ts"; | |
| 14 | import type { SweepResult } from "../src/tuning-run.ts"; | |
| 15 | import type { Scorecard } from "../src/precision-history.ts"; | |
| 16 | import { fakeArm } from "./precision.ts"; | |
| 17 | import { targets } from "./limits-sweep.ts"; | |
| 18 | ||
| 19 | /** Today's limits, the first the search falls back to. */ | |
| 20 | export const TODAY: Limits = { speedDegS: 90, accelDegS2: 360 }; | |
| 21 | /** Ascending steps, every one inside the ranges of src/safety.ts (30-360 °/s, 30-3600 °/s²). */ | |
| 22 | export const STEPS: Limits[] = [ | |
| 23 | { speedDegS: 135, accelDegS2: 600 }, | |
| 24 | { speedDegS: 180, accelDegS2: 1000 }, | |
| 25 | { speedDegS: 240, accelDegS2: 1600 }, | |
| 26 | { speedDegS: 300, accelDegS2: 2400 }, | |
| 27 | { speedDegS: 360, accelDegS2: 3600 }, | |
| 28 | ]; | |
| 29 | ||
| 30 | interface Status { | |
| 31 | joints: Array<{ id: number; joint: string | null; lo: number | null; hi: number | null }>; | |
| 32 | driving: string | null; | |
| 33 | held: unknown; | |
| 34 | estop: unknown; | |
| 35 | } | |
| 36 | type Settings = { maxSpeedDegS?: number; maxAccelDegS2?: number; jointSpeedDegS?: Record<string, number>; jointAccelDegS2?: Record<string, number> }; | |
| 37 | ||
| 38 | const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)); | |
| 39 | const get = async <T>(url: string, path: string): Promise<T> => { | |
| 40 | const res = await fetch(url + path); | |
| 41 | if (!res.ok) throw new Error(`${path}: ${res.status}`); | |
| 42 | return (await res.json()) as T; | |
| 43 | }; | |
| 44 | const refusal = (s: Status) => (s.estop ? "emergency stop" : s.held ? "STOP" : null); | |
| 45 | ||
| 46 | /** Throws, naming why, unless /status shows nobody driving, no STOP and no E-stop. */ | |
| 47 | async function assertIdle(url: string, starting: boolean): Promise<Status> { | |
| 48 | const s = await get<Status>(url, "/status"); | |
| 49 | const why = s.driving ? `${s.driving} is driving the arm` : refusal(s); | |
| 50 | if (why) throw new Error(starting ? `refusing to start: ${why}` : `stopped: ${why}`); | |
| 51 | return s; | |
| 52 | } | |
| 53 | ||
| 54 | /** One joint's sweep through its own /follow stream; the numbers are the stream's session, read just before it ends. */ | |
| 55 | async function sweepJoint(url: string, id: number, hold: number): Promise<SweepResult> { | |
| 56 | const status = await assertIdle(url, true); | |
| 57 | const joint = status.joints.find((j) => j.id === id); | |
| 58 | if (!joint || joint.lo === null || joint.hi === null) throw new Error(`joint ${id} has no bounds`); | |
| 59 | const ws = new WebSocket(`${url.replace(/^http/, "ws")}/follow`); | |
| 60 | let closed: number | null = null; | |
| 61 | ws.on("close", (code) => (closed = code)); | |
| 62 | await new Promise((r, j) => (ws.once("open", r), ws.once("error", j), ws.once("close", (c) => j(new Error(`the stream was refused (${c})`))))); | |
| 63 | let session: Scorecard["session"] = null; | |
| 64 | try { | |
| 65 | for (const t of targets(joint.lo, joint.hi)) { | |
| 66 | const frame = JSON.stringify({ [id]: t }); | |
| 67 | const until = performance.now() + hold; | |
| 68 | for (let left = hold; left > 0; left = until - performance.now()) { | |
| 69 | if (closed !== null) break; | |
| 70 | ws.send(frame); | |
| 71 | await sleep(Math.min(RESEND_MS, left)); | |
| 72 | } | |
| 73 | const now = await get<Status>(url, "/status"); | |
| 74 | const why = refusal(now) ?? (closed !== null ? `the stream closed (${closed})` : null); | |
| 75 | if (why) throw new Error(`stopped: ${why}`); | |
| 76 | } | |
| 77 | session = (await get<Scorecard>(url, "/precision")).session; | |
| 78 | } finally { | |
| 79 | ws.close(1000); | |
| 80 | await new Promise((r) => (ws.readyState === WebSocket.CLOSED ? r(null) : ws.once("close", r))); | |
| 81 | await sleep(150); | |
| 82 | } | |
| 83 | const j = session?.joints.find((x) => x.id === id); | |
| 84 | if (!j || j.errorDeg.p95 === null) throw new Error("the sweep measured nothing"); | |
| 85 | if (session!.snapBacks.total) throw new Error("stopped: the arm refused or held back a target"); | |
| 86 | return { p95Error: j.errorDeg.p95, overshoot: j.overshootTicks.max ?? 0, timeToArrive: j.arriveMs.p95 ?? 0 }; | |
| 87 | } | |
| 88 | ||
| 89 | /** One joint's search and the sweep at its starting limits. */ | |
| 90 | export interface Found { | |
| 91 | search: LimitsSearch; | |
| 92 | before: SweepResult; | |
| 93 | } | |
| 94 | ||
| 95 | /** Runs the search for each joint id; answers one result per joint. Throws where it must stop, the limits already put back. */ | |
| 96 | export async function run(url: string, ids: number[], hold = 1500): Promise<Found[]> { | |
| 97 | await assertIdle(url, true); | |
| 98 | const out: Found[] = []; | |
| 99 | const put = async (body: object) => { | |
| 100 | const res = await fetch(`${url}/settings`, { method: "PUT", headers: { "content-type": "application/json" }, body: JSON.stringify(body) }); | |
| 101 | if (!res.ok) throw new Error(`PUT /settings: ${res.status}`); | |
| 102 | }; | |
| 103 | for (const id of ids) { | |
| 104 | const name = JOINTS[id]; | |
| 105 | if (!name) throw new Error(`no joint ${id}`); | |
| 106 | let before: SweepResult | null = null; | |
| 107 | const search = await searchLimits( | |
| 108 | { joint: id, steps: STEPS }, | |
| 109 | { | |
| 110 | isIdle: () => true, // checked against /status by sweepJoint before and during every sweep | |
| 111 | sweep: async (joint) => { | |
| 112 | const r = await sweepJoint(url, joint, hold); | |
| 113 | before ??= r; | |
| 114 | return r; | |
| 115 | }, | |
| 116 | readLimits: async () => { | |
| 117 | const s = await get<Settings>(url, "/settings"); | |
| 118 | return { speedDegS: s.jointSpeedDegS?.[name] ?? s.maxSpeedDegS ?? TODAY.speedDegS, accelDegS2: s.jointAccelDegS2?.[name] ?? s.maxAccelDegS2 ?? TODAY.accelDegS2 }; | |
| 119 | }, | |
| 120 | writeLimits: (_joint, l) => put({ jointSpeedDegS: { [name]: l.speedDegS }, jointAccelDegS2: { [name]: l.accelDegS2 } }), | |
| 121 | }, | |
| 122 | ); | |
| 123 | out.push({ search, before: before! }); | |
| 124 | } | |
| 125 | return out; | |
| 126 | } | |
| 127 | ||
| 128 | const n = (v: number) => String(Math.round(v * 100) / 100); | |
| 129 | const pair = (a: number, b: number) => `${n(a)} → ${n(b)}`; | |
| 130 | ||
| 131 | /** The table: joint, limits before → after, then p95 error, overshoot and time to arrive before → after. */ | |
| 132 | export function table(found: Found[]): string { | |
| 133 | const head = ["joint", "speed °/s", "accel °/s²", "p95 error °", "overshoot ticks", "time to arrive ms"]; | |
| 134 | return [ | |
| 135 | `| ${head.join(" | ")} |`, | |
| 136 | `|${head.map(() => " --- ").join("|")}|`, | |
| 137 | ...found.map(({ search: r, before }) => { | |
| 138 | const after = r.runs.find((x) => x.step === r.to)?.result ?? before; | |
| 139 | return `| ${JOINTS[r.joint] ?? r.joint} | ${pair(r.from.speedDegS, r.to.speedDegS)} | ${pair(r.from.accelDegS2, r.to.accelDegS2)} | ${pair(before.p95Error, after.p95Error)} | ${pair(before.overshoot, after.overshoot)} | ${pair(before.timeToArrive, after.timeToArrive)} |`; | |
| 140 | }), | |
| 141 | ].join("\n"); | |
| 142 | } | |
| 143 | ||
| 144 | if (import.meta.main) { | |
| 145 | const args = process.argv.slice(2); | |
| 146 | const at = args.indexOf("--joint"); | |
| 147 | const joint = at >= 0 ? Number(args[at + 1]) : null; | |
| 148 | const given = args.find((a, i) => !a.startsWith("--") && i !== at + 1); | |
| 149 | if (joint !== null && !Number.isInteger(joint)) { | |
| 150 | console.error("--joint needs a joint number, 1-6"); | |
| 151 | process.exit(1); | |
| 152 | } | |
| 153 | const ids = joint !== null ? [joint] : [1, 2, 3, 4, 5]; | |
| 154 | console.log(`bun bench/limits-search.ts${given ? ` ${given}` : ""}${joint !== null ? ` --joint ${joint}` : ""}`); | |
| 155 | const arm = given ? { url: given.replace(/\/$/, ""), close: async () => {} } : await fakeArm(); | |
| 156 | try { | |
| 157 | console.log(table(await run(arm.url, ids))); | |
| 158 | } catch (err) { | |
| 159 | console.error((err as Error).message); | |
| 160 | process.exitCode = 1; | |
| 161 | } finally { | |
| 162 | await arm.close(); | |
| 163 | } | |
| 164 | } |
new above
offline