AP_BattMonitor_Sum.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include <AP_HAL/AP_HAL.h>
  2. #include <AP_Common/AP_Common.h>
  3. #include <AP_Math/AP_Math.h>
  4. #include "AP_BattMonitor.h"
  5. #include "AP_BattMonitor_Sum.h"
  6. /*
  7. battery monitor that is the sum of other battery monitors after this one
  8. This can be used to combined other current/voltage sensors into a
  9. single backend
  10. */
  11. extern const AP_HAL::HAL& hal;
  12. /// Constructor
  13. AP_BattMonitor_Sum::AP_BattMonitor_Sum(AP_BattMonitor &mon,
  14. AP_BattMonitor::BattMonitor_State &mon_state,
  15. AP_BattMonitor_Params &params,
  16. uint8_t instance) :
  17. AP_BattMonitor_Backend(mon, mon_state, params),
  18. _instance(instance)
  19. {
  20. }
  21. // read - read the voltage and current
  22. void
  23. AP_BattMonitor_Sum::read()
  24. {
  25. float voltage_sum = 0;
  26. uint8_t voltage_count = 0;
  27. float current_sum = 0;
  28. uint8_t current_count = 0;
  29. for (uint8_t i=_instance+1; i<_mon.num_instances(); i++) {
  30. if (!_mon.healthy(i)) {
  31. continue;
  32. }
  33. voltage_sum += _mon.voltage(i);
  34. voltage_count++;
  35. float current;
  36. if (_mon.current_amps(current, i)) {
  37. current_sum += current;
  38. current_count++;
  39. }
  40. }
  41. if (voltage_count > 0) {
  42. _state.voltage = voltage_sum / voltage_count;
  43. _state.last_time_micros = AP_HAL::micros();
  44. }
  45. if (current_count > 0) {
  46. _state.current_amps = current_sum;
  47. }
  48. _has_current = (current_count > 0);
  49. _state.healthy = (voltage_count > 0);
  50. }