Browse Source
this is used to combine battery monitors into a single reporting unit. It operates on all instances beyond itselfmission-4.1.18
Andrew Tridgell
6 years ago
6 changed files with 89 additions and 2 deletions
@ -0,0 +1,54 @@
@@ -0,0 +1,54 @@
|
||||
#include <AP_HAL/AP_HAL.h> |
||||
#include <AP_Common/AP_Common.h> |
||||
#include <AP_Math/AP_Math.h> |
||||
#include "AP_BattMonitor.h" |
||||
#include "AP_BattMonitor_Sum.h" |
||||
|
||||
/*
|
||||
battery monitor that is the sum of other battery monitors after this one |
||||
|
||||
This can be used to combined other current/voltage sensors into a |
||||
single backend |
||||
*/ |
||||
extern const AP_HAL::HAL& hal; |
||||
|
||||
/// Constructor
|
||||
AP_BattMonitor_Sum::AP_BattMonitor_Sum(AP_BattMonitor &mon, |
||||
AP_BattMonitor::BattMonitor_State &mon_state, |
||||
AP_BattMonitor_Params ¶ms, |
||||
uint8_t instance) : |
||||
AP_BattMonitor_Backend(mon, mon_state, params), |
||||
_instance(instance) |
||||
{ |
||||
} |
||||
|
||||
// read - read the voltage and current
|
||||
void |
||||
AP_BattMonitor_Sum::read() |
||||
{ |
||||
float voltage_sum = 0; |
||||
uint8_t voltage_count = 0; |
||||
float current_sum = 0; |
||||
uint8_t current_count = 0; |
||||
|
||||
for (uint8_t i=_instance+1; i<_mon.num_instances(); i++) { |
||||
if (!_mon.healthy(i)) { |
||||
continue; |
||||
} |
||||
voltage_sum += _mon.voltage(i); |
||||
voltage_count++; |
||||
if (_mon.has_current(i)) { |
||||
current_sum += _mon.current_amps(i); |
||||
current_count++; |
||||
} |
||||
} |
||||
if (voltage_count > 0) { |
||||
_state.voltage = voltage_sum / voltage_count; |
||||
_state.last_time_micros = AP_HAL::micros(); |
||||
} |
||||
if (current_count > 0) { |
||||
_state.current_amps = current_sum; |
||||
} |
||||
_has_current = (current_count > 0); |
||||
_state.healthy = (voltage_count > 0); |
||||
} |
@ -0,0 +1,27 @@
@@ -0,0 +1,27 @@
|
||||
#pragma once |
||||
|
||||
#include "AP_BattMonitor.h" |
||||
#include "AP_BattMonitor_Backend.h" |
||||
|
||||
class AP_BattMonitor_Sum : public AP_BattMonitor_Backend |
||||
{ |
||||
public: |
||||
|
||||
/// Constructor
|
||||
AP_BattMonitor_Sum(AP_BattMonitor &mon, AP_BattMonitor::BattMonitor_State &mon_state, AP_BattMonitor_Params ¶ms, uint8_t instance); |
||||
|
||||
/// Read the battery voltage and current. Should be called at 10hz
|
||||
void read() override; |
||||
|
||||
/// returns true if battery monitor provides consumed energy info
|
||||
bool has_consumed_energy() const override { return has_current(); } |
||||
|
||||
/// returns true if battery monitor provides current info
|
||||
bool has_current() const override { return _has_current; } |
||||
|
||||
void init(void) override {} |
||||
|
||||
private: |
||||
uint8_t _instance; |
||||
bool _has_current; |
||||
}; |
Loading…
Reference in new issue