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
This commit is contained in:
Andrew Tridgell 2018-11-19 15:25:45 +11:00
parent ba3303dc61
commit 4673fd8c7c

View File

@ -107,36 +107,30 @@ AP_BattMonitor::init()
case AP_BattMonitor_Params::BattMonitor_TYPE_ANALOG_VOLTAGE_ONLY: case AP_BattMonitor_Params::BattMonitor_TYPE_ANALOG_VOLTAGE_ONLY:
case AP_BattMonitor_Params::BattMonitor_TYPE_ANALOG_VOLTAGE_AND_CURRENT: case AP_BattMonitor_Params::BattMonitor_TYPE_ANALOG_VOLTAGE_AND_CURRENT:
drivers[instance] = new AP_BattMonitor_Analog(*this, state[instance], _params[instance]); drivers[instance] = new AP_BattMonitor_Analog(*this, state[instance], _params[instance]);
_num_instances++;
break; break;
case AP_BattMonitor_Params::BattMonitor_TYPE_SOLO: case AP_BattMonitor_Params::BattMonitor_TYPE_SOLO:
drivers[instance] = new AP_BattMonitor_SMBus_Solo(*this, state[instance], _params[instance], 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, hal.i2c_mgr->get_device(AP_BATTMONITOR_SMBUS_BUS_INTERNAL, AP_BATTMONITOR_SMBUS_I2C_ADDR,
100000, true, 20)); 100000, true, 20));
_num_instances++;
break; break;
case AP_BattMonitor_Params::BattMonitor_TYPE_MAXELL: case AP_BattMonitor_Params::BattMonitor_TYPE_MAXELL:
drivers[instance] = new AP_BattMonitor_SMBus_Maxell(*this, state[instance], _params[instance], 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, hal.i2c_mgr->get_device(AP_BATTMONITOR_SMBUS_BUS_EXTERNAL, AP_BATTMONITOR_SMBUS_I2C_ADDR,
100000, true, 20)); 100000, true, 20));
_num_instances++;
break; break;
case AP_BattMonitor_Params::BattMonitor_TYPE_BEBOP: 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 #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]); drivers[instance] = new AP_BattMonitor_Bebop(*this, state[instance], _params[instance]);
_num_instances++;
#endif #endif
break; break;
case AP_BattMonitor_Params::BattMonitor_TYPE_UAVCAN_BatteryInfo: case AP_BattMonitor_Params::BattMonitor_TYPE_UAVCAN_BatteryInfo:
#if HAL_WITH_UAVCAN #if HAL_WITH_UAVCAN
drivers[instance] = new AP_BattMonitor_UAVCAN(*this, state[instance], AP_BattMonitor_UAVCAN::UAVCAN_BATTERY_INFO, _params[instance]); drivers[instance] = new AP_BattMonitor_UAVCAN(*this, state[instance], AP_BattMonitor_UAVCAN::UAVCAN_BATTERY_INFO, _params[instance]);
_num_instances++;
#endif #endif
break; break;
case AP_BattMonitor_Params::BattMonitor_TYPE_BLHeliESC: case AP_BattMonitor_Params::BattMonitor_TYPE_BLHeliESC:
#ifdef HAVE_AP_BLHELI_SUPPORT #ifdef HAVE_AP_BLHELI_SUPPORT
drivers[instance] = new AP_BattMonitor_BLHeliESC(*this, state[instance], _params[instance]); drivers[instance] = new AP_BattMonitor_BLHeliESC(*this, state[instance], _params[instance]);
_num_instances++;
#endif #endif
break; break;
case AP_BattMonitor_Params::BattMonitor_TYPE_NONE: case AP_BattMonitor_Params::BattMonitor_TYPE_NONE:
@ -147,6 +141,11 @@ AP_BattMonitor::init()
// call init function for each backend // call init function for each backend
if (drivers[instance] != nullptr) { if (drivers[instance] != nullptr) {
drivers[instance]->init(); 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;
} }
} }
} }