Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
src/tuning-run.ts runs one P-coefficient experiment: sweep, write, sweep, then keep or put back - orchestration and test only #489
Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.
smith/arm:smith/485-src-tuning-run-ts-runs-one-p-coefficient → main 2 commits fork 2 threads · 2 unresolved
2 files
changed +122
−0
Software/so101/src/tuning-run.test.ts added +69
| @@ -0,0 +1,69 @@ | ||
| 1 | import { expect, test } from "bun:test"; | |
| 2 | import { runPExperiment, type SweepResult, type TuningDeps } from "./tuning-run.ts"; | |
| 3 | ||
| 4 | function fake(sweeps: Array<SweepResult | Error>, idle = true, failFirstWrite = false) { | |
| 5 | const writes: number[] = []; | |
| 6 | const deps: TuningDeps = { | |
| 7 | isIdle: () => idle, | |
| 8 | sweep: async () => { | |
| 9 | const next = sweeps.shift()!; | |
| 10 | if (next instanceof Error) throw next; | |
| 11 | return next; | |
| 12 | }, | |
| 13 | readParams: async () => ({ P: 16 }), | |
| 14 | writeParams: async (_id, { P }) => { | |
| 15 | writes.push(P); | |
| 16 | if (failFirstWrite && writes.length === 1) throw new Error("bus timeout"); // it may have landed | |
| 17 | }, | |
| 18 | }; | |
| 19 | return { deps, writes }; | |
| 20 | } | |
| 21 | const r = (p95Error: number, overshoot: number): SweepResult => ({ p95Error, overshoot, timeToArrive: 1 }); | |
| 22 | ||
| 23 | test("keeps a candidate that is better and no worse", async () => { | |
| 24 | const { deps, writes } = fake([r(10, 5), r(8, 5)]); | |
| 25 | const out = await runPExperiment({ joint: 2, p: 24 }, deps); | |
| 26 | expect(out).toMatchObject({ joint: 2, from: 16, to: 24, kept: true }); | |
| 27 | expect(writes).toEqual([24]); | |
| 28 | }); | |
| 29 | ||
| 30 | test("puts the old P back when one measure is worse or nothing improved", async () => { | |
| 31 | for (const after of [r(8, 9), r(10, 5), r(12, 2)]) { | |
| 32 | const { deps, writes } = fake([r(10, 5), after]); | |
| 33 | const out = await runPExperiment({ joint: 2, p: 24 }, deps); | |
| 34 | expect(out).toMatchObject({ from: 16, to: 16, kept: false, after }); | |
| 35 | expect(writes).toEqual([24, 16]); | |
| 36 | } | |
| 37 | }); | |
| 38 | ||
| 39 | test("puts the old P back when the second sweep throws", async () => { | |
| 40 | const { deps, writes } = fake([r(10, 5), new Error("STOP")]); | |
| 41 | await expect(runPExperiment({ joint: 2, p: 24 }, deps)).rejects.toThrow("STOP"); | |
| 42 | expect(writes).toEqual([24, 16]); | |
| 43 | }); | |
| 44 | ||
| 45 | test("puts the old P back when the candidate write itself throws", async () => { | |
| 46 | const { deps, writes } = fake([r(10, 5)], true, true); | |
| 47 | await expect(runPExperiment({ joint: 2, p: 24 }, deps)).rejects.toThrow("bus timeout"); | |
| 48 | expect(writes).toEqual([24, 16]); | |
| 49 | }); | |
| 50 | ||
| 51 | test("writes nothing when the first sweep throws", async () => { | |
| 52 | const { deps, writes } = fake([new Error("STOP")]); | |
| 53 | await expect(runPExperiment({ joint: 2, p: 24 }, deps)).rejects.toThrow("STOP"); | |
| 54 | expect(writes).toEqual([]); | |
| 55 | }); | |
| 56 | ||
| 57 | test("refuses while someone drives", async () => { | |
| 58 | const { deps, writes } = fake([], false); | |
| 59 | await expect(runPExperiment({ joint: 2, p: 24 }, deps)).rejects.toThrow("driving"); | |
| 60 | expect(writes).toEqual([]); | |
| 61 | }); | |
| 62 | ||
| 63 | test("refuses a P outside the documented range before writing", async () => { | |
| 64 | for (const p of [0, 65, 1.5, NaN]) { | |
| 65 | const { deps, writes } = fake([r(1, 1), r(1, 1)]); | |
| 66 | await expect(runPExperiment({ joint: 2, p }, deps)).rejects.toThrow("P must be"); | |
| 67 | expect(writes).toEqual([]); | |
| 68 | } | |
| 69 | }); |
Software/so101/src/tuning-run.ts added +53
| @@ -0,0 +1,53 @@ | ||
| 1 | import { TUNABLE } from "./servo-params.ts"; | |
| 2 | ||
| 3 | /** What one sweep of a joint measured. */ | |
| 4 | export interface SweepResult { | |
| 5 | p95Error: number; | |
| 6 | overshoot: number; | |
| 7 | timeToArrive: number; | |
| 8 | } | |
| 9 | ||
| 10 | export interface TuningDeps { | |
| 11 | isIdle(): boolean; | |
| 12 | /** Drive the joint through its test sweep. Throws if the sweep is stopped. */ | |
| 13 | sweep(joint: number): Promise<SweepResult>; | |
| 14 | readParams(id: number): Promise<{ P: number }>; | |
| 15 | writeParams(id: number, values: { P: number }): Promise<void>; | |
| 16 | } | |
| 17 | ||
| 18 | export interface PExperiment { | |
| 19 | joint: number; | |
| 20 | from: number; | |
| 21 | to: number; | |
| 22 | before: SweepResult; | |
| 23 | after: SweepResult; | |
| 24 | kept: boolean; | |
| 25 | at: string; | |
| 26 | } | |
| 27 | ||
| 28 | /** | |
| 29 | * Sweep a joint, write the candidate P, sweep again, and keep it only if p95 error and overshoot are both no worse | |
| 30 | * and one is better. Otherwise, and on any throw, the old P is written back. | |
| 31 | */ | |
| 32 | export async function runPExperiment({ joint, p }: { joint: number; p: number }, deps: TuningDeps): Promise<PExperiment> { | |
| 33 | const { min, max } = TUNABLE.P; | |
| 34 | if (!Number.isInteger(p) || p < min || p > max) throw new Error(`P must be a whole number from ${min} to ${max}`); | |
| 35 | if (!deps.isIdle()) throw new Error("someone is driving; not running the experiment"); | |
| 36 | const from = (await deps.readParams(joint)).P; | |
| 37 | let written = false; | |
| 38 | try { | |
| 39 | const before = await deps.sweep(joint); | |
| 40 | written = true; // before the write: a write that throws may still have landed | |
| 41 | await deps.writeParams(joint, { P: p }); | |
| 42 | const after = await deps.sweep(joint); | |
| 43 | const kept = | |
| 44 | after.p95Error <= before.p95Error && | |
| 45 | after.overshoot <= before.overshoot && | |
| 46 | (after.p95Error < before.p95Error || after.overshoot < before.overshoot); | |
| 47 | if (!kept) await deps.writeParams(joint, { P: from }); | |
If this restore throws, the catch below runs it again and the failure replaces the sweep or write error. Move the restore into one place that rethrows both errors. |
||
| 48 | return { joint, from, to: kept ? p : from, before, after, kept, at: new Date().toISOString() }; | |
| 49 | } catch (err) { | |
| 50 | if (written) await deps.writeParams(joint, { P: from }); | |
| 51 | throw err; | |
| 52 | } | |
| 53 | } |
new above
offline
This ordering is the safety property, and no test fails if it changes. Add a test where the candidate
writeParamsthrows and assert the old P is written back.