From b6607a7b78f6ea3742e8247f5ce8b6166d19dcae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Fri, 28 Jan 2022 08:48:11 +0100 Subject: [PATCH] battery_status: do not publish if no voltage channel is defined This is the case for boards with digital readout, like v5x, but still enable the battery_status module for external analog driver options. An alternative would be to not run battery_status depending on config. --- .../px4_platform_common/board_common.h | 5 ++- src/modules/battery_status/analog_battery.cpp | 7 ++-- src/modules/battery_status/battery_status.cpp | 34 +++++++++++-------- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/platforms/common/include/px4_platform_common/board_common.h b/platforms/common/include/px4_platform_common/board_common.h index 1edc748721..08a23476a6 100644 --- a/platforms/common/include/px4_platform_common/board_common.h +++ b/platforms/common/include/px4_platform_common/board_common.h @@ -145,7 +145,10 @@ # define BOARD_BATT_I_LIST {ADC_BATTERY_CURRENT_CHANNEL} # define BOARD_BRICK_VALID_LIST {BOARD_ADC_BRICK_VALID} #elif BOARD_NUMBER_BRICKS == 2 -# if !defined(BOARD_NUMBER_DIGITAL_BRICKS) +# if defined(BOARD_NUMBER_DIGITAL_BRICKS) +# define BOARD_BATT_V_LIST {-1, -1} +# define BOARD_BATT_I_LIST {-1, -1} +# else # define BOARD_BATT_V_LIST {ADC_BATTERY1_VOLTAGE_CHANNEL, ADC_BATTERY2_VOLTAGE_CHANNEL} # define BOARD_BATT_I_LIST {ADC_BATTERY1_CURRENT_CHANNEL, ADC_BATTERY2_CURRENT_CHANNEL} # endif diff --git a/src/modules/battery_status/analog_battery.cpp b/src/modules/battery_status/analog_battery.cpp index 41c7832d64..e7ca828ce0 100644 --- a/src/modules/battery_status/analog_battery.cpp +++ b/src/modules/battery_status/analog_battery.cpp @@ -41,12 +41,11 @@ static constexpr int DEFAULT_V_CHANNEL[BOARD_NUMBER_BRICKS] = BOARD_BATT_V_LIST; static constexpr int DEFAULT_I_CHANNEL[BOARD_NUMBER_BRICKS] = BOARD_BATT_I_LIST; #else -static constexpr int DEFAULT_V_CHANNEL[BOARD_NUMBER_BRICKS] = {0}; -static constexpr int DEFAULT_I_CHANNEL[BOARD_NUMBER_BRICKS] = {0}; +#error "BOARD_BATT_V_LIST and BOARD_BATT_I_LIST need to be defined" #endif #else -static constexpr int DEFAULT_V_CHANNEL[1] = {0}; -static constexpr int DEFAULT_I_CHANNEL[1] = {0}; +static constexpr int DEFAULT_V_CHANNEL[1] = {-1}; +static constexpr int DEFAULT_I_CHANNEL[1] = {-1}; #endif AnalogBattery::AnalogBattery(int index, ModuleParams *parent, const int sample_interval_us, const uint8_t source, diff --git a/src/modules/battery_status/battery_status.cpp b/src/modules/battery_status/battery_status.cpp index 6ae7e86d96..ef921f3eaf 100644 --- a/src/modules/battery_status/battery_status.cpp +++ b/src/modules/battery_status/battery_status.cpp @@ -177,6 +177,7 @@ BatteryStatus::adc_poll() /* Per Brick readings with default unread channels at 0 */ float bat_current_adc_readings[BOARD_NUMBER_BRICKS] {}; float bat_voltage_adc_readings[BOARD_NUMBER_BRICKS] {}; + bool has_bat_voltage_adc_channel[BOARD_NUMBER_BRICKS] {}; int selected_source = -1; @@ -201,16 +202,19 @@ BatteryStatus::adc_poll() /* look for specific channels and process the raw voltage to measurement data */ - if (adc_report.channel_id[i] == _analogBatteries[b]->get_voltage_channel()) { - /* Voltage in volts */ - bat_voltage_adc_readings[b] = adc_report.raw_data[i] * - adc_report.v_ref / - adc_report.resolution; + if (adc_report.channel_id[i] >= 0) { + if (adc_report.channel_id[i] == _analogBatteries[b]->get_voltage_channel()) { + /* Voltage in volts */ + bat_voltage_adc_readings[b] = adc_report.raw_data[i] * + adc_report.v_ref / + adc_report.resolution; + has_bat_voltage_adc_channel[b] = true; - } else if (adc_report.channel_id[i] == _analogBatteries[b]->get_current_channel()) { - bat_current_adc_readings[b] = adc_report.raw_data[i] * - adc_report.v_ref / - adc_report.resolution; + } else if (adc_report.channel_id[i] == _analogBatteries[b]->get_current_channel()) { + bat_current_adc_readings[b] = adc_report.raw_data[i] * + adc_report.v_ref / + adc_report.resolution; + } } } @@ -218,11 +222,13 @@ BatteryStatus::adc_poll() for (int b = 0; b < BOARD_NUMBER_BRICKS; b++) { - _analogBatteries[b]->updateBatteryStatusADC( - hrt_absolute_time(), - bat_voltage_adc_readings[b], - bat_current_adc_readings[b] - ); + if (has_bat_voltage_adc_channel[b]) { // Do not publish if no voltage channel configured + _analogBatteries[b]->updateBatteryStatusADC( + hrt_absolute_time(), + bat_voltage_adc_readings[b], + bat_current_adc_readings[b] + ); + } } } }