2016-02-17 21:25:15 -04:00
|
|
|
#pragma once
|
2013-09-28 10:35:27 -03:00
|
|
|
|
2015-08-11 03:28:42 -03:00
|
|
|
#include <AP_Common/AP_Common.h>
|
|
|
|
#include <AP_Param/AP_Param.h>
|
|
|
|
#include <AP_Math/AP_Math.h>
|
2017-04-08 00:27:31 -03:00
|
|
|
#include <GCS_MAVLink/GCS_MAVLink.h>
|
2017-10-27 02:36:49 -03:00
|
|
|
#include "AP_BattMonitor_Params.h"
|
2013-09-28 10:35:27 -03:00
|
|
|
|
2014-12-04 01:27:56 -04:00
|
|
|
// maximum number of battery monitors
|
2021-08-30 09:23:55 -03:00
|
|
|
#ifndef AP_BATT_MONITOR_MAX_INSTANCES
|
2018-09-12 20:01:04 -03:00
|
|
|
#define AP_BATT_MONITOR_MAX_INSTANCES 9
|
2021-08-30 09:23:55 -03:00
|
|
|
#endif
|
2014-12-04 01:27:56 -04:00
|
|
|
|
|
|
|
// first monitor is always the primary monitor
|
|
|
|
#define AP_BATT_PRIMARY_INSTANCE 0
|
|
|
|
|
2017-04-24 03:16:49 -03:00
|
|
|
#define AP_BATT_SERIAL_NUMBER_DEFAULT -1
|
2013-09-28 10:35:27 -03:00
|
|
|
|
2017-04-08 05:20:08 -03:00
|
|
|
#define AP_BATT_MONITOR_TIMEOUT 5000
|
|
|
|
|
2017-05-23 04:12:58 -03:00
|
|
|
#define AP_BATT_MONITOR_RES_EST_TC_1 0.5f
|
|
|
|
#define AP_BATT_MONITOR_RES_EST_TC_2 0.1f
|
|
|
|
|
2020-09-23 05:15:37 -03:00
|
|
|
#if !HAL_MINIMIZE_FEATURES && BOARD_FLASH_SIZE > 1024
|
|
|
|
#define AP_BATT_MONITOR_CELLS_MAX 14
|
|
|
|
#else
|
2020-06-02 22:59:54 -03:00
|
|
|
#define AP_BATT_MONITOR_CELLS_MAX 12
|
2020-09-23 05:15:37 -03:00
|
|
|
#endif
|
2020-06-02 22:59:54 -03:00
|
|
|
|
2019-12-09 00:22:40 -04:00
|
|
|
#ifndef HAL_BATTMON_SMBUS_ENABLE
|
|
|
|
#define HAL_BATTMON_SMBUS_ENABLE 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAL_BATTMON_FUEL_ENABLE
|
|
|
|
#define HAL_BATTMON_FUEL_ENABLE 1
|
|
|
|
#endif
|
|
|
|
|
2014-12-04 01:27:56 -04:00
|
|
|
// declare backend class
|
|
|
|
class AP_BattMonitor_Backend;
|
|
|
|
class AP_BattMonitor_Analog;
|
|
|
|
class AP_BattMonitor_SMBus;
|
2017-02-08 20:28:14 -04:00
|
|
|
class AP_BattMonitor_SMBus_Solo;
|
2020-06-02 04:21:10 -03:00
|
|
|
class AP_BattMonitor_SMBus_Generic;
|
2020-06-02 04:44:38 -03:00
|
|
|
class AP_BattMonitor_SMBus_Maxell;
|
2019-09-16 12:44:31 -03:00
|
|
|
class AP_BattMonitor_SMBus_Rotoye;
|
2018-02-16 18:32:25 -04:00
|
|
|
class AP_BattMonitor_UAVCAN;
|
2019-11-28 19:04:33 -04:00
|
|
|
class AP_BattMonitor_Generator;
|
2021-10-24 21:58:19 -03:00
|
|
|
class AP_BattMonitor_INA2XX;
|
2021-10-10 05:56:41 -03:00
|
|
|
class AP_BattMonitor_LTC2946;
|
2021-10-12 05:15:52 -03:00
|
|
|
class AP_BattMonitor_Torqeedo;
|
2014-12-04 01:27:56 -04:00
|
|
|
|
2013-09-28 10:35:27 -03:00
|
|
|
class AP_BattMonitor
|
|
|
|
{
|
2014-12-04 01:27:56 -04:00
|
|
|
friend class AP_BattMonitor_Backend;
|
|
|
|
friend class AP_BattMonitor_Analog;
|
|
|
|
friend class AP_BattMonitor_SMBus;
|
2017-02-08 20:28:14 -04:00
|
|
|
friend class AP_BattMonitor_SMBus_Solo;
|
2020-06-02 04:21:10 -03:00
|
|
|
friend class AP_BattMonitor_SMBus_Generic;
|
2020-06-02 04:44:38 -03:00
|
|
|
friend class AP_BattMonitor_SMBus_Maxell;
|
2019-09-16 12:44:31 -03:00
|
|
|
friend class AP_BattMonitor_SMBus_Rotoye;
|
2018-02-16 18:32:25 -04:00
|
|
|
friend class AP_BattMonitor_UAVCAN;
|
2018-11-19 04:59:58 -04:00
|
|
|
friend class AP_BattMonitor_Sum;
|
2019-03-12 05:02:29 -03:00
|
|
|
friend class AP_BattMonitor_FuelFlow;
|
2019-06-18 18:20:55 -03:00
|
|
|
friend class AP_BattMonitor_FuelLevel_PWM;
|
2019-11-28 19:04:33 -04:00
|
|
|
friend class AP_BattMonitor_Generator;
|
2021-10-24 21:58:19 -03:00
|
|
|
friend class AP_BattMonitor_INA2XX;
|
2021-10-10 05:56:41 -03:00
|
|
|
friend class AP_BattMonitor_LTC2946;
|
2014-12-04 01:27:56 -04:00
|
|
|
|
2021-10-12 05:15:52 -03:00
|
|
|
friend class AP_BattMonitor_Torqeedo;
|
|
|
|
|
2013-09-28 10:35:27 -03:00
|
|
|
public:
|
2017-11-09 18:33:44 -04:00
|
|
|
|
|
|
|
// battery failsafes must be defined in levels of severity so that vehicles wont fall backwards
|
2020-10-22 22:33:03 -03:00
|
|
|
enum class Failsafe : uint8_t {
|
|
|
|
None = 0,
|
|
|
|
Low,
|
|
|
|
Critical
|
2017-11-09 18:33:44 -04:00
|
|
|
};
|
|
|
|
|
2020-08-06 02:33:07 -03:00
|
|
|
// Battery monitor driver types
|
|
|
|
enum class Type {
|
|
|
|
NONE = 0,
|
|
|
|
ANALOG_VOLTAGE_ONLY = 3,
|
|
|
|
ANALOG_VOLTAGE_AND_CURRENT = 4,
|
|
|
|
SOLO = 5,
|
|
|
|
BEBOP = 6,
|
|
|
|
SMBus_Generic = 7,
|
|
|
|
UAVCAN_BatteryInfo = 8,
|
|
|
|
BLHeliESC = 9,
|
|
|
|
Sum = 10,
|
|
|
|
FuelFlow = 11,
|
|
|
|
FuelLevel_PWM = 12,
|
|
|
|
SUI3 = 13,
|
|
|
|
SUI6 = 14,
|
|
|
|
NeoDesign = 15,
|
|
|
|
MAXELL = 16,
|
2020-10-22 14:11:47 -03:00
|
|
|
GENERATOR_ELEC = 17,
|
|
|
|
GENERATOR_FUEL = 18,
|
2019-09-16 12:44:31 -03:00
|
|
|
Rotoye = 19,
|
2021-12-17 02:29:32 -04:00
|
|
|
// 20 was MPPT_PacketDigital
|
2021-10-24 21:58:19 -03:00
|
|
|
INA2XX = 21,
|
2021-10-10 05:56:41 -03:00
|
|
|
LTC2946 = 22,
|
2021-10-12 05:15:52 -03:00
|
|
|
Torqeedo = 23,
|
2020-08-06 02:33:07 -03:00
|
|
|
};
|
|
|
|
|
2017-11-09 18:33:44 -04:00
|
|
|
FUNCTOR_TYPEDEF(battery_failsafe_handler_fn_t, void, const char *, const int8_t);
|
|
|
|
|
|
|
|
AP_BattMonitor(uint32_t log_battery_bit, battery_failsafe_handler_fn_t battery_failsafe_handler_fn, const int8_t *failsafe_priorities);
|
2017-08-28 21:41:49 -03:00
|
|
|
|
|
|
|
/* Do not allow copies */
|
|
|
|
AP_BattMonitor(const AP_BattMonitor &other) = delete;
|
|
|
|
AP_BattMonitor &operator=(const AP_BattMonitor&) = delete;
|
2013-09-28 10:35:27 -03:00
|
|
|
|
2019-02-10 14:56:13 -04:00
|
|
|
static AP_BattMonitor *get_singleton() {
|
|
|
|
return _singleton;
|
2018-01-16 15:09:28 -04:00
|
|
|
}
|
|
|
|
|
2020-06-11 21:19:08 -03:00
|
|
|
// cell voltages in millivolts
|
2017-04-08 00:27:31 -03:00
|
|
|
struct cells {
|
2020-06-02 22:59:54 -03:00
|
|
|
uint16_t cells[AP_BATT_MONITOR_CELLS_MAX];
|
2017-04-08 00:27:31 -03:00
|
|
|
};
|
|
|
|
|
2014-12-04 01:27:56 -04:00
|
|
|
// The BattMonitor_State structure is filled in by the backend driver
|
|
|
|
struct BattMonitor_State {
|
2017-11-09 18:33:44 -04:00
|
|
|
cells cell_voltages; // battery cell voltages in millivolts, 10 cells matches the MAVLink spec
|
|
|
|
float voltage; // voltage in volts
|
|
|
|
float current_amps; // current in amperes
|
|
|
|
float consumed_mah; // total current draw in milliamp hours since start-up
|
|
|
|
float consumed_wh; // total energy consumed in Wh since start-up
|
2018-02-15 11:51:08 -04:00
|
|
|
uint32_t last_time_micros; // time when voltage and current was last read in microseconds
|
|
|
|
uint32_t low_voltage_start_ms; // time when voltage dropped below the minimum in milliseconds
|
2017-11-09 18:33:44 -04:00
|
|
|
uint32_t critical_voltage_start_ms; // critical voltage failsafe start timer in milliseconds
|
2018-02-15 11:51:08 -04:00
|
|
|
float temperature; // battery temperature in degrees Celsius
|
2017-11-09 18:33:44 -04:00
|
|
|
uint32_t temperature_time; // timestamp of the last received temperature message
|
2018-02-15 11:51:08 -04:00
|
|
|
float voltage_resting_estimate; // voltage with sag removed based on current and resistance estimate in Volt
|
2017-11-09 18:33:44 -04:00
|
|
|
float resistance; // resistance, in Ohms, calculated by comparing resting voltage vs in flight voltage
|
2020-10-22 22:33:03 -03:00
|
|
|
Failsafe failsafe; // stage failsafe the battery is in
|
2017-11-09 18:33:44 -04:00
|
|
|
bool healthy; // battery monitor is communicating correctly
|
2018-11-27 20:37:13 -04:00
|
|
|
bool is_powering_off; // true when power button commands power off
|
|
|
|
bool powerOffNotified; // only send powering off notification once
|
2021-10-11 04:25:04 -03:00
|
|
|
uint32_t time_remaining; // remaining battery time
|
|
|
|
bool has_time_remaining; // time_remaining is only valid if this is true
|
2021-06-18 18:16:06 -03:00
|
|
|
const struct AP_Param::GroupInfo *var_info;
|
2014-12-04 01:27:56 -04:00
|
|
|
};
|
|
|
|
|
2021-11-01 13:23:28 -03:00
|
|
|
static const struct AP_Param::GroupInfo *backend_var_info[AP_BATT_MONITOR_MAX_INSTANCES];
|
2021-06-18 18:16:06 -03:00
|
|
|
|
2014-12-04 01:27:56 -04:00
|
|
|
// Return the number of battery monitor instances
|
|
|
|
uint8_t num_instances(void) const { return _num_instances; }
|
|
|
|
|
|
|
|
// detect and initialise any available battery monitors
|
2013-09-28 10:35:27 -03:00
|
|
|
void init();
|
|
|
|
|
2014-12-04 01:27:56 -04:00
|
|
|
/// Read the battery voltage and current for all batteries. Should be called at 10hz
|
2013-09-28 10:35:27 -03:00
|
|
|
void read();
|
|
|
|
|
2014-12-04 01:27:56 -04:00
|
|
|
// healthy - returns true if monitor is functioning
|
|
|
|
bool healthy(uint8_t instance) const;
|
|
|
|
bool healthy() const { return healthy(AP_BATT_PRIMARY_INSTANCE); }
|
2013-09-28 10:35:27 -03:00
|
|
|
|
2020-06-11 21:19:08 -03:00
|
|
|
/// voltage - returns battery voltage in volts
|
2014-12-04 01:27:56 -04:00
|
|
|
float voltage(uint8_t instance) const;
|
|
|
|
float voltage() const { return voltage(AP_BATT_PRIMARY_INSTANCE); }
|
2013-09-28 10:35:27 -03:00
|
|
|
|
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 voltage_resting_estimate(uint8_t instance) const;
|
|
|
|
float voltage_resting_estimate() const { return voltage_resting_estimate(AP_BATT_PRIMARY_INSTANCE); }
|
|
|
|
|
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 current_amps(float ¤t, const uint8_t instance = AP_BATT_PRIMARY_INSTANCE) const WARN_IF_UNUSED;
|
2013-09-28 10:35:27 -03: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 consumed_mah(float &mah, const uint8_t instance = AP_BATT_PRIMARY_INSTANCE) const WARN_IF_UNUSED;
|
2013-09-28 10:35:27 -03:00
|
|
|
|
2018-02-15 12:10:36 -04:00
|
|
|
/// consumed_wh - returns total energy drawn since start-up in watt.hours
|
2019-07-07 11:34:41 -03:00
|
|
|
bool consumed_wh(float&wh, const uint8_t instance = AP_BATT_PRIMARY_INSTANCE) const WARN_IF_UNUSED;
|
2016-08-25 09:53:33 -03:00
|
|
|
|
2020-04-23 13:15:34 -03:00
|
|
|
/// capacity_remaining_pct - returns true if the percentage is valid and writes to percentage argument
|
|
|
|
virtual bool capacity_remaining_pct(uint8_t &percentage, uint8_t instance) const WARN_IF_UNUSED;
|
|
|
|
bool capacity_remaining_pct(uint8_t &percentage) const WARN_IF_UNUSED { return capacity_remaining_pct(percentage, AP_BATT_PRIMARY_INSTANCE); }
|
2014-12-04 01:27:56 -04:00
|
|
|
|
2021-10-11 04:25:04 -03:00
|
|
|
/// time_remaining - returns remaining battery time
|
|
|
|
bool time_remaining(uint32_t &seconds, const uint8_t instance = AP_BATT_PRIMARY_INSTANCE) const WARN_IF_UNUSED;
|
|
|
|
|
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
|
|
|
|
int32_t pack_capacity_mah(uint8_t instance) const;
|
|
|
|
int32_t pack_capacity_mah() const { return pack_capacity_mah(AP_BATT_PRIMARY_INSTANCE); }
|
|
|
|
|
2017-11-09 18:33:44 -04:00
|
|
|
/// returns true if a battery failsafe has ever been triggered
|
|
|
|
bool has_failsafed(void) const { return _has_triggered_failsafe; };
|
|
|
|
|
|
|
|
/// returns the highest failsafe action that has been triggered
|
|
|
|
int8_t get_highest_failsafe_priority(void) const { return _highest_failsafe_priority; };
|
2013-09-28 10:35:27 -03:00
|
|
|
|
2015-01-16 03:11:55 -04:00
|
|
|
/// get_type - returns battery monitor type
|
2020-08-06 02:33:07 -03:00
|
|
|
enum Type get_type() const { return get_type(AP_BATT_PRIMARY_INSTANCE); }
|
|
|
|
enum Type get_type(uint8_t instance) const {
|
|
|
|
return (Type)_params[instance]._type.get();
|
|
|
|
}
|
2015-01-16 03:11:55 -04:00
|
|
|
|
2020-11-21 01:28:12 -04:00
|
|
|
/// get_serial_number - returns battery serial number
|
|
|
|
int32_t get_serial_number() const { return get_serial_number(AP_BATT_PRIMARY_INSTANCE); }
|
|
|
|
int32_t get_serial_number(uint8_t instance) const {
|
|
|
|
return _params[instance]._serial_number;
|
|
|
|
}
|
|
|
|
|
2016-01-08 13:16:29 -04:00
|
|
|
/// true when (voltage * current) > watt_max
|
|
|
|
bool overpower_detected() const;
|
|
|
|
bool overpower_detected(uint8_t instance) const;
|
|
|
|
|
2020-06-11 21:19:08 -03:00
|
|
|
// cell voltages in millivolts
|
2021-02-01 12:26:26 -04:00
|
|
|
bool has_cell_voltages() const { return has_cell_voltages(AP_BATT_PRIMARY_INSTANCE); }
|
2017-05-31 00:31:18 -03:00
|
|
|
bool has_cell_voltages(const uint8_t instance) const;
|
2020-06-11 21:19:08 -03:00
|
|
|
const cells &get_cell_voltages() const { return get_cell_voltages(AP_BATT_PRIMARY_INSTANCE); }
|
|
|
|
const cells &get_cell_voltages(const uint8_t instance) const;
|
2017-04-08 00:27:31 -03:00
|
|
|
|
2017-04-08 05:20:08 -03:00
|
|
|
// temperature
|
2019-12-10 20:55:20 -04:00
|
|
|
bool get_temperature(float &temperature) const { return get_temperature(temperature, AP_BATT_PRIMARY_INSTANCE); }
|
2017-04-08 05:20:08 -03:00
|
|
|
bool get_temperature(float &temperature, const uint8_t instance) const;
|
|
|
|
|
2019-12-10 20:55:20 -04:00
|
|
|
// cycle count
|
|
|
|
bool get_cycle_count(uint8_t instance, uint16_t &cycles) const;
|
|
|
|
|
2017-05-23 04:12:58 -03:00
|
|
|
// get battery resistance estimate in ohms
|
|
|
|
float get_resistance() const { return get_resistance(AP_BATT_PRIMARY_INSTANCE); }
|
|
|
|
float get_resistance(uint8_t instance) const { return state[instance].resistance; }
|
|
|
|
|
2018-09-12 19:26:39 -03:00
|
|
|
// returns false if we fail arming checks, in which case the buffer will be populated with a failure message
|
|
|
|
bool arming_checks(size_t buflen, char *buffer) const;
|
|
|
|
|
2018-11-27 20:37:13 -04:00
|
|
|
// sends powering off mavlink broadcasts and sets notify flag
|
|
|
|
void checkPoweringOff(void);
|
|
|
|
|
2019-06-18 06:30:18 -03:00
|
|
|
// reset battery remaining percentage
|
2021-03-26 17:14:21 -03:00
|
|
|
bool reset_remaining_mask(uint16_t battery_mask, float percentage);
|
|
|
|
bool reset_remaining(uint8_t instance, float percentage) { return reset_remaining_mask(1U<<instance, percentage);}
|
2019-06-18 06:30:18 -03:00
|
|
|
|
2021-01-02 15:50:26 -04:00
|
|
|
// Returns mavlink charge state
|
|
|
|
MAV_BATTERY_CHARGE_STATE get_mavlink_charge_state(const uint8_t instance) const;
|
|
|
|
|
2021-12-06 09:32:49 -04:00
|
|
|
// Returns mavlink fault state
|
|
|
|
uint32_t get_mavlink_fault_bitmask(const uint8_t instance) const;
|
|
|
|
|
2013-09-28 10:35:27 -03:00
|
|
|
static const struct AP_Param::GroupInfo var_info[];
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
/// parameters
|
2017-10-27 02:36:49 -03:00
|
|
|
AP_BattMonitor_Params _params[AP_BATT_MONITOR_MAX_INSTANCES];
|
2017-04-27 20:24:20 -03:00
|
|
|
|
2014-12-04 01:27:56 -04:00
|
|
|
private:
|
2018-01-16 15:09:28 -04:00
|
|
|
static AP_BattMonitor *_singleton;
|
|
|
|
|
2014-12-04 01:27:56 -04:00
|
|
|
BattMonitor_State state[AP_BATT_MONITOR_MAX_INSTANCES];
|
|
|
|
AP_BattMonitor_Backend *drivers[AP_BATT_MONITOR_MAX_INSTANCES];
|
2018-01-16 15:09:28 -04:00
|
|
|
uint32_t _log_battery_bit;
|
2014-12-04 01:27:56 -04:00
|
|
|
uint8_t _num_instances; /// number of monitors
|
2017-10-27 02:36:49 -03:00
|
|
|
|
2021-06-18 18:16:06 -03:00
|
|
|
void convert_dynamic_param_groups(uint8_t instance);
|
2017-10-27 02:36:49 -03:00
|
|
|
|
2019-04-20 18:53:39 -03:00
|
|
|
/// returns the failsafe state of the battery
|
2020-10-22 22:33:03 -03:00
|
|
|
Failsafe check_failsafe(const uint8_t instance);
|
2019-04-20 18:53:39 -03:00
|
|
|
void check_failsafes(void); // checks all batteries failsafes
|
|
|
|
|
2017-11-09 18:33:44 -04:00
|
|
|
battery_failsafe_handler_fn_t _battery_failsafe_handler_fn;
|
|
|
|
const int8_t *_failsafe_priorities; // array of failsafe priorities, sorted highest to lowest priority, -1 indicates no more entries
|
|
|
|
|
|
|
|
int8_t _highest_failsafe_priority; // highest selected failsafe action level (used to restrict what actions we move into)
|
|
|
|
bool _has_triggered_failsafe; // true after a battery failsafe has been triggered for the first time
|
|
|
|
|
2013-09-28 10:35:27 -03:00
|
|
|
};
|
2018-01-16 15:09:28 -04:00
|
|
|
|
|
|
|
namespace AP {
|
|
|
|
AP_BattMonitor &battery();
|
|
|
|
};
|