Skip to content

About

minimalist message protocol to exchange messages between betaflight and RPi via UART

Resources

Stars

2 stars

Watchers

3 watching

Forks

Latest commit

 

History

62 Commits

Folders and files

Repository files navigation

pi-protocol

Minimalist message protocol in C to exchange messages between MCU's. Includes a python generation utility for the header and source files.

Description

The protocol

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.

Working principle of the generation of the protocol

  • The .yaml files 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, like ALL_CAPS_SNAKE_CASE.yaml.
  • A configuration .yaml file in the root of this repo defines which messages should be included and if they are to be received, sent or both (RX, TX or RXTX). Additionally, the global_mode option can be set to RX or TX instead of RXTX if it is known that all messages are RX or TX-only. In that case, the mode flags of each message have no effect.
  • Python generates c-headers form jinja2-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.h and stdbool.h. For testing and debugging output also stdio.h, of course.

Generation and Usage

Pre-requesites

Python 3.10

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 generator

Generation of C files

Edit 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.yaml

Usage in your project

Look 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.

Python client

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.

Tests

You need gcc installed, then do

make clean && make CONFIG=config.yaml tester && ./tester

Python client, including a cross-check against the C encoder and parser:

make test-python

Releasing to PyPI

Releases are published by .github/workflows/publish.yml using PyPI trusted publishing (no API token). To release:

  1. Bump version in pyproject.toml and commit. The major version follows the wire protocol (protocol_version in config.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.
  2. git tag v2.0.1 && git push --tags (the tag must equal v<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".

History

  • 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)

Authors

  • Till Blaha (@tblaha, Delft University of Technology, t.m.blaha - @ - tudelft.nl)

Structure

.
├── 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.md

License

License License

The 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

Citation

About

minimalist message protocol to exchange messages between betaflight and RPi via UART

Resources

Stars

2 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages