mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-05-16 12:07:34 +08:00
batt_smbus: calculate current discharged
This commit is contained in:
@@ -84,6 +84,7 @@
|
||||
#define BATT_SMBUS_ADDR 0x0B ///< I2C address
|
||||
#define BATT_SMBUS_TEMP 0x08 ///< temperature register
|
||||
#define BATT_SMBUS_VOLTAGE 0x09 ///< voltage register
|
||||
#define BATT_SMBUS_REMAINING_CAPACITY 0x0f ///< predicted remaining battery capacity as a percentage
|
||||
#define BATT_SMBUS_DESIGN_CAPACITY 0x18 ///< design capacity register
|
||||
#define BATT_SMBUS_DESIGN_VOLTAGE 0x19 ///< design voltage register
|
||||
#define BATT_SMBUS_SERIALNUM 0x1c ///< serial number register
|
||||
@@ -179,6 +180,7 @@ private:
|
||||
orb_advert_t _batt_topic; ///< uORB battery topic
|
||||
orb_id_t _batt_orb_id; ///< uORB battery topic ID
|
||||
uint64_t _start_time; ///< system time we first attempt to communicate with battery
|
||||
uint16_t _batt_design_capacity; ///< battery's design capacity in mAh (0 means unknown)
|
||||
};
|
||||
|
||||
namespace
|
||||
@@ -197,7 +199,8 @@ BATT_SMBUS::BATT_SMBUS(int bus, uint16_t batt_smbus_addr) :
|
||||
_reports(nullptr),
|
||||
_batt_topic(-1),
|
||||
_batt_orb_id(nullptr),
|
||||
_start_time(0)
|
||||
_start_time(0),
|
||||
_batt_design_capacity(0)
|
||||
{
|
||||
// work_cancel in the dtor will explode if we don't do this...
|
||||
memset(&_work, 0, sizeof(_work));
|
||||
@@ -263,7 +266,7 @@ BATT_SMBUS::test()
|
||||
|
||||
if (updated) {
|
||||
if (orb_copy(ORB_ID(battery_status), sub, &status) == OK) {
|
||||
warnx("V=%4.2f C=%4.2f", status.voltage_v, status.current_a);
|
||||
warnx("V=%4.2f C=%4.2f DismAh=%4.2f", (float)status.voltage_v, (float)status.current_a, (float)status.discharged_mah);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -371,6 +374,24 @@ BATT_SMBUS::cycle()
|
||||
new_report.current_a = -(float)((int32_t)((uint32_t)buff[3] << 24 | (uint32_t)buff[2] << 16 | (uint32_t)buff[1] << 8 | (uint32_t)buff[0])) / 1000.0f;
|
||||
}
|
||||
|
||||
// read battery design capacity
|
||||
if (_batt_design_capacity == 0) {
|
||||
usleep(1);
|
||||
if (read_reg(BATT_SMBUS_DESIGN_CAPACITY, tmp) == OK) {
|
||||
_batt_design_capacity = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
// read remaining capacity
|
||||
if (_batt_design_capacity > 0) {
|
||||
usleep(1);
|
||||
if (read_reg(BATT_SMBUS_REMAINING_CAPACITY, tmp) == OK) {
|
||||
if (tmp < _batt_design_capacity) {
|
||||
new_report.discharged_mah = _batt_design_capacity - tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// publish to orb
|
||||
if (_batt_topic != -1) {
|
||||
orb_publish(_batt_orb_id, _batt_topic, &new_report);
|
||||
|
||||
Reference in New Issue
Block a user