AP_BattMonitor: added 2nd battery voltage monitoring

This commit is contained in:
Andrew Tridgell 2014-08-09 12:13:49 +10:00
parent 11025c8da9
commit ab6302540d
2 changed files with 42 additions and 3 deletions

View File

@ -13,7 +13,7 @@ const AP_Param::GroupInfo AP_BattMonitor::var_info[] PROGMEM = {
// @Param: VOLT_PIN
// @DisplayName: Battery Voltage sensing pin
// @Description: Setting this to 0 ~ 13 will enable battery current sensing on pins A0 ~ A13. For the 3DR power brick on APM2.5 it should be set to 13. On the PX4 it should be set to 100. On the Pixhawk powered from the PM connector it should be set to 2.
// @Description: Setting this to 0 ~ 13 will enable battery voltage sensing on pins A0 ~ A13. For the 3DR power brick on APM2.5 it should be set to 13. On the PX4 it should be set to 100. On the Pixhawk powered from the PM connector it should be set to 2.
// @Values: -1:Disabled, 0:A0, 1:A1, 2:Pixhawk, 13:A13, 100:PX4
// @User: Standard
AP_GROUPINFO("VOLT_PIN", 1, AP_BattMonitor, _volt_pin, AP_BATT_VOLT_PIN),
@ -53,6 +53,18 @@ const AP_Param::GroupInfo AP_BattMonitor::var_info[] PROGMEM = {
// @User: Standard
AP_GROUPINFO("CAPACITY", 6, AP_BattMonitor, _pack_capacity, AP_BATT_CAPACITY_DEFAULT),
// @Param: VOLT2_PIN
// @DisplayName: 2nd Battery Voltage sensing pin
// @Description: This sets the pin for sensing the voltage on a 2nd battery. Set to -1 to disable sensing of a second battery
// @User: Standard
AP_GROUPINFO("VOLT2_PIN", 7, AP_BattMonitor, _volt2_pin, -1),
// @Param: VOLT2_MULT
// @DisplayName: 2nd battery voltage multiplier
// @Description: Used to convert the voltage of the VOLT2_PIN to the actual battery's voltage (pin_voltage * VOLT_MULT).
// @User: Advanced
AP_GROUPINFO("VOLT2_MULT", 8, AP_BattMonitor, _volt2_multiplier, 1),
AP_GROUPEND
};
@ -62,6 +74,7 @@ const AP_Param::GroupInfo AP_BattMonitor::var_info[] PROGMEM = {
//
AP_BattMonitor::AP_BattMonitor(void) :
_voltage(0),
_voltage2(0),
_current_amps(0),
_current_total_mah(0),
_last_time_micros(0)
@ -75,6 +88,11 @@ AP_BattMonitor::init()
{
_volt_pin_analog_source = hal.analogin->channel(_volt_pin);
_curr_pin_analog_source = hal.analogin->channel(_curr_pin);
if (_volt2_pin != -1) {
_volt2_pin_analog_source = hal.analogin->channel(_volt2_pin);
} else {
_volt2_pin_analog_source = NULL;
}
}
// read - read the voltage and current
@ -90,6 +108,9 @@ AP_BattMonitor::read()
// this copes with changing the pin at runtime
_volt_pin_analog_source->set_pin(_volt_pin);
_voltage = _volt_pin_analog_source->voltage_average() * _volt_multiplier;
if (_volt2_pin_analog_source != NULL) {
_voltage2 = _volt2_pin_analog_source->voltage_average() * _volt2_multiplier;
}
}
// read current
@ -145,3 +166,13 @@ bool AP_BattMonitor::exhausted(float low_voltage, float min_capacity_mah)
// if we've gotten this far battery is ok
return false;
}
/// 2nd Battery voltage, if available. return false otherwise
bool AP_BattMonitor::voltage2(float &v) const
{
if (_volt2_pin_analog_source != NULL) {
v = _voltage2;
return true;
}
return false;
}

View File

@ -116,9 +116,12 @@ public:
/// monitoring - returns whether we are monitoring voltage only or voltage and current
void set_monitoring(uint8_t mon) { _monitoring.set(mon); }
/// Battery voltage. Initialized to 99 to prevent low voltage events at startup
/// Battery voltage. Initialized to 0
float voltage() const { return _voltage; }
/// 2nd Battery voltage, if available. return false otherwise
bool voltage2(float &voltage) const;
/// Battery pack instantaneous currrent draw in amperes
float current_amps() const { return _current_amps; }
@ -144,8 +147,13 @@ protected:
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
// 2nd battery monitoring
AP_Int8 _volt2_pin; /// board pin used to measure 2nd battery voltage
AP_Float _volt2_multiplier; /// voltage on volt2 pin multiplier
/// internal variables
float _voltage; /// last read voltage
float _voltage2; /// last read voltage 2nd battery
float _current_amps; /// last read current drawn
float _current_total_mah; /// total current drawn since startup (Amp-hours)
uint32_t _last_time_micros; /// time when current was last read
@ -153,6 +161,6 @@ protected:
AP_HAL::AnalogSource *_volt_pin_analog_source;
AP_HAL::AnalogSource *_curr_pin_analog_source;
AP_HAL::AnalogSource *_volt2_pin_analog_source;
};
#endif // AP_BATTMONITOR_H