diff --git a/src/main/interface/settings.c b/src/main/interface/settings.c index 70809611a..59cdd8dfc 100644 --- a/src/main/interface/settings.c +++ b/src/main/interface/settings.c @@ -782,8 +782,10 @@ const clivalue_t valueTable[] = { { "use_vbat_alerts", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, .config.lookup = { TABLE_OFF_ON }, PG_BATTERY_CONFIG, offsetof(batteryConfig_t, useVBatAlerts) }, { "use_cbat_alerts", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, .config.lookup = { TABLE_OFF_ON }, PG_BATTERY_CONFIG, offsetof(batteryConfig_t, useConsumptionAlerts) }, { "cbat_alert_percent", VAR_UINT8 | MASTER_VALUE, .config.minmax = { 0, 100 }, PG_BATTERY_CONFIG, offsetof(batteryConfig_t, consumptionWarningPercentage) }, - { "vbat_cutoff_percent", VAR_UINT8 | MASTER_VALUE, .config.minmax = { 0, 100 }, PG_BATTERY_CONFIG, offsetof(batteryConfig_t, lvcPercentage) }, + { "vbat_cutoff_percent", VAR_UINT8 | MASTER_VALUE, .config.minmax = { 0, 100 }, PG_BATTERY_CONFIG, offsetof(batteryConfig_t, lvcPercentage) }, { "force_battery_cell_count", VAR_UINT8 | MASTER_VALUE, .config.minmax = { 0, 24 }, PG_BATTERY_CONFIG, offsetof(batteryConfig_t, forceBatteryCellCount) }, + { "vbat_lpf_period", VAR_UINT8 | MASTER_VALUE, .config.minmax = { 0, UINT8_MAX }, PG_BATTERY_CONFIG, offsetof(batteryConfig_t, vbatLpfPeriod) }, + { "ibat_lpf_period", VAR_UINT8 | MASTER_VALUE, .config.minmax = { 0, UINT8_MAX }, PG_BATTERY_CONFIG, offsetof(batteryConfig_t, ibatLpfPeriod) }, // PG_VOLTAGE_SENSOR_ADC_CONFIG { "vbat_scale", VAR_UINT8 | MASTER_VALUE, .config.minmax = { VBAT_SCALE_MIN, VBAT_SCALE_MAX }, PG_VOLTAGE_SENSOR_ADC_CONFIG, offsetof(voltageSensorADCConfig_t, vbatscale) }, diff --git a/src/main/sensors/battery.c b/src/main/sensors/battery.c index b24ba9a61..5ed4479b6 100644 --- a/src/main/sensors/battery.c +++ b/src/main/sensors/battery.c @@ -114,7 +114,10 @@ PG_RESET_TEMPLATE(batteryConfig_t, batteryConfig, .consumptionWarningPercentage = 10, .vbathysteresis = 1, - .vbatfullcellvoltage = 410 + .vbatfullcellvoltage = 410, + + .vbatLpfPeriod = 30, + .ibatLpfPeriod = 10, ); void batteryUpdateVoltage(timeUs_t currentTimeUs) diff --git a/src/main/sensors/battery.h b/src/main/sensors/battery.h index 4c4b99c02..2685bcad5 100644 --- a/src/main/sensors/battery.h +++ b/src/main/sensors/battery.h @@ -30,6 +30,8 @@ #define VBAT_CELL_VOTAGE_RANGE_MIN 100 #define VBAT_CELL_VOTAGE_RANGE_MAX 500 +#define GET_BATTERY_LPF_FREQUENCY(period) (1.0f / (period / 10)) + typedef struct batteryConfig_s { // voltage uint16_t vbatmaxcellvoltage; // maximum voltage per cell, used for auto-detecting battery voltage in 0.01V units, default is 430 (4.30V) @@ -51,8 +53,9 @@ typedef struct batteryConfig_s { uint16_t vbatfullcellvoltage; // Cell voltage at which the battery is deemed to be "full" 0.01V units, default is 410 (4.1V) - uint8_t forceBatteryCellCount; // number of cells in battery, used for overwriting auto-detected cell count if someone has issues with it. - + uint8_t forceBatteryCellCount; // Number of cells in battery, used for overwriting auto-detected cell count if someone has issues with it. + uint8_t vbatLpfPeriod; // Period of the cutoff frequency for the Vbat filter (in 0.1 s) + uint8_t ibatLpfPeriod; // Period of the cutoff frequency for the Ibat filter (in 0.1 s) } batteryConfig_t; PG_DECLARE(batteryConfig_t, batteryConfig); diff --git a/src/main/sensors/current.c b/src/main/sensors/current.c index 881d629aa..2de6071ac 100644 --- a/src/main/sensors/current.c +++ b/src/main/sensors/current.c @@ -23,6 +23,7 @@ #include "string.h" #include "platform.h" + #include "build/build_config.h" #include "build/debug.h" @@ -34,12 +35,13 @@ #include "pg/pg.h" #include "pg/pg_ids.h" -#include "config/config_reset.h" #include "sensors/adcinternal.h" -#include "sensors/current.h" +#include "sensors/battery.h" #include "sensors/esc_sensor.h" +#include "current.h" + const char * const currentMeterSourceNames[CURRENT_METER_COUNT] = { "NONE", "ADC", "VIRTUAL", "ESC", "MSP" }; @@ -86,7 +88,6 @@ void currentMeterReset(currentMeter_t *meter) // ADC/Virtual shared // -#define IBAT_LPF_FREQ 2.0f static biquadFilter_t adciBatFilter; #ifndef CURRENT_METER_SCALE_DEFAULT @@ -140,7 +141,7 @@ currentMeterADCState_t currentMeterADCState; void currentMeterADCInit(void) { memset(¤tMeterADCState, 0, sizeof(currentMeterADCState_t)); - biquadFilterInitLPF(&adciBatFilter, IBAT_LPF_FREQ, HZ_TO_INTERVAL_US(50)); + biquadFilterInitLPF(&adciBatFilter, GET_BATTERY_LPF_FREQUENCY(batteryConfig()->ibatLpfPeriod), HZ_TO_INTERVAL_US(50)); } void currentMeterADCRefresh(int32_t lastUpdateAt) diff --git a/src/main/sensors/voltage.c b/src/main/sensors/voltage.c index 654b637aa..8baa4669a 100644 --- a/src/main/sensors/voltage.c +++ b/src/main/sensors/voltage.c @@ -26,20 +26,23 @@ #include "build/build_config.h" -#include "common/maths.h" #include "common/filter.h" +#include "common/maths.h" #include "common/utils.h" +#include "config/config_reset.h" + #include "drivers/adc.h" #include "pg/pg.h" #include "pg/pg_ids.h" -#include "config/config_reset.h" #include "sensors/adcinternal.h" -#include "sensors/voltage.h" +#include "sensors/battery.h" #include "sensors/esc_sensor.h" +#include "voltage.h" + const char * const voltageMeterSourceNames[VOLTAGE_METER_COUNT] = { "NONE", "ADC", "ESC" }; @@ -191,7 +194,7 @@ void voltageMeterADCInit(void) voltageMeterADCState_t *state = &voltageMeterADCStates[i]; memset(state, 0, sizeof(voltageMeterADCState_t)); - biquadFilterInitLPF(&state->filter, VBAT_LPF_FREQ, HZ_TO_INTERVAL_US(50)); + biquadFilterInitLPF(&state->filter, GET_BATTERY_LPF_FREQUENCY(batteryConfig()->vbatLpfPeriod), HZ_TO_INTERVAL_US(50)); } } @@ -215,7 +218,7 @@ void voltageMeterESCInit(void) { #ifdef USE_ESC_SENSOR memset(&voltageMeterESCState, 0, sizeof(voltageMeterESCState_t)); - biquadFilterInitLPF(&voltageMeterESCState.filter, VBAT_LPF_FREQ, HZ_TO_INTERVAL_US(50)); + biquadFilterInitLPF(&voltageMeterESCState.filter, GET_BATTERY_LPF_FREQUENCY(batteryConfig()->vbatLpfPeriod), HZ_TO_INTERVAL_US(50)); #endif } diff --git a/src/main/sensors/voltage.h b/src/main/sensors/voltage.h index 0485717d7..11566cfb0 100644 --- a/src/main/sensors/voltage.h +++ b/src/main/sensors/voltage.h @@ -67,8 +67,6 @@ typedef enum { #define VBAT_MULTIPLIER_MIN 1 #define VBAT_MULTIPLIER_MAX 255 -#define VBAT_LPF_FREQ 0.5f - #ifndef MAX_VOLTAGE_SENSOR_ADC #define MAX_VOLTAGE_SENSOR_ADC 1 // VBAT - some boards have external, 12V, 9V and 5V meters. #endif