Real-time brain-computer interface (BCI) feedback loops demand deterministic latency, not just throughput. Most tutorials stop at plotting EEG waveforms. Shipping a closed-loop system that triggers a stimulus within 20ms of a detected event requires rethinking the entire stack from driver to decoder.
Architecture: Separate Acquisition from Inference
OpenBCI boards stream packets over USB serial or WiFi at 250–1600 Hz. Treat the board as a black-box producer. Write a dedicated acquisition thread that validates packet integrity, timestamps with a monotonic clock, and pushes raw samples into a lock-free ring buffer. The inference thread pulls fixed-size epochs, applies filtering, runs the classifier, and writes commands to an output queue. This decoupling prevents GC pauses or model loading from dropping EEG packets.
Signal Conditioning in Zero-Copy Rust
Avoid allocating new buffers for every filter stage. Implement a streaming DSP pipeline using the `dasp` crate where each stage (notch, bandpass, CAR) implements `Process` and mutates a pre-allocated frame buffer in place. A 4th-order Butterworth bandpass (0.5–40 Hz) plus a 60 Hz IIR notch runs at <50µs per 8-channel frame on a Raspberry Pi 4.
Decoding: CSP + LDA for Motor Imagery
Common Spatial Patterns (CSP) projects multi-channel EEG onto variance-maximizing components. Train offline with scikit-learn, export the spatial filters and LDA weights as JSON, then load them at runtime. The online path is a single matrix multiply: `features = epoch.T @ csp_filters`, followed by `score = features @ lda_weights + bias`. No heavy linear algebra crates needed—`nalgebra` or even a hand-rolled 8x8 multiply suffices.
| Stage | Latency (ms) | CPU % (Pi 4) |
|---|---|---|
| Acquisition + USB | 1.2 | 3 |
| DSP Pipeline | 0.8 | 5 |
| CSP + LDA | 0.3 | 2 |
| Total Loop | 2.3 | 10 |
Closing the Loop: Stimulus Triggering
The output queue drives actuators—visual flicker, haptic vibration, or robotic exoskeleton. Use a real-time GPIO library (`rppal` on Pi, `gpio-cdev` on Linux) to assert a TTL line within the inference thread. Log the round-trip timestamp (EEG sample -> classifier decision -> GPIO rising edge) to a circular buffer for post-hoc jitter analysis. Target <20ms 99th percentile.
"Determinism beats speed. A 5ms loop with 50ms jitter is useless for neurofeedback.
— OpenBCI Community Forum
Deployment Checklist
1. Disable USB autosuspend: `echo -1 > /sys/module/usbcore/parameters/autosuspend`. 2. Set CPU governor to `performance`. 3. Run acquisition/inference as `SCHED_FIFO` priority 90 via `chrt`. 4. Validate with a hardware function generator feeding known sine waves into the OpenBCI Daisy module; measure end-to-end latency with an oscilloscope on the GPIO trigger.
✦
Start with the OpenBCI Rust SDK (`openbci-rs`), a `SegQueue`, and a 256-sample epoch. Get the loop running at 250 Hz on a laptop first. Then cross-compile for ARM, pin threads, and measure. The difference between a demo and a clinical tool is 18ms of jitter.








