Skip to content

Add BusLoadListener to measure the bus load - #2180

Open
physicalfleet-dev-team wants to merge 1 commit into
hardbyte:mainfrom
physicalfleet-dev-team:bus-load-listener
Open

physicalfleet-dev-team wants to merge 1 commit into
hardbyte:mainfrom
physicalfleet-dev-team:bus-load-listener

Conversation

@physicalfleet-dev-team

Copy link
Copy Markdown

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 canbusload next 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

  • New can.BusLoadListener: plug it into a Notifier and call get_bus_load() to get the bus load in percent over a sliding window (1 s by default).
    • Frames are placed in time by 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 explicit timestamp.
    • Accepts bitrate / data_bitrate or a BitTiming / BitTimingFd as timing, the same names can.Bus uses.
    • Error frames are ignored. It's thread-safe, so the notifier thread and the reader can run at the same time.
  • New can.bus_load.frame_bit_length(msg) and can.bus_load.frame_duration(msg, bitrate, data_bitrate), which the listener uses:
    • standard and extended classical CAN frames, and remote frames (no data field)
    • CAN FD with CRC-17/CRC-21, the stuff count and fixed stuff bits, and data padded to the next valid FD length
    • with bitrate switching, ESI, DLC, the data and the CRC field are timed at the data bitrate and everything else at the nominal bitrate
    • worst-case bit stuffing by default (like canbusload), or bit_stuffing=False to leave dynamic stuff bits out
    • lengths include the 3-bit intermission
  • Docs: a BusLoadListener section in doc/notifier.rst with a usage example. News fragment: doc/changelog.d/1895.added.rst.

Usage:

import time
import can

with can.Bus(interface="socketcan", channel="can0") as bus:
    bus_load = can.BusLoadListener(bitrate=500_000, window=1.0)
    with can.Notifier(bus, [bus_load]):
        while True:
            time.sleep(1)
            print(f"bus load: {bus_load.get_bus_load():.1f} %")

Related Issues / Pull Requests

Type of Change

  • Bug fix
  • New feature
  • Documentation update
  • Refactoring
  • Other (please describe):

Checklist

  • I have followed the contribution guide.
  • I have added or updated tests as appropriate.
  • I have added or updated documentation as appropriate.
  • I have added a news fragment for towncrier.
  • All checks and tests pass (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 through Notifier with two virtual buses.
  • Full pytest: 805 passed, 117 skipped. The 5 failures are test_logger.py / test_scripts.py failing on the argparse --help assertion under Python 3.12, and they fail on main here too.
  • black --check ., ruff check can examples doc, pylint can/bus_load.py can/__init__.py (10.00/10), and mypy --python-version 3.11 . / 3.12 / 3.13 / 3.14, all clean.
  • sphinx -b html -Wan: no new warnings. The only warnings are the bcm.rst reference and the can.logger -h program output, and both appear on main too.

Open points you may have views on:

  • Naming: I went with BusLoadListener / get_bus_load() and a new can.bus_load module. Happy to rename, or to move the frame-length functions into can.util.
  • Stuffing is worst-case or off. Counting the exact stuff bits for each frame needs the CRC and a bit-by-bit model. That could come in a follow-up if you'd like it.
  • A natural next step would be showing the value in the can.viewer header. I left that out to keep this PR focused.

Thanks for maintaining python-can, it's a pleasure to build on! 😊

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add CAN bus utilization tracking Calculate Bus-Load

1 participant