Repository navigation
Add BusLoadListener to measure the bus load - #2180
Open
physicalfleet-dev-team wants to merge 1 commit into
Open
physicalfleet-dev-team wants to merge 1 commit into
physicalfleet-dev-team wants to merge 1 commit 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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! 😊