Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated batt_smbus. #9080

Merged
merged 6 commits into from
Mar 15, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions src/drivers/batt_smbus/batt_smbus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,38 +541,50 @@ BATT_SMBUS::cycle()
}

// read remaining capacity
if (_batt_capacity > 0) {
if (read_reg(BATT_SMBUS_REMAINING_CAPACITY, tmp) == OK) {
if (read_reg(BATT_SMBUS_REMAINING_CAPACITY, tmp) == OK) {

if (tmp > _batt_capacity) {
PX4_WARN("Remaining Cap greater than total: Cap:%hu RemainingCap:%hu", (uint16_t)_batt_capacity, (uint16_t)tmp);
_batt_capacity = (uint16_t)tmp;
}
if (tmp > _batt_capacity) {
PX4_WARN("Remaining Cap greater than total: Cap:%hu RemainingCap:%hu", (uint16_t)_batt_capacity, (uint16_t)tmp);
_batt_capacity = (uint16_t)tmp;
}

new_report.remaining = (float)(1.000f - (((float)_batt_capacity - (float)tmp) / (float)_batt_capacity));
// Calculate remaining capacity percent with complementary filter
new_report.remaining = (float)(_last_report.remaining * 0.8f) + (float)(0.2f * (float)(1.000f - (((
float)_batt_capacity - (float)tmp) / (float)_batt_capacity)));

// calculate total discharged amount
new_report.discharged_mah = (float)((float)_batt_startup_capacity - (float)tmp);
}
// calculate total discharged amount
new_report.discharged_mah = (float)((float)_batt_startup_capacity - (float)tmp);
}

// read battery temperature and covert to Celsius
if (read_reg(BATT_SMBUS_TEMP, tmp) == OK) {
new_report.temperature = (float)(((float)tmp / 10.0f) - 273.15f);
}

// propagate warning state only if the state
if (new_report.remaining < _emergency_thr) {
//Check if remaining % is out of range
if ((new_report.remaining > 1.00f) || (new_report.remaining <= 0.00f)) {
new_report.warning = battery_status_s::BATTERY_WARNING_EMERGENCY;
}

//Check if discharged amount is greater than the starting capacity
else if (new_report.discharged_mah > (float)_batt_startup_capacity) {
new_report.warning = battery_status_s::BATTERY_WARNING_EMERGENCY;
}

} else if (new_report.remaining < _crit_thr) {
new_report.warning = battery_status_s::BATTERY_WARNING_CRITICAL;
// propagate warning state
else {
if (new_report.remaining > _low_thr) {
new_report.warning = battery_status_s::BATTERY_WARNING_NONE;

} else if (new_report.remaining < _low_thr) {
new_report.warning = battery_status_s::BATTERY_WARNING_LOW;
} else if (new_report.remaining > _crit_thr) {
new_report.warning = battery_status_s::BATTERY_WARNING_LOW;

} else {
new_report.warning = battery_status_s::BATTERY_WARNING_NONE;
} else if (new_report.remaining > _emergency_thr) {
new_report.warning = battery_status_s::BATTERY_WARNING_CRITICAL;

} else {
new_report.warning = battery_status_s::BATTERY_WARNING_EMERGENCY;
}
}

new_report.capacity = _batt_capacity;
Expand Down