pi-protocol 2.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,170 @@
1
+ Metadata-Version: 2.4
2
+ Name: pi-protocol
3
+ Version: 2.0.0
4
+ Summary: Python client for pi-protocol, a minimalist framed message protocol for talking to MCUs over serial
5
+ Author: Till Blaha
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/tudelft/pi-protocol
8
+ Project-URL: Issues, https://github.com/tudelft/pi-protocol/issues
9
+ Keywords: serial,protocol,embedded,mcu,betaflight,uart
10
+ Classifier: Development Status :: 4 - Beta
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: Operating System :: OS Independent
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Topic :: Communications
15
+ Classifier: Topic :: System :: Hardware :: Hardware Drivers
16
+ Requires-Python: >=3.10
17
+ Description-Content-Type: text/markdown
18
+ License-File: LICENSE
19
+ Requires-Dist: pyserial>=3.5
20
+ Dynamic: license-file
21
+
22
+ # pi-protocol
23
+
24
+ Minimalist message protocol in C to exchange messages between MCU's. Includes a
25
+ python generation utility for the header and source files.
26
+
27
+ ## Description
28
+
29
+ ### The protocol
30
+
31
+ A frame consists of:
32
+ ```
33
+ | STX | MSG_ID | PAYLOAD_BYTE_1 | PAYLOAD_BYTE_2 | ... | PAYLOAD_BYTE_N | CHECKSUM |
34
+ ```
35
+
36
+ `STX` bytes in `MSG_ID` and `PAYLOAD` are escaped by the sequence `ESC STX_ESC` and `ESC` bytes are escaped with `ESC ESC_ESC`.
37
+ This is simple and very robust to parse, but causes worst case overhead of `100%`, if every payload byte is either `STX` or `ESC`.
38
+
39
+ ### Working principle of the generation of the protocol
40
+
41
+ - 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`.
42
+ - 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.
43
+ - 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).
44
+ - 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.
45
+
46
+
47
+ ## Generation and Usage
48
+
49
+ ### Pre-requesites
50
+
51
+ [![Python 3.10](https://img.shields.io/badge/Python-3.10-3776AB?logo=python)](https://www.python.org/downloads/release/python-3100/)
52
+
53
+ Tested on Ubuntu 22.04. Install `make`, `python3`, `pip` and then the required python modules for generation
54
+ ```bash
55
+ sudo apt install make python3 python3-pip
56
+ pip install -r python/requirements.txt # only needed to run the generator
57
+ ```
58
+
59
+ ### Generation of C files
60
+
61
+ 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:
62
+
63
+ ```bash
64
+ make clean && make CONFIG=config.yaml
65
+ ```
66
+
67
+ ### Usage in your project
68
+
69
+ 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.
70
+
71
+ ### Python client
72
+
73
+ 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).
74
+
75
+ ```python
76
+ from pi_protocol import PiSerial
77
+ from pi_protocol.messages import Imu, OffboardPose
78
+
79
+ with PiSerial("/dev/ttyUSB0", 921600) as pi:
80
+ pi.send(Imu(time_us=1, roll=0.1)) # several messages: pi.send(a, b, c) -> one write()
81
+
82
+ for msg in pi.recv(timeout=0.01): # blocks until >= 1 message or timeout
83
+ if type(msg) is OffboardPose:
84
+ print(msg.x, msg.y, msg.z)
85
+
86
+ pi.poll() # non-blocking variant of recv()
87
+ pose = pi.latest.get(OffboardPose) # most recent one, like piMsgOffboardPoseRx
88
+ pi.on(OffboardPose, callback) # or get called from inside recv()/poll()
89
+ ```
90
+
91
+ For other transports (UDP, sockets, log files), use `encode(msg) -> bytes` and `PiParser().feed(data) -> [msgs]` directly.
92
+
93
+ The client is tuned for latency rather than elegance:
94
+ * each message is decoded as soon as its last byte arrives;
95
+ * the per-byte work is done by C-level `bytes.split`/`bytes.replace`, with a table-driven CRC;
96
+ * the raw file descriptor is used on POSIX;
97
+ * the USB-serial low-latency mode is enabled (FTDI: 16 ms -> 1 ms).
98
+
99
+ 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.
100
+
101
+ ### Tests
102
+
103
+ You need `gcc` installed, then do
104
+
105
+ ```bash
106
+ make clean && make CONFIG=config.yaml tester && ./tester
107
+ ```
108
+
109
+ Python client, including a cross-check against the C encoder and parser:
110
+
111
+ ```bash
112
+ make test-python
113
+ ```
114
+
115
+ ### Releasing to PyPI
116
+
117
+ Releases are published by `.github/workflows/publish.yml` using PyPI trusted publishing (no API token). To release:
118
+
119
+ 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.
120
+ 2. `git tag v2.0.1 && git push --tags` (the tag must equal `v<version>`, otherwise the workflow fails).
121
+
122
+ The workflow refuses to publish if the tag and the version differ, or if the major version differs from the protocol's.
123
+
124
+ 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.
125
+
126
+ Local build (sdist + wheel in `dist/`): `make dist PYTHON="uv run --group gen python"`.
127
+
128
+ ## History
129
+
130
+ * 2024-08-27 -- Version 2.0.0 -- Initial release
131
+
132
+
133
+ ## Authors
134
+
135
+ * Till Blaha ([@tblaha](https://github.com/tblaha), Delft University of Technology, t.m.blaha - @ - tudelft.nl)
136
+
137
+
138
+ ## Structure
139
+
140
+ ```shell
141
+ .
142
+ ├── msgs/ # Message definitions
143
+ ├── python/ # generation utility
144
+ ├── src/ # static source files (C and python protocol implementation)
145
+ ├── templates/ # source files to be generated
146
+ ├── tests/
147
+ ├── config.yaml # configurations of which messages should be generated
148
+ ├── LICENSE
149
+ ├── Makefile
150
+ ├── README.md
151
+ ```
152
+
153
+
154
+ ## License
155
+
156
+ [![License](https://img.shields.io/badge/License-LGPL--3.0--or--later-4398cc.svg?logo=spdx)](https://spdx.org/licenses/LGPL-3.0-or-later.html)
157
+ [![License](https://img.shields.io/badge/License-MIT-4398cc.svg?logo=spdx)](https://spdx.org/licenses/MIT.html)
158
+
159
+ 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.
160
+
161
+ Technische Universiteit Delft hereby disclaims all copyright interest in the
162
+ program “Indiflight Support” (one line description of the content or function)
163
+ written by the Author(s).
164
+
165
+ Henri Werij, Dean of the Faculty of Aerospace Engineering
166
+
167
+ Copyright © 2024, Till Blaha
168
+
169
+
170
+ ## Citation
@@ -0,0 +1,7 @@
1
+ pi_protocol/__init__.py,sha256=-sUNr8GTRZUyq9gvRAn8ZJEaWzAU0-lH1-jKi-Hvv1E,11841
2
+ pi_protocol/messages.py,sha256=sO1EwkhIk9Ao4LZ4oyNJMVyjVl0GZ3Ikaa19DuDUNmw,7757
3
+ pi_protocol-2.0.0.dist-info/licenses/LICENSE,sha256=ERcumdnXT7fJ5Gt5bCpXtIpzXP7NNSZQP5vPDJrVNJU,41597
4
+ pi_protocol-2.0.0.dist-info/METADATA,sha256=Pnei4b5lDFYHEalIJ0rGjyx3zjmGRcLG6eQdb0DY1Yc,7696
5
+ pi_protocol-2.0.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
6
+ pi_protocol-2.0.0.dist-info/top_level.txt,sha256=tV8Hb9m1P4QMqBjRlTSPpyli3WvJ0ILYWLJRPUQkBp4,12
7
+ pi_protocol-2.0.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+