Concept

Every sensor reading your Python code receives, and every motor command your Python code sends, travels through a five-stage pipeline that spans two processors, two buses, and three IPC mechanisms.

Understanding this pipeline helps you reason about latency (why sensor reads are always slightly stale), reliability (why retained channels exist), and architecture (why from raccoon_transport import Transport is the low-level API and get_transport() is what project services should use).

graph LR
    subgraph STM32["STM32F427 — Hard Real-Time"]
        HW["Physical sensor\nor motor"]
        ISR["ADC1 DMA (250 Hz)\nBEMF ADC2 (800 Hz)\nIMU DMP (50 Hz)\nDigital GPIO"]
        TX["TxBuffer\n(volatile packed struct,\nTRANSFER_VERSION 21)"]
    end

    subgraph SPIBus["SPI2 — Full-Duplex\n20 MHz, ~5 ms poll cycle"]
        WIRE["ioctl(SPI_IOC_MESSAGE)\nTxBuffer ⇄ RxBuffer\nBUFFER_LENGTH_DUPLEX_COMMUNICATION bytes"]
    end

    subgraph Reader["stm32-data-reader (C++, wombat ns)"]
        UNPACK["SpiReal::readSensorData()\nunpack + unit-convert\n(ADC counts → V, EMA filter)"]
        GATE["DataPublisher\nPublishGate 50 Hz cap\n+ L∞ noise epsilon"]
        PUB["LcmBroker\nserialise + dedup\nraccoon::Transport"]
    end

    subgraph SHM["raccoon_ring SHM\n(/dev/shm/raccoon_ring_*)"]
        RING["64-slot × 2 KiB ring\nper channel\nSeqLock, futex wake"]
    end

    subgraph Lib["raccoon-lib (Python)"]
        LCMR["TransportReader\nspinOnce() thread\nfutex_waitv"]
        CACHE["mutex-protected\nper-channel cache"]
        API["motor.get_position()\nanalog.read()\nimu.heading()"]
    end

    HW --> ISR --> TX
    TX --> WIRE --> UNPACK --> GATE --> PUB --> RING
    RING --> LCMR --> CACHE --> API

The reverse direction (Python command → motor output) follows the same path in reverse: Python publishes to a raccoon/motor/N/power_cmd transport channel, the reader’s CommandSubscriber picks it up, writes the RxBuffer, and the next SPI transfer delivers it to the STM32.

This page traces a complete data path from a physical sensor reading to a value accessed in user Python code, and a complete command path from Python code to motor movement.

Sensor Data Path (STM32 → Python)

Physical sensor
      │
      ▼
STM32 ADC / GPIO / IMU
      │  (interrupt-driven, µs latency)
      ▼
txBuffer (volatile struct in STM32 RAM)
      │  (updated after each measurement cycle)
      ▼
SPI2 DMA transfer to Raspberry Pi
      │  (circular DMA, Pi polls at main-loop rate)
      ▼
stm32-data-reader: SpiReal::readSensorData()
      │  (spi_update() call in C, copies RxBuf → TxBuf,
      │   returns SensorData struct)
      ▼
stm32-data-reader: DataPublisher::publishSensorData()
      │  (serialises fields to transport messages, publishes
      │   via raccoon::Transport using raccoon_ring SHM)
      ▼
raccoon_ring shared memory (/dev/shm/raccoon_ring_<channel>)
      │  (primary IPC; one ring-buffer file per channel)
      ▼
raccoon-lib: TransportReader (callbacks on the shared spin thread)
      │  (raccoon::Transport::spinOnce, updates caches)
      ▼
TransportReader caches (mutex-protected std::unordered_map
      │   / scalar fields)
      ▼
Python binding call (e.g., motor.get_position(),
      analog.read(), imu.heading())

Step 1: STM32 Hardware Layer

Analog sensors and battery: ADC1 runs in continuous circular DMA mode, started once at boot by startContinuousAnalogSampling(). DMA streams results into an oversampling accumulator continuously. TIM6 fires at ANALOG_OUTPUT_INTERVAL (4000 µs = 250 Hz): the accumulator is snapshotted, averaged, VDDA-corrected, and written to txBuffer. There is no separate ANALOG_SENSOR_SAMPLING_INTERVAL constant.

Digital sensors: readDigitalInputs() is called directly from HAL_SPI_TxRxCpltCallback and the result written into txBuffer.digitalSensors. Digital sensors are updated on every single SPI transaction.

BEMF and motor state: TIM6 calls stop_motors_for_bemf_conv() every 1250 µs (one motor at a time, round-robin). After 500 µs settle time, ADC2 DMA conversion runs for the current motor’s two channels. On ADC2 completion, BEMF is processed through the two-stage filter (median-of-3 + IIR α=0.2), offset-corrected, dead-zone applied, and dt-aware position integration runs. The motor control loop re-applies the PWM command. updatingMotorsInSpiBuffer() copies motor_data into txBuffer.motor after confirming SPI2 is idle. Each motor is measured every 4 × 1250 = 5000 µs (200 Hz effective per motor).

IMU: Main loop calls readImu(). When the MPL has new fused data, txBuffer.imu is updated.

Step 2: SPI Transfer

The Pi initiates SPI transactions by calling spi_update() in the C SPI layer. This performs a synchronous ioctl(SPI_IOC_MESSAGE) on /dev/spidev0.0 — a single system call that transfers BUFFER_LENGTH_DUPLEX_COMMUNICATION bytes (the max(sizeof(TxBuffer), sizeof(RxBuffer)) at compile time) in both directions simultaneously.

sequenceDiagram
    participant App as Application main loop
    participant DC as DeviceController
    participant SR as SpiReal
    participant C as C SPI layer (Spi.c)
    participant STM as STM32F427

    Note over App: every 5 ms (mainLoopDelay)
    App->>DC: processUpdate()
    DC->>SR: readSensorData()
    SR->>C: spi_update()
    C->>STM: ioctl(SPI_IOC_MESSAGE)\nPi sends RxBuffer\nSTM32 sends TxBuffer\n(full-duplex, 20 MHz)
    STM-->>C: TxBuffer (sensor data,\nupdateTime, TRANSFER_VERSION 21)
    C-->>SR: return true, rx_buffer populated
    SR->>SR: unpack TxBuffer fields\nbattery: ADC × 3.3 × 11 / 4096\nEMA alpha=0.05
    SR-->>DC: SensorData struct
    DC->>DC: lastSensorData_ = result
    DC-->>App: Result::success()
    App->>App: publishCurrentData()\nif lastUpdate changed

The Pi sends the current RxBuffer (commands: motor modes, targets, servo positions, PID gains, feature flags) while simultaneously receiving the STM32’s TxBuffer (sensor data, motor telemetry, IMU, odometry). Both directions happen in one system call with no gaps.

The protocol version field (TxBuffer.transferVersion = 21, TRANSFER_VERSION 21 in pi_buffer.h) is checked once at startup via spi_probe_version(). A mismatch triggers an automatic firmware reflash on the next update call.

The Pi’s stm32-data-reader calls spi_update() on every main-loop iteration. The default mainLoopDelay is 5 ms (Configuration::mainLoopDelay).

Step 3: stm32-data-reader → transport

SpiReal::readSensorData() unpacks the raw TxBuffer into a C++ SensorData struct with named fields. Unit conversions that happen here:

  • Battery voltage is converted from ADC counts to volts using the known 11× resistor divider and 3.3 V reference, then filtered with a 5% EMA.
  • Analog sensor values are passed as raw int16_t counts. No physical conversion is applied in the SPI layer.
  • Motor position reset is on-STM32 (via motorPositionReset bitmask + PI_BUFFER_UPDATE_MOTOR_POS_RESET); there are no Pi-side positionOffsets_ in the current code.

DataPublisher::publishSensorData() serialises each field as an transport message on the raccoon transport. All channel names use the raccoon/ prefix.

Publish-rate gating

Most IMU channels are capped to 50 Hz with an L-infinity noise-floor epsilon to reduce transport traffic. The accelerometer is intentionally ungated (publishForce) so downstream calibration routines receive every raw frame even when consecutive values are identical.

Channel classRate capNoise epsilon
Gyro50 Hz0.01 rad/s
Magnetometer50 Hz0.5 counts
Linear acceleration50 Hz0.05 m/s²
Accel velocity50 Hz0.05 m/s
DMP quaternion50 Hz0.001 (per-component)
Heading50 Hz0.05°
Temperature50 Hz0.1°C
Accelerometerungatednone
Odometry fields50 Hz0 (any change passes)

transport channel table

All topics below are on raccoon::Transport (shared-memory raccoon_ring). Message types are the raccoon:: types of raccoon-transport.

transport channelMessage typeContentRetained?
raccoon/gyro/valuevector3f_tangular velocity (rad/s)no
raccoon/accel/valuevector3f_tacceleration (m/s²), ungatedno
raccoon/linear_accel/valuevector3f_tgravity-removed accel (m/s²)no
raccoon/accel_velocity/valuevector3f_tintegrated accel velocity (m/s, decay 0.998/cycle)no
raccoon/mag/valuevector3f_tmagnetometer (raw counts)no
raccoon/imu/quaternionquaternion_tDMP 6-axis orientation (w, x, y, z)no
raccoon/imu/headingscalar_f_tdegrees, mag-corrected when calibratedyes
raccoon/imu/temp/valuescalar_f_tIMU temperature (°C)no
raccoon/gyro/accuracyscalar_i8_tgyro calibration accuracy 0–3no
raccoon/accel/accuracyscalar_i8_taccel calibration accuracy 0–3no
raccoon/mag/accuracyscalar_i8_tcompass calibration accuracy 0–3no
raccoon/imu/quaternion_accuracyscalar_i8_tquaternion accuracy 0–3no
raccoon/battery/voltagescalar_f_tbattery voltage (volts)yes
raccoon/analog/N/valuescalar_i32_traw ADC counts for port Nno
raccoon/digital/N/valuescalar_i32_t0 or 1 for digital port Nno
raccoon/bemf/N/valuescalar_i32_tfiltered + offset-corrected BEMF ticksyes
raccoon/motor/N/powerscalar_i32_tlast commanded duty (−399 to +399)yes
raccoon/motor/N/positionscalar_i32_taccumulated BEMF ticks (dt-aware)yes
raccoon/servo/N/modescalar_i8_tservo mode state (reader→UI, not commands)yes
raccoon/servo/N/positionscalar_f_tservo position (degrees)yes
raccoon/servo/N/feedbackvector3f_tbus servo: x=position (deg, multi-turn), y=speed (deg/s), z=load (% of max torque, signed)yes
raccoon/servo/N/healthvector3f_tbus servo: x=voltage (V), y=temperature (°C), z=error bits (−1 = no servo answers)yes
raccoon/servo/N/donescalar_i32_tbus servo: 0 = moving, 1 = reached, 2 = stalledyes
raccoon/odometry/pos_xscalar_f_tworld-frame x (meters)yes
raccoon/odometry/pos_yscalar_f_tworld-frame y (meters)yes
raccoon/odometry/headingscalar_f_theading (radians, CCW+)yes
raccoon/odometry/vxscalar_f_tbody-frame vx (m/s)yes
raccoon/odometry/vyscalar_f_tbody-frame vy (m/s)yes
raccoon/odometry/wzscalar_f_tbody-frame wz (rad/s)yes
raccoon/feature/bemf_enabledscalar_i32_tBEMF enable state (0 or 1)yes
raccoon/system/shutdown_statusscalar_i32_tshutdown bitmask (bits: servo, motor, watchdog-source)yes
raccoon/errorsstring_tSTM32 UART error messagesno
raccoon/cpu/temp/valuescalar_f_tPi CPU temperature (°C)no

“Retained” channels use publishRetained(). A new subscriber that calls subscribeWithRetain immediately receives the last published value without waiting for the next SPI cycle. This is important for motor position and battery voltage, where a late subscriber must not block waiting for the next update.

IMU accuracy channels (gyro/accuracy, accel/accuracy, mag/accuracy, imu/quaternion_accuracy) are published only when the accuracy value changes, not on every loop.

Step 4: raccoon-transport

The Transport carries every value from the bridge to its subscribers (raccoon-lib, BotUI, diagnostic tools). Each channel is one ring file /dev/shm/raccoon_ring_<channel> with 64 slots of 2 KiB. The publisher copies the encoded message into the next slot and wakes parked subscribers with a futex. A subscriber that is too slow loses old frames and never blocks the publisher. You can list the files with ls /dev/shm/raccoon_ring_*.

sequenceDiagram
    participant PUB as DataPublisher (bridge)
    participant RING as raccoon_ring file\n(/dev/shm/raccoon_ring_*)
    participant SUB as raccoon-lib spin thread

    PUB->>RING: rrb_writer_publish(encoded_msg, len)\nslot = producer_seq % 64\nSeqLock write, bump producer_seq\nFUTEX_WAKE if a subscriber is parked

    SUB->>RING: rrb_reader_recv_wait_many()\nfutex_waitv on the wake word of all channels
    RING-->>SUB: frame available\ncopy payload out, verify the SeqLock\ndecode + dispatch callback

Retained channels: publishRetained() marks a channel so that a new subscriber starts at the latest frame in the ring and gets the last value immediately. Battery voltage, motor position, servo state, odometry, heading and BEMF are retained for this reason.

Producer restart: when stm32-data-reader restarts it re-initialises the existing ring files in place, and subscribers resync. Never wipe /dev/shm/raccoon_ring_* while components run.

Namespace: stm32_data_reader.service sets PrivateTmp=false so the ring files are visible to every process, see Robot Services and systemd .

Reliable command delivery uses the same ring: the publisher re-sends until the subscriber acknowledges on the internal __raccoon/ack channel. Nothing goes over the network; the old lcm-loopback-multicast.service no longer exists.

Step 5: raccoon-lib HAL

TransportReader is a singleton that subscribes to the sensor channels and keeps mutex-protected caches of the latest value. The callbacks run on SharedTransport’s one spin thread, which parks in futex_waitv and wakes on every publish. Each HAL class (Motor, Analog, Digital, IMU) calls the appropriate TransportReader::read*() method, which takes the lock and returns the cached value. Sensor reads in user code are non-blocking and always return the most recently observed value.

Command Path (Python → STM32)

Python: motor.set_speed(50)
      │
      ▼
raccoon-lib::hal::motor::Motor::setSpeed()
      │  (maps percent to signed duty, applies inversion)
      ▼
TransportWriter::setMotor(port, duty)
      │  (publishes scalar_i32_t to raccoon/motor/N/power_cmd)
      ▼
raccoon_ring SHM (/dev/shm)
      │
      ▼
stm32-data-reader: CommandSubscriber::onMotorPowerCommand()
      │  (maps percent → duty: duty = percent × 4)
      ▼
DeviceController::setMotorPwm()
      │
      ▼
SpiReal::setMotorPwm()
      │  (calls C API: set_motor_pwm(port, duty))
      ▼
C SPI layer: updates rxBuffer in Pi's memory
      │
      ▼
Next spi_update() call: Pi sends updated rxBuffer to STM32
      │  (full-duplex SPI transaction)
      ▼
STM32: rxBuffer updated by DMA
      │  (SPI completion callback fires)
      ▼
HAL_SPI_TxRxCpltCallback validates version
      │
      ▼
Next BEMF cycle (≤ 1250 µs): update_motor() reads motorControlMode
      │  and motorTarget, calls motor_setDutycycle()
      ▼
TIM1/TIM8 compare register updated
      │
      ▼
Motor PWM changes

The following sequence diagram shows the timing of a motor power command in detail, including where the main loop interleaves with the command subscriber:

sequenceDiagram
    participant PY as Python (raccoon-lib)
    participant RING as raccoon_ring SHM
    participant CS as CommandSubscriber
    participant DC as DeviceController
    participant SPI as SpiReal / C SPI layer
    participant STM as STM32F427

    PY->>RING: publish raccoon/motor/0/power_cmd\n(scalar_i32_t, value=50)
    Note over RING: slot written, wake_seq bumped

    Note over CS: main loop: processMessages() → spinOnce(1ms)
    RING-->>CS: frame delivered, callback fires
    CS->>CS: isTimestampNewer()? yes
    CS->>CS: duty = 50 × 4 = 200
    CS->>DC: setMotorPwm(0, 200)
    DC->>DC: hasSameCommand()? no
    DC->>SPI: setMotorState(0, {Pwm, 200})
    SPI->>SPI: set_motor_pwm(0, 200)\nupdates RxBuffer.motorTarget[0]=200\nRxBuffer.motorControlMode |= MOT_MODE_PWM

    Note over STM: ~5 ms: next spi_update()
    SPI->>STM: ioctl(SPI_IOC_MESSAGE)\nsends RxBuffer with new target
    STM-->>SPI: TxBuffer (updated sensor data)
    Note over STM: HAL_SPI_TxRxCpltCallback\nvalidates TRANSFER_VERSION 21
    Note over STM: next BEMF cycle (≤1250 µs)\nupdate_motor() → motor_setDutycycle(200)\nTIM1/TIM8 CCR updated

transport command channels

ChannelMessage typeReliable?Purpose
raccoon/motor/N/power_cmdscalar_i32_tnoopen-loop PWM (−100 to +100%)
raccoon/motor/N/velocity_cmdscalar_i32_tnoMAV velocity setpoint (BEMF ticks/s)
raccoon/motor/N/position_cmdvector3f_tyesMTP: x=speed_limit, y=goal_position
raccoon/motor/N/position_reset_cmdscalar_i32_tyesreset motor N position on STM32
raccoon/motor/N/stop_cmdscalar_i32_tyesstop motor N (passive brake)
raccoon/motor/N/mode_cmdscalar_i32_tyesset raw motor control mode
raccoon/motor/N/pid_cmdvector3f_tyesoverride PID: x=kp, y=ki, z=kd
raccoon/chassis/velocity_cmdvector3f_tnochassis velocity: x=vx, y=vy, z=wz
raccoon/servo/N/mode_cmdscalar_i8_tyesservo mode (separate from state channel)
raccoon/servo/N/position_cmdscalar_f_tyesservo target angle (degrees)
raccoon/servo/N/smooth_cmdvector3f_tyessmooth servo: x=angle, y=speed, z=easing
raccoon/servo/N/velocity_cmdscalar_f_tyesbus servo: continuous rotation in deg/s (signed)
raccoon/odometry/reset_cmdscalar_i32_tyesreset odometry
raccoon/kinematics/config_cmd(kinematics type)yessend KinematicsConfig to STM32
raccoon/system/heartbeat_cmdscalar_i32_tnoMotorWatchdog heartbeat
raccoon/system/shutdown_cmdscalar_i32_tyessystem shutdown command
raccoon/cmd/feature/bemf_enabledscalar_i32_tyesenable/disable BEMF at runtime

Servo mode vs. servo mode command: The servo mode channel is split into two: raccoon/servo/N/mode (state, reader publishes) and raccoon/servo/N/mode_cmd (command, raccoon-lib publishes). This prevents the reader from subscribing to its own publishes, which previously caused a ~5 ms internal latency floor.

Bus Servo Channels

A servo port can drive a Feetech STS serial bus servo (e.g. STS3215) instead of a PWM servo. Which one it is depends on the port’s servo mode, which is set on raccoon/servo/N/mode_cmd:

ValueMode
0Fully disabled (PWM off, 6 V supply off if all servos are)
1Disabled (PWM off for this servo)
2PWM servo enabled
3Bus servo

Setting mode 3 hands the pin from the PWM timer to the bus and leaves the servo relaxed (torque off). The STM32 then searches for one servo on the port, whatever its ID is, and reports it on the health channel. A servo that stops answering is searched for again.

The same command channels as for PWM servos drive a bus servo, with these meanings:

ChannelOn a bus servo
position_cmdGo to the angle (0–360°) at the servo’s maximum speed, then hold
smooth_cmdGo to x=angle at y=speed (deg/s). The servo runs the ramp itself; z=0 uses its default acceleration, any other easing uses a ramp over about the first third of the move
velocity_cmdTurn continuously at the given signed speed
mode_cmd = 3 while already in bus modeRelax the servo (torque off)

The servo has 4096 steps per revolution. feedback reports the position in degrees, unwrapped by the STM32, so it keeps counting past 360° and goes negative when the servo is turned back. done refers to the latest command: it is 0 until the move has finished, then 1 (target reached) or 2 (the servo stopped before the target). The error bits of health are 0x01 voltage, 0x02 sensor, 0x04 overheat, 0x08 current, 0x10 angle and 0x20 overload.

velocity_cmd is the only command that fails on a PWM port: the reader logs an error and ignores it.

Reliable Commands

Commands that must not be lost (position targets, PID configuration, servo mode changes, shutdown, position resets) are subscribed with reliableOpts (SubscribeOptions{.reliable = true}). Reliable delivery is a transport-level option implemented in raccoon::Transport; there is no separate ReliablePublisher class. The reliable path adds a sequence number envelope and retransmits until an ACK is received.

Continuous commands (motor power percentage, motor velocity) are sent without reliable delivery to avoid queuing stale commands. If a power command is dropped, the next iteration of the user’s control loop sends a fresh one.

Timestamp-Based Deduplication

The CommandSubscriber tracks the timestamp of the most recently applied command per channel. If a message arrives with a timestamp older than the last applied message (possible if messages arrive out of order, for example re-sends of a reliable command), it is silently dropped. This is the isTimestampNewer() check.

Timing Budget

StageTypical latency
Sensor measurement to txBuffer0–5 ms (BEMF-dominated)
SPI transfer< 1 ms
stm32-data-reader main loop< 5 ms (mainLoopDelay = 5 ms)
raccoon_ring SHM delivery< 0.1 ms
TransportReader cache update< 0.1 ms
Python read call< 0.1 ms (non-blocking)
Total sensor→Python< 12 ms

Command latency follows the same path in reverse; the dominant factor is the BEMF cycle (up to 1250 µs per motor in round-robin, up to one full 4-motor rotation = 5 ms in worst case) from when the STM32 receives the new command to when the motor output actually changes.

  • Firmware Runtime and Scheduling — ISR context, ADC interrupt rates, and super-loop scheduling that produce the data entering the pipeline
  • Pi Bridge Internals — deep dive into Application lifecycle, SpiReal vs SpiMock, DataPublisher gate logic, CommandSubscriber, MotorWatchdog, LcmBroker, and raccoon_ring internals
  • SPI Communication Protocol — the wire format and all transport channel names
  • Motor Control — what happens inside each BEMF cycle
  • IMU Stack — how txBuffer.imu fields are produced by the MPL fusion pipeline
  • Robot Services And systemd — how stm32_data_reader.service fits into the full service topology