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
Conversation
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).
Do you want to test this code? You can flash it directly from Betaflight Configurator:
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
battery - improve stable voltage detection
|
Tested, works fine on H7NANO. I've extended detection window to 1s - now filtered voltage is close to final value. |
There was a problem hiding this 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 .
There was a problem hiding this 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.
@pfeerick it takes some time for the voltage filter to stabilize plus 1s detection window. Are the initial beeps a big problem? |
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. 🥳 |
src/main/sensors/voltage.c
Outdated
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense!
There was a problem hiding this 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.
backport to 4.5-maintenance? |
…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
…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>
Detection logic is refactored:
slowly changing voltage will update threshold, but voltage will be considered stable, 1 update/s (100mV/s) is tolerated