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_NeoDesign.cpp
Views: 1798
#include "AP_BattMonitor_config.h"12#if AP_BATTERY_SMBUS_NEODESIGN_ENABLED34#include <AP_HAL/AP_HAL.h>5#include <AP_Common/AP_Common.h>6#include "AP_BattMonitor.h"78#include "AP_BattMonitor_SMBus_NeoDesign.h"910#define BATTMONITOR_ND_CELL_COUNT 0x5C // cell-count register11#define BATTMONITOR_ND_CELL_START 0x30 // first cell register1213// Constructor14AP_BattMonitor_SMBus_NeoDesign::AP_BattMonitor_SMBus_NeoDesign(AP_BattMonitor &mon,15AP_BattMonitor::BattMonitor_State &mon_state,16AP_BattMonitor_Params ¶ms)17: AP_BattMonitor_SMBus(mon, mon_state, params, AP_BATTMONITOR_SMBUS_BUS_INTERNAL)18{19_pec_supported = true;20}2122void AP_BattMonitor_SMBus_NeoDesign::timer()23{24uint16_t data;25// Get the cell count once, it's not likely to change in flight26if (_cell_count == 0) {27if (!read_word(BATTMONITOR_ND_CELL_COUNT, data)) {28return; // something wrong, don't try anything else29}30// constrain maximum cellcount in case of i2c corruption31if (data > max_cell_count) {32_cell_count = max_cell_count;33} else {34_cell_count = data;35}36}3738bool read_all_cells = true;39for(uint8_t i = 0; i < _cell_count; ++i) {40if(read_word(BATTMONITOR_ND_CELL_START + i, data)) {41_state.cell_voltages.cells[i] = data;42_has_cell_voltages = true;43} else {44read_all_cells = false;45}46}4748const uint32_t tnow = AP_HAL::micros();4950if (read_all_cells && (_cell_count > 0)) {51uint32_t summed = 0;52for (int i = 0; i < _cell_count; i++) {53summed += _state.cell_voltages.cells[i];54}55_state.voltage = (float)summed * 1e-3f;56_state.last_time_micros = tnow;57_state.healthy = true;58} else if (read_word(BATTMONITOR_SMBUS_VOLTAGE, data)) {59// fallback to the voltage register if we didn't manage to poll the cells60_state.voltage = (float)data * 1e-3f;61_state.last_time_micros = tnow;62_state.healthy = true;63}6465// timeout after 5 seconds66if ((tnow - _state.last_time_micros) > AP_BATTMONITOR_SMBUS_TIMEOUT_MICROS) {67_state.healthy = false;68// do not attempt to read any more data from battery69return;70}7172// read current (A)73if (read_word(BATTMONITOR_SMBUS_CURRENT, data)) {74_state.current_amps = -(float)((int16_t)data) * 1e-3f;75_state.last_time_micros = tnow;76}7778read_full_charge_capacity();79read_remaining_capacity();80read_temp();81}8283#endif // AP_BATTERY_SMBUS_NEODESIGN_ENABLED848586