sensors — API Reference¶
Each sensor package registers itself via @register("type_key"). The classes below are the public plugin interface.
tf_mini¶
sensors.tf_mini.sensor.TFminiSensor(sensor_id, config)
¶
Bases: SensorBase
Benewake TF Mini Plus LiDAR over UART.
Config connection fields
type: uart port: /dev/ttyAMA0 (or /dev/ttyUSB0, etc.) baud_rate: 115200
Source code in sensors/tf_mini/sensor.py
sensors.tf_mini.driver
¶
TF Mini Plus LiDAR — serial frame parser.
9-byte frames at 115200 baud.
Byte 0–1: 0x59 0x59 (header) Byte 2–3: distance (cm, little-endian uint16) Byte 4–5: signal strength (little-endian uint16) Byte 6–7: chip temperature raw (little-endian uint16) Byte 8: checksum (sum of bytes 0–7, lowest byte)
parse_frame(frame)
¶
Parse a 9-byte TF Mini Plus frame.
Returns (distance_cm, strength, temp_c) on success, None on bad frame.
Source code in sensors/tf_mini/driver.py
lv_maxsonar¶
sensors.lv_maxsonar.sensor.LVMaxSonarSensor(sensor_id, config)
¶
Bases: SensorBase
MaxBotix LV-MaxSonar-EZ ultrasonic distance sensor over UART (direct).
Config connection fields
type: uart port: /dev/ttyS0 baud_rate: 9600
The MB1010 TX is inverted (RS232-format, idles LOW); reading it directly on an SBC UART needs a hardware inverter (74HC14 / 2N3904) on the TX line. See README.
Source code in sensors/lv_maxsonar/sensor.py
sensors.lv_maxsonar.driver
¶
LV-MaxSonar-EZ ultrasonic sensor — ASCII serial protocol parser.
ASCII frames at 9600 baud, CR-terminated.
Format: R
parse_line(raw)
¶
Parse one ASCII frame.
Returns distance in inches on success, None on malformed input.
Source code in sensors/lv_maxsonar/driver.py
vl53l1x¶
sensors.vl53l1x.sensor.VL53L1XSensor(sensor_id, config)
¶
Bases: SensorBase
ST VL53L1X time-of-flight distance sensor over I2C.
Config connection fields
type: i2c bus: 3 (I2C bus number, e.g. /dev/i2c-3) address: 0x29 (default VL53L1X address)
Ranging modes (1=short ~1.3 m, 2=medium ~2 m, 3=long ~4 m) trade maximum range for speed and ambient-light immunity. The sensor starts in long mode. Two runtime controls are exposed (via node/routers/vl53l1x.py):
set_mode(m) — pin a fixed mode (also turns auto off) set_auto(on) — let the sensor pick the tightest mode that covers the current distance, with hysteresis to prevent flapping
Both are thread-safe: the caller only stages intent; the background read loop is the single thread that ever touches the I2C bus.
Source code in sensors/vl53l1x/sensor.py
mode_info()
¶
Current control state — used by the state endpoint and named events.
Source code in sensors/vl53l1x/sensor.py
set_auto(enabled)
¶
Enable or disable distance-driven auto mode selection.
Source code in sensors/vl53l1x/sensor.py
set_mode(mode)
¶
Pin a fixed ranging mode (1/2/3). Disables auto-ranging.
Source code in sensors/vl53l1x/sensor.py
sensors.vl53l1x.driver.VL53L1X(bus, addr=41)
¶
Low-level smbus2 driver for the ST VL53L1X ToF sensor.
Usage
bus = smbus2.SMBus(3) sensor = VL53L1X(bus, addr=0x29) sensor.init() sensor.set_distance_mode(3) # long range sensor.start_continuous() while True: mm = sensor.read_mm() # returns int or None
Source code in sensors/vl53l1x/driver.py
init()
¶
Load default config blob and wait for firmware boot.
Source code in sensors/vl53l1x/driver.py
read_mm(timeout=1.0)
¶
Block until a measurement is ready and return distance in mm. Returns None on ranging error or timeout.
Source code in sensors/vl53l1x/driver.py
set_distance_mode(mode)
¶
Set ranging mode (1=short, 2=medium, 3=long). Default config is mode 3.
Source code in sensors/vl53l1x/driver.py
mlx90640¶
sensors.mlx90640.sensor.MLX90640Sensor(sensor_id, config)
¶
Bases: SensorBase
Melexis MLX90640 32×24 IR thermal array over I2C.
Config connection fields
type: i2c bus: 3 (I2C bus number) address: 0x33 (default MLX90640 address)
Two output lanes
JSON reading (/sensors/
Source code in sensors/mlx90640/sensor.py
sensors.mlx90640.driver.MLX90640(bus, addr=51, refresh_hz=8)
¶
Complete MLX90640 driver implementing datasheet §11 calibration compensation.
Reads EEPROM calibration data on construction; subsequent get_frame() calls fill a 768-element list with temperatures in °C.
Source code in sensors/mlx90640/driver.py
get_frame(out)
¶
Fill out[768] with temperatures in °C (updates both subpages). out must be a pre-allocated list of 768 floats.
Source code in sensors/mlx90640/driver.py
mq_array¶
sensors.mq_array.sensor.MqArray(sensor_id, config)
¶
Bases: AnalogArrayBase
An array of MQ-series gas sensors, read as raw ADC over an MCU uplink.
Wiring (which gas is on which channel) and per-unit calibration live in the
sensor's channels config — authored consistently with the MCU's forge
contract (config/mcus/
Source code in core/analog_array.py
sensors.mq_array.driver
¶
MQ gas-sensor math — pure functions, no I/O, no hardware.
MQ-series sensors are resistive: a heated element's resistance Rs falls as target gas rises. The board reads the voltage across a load resistor RL, which the MCU digitizes to a raw ADC count. Datasheet ppm curves are all plotted against the ratio Rs/R0, where R0 is the sensor's resistance in clean air (per-unit, measured during calibration). These helpers convert raw counts → that ratio; turning the ratio into ppm needs per-gas curve constants and is deferred.
rs_over_r0(raw, rl_ohms, r0_ohms, adc_max=ADC_MAX)
¶
Rs/R0 ratio — the x-axis of every MQ datasheet ppm curve.
Needs the load resistor RL and the clean-air baseline R0 (both ohms). Returns None when the inputs are unusable (raw <= 0 or r0 <= 0).
Source code in sensors/mq_array/driver.py
rs_over_rl(raw, adc_max=ADC_MAX)
¶
Rs/RL from a raw ADC count: (adc_max - raw) / raw.
Returns None for raw <= 0 (open input / no reading) to avoid divide-by-zero.
Source code in sensors/mq_array/driver.py
pressure_array¶
sensors.pressure_array.sensor.PressureArray(sensor_id, config)
¶
Bases: AnalogArrayBase
A logical pressure surface read across one or more MCU devices.
Two shapes, decided by config:
Flat array (no row_tag channels): every channel is a static tap;
AnalogArrayBase composes the per-device frames into one reading and
enrich adds kPa for linear-calibrated channels. The original
cranial-pressure shape.
Scanned lattice (params.rows + one row_tag channel per device):
the velostat lattice is energized one row at a time by a conductor MCU
(see mcu/circuit_python/modules/matrix_scan), so each device frame is
one row's worth of that device's columns, tagged with the row index in
its row_tag channel (-1 = the follower saw no conductor). This class
accumulates rows per device, freezes a device's sweep when its row
counter wraps, and when every device has a frozen sweep composes the
full rows x columns grid:
- JSON lane: latest raw values + a
sweepsummary (n / complete / missing cells / follower timeouts) — light, for status + scalars. - Binary frame lane (
/sensors/<id>/frames): the full grid as float32, header<IHHff= (sweep_n, rows, cols, min, max), row-major, NaN for cells a device never delivered. Columns are the non-row_tag channels in config order.
A device that laps the others (double wrap before composition) forces a
partial emit so a dead follower degrades to complete: false, never to
silence — mirroring the firmware's -1 sentinel philosophy.
Source code in sensors/pressure_array/sensor.py
sensors.pressure_array.driver
¶
Pressure transfer math — pure functions, no I/O.
to_kpa(count, scale, offset=0.0)
¶
Linear transfer from a raw ADC count to kPa: count * scale + offset.
scale/offset come from the transducer datasheet + the ADC reference, per
channel. (Non-linear transducers can extend this with their own kind.)
Source code in sensors/pressure_array/driver.py
ir_xcvr¶
sensors.ir_xcvr.sensor.IrXcvrSensor(sensor_id, config)
¶
Bases: SensorBase
IR transceiver sensor wrapping TSOP38238 (RX) and TSAL6200 (TX).
Config example::
- id: ir_xcvr
type: ir_xcvr
enabled: true
connection:
type: ir
rx_device: /dev/lirc0
tx_device: /dev/lirc1
Source code in sensors/ir_xcvr/sensor.py
transmit(protocol, address, command)
¶
Transmit one IR code.
Parameters¶
protocol : "NEC" or "NECX" address : 0-255 (NEC) / 0-65535 (NECX) command : 0-255
Returns¶
bool True on success; False if TX unavailable or the write failed.
Source code in sensors/ir_xcvr/sensor.py
analog_in¶
sensors.analog_in.sensor.AnalogIn(sensor_id, config)
¶
Bases: SensorBase
Heterogeneous scalar analog inputs read through a pull device (an ADS1115).
Unlike an array sensor, each channel is its own signal with its own meaning
and calibration (e.g. 4 different sources on one Pi02W ADS1115). Polls each
configured channel via the device's read_channel(); emits raw counts always,
plus a calibrated value per channel whose calibration is linear.
Source code in sensors/analog_in/sensor.py
board_temp¶
sensors.board_temp.sensor.BoardTemp(sensor_id, config)
¶
Bases: SensorBase
SBC board/CPU temperatures from Linux thermal zones (sysfs).
No external hardware and no device — reads /sys/class/thermal/thermal_zone*/temp.
Emits one key per zone (zone0_c, zone1_c, …) plus cpu_c (the primary, zone0).
A common input to a cooling-control policy.
Source code in sensors/board_temp/sensor.py
fan_tach¶
sensors.fan_tach.sensor.FanTach(sensor_id, config)
¶
Bases: AnalogArrayBase
Fan RPM streamed from an MCU's tach (FG) counters.
A thin AnalogArrayBase: the device already streams RPM (the firmware tach
module counts FG edges and converts), so the raw frame value is the RPM —
no calibration. enrich just mirrors raw under rpm for a friendly key.
Source code in core/analog_array.py
servo_feedback¶
sensors.servo_feedback.sensor.ServoFeedback(sensor_id, config)
¶
Bases: AnalogArrayBase
Proprioception: servo pot-wiper taps read back as joint angles.
Device-fed (no connection): channels arrive over an MCU serial uplink
(e.g. the cervical SAMD21's analog_in pins) or an SBC-side ADS1115 (the
ear pots on the pizero). The taps are voltage-divided to the ADC rail —
the divider ratio is part of the calibration end points, never firmware.
Calibration {type: servo_pot, counts_min, counts_max, deg_min, deg_max}:
deg = deg_min + (count - counts_min) / (counts_max - counts_min)
* (deg_max - deg_min), clamped to [deg_min, deg_max]
Endpoints are measured at bench (command the servo to each soft limit and
record the counts). Raw counts are always emitted alongside.
Source code in core/analog_array.py
current¶
sensors.current.sensor.CurrentSensor(sensor_id, config)
¶
Bases: AnalogArrayBase
Rail current from hall-effect sensors (ACS712 et al.) — interoception.
Device-fed: the ACS712's analog output (Vcc/2 at zero current, ±mV/A)
arrives as raw ADC counts over an ads1115 device or MCU uplink. The
counts→amps meaning lives here, per channel:
Calibration {type: acs712, zero_counts, counts_per_amp}:
amps = (count - zero_counts) / counts_per_amp (signed)
zero_counts is captured at bench with the load off (it drifts with the
sensor's actual Vcc); counts_per_amp folds together the sensor's mV/A
(66 for the 30 A part) and the ADC's counts/mV at the configured gain.
The overcurrent guard observes the published amps signal — see
policies/threshold.
Source code in core/analog_array.py
radar_motion¶
sensors.radar_motion.sensor.RadarMotion(sensor_id, config)
¶
Bases: AnalogArrayBase
Microwave Doppler motion from analog-hacked RCWL-0516 modules.
The stock RCWL-0516 outputs a 2 s digital pulse; these units are hacked to expose the analog Doppler stage instead, so motion appears as deviation from a slowly-drifting resting level. Device-fed raw counts; meaning here:
Per channel, a slow EMA baseline tracks the resting level and the reading
publishes the absolute deviation plus a boolean over params.threshold:
level = |count - baseline| (counts)
motion = level > threshold
Config keys (params): baseline_alpha — EMA coefficient for the resting level (default 0.02; smaller = slower drift tracking) threshold — counts of deviation that count as motion (default 500; tune at bench — VERIFY against the hack's gain)
The baseline only adapts while quiescent (level under threshold), so a person standing in the field doesn't get absorbed into "resting".
Source code in sensors/radar_motion/sensor.py
audio_in¶
sensors.audio_in.sensor.AudioIn(sensor_id, config)
¶
Bases: SensorBase
Stereo audio capture (PCM1808 over the shared-clock I2S bus) — audition.
Two lanes, per the high-rate array convention:
- binary frame lane (/sensors/<id>/frames): every captured block,
header <IHHI> = (frame_id, channels, bits, rate) + raw interleaved
S16_LE samples — the waveform/feature stream for viewers and the
inference hub. ~2.3 Mbps at 48 kHz stereo: trivial for the network lane.
- JSON lane: rate-limited per-channel level summaries (rms / peak /
dBFS), throttled to ~params.json_hz (default 5) readings/s.
Connectionless (the I2S bus is OS plumbing, not a fleet-config wiring
detail); params: alsa_device ("hw:0,0"), sample_rate (48000),
channels (2), block_ms (50), json_hz (5).
Capture uses pyalsaaudio when present, else an arecord raw pipe — and on
a machine with neither (Windows dev) the sensor stays cleanly unhealthy.
Source code in sensors/audio_in/sensor.py
sensors.audio_in.driver
¶
ALSA capture + PCM block math for audio_in. Hardware I/O only — no threading, no HTTP. The DSP helpers are pure and unit-tested on any platform.
Capture backends, tried in order:
1. pyalsaaudio (alsaaudio) — in-process, lowest overhead.
2. arecord subprocess piping raw S16_LE — present on every ALSA board.
Both deliver interleaved signed 16-bit little-endian blocks.
AlsaCapture(device, rate, channels, block_frames)
¶
One opened capture stream delivering raw S16_LE blocks.
Source code in sensors/audio_in/driver.py
read()
¶
Block until one block is available; b'' on stream end.
Source code in sensors/audio_in/driver.py
block_stats(pcm, channels)
¶
Per-channel RMS + peak of one interleaved S16_LE block, as 0..1 of FS.
Pure function (the unit-tested meaning layer). Returns one {"rms": …, "peak": …, "dbfs": …} per channel; silence → dbfs -120.