1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-21 07:15:18 +03:00

Prevent 0 in 'vbat_display_lpf_period' to avoid division by zero.

This commit is contained in:
mikeller 2020-03-21 12:57:10 +13:00
parent d63ba914c6
commit b020917c4b
4 changed files with 14 additions and 12 deletions

View file

@ -840,7 +840,7 @@ const clivalue_t valueTable[] = {
{ "cbat_alert_percent", VAR_UINT8 | MASTER_VALUE, .config.minmaxUnsigned = { 0, 100 }, PG_BATTERY_CONFIG, offsetof(batteryConfig_t, consumptionWarningPercentage) }, { "cbat_alert_percent", VAR_UINT8 | MASTER_VALUE, .config.minmaxUnsigned = { 0, 100 }, PG_BATTERY_CONFIG, offsetof(batteryConfig_t, consumptionWarningPercentage) },
{ "vbat_cutoff_percent", VAR_UINT8 | MASTER_VALUE, .config.minmaxUnsigned = { 0, 100 }, PG_BATTERY_CONFIG, offsetof(batteryConfig_t, lvcPercentage) }, { "vbat_cutoff_percent", VAR_UINT8 | MASTER_VALUE, .config.minmaxUnsigned = { 0, 100 }, PG_BATTERY_CONFIG, offsetof(batteryConfig_t, lvcPercentage) },
{ "force_battery_cell_count", VAR_UINT8 | MASTER_VALUE, .config.minmaxUnsigned = { 0, 24 }, PG_BATTERY_CONFIG, offsetof(batteryConfig_t, forceBatteryCellCount) }, { "force_battery_cell_count", VAR_UINT8 | MASTER_VALUE, .config.minmaxUnsigned = { 0, 24 }, PG_BATTERY_CONFIG, offsetof(batteryConfig_t, forceBatteryCellCount) },
{ "vbat_display_lpf_period", VAR_UINT8 | MASTER_VALUE, .config.minmaxUnsigned = { 0, UINT8_MAX }, PG_BATTERY_CONFIG, offsetof(batteryConfig_t, vbatDisplayLpfPeriod) }, { "vbat_display_lpf_period", VAR_UINT8 | MASTER_VALUE, .config.minmaxUnsigned = { 1, UINT8_MAX }, PG_BATTERY_CONFIG, offsetof(batteryConfig_t, vbatDisplayLpfPeriod) },
#if defined(USE_BATTERY_VOLTAGE_SAG_COMPENSATION) #if defined(USE_BATTERY_VOLTAGE_SAG_COMPENSATION)
{ "vbat_sag_lpf_period", VAR_UINT8 | MASTER_VALUE, .config.minmaxUnsigned = { 1, UINT8_MAX }, PG_BATTERY_CONFIG, offsetof(batteryConfig_t, vbatSagLpfPeriod) }, { "vbat_sag_lpf_period", VAR_UINT8 | MASTER_VALUE, .config.minmaxUnsigned = { 1, UINT8_MAX }, PG_BATTERY_CONFIG, offsetof(batteryConfig_t, vbatSagLpfPeriod) },
#endif #endif

View file

@ -232,7 +232,9 @@ void tasksInit(void)
#if defined(USE_BATTERY_VOLTAGE_SAG_COMPENSATION) #if defined(USE_BATTERY_VOLTAGE_SAG_COMPENSATION)
// If vbat motor output compensation is used, use fast vbat samplingTime // If vbat motor output compensation is used, use fast vbat samplingTime
rescheduleTask(TASK_BATTERY_VOLTAGE, TASK_PERIOD_HZ(getBatteryVoltageTaskFrequencyHz())); if (isSagCompensationConfigured()) {
rescheduleTask(TASK_BATTERY_VOLTAGE, TASK_PERIOD_HZ(FAST_VOLTAGE_TASK_FREQ_HZ));
}
#endif #endif
const bool useBatteryCurrent = batteryConfig()->currentMeterSource != CURRENT_METER_NONE; const bool useBatteryCurrent = batteryConfig()->currentMeterSource != CURRENT_METER_NONE;

View file

@ -178,7 +178,7 @@ void voltageMeterADCRefresh(void)
state->voltageUnfiltered = voltageAdcToVoltage(rawSample, config); state->voltageUnfiltered = voltageAdcToVoltage(rawSample, config);
#if defined(USE_BATTERY_VOLTAGE_SAG_COMPENSATION) #if defined(USE_BATTERY_VOLTAGE_SAG_COMPENSATION)
if (currentPidProfile->vbat_sag_compensation > 0) { if (isSagCompensationConfigured()) {
uint16_t filteredSagSample = pt1FilterApply(&state->sagFilter, rawSample); uint16_t filteredSagSample = pt1FilterApply(&state->sagFilter, rawSample);
state->voltageSagFiltered = voltageAdcToVoltage(filteredSagSample, config); state->voltageSagFiltered = voltageAdcToVoltage(filteredSagSample, config);
} }
@ -206,18 +206,18 @@ void voltageMeterADCRead(voltageSensorADC_e adcChannel, voltageMeter_t *voltageM
#endif #endif
} }
uint16_t getBatteryVoltageTaskFrequencyHz(void) bool isSagCompensationConfigured(void)
{ {
uint16_t frequencyHz = SLOW_VOLTAGE_TASK_FREQ_HZ; bool isConfigured = false;
#if defined(USE_BATTERY_VOLTAGE_SAG_COMPENSATION) #if defined(USE_BATTERY_VOLTAGE_SAG_COMPENSATION)
for (unsigned i = 0; i < PID_PROFILE_COUNT; i++) { for (unsigned i = 0; i < PID_PROFILE_COUNT; i++) {
if (pidProfiles(i)->vbat_sag_compensation > 0) { if (pidProfiles(i)->vbat_sag_compensation > 0) {
frequencyHz = FAST_VOLTAGE_TASK_FREQ_HZ; isConfigured = true;
} }
} }
#endif #endif
return frequencyHz; return isConfigured;
} }
void voltageMeterADCInit(void) void voltageMeterADCInit(void)
@ -228,10 +228,10 @@ void voltageMeterADCInit(void)
voltageMeterADCState_t *state = &voltageMeterADCStates[i]; voltageMeterADCState_t *state = &voltageMeterADCStates[i];
memset(state, 0, sizeof(voltageMeterADCState_t)); memset(state, 0, sizeof(voltageMeterADCState_t));
pt1FilterInit(&state->displayFilter, pt1FilterGain(GET_BATTERY_LPF_FREQUENCY(batteryConfig()->vbatDisplayLpfPeriod), HZ_TO_INTERVAL(getBatteryVoltageTaskFrequencyHz()))); pt1FilterInit(&state->displayFilter, pt1FilterGain(GET_BATTERY_LPF_FREQUENCY(batteryConfig()->vbatDisplayLpfPeriod), HZ_TO_INTERVAL(isSagCompensationConfigured() ? FAST_VOLTAGE_TASK_FREQ_HZ : SLOW_VOLTAGE_TASK_FREQ_HZ)));
#if defined(USE_BATTERY_VOLTAGE_SAG_COMPENSATION) #if defined(USE_BATTERY_VOLTAGE_SAG_COMPENSATION)
if (currentPidProfile->vbat_sag_compensation > 0) { if (isSagCompensationConfigured()) {
pt1FilterInit(&state->sagFilter, pt1FilterGain(GET_BATTERY_LPF_FREQUENCY(batteryConfig()->vbatSagLpfPeriod), HZ_TO_INTERVAL(getBatteryVoltageTaskFrequencyHz()))); pt1FilterInit(&state->sagFilter, pt1FilterGain(GET_BATTERY_LPF_FREQUENCY(batteryConfig()->vbatSagLpfPeriod), HZ_TO_INTERVAL(FAST_VOLTAGE_TASK_FREQ_HZ)));
} }
#endif #endif
} }
@ -257,7 +257,7 @@ void voltageMeterESCInit(void)
{ {
#ifdef USE_ESC_SENSOR #ifdef USE_ESC_SENSOR
memset(&voltageMeterESCState, 0, sizeof(voltageMeterESCState_t)); memset(&voltageMeterESCState, 0, sizeof(voltageMeterESCState_t));
pt1FilterInit(&voltageMeterESCState.displayFilter, pt1FilterGain(GET_BATTERY_LPF_FREQUENCY(batteryConfig()->vbatDisplayLpfPeriod), HZ_TO_INTERVAL(getBatteryVoltageTaskFrequencyHz()))); pt1FilterInit(&voltageMeterESCState.displayFilter, pt1FilterGain(GET_BATTERY_LPF_FREQUENCY(batteryConfig()->vbatDisplayLpfPeriod), HZ_TO_INTERVAL(isSagCompensationConfigured() ? FAST_VOLTAGE_TASK_FREQ_HZ : SLOW_VOLTAGE_TASK_FREQ_HZ)));
#endif #endif
} }

View file

@ -119,4 +119,4 @@ extern const uint8_t supportedVoltageMeterCount;
extern const uint8_t voltageMeterIds[]; extern const uint8_t voltageMeterIds[];
void voltageMeterRead(voltageMeterId_e id, voltageMeter_t *voltageMeter); void voltageMeterRead(voltageMeterId_e id, voltageMeter_t *voltageMeter);
uint16_t getBatteryVoltageTaskFrequencyHz(void); bool isSagCompensationConfigured(void);