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

Voltage detection method #13350

Merged
merged 1 commit into from Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 19 additions & 13 deletions src/main/sensors/battery.c
Expand Up @@ -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
Expand Down Expand Up @@ -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()) {
Copy link
Contributor

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.

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)
Expand All @@ -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

Expand All @@ -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;
Expand All @@ -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()) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
Expand Down
2 changes: 2 additions & 0 deletions src/main/sensors/battery.h
Expand Up @@ -97,6 +97,8 @@ void batteryInit(void);
void batteryUpdateVoltage(timeUs_t currentTimeUs);
void batteryUpdatePresence(void);

bool isVoltageFromBattery(void);

batteryState_e getBatteryState(void);
batteryState_e getVoltageState(void);
batteryState_e getConsumptionState(void);
Expand Down
3 changes: 3 additions & 0 deletions src/main/sensors/voltage.c
Expand Up @@ -89,7 +89,10 @@ const uint8_t supportedVoltageMeterCount = ARRAYLEN(voltageMeterIds);
void voltageMeterReset(voltageMeter_t *meter)
{
meter->displayFiltered = 0;
meter->prevDisplayFiltered = 0;
meter->prevDisplayFilteredTime = 0;
meter->unfiltered = 0;
meter->isVoltageStable = false;
#if defined(USE_BATTERY_VOLTAGE_SAG_COMPENSATION)
meter->sagFiltered = 0;
#endif
Expand Down
5 changes: 5 additions & 0 deletions src/main/sensors/voltage.h
Expand Up @@ -25,6 +25,8 @@
#define SLOW_VOLTAGE_TASK_FREQ_HZ 50
#define FAST_VOLTAGE_TASK_FREQ_HZ 200

#define PREV_DISPLAY_FILTERED_TIME_DIFF 500 // ms

//
// meters
//
Expand All @@ -42,6 +44,9 @@ extern const char * const voltageMeterSourceNames[VOLTAGE_METER_COUNT];

typedef struct voltageMeter_s {
uint16_t displayFiltered; // voltage in 0.01V steps
uint16_t prevDisplayFiltered; // voltage in 0.01V steps
timeMs_t prevDisplayFilteredTime;
bool isVoltageStable;
uint16_t unfiltered; // voltage in 0.01V steps
#if defined(USE_BATTERY_VOLTAGE_SAG_COMPENSATION)
uint16_t sagFiltered; // voltage in 0.01V steps
Expand Down