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

battery - fix BATTERY_NOT_PRESENT detection, detection logic change #13599

Merged
merged 3 commits into from May 4, 2024

Conversation

ledvinap
Copy link
Contributor

@ledvinap ledvinap commented Apr 28, 2024

Detection logic is refactored:

  • voltageStablePrevFiltered is reset every time delta is exceeded
  • voltage within range is ANDed over 100ms periods (sampling at battery update interval)
  • voltage is stable if it was within range for 10 out of 11 periods

slowly changing voltage will update threshold, but voltage will be considered stable, 1 update/s (100mV/s) is tolerated

Detection logic is refactored - battery voltage delta is tested each
50ms, voltage is considered stable when difference of last 10 samples
is smaller than 100mV
This makes stable threshold more forgiving than betaflight#13350 (time for
comparison is 50 instead of 500ms).
Copy link

Do you want to test this code? You can flash it directly from Betaflight Configurator:

  • Simply put #13599 (this pull request number) in the Select commit field of the Configurator firmware flasher tab (you need to Enable expert mode, Show release candidates and Development).

WARNING: It may be unstable. Use only for testing!

- voltageStablePrevFiltered every time delta is exceeded
- voltage within range is ANDed over 100ms periods
- voltage is stable if it was within range for 10 out of 11 periods
  - slowly changing voltage will update threshold, but voltage will be
    considered stable
  - 1 update/s (100mV/s) is tolerated
@ledvinap
Copy link
Contributor Author

voltage

@ledvinap
Copy link
Contributor Author

battery - improve stable voltage detection

  • voltageStablePrevFiltered every time delta is exceeded
  • voltage within range is ANDed over 100ms periods
  • voltage is stable if it was within range for 10 out of 11 periods
    • slowly changing voltage will update threshold, but voltage will be
      considered stable
    • 1 update/s (100mV/s) is tolerated

@haslinghuis haslinghuis linked an issue Apr 29, 2024 that may be closed by this pull request
@ledvinap
Copy link
Contributor Author

Tested, works fine on H7NANO.

I've extended detection window to 1s - now filtered voltage is close to final value.
Default time constant is ~.5s (Its really bad idea to specify inverse of filter cutoff - time constant would make much more time)
It seems that resulting value is roughly within VOLTAGE_STABLE_MAX_DELTA of final value under these conditions (anyone wants to do the math?).

@nerdCopter nerdCopter requested a review from freasy April 29, 2024 15:17
Copy link
Member

@nerdCopter nerdCopter left a comment

Choose a reason for hiding this comment

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

  • untested, approving for workflow.
  • quick review of code, seems good, but i don't know maths.
  • request more than 2 independent tests.
  • will need backport to 4.5-maintenance .

Copy link

@pfeerick pfeerick left a comment

Choose a reason for hiding this comment

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

With my Mobula7 (HAMO/CRAZYBEEF4FR(STM32F411), ELRS via UART), this has mostly restored the -ON_USB 'no beep if USB only' behaviour. Now it only gives the several rapid startup beeps and usually just a couple of beeper beeps before it quietens for good unless the ON_USB setting is changed. No beeps if the beeper mode is active. Constant beeps if the flight battery is connected, with correct voltage being shown. So pretty much same as 4.4.x behaviour.

@ledvinap
Copy link
Contributor Author

@pfeerick it takes some time for the voltage filter to stabilize plus 1s detection window. Are the initial beeps a big problem?

@pfeerick
Copy link

pfeerick commented Apr 30, 2024

Not as far as I'm concerned... If I were being pedantic, sure, it should do just the initial fast startup beeps and zip it... but my preference is more that it doesn't beep incessantly all the frigging time if you have beeper active on middle position, and don't have the handset on. 🥳

if (abs(vm->voltageStablePrevFiltered - vm->displayFiltered) > VOLTAGE_STABLE_MAX_DELTA) {
// reset stable voltage reference
vm->voltageStablePrevFiltered = vm->displayFiltered;
vm->voltageStableBits &= ~BIT(0); // woltage threshold exceeded in this period
Copy link
Member

Choose a reason for hiding this comment

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

typo: woltage
i would rather define a enum, ~BIT(0) is not a 100% selfexplenatory term: IS_STABLE in this case.

typedef enum voltageStable_e {
  VOLTAGE_UNSTABLE = 0,
  VOLTAGE_IS_STABLE = ~VOLTAGE_UNSTABLE
} voltageStable_t;

or something like this. Or do we already have a global enum type for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is just clearing of lowest bit. IMO &= ~(bits) is quite idiomatic in C and used in lot of places of BF.
I'll update the comment

vm->voltageStableBits &= ~BIT(0); // woltage threshold exceeded in this period
}
if (cmp32(now, vm->voltageStableLastUpdate) >= VOLTAGE_STABLE_TICK_MS) {
vm->voltageStableBits = (vm->voltageStableBits << 1) | BIT(0); // start with 'stable' state
Copy link
Member

Choose a reason for hiding this comment

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

is | BIT(0) really necessary as << 1 shifts a 0 already?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It it bit zero (LSB), not bit with value of 0. New 1 bit is shifted in on each update and possibly cleared above.

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense!

@ledvinap ledvinap requested a review from ctzsnooze May 1, 2024 13:38
Copy link
Member

@ctzsnooze ctzsnooze left a comment

Choose a reason for hiding this comment

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

Thanks, @ledvinap
Tested, no change in beeper behaviour with USB-only or LiPo power, rise time or stability of battery detection using a wall supply or a LiPo seems fast and accurate.
Saves around 24 bytes of flash on F411.

@haslinghuis haslinghuis merged commit 5fd3852 into betaflight:master May 4, 2024
23 checks passed
@nerdCopter
Copy link
Member

backport to 4.5-maintenance?

haslinghuis pushed a commit to haslinghuis/betaflight that referenced this pull request May 4, 2024
…etaflight#13599)

* battery - fix BATTERY_NOT_PRESENT detection, detection logic change

Detection logic is refactored - battery voltage delta is tested each
50ms, voltage is considered stable when difference of last 10 samples
is smaller than 100mV
This makes stable threshold more forgiving than betaflight#13350 (time for
comparison is 50 instead of 500ms).

* battery - improve stable voltage detection

- voltageStablePrevFiltered every time delta is exceeded
- voltage within range is ANDed over 100ms periods
- voltage is stable if it was within range for 10 out of 11 periods
  - slowly changing voltage will update threshold, but voltage will be
    considered stable
  - 1 update/s (100mV/s) is tolerated

* battery - fuix typos, improve comments
blckmn pushed a commit that referenced this pull request May 10, 2024
…change (#13623)

battery - fix BATTERY_NOT_PRESENT detection, detection logic change (#13599)

* battery - fix BATTERY_NOT_PRESENT detection, detection logic change

Detection logic is refactored - battery voltage delta is tested each
50ms, voltage is considered stable when difference of last 10 samples
is smaller than 100mV
This makes stable threshold more forgiving than #13350 (time for
comparison is 50 instead of 500ms).

* battery - improve stable voltage detection

- voltageStablePrevFiltered every time delta is exceeded
- voltage within range is ANDed over 100ms periods
- voltage is stable if it was within range for 10 out of 11 periods
  - slowly changing voltage will update threshold, but voltage will be
    considered stable
  - 1 update/s (100mV/s) is tolerated

* battery - fuix typos, improve comments

Co-authored-by: Petr Ledvina <ledvinap@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

beeper always on when connected to Configurator
6 participants