Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AP_BattMonitor/AP_BattMonitor_SMBus_Generic.cpp
Views: 1798
#include "AP_BattMonitor_config.h"12#if AP_BATTERY_SMBUS_GENERIC_ENABLED34#include <AP_HAL/AP_HAL.h>5#include <AP_Common/AP_Common.h>6#include <AP_Math/AP_Math.h>7#include "AP_BattMonitor.h"89#include "AP_BattMonitor_SMBus_Generic.h"1011uint8_t smbus_cell_ids[] = { 0x3f, // cell 1120x3e, // cell 2130x3d, // cell 3140x3c, // cell 4150x3b, // cell 5160x3a, // cell 6170x39, // cell 7180x38, // cell 8190x37, // cell 9200x36, // cell 10210x35, // cell 11220x34, // cell 1223#if CONFIG_HAL_BOARD == HAL_BOARD_SITL240x33, // cell 13250x32 // cell 1426#endif27};2829#define SMBUS_CELL_COUNT_CHECK_TIMEOUT 15 // check cell count for up to 15 seconds3031/*32* Other potentially useful registers, listed here for future use33* #define BATTMONITOR_SMBUS_MAXELL_CHARGE_STATUS 0x0d // relative state of charge34* #define BATTMONITOR_SMBUS_MAXELL_BATTERY_STATUS 0x16 // battery status register including alarms35* #define BATTMONITOR_SMBUS_MAXELL_BATTERY_CYCLE_COUNT 0x17 // cycle count36* #define BATTMONITOR_SMBUS_MAXELL_DESIGN_VOLTAGE 0x19 // design voltage register37* #define BATTMONITOR_SMBUS_MAXELL_MANUFACTURE_DATE 0x1b // manufacturer date38* #define BATTMONITOR_SMBUS_MAXELL_SERIALNUM 0x1c // serial number register39* #define BATTMONITOR_SMBUS_MAXELL_HEALTH_STATUS 0x4f // state of health40* #define BATTMONITOR_SMBUS_MAXELL_SAFETY_ALERT 0x50 // safety alert41* #define BATTMONITOR_SMBUS_MAXELL_SAFETY_STATUS 0x51 // safety status42* #define BATTMONITOR_SMBUS_MAXELL_PF_ALERT 0x52 // safety status43* #define BATTMONITOR_SMBUS_MAXELL_PF_STATUS 0x53 // safety status44*/4546// Constructor47AP_BattMonitor_SMBus_Generic::AP_BattMonitor_SMBus_Generic(AP_BattMonitor &mon,48AP_BattMonitor::BattMonitor_State &mon_state,49AP_BattMonitor_Params ¶ms)50: AP_BattMonitor_SMBus(mon, mon_state, params, AP_BATTMONITOR_SMBUS_BUS_EXTERNAL)51{}5253void AP_BattMonitor_SMBus_Generic::timer()54{55// check if PEC is supported56if (!check_pec_support()) {57return;58}5960uint16_t data;61uint32_t tnow = AP_HAL::micros();6263// read voltage (V)64if (read_word(BATTMONITOR_SMBUS_VOLTAGE, data)) {65_state.voltage = (float)data * 0.001f;66_state.last_time_micros = tnow;67_state.healthy = true;68}6970// assert that BATTMONITOR_SMBUS_NUM_CELLS_MAX must be no more than smbus_cell_ids71static_assert(BATTMONITOR_SMBUS_NUM_CELLS_MAX <= ARRAY_SIZE(smbus_cell_ids), "BATTMONITOR_SMBUS_NUM_CELLS_MAX must be no more than smbus_cell_ids");7273// check cell count74if (!_cell_count_fixed) {75if (_state.healthy) {76// when battery first becomes healthy, start check of cell count77if (_cell_count_check_start_us == 0) {78_cell_count_check_start_us = tnow;79}80if (tnow - _cell_count_check_start_us > (SMBUS_CELL_COUNT_CHECK_TIMEOUT * 1e6)) {81// give up checking cell count after 15sec of continuous healthy battery reads82_cell_count_fixed = true;83}84} else {85// if battery becomes unhealthy restart cell count check86_cell_count_check_start_us = 0;87}88}8990// we loop over something limited by91// BATTMONITOR_SMBUS_NUM_CELLS_MAX but assign into something92// limited by AP_BATT_MONITOR_CELLS_MAX - so make sure we won't93// over-write:94static_assert(BATTMONITOR_SMBUS_NUM_CELLS_MAX <= ARRAY_SIZE(_state.cell_voltages.cells), "BATTMONITOR_SMBUS_NUM_CELLS_MAX must be <= number of cells in state voltages");9596// read cell voltages97for (uint8_t i = 0; i < (_cell_count_fixed ? _cell_count : BATTMONITOR_SMBUS_NUM_CELLS_MAX); i++) {98if (read_word(smbus_cell_ids[i], data) && (data > 0) && (data < UINT16_MAX)) {99_has_cell_voltages = true;100_state.cell_voltages.cells[i] = data;101_last_cell_update_us[i] = tnow;102if (!_cell_count_fixed) {103_cell_count = MAX(_cell_count, i + 1);104}105} else if ((tnow - _last_cell_update_us[i]) > AP_BATTMONITOR_SMBUS_TIMEOUT_MICROS) {106_state.cell_voltages.cells[i] = UINT16_MAX;107}108}109110// timeout after 5 seconds111if ((tnow - _state.last_time_micros) > AP_BATTMONITOR_SMBUS_TIMEOUT_MICROS) {112_state.healthy = false;113return;114}115116// read current (A)117if (read_word(BATTMONITOR_SMBUS_CURRENT, data)) {118_state.current_amps = -(float)((int16_t)data) * 0.001f;119_state.last_time_micros = tnow;120}121122read_full_charge_capacity();123124// FIXME: Perform current integration if the remaining capacity can't be requested125read_remaining_capacity();126127read_temp();128129read_serial_number();130131read_cycle_count();132}133134// check if PEC supported with the version value in SpecificationInfo() function135// returns true once PEC is confirmed as working or not working136bool AP_BattMonitor_SMBus_Generic::check_pec_support()137{138// exit immediately if we have already confirmed pec support139if (_pec_confirmed) {140return true;141}142143// specification info144uint16_t data;145if (!read_word(BATTMONITOR_SMBUS_SPECIFICATION_INFO, data)) {146return false;147}148149// extract version150uint8_t version = (data & 0xF0) >> 4;151152// version less than 0011b (i.e. 3) do not support PEC153if (version < 3) {154_pec_supported = false;155_pec_confirmed = true;156return true;157}158159// check manufacturer name160uint8_t buff[AP_BATTMONITOR_SMBUS_READ_BLOCK_MAXIMUM_TRANSFER + 1] {};161if (read_block(BATTMONITOR_SMBUS_MANUFACTURE_NAME, buff, sizeof(buff))) {162// Hitachi maxell batteries do not support PEC163if (strcmp((char*)buff, "Hitachi maxell") == 0) {164_pec_supported = false;165_pec_confirmed = true;166return true;167}168}169170// assume all other batteries support PEC171_pec_supported = true;172_pec_confirmed = true;173return true;174}175176#endif // AP_BATTERY_SMBUS_GENERIC_ENABLED177178179