Public: anyone can read this; the crew can change it.

Migrated from github.com/Trevato/SO-ARM100

workbenchthis machine

The arm server serves the Pi's temperature and under-voltage flags at GET /health/pi, read from sysfs, never a shell #540

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/531-the-arm-server-serves-the-pi-s-temperatu → main 2 commits fork 2 threads · 2 unresolved

  • Merged. smith/531-the-arm-server-serves-the-pi-s-temperatu was merged into main as 736c70a.

Review

Sign in to review this change.
5 files changed +110 −0
Software/so101/README.md +4
@@ -2,6 +2,10 @@
22
33 `bench/fake-serve.ts` is the hosted app (the page, the room, the relay); `so101 serve` is the arm server on the Pi.
44
5 ## The Pi's own health
6
7 `GET /health/pi` on the arm server answers `{ tempC, underVoltage, underVoltageSince, throttled, at }`, read from `/sys/class/thermal/thermal_zone0/temp` and `/sys/devices/platform/soc/soc:firmware/get_throttled` (src/pi-health.ts; never a shell). `underVoltage` is bit 0x1 (now), `underVoltageSince` bit 0x10000 (since boot), `throttled` any of 0x2, 0x4, 0x8 (now). Anything the files do not give is `null`, so off a Pi it is 200 with nulls. The hosted app passes the route to the arm, so it shows once the Pi runs this commit.
8
59 ## Keeping the Pi at the deployed commit
610
711 After each production deploy the app asks the Pi's `GET /version`. If it differs from the app's commit, and nobody holds a turn, the app:
Software/so101/src/pi-health.test.ts added +40
@@ -0,0 +1,40 @@
1 import { expect, test } from "bun:test";
2 import { mkdtempSync, writeFileSync } from "node:fs";
3 import { tmpdir } from "node:os";
4 import { join } from "node:path";
5 import { readPiHealth } from "./pi-health.ts";
6
7 const dir = mkdtempSync(join(tmpdir(), "pi-health-"));
8 const file = (name: string, content: string) => {
9 const p = join(dir, name);
10 writeFileSync(p, content);
11 return p;
12 };
13 const at = () => 42;
14
15 test("a normal reading", () => {
16 const h = readPiHealth({ temp: file("t1", "48312\n"), throttled: file("g1", "0x0\n") }, at);
17 expect(h).toEqual({ tempC: 48.312, underVoltage: false, underVoltageSince: false, throttled: false, at: 42 });
18 });
19
20 test("under-voltage now (0x1) and since boot (0x10000)", () => {
21 const now = readPiHealth({ temp: file("t2", "50000"), throttled: file("g2", "0x5\n") }, at);
22 expect(now.underVoltage).toBe(true);
23 expect(now.throttled).toBe(true);
24 const since = readPiHealth({ temp: file("t2", "50000"), throttled: file("g3", "0x10000\n") }, at);
25 expect(since.underVoltage).toBe(false);
26 expect(since.underVoltageSince).toBe(true);
27 expect(since.throttled).toBe(false);
28 });
29
30 test("missing files are null", () => {
31 const h = readPiHealth({ temp: join(dir, "nope"), throttled: join(dir, "nope2") }, at);
32 expect(h).toEqual({ tempC: null, underVoltage: null, underVoltageSince: null, throttled: null, at: 42 });
33 });
34
35 test("garbage content is null", () => {
36 const h = readPiHealth({ temp: file("t4", "hot"), throttled: file("g4", "0xzz; rm -rf /") }, at);
37 expect(h.tempC).toBeNull();
38 expect(h.underVoltage).toBeNull();
39 expect(h.throttled).toBeNull();
40 });
Software/so101/src/pi-health.ts added +55
@@ -0,0 +1,55 @@
1 /**
2 * The Pi's own health, read from the kernel's files and never a shell: the SoC temperature and the
3 * firmware's throttled bits. Anything missing (not a Pi, unreadable, garbage) is null.
4 */
5 import { readFileSync } from "node:fs";
6
7 export const TEMP_PATH = "/sys/class/thermal/thermal_zone0/temp";
8 export const THROTTLED_PATH = "/sys/devices/platform/soc/soc:firmware/get_throttled";
9
10 const UNDER_VOLTAGE_NOW = 0x1;
11 const THROTTLED_NOW = 0x2 | 0x4 | 0x8; // arm frequency capped, throttled, soft temperature limit
12 const UNDER_VOLTAGE_SINCE_BOOT = 0x10000;
13
14 export interface PiHealth {
15 tempC: number | null;
16 underVoltage: boolean | null;
17 underVoltageSince: boolean | null;
18 throttled: boolean | null;
19 at: number;
20 }
21
22 export interface PiHealthPaths {
23 temp?: string;
24 throttled?: string;
25 }
26
27 function readText(path: string): string | null {
28 try {
29 return readFileSync(path, "utf8").trim();
30 } catch {
31 return null;
32 }
33 }
34
35 function parseTemp(text: string | null): number | null {
36 if (text === null || !/^-?\d{1,7}$/.test(text)) return null;
37 return Number(text) / 1000;
38 }
39
40 function parseBits(text: string | null): number | null {
41 if (text === null || !/^(?:0x)?[0-9a-fA-F]{1,8}$/.test(text)) return null;
42 return parseInt(text, 16);
43 }
44
45 export function readPiHealth(paths: PiHealthPaths = {}, now: () => number = Date.now): PiHealth {
46 const tempC = parseTemp(readText(paths.temp ?? TEMP_PATH));
47 const bits = parseBits(readText(paths.throttled ?? THROTTLED_PATH));

Fine as is. at is wall-clock ms, and the test pins it with an injected clock.

48 return {
49 tempC,
50 underVoltage: bits === null ? null : (bits & UNDER_VOLTAGE_NOW) !== 0,
51 underVoltageSince: bits === null ? null : (bits & UNDER_VOLTAGE_SINCE_BOOT) !== 0,
52 throttled: bits === null ? null : (bits & THROTTLED_NOW) !== 0,
53 at: now(),
54 };
55 }
Software/so101/src/server.test.ts +9
@@ -32,6 +32,15 @@ beforeAll(async () => {
3232 afterAll(() => Promise.race([server.close(), sleep(500)]));
3333
3434 describe("so101 serve", () => {
35 test("GET /health/pi answers 200 JSON with its five keys, null where the Pi's files are absent", async () => {
36 const res = await fetch(`${base}/health/pi`);
37 expect(res.status).toBe(200);
38 expect(res.headers.get("content-type")).toContain("application/json");
39 const body = (await res.json()) as Record<string, unknown>;
40 expect(Object.keys(body).sort()).toEqual(["at", "tempC", "throttled", "underVoltage", "underVoltageSince"]);
41 expect(typeof body.at).toBe("number");
42 });
43
3544 test("reports status and limits", async () => {
3645 const status = (await (await fetch(`${base}/status`)).json()) as { joints: Array<{ id: number; position: number }> };
3746 expect(status.joints.map((j) => [j.id, j.position])).toEqual([
Software/so101/src/server.ts +2
@@ -85,6 +85,7 @@ import { clamp, type Frame } from "./mapping.ts";
8585 import { MachineLog, perServo } from "./machine-log.ts";
8686 import { Metrics } from "./metrics.ts";
8787 import { wallMs } from "./clock.ts";
88 import { readPiHealth } from "./pi-health.ts";
8889 import { ARRIVE_TICKS, LINK_HOPS, parseSnapBack, READ_BACK_HOP, RELAY_HOP, type Cause, type Tracker } from "./precision.ts";
8990 import { deployedCommit, PrecisionHistory } from "./precision-history.ts";
9091 import { Tuning, TuningRefused } from "./tuning.ts";
@@ -763,6 +764,7 @@ export async function startServer(bus: FeetechBus, options: ServerOptions = {}):
763764 return "\0"; // never a file
764765 }
765766 };
767 if (req.method === "GET" && url.pathname === "/health/pi") return json(res, 200, readPiHealth());

Untested wiring: add a server.test.ts case for GET /health/pi (200, application/json, keys tempC/underVoltage/underVoltageSince/throttled/at). The preview currently 404s here.

766768 if (req.method === "GET" && url.pathname.startsWith("/model/") && modelDir) {
767769 const path = safeFile(resolve(modelDir), decode(url.pathname.slice("/model/".length)));
768770 if (!path || ![".urdf", ".stl", ".dae", ".png", ".jpg"].includes(extname(path).toLowerCase())) return json(res, 404, { error: "no such model file" });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs