AP_BattMonitor_UAVCAN.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include <AP_HAL/AP_HAL.h>
  2. #if HAL_WITH_UAVCAN
  3. #include "AP_BattMonitor.h"
  4. #include "AP_BattMonitor_UAVCAN.h"
  5. #include <AP_BoardConfig/AP_BoardConfig_CAN.h>
  6. #include <AP_Common/AP_Common.h>
  7. #include <AP_Math/AP_Math.h>
  8. #include <AP_UAVCAN/AP_UAVCAN.h>
  9. #include <uavcan/equipment/power/BatteryInfo.hpp>
  10. extern const AP_HAL::HAL& hal;
  11. #define debug_bm_uavcan(level_debug, can_driver, fmt, args...) do { if ((level_debug) <= AP::can().get_debug_level_driver(can_driver)) { printf(fmt, ##args); }} while (0)
  12. UC_REGISTRY_BINDER(BattInfoCb, uavcan::equipment::power::BatteryInfo);
  13. /// Constructor
  14. AP_BattMonitor_UAVCAN::AP_BattMonitor_UAVCAN(AP_BattMonitor &mon, AP_BattMonitor::BattMonitor_State &mon_state, BattMonitor_UAVCAN_Type type, AP_BattMonitor_Params &params) :
  15. AP_BattMonitor_Backend(mon, mon_state, params),
  16. _type(type)
  17. {
  18. // starts with not healthy
  19. _state.healthy = false;
  20. }
  21. void AP_BattMonitor_UAVCAN::subscribe_msgs(AP_UAVCAN* ap_uavcan)
  22. {
  23. if (ap_uavcan == nullptr) {
  24. return;
  25. }
  26. auto* node = ap_uavcan->get_node();
  27. uavcan::Subscriber<uavcan::equipment::power::BatteryInfo, BattInfoCb> *battinfo_listener;
  28. battinfo_listener = new uavcan::Subscriber<uavcan::equipment::power::BatteryInfo, BattInfoCb>(*node);
  29. // Backend Msg Handler
  30. const int battinfo_listener_res = battinfo_listener->start(BattInfoCb(ap_uavcan, &handle_battery_info_trampoline));
  31. if (battinfo_listener_res < 0) {
  32. AP_HAL::panic("UAVCAN BatteryInfo subscriber start problem\n\r");
  33. return;
  34. }
  35. }
  36. AP_BattMonitor_UAVCAN* AP_BattMonitor_UAVCAN::get_uavcan_backend(AP_UAVCAN* ap_uavcan, uint8_t node_id)
  37. {
  38. if (ap_uavcan == nullptr) {
  39. return nullptr;
  40. }
  41. for (uint8_t i = 0; i < AP::battery()._num_instances; i++) {
  42. if (AP::battery().drivers[i] == nullptr ||
  43. AP::battery().get_type(i) != AP_BattMonitor_Params::BattMonitor_TYPE_UAVCAN_BatteryInfo) {
  44. continue;
  45. }
  46. AP_BattMonitor_UAVCAN* driver = (AP_BattMonitor_UAVCAN*)AP::battery().drivers[i];
  47. if (driver->_ap_uavcan == ap_uavcan && driver->_node_id == node_id) {
  48. return driver;
  49. }
  50. }
  51. // find empty uavcan driver
  52. for (uint8_t i = 0; i < AP::battery()._num_instances; i++) {
  53. if (AP::battery().drivers[i] != nullptr &&
  54. AP::battery().get_type(i) == AP_BattMonitor_Params::BattMonitor_TYPE_UAVCAN_BatteryInfo) {
  55. AP_BattMonitor_UAVCAN* batmon = (AP_BattMonitor_UAVCAN*)AP::battery().drivers[i];
  56. batmon->_ap_uavcan = ap_uavcan;
  57. batmon->_node_id = node_id;
  58. batmon->init();
  59. debug_bm_uavcan(2,
  60. ap_uavcan->get_driver_index(),
  61. "Registered BattMonitor Node %d on Bus %d\n",
  62. node_id,
  63. ap_uavcan->get_driver_index());
  64. return batmon;
  65. }
  66. }
  67. return nullptr;
  68. }
  69. void AP_BattMonitor_UAVCAN::handle_battery_info(const BattInfoCb &cb)
  70. {
  71. WITH_SEMAPHORE(_sem_battmon);
  72. _interim_state.temperature = cb.msg->temperature;
  73. _interim_state.voltage = cb.msg->voltage;
  74. _interim_state.current_amps = cb.msg->current;
  75. uint32_t tnow = AP_HAL::micros();
  76. uint32_t dt = tnow - _interim_state.last_time_micros;
  77. // update total current drawn since startup
  78. if (_interim_state.last_time_micros != 0 && dt < 2000000) {
  79. // .0002778 is 1/3600 (conversion to hours)
  80. float mah = (float) ((double) _interim_state.current_amps * (double) dt * (double) 0.0000002778f);
  81. _interim_state.consumed_mah += mah;
  82. _interim_state.consumed_wh += 0.001f * mah * _interim_state.voltage;
  83. }
  84. // record time
  85. _interim_state.last_time_micros = tnow;
  86. _interim_state.healthy = true;
  87. }
  88. void AP_BattMonitor_UAVCAN::handle_battery_info_trampoline(AP_UAVCAN* ap_uavcan, uint8_t node_id, const BattInfoCb &cb)
  89. {
  90. AP_BattMonitor_UAVCAN* driver = get_uavcan_backend(ap_uavcan, node_id);
  91. if (driver == nullptr) {
  92. return;
  93. }
  94. driver->handle_battery_info(cb);
  95. }
  96. // read - read the voltage and current
  97. void AP_BattMonitor_UAVCAN::read()
  98. {
  99. uint32_t tnow = AP_HAL::micros();
  100. // timeout after 5 seconds
  101. if ((tnow - _interim_state.last_time_micros) > AP_BATTMONITOR_UAVCAN_TIMEOUT_MICROS) {
  102. _interim_state.healthy = false;
  103. }
  104. // Copy over relevant states over to main state
  105. WITH_SEMAPHORE(_sem_battmon);
  106. _state.temperature = _interim_state.temperature;
  107. _state.voltage = _interim_state.voltage;
  108. _state.current_amps = _interim_state.current_amps;
  109. _state.consumed_mah = _interim_state.consumed_mah;
  110. _state.consumed_wh = _interim_state.consumed_wh;
  111. _state.last_time_micros = _interim_state.last_time_micros;
  112. _state.healthy = _interim_state.healthy;
  113. }
  114. #endif