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 <inttypes.h>
|
|
|
|
#include <AP_Common.h>
|
|
|
|
#include <AP_Param.h>
|
|
|
|
#include <AP_Math.h> // ArduPilot Mega Vector/Matrix math Library
|
|
|
|
#include <AP_ADC.h> // ArduPilot Mega Analog to Digital Converter Library
|
|
|
|
#include <AP_ADC_AnalogSource.h>
|
|
|
|
|
|
|
|
// battery monitor types
|
|
|
|
#define AP_BATT_MONITOR_DISABLED 0
|
|
|
|
#define AP_BATT_MONITOR_VOLTAGE_ONLY 3
|
|
|
|
#define AP_BATT_MONITOR_VOLTAGE_AND_CURRENT 4
|
|
|
|
|
|
|
|
// setup default mag orientation for each board type
|
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_APM1
|
|
|
|
# define AP_BATT_VOLT_PIN 0 // Battery voltage on A0
|
|
|
|
# define AP_BATT_CURR_PIN 1 // Battery current on A1
|
|
|
|
# define AP_BATT_VOLTDIVIDER_DEFAULT 3.56 // on-board APM1 voltage divider with a 3.9kOhm resistor
|
|
|
|
# define AP_BATT_CURR_AMP_PERVOLT_DEFAULT 0
|
|
|
|
#elif CONFIG_HAL_BOARD == HAL_BOARD_APM2
|
|
|
|
# define AP_BATT_VOLT_PIN 13 // APM2.5/2.6 with 3dr power module
|
|
|
|
# define AP_BATT_CURR_PIN 12
|
2013-09-30 02:35:19 -03:00
|
|
|
# define AP_BATT_VOLTDIVIDER_DEFAULT 10.1
|
2013-10-09 23:14:01 -03:00
|
|
|
# define AP_BATT_CURR_AMP_PERVOLT_DEFAULT 17.0
|
2013-09-28 10:35:27 -03:00
|
|
|
#elif CONFIG_HAL_BOARD == HAL_BOARD_FLYMAPLE
|
|
|
|
// Flymaple board pin 20 is connected to the external battery supply
|
|
|
|
// via a 24k/5.1k voltage divider. The schematic claims the divider is 25k/5k,
|
|
|
|
// but the actual installed resistors are not so.
|
|
|
|
// So the divider ratio is 5.70588 = (24000+5100)/5100
|
|
|
|
# define AP_BATT_VOLT_PIN 20
|
|
|
|
# define AP_BATT_CURR_PIN 19
|
|
|
|
# define AP_BATT_VOLTDIVIDER_DEFAULT 5.70588
|
2013-10-09 23:14:01 -03:00
|
|
|
# define AP_BATT_CURR_AMP_PERVOLT_DEFAULT 17.0
|
2013-09-30 09:56:49 -03:00
|
|
|
#elif CONFIG_HAL_BOARD == HAL_BOARD_PX4 && defined(CONFIG_ARCH_BOARD_PX4FMU_V1)
|
|
|
|
// px4
|
2013-10-07 22:28:17 -03:00
|
|
|
# define AP_BATT_VOLT_PIN 100
|
|
|
|
# define AP_BATT_CURR_PIN 101
|
2013-09-30 09:56:49 -03:00
|
|
|
# define AP_BATT_VOLTDIVIDER_DEFAULT 1.1
|
2013-10-09 23:14:01 -03:00
|
|
|
# define AP_BATT_CURR_AMP_PERVOLT_DEFAULT 17.0
|
2013-09-30 09:56:49 -03:00
|
|
|
#elif CONFIG_HAL_BOARD == HAL_BOARD_PX4 && defined(CONFIG_ARCH_BOARD_PX4FMU_V2)
|
|
|
|
// pixhawk
|
2013-09-28 10:35:27 -03:00
|
|
|
# define AP_BATT_VOLT_PIN 2
|
|
|
|
# define AP_BATT_CURR_PIN 3
|
2013-09-30 02:35:19 -03:00
|
|
|
# define AP_BATT_VOLTDIVIDER_DEFAULT 10.1
|
2013-10-09 23:14:01 -03:00
|
|
|
# define AP_BATT_CURR_AMP_PERVOLT_DEFAULT 17.0
|
2013-09-28 10:35:27 -03:00
|
|
|
#elif CONFIG_HAL_BOARD == HAL_BOARD_AVR_SITL
|
2013-09-30 02:35:19 -03:00
|
|
|
# define AP_BATT_VOLT_PIN 13
|
|
|
|
# define AP_BATT_CURR_PIN 12
|
|
|
|
# define AP_BATT_VOLTDIVIDER_DEFAULT 10.1
|
2013-10-09 23:14:01 -03:00
|
|
|
# define AP_BATT_CURR_AMP_PERVOLT_DEFAULT 17.0
|
2014-05-30 17:58:34 -03:00
|
|
|
#elif CONFIG_HAL_BOARD == HAL_BOARD_VRBRAIN && defined(CONFIG_ARCH_BOARD_VRBRAIN_V40)
|
2014-07-01 07:23:25 -03:00
|
|
|
# define AP_BATT_VOLT_PIN 10
|
2014-03-31 14:52:18 -03:00
|
|
|
# define AP_BATT_CURR_PIN -1
|
|
|
|
# define AP_BATT_VOLTDIVIDER_DEFAULT 1.1
|
|
|
|
# define AP_BATT_CURR_AMP_PERVOLT_DEFAULT 17.0
|
2014-05-30 17:58:34 -03:00
|
|
|
#elif CONFIG_HAL_BOARD == HAL_BOARD_VRBRAIN && defined(CONFIG_ARCH_BOARD_VRBRAIN_V45)
|
2014-07-01 07:23:25 -03:00
|
|
|
# define AP_BATT_VOLT_PIN 10
|
|
|
|
# define AP_BATT_CURR_PIN 11
|
2014-05-30 17:58:34 -03:00
|
|
|
# define AP_BATT_VOLTDIVIDER_DEFAULT 1.1
|
|
|
|
# define AP_BATT_CURR_AMP_PERVOLT_DEFAULT 17.0
|
|
|
|
#elif CONFIG_HAL_BOARD == HAL_BOARD_VRBRAIN && defined(CONFIG_ARCH_BOARD_VRBRAIN_V50)
|
2014-07-01 07:23:25 -03:00
|
|
|
# define AP_BATT_VOLT_PIN 10
|
|
|
|
# define AP_BATT_CURR_PIN 11
|
2014-05-30 17:58:34 -03:00
|
|
|
# define AP_BATT_VOLTDIVIDER_DEFAULT 1.1
|
|
|
|
# define AP_BATT_CURR_AMP_PERVOLT_DEFAULT 17.0
|
|
|
|
#elif CONFIG_HAL_BOARD == HAL_BOARD_VRBRAIN && defined(CONFIG_ARCH_BOARD_VRBRAIN_V51)
|
2014-07-01 07:23:25 -03:00
|
|
|
# define AP_BATT_VOLT_PIN 10
|
|
|
|
# define AP_BATT_CURR_PIN 11
|
2014-03-31 14:52:18 -03:00
|
|
|
# define AP_BATT_VOLTDIVIDER_DEFAULT 1.1
|
|
|
|
# define AP_BATT_CURR_AMP_PERVOLT_DEFAULT 17.0
|
2014-05-30 17:58:34 -03:00
|
|
|
#elif CONFIG_HAL_BOARD == HAL_BOARD_VRBRAIN && defined(CONFIG_ARCH_BOARD_VRUBRAIN_V51)
|
2014-07-01 07:23:25 -03:00
|
|
|
# define AP_BATT_VOLT_PIN 10
|
2014-05-30 17:58:34 -03:00
|
|
|
# define AP_BATT_CURR_PIN -1
|
|
|
|
# define AP_BATT_VOLTDIVIDER_DEFAULT 1.1
|
|
|
|
# define AP_BATT_CURR_AMP_PERVOLT_DEFAULT 17.0
|
|
|
|
#elif CONFIG_HAL_BOARD == HAL_BOARD_VRBRAIN && defined(CONFIG_ARCH_BOARD_VRHERO_V10)
|
2014-03-31 14:52:18 -03:00
|
|
|
# define AP_BATT_VOLT_PIN 100
|
|
|
|
# define AP_BATT_CURR_PIN 101
|
|
|
|
# define AP_BATT_VOLTDIVIDER_DEFAULT 1.1
|
|
|
|
# define AP_BATT_CURR_AMP_PERVOLT_DEFAULT 17.0
|
2013-09-28 10:35:27 -03:00
|
|
|
#else
|
|
|
|
# define AP_BATT_VOLT_PIN -1
|
|
|
|
# define AP_BATT_CURR_PIN -1
|
2013-09-30 02:35:19 -03:00
|
|
|
# define AP_BATT_VOLTDIVIDER_DEFAULT 10.1
|
2013-10-09 23:14:01 -03:00
|
|
|
# define AP_BATT_CURR_AMP_PERVOLT_DEFAULT 17.0
|
2013-09-28 10:35:27 -03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Other values normally set directly by mission planner
|
|
|
|
// # define AP_BATT_VOLTDIVIDER_DEFAULT 15.70 // Volt divider for AttoPilot 50V/90A sensor
|
|
|
|
// # define AP_BATT_VOLTDIVIDER_DEFAULT 4.127 // Volt divider for AttoPilot 13.6V/45A sensor
|
|
|
|
// # define AP_BATT_CURR_AMP_PERVOLT_DEFAULT 27.32 // Amp/Volt for AttoPilot 50V/90A sensor
|
|
|
|
// # define AP_BATT_CURR_AMP_PERVOLT_DEFAULT 13.66 // Amp/Volt for AttoPilot 13.6V/45A sensor
|
|
|
|
|
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
|
|
|
|
|
|
|
class AP_BattMonitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
/// Constructor
|
|
|
|
AP_BattMonitor();
|
|
|
|
|
|
|
|
/// Initialize the battery monitor
|
|
|
|
void init();
|
|
|
|
|
|
|
|
/// Read the battery voltage and current. Should be called at 10hz
|
|
|
|
void read();
|
|
|
|
|
|
|
|
/// monitoring - returns whether we are monitoring voltage only or voltage and current
|
|
|
|
int8_t monitoring() const { return _monitoring; }
|
|
|
|
|
2013-10-22 19:40:09 -03:00
|
|
|
/// monitoring - returns whether we are monitoring voltage only or voltage and current
|
|
|
|
void set_monitoring(uint8_t mon) { _monitoring.set(mon); }
|
|
|
|
|
2013-09-28 10:35:27 -03:00
|
|
|
/// Battery voltage. Initialized to 99 to prevent low voltage events at startup
|
|
|
|
float voltage() const { return _voltage; }
|
|
|
|
|
|
|
|
/// Battery pack instantaneous currrent draw in amperes
|
|
|
|
float current_amps() const { return _current_amps; }
|
|
|
|
|
|
|
|
/// Total current drawn since start-up (Amp-hours)
|
2013-10-01 10:30:09 -03:00
|
|
|
float current_total_mah() const { return _current_total_mah; }
|
2013-09-28 10:35:27 -03:00
|
|
|
|
|
|
|
/// capacity_remaining_pct - returns the % battery capacity remaining (0 ~ 100)
|
|
|
|
uint8_t capacity_remaining_pct() const;
|
|
|
|
|
2013-09-30 11:14:09 -03:00
|
|
|
/// exhausted - returns true if the voltage remains below the low_voltage for 10 seconds or remaining capacity falls below min_capacity
|
|
|
|
bool exhausted(float low_voltage, float min_capacity_mah);
|
|
|
|
|
2013-09-28 10:35:27 -03:00
|
|
|
static const struct AP_Param::GroupInfo var_info[];
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
/// parameters
|
2013-09-30 09:56:49 -03:00
|
|
|
AP_Int8 _monitoring; /// 0=disabled, 3=voltage only, 4=voltage and current
|
|
|
|
AP_Int8 _volt_pin; /// board pin used to measure battery voltage
|
|
|
|
AP_Int8 _curr_pin; /// board pin used to measure battery current
|
|
|
|
AP_Float _volt_multiplier; /// voltage on volt pin multiplied by this to calculate battery voltage
|
|
|
|
AP_Float _curr_amp_per_volt; /// voltage on current pin multiplied by this to calculate current in amps
|
|
|
|
AP_Float _curr_amp_offset; /// offset voltage that is subtracted from current pin before conversion to amps
|
|
|
|
AP_Int32 _pack_capacity; /// battery pack capacity less reserve in mAh
|
2013-09-28 10:35:27 -03:00
|
|
|
|
|
|
|
/// internal variables
|
|
|
|
float _voltage; /// last read voltage
|
|
|
|
float _current_amps; /// last read current drawn
|
|
|
|
float _current_total_mah; /// total current drawn since startup (Amp-hours)
|
2013-09-30 11:14:09 -03:00
|
|
|
uint32_t _last_time_micros; /// time when current was last read
|
|
|
|
uint32_t _low_voltage_start_ms; /// time when voltage dropped below the minimum
|
2013-09-28 10:35:27 -03:00
|
|
|
|
2013-09-30 02:17:08 -03:00
|
|
|
AP_HAL::AnalogSource *_volt_pin_analog_source;
|
|
|
|
AP_HAL::AnalogSource *_curr_pin_analog_source;
|
2013-09-28 10:35:27 -03:00
|
|
|
|
|
|
|
};
|
|
|
|
#endif // AP_BATTMONITOR_H
|