Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AP_HAL_SITL: add packet loss and delay to each uart #25775

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

robertlong13
Copy link
Collaborator

@robertlong13 robertlong13 commented Dec 15, 2023

I need a way to simulate:

  1. Packet loss rate on a per-link basis
  2. Comm latency

(I am working on a redundant link manager plugin for Mission Planner that tries to intelligently select the "best" link to the aircraft.)

Before this, you could only simulate single-byte losses, and the setting applied to all serial ports. This switches to dropping full packets (I may extend this to partial packets at some point, to catch any issues caused by dropped data not aligning with packet boundaries, but I'm trying to figure out how to do that without introducing a million parameters), and it allows you to set the drop rate per serial port. In conjunction with a lua script, this could be used to cause burst losses for multiple seconds or could steadily increase the loss rate as you fly further from home.

I also added two queues to delay packets for a period of time both in and out. This is to simulate, for example, the ping time of a 4G link or satcom. This could even be used with a script that randomly sets this high and then low to simulate a TCP link that locks up for some period of time, then delivers all that data all at once when it comes back.

I have included a simple example script for an RF link, a cell link, and a hypothetical satellite device. Here's a screen of flying this aircraft in Mission Planner with all three links:
image

Here's a little animation of me testing that the latency works two-ways. Three instances of Mission Planner running on three different links. The left has no delay, and the two on the right have a delay of 500ms both ways. 500ms after I click the button on the right, the status message on the left updates (because the RX latency of the SITL link receiving the AUX function command), and then 500ms after that, the status message on the right updates (because the TX latency of that SITL link)

CommLatencySim

@robertlong13 robertlong13 marked this pull request as draft December 15, 2023 00:30
@robertlong13 robertlong13 changed the title AP_HAL_SITL: add packet loss and delay to all uart AP_HAL_SITL: add packet loss and delay to each uart Dec 15, 2023
@robertlong13 robertlong13 force-pushed the pr/uart_delays branch 2 times, most recently from 478ba77 to 21f0e16 Compare December 22, 2023 00:13
@robertlong13 robertlong13 force-pushed the pr/uart_delays branch 2 times, most recently from dc71606 to 1b031c4 Compare May 19, 2024 02:21
@robertlong13 robertlong13 marked this pull request as ready for review May 19, 2024 02:43
This would sometimes happen when initiating a connection when using
high simulated latency values when the queue would suddenly flush
to the buffer.
@IamPete1
Copy link
Member

I did a similar (ish) thing for the ELRS sim here: #26663. The key difference is that it does all the limiting in the "radio" sim rather in the UART driver.

if (_sitl && _sitl->uart_byte_loss_pct > 0) {
if (fabsf(rand_float()) < _sitl->uart_byte_loss_pct.get() * 0.01 * size) {
lost_byte = 1;
// simulate whole packet loss rate per uart driver
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be nice to break this out to a separate function

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.

4 participants