Repository navigation
mavcan: send queued frames without waiting for the receive timeout - #99
Merged
Merged
Conversation
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).
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.
The MAVCAN IO process drains the transmit queue and then blocks in
recv_match(..., timeout=0.005). A frame handed tosend_frame()justafter 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:
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 becausereconnect()replacesconn.select()there onlyaccepts sockets, so a serial handle and the queue's pipe cannot be
waited on together.
tx_wait_fd()returnsNonein that case, andalso 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 a200 Hz
esc.RawCommandstream 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):
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.Queuehandoff to the IO process measures 0.30 msmedian (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_FRAMEdispatch fromGCS::update_receiveat300 Hz.