AP_BattMonitor_SMBus_Solo.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_SMBus_Solo.h"
  6. #include <utility>
  7. #define BATTMONITOR_SMBUS_SOLO_CELL_VOLTAGE 0x28 // cell voltage register
  8. #define BATTMONITOR_SMBUS_SOLO_CURRENT 0x2a // current register
  9. #define BATTMONITOR_SMBUS_SOLO_BUTTON_DEBOUNCE 6 // button held down for 5 intervals will cause a power off event
  10. #define BATTMONITOR_SMBUS_SOLO_NUM_CELLS 4 // solo's battery pack is 4S
  11. /*
  12. * Other potentially useful registers, listed here for future use
  13. * #define BATTMONITOR_SMBUS_SOLO_VOLTAGE 0x09 // voltage register
  14. * #define BATTMONITOR_SMBUS_SOLO_BATTERY_STATUS 0x16 // battery status register including alarms
  15. * #define BATTMONITOR_SMBUS_SOLO_DESIGN_CAPACITY 0x18 // design capacity register
  16. * #define BATTMONITOR_SMBUS_SOLO_DESIGN_VOLTAGE 0x19 // design voltage register
  17. * #define BATTMONITOR_SMBUS_SOLO_SERIALNUM 0x1c // serial number register
  18. * #define BATTMONITOR_SMBUS_SOLO_MANUFACTURE_NAME 0x20 // manufacturer name
  19. * #define BATTMONITOR_SMBUS_SOLO_DEVICE_NAME 0x21 // device name
  20. * #define BATTMONITOR_SMBUS_SOLO_DEVICE_CHEMISTRY 0x22 // device chemistry
  21. * #define BATTMONITOR_SMBUS_SOLO_MANUFACTURE_INFO 0x25 // manufacturer info including cell voltage
  22. */
  23. // Constructor
  24. AP_BattMonitor_SMBus_Solo::AP_BattMonitor_SMBus_Solo(AP_BattMonitor &mon,
  25. AP_BattMonitor::BattMonitor_State &mon_state,
  26. AP_BattMonitor_Params &params,
  27. AP_HAL::OwnPtr<AP_HAL::I2CDevice> dev)
  28. : AP_BattMonitor_SMBus(mon, mon_state, params, std::move(dev))
  29. {
  30. _pec_supported = true;
  31. }
  32. void AP_BattMonitor_SMBus_Solo::timer()
  33. {
  34. uint8_t buff[8];
  35. uint32_t tnow = AP_HAL::micros();
  36. // read cell voltages
  37. if (read_block(BATTMONITOR_SMBUS_SOLO_CELL_VOLTAGE, buff, 8, false)) {
  38. float pack_voltage_mv = 0.0f;
  39. for (uint8_t i = 0; i < BATTMONITOR_SMBUS_SOLO_NUM_CELLS; i++) {
  40. uint16_t cell = buff[(i * 2) + 1] << 8 | buff[i * 2];
  41. _state.cell_voltages.cells[i] = cell;
  42. pack_voltage_mv += (float)cell;
  43. }
  44. _has_cell_voltages = true;
  45. // accumulate the pack voltage out of the total of the cells
  46. // because the Solo's I2C bus is so noisy, it's worth not spending the
  47. // time and bus bandwidth to request the pack voltage as a seperate
  48. // transaction
  49. _state.voltage = pack_voltage_mv * 1e-3f;
  50. _state.last_time_micros = tnow;
  51. _state.healthy = true;
  52. }
  53. // timeout after 5 seconds
  54. if ((tnow - _state.last_time_micros) > AP_BATTMONITOR_SMBUS_TIMEOUT_MICROS) {
  55. _state.healthy = false;
  56. // do not attempt to ready any more data from battery
  57. return;
  58. }
  59. // read current
  60. if (read_block(BATTMONITOR_SMBUS_SOLO_CURRENT, buff, 4, false) == 4) {
  61. _state.current_amps = -(float)((int32_t)((uint32_t)buff[3]<<24 | (uint32_t)buff[2]<<16 | (uint32_t)buff[1]<<8 | (uint32_t)buff[0])) / 1000.0f;
  62. _state.last_time_micros = tnow;
  63. }
  64. read_full_charge_capacity();
  65. read_remaining_capacity();
  66. // read the button press indicator
  67. if (read_block(BATTMONITOR_SMBUS_MANUFACTURE_DATA, buff, 6, false) == 6) {
  68. bool pressed = (buff[1] >> 3) & 0x01;
  69. if (_button_press_count >= BATTMONITOR_SMBUS_SOLO_BUTTON_DEBOUNCE) {
  70. // vehicle will power off, set state flag
  71. _state.is_powering_off = true;
  72. } else if (pressed) {
  73. // battery will power off if the button is held
  74. _button_press_count++;
  75. } else {
  76. // button released, reset counters
  77. _button_press_count = 0;
  78. }
  79. }
  80. read_temp();
  81. read_serial_number();
  82. }
  83. // read_block - returns number of characters read if successful, zero if unsuccessful
  84. uint8_t AP_BattMonitor_SMBus_Solo::read_block(uint8_t reg, uint8_t* data, uint8_t max_len, bool append_zero) const
  85. {
  86. uint8_t buff[max_len+2]; // buffer to hold results (2 extra byte returned holding length and PEC)
  87. // read bytes
  88. if (!_dev->read_registers(reg, buff, sizeof(buff))) {
  89. return 0;
  90. }
  91. // get length
  92. uint8_t bufflen = buff[0];
  93. // sanity check length returned by smbus
  94. if (bufflen == 0 || bufflen > max_len) {
  95. return 0;
  96. }
  97. // check PEC
  98. uint8_t pec = get_PEC(AP_BATTMONITOR_SMBUS_I2C_ADDR, reg, true, buff, bufflen+1);
  99. if (pec != buff[bufflen+1]) {
  100. return 0;
  101. }
  102. // copy data (excluding PEC)
  103. memcpy(data, &buff[1], bufflen);
  104. // optionally add zero to end
  105. if (append_zero) {
  106. data[bufflen] = '\0';
  107. }
  108. // return success
  109. return bufflen;
  110. }