diff --git a/libraries/AP_BattMonitor/AP_BattMonitor_Analog.cpp b/libraries/AP_BattMonitor/AP_BattMonitor_Analog.cpp index 6e341dd035..c6e1a26c57 100644 --- a/libraries/AP_BattMonitor/AP_BattMonitor_Analog.cpp +++ b/libraries/AP_BattMonitor/AP_BattMonitor_Analog.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include "AP_BattMonitor_Analog.h" @@ -88,7 +89,15 @@ AP_BattMonitor_Analog::AP_BattMonitor_Analog(AP_BattMonitor &mon, _state.var_info = var_info; _volt_pin_analog_source = hal.analogin->channel(_volt_pin); - _curr_pin_analog_source = hal.analogin->channel(_curr_pin); + if (_volt_pin_analog_source == nullptr) { + AP_BoardConfig::config_error("No analog channels for battery %d", mon_state.instance); + } + if ((AP_BattMonitor::Type)_params._type.get() == AP_BattMonitor::Type::ANALOG_VOLTAGE_AND_CURRENT) { + _curr_pin_analog_source = hal.analogin->channel(_curr_pin); + if (_curr_pin_analog_source == nullptr) { + AP_BoardConfig::config_error("No analog channels for battery %d", mon_state.instance); + } + } } @@ -124,7 +133,8 @@ AP_BattMonitor_Analog::read() /// return true if battery provides current info bool AP_BattMonitor_Analog::has_current() const { - return ((AP_BattMonitor::Type)_params._type.get() == AP_BattMonitor::Type::ANALOG_VOLTAGE_AND_CURRENT); + return (_curr_pin_analog_source != nullptr) && + ((AP_BattMonitor::Type)_params._type.get() == AP_BattMonitor::Type::ANALOG_VOLTAGE_AND_CURRENT); } #endif // AP_BATTERY_ANALOG_ENABLED