-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
#include <stdbool.h> | ||
#include <stdint.h> | ||
#include <string.h> | ||
#include <stdlib.h> | ||
|
||
#include "platform.h" | ||
|
||
|
@@ -89,10 +90,10 @@ const uint8_t supportedVoltageMeterCount = ARRAYLEN(voltageMeterIds); | |
void voltageMeterReset(voltageMeter_t *meter) | ||
{ | ||
meter->displayFiltered = 0; | ||
meter->prevDisplayFiltered = 0; | ||
meter->prevDisplayFilteredTime = 0; | ||
meter->voltageStablePrevFiltered = 0; | ||
meter->voltageStableLastUpdate = 0; | ||
meter->voltageStableBits = 0; | ||
meter->unfiltered = 0; | ||
meter->isVoltageStable = false; | ||
#if defined(USE_BATTERY_VOLTAGE_SAG_COMPENSATION) | ||
meter->sagFiltered = 0; | ||
#endif | ||
|
@@ -362,3 +363,27 @@ void voltageMeterRead(voltageMeterId_e id, voltageMeter_t *meter) | |
voltageMeterReset(meter); | ||
} | ||
} | ||
|
||
// update voltageStableBits | ||
// new bit is shifted in every VOLTAGE_STABLE_UPDATE_MS | ||
// bit 1 is shifted in when voltage did not change more than VOLTAGE_STABLE_MAX_DELTA | ||
// bit 0 is shifted in othervise | ||
void voltageStableUpdate(voltageMeter_t* vm) | ||
{ | ||
const uint32_t now = millis(); | ||
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 | ||
} | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. is There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense! |
||
vm->voltageStableLastUpdate = now; | ||
} | ||
} | ||
|
||
// voltage is stable when it was within VOLTAGE_STABLE_MAX_DELTA for 10 update periods | ||
bool voltageIsStable(voltageMeter_t* vm) | ||
{ | ||
return __builtin_popcount(vm->voltageStableBits & (BIT(VOLTAGE_STABLE_BITS_TOTAL + 1) - 1)) >= VOLTAGE_STABLE_BITS_THRESHOLD; | ||
} |
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.
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