Minimalist message protocol in C to exchange messages between MCU's. Includes a python generation utility for the header and source files.
A frame consists of:
| STX | MSG_ID | PAYLOAD_BYTE_1 | PAYLOAD_BYTE_2 | ... | PAYLOAD_BYTE_N | CHECKSUM |
STX bytes in MSG_ID and PAYLOAD are escaped by the sequence ESC STX_ESC and ESC bytes are escaped with ESC ESC_ESC.
This is simple and very robust to parse, but causes worst case overhead of 100%, if every payload byte is either STX or ESC.
- The
.yamlfiles inside./msgs/define message fields and the filename defines the message name. The filename (and, as a result, the message name) should be formatted as all-caps-snake-case, likeALL_CAPS_SNAKE_CASE.yaml. - A configuration
.yamlfile in the root of this repo defines which messages should be included and if they are to be received, sent or both (RX,TXorRXTX). Additionally, theglobal_modeoption can be set toRXorTXinstead ofRXTXif it is known that all messages areRXorTX-only. In that case, the mode flags of each message have no effect. - Python generates
c-headers formjinja2-templates and define the message serialization ("packing") and parsing for receiving (packing for serial sending, parsing for serial reading). - The resulting headers depend only on some standard libraries:
string.h,stdint.handstdbool.h. For testing and debugging output alsostdio.h, of course.
Tested on Ubuntu 22.04. Install make, python3, pip and then the required python modules for generation
sudo apt install make python3 python3-pip
pip install -r python/requirements.txt # only needed to run the generatorEdit the yaml files to your liking, they should be self-explanatory. You can copy the config.yaml and give it a different name. Then generate the headers:
make clean && make CONFIG=config.yamlLook at tester.c. Especially note how the dummyWriter() is passed to piSerialWrite(). For TX, this is the function that needs to be modified/replaced to actually get the bytes on the transmission line, byte-for-byte.
The client is published on PyPI as pi-protocol (pip install pi-protocol). It ships the framing, parser and serial client plus the messages of the default config.yaml; the only runtime dependency is pyserial. For your own message set, run make CONFIG=your.yaml and install from the checkout (pip install .), or copy src/pi_protocol/ into your project. make writes the generated messages to src/pi_protocol/messages.py (one NamedTuple per message).
from pi_protocol import PiSerial
from pi_protocol.messages import Imu, OffboardPose
with PiSerial("/dev/ttyUSB0", 921600) as pi:
pi.send(Imu(time_us=1, roll=0.1)) # several messages: pi.send(a, b, c) -> one write()
for msg in pi.recv(timeout=0.01): # blocks until >= 1 message or timeout
if type(msg) is OffboardPose:
print(msg.x, msg.y, msg.z)
pi.poll() # non-blocking variant of recv()
pose = pi.latest.get(OffboardPose) # most recent one, like piMsgOffboardPoseRx
pi.on(OffboardPose, callback) # or get called from inside recv()/poll()For other transports (UDP, sockets, log files), use encode(msg) -> bytes and PiParser().feed(data) -> [msgs] directly.
The client is tuned for latency rather than elegance:
- each message is decoded as soon as its last byte arrives;
- the per-byte work is done by C-level
bytes.split/bytes.replace, with a table-driven CRC; - the raw file descriptor is used on POSIX;
- the USB-serial low-latency mode is enabled (FTDI: 16 ms -> 1 ms).
Encoding takes about 1 µs per message and decoding about 1–2 µs (CPython 3.12, x86). The wire time of a 30-byte frame at 921600 baud is about 330 µs.
You need gcc installed, then do
make clean && make CONFIG=config.yaml tester && ./testerPython client, including a cross-check against the C encoder and parser:
make test-pythonReleases are published by .github/workflows/publish.yml using PyPI trusted publishing (no API token). To release:
- Bump
versioninpyproject.tomland commit. The major version follows the wire protocol (protocol_versioninconfig.yaml), so the package 2.x talks to firmware built with protocol 2.x. Minor and patch belong to the package: new messages or API features bump minor, fixes bump patch. git tag v2.0.1 && git push --tags(the tag must equalv<version>, otherwise the workflow fails).
The workflow refuses to publish if the tag and the version differ, or if the major version differs from the protocol's.
One-time setup on PyPI (Account → Publishing → add a pending publisher): project pi-protocol, owner tudelft, repository pi-protocol, workflow publish.yml, environment pypi. Create the matching pypi environment in the GitHub repo settings.
Local build (sdist + wheel in dist/): make dist PYTHON="uv run --group gen python".
- 2024-08-27 -- Version 2.0.0 -- Initial release
- 2026-12-02 -- Version 3.0.0 -- HIL_IN/HIL_OUT extended with 8 rpms/setpoints and 8 servos (wire-incompatible with 2.x)
- Till Blaha (@tblaha, Delft University of Technology, t.m.blaha - @ - tudelft.nl)
.
├── msgs/ # Message definitions
├── python/ # generation utility
├── src/ # static source files (C and python protocol implementation)
├── templates/ # source files to be generated
├── tests/
├── config.yaml # configurations of which messages should be generated
├── LICENSE
├── Makefile
├── README.mdThe contents of this repository are licensed under a GNU Lesser General Public License v3.0, with the exception of the contents of src/. The contents of src/, as well as the generated output files are licensed under an MIT License. See the LICENSE file.
Technische Universiteit Delft hereby disclaims all copyright interest in the program “Indiflight Support” (one line description of the content or function) written by the Author(s).
Henri Werij, Dean of the Faculty of Aerospace Engineering
Copyright © 2024, Till Blaha