2016-02-17 21:25:15 -04:00
|
|
|
#pragma once
|
2014-11-22 02:04:04 -04:00
|
|
|
|
2015-08-11 03:28:42 -03:00
|
|
|
#include <AP_Common/AP_Common.h>
|
|
|
|
#include <AP_Param/AP_Param.h>
|
|
|
|
#include <AP_Math/AP_Math.h>
|
2020-06-02 07:57:47 -03:00
|
|
|
#include <AP_HAL/I2CDevice.h>
|
2014-11-22 02:04:04 -04:00
|
|
|
#include "AP_BattMonitor_Backend.h"
|
2017-04-19 17:10:15 -03:00
|
|
|
#include <utility>
|
2014-11-22 02:04:04 -04:00
|
|
|
|
2017-02-08 20:28:14 -04:00
|
|
|
#define AP_BATTMONITOR_SMBUS_BUS_INTERNAL 0
|
|
|
|
#define AP_BATTMONITOR_SMBUS_BUS_EXTERNAL 1
|
|
|
|
#define AP_BATTMONITOR_SMBUS_I2C_ADDR 0x0B
|
|
|
|
#define AP_BATTMONITOR_SMBUS_TIMEOUT_MICROS 5000000 // sensor becomes unhealthy if no successful readings for 5 seconds
|
2014-11-22 02:04:04 -04:00
|
|
|
|
|
|
|
class AP_BattMonitor_SMBus : public AP_BattMonitor_Backend
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2017-12-29 05:39:57 -04:00
|
|
|
// Smart Battery Data Specification Revision 1.1
|
|
|
|
enum BATTMONITOR_SMBUS {
|
|
|
|
BATTMONITOR_SMBUS_TEMP = 0x08, // Temperature
|
|
|
|
BATTMONITOR_SMBUS_VOLTAGE = 0x09, // Voltage
|
|
|
|
BATTMONITOR_SMBUS_CURRENT = 0x0A, // Current
|
|
|
|
BATTMONITOR_SMBUS_REMAINING_CAPACITY = 0x0F, // Remaining Capacity
|
|
|
|
BATTMONITOR_SMBUS_FULL_CHARGE_CAPACITY = 0x10, // Full Charge Capacity
|
2019-12-10 20:55:20 -04:00
|
|
|
BATTMONITOR_SMBUS_CYCLE_COUNT = 0x17, // Cycle Count
|
2017-12-29 05:39:57 -04:00
|
|
|
BATTMONITOR_SMBUS_SPECIFICATION_INFO = 0x1A, // Specification Info
|
|
|
|
BATTMONITOR_SMBUS_SERIAL = 0x1C, // Serial Number
|
|
|
|
BATTMONITOR_SMBUS_MANUFACTURE_NAME = 0x20, // Manufacture Name
|
|
|
|
BATTMONITOR_SMBUS_MANUFACTURE_DATA = 0x23, // Manufacture Data
|
|
|
|
};
|
|
|
|
|
2014-11-22 02:04:04 -04:00
|
|
|
/// Constructor
|
2017-04-24 03:25:52 -03:00
|
|
|
AP_BattMonitor_SMBus(AP_BattMonitor &mon,
|
2017-04-19 17:10:15 -03:00
|
|
|
AP_BattMonitor::BattMonitor_State &mon_state,
|
2017-10-27 02:36:49 -03:00
|
|
|
AP_BattMonitor_Params ¶ms,
|
2017-04-24 03:16:49 -03:00
|
|
|
AP_HAL::OwnPtr<AP_HAL::I2CDevice> dev);
|
2014-11-22 02:04:04 -04:00
|
|
|
|
|
|
|
// virtual destructor to reduce compiler warnings
|
|
|
|
virtual ~AP_BattMonitor_SMBus() {}
|
|
|
|
|
2017-06-11 05:59:02 -03:00
|
|
|
bool has_cell_voltages() const override { return _has_cell_voltages; }
|
|
|
|
|
2021-01-07 22:11:56 -04:00
|
|
|
bool has_temperature() const override { return _has_temperature; }
|
|
|
|
|
2017-06-11 05:59:02 -03:00
|
|
|
// all smart batteries are expected to provide current
|
2017-05-24 04:43:10 -03:00
|
|
|
bool has_current() const override { return true; }
|
2017-04-19 16:15:24 -03:00
|
|
|
|
2019-06-18 06:30:18 -03:00
|
|
|
// don't allow reset of remaining capacity for SMBus
|
|
|
|
bool reset_remaining(float percentage) override { return false; }
|
2019-12-10 20:55:20 -04:00
|
|
|
|
|
|
|
// return true if cycle count can be provided and fills in cycles argument
|
|
|
|
bool get_cycle_count(uint16_t &cycles) const override;
|
|
|
|
|
2019-12-04 08:03:33 -04:00
|
|
|
virtual void init(void) override;
|
2017-10-26 00:58:56 -03:00
|
|
|
|
2017-04-19 16:15:24 -03:00
|
|
|
protected:
|
|
|
|
|
2017-04-24 03:16:49 -03:00
|
|
|
void read(void) override;
|
|
|
|
|
2017-04-24 03:49:51 -03:00
|
|
|
// reads the pack full charge capacity
|
2021-07-14 03:52:59 -03:00
|
|
|
void read_full_charge_capacity(void);
|
2017-04-24 03:49:51 -03:00
|
|
|
|
2017-05-16 01:34:08 -03:00
|
|
|
// reads the remaining capacity
|
2021-07-14 03:52:59 -03:00
|
|
|
// which will only be read if we know the full charge capacity (accounting for battery degradation)
|
|
|
|
void read_remaining_capacity(void);
|
2017-05-16 01:34:08 -03:00
|
|
|
|
2020-06-02 21:23:45 -03:00
|
|
|
// return a scaler that should be multiplied by the battery's reported capacity numbers to arrive at the actual capacity in mAh
|
|
|
|
virtual uint16_t get_capacity_scaler() const { return 1; }
|
|
|
|
|
2017-04-19 17:49:38 -03:00
|
|
|
// reads the temperature word from the battery
|
2021-07-14 03:52:59 -03:00
|
|
|
virtual void read_temp(void);
|
2017-04-19 17:49:38 -03:00
|
|
|
|
2017-04-24 03:16:49 -03:00
|
|
|
// reads the serial number if it's not already known
|
2021-07-14 03:52:59 -03:00
|
|
|
// returns if the serial number was already known
|
|
|
|
void read_serial_number(void);
|
2017-04-24 03:16:49 -03:00
|
|
|
|
2019-12-10 20:55:20 -04:00
|
|
|
// reads the battery's cycle count
|
|
|
|
void read_cycle_count();
|
|
|
|
|
2017-04-19 17:10:15 -03:00
|
|
|
// read word from register
|
|
|
|
// returns true if read was successful, false if failed
|
|
|
|
bool read_word(uint8_t reg, uint16_t& data) const;
|
|
|
|
|
2017-04-19 16:15:24 -03:00
|
|
|
// get_PEC - calculate PEC for a read or write from the battery
|
|
|
|
// buff is the data that was read or will be written
|
|
|
|
uint8_t get_PEC(const uint8_t i2c_addr, uint8_t cmd, bool reading, const uint8_t buff[], uint8_t len) const;
|
|
|
|
|
2017-04-19 17:10:15 -03:00
|
|
|
AP_HAL::OwnPtr<AP_HAL::I2CDevice> _dev;
|
|
|
|
bool _pec_supported; // true if PEC is supported
|
2017-04-19 17:49:38 -03:00
|
|
|
|
2017-04-24 03:16:49 -03:00
|
|
|
int32_t _serial_number = -1; // battery serial number
|
2017-04-24 03:49:51 -03:00
|
|
|
uint16_t _full_charge_capacity; // full charge capacity, used to stash the value before setting the parameter
|
2019-12-10 20:55:20 -04:00
|
|
|
bool _has_cell_voltages; // smbus backends flag this as true once they have received a valid cell voltage report
|
|
|
|
uint16_t _cycle_count = 0; // number of cycles the battery has experienced. An amount of discharge approximately equal to the value of DesignCapacity.
|
|
|
|
bool _has_cycle_count; // true if cycle count has been retrieved from the battery
|
2021-01-07 22:11:56 -04:00
|
|
|
bool _has_temperature;
|
2017-06-11 05:59:02 -03:00
|
|
|
|
2017-10-26 00:58:56 -03:00
|
|
|
virtual void timer(void) = 0; // timer function to read from the battery
|
|
|
|
|
2019-12-04 08:03:33 -04:00
|
|
|
AP_HAL::Device::PeriodicHandle timer_handle;
|
2014-11-22 02:04:04 -04:00
|
|
|
};
|