Skip to content

Commit

Permalink
Merge pull request iNavFlight#3166 from shellixyz/add_vbat_cell_detec…
Browse files Browse the repository at this point in the history
…t_voltage

Replace vbat_max_cell_voltage by the new vbat_cell_detect_voltage setting for detecting number of battery cells
  • Loading branch information
digitalentity authored May 6, 2018
2 parents 05a9be3 + 49b3444 commit 39de8c5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
8 changes: 6 additions & 2 deletions src/main/fc/fc_msp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions src/main/fc/settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions src/main/sensors/battery.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
},
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/main/sensors/battery.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 39de8c5

Please sign in to comment.