2013-09-28 10:35:27 -03:00
|
|
|
#include "AP_BattMonitor.h"
|
2014-12-04 01:27:56 -04:00
|
|
|
#include "AP_BattMonitor_Analog.h"
|
|
|
|
#include "AP_BattMonitor_SMBus.h"
|
2015-07-23 06:16:37 -03:00
|
|
|
#include "AP_BattMonitor_Bebop.h"
|
2018-05-30 18:35:11 -03:00
|
|
|
#include "AP_BattMonitor_BLHeliESC.h"
|
2018-11-19 04:59:58 -04:00
|
|
|
#include "AP_BattMonitor_Sum.h"
|
2019-03-12 05:02:29 -03:00
|
|
|
#include "AP_BattMonitor_FuelFlow.h"
|
2018-07-20 10:01:13 -03:00
|
|
|
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
|
2018-02-16 18:32:25 -04:00
|
|
|
#if HAL_WITH_UAVCAN
|
|
|
|
#include "AP_BattMonitor_UAVCAN.h"
|
|
|
|
#endif
|
2018-07-20 10:01:13 -03:00
|
|
|
|
2016-07-25 22:10:17 -03:00
|
|
|
#include <AP_Vehicle/AP_Vehicle_Type.h>
|
2019-01-18 00:23:42 -04:00
|
|
|
#include <AP_Logger/AP_Logger.h>
|
2017-11-09 18:33:44 -04:00
|
|
|
#include <GCS_MAVLink/GCS.h>
|
2018-11-27 20:37:13 -04:00
|
|
|
#include <AP_Notify/AP_Notify.h>
|
2013-09-28 10:35:27 -03:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2018-01-16 15:09:28 -04:00
|
|
|
AP_BattMonitor *AP_BattMonitor::_singleton;
|
|
|
|
|
2015-10-25 14:03:46 -03:00
|
|
|
const AP_Param::GroupInfo AP_BattMonitor::var_info[] = {
|
2017-10-27 02:36:49 -03:00
|
|
|
// 0 - 18, 20- 22 used by old parameter indexes
|
2016-01-08 13:16:29 -04:00
|
|
|
|
2017-10-27 02:36:49 -03:00
|
|
|
// @Group: _
|
|
|
|
// @Path: AP_BattMonitor_Params.cpp
|
2018-02-28 17:29:36 -04:00
|
|
|
AP_SUBGROUPINFO(_params[0], "_", 23, AP_BattMonitor, AP_BattMonitor_Params),
|
2016-01-08 13:16:29 -04:00
|
|
|
|
2017-10-27 02:36:49 -03:00
|
|
|
// @Group: 2_
|
|
|
|
// @Path: AP_BattMonitor_Params.cpp
|
|
|
|
AP_SUBGROUPINFO(_params[1], "2_", 24, AP_BattMonitor, AP_BattMonitor_Params),
|
2014-12-04 01:27:56 -04:00
|
|
|
|
2018-09-12 20:01:04 -03:00
|
|
|
// @Group: 3_
|
|
|
|
// @Path: AP_BattMonitor_Params.cpp
|
|
|
|
AP_SUBGROUPINFO(_params[2], "3_", 25, AP_BattMonitor, AP_BattMonitor_Params),
|
|
|
|
|
|
|
|
// @Group: 4_
|
|
|
|
// @Path: AP_BattMonitor_Params.cpp
|
|
|
|
AP_SUBGROUPINFO(_params[3], "4_", 26, AP_BattMonitor, AP_BattMonitor_Params),
|
|
|
|
|
|
|
|
// @Group: 5_
|
|
|
|
// @Path: AP_BattMonitor_Params.cpp
|
|
|
|
AP_SUBGROUPINFO(_params[4], "5_", 27, AP_BattMonitor, AP_BattMonitor_Params),
|
|
|
|
|
|
|
|
// @Group: 6_
|
|
|
|
// @Path: AP_BattMonitor_Params.cpp
|
|
|
|
AP_SUBGROUPINFO(_params[5], "6_", 28, AP_BattMonitor, AP_BattMonitor_Params),
|
|
|
|
|
|
|
|
// @Group: 7_
|
|
|
|
// @Path: AP_BattMonitor_Params.cpp
|
|
|
|
AP_SUBGROUPINFO(_params[6], "7_", 29, AP_BattMonitor, AP_BattMonitor_Params),
|
|
|
|
|
|
|
|
// @Group: 8_
|
|
|
|
// @Path: AP_BattMonitor_Params.cpp
|
|
|
|
AP_SUBGROUPINFO(_params[7], "8_", 30, AP_BattMonitor, AP_BattMonitor_Params),
|
|
|
|
|
|
|
|
// @Group: 9_
|
|
|
|
// @Path: AP_BattMonitor_Params.cpp
|
|
|
|
AP_SUBGROUPINFO(_params[8], "9_", 31, AP_BattMonitor, AP_BattMonitor_Params),
|
|
|
|
|
2013-09-28 10:35:27 -03:00
|
|
|
AP_GROUPEND
|
|
|
|
};
|
|
|
|
|
|
|
|
// Default constructor.
|
|
|
|
// Note that the Vector/Matrix constructors already implicitly zero
|
|
|
|
// their values.
|
|
|
|
//
|
2017-11-09 18:33:44 -04:00
|
|
|
AP_BattMonitor::AP_BattMonitor(uint32_t log_battery_bit, battery_failsafe_handler_fn_t battery_failsafe_handler_fn, const int8_t *failsafe_priorities) :
|
2018-01-16 15:09:28 -04:00
|
|
|
_log_battery_bit(log_battery_bit),
|
2018-03-28 03:04:30 -03:00
|
|
|
_num_instances(0),
|
2017-11-09 18:33:44 -04:00
|
|
|
_battery_failsafe_handler_fn(battery_failsafe_handler_fn),
|
2018-03-28 03:04:30 -03:00
|
|
|
_failsafe_priorities(failsafe_priorities)
|
2013-09-28 10:35:27 -03:00
|
|
|
{
|
|
|
|
AP_Param::setup_object_defaults(this, var_info);
|
2018-01-16 15:09:28 -04:00
|
|
|
|
|
|
|
if (_singleton != nullptr) {
|
|
|
|
AP_HAL::panic("AP_BattMonitor must be singleton");
|
|
|
|
}
|
|
|
|
_singleton = this;
|
2013-09-28 10:35:27 -03:00
|
|
|
}
|
|
|
|
|
2014-12-04 01:27:56 -04:00
|
|
|
// init - instantiate the battery monitors
|
2013-09-28 10:35:27 -03:00
|
|
|
void
|
|
|
|
AP_BattMonitor::init()
|
|
|
|
{
|
2014-12-04 01:27:56 -04:00
|
|
|
// check init has not been called before
|
|
|
|
if (_num_instances != 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-09 18:33:44 -04:00
|
|
|
_highest_failsafe_priority = INT8_MAX;
|
|
|
|
|
2017-10-27 02:36:49 -03:00
|
|
|
convert_params();
|
|
|
|
|
2018-10-30 18:26:54 -03:00
|
|
|
#ifdef HAL_BATT_MONITOR_DEFAULT
|
|
|
|
if (_params[0]._type == 0) {
|
|
|
|
// we can't use set_default() as the type is used as a flag for parameter conversion
|
|
|
|
_params[0]._type.set((AP_BattMonitor_Params::BattMonitor_Type)HAL_BATT_MONITOR_DEFAULT);
|
|
|
|
}
|
2015-07-23 07:49:48 -03:00
|
|
|
#endif
|
|
|
|
|
2014-12-04 01:27:56 -04:00
|
|
|
// create each instance
|
|
|
|
for (uint8_t instance=0; instance<AP_BATT_MONITOR_MAX_INSTANCES; instance++) {
|
2017-04-08 00:27:31 -03:00
|
|
|
// clear out the cell voltages
|
|
|
|
memset(&state[instance].cell_voltages, 0xFF, sizeof(cells));
|
|
|
|
|
2017-10-27 02:36:49 -03:00
|
|
|
switch (get_type(instance)) {
|
|
|
|
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]);
|
2015-07-23 07:49:48 -03:00
|
|
|
break;
|
2017-10-27 02:36:49 -03:00
|
|
|
case AP_BattMonitor_Params::BattMonitor_TYPE_SOLO:
|
|
|
|
drivers[instance] = new AP_BattMonitor_SMBus_Solo(*this, state[instance], _params[instance],
|
2018-01-10 00:28:03 -04:00
|
|
|
hal.i2c_mgr->get_device(AP_BATTMONITOR_SMBUS_BUS_INTERNAL, AP_BATTMONITOR_SMBUS_I2C_ADDR,
|
|
|
|
100000, true, 20));
|
2017-02-08 20:28:14 -04:00
|
|
|
break;
|
2017-10-27 02:36:49 -03:00
|
|
|
case AP_BattMonitor_Params::BattMonitor_TYPE_MAXELL:
|
|
|
|
drivers[instance] = new AP_BattMonitor_SMBus_Maxell(*this, state[instance], _params[instance],
|
2018-01-10 00:28:03 -04:00
|
|
|
hal.i2c_mgr->get_device(AP_BATTMONITOR_SMBUS_BUS_EXTERNAL, AP_BATTMONITOR_SMBUS_I2C_ADDR,
|
|
|
|
100000, true, 20));
|
2015-07-23 07:49:48 -03:00
|
|
|
break;
|
2017-10-27 02:36:49 -03:00
|
|
|
case AP_BattMonitor_Params::BattMonitor_TYPE_BEBOP:
|
2016-06-06 19:14:35 -03:00
|
|
|
#if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BEBOP || CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_DISCO
|
2017-10-27 02:36:49 -03:00
|
|
|
drivers[instance] = new AP_BattMonitor_Bebop(*this, state[instance], _params[instance]);
|
2018-02-16 18:32:25 -04:00
|
|
|
#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]);
|
2018-05-30 18:35:11 -03:00
|
|
|
#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]);
|
2014-12-04 01:27:56 -04:00
|
|
|
#endif
|
2015-07-23 07:49:48 -03:00
|
|
|
break;
|
2018-11-19 04:59:58 -04:00
|
|
|
case AP_BattMonitor_Params::BattMonitor_TYPE_Sum:
|
|
|
|
drivers[instance] = new AP_BattMonitor_Sum(*this, state[instance], _params[instance], instance);
|
|
|
|
break;
|
2019-03-12 05:02:29 -03:00
|
|
|
case AP_BattMonitor_Params::BattMonitor_TYPE_FuelFlow:
|
|
|
|
drivers[instance] = new AP_BattMonitor_FuelFlow(*this, state[instance], _params[instance]);
|
|
|
|
break;
|
2017-10-27 02:36:49 -03:00
|
|
|
case AP_BattMonitor_Params::BattMonitor_TYPE_NONE:
|
|
|
|
default:
|
|
|
|
break;
|
2014-12-04 01:27:56 -04:00
|
|
|
}
|
2015-04-09 02:37:15 -03:00
|
|
|
|
|
|
|
// call init function for each backend
|
2016-10-30 02:24:21 -03:00
|
|
|
if (drivers[instance] != nullptr) {
|
2015-04-09 02:37:15 -03:00
|
|
|
drivers[instance]->init();
|
2018-11-19 00:25:45 -04:00
|
|
|
// _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;
|
2015-04-09 02:37:15 -03:00
|
|
|
}
|
2014-08-08 23:13:49 -03:00
|
|
|
}
|
2013-09-28 10:35:27 -03:00
|
|
|
}
|
|
|
|
|
2017-10-27 02:36:49 -03:00
|
|
|
void AP_BattMonitor::convert_params(void) {
|
|
|
|
if (_params[0]._type.configured_in_storage()) {
|
|
|
|
// _params[0]._type will always be configured in storage after conversion is done the first time
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define SECOND_BATT_CONVERT_MASK 0x80
|
|
|
|
const struct ConversionTable {
|
|
|
|
uint8_t old_element;
|
|
|
|
uint8_t new_index; // upper bit used to indicate if its the first or second instance
|
|
|
|
}conversionTable[22] = {
|
|
|
|
{ 0, 0 }, // _MONITOR
|
|
|
|
{ 1, 1 }, // _VOLT_PIN
|
|
|
|
{ 2, 2 }, // _CURR_PIN
|
|
|
|
{ 3, 3 }, // _VOLT_MULT
|
|
|
|
{ 4, 4 }, // _AMP_PERVOLT
|
|
|
|
{ 5, 5 }, // _AMP_OFFSET
|
|
|
|
{ 6, 6 }, // _CAPACITY
|
|
|
|
{ 9, 7 }, // _WATT_MAX
|
|
|
|
{10, 8 }, // _SERIAL_NUM
|
|
|
|
{11, (SECOND_BATT_CONVERT_MASK | 0)}, // 2_MONITOR
|
|
|
|
{12, (SECOND_BATT_CONVERT_MASK | 1)}, // 2_VOLT_PIN
|
|
|
|
{13, (SECOND_BATT_CONVERT_MASK | 2)}, // 2_CURR_PIN
|
|
|
|
{14, (SECOND_BATT_CONVERT_MASK | 3)}, // 2_VOLT_MULT
|
|
|
|
{15, (SECOND_BATT_CONVERT_MASK | 4)}, // 2_AMP_PERVOLT
|
|
|
|
{16, (SECOND_BATT_CONVERT_MASK | 5)}, // 2_AMP_OFFSET
|
|
|
|
{17, (SECOND_BATT_CONVERT_MASK | 6)}, // 2_CAPACITY
|
|
|
|
{18, (SECOND_BATT_CONVERT_MASK | 7)}, // 2_WATT_MAX
|
|
|
|
{20, (SECOND_BATT_CONVERT_MASK | 8)}, // 2_SERIAL_NUM
|
|
|
|
{21, 9 }, // _LOW_TIMER
|
|
|
|
{22, 10 }, // _LOW_TYPE
|
|
|
|
{21, (SECOND_BATT_CONVERT_MASK | 9)}, // 2_LOW_TIMER
|
|
|
|
{22, (SECOND_BATT_CONVERT_MASK |10)}, // 2_LOW_TYPE
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
char param_name[17];
|
|
|
|
AP_Param::ConversionInfo info;
|
|
|
|
info.new_name = param_name;
|
|
|
|
|
|
|
|
#if APM_BUILD_TYPE(APM_BUILD_ArduPlane)
|
|
|
|
info.old_key = 166;
|
|
|
|
#elif APM_BUILD_TYPE(APM_BUILD_ArduCopter)
|
|
|
|
info.old_key = 36;
|
|
|
|
#elif APM_BUILD_TYPE(APM_BUILD_ArduSub)
|
|
|
|
info.old_key = 33;
|
|
|
|
#elif APM_BUILD_TYPE(APM_BUILD_APMrover2)
|
|
|
|
info.old_key = 145;
|
|
|
|
#else
|
|
|
|
_params[0]._type.save(true);
|
|
|
|
return; // no conversion is supported on this platform
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < ARRAY_SIZE(conversionTable); i++) {
|
|
|
|
uint8_t param_instance = conversionTable[i].new_index >> 7;
|
|
|
|
uint8_t destination_index = 0x7F & conversionTable[i].new_index;
|
|
|
|
|
|
|
|
info.old_group_element = conversionTable[i].old_element;
|
|
|
|
info.type = (ap_var_type)AP_BattMonitor_Params::var_info[destination_index].type;
|
|
|
|
if (param_instance) {
|
2018-09-06 00:30:32 -03:00
|
|
|
hal.util->snprintf(param_name, sizeof(param_name), "BATT2_%s", AP_BattMonitor_Params::var_info[destination_index].name);
|
2017-10-27 02:36:49 -03:00
|
|
|
} else {
|
2018-09-06 00:30:32 -03:00
|
|
|
hal.util->snprintf(param_name, sizeof(param_name), "BATT_%s", AP_BattMonitor_Params::var_info[destination_index].name);
|
2017-10-27 02:36:49 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
AP_Param::convert_old_parameter(&info, 1.0f, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// force _params[0]._type into storage to flag that conversion has been done
|
|
|
|
_params[0]._type.save(true);
|
|
|
|
}
|
|
|
|
|
2014-12-04 01:27:56 -04:00
|
|
|
// read - read the voltage and current for all instances
|
2013-09-28 10:35:27 -03:00
|
|
|
void
|
|
|
|
AP_BattMonitor::read()
|
|
|
|
{
|
2016-06-02 19:02:20 -03:00
|
|
|
for (uint8_t i=0; i<_num_instances; i++) {
|
2017-10-27 02:36:49 -03:00
|
|
|
if (drivers[i] != nullptr && _params[i].type() != AP_BattMonitor_Params::BattMonitor_TYPE_NONE) {
|
2014-12-04 01:27:56 -04:00
|
|
|
drivers[i]->read();
|
2017-05-23 04:12:58 -03:00
|
|
|
drivers[i]->update_resistance_estimate();
|
2014-08-08 23:13:49 -03:00
|
|
|
}
|
2013-09-28 10:35:27 -03:00
|
|
|
}
|
2018-01-16 15:09:28 -04:00
|
|
|
|
2019-02-11 04:16:49 -04:00
|
|
|
AP_Logger *logger = AP_Logger::get_singleton();
|
|
|
|
if (logger->should_log(_log_battery_bit)) {
|
|
|
|
logger->Write_Current();
|
|
|
|
logger->Write_Power();
|
2018-01-16 15:09:28 -04:00
|
|
|
}
|
2017-11-09 18:33:44 -04:00
|
|
|
|
|
|
|
check_failsafes();
|
2018-11-27 20:37:13 -04:00
|
|
|
|
|
|
|
checkPoweringOff();
|
2014-12-04 01:27:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// healthy - returns true if monitor is functioning
|
|
|
|
bool AP_BattMonitor::healthy(uint8_t instance) const {
|
2017-10-27 02:36:49 -03:00
|
|
|
return instance < _num_instances && state[instance].healthy;
|
2014-12-04 01:27:56 -04:00
|
|
|
}
|
2013-09-28 10:35:27 -03:00
|
|
|
|
2014-12-04 01:27:56 -04:00
|
|
|
/// voltage - returns battery voltage in volts
|
|
|
|
float AP_BattMonitor::voltage(uint8_t instance) const
|
|
|
|
{
|
2016-06-02 19:02:20 -03:00
|
|
|
if (instance < _num_instances) {
|
2017-10-27 02:36:49 -03:00
|
|
|
return state[instance].voltage;
|
2014-12-04 01:27:56 -04:00
|
|
|
} else {
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-23 04:12:58 -03:00
|
|
|
/// get voltage with sag removed (based on battery current draw and resistance)
|
2017-05-25 02:09:20 -03:00
|
|
|
/// this will always be greater than or equal to the raw voltage
|
2017-05-23 04:12:58 -03:00
|
|
|
float AP_BattMonitor::voltage_resting_estimate(uint8_t instance) const
|
|
|
|
{
|
2018-09-12 18:18:06 -03:00
|
|
|
if (instance < _num_instances && drivers[instance] != nullptr) {
|
|
|
|
return drivers[instance]->voltage_resting_estimate();
|
2017-05-23 04:12:58 -03:00
|
|
|
} else {
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-04 01:27:56 -04:00
|
|
|
/// current_amps - returns the instantaneous current draw in amperes
|
2019-07-07 11:34:41 -03:00
|
|
|
bool AP_BattMonitor::current_amps(float ¤t, uint8_t instance) const {
|
|
|
|
if ((instance < _num_instances) && (drivers[instance] != nullptr) && drivers[instance]->has_current()) {
|
|
|
|
current = state[instance].current_amps;
|
|
|
|
return true;
|
2014-12-04 01:27:56 -04:00
|
|
|
} else {
|
2019-07-07 11:34:41 -03:00
|
|
|
return false;
|
2014-12-04 01:27:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-15 12:10:36 -04:00
|
|
|
/// consumed_mah - returns total current drawn since start-up in milliampere.hours
|
2019-07-07 11:34:41 -03:00
|
|
|
bool AP_BattMonitor::consumed_mah(float &mah, const uint8_t instance) const {
|
|
|
|
if ((instance < _num_instances) && (drivers[instance] != nullptr) && drivers[instance]->has_current()) {
|
|
|
|
mah = state[instance].consumed_mah;
|
|
|
|
return true;
|
2014-12-04 01:27:56 -04:00
|
|
|
} else {
|
2019-07-07 11:34:41 -03:00
|
|
|
return false;
|
2013-09-28 10:35:27 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-15 12:10:36 -04:00
|
|
|
/// consumed_wh - returns energy consumed since start-up in Watt.hours
|
2019-07-07 11:34:41 -03:00
|
|
|
bool AP_BattMonitor::consumed_wh(float &wh, const uint8_t instance) const {
|
|
|
|
if (instance < _num_instances && drivers[instance] != nullptr && drivers[instance]->has_consumed_energy()) {
|
|
|
|
wh = state[instance].consumed_wh;
|
|
|
|
return true;
|
2016-08-25 09:53:33 -03:00
|
|
|
} else {
|
2019-07-07 11:34:41 -03:00
|
|
|
return false;
|
2016-08-25 09:53:33 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-28 10:35:27 -03:00
|
|
|
/// capacity_remaining_pct - returns the % battery capacity remaining (0 ~ 100)
|
2014-12-04 01:27:56 -04:00
|
|
|
uint8_t AP_BattMonitor::capacity_remaining_pct(uint8_t instance) const
|
2013-09-28 10:35:27 -03:00
|
|
|
{
|
2016-10-30 02:24:21 -03:00
|
|
|
if (instance < _num_instances && drivers[instance] != nullptr) {
|
2014-12-04 01:27:56 -04:00
|
|
|
return drivers[instance]->capacity_remaining_pct();
|
2016-06-02 19:02:20 -03:00
|
|
|
} else {
|
|
|
|
return 0;
|
2014-12-04 01:27:56 -04:00
|
|
|
}
|
2013-09-30 11:14:09 -03:00
|
|
|
}
|
|
|
|
|
2016-05-03 11:40:00 -03:00
|
|
|
/// pack_capacity_mah - returns the capacity of the battery pack in mAh when the pack is full
|
2017-10-27 02:36:49 -03:00
|
|
|
int32_t AP_BattMonitor::pack_capacity_mah(uint8_t instance) const
|
|
|
|
{
|
|
|
|
if (instance < AP_BATT_MONITOR_MAX_INSTANCES) {
|
|
|
|
return _params[instance]._pack_capacity;
|
2016-05-03 11:40:00 -03:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2017-10-27 02:36:49 -03:00
|
|
|
}
|
2017-02-08 20:28:57 -04:00
|
|
|
|
2017-11-09 18:33:44 -04:00
|
|
|
void AP_BattMonitor::check_failsafes(void)
|
|
|
|
{
|
|
|
|
if (hal.util->get_soft_armed()) {
|
|
|
|
for (uint8_t i = 0; i < _num_instances; i++) {
|
2018-09-12 18:18:06 -03:00
|
|
|
if (drivers[i] == nullptr) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const BatteryFailsafe type = drivers[i]->update_failsafes();
|
2017-11-09 18:33:44 -04:00
|
|
|
if (type <= state[i].failsafe) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int8_t action = 0;
|
|
|
|
const char *type_str = nullptr;
|
|
|
|
switch (type) {
|
|
|
|
case AP_BattMonitor::BatteryFailsafe_None:
|
|
|
|
continue; // should not have been called in this case
|
|
|
|
case AP_BattMonitor::BatteryFailsafe_Low:
|
|
|
|
action = _params[i]._failsafe_low_action;
|
|
|
|
type_str = "low";
|
|
|
|
break;
|
|
|
|
case AP_BattMonitor::BatteryFailsafe_Critical:
|
|
|
|
action = _params[i]._failsafe_critical_action;
|
|
|
|
type_str = "critical";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
gcs().send_text(MAV_SEVERITY_WARNING, "Battery %d is %s %.2fV used %.0f mAh", i + 1, type_str,
|
2019-07-07 11:34:41 -03:00
|
|
|
(double)voltage(i), (double)state[i].consumed_mah);
|
2017-11-09 18:33:44 -04:00
|
|
|
_has_triggered_failsafe = true;
|
|
|
|
AP_Notify::flags.failsafe_battery = true;
|
|
|
|
state[i].failsafe = type;
|
|
|
|
|
|
|
|
// map the desired failsafe action to a prioritiy level
|
|
|
|
int8_t priority = 0;
|
|
|
|
if (_failsafe_priorities != nullptr) {
|
|
|
|
while (_failsafe_priorities[priority] != -1) {
|
|
|
|
if (_failsafe_priorities[priority] == action) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
priority++;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// trigger failsafe if the action was equal or higher priority
|
|
|
|
// It's valid to retrigger the same action if a different battery provoked the event
|
|
|
|
if (priority <= _highest_failsafe_priority) {
|
|
|
|
_battery_failsafe_handler_fn(type_str, action);
|
|
|
|
_highest_failsafe_priority = priority;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-08 13:16:29 -04:00
|
|
|
// return true if any battery is pushing too much power
|
|
|
|
bool AP_BattMonitor::overpower_detected() const
|
|
|
|
{
|
|
|
|
bool result = false;
|
2017-05-31 00:30:48 -03:00
|
|
|
for (uint8_t instance = 0; instance < _num_instances; instance++) {
|
2016-01-08 13:16:29 -04:00
|
|
|
result |= overpower_detected(instance);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AP_BattMonitor::overpower_detected(uint8_t instance) const
|
|
|
|
{
|
2016-07-25 22:10:17 -03:00
|
|
|
#if APM_BUILD_TYPE(APM_BUILD_ArduPlane)
|
2017-10-27 02:36:49 -03:00
|
|
|
if (instance < _num_instances && _params[instance]._watt_max > 0) {
|
|
|
|
float power = state[instance].current_amps * state[instance].voltage;
|
|
|
|
return state[instance].healthy && (power > _params[instance]._watt_max);
|
2016-01-08 13:16:29 -04:00
|
|
|
}
|
|
|
|
return false;
|
2016-07-25 22:10:17 -03:00
|
|
|
#else
|
|
|
|
return false;
|
2016-04-19 21:21:48 -03:00
|
|
|
#endif
|
2016-07-25 22:10:17 -03:00
|
|
|
}
|
2017-04-08 00:27:31 -03:00
|
|
|
|
2017-05-31 00:31:18 -03:00
|
|
|
bool AP_BattMonitor::has_cell_voltages(const uint8_t instance) const
|
|
|
|
{
|
|
|
|
if (instance < _num_instances && drivers[instance] != nullptr) {
|
|
|
|
return drivers[instance]->has_cell_voltages();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-08 00:27:31 -03:00
|
|
|
// return the current cell voltages, returns the first monitor instances cells if the instance is out of range
|
|
|
|
const AP_BattMonitor::cells & AP_BattMonitor::get_cell_voltages(const uint8_t instance) const
|
|
|
|
{
|
|
|
|
if (instance >= AP_BATT_MONITOR_MAX_INSTANCES) {
|
|
|
|
return state[AP_BATT_PRIMARY_INSTANCE].cell_voltages;
|
|
|
|
} else {
|
|
|
|
return state[instance].cell_voltages;
|
|
|
|
}
|
|
|
|
}
|
2017-04-08 05:20:08 -03:00
|
|
|
|
|
|
|
// returns true if there is a temperature reading
|
|
|
|
bool AP_BattMonitor::get_temperature(float &temperature, const uint8_t instance) const
|
|
|
|
{
|
|
|
|
if (instance >= AP_BATT_MONITOR_MAX_INSTANCES) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
temperature = state[instance].temperature;
|
|
|
|
return (AP_HAL::millis() - state[instance].temperature_time) <= AP_BATT_MONITOR_TIMEOUT;
|
|
|
|
}
|
|
|
|
}
|
2018-01-16 15:09:28 -04:00
|
|
|
|
2018-09-12 19:26:39 -03:00
|
|
|
bool AP_BattMonitor::arming_checks(size_t buflen, char *buffer) const
|
|
|
|
{
|
|
|
|
char temp_buffer[MAVLINK_MSG_STATUSTEXT_FIELD_TEXT_LEN+1] {};
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < AP_BATT_MONITOR_MAX_INSTANCES; i++) {
|
|
|
|
if (drivers[i] != nullptr && !(drivers[i]->arming_checks(temp_buffer, sizeof(temp_buffer)))) {
|
|
|
|
hal.util->snprintf(buffer, buflen, "Battery %d %s", i + 1, temp_buffer);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-01-16 15:09:28 -04:00
|
|
|
|
2018-11-27 20:37:13 -04:00
|
|
|
// Check's each smart battery instance for its powering off state and broadcasts notifications
|
|
|
|
void AP_BattMonitor::checkPoweringOff(void)
|
|
|
|
{
|
|
|
|
for (uint8_t i = 0; i < _num_instances; i++) {
|
|
|
|
if (state[i].is_powering_off && !state[i].powerOffNotified) {
|
|
|
|
// Set the AP_Notify flag, which plays the power off tones
|
|
|
|
AP_Notify::flags.powering_off = true;
|
|
|
|
|
|
|
|
// Send a Mavlink broadcast announcing the shutdown
|
|
|
|
mavlink_message_t msg;
|
2019-01-28 22:03:11 -04:00
|
|
|
mavlink_command_long_t cmd_msg{};
|
2018-11-27 20:37:13 -04:00
|
|
|
cmd_msg.command = MAV_CMD_POWER_OFF_INITIATED;
|
|
|
|
cmd_msg.param1 = i+1;
|
|
|
|
mavlink_msg_command_long_encode(mavlink_system.sysid, MAV_COMP_ID_ALL, &msg, &cmd_msg);
|
2019-04-30 07:22:48 -03:00
|
|
|
GCS_MAVLINK::send_to_components(msg);
|
2018-11-27 20:37:13 -04:00
|
|
|
gcs().send_text(MAV_SEVERITY_WARNING, "Vehicle %d battery %d is powering off", mavlink_system.sysid, i+1);
|
|
|
|
|
|
|
|
// only send this once
|
|
|
|
state[i].powerOffNotified = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 06:30:18 -03:00
|
|
|
/*
|
|
|
|
reset battery remaining percentage for batteries that integrate to
|
|
|
|
calculate percentage remaining
|
|
|
|
*/
|
|
|
|
bool AP_BattMonitor::reset_remaining(uint16_t battery_mask, float percentage)
|
|
|
|
{
|
|
|
|
bool ret = true;
|
2019-06-18 21:14:46 -03:00
|
|
|
BatteryFailsafe highest_failsafe = BatteryFailsafe_None;
|
2019-06-18 06:30:18 -03:00
|
|
|
for (uint8_t i = 0; i < _num_instances; i++) {
|
|
|
|
if ((1U<<i) & battery_mask) {
|
|
|
|
ret &= drivers[i]->reset_remaining(percentage);
|
|
|
|
}
|
2019-06-18 21:14:46 -03:00
|
|
|
if (state[i].failsafe > highest_failsafe) {
|
|
|
|
highest_failsafe = state[i].failsafe;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If all backends are not in failsafe then set overall failsafe state
|
|
|
|
if (highest_failsafe == BatteryFailsafe_None) {
|
|
|
|
_highest_failsafe_priority = INT8_MAX;
|
|
|
|
_has_triggered_failsafe = false;
|
|
|
|
// and reset notify flag
|
|
|
|
AP_Notify::flags.failsafe_battery = false;
|
2019-06-18 06:30:18 -03:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-01-16 15:09:28 -04:00
|
|
|
namespace AP {
|
|
|
|
|
|
|
|
AP_BattMonitor &battery()
|
|
|
|
{
|
2019-02-10 14:56:13 -04:00
|
|
|
return *AP_BattMonitor::get_singleton();
|
2018-01-16 15:09:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|