Inspiration
Learned controllers track a target well, and nobody can prove they will never do something dangerous. Safety standards still ask for that proof. The classic answer is a Simplex architecture (ASTM F3269 names the roles input manager, safety monitor, switch and recovery controller): let the complex controller work, and let a simple one you can verify take over the moment it misbehaves.
I wanted to see how much of that guarantee a real-time operating system can carry, instead of trusting good behaviour inside one process. A ventilator was the clearest example: a pressure loop that must keep running, and a patient for whom a stuck or runaway controller is the worst case. The patient in this project is simulated.
What it does
A small neural network drives a simulated ventilator. Between the network and the blower sits an arbiter. Every command the network proposes is checked: is it fresh, finite, in range, not rising too fast, silent during expiration, and does a lookahead through the lung model say the breath will stay under the volume limit? If any check fails, a verified recovery controller takes over, and the network is readmitted only at a breath boundary after a run of healthy proposals.
The network cannot go around the arbiter. It runs as an ordinary user with its abilities locked, and the actuator device is root-only, so the kernel refuses its open(). From a dashboard I can inject nine faults: NaN, spike, drift, stuck, slow, hang, crash, CPU hog and an attempt to open the actuator. A model can be swapped in at run time, and a switch turns Tether off so you can see what the same fault does without it.
How I built it
- Portable C11 core (no I/O, no allocation in the control path): the arbiter, the safety monitor, a sensor-only estimator of lung compliance and resistance, the MLP and the real-time primitives. The same code runs in a host simulator, in 775 unit checks, and inside the QNX arbiter.
- Six QNX processes.
tether-hwis a resource manager serving/dev/tether/{sensor,actuator,plant}with a physics thread and a hardware watchdog.tether-arbiterruns the control loop from a timer pulse at SCHED_FIFO 60.tether-aiis the network: it starts as root, locks its abilities withprocmgr_ability(), then drops to uid 2002.tetherdrestarts a crashed AI and kills a hung one.tether-bridgeserves the dashboard as an unprivileged user.tether-ctlis the command-line tool. - A bootable QNX 8.0 image built with
mkqnximagethat starts straight into Tether, plus a self-test that runs on the live system and prints PASS or FAIL. - Training: a privileged teacher that knows the patient's true lung mechanics generates about 495,000 samples; a NumPy trainer writes a 1,377-weight model that runs from static memory in about 0.15 ms.
QNX features used: resource manager (resmgr, iofunc, devctl); kernel permission checks on open(); procmgr_ability() plus setuid(); message passing with name_attach(); the kernel's disconnect pulse to learn an AI process died; timer pulses with SCHED_FIFO and a priority-inheriting mutex; TimerTimeout(); posix_spawn/waitpid supervision; slog2; mkqnximage.
Challenges I ran into
- No QNX host tools for Apple silicon. SDP 8.0 hosts are x86 Linux or Windows, so I cross-compiled in an x86_64 container and ran QNX in QEMU. QEMU's Apple-silicon accelerator stops in the QNX kernel start-up, so everything runs under software emulation and the control loop is 250 Hz, not the 1 kHz my defaults assume.
- A deadline that does not fire.
TimerTimeout()cannot release a client that is reply-blocked on a channel created with_NTO_CHF_UNBLOCK, which everyname_attach()channel is. I found it by testing: a silent AI control channel wedged my dashboard gateway. Calls into the AI now go through a helper thread, and a regression test keeps it fixed. - A first model that fought its own safety net. It read its own last output, so it oscillated against the fallback. It now reads a sensor-only estimate of the lung.
- Logging from a high-priority thread. A refused-open counter that wrote to disk let a hostile AI slow the control loop. The hardware resource manager now logs only to memory.
Accomplishments I am proud of
- 29 pass/fail checks on the running QNX system, all passing: every injected fault is contained, a crashed or hung AI is restarted, killing the arbiter or the hardware makes the supervisor restart everything, and the AI's user is refused all five operator actions. Outside the deliberate guard-off test there is no violation of the safety envelope.
- With the AI saturating both CPUs, the control loop's wake-up latency p50 and p99 do not move (p99 1.91 ms against 2.01 ms idle, on emulation).
- With Tether switched off, the same
spikefault produces about 1,250 ticks over the limits. A model biased to overshoot reaches 44 cmH2O and 1.9 L on its own, and stays at 14 cmH2O and 0.38 L behind Tether. - A hands-on codelab built from the same pieces, submitted to QNX: https://github.com/qnx/codelabs/pull/73
What I learned
Put the guarantee where the AI cannot reach it. A monitor inside the AI's process is a suggestion; a root-only device node and a locked set of abilities are enforcement. And test the failure you think you have handled: my "every call has a deadline" claim was wrong until a silent server proved it.
What's next
Real hardware and real timing instead of emulation; a formal proof of the monitor's envelope; a recovery controller that ventilates a stiff lung better than a fixed 9 cmH2O; validating the model-swap workflow against a real change-control process.