Expand description
MSP (MultiWii Serial Protocol) v1 + v2 codec.
The agent’s MAVLink router already exposes a raw MSP byte pipe on
/run/ados/msp.sock (it forwards bytes to and from a Betaflight / iNav /
KISS flight controller’s serial link), but nothing in the tree could build
or parse an MSP frame. This module is that codec: the wire framing for both
MSP versions plus the one command a companion needs to fly a rate-control FC,
MSP_SET_RAW_RC (200), with the stick scaling the GCS already standardized.
Two frame formats, both little-endian:
- MSPv1
$M<:'$' 'M' <dir> <size:u8> <cmd:u8> <payload...> <crc:u8>wherecrcis the XOR ofsize,cmd, and every payload byte.sizeis the payload length, so a v1 command id and a single payload byte cannot exceed 255.<dir>is<for a request to the FC,>for a reply,!for an error reply. - MSPv2
$X<:'$' 'X' <dir> <flag:u8> <cmd:u16 LE> <size:u16 LE> <payload...> <crc:u8>wherecrcis CRC8/DVB-S2 (poly0xD5) accumulated overflag, the two command bytes, the two size bytes, and every payload byte. v2 lifts the 8-bit command/size ceiling (needed for the full command space and larger settings payloads).
The codec is transport-agnostic: it produces and consumes byte buffers, and the plugin host / MSP client move those over the socket. It never opens the socket or the FC itself.
Structs§
- MspFrame
- A decoded MSP frame: its command id, direction/marker byte, and payload.
Enums§
- MspDecode
Error - Why a byte buffer could not be decoded as a single complete MSP frame.
Constants§
- DIR_
ERROR - The v1 marker for an error reply from the FC (
!). - DIR_
FROM_ FC - The v1 marker for a normal reply from the FC (
>). - DIR_
TO_ FC - The v1 direction/marker byte for a request to the FC (
<). - MSP_
SET_ RAW_ RC MSP_SET_RAW_RC— write raw RC channel values (PWM microseconds) to the FC. Payload is N little-endianu16channels. This is the command a companion uses to fly an FC in a rate mode (ACRO), the FC treating the values as if they came from a receiver.
Functions§
- bipolar_
to_ pwm - Map a bipolar stick axis (roll / pitch / yaw),
-1.0..=1.0, to a PWM value1000..=2000with centre1500. Out-of-range inputs clamp. This matches the GCSbipolarToPwmmapping so a companion and the GCS scale sticks identically. - crc8_
dvb_ s2 - Accumulate one byte into a CRC8/DVB-S2 running value (polynomial
0xD5). Seed with0and fold every byte of the v2 header+payload. - crc8_
dvb_ s2_ buf - CRC8/DVB-S2 over a slice, seeded at 0.
- decode_
frame - Decode exactly one MSP frame from the START of
buf(v1 or v2, auto-detected from the$M/$Xpreamble). Returns the frame and the number of bytes it consumed, so a caller streaming from the socket can advance past it. Verifies the CRC and fails closed on a mismatch — a corrupt FC reply must never be handed on as if it were valid (a status surface reports verified data). - encode_
v1 - Encode an MSPv1 request
$M<.cmdandpayload.len()must each fit in a byte; a command or payload that does not is a v2-only frame, so this returnsNonerather than truncating (a silent truncation would corrupt the FC command — fail loudly instead). - encode_
v2 - Encode an MSPv2 request
$X<withflag = 0(the common case). - encode_
v2_ flagged - Encode an MSPv2 request
$X<with an explicit flag byte. - set_
raw_ rc - Encode an
MSP_SET_RAW_RCcommand carryingchannelsPWM microsecond values as little-endianu16s.v2selects the framing (v2 is the safe default for a modern FC; v1 is offered for a legacy target). A v1 encode of more than 127 channels overflows the byte size field and returnsNone. - throttle_
to_ pwm - Map a throttle input,
0.0..=1.0, to a PWM value1000..=2000with idle at1000(NOT centre — a throttle at rest is idle, not mid-stick). Matches the GCSthrottleToPwmmapping.