Repository navigation
Add BusLoadListener to measure the bus load - #2180
physicalfleet-dev-team wants to merge 2 commits into
Conversation
Add can.BusLoadListener, which estimates the bus load in percent over a sliding time window from the frames it receives, similar to canbusload from can-utils. Frames are placed in time by their timestamps, so the listener works with live buses and with log files. The frame lengths are also available as can.bus_load.frame_bit_length() and can.bus_load.frame_duration(). They cover standard and extended classical CAN frames, remote frames and CAN FD frames with and without bitrate switching, with worst-case or without bit stuffing.
|
Thanks for approving the workflow run! 🙂 The one red job, On my machine (macOS, CPython 3.12), the periodic tests in |
AI Review: Add BusLoadListener to measure the bus load (#2180)Checked against ISO 11898-1:2015. Field lengths (§10.4.2), the fixed stuff bits in the FD CRC field (§10.5), CRC-17/21 selection, FD length padding and the 13-bit trailer are correct. Findings
|
- Time the BRS bit and the CRC delimiter as one nominal and one data bit, since the bitrate switches at their sample points (ISO 11898-1:2015, 11.3.1.1). A 0-byte FBFF frame at 500 kbit/s / 2 Mbit/s without stuffing now takes 74.5 us instead of 76.0 us. - get_bus_load() raises ValueError for an explicit timestamp whose window starts before frames that were already discarded (earlier than the newest frame or a previous call) instead of returning a wrong value. - Call the stuff bit count an upper bound (Davis et al., 2007) in the docstrings and doc/notifier.rst. - Tests: drop the slow Notifier/virtual bus test and a redundant invalid bitrate case, merge test_listener_bus_load into test_listener_window and check the window start after the newest frame, check eviction through get_bus_load(), cover the default window end with out-of-order frames.
|
Thanks a lot for the careful review, @zariiii9003! Everything is addressed in caef548 :)
Verified locally on Python 3.12: |
Hi python-can team! 👋
We drive a lot of CAN servos and motor controllers from Python, and "how busy is this bus?" keeps coming up. Today that means running
canbusloadnext to the program, or writing the frame-length math again in each project. This PR adds it to python-can as a listener, as requested in #1895 and #972.Summary of Changes
can.BusLoadListener: plug it into aNotifierand callget_bus_load()to get the bus load in percent over a sliding window (1 s by default).msg.timestamp, so it also works when you feed it messages from a log file. By default the window ends at the newest frame's timestamp plus the time since that frame arrived, so the value falls to 0 when the bus goes quiet. You can also pass an explicittimestamp.bitrate/data_bitrateor aBitTiming/BitTimingFdastiming, the same namescan.Bususes.can.bus_load.frame_bit_length(msg)andcan.bus_load.frame_duration(msg, bitrate, data_bitrate), which the listener uses:canbusload), orbit_stuffing=Falseto leave dynamic stuff bits outBusLoadListenersection indoc/notifier.rstwith a usage example. News fragment:doc/changelog.d/1895.added.rst.Usage:
Related Issues / Pull Requests
Type of Change
Checklist
tox). I ran the individual tools rather than tox; see below.Additional Notes
How I tested it (Python 3.12, M1 Mac):
pytest test/test_bus_load.py: 41 new tests, all passing. The expected classical CAN lengths come from the worst-case formula in Davis et al. (2007), e.g. 135 and 160 bits for 8-byte standard and extended frames. The CAN FD values are worked out field by field from ISO 11898-1. The tests also cover FD timing with and without bitrate switching, the listener's window and eviction, out-of-order frames, the clock advancing while the bus is quiet,timing=, argument validation, and a run throughNotifierwith two virtual buses.pytest: 805 passed, 117 skipped. The 5 failures aretest_logger.py/test_scripts.pyfailing on the argparse--helpassertion under Python 3.12, and they fail onmainhere too.black --check .,ruff check can examples doc,pylint can/bus_load.py can/__init__.py(10.00/10), andmypy --python-version 3.11 ./3.12/3.13/3.14, all clean.sphinx -b html -Wan: no new warnings. The only warnings are thebcm.rstreference and thecan.logger -hprogram output, and both appear onmaintoo.Open points you may have views on:
BusLoadListener/get_bus_load()and a newcan.bus_loadmodule. Happy to rename, or to move the frame-length functions intocan.util.can.viewerheader. I left that out to keep this PR focused.Thanks for maintaining python-can, it's a pleasure to build on! 😊