Skip to content

Commit

Permalink
Merge pull request #10607 from iNavFlight/mmosca-remove-vbat-delay
Browse files Browse the repository at this point in the history
Remove blocking delay from batteryUpdate initialization
  • Loading branch information
mmosca authored Jan 20, 2025
2 parents a687910 + 6e06742 commit 3f87202
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/main/sensors/battery.c
Original file line number Diff line number Diff line change
Expand Up @@ -351,16 +351,23 @@ static void checkBatteryCapacityState(void)

void batteryUpdate(timeUs_t timeDelta)
{
static timeUs_t batteryConnectedTime = 0;
/* battery has just been connected*/
if (batteryState == BATTERY_NOT_PRESENT && vbat > VBATT_PRESENT_THRESHOLD) {
if(batteryConnectedTime == 0) {
batteryConnectedTime = micros();
return;
}

/* Actual battery state is calculated below, this is really BATTERY_PRESENT */
batteryState = BATTERY_OK;
/* wait for VBatt to stabilise then we can calc number of cells
(using the filtered value takes a long time to ramp up)
We only do this on the ground so don't care if we do block, not
worse than original code anyway*/
delay(VBATT_STABLE_DELAY);
Blocking can cause issues with some ESCs */
if((micros() - batteryConnectedTime) < VBATT_STABLE_DELAY) {
return;
}

/* Actual battery state is calculated below, this is really BATTERY_PRESENT */
batteryState = BATTERY_OK;
updateBatteryVoltage(timeDelta, true);

int8_t detectedProfileIndex = -1;
Expand Down Expand Up @@ -396,6 +403,7 @@ void batteryUpdate(timeUs_t timeDelta)
/* battery has been disconnected - can take a while for filter cap to disharge so we use a threshold of VBATT_PRESENT_THRESHOLD */
if (batteryState != BATTERY_NOT_PRESENT && vbat <= VBATT_PRESENT_THRESHOLD) {
batteryState = BATTERY_NOT_PRESENT;
batteryConnectedTime = 0;
batteryCellCount = 0;
batteryWarningVoltage = 0;
batteryCriticalVoltage = 0;
Expand Down

0 comments on commit 3f87202

Please sign in to comment.