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.h
Views: 1798
#pragma once12#include "AP_BattMonitor_Backend.h"34#if AP_BATTERY_SMBUS_ENABLED56#include <AP_Common/AP_Common.h>7#include <AP_Param/AP_Param.h>8#include <AP_Math/AP_Math.h>9#include <AP_HAL/I2CDevice.h>10#include <utility>1112#define AP_BATTMONITOR_SMBUS_BUS_INTERNAL 013#define AP_BATTMONITOR_SMBUS_BUS_EXTERNAL 114#define AP_BATTMONITOR_SMBUS_I2C_ADDR 0x0B15#define AP_BATTMONITOR_SMBUS_TIMEOUT_MICROS 5000000 // sensor becomes unhealthy if no successful readings for 5 seconds16#define AP_BATTMONITOR_SMBUS_READ_BLOCK_MAXIMUM_TRANSFER 0x20 // A Block Read or Write is allowed to transfer a maximum of 32 data bytes.1718class AP_BattMonitor_SMBus : public AP_BattMonitor_Backend19{20public:2122// Smart Battery Data Specification Revision 1.123enum BATTMONITOR_SMBUS {24BATTMONITOR_SMBUS_TEMP = 0x08, // Temperature25BATTMONITOR_SMBUS_VOLTAGE = 0x09, // Voltage26BATTMONITOR_SMBUS_CURRENT = 0x0A, // Current27BATTMONITOR_SMBUS_REMAINING_CAPACITY = 0x0F, // Remaining Capacity28BATTMONITOR_SMBUS_FULL_CHARGE_CAPACITY = 0x10, // Full Charge Capacity29BATTMONITOR_SMBUS_CYCLE_COUNT = 0x17, // Cycle Count30BATTMONITOR_SMBUS_SPECIFICATION_INFO = 0x1A, // Specification Info31BATTMONITOR_SMBUS_SERIAL = 0x1C, // Serial Number32BATTMONITOR_SMBUS_MANUFACTURE_NAME = 0x20, // Manufacture Name33BATTMONITOR_SMBUS_MANUFACTURE_DATA = 0x23, // Manufacture Data34};3536/// Constructor37AP_BattMonitor_SMBus(AP_BattMonitor &mon,38AP_BattMonitor::BattMonitor_State &mon_state,39AP_BattMonitor_Params ¶ms,40uint8_t i2c_bus);4142// virtual destructor to reduce compiler warnings43virtual ~AP_BattMonitor_SMBus() {}4445bool has_cell_voltages() const override { return _has_cell_voltages; }4647bool has_temperature() const override { return _has_temperature; }4849// all smart batteries are expected to provide current50bool has_current() const override { return true; }5152// don't allow reset of remaining capacity for SMBus53bool reset_remaining(float percentage) override { return false; }5455// return true if cycle count can be provided and fills in cycles argument56bool get_cycle_count(uint16_t &cycles) const override;5758virtual void init(void) override;5960static const struct AP_Param::GroupInfo var_info[];6162protected:6364void read(void) override;6566// reads the pack full charge capacity67void read_full_charge_capacity(void);6869// reads the remaining capacity70// which will only be read if we know the full charge capacity (accounting for battery degradation)71void read_remaining_capacity(void);7273// return a scaler that should be multiplied by the battery's reported capacity numbers to arrive at the actual capacity in mAh74virtual uint16_t get_capacity_scaler() const { return 1; }7576// reads the temperature word from the battery77virtual void read_temp(void);7879// reads the serial number if it's not already known80// returns if the serial number was already known81void read_serial_number(void);8283// reads the battery's cycle count84void read_cycle_count();8586// read word from register87// returns true if read was successful, false if failed88bool read_word(uint8_t reg, uint16_t& data) const;8990// read_block - returns number of characters read if successful, zero if unsuccessful91uint8_t read_block(uint8_t reg, uint8_t* data, uint8_t len) const;9293// get_PEC - calculate PEC for a read or write from the battery94// buff is the data that was read or will be written95uint8_t get_PEC(const uint8_t i2c_addr, uint8_t cmd, bool reading, const uint8_t buff[], uint8_t len) const;9697AP_HAL::OwnPtr<AP_HAL::I2CDevice> _dev;98bool _pec_supported; // true if PEC is supported99100int32_t _serial_number = -1; // battery serial number101uint16_t _full_charge_capacity; // full charge capacity, used to stash the value before setting the parameter102bool _has_cell_voltages; // smbus backends flag this as true once they have received a valid cell voltage report103uint16_t _cycle_count = 0; // number of cycles the battery has experienced. An amount of discharge approximately equal to the value of DesignCapacity.104bool _has_cycle_count; // true if cycle count has been retrieved from the battery105bool _has_temperature;106107virtual void timer(void) = 0; // timer function to read from the battery108109AP_HAL::Device::PeriodicHandle timer_handle;110111// Parameters112AP_Int8 _bus; // I2C bus number113AP_Int8 _address; // I2C address114115};116117#endif // AP_BATTERY_SMBUS_ENABLED118119120