Skip to content

TX Airtime budget refills up much slower than configured due to fraction cut-off during calculation #3988

Backlog

Description

@h0lad

I think I found a huge issue during code review. Can someone please have a look and verify this?

Issue

Every time the budget is topped up, the fractional part of a millisecond is thrown away causing the TX airtime bucket to be refilled much slower as wanted.

What goes wrong

The top-up was computed and applied like this:

refill = (unsigned long)(elapsed * duty)     // the fraction is cut off due to the cast being used
if refill > 0:
  tx_budget_ms = tx_budget_ms + refill
  last_budget_update = now                        // the time behind the cut-off fraction is gone too

Converting to unsigned long drops everything after the decimal point and moving last_budget_update to now forgets the time that fraction stood for. The loss is at most 1 ms per top-up.

But a top-up happens as soon as elapsed x duty reaches 1 so the loss is a large share of each top-up.

Example

Airtime factor 0.5 (duty 2/3) - main loop every 1 ms:

after 1 ms: 1 x 0.667 = 0.67  -> 0, no top-up yet
after 2 ms: 2 x 0.667 = 1.33  -> 1 ms added, 0.33 ms lost

1 ms per 2 ms instead of 1.33 ms: 25% less.

Airtime factor 9 (duty 10%) - main loop every 7 ms:

after  7 ms:  7 x 0.1 = 0.7  -> 0, no top-up yet
after 14 ms: 14 x 0.1 = 1.4  -> 1 ms added, 0.4 ms lost

1 ms per 14 ms instead of 1.4 ms: 29% less.

Simulation

Airtime factor Duty Loop 1 ms Loop 7 ms Should be
0.5 67% 5000 ms (-25%) 5717 ms (-14%) 6667 ms
1 (default) 50% 5000 ms (exact) 4288 ms (-14%) 5000 ms
1.5 40% 3333 ms (-17%) 2859 ms (-29%) 4000 ms
2 33% 3333 ms (exact) 2858 ms (-14%) 3333 ms
3 25% 2500 ms (exact) 1430 ms (-43%) 2500 ms
9 10% 1000 ms (exact) 714 ms (-29%) 1000 ms

Suggested Fix

Keep the fraction and add it to the next top-up:

elapsed = now - last_budget_update
last_budget_update = now
refill = elapsed * duty + tx_budget_carry          // as a float
if the bucket would overflow:
  tx_budget_ms = max_budget, tx_budget_carry = 0
else:
  tx_budget_ms = tx_budget_ms + whole part of refill
  tx_budget_carry = fractional part of refill

Patch attached and in draft mode but not yet tested.

Activity

  1. changed the title [-]Airtime budget refills up much slower than configured as MC cuts off the fraction during calculation[/-] [+]TX Airtime budget refills up much slower than configured as MC cuts off the fraction during calculation[/+] on Nov 28, 2026
  2. changed the title [-]TX Airtime budget refills up much slower than configured as MC cuts off the fraction during calculation[/-] [+]TX Airtime budget refills up much slower than configured due to fraction cut-off during calculation[/+] on Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions