Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
GET /servo-params on the arm server reports each joint's control registers, read-only #454
Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.
smith/arm:smith/442-get-servo-params-on-the-arm-server-repor → main 1 commit fork
6 files
changed +106
−0
Software/so101/src/admin-relay.test.ts +8
| @@ -151,3 +151,11 @@ test("POST /admin/lift lifts a mute or ban for the owner or a moderator, logged; | ||
| 151 | 151 | expect(lobby!.chat.muted.size).toBe(0); |
| 152 | 152 | expect(lobby!.admin.log.at(-1)?.what).toBe("unmuted carol"); |
| 153 | 153 | }); |
| 154 | ||
| 155 | test("/servo-params is the owner's; anyone else, signed in or not, gets 404", async () => { | |
| 156 | const base = await boot(); | |
| 157 | expect((await fetch(`${base}/servo-params`)).status).toBe(404); | |
| 158 | expect((await fetch(`${base}/servo-params`, { headers: as("bob", "user") })).status).toBe(404); | |
| 159 | // the owner's request is passed on; nothing listens at the simulation address, so it is not a 404 | |
| 160 | expect((await fetch(`${base}/servo-params`, { headers: as("trevato", "admin") })).status).not.toBe(404); | |
| 161 | }); |
Software/so101/src/relay.ts +3
| @@ -159,6 +159,8 @@ const STATIC = /^\/(?:index\.html)?$|^\/(?:model|vendor|ui)\//; | ||
| 159 | 159 | const LEADER = "/leader/calibration"; |
| 160 | 160 | /** Admins only: clearing an emergency stop and changing the settings (the safety limits among them). */ |
| 161 | 161 | const ADMIN_ONLY = new Set(["POST /estop/clear", "PUT /settings", "POST /settings/table-here"]); |
| 162 | /** Reads only the owner may make; anyone else is told there is no such page. */ | |
| 163 | const OWNER_READS = new Set(["GET /servo-params"]); | |
| 162 | 164 | /** The driver's, and any admin's at any time. */ |
| 163 | 165 | const ADMIN_TOO = new Set(["POST /stop", "POST /resume", "POST /estop"]); |
| 164 | 166 | /** What still goes to the arm server while the link is reconnecting: stopping it. */ |
| @@ -577,6 +579,7 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> { | ||
| 577 | 579 | } |
| 578 | 580 | const route = `${req.method} ${url.pathname}`; |
| 579 | 581 | let by = "the arm's page"; |
| 582 | if (OWNER_READS.has(route) && !(lobby ? lobby.who(req)?.owner : false)) return json(res, 404, { error: "not found" }); | |
| 580 | 583 | if (!reading && lobby) { |
| 581 | 584 | const who = lobby.who(req); |
| 582 | 585 | const refused = ADMIN_ONLY.has(route) |
Software/so101/src/server.test.ts +15
| @@ -42,6 +42,21 @@ describe("so101 serve", () => { | ||
| 42 | 42 | expect(limits["6"]).toEqual({ min: 1995, max: 3525 }); |
| 43 | 43 | }); |
| 44 | 44 | |
| 45 | test("/servo-params reads the control registers, writes nothing, and waits while a stream drives", async () => { | |
| 46 | const before = fake.writes.length; | |
| 47 | const rows = (await (await fetch(`${base}/servo-params`)).json()) as Array<{ id: number; joint: string; registers: Record<string, number> }>; | |
| 48 | expect(rows.map((r) => [r.id, r.joint])).toEqual([[1, "shoulder_pan"], [2, "shoulder_lift"], [6, "gripper"]]); | |
| 49 | expect(Object.keys(rows[0]!.registers)).toContain("Return_Delay_Time"); | |
| 50 | expect(fake.writes.length).toBe(before); | |
| 51 | const ws = new WebSocket(`ws://127.0.0.1:${server.port}/follow`); | |
| 52 | await new Promise((r) => ws.once("open", r)); | |
| 53 | expect((await fetch(`${base}/servo-params`)).status).toBe(409); | |
| 54 | const closed = new Promise((r) => ws.once("close", r)); | |
| 55 | ws.close(); | |
| 56 | await closed; | |
| 57 | await sleep(40); | |
| 58 | }); | |
| 59 | ||
| 45 | 60 | test("a stream drives the arm inside the limits and drops impossible jumps", async () => { |
| 46 | 61 | const ws = new WebSocket(`ws://127.0.0.1:${server.port}/follow`); |
| 47 | 62 | await new Promise((r) => ws.once("open", r)); |
Software/so101/src/server.ts +5
| @@ -5,6 +5,7 @@ | ||
| 5 | 5 | * GET / the browser app: cameras, 3D model, live joints, poses, jog, record and play, metrics |
| 6 | 6 | * GET /status every joint: position, load, temperature, torque; what drives the arm; zone state and the table (tableZ) |
| 7 | 7 | * GET /limits each servo's calibrated Min/Max_Position_Limit |
| 8 | * GET /servo-params each servo's control registers (delay, P/D/I, startup force, dead zones, velocity loop), read-only; 409 while something drives | |
| 8 | 9 | * GET /poses the saved poses |
| 9 | 10 | * POST /pose/save {"name": "he1", "ids"?: [1,2,3,4,5]} remember where the joints are now |
| 10 | 11 | * POST /pose/delete {"name": "he1"} |
| @@ -58,6 +59,7 @@ import { dirname, extname, join, resolve } from "node:path"; | ||
| 58 | 59 | import type { Duplex } from "node:stream"; |
| 59 | 60 | import { createHash, randomUUID } from "node:crypto"; |
| 60 | 61 | import { fileURLToPath } from "node:url"; |
| 62 | import { readServoParams } from "./servo-params.ts"; | |
| 61 | 63 | import { WebSocketServer, type WebSocket } from "ws"; |
| 62 | 64 | import { browserModule } from "./browser-modules.ts"; |
| 63 | 65 | import { Clock, CommandGate, HELD_CLOSE } from "./command-gate.ts"; |
| @@ -729,6 +731,9 @@ export async function startServer(bus: FeetechBus, options: ServerOptions = {}): | ||
| 729 | 731 | return json(res, 200, await status()); |
| 730 | 732 | case "GET /limits": |
| 731 | 733 | return json(res, 200, Object.fromEntries(limits)); |
| 734 | case "GET /servo-params": | |
| 735 | if (driver) throw new HttpError(409, `${describe(driver)} is driving the arm`); | |
| 736 | return json(res, 200, await readServoParams(bus, ids)); | |
| 732 | 737 | case "GET /poses": |
| 733 | 738 | return json(res, 200, loadPoses()); |
| 734 | 739 | case "GET /recordings": |
Software/so101/src/servo-params.test.ts added +29
| @@ -0,0 +1,29 @@ | ||
| 1 | import { expect, test } from "bun:test"; | |
| 2 | import { FeetechBus } from "./bus.ts"; | |
| 3 | import { FakeBus } from "./fake-bus.ts"; | |
| 4 | import { readServoParams } from "./servo-params.ts"; | |
| 5 | ||
| 6 | test("reads each joint's control registers, lists a silent servo with its error, and writes nothing", async () => { | |
| 7 | const fake = new FakeBus([1, 2, 3], { drop: new Set([3]) }); | |
| 8 | const memory = fake.servos.get(1)!; | |
| 9 | Object.entries({ 7: 250, 21: 32, 22: 32, 23: 0, 24: 16, 26: 1, 27: 2, 37: 30, 39: 40 }).forEach(([address, value]) => (memory[Number(address)] = value)); | |
| 10 | const rows = await readServoParams(new FeetechBus(fake), [1, 2, 3]); | |
| 11 | expect(rows[0]).toEqual({ | |
| 12 | id: 1, | |
| 13 | joint: "shoulder_pan", | |
| 14 | registers: { | |
| 15 | Return_Delay_Time: 250, | |
| 16 | P_Coefficient: 32, | |
| 17 | D_Coefficient: 32, | |
| 18 | I_Coefficient: 0, | |
| 19 | Minimum_Startup_Force: 16, | |
| 20 | CW_Dead_Zone: 1, | |
| 21 | CCW_Dead_Zone: 2, | |
| 22 | Velocity_closed_loop_P_proportional_coefficient: 30, | |
| 23 | Velocity_closed_loop_I_integral_coefficient: 40, | |
| 24 | }, | |
| 25 | }); | |
| 26 | expect(rows[1]!.error).toBeUndefined(); | |
| 27 | expect(rows[2]).toEqual({ id: 3, joint: "elbow_flex", registers: {}, error: "no answer" }); | |
| 28 | expect(fake.writes).toEqual([]); | |
| 29 | }); |
Software/so101/src/servo-params.ts added +46
| @@ -0,0 +1,46 @@ | ||
| 1 | import type { FeetechBus } from "./bus.ts"; | |
| 2 | import { JOINTS, STS3215 } from "./sts3215.ts"; | |
| 3 | ||
| 4 | /** The control registers worth knowing before tuning a joint, by the name the control table gives them. */ | |
| 5 | const REGISTERS = [ | |
| 6 | "Return_Delay_Time", | |
| 7 | "P_Coefficient", | |
| 8 | "D_Coefficient", | |
| 9 | "I_Coefficient", | |
| 10 | "Minimum_Startup_Force", | |
| 11 | "CW_Dead_Zone", | |
| 12 | "CCW_Dead_Zone", | |
| 13 | "Velocity_closed_loop_P_proportional_coefficient", | |
| 14 | "Velocity_closed_loop_I_integral_coefficient", | |
| 15 | ] as const; | |
| 16 | ||
| 17 | export interface ServoParams { | |
| 18 | id: number; | |
| 19 | joint: string | null; | |
| 20 | registers: Record<string, number>; | |
| 21 | /** Set when the servo did not answer (every register missing) or answered only in part. */ | |
| 22 | error?: string; | |
| 23 | } | |
| 24 | ||
| 25 | /** Read each servo's control registers. Reads only; a servo that is silent is listed with its error. */ | |
| 26 | export async function readServoParams(bus: FeetechBus, ids: readonly number[]): Promise<ServoParams[]> { | |
| 27 | const rows: ServoParams[] = ids.map((id) => ({ id, joint: JOINTS[id] ?? null, registers: {} })); | |
| 28 | for (const name of REGISTERS) { | |
| 29 | let values: Map<number, number>; | |
| 30 | try { | |
| 31 | values = await bus.syncRead(ids, STS3215[name]); | |
| 32 | } catch { | |
| 33 | values = new Map(); | |
| 34 | } | |
| 35 | for (const row of rows) { | |
| 36 | const value = values.get(row.id); | |
| 37 | if (value !== undefined) row.registers[name] = value; | |
| 38 | } | |
| 39 | } | |
| 40 | for (const row of rows) { | |
| 41 | const missing = REGISTERS.filter((name) => !(name in row.registers)); | |
| 42 | if (missing.length === REGISTERS.length) row.error = "no answer"; | |
| 43 | else if (missing.length) row.error = `no answer for ${missing.join(", ")}`; | |
| 44 | } | |
| 45 | return rows; | |
| 46 | } |
new above
offline