From 4673fd8c7c0e858b5f839a1fd63c36f2d5b27f6e Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 19 Nov 2018 15:25:45 +1100 Subject: [PATCH] AP_BattMonitor: fix problem with BATT_MONITOR=0 and BATT2_MONITOR=N this fixes a problem that came up with a user where they had BATT_MONITOR=0 and BATT2_MONITOR=7. They did not get any battery monitoring. There are several ways to tackle this, but this is the simplest --- libraries/AP_BattMonitor/AP_BattMonitor.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/libraries/AP_BattMonitor/AP_BattMonitor.cpp b/libraries/AP_BattMonitor/AP_BattMonitor.cpp index 3f5ce95200..80d471c1a5 100644 --- a/libraries/AP_BattMonitor/AP_BattMonitor.cpp +++ b/libraries/AP_BattMonitor/AP_BattMonitor.cpp @@ -107,36 +107,30 @@ AP_BattMonitor::init() case AP_BattMonitor_Params::BattMonitor_TYPE_ANALOG_VOLTAGE_ONLY: case AP_BattMonitor_Params::BattMonitor_TYPE_ANALOG_VOLTAGE_AND_CURRENT: drivers[instance] = new AP_BattMonitor_Analog(*this, state[instance], _params[instance]); - _num_instances++; break; case AP_BattMonitor_Params::BattMonitor_TYPE_SOLO: drivers[instance] = new AP_BattMonitor_SMBus_Solo(*this, state[instance], _params[instance], hal.i2c_mgr->get_device(AP_BATTMONITOR_SMBUS_BUS_INTERNAL, AP_BATTMONITOR_SMBUS_I2C_ADDR, 100000, true, 20)); - _num_instances++; break; case AP_BattMonitor_Params::BattMonitor_TYPE_MAXELL: drivers[instance] = new AP_BattMonitor_SMBus_Maxell(*this, state[instance], _params[instance], hal.i2c_mgr->get_device(AP_BATTMONITOR_SMBUS_BUS_EXTERNAL, AP_BATTMONITOR_SMBUS_I2C_ADDR, 100000, true, 20)); - _num_instances++; break; case AP_BattMonitor_Params::BattMonitor_TYPE_BEBOP: #if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BEBOP || CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_DISCO drivers[instance] = new AP_BattMonitor_Bebop(*this, state[instance], _params[instance]); - _num_instances++; #endif break; case AP_BattMonitor_Params::BattMonitor_TYPE_UAVCAN_BatteryInfo: #if HAL_WITH_UAVCAN drivers[instance] = new AP_BattMonitor_UAVCAN(*this, state[instance], AP_BattMonitor_UAVCAN::UAVCAN_BATTERY_INFO, _params[instance]); - _num_instances++; #endif break; case AP_BattMonitor_Params::BattMonitor_TYPE_BLHeliESC: #ifdef HAVE_AP_BLHELI_SUPPORT drivers[instance] = new AP_BattMonitor_BLHeliESC(*this, state[instance], _params[instance]); - _num_instances++; #endif break; case AP_BattMonitor_Params::BattMonitor_TYPE_NONE: @@ -147,6 +141,11 @@ AP_BattMonitor::init() // call init function for each backend if (drivers[instance] != nullptr) { drivers[instance]->init(); + // _num_instances is actually the index for looping over instances + // the user may have BATT_MONITOR=0 and BATT2_MONITOR=7, in which case + // there will be a gap, but as we always check for drivers[instances] being nullptr + // this is safe + _num_instances = instance + 1; } } }