2013-09-28 10:35:27 -03:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
#ifndef AP_BATTMONITOR_H
|
|
|
|
#define AP_BATTMONITOR_H
|
|
|
|
|
|
|
|
#include <AP_Common.h>
|
|
|
|
#include <AP_Param.h>
|
2014-12-04 01:27:56 -04:00
|
|
|
#include <AP_Math.h>
|
2013-09-28 10:35:27 -03:00
|
|
|
|
2014-12-04 01:27:56 -04:00
|
|
|
// maximum number of battery monitors
|
|
|
|
#define AP_BATT_MONITOR_MAX_INSTANCES 2
|
|
|
|
|
|
|
|
// first monitor is always the primary monitor
|
|
|
|
#define AP_BATT_PRIMARY_INSTANCE 0
|
|
|
|
|
2013-09-30 09:56:49 -03:00
|
|
|
#define AP_BATT_CAPACITY_DEFAULT 3300
|
2013-09-30 11:14:09 -03:00
|
|
|
#define AP_BATT_LOW_VOLT_TIMEOUT_MS 10000 // low voltage of 10 seconds will cause battery_exhausted to return true
|
2013-09-28 10:35:27 -03:00
|
|
|
|
2014-12-04 01:27:56 -04:00
|
|
|
// declare backend class
|
|
|
|
class AP_BattMonitor_Backend;
|
|
|
|
class AP_BattMonitor_Analog;
|
|
|
|
class AP_BattMonitor_SMBus;
|
|
|
|
class AP_BattMonitor_SMBus_I2C;
|
|
|
|
class AP_BattMonitor_SMBus_PX4;
|
|
|
|
|
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;
|
|
|
|
friend class AP_BattMonitor_SMBus_I2C;
|
|
|
|
friend class AP_BattMonitor_SMBus_PX4;
|
|
|
|
|
2013-09-28 10:35:27 -03:00
|
|
|
public:
|
|
|
|
|
|
|
|
/// Constructor
|
|
|
|
AP_BattMonitor();
|
|
|
|
|
2014-12-04 01:27:56 -04:00
|
|
|
// Battery monitor driver types
|
|
|
|
enum BattMonitor_Type {
|
|
|
|
BattMonitor_TYPE_NONE = 0,
|
|
|
|
BattMonitor_TYPE_ANALOG_VOLTAGE_ONLY = 3,
|
|
|
|
BattMonitor_TYPE_ANALOG_VOLTAGE_AND_CURRENT = 4,
|
2014-12-15 09:49:28 -04:00
|
|
|
BattMonitor_TYPE_SMBUS = 5
|
2014-12-04 01:27:56 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// The BattMonitor_State structure is filled in by the backend driver
|
|
|
|
struct BattMonitor_State {
|
|
|
|
uint8_t instance; // the instance number of this monitor
|
|
|
|
bool healthy; // battery monitor is communicating correctly
|
|
|
|
float voltage; // voltage in volts
|
|
|
|
float current_amps; // current in amperes
|
|
|
|
float current_total_mah; // total current draw since start-up
|
|
|
|
uint32_t last_time_micros; // time when voltage and current was last read
|
|
|
|
uint32_t low_voltage_start_ms; // time when voltage dropped below the minimum
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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
|
|
|
#define _BattMonitor_STATE(instance) state[instance]
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2014-12-04 01:27:56 -04:00
|
|
|
/// has_current - returns true if battery monitor instance provides current info
|
|
|
|
bool has_current(uint8_t instance) const;
|
|
|
|
bool has_current() const { return has_current(AP_BATT_PRIMARY_INSTANCE); }
|
2013-10-22 19:40:09 -03:00
|
|
|
|
2014-12-04 01:27:56 -04:00
|
|
|
/// voltage - returns battery voltage in millivolts
|
|
|
|
float voltage(uint8_t instance) const;
|
|
|
|
float voltage() const { return voltage(AP_BATT_PRIMARY_INSTANCE); }
|
2013-09-28 10:35:27 -03:00
|
|
|
|
2014-12-04 01:27:56 -04:00
|
|
|
// voltage2 - returns the voltage of the second battery (helper function to send 2nd voltage to GCS)
|
|
|
|
float voltage2() const;
|
2014-08-08 23:13:49 -03:00
|
|
|
|
2014-12-04 01:27:56 -04:00
|
|
|
/// current_amps - returns the instantaneous current draw in amperes
|
|
|
|
float current_amps(uint8_t instance) const;
|
|
|
|
float current_amps() const { return current_amps(AP_BATT_PRIMARY_INSTANCE); }
|
2013-09-28 10:35:27 -03:00
|
|
|
|
2014-12-04 01:27:56 -04:00
|
|
|
/// current_total_mah - returns total current drawn since start-up in amp-hours
|
|
|
|
float current_total_mah(uint8_t instance) const;
|
|
|
|
float current_total_mah() const { return current_total_mah(AP_BATT_PRIMARY_INSTANCE); }
|
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
|
|
|
virtual uint8_t capacity_remaining_pct(uint8_t instance) const;
|
|
|
|
uint8_t capacity_remaining_pct() const { return capacity_remaining_pct(AP_BATT_PRIMARY_INSTANCE); }
|
|
|
|
|
|
|
|
/// exhausted - returns true if the battery's voltage remains below the low_voltage for 10 seconds or remaining capacity falls below min_capacity
|
|
|
|
bool exhausted(uint8_t instance, float low_voltage, float min_capacity_mah);
|
|
|
|
bool exhausted(float low_voltage, float min_capacity_mah) { return exhausted(AP_BATT_PRIMARY_INSTANCE, low_voltage, min_capacity_mah); }
|
2013-09-28 10:35:27 -03:00
|
|
|
|
2014-12-04 01:27:56 -04:00
|
|
|
/// monitoring - sets the monitor type (used for example sketch only)
|
|
|
|
void set_monitoring(uint8_t instance, uint8_t mon) { _monitoring[instance].set(mon); }
|
2013-09-30 11:14:09 -03:00
|
|
|
|
2013-09-28 10:35:27 -03:00
|
|
|
static const struct AP_Param::GroupInfo var_info[];
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
/// parameters
|
2014-12-04 01:27:56 -04:00
|
|
|
AP_Int8 _monitoring[AP_BATT_MONITOR_MAX_INSTANCES]; /// 0=disabled, 3=voltage only, 4=voltage and current
|
|
|
|
AP_Int8 _volt_pin[AP_BATT_MONITOR_MAX_INSTANCES]; /// board pin used to measure battery voltage
|
|
|
|
AP_Int8 _curr_pin[AP_BATT_MONITOR_MAX_INSTANCES]; /// board pin used to measure battery current
|
|
|
|
AP_Float _volt_multiplier[AP_BATT_MONITOR_MAX_INSTANCES]; /// voltage on volt pin multiplied by this to calculate battery voltage
|
|
|
|
AP_Float _curr_amp_per_volt[AP_BATT_MONITOR_MAX_INSTANCES]; /// voltage on current pin multiplied by this to calculate current in amps
|
|
|
|
AP_Float _curr_amp_offset[AP_BATT_MONITOR_MAX_INSTANCES]; /// offset voltage that is subtracted from current pin before conversion to amps
|
|
|
|
AP_Int32 _pack_capacity[AP_BATT_MONITOR_MAX_INSTANCES]; /// battery pack capacity less reserve in mAh
|
|
|
|
|
|
|
|
private:
|
|
|
|
BattMonitor_State state[AP_BATT_MONITOR_MAX_INSTANCES];
|
|
|
|
AP_BattMonitor_Backend *drivers[AP_BATT_MONITOR_MAX_INSTANCES];
|
|
|
|
uint8_t _num_instances; /// number of monitors
|
2013-09-28 10:35:27 -03:00
|
|
|
};
|
|
|
|
#endif // AP_BATTMONITOR_H
|