diff --git a/src/main/fc/fc_msp.c b/src/main/fc/fc_msp.c index 133b55930..a5dfa5ff4 100644 --- a/src/main/fc/fc_msp.c +++ b/src/main/fc/fc_msp.c @@ -700,6 +700,7 @@ static bool mspFcProcessOutCommand(uint16_t cmdMSP, sbuf_t *dst, mspPostProcessF sbufWriteU16(dst, compassConfig()->mag_declination / 10); sbufWriteU16(dst, batteryConfig()->voltage.scale); + sbufWriteU16(dst, batteryConfig()->voltage.cellDetect); sbufWriteU16(dst, batteryConfig()->voltage.cellMin); sbufWriteU16(dst, batteryConfig()->voltage.cellMax); sbufWriteU16(dst, batteryConfig()->voltage.cellWarning); @@ -712,6 +713,7 @@ static bool mspFcProcessOutCommand(uint16_t cmdMSP, sbuf_t *dst, mspPostProcessF case MSP2_INAV_BATTERY_CONFIG: sbufWriteU16(dst, batteryConfig()->voltage.scale); + sbufWriteU16(dst, batteryConfig()->voltage.cellDetect); sbufWriteU16(dst, batteryConfig()->voltage.cellMin); sbufWriteU16(dst, batteryConfig()->voltage.cellMax); sbufWriteU16(dst, batteryConfig()->voltage.cellWarning); @@ -1613,7 +1615,7 @@ static mspResult_e mspFcProcessInCommand(uint16_t cmdMSP, sbuf_t *src) break; case MSP2_INAV_SET_MISC: - if (dataSize == 37) { + if (dataSize == 39) { rxConfigMutable()->midrc = constrain(sbufReadU16(src), MIDRC_MIN, MIDRC_MAX); motorConfigMutable()->minthrottle = constrain(sbufReadU16(src), PWM_RANGE_MIN, PWM_RANGE_MAX); @@ -1643,6 +1645,7 @@ static mspResult_e mspFcProcessInCommand(uint16_t cmdMSP, sbuf_t *src) #endif batteryConfigMutable()->voltage.scale = sbufReadU16(src); + batteryConfigMutable()->voltage.cellDetect = sbufReadU16(src); batteryConfigMutable()->voltage.cellMin = sbufReadU16(src); batteryConfigMutable()->voltage.cellMax = sbufReadU16(src); batteryConfigMutable()->voltage.cellWarning = sbufReadU16(src); @@ -1660,8 +1663,9 @@ static mspResult_e mspFcProcessInCommand(uint16_t cmdMSP, sbuf_t *src) break; case MSP2_INAV_SET_BATTERY_CONFIG: - if (dataSize == 25) { + if (dataSize == 27) { batteryConfigMutable()->voltage.scale = sbufReadU16(src); + batteryConfigMutable()->voltage.cellDetect = sbufReadU16(src); batteryConfigMutable()->voltage.cellMin = sbufReadU16(src); batteryConfigMutable()->voltage.cellMax = sbufReadU16(src); batteryConfigMutable()->voltage.cellWarning = sbufReadU16(src); diff --git a/src/main/fc/settings.yaml b/src/main/fc/settings.yaml index 356c7a320..9f8589df8 100644 --- a/src/main/fc/settings.yaml +++ b/src/main/fc/settings.yaml @@ -501,6 +501,11 @@ groups: condition: USE_ADC min: VBAT_SCALE_MIN max: VBAT_SCALE_MAX + - name: vbat_cell_detect_voltage + field: voltage.cellDetect + condition: USE_ADC + min: 100 + max: 500 - name: vbat_max_cell_voltage field: voltage.cellMax condition: USE_ADC diff --git a/src/main/sensors/battery.c b/src/main/sensors/battery.c index 21e742479..3baadd7d8 100644 --- a/src/main/sensors/battery.c +++ b/src/main/sensors/battery.c @@ -45,7 +45,7 @@ #define ADCVREF 3300 // in mV (3300 = 3.3V) -#define VBATT_CELL_FULL_MAX_DIFF 14 // Max difference with cell max voltage for the battery to be considered full (10mV steps) +#define VBATT_CELL_FULL_MAX_DIFF 10 // Max difference with cell max voltage for the battery to be considered full (10mV steps) #define VBATT_PRESENT_THRESHOLD 100 // Minimum voltage to consider battery present #define VBATT_STABLE_DELAY 40 // Delay after connecting battery to begin monitoring #define VBATT_HYSTERESIS 10 // Batt Hysteresis of +/-100mV for changing battery state @@ -73,13 +73,14 @@ static int32_t mWhDrawn = 0; // energy (milliWatt hours) drawn fro batteryState_e batteryState; -PG_REGISTER_WITH_RESET_TEMPLATE(batteryConfig_t, batteryConfig, PG_BATTERY_CONFIG, 1); +PG_REGISTER_WITH_RESET_TEMPLATE(batteryConfig_t, batteryConfig, PG_BATTERY_CONFIG, 2); PG_RESET_TEMPLATE(batteryConfig_t, batteryConfig, .voltage = { .scale = VBAT_SCALE_DEFAULT, - .cellMax = 424, + .cellDetect = 430, + .cellMax = 420, .cellMin = 330, .cellWarning = 350 }, @@ -148,7 +149,7 @@ void batteryUpdate(uint32_t vbatTimeDelta) delay(VBATT_STABLE_DELAY); updateBatteryVoltage(vbatTimeDelta); - unsigned cells = (batteryAdcToVoltage(vbatLatestADC) / batteryConfig()->voltage.cellMax) + 1; + unsigned cells = (batteryAdcToVoltage(vbatLatestADC) / batteryConfig()->voltage.cellDetect) + 1; if (cells > 8) cells = 8; // something is wrong, we expect 8 cells maximum (and autodetection will be problematic at 6+ cells) batteryCellCount = cells; diff --git a/src/main/sensors/battery.h b/src/main/sensors/battery.h index 5e032d6a1..dc729a07f 100644 --- a/src/main/sensors/battery.h +++ b/src/main/sensors/battery.h @@ -49,7 +49,8 @@ typedef struct batteryConfig_s { struct { uint16_t scale; // adjust this to match battery voltage to reported value - uint16_t cellMax; // maximum voltage per cell, used for auto-detecting battery voltage in 0.01V units, default is 421 (4.21V) + uint16_t cellDetect; // maximum voltage per cell, used for auto-detecting battery cell count in 0.01V units, default is 430 (4.3V) + uint16_t cellMax; // maximum voltage per cell, used for full battery detection and battery gauge voltage in 0.01V units, default is 424 (4.24V) uint16_t cellMin; // minimum voltage per cell, this triggers battery critical alarm, in 0.01V units, default is 330 (3.3V) uint16_t cellWarning; // warning voltage per cell, this triggers battery warning alarm, in 0.01V units, default is 350 (3.5V) } voltage;