Skip to content

RFC-0001 — The NXT web UI: drive, watch, record and run programs from a browser

Status: Proposed — describes what was built and checked on the robot on 2026-11-13/14, for the operator's review. On acceptance it becomes an ADR; the open questions in §7 become follow-ups. Builds on: IDEAS.md (the NXT as the first robot project), ECOSYSTEM.md (one interface per robot). Code: apps/nxt-web/, served by uv run nxt-web.

1. Motivation

The operator has a LEGO Mindstorms NXT 2.0 (stock firmware 1.28) built as a tracked Shooterbot, and wants to drive it from their own software, see what its sensors see, log data, and put programs on the brick that run on their own. LEGO's own NXT-G software is Windows/Mac only and was never part of the plan. The UI is the first piece of the robotics lab: the place where a person and the robot meet before any automation is trusted.

2. Goals and non-goals

Goals:

  • drive the robot by hand, from a desktop or a phone, safely;
  • live sensor and motor readings, charts, and recordings to CSV;
  • manage programs on the brick: upload, run, stop, read the logs they write;
  • configure which port does what, without editing code;
  • show how the robot is connected and whether the link is healthy;
  • keep a log of every command sent.

Non-goals (v1):

  • autonomy — that is the robot brain (RFC-0002);
  • several robots, or several users at once;
  • access beyond the local machine (it binds 127.0.0.1; see §7);
  • replacing the brick's firmware.

3. Proposal (as built)

3.1 Shape

  • Server: Python standard library only (http.server, threads); no web framework and no new runtime dependency beyond nxt-python 3.5.1.
  • Page: one HTML file, no build step, no outside libraries; the chart is drawn on a canvas. Light and dark themes; works at phone width.
  • Robot backends: NxtRobot (the real brick) and FakeRobot (a simulated brick with made-up readings, --fake), behind the same methods, so everything above them is tested without hardware.

3.2 Tabs

Tab What it does
Drive Hold-to-drive pad (arrows / WASD), power slider, fire, live motor readout
Sensors & log Live readings, chart (pick series, time window), recording to runs/*.csv, re-plot or download runs, the command log
Programs Files on the brick and free space; upload, run, stop, view or plot a CSV a program wrote, download, delete
Setup Connection & diagnostics; which port is left/right/shooter, inverting a side, which sensor on each port, samples per second (saved to config.json)

STOP ALL in the header stops the drive motors and any program on the brick.

3.3 Safety

The NXT's stock firmware has no deadman: a motor keeps its last command. So:

  1. Hold-to-drive with a watchdog. The page resends the drive command every 200 ms while a direction is held; the server stops the motors when none has arrived for 0.6 s (tab closed, connection lost, button released without a stop).
  2. Stopping the server stops the motors — on Ctrl-C and on SIGTERM. SIGTERM was added after incident 1 (§6) and checked on the brick: drive at power 30, SIGTERM within 0.2 s, all three motors idle.
  3. A program on the brick has the robot. While one runs, driving and fire are refused, and the server stops reading sensors (reading can change a sensor's mode under the program); motor counters are still logged.
  4. Power is clamped to ±100, upload names are checked against the brick's file-name rules, and every request body is limited to 256 KB.
  5. Motors are stopped on every connect (server start, reconnect, link switch), and the command log records "stop on connect" with the ports that still had power: after a lost link or a crash the brick keeps its last power while a fresh connection believes it stopped (review 2, MAJ-3).
  6. The shooter is off unless switched on in Setup (shooter_enabled, default off; Fire is disabled until then): its 2 cm balls are small enough for a dog to swallow.

What it cannot cover:

  • a hard crash of the server, or a lost link, leaves the motors running until the next connect — unless robotd-lite drives them: the deadman on the brick (RFC-0003 §2.3), the default drive mode since 2026-11-14 (not yet checked on the real brick);
  • no obstacle stop in direct drive mode: the ultrasonic is sampled for display only (2–5 times a second); under robotd-lite forward motion stops closer than 15 cm, on the brick;
  • the program check is up to 1 s stale, so a program started from the brick's own menu can be driven over for up to a second;
  • fire() holds the robot's lock for the whole turn, so a watchdog stop waits behind it.

3.4 Data

  • Live: a sampler reads motors and sensors at the configured rate into a ring buffer the page polls.
  • Recording: each sample to apps/nxt-web/runs/run-<date>-<time>.csv (not committed): time, motor powers, three tacho counts, one column per configured sensor, the running program.
  • Command log: every command with time and details — drive changes (one entry when a direction is held, not one per keepalive), stops, watchdog stops, fire, programs, uploads, setup changes, connects and lost links, failures marked — shown in the UI and appended to apps/nxt-web/runs/events.jsonl (events-fake.jsonl for the simulated brick).
  • Links: USB, or Bluetooth through /dev/rfcomm* (nxt-python's device-file backend). auto tries USB first; the Setup tab switches link (stopping the motors first).
  • Header: current link and median command time next to the battery.
  • Diagnostics: firmware, command times (median, 90 %, max) and errors, actual vs configured sample rate, time connected, whether the NXT is on USB, the rfcomm binding and its pairing and connection state, and a link speed test.

3.6 Programs on the brick

NXC programs are compiled by Debian's nbc in Docker (apps/nxt-web/toolchain/, scripts/nxc.sh) and uploaded from the Programs tab. first (drive and log) ran and logged correctly; chase and patrol (dog games) are compiled, not yet run. Programs run with no link at all; a link is only needed to upload and to read their logs.

4. How it was checked

  • Smoke test against the simulated brick: every endpoint, the watchdog, clamping, recording, path-traversal refusal, programs, the command log, diagnostics, and SIGTERM taking the stop path (that check fails on the code before the fix), stop on connect, the shooter lockout, and a bad since= answered with 400. It skips its real-hardware checks whenever a brick may be reachable (USB or any /dev/rfcomm*). make test-unit.
  • Image: make test-integration builds the Docker image and checks it answers with the simulated brick.
  • Hardware checks (make test-hardware, manual, moves the tracks briefly): sensors, link speed, stop on SIGTERM, stop on connect.
  • On the robot (2026-11-14): each motor turned and identified (A shooter, B right, C left, positive power forward), sensors read (colour on 3, ultrasonic on 4, which reports "Sonar"), the operator drove and recorded over USB, then over Bluetooth; first.rxe ran and its log was read back; diagnostics and the link test checked over Bluetooth.
  • Measured: about 41 ms per command over Bluetooth with nothing else running; about 170–190 ms median while live sampling shares the link; the sampler reaches 1.96 of 2 samples/s over Bluetooth.

Not covered by any test: switching link on a real brick (the simulated brick ignores it); the lost-link path; fire refused while a program runs; the watchdog's failure branch; SIGTERM while a Bluetooth read is stalled; the image passing USB or rfcomm devices through (it is only tested with the simulated brick); hw_stop_on_connect.py has not yet been run on the brick.

5. Alternatives considered

  • A web framework (FastAPI, Flask) with WebSockets: smoother live updates, but new dependencies; polling at 300 ms is enough at these rates.
  • nxt-python's own nxt-server: a byte pipe to the brick, no UI; kept in mind as the protocol for a Wi-Fi bridge (RFC-0003).
  • Replacing the firmware (leJOS): more capable programs on the brick, but a reflash; rejected for now, NXC on the stock firmware covers what is needed.
  • A desktop app: no phone access; rejected.

6. Incidents (2026-11-14)

  1. The smoke test's "no brick" section ran against the plugged-in brick and left both tracks at power 50 after the server was killed (about a minute; the robot was lifted). Fixed: SIGTERM stops the motors; the test skips real-hardware checks when a brick is reachable.
  2. With USB unplugged, the same section reached the brick over Bluetooth; its second connection hung the live server. Motors were idle. Fixed in the test; the limit underneath is open (§7).

7. Open questions

  1. A deadman on the brick — the only fix for a crashed server or a lost link (RFC-0003 §2.3). Built as robotd-lite (2026-11-14); open until it passes hw_deadman.py on the brick.
  2. One connection at a time. A second program on the same link scrambles replies, and Bluetooth reads have no timeout, so a stalled link hangs the server and its watchdog. Needs a read timeout and, ideally, the NXT service as the only owner of the link.
  3. The Docker image has no rfcomm or bluetoothctl, so the Bluetooth part of Diagnostics is empty inside it, and it runs as root (to reach the device nodes).
  4. Access from other devices. Today: an ssh tunnel over the tailnet. Opening the port needs a firewall rule and, before that, some form of login — the UI can drive a robot.
  5. The JSON-RPC interface of RFC-0003: the UI would become one client of it rather than having its own HTTP API.
  6. More sensors and motors (multiplexers, a second brick; NXT-PARTS.md): the Setup tab offers LEGO's six sensor types only.

8. Discussion

  • 2026-11-13 — First version (drive pad, watchdog) asked for by the operator; extended to the four tabs, recording and programs; NXC toolchain in Docker chosen by the operator over an AUR install.
  • 2026-11-14 — First run on the robot over USB; ports checked; incident 1 and the SIGTERM fix; Bluetooth paired and used; the operator asked for the link next to the battery, diagnostics, and a command log; incident 2. The operator asked for this RFC.
  • 2026-11-14 — The operator asked for flotilla-style make targets, a target to serve the UI, and a Docker image "so we can deploy it anywhere": make nxt-web / nxt-web-fake, apps/nxt-web/Dockerfile (make nxt-image, nxt-image-run; NXT_LAB_DATA points config and recordings at a volume), the smoke test as make test-unit, the image checked by make test-integration, and the hardware checks as make test-hardware (manual; it moves the tracks briefly).