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
Voltage detection method #13350
Merged
Merged
Voltage detection method #13350
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,7 +67,7 @@ | |
* | ||
*/ | ||
|
||
#define VBAT_STABLE_MAX_DELTA 20 | ||
#define VBAT_STABLE_MAX_DELTA 10 // 100mV | ||
#define LVC_AFFECT_TIME 10000000 //10 secs for the LVC to slowly kick in | ||
|
||
// Battery monitoring stuff | ||
|
@@ -159,8 +159,21 @@ void batteryUpdateVoltage(timeUs_t currentTimeUs) | |
break; | ||
} | ||
|
||
if (!voltageMeter.isVoltageStable && cmp32(millis(), voltageMeter.prevDisplayFilteredTime) >= PREV_DISPLAY_FILTERED_TIME_DIFF) { | ||
if ((voltageState == BATTERY_NOT_PRESENT || voltageState == BATTERY_INIT) && isVoltageFromBattery()) { | ||
voltageMeter.isVoltageStable = abs(voltageMeter.prevDisplayFiltered - voltageMeter.displayFiltered) <= VBAT_STABLE_MAX_DELTA; | ||
} | ||
|
||
voltageMeter.prevDisplayFiltered = voltageMeter.displayFiltered; | ||
voltageMeter.prevDisplayFilteredTime = millis(); | ||
} | ||
|
||
DEBUG_SET(DEBUG_BATTERY, 0, voltageMeter.unfiltered); | ||
DEBUG_SET(DEBUG_BATTERY, 1, voltageMeter.displayFiltered); | ||
DEBUG_SET(DEBUG_BATTERY, 4, voltageMeter.isVoltageStable ? 1 : 0); | ||
DEBUG_SET(DEBUG_BATTERY, 5, isVoltageFromBattery() ? 1 : 0); | ||
DEBUG_SET(DEBUG_BATTERY, 6, voltageMeter.prevDisplayFiltered); | ||
DEBUG_SET(DEBUG_BATTERY, 7, voltageState); | ||
} | ||
|
||
static void updateBatteryBeeperAlert(void) | ||
|
@@ -181,14 +194,7 @@ static void updateBatteryBeeperAlert(void) | |
} | ||
} | ||
|
||
//TODO: make all of these independent of voltage filtering for display | ||
|
||
static bool isVoltageStable(void) | ||
{ | ||
return abs(voltageMeter.displayFiltered - voltageMeter.unfiltered) <= VBAT_STABLE_MAX_DELTA; | ||
} | ||
|
||
static bool isVoltageFromBat(void) | ||
bool isVoltageFromBattery(void) | ||
{ | ||
// We want to disable battery getting detected around USB voltage or 0V | ||
|
||
|
@@ -199,7 +205,7 @@ static bool isVoltageFromBat(void) | |
|
||
void batteryUpdatePresence(void) | ||
{ | ||
if ((voltageState == BATTERY_NOT_PRESENT || voltageState == BATTERY_INIT) && isVoltageFromBat() && isVoltageStable()) { | ||
if ((voltageState == BATTERY_NOT_PRESENT || voltageState == BATTERY_INIT) && isVoltageFromBattery() && voltageMeter.isVoltageStable) { | ||
// Battery has just been connected - calculate cells, warning voltages and reset state | ||
|
||
consumptionState = voltageState = BATTERY_OK; | ||
|
@@ -226,11 +232,11 @@ void batteryUpdatePresence(void) | |
batteryCriticalHysteresisVoltage = (batteryCriticalVoltage > batteryConfig()->vbathysteresis) ? batteryCriticalVoltage - batteryConfig()->vbathysteresis : 0; | ||
lowVoltageCutoff.percentage = 100; | ||
lowVoltageCutoff.startTime = 0; | ||
} else if (voltageState != BATTERY_NOT_PRESENT && isVoltageStable() && !isVoltageFromBat()) { | ||
} else if (voltageState != BATTERY_NOT_PRESENT && voltageMeter.isVoltageStable && !isVoltageFromBattery()) { | ||
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. This will never never be true: voltageMeter.isVoltageStable && !isVoltageFromBattery() |
||
/* battery has been disconnected - can take a while for filter cap to disharge so we use a threshold of batteryConfig()->vbatnotpresentcellvoltage */ | ||
|
||
consumptionState = voltageState = BATTERY_NOT_PRESENT; | ||
|
||
|
||
voltageMeter.isVoltageStable = false; | ||
batteryCellCount = 0; | ||
batteryWarningVoltage = 0; | ||
batteryCriticalVoltage = 0; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Due to
isVoltageFromBattery()
,isVoltageStable
is never set unless battery is connected.