Skip to content

mavcan: send queued frames without waiting for the receive timeout - #99

Merged
tridge merged 1 commit into
dronecan:masterfrom
tridge:pr-mavcan-tx-latency
Jul 31, 2026
Merged

tridge merged 1 commit into
dronecan:masterfrom
tridge:pr-mavcan-tx-latency

Conversation

@tridge

@tridge tridge commented Jul 31, 2026

Copy link
Copy Markdown
Member

The MAVCAN IO process drains the transmit queue and then blocks in
recv_match(..., timeout=0.005). A frame handed to send_frame() just
after that drain sits in the queue until the receive times out, or until
an incoming packet happens to wake the loop. Every outgoing frame picks
up latency that has nothing to do with the link.

This waits on the link and the transmit queue together with select(),
so a queued frame wakes the loop immediately.

Details:

  • Anything already parsed is taken before selecting. The link fd is not
    readable while a decoded message is sitting in the connection's own
    buffer, so selecting first would stall a ready message for the full
    timeout.
  • wait_set() is recomputed each pass because reconnect() replaces
    conn.
  • Windows keeps the previous blocking receive. select() there only
    accepts sockets, so a serial handle and the queue's pipe cannot be
    waited on together. tx_wait_fd() returns None in that case, and
    also if the queue does not expose a readable fd, so the fallback is
    taken rather than the change being conditional on the platform at the
    call site.

Measured on a CUAV-X25 forwarding to an AM32 ESC over mavcan:, with a
200 Hz esc.RawCommand stream out and ~300 frames/s of telemetry back.
The figure is the pure delay between the commanded throttle and the
ESC's own reported view of its input, extracted from a 0.5-20 Hz chirp
(it is flat in milliseconds across the whole sweep, and the fitted line
passes through the origin, so it is a transport delay rather than any
part of the ESC's response):

delay
before 10.9 ms
after 9.4 ms

The gain is smaller than the 5 ms timeout suggests because at ~300
frames/s inbound the blocking receive usually returns early anyway; this
removes the residual wait.

For reference on where the rest of that 9.4 ms lives: the
multiprocessing.Queue handoff to the IO process measures 0.30 ms
median (p90 0.87 ms) at 200 Hz, so it is not worth replacing. The
remainder is the MAVLink/USB link and the autopilot's inbound handling,
which schedules CAN_FRAME dispatch from GCS::update_receive at
300 Hz.

The IO process drained the transmit queue and then blocked in
recv_match() for up to 5ms. A frame handed to send_frame() just after
that drain sat in the queue until the receive timed out or a packet
happened to arrive, so every outgoing frame picked up milliseconds of
latency for no reason.

Wait on the link and the transmit queue together with select() instead.
Anything already parsed is taken first, since the link fd is not
readable while a decoded message is sitting in the connection's own
buffer. Windows keeps the old blocking receive: select() there only
accepts sockets, so the serial handle and the queue pipe cannot be
waited on together.

Measured on a CUAV-X25 forwarding to an AM32 ESC, driving 200Hz
RawCommand and receiving 300 frames/s of telemetry: the throttle
command reaches the ESC 1.5ms sooner (10.9ms -> 9.4ms end to end).
@tridge
tridge merged commit 622229f into dronecan:master Jul 31, 2026
7 checks passed
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.

1 participant