-
Notifications
You must be signed in to change notification settings - Fork 18k
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
AP_BattMonitor: Adds smart battery shutdown framework and TI BQ battery monitor #27288
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,50 @@ | ||||||||||||
#include "AP_BattMonitor_config.h" | ||||||||||||
|
||||||||||||
#if AP_BATTERY_SMBUS_TIBQ_ENABLED | ||||||||||||
|
||||||||||||
#include "AP_BattMonitor_SMBus_TIBQ.h" | ||||||||||||
|
||||||||||||
#include <AP_HAL/AP_HAL.h> | ||||||||||||
#include <GCS_MAVLink/GCS.h> | ||||||||||||
|
||||||||||||
|
||||||||||||
// Extention of AP_BattMonitor_SMBus_Generic to include TI's BQ40Z chip shutdown mechanism | ||||||||||||
AP_BattMonitor_SMBus_TIBQ::AP_BattMonitor_SMBus_TIBQ(AP_BattMonitor &mon, | ||||||||||||
AP_BattMonitor::BattMonitor_State &mon_state, | ||||||||||||
AP_BattMonitor_Params ¶ms) | ||||||||||||
: AP_BattMonitor_SMBus_Generic(mon, mon_state, params) | ||||||||||||
{ | ||||||||||||
_exit_emshut = true; | ||||||||||||
} | ||||||||||||
|
||||||||||||
|
||||||||||||
void AP_BattMonitor_SMBus_TIBQ::timer() { | ||||||||||||
if (_exit_emshut) { | ||||||||||||
// Exit EMERGENCY SHUTDOWN state in case it was engaged on the last poweroff: | ||||||||||||
uint8_t cmd[] = {0x00, 0xA7, 0x23}; | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
if (_dev->transfer(cmd, 3, nullptr, 0)) { | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "BQ40Z bms exited shutdown"); | ||||||||||||
_exit_emshut = false; | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
AP_BattMonitor_SMBus_Generic::timer(); | ||||||||||||
} | ||||||||||||
|
||||||||||||
|
||||||||||||
bool AP_BattMonitor_SMBus_TIBQ::shutdown() { | ||||||||||||
// Semaphore is needed in case this is called from another thread | ||||||||||||
WITH_SEMAPHORE(_dev->get_semaphore()); | ||||||||||||
|
||||||||||||
uint8_t cmd[] = {0x00, 0x10, 0x00}; | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
if (!_dev->transfer(cmd, 3, nullptr, 0)) { | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
GCS_SEND_TEXT(MAV_SEVERITY_ERROR, "Failed to shutdown TIBQ"); | ||||||||||||
return false; | ||||||||||||
} else { | ||||||||||||
_state.is_powering_off = true; | ||||||||||||
} | ||||||||||||
Comment on lines
+43
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
||||||||||||
return true; | ||||||||||||
} | ||||||||||||
|
||||||||||||
#endif // AP_BATTERY_SMBUS_TIBQ_ENABLED |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#pragma once | ||
|
||
#include "AP_BattMonitor_config.h" | ||
|
||
#if AP_BATTERY_SMBUS_TIBQ_ENABLED | ||
|
||
#include "AP_BattMonitor_SMBus_Generic.h" | ||
|
||
class AP_BattMonitor_SMBus_TIBQ : public AP_BattMonitor_SMBus_Generic | ||
{ | ||
public: | ||
// Constructor | ||
AP_BattMonitor_SMBus_TIBQ(AP_BattMonitor &mon, | ||
AP_BattMonitor::BattMonitor_State &mon_state, | ||
AP_BattMonitor_Params ¶ms); | ||
|
||
private: | ||
void timer(void) override; | ||
|
||
// returns true if the battery can be shutdown with shutdown() | ||
bool can_shutdown() override { return true; }; | ||
// shuts the battery down if supported | ||
bool shutdown() override; | ||
|
||
bool _exit_emshut; | ||
}; | ||
|
||
#endif // AP_BATTERY_SMBUS_TIBQ_ENABLED |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3389,7 +3389,7 @@ MAV_RESULT GCS_MAVLINK::handle_preflight_reboot(const mavlink_command_int_t &pac | |
#endif | ||
} | ||
|
||
// refuse reboot when armed: | ||
// refuse shutdown/reboot when armed: | ||
if (hal.util->get_soft_armed()) { | ||
/// but allow it if forced: | ||
const uint32_t magic_force_reboot_value = 20190226; | ||
|
@@ -3398,6 +3398,18 @@ MAV_RESULT GCS_MAVLINK::handle_preflight_reboot(const mavlink_command_int_t &pac | |
} | ||
} | ||
|
||
#if AP_BATTERY_ENABLED | ||
// Check if shutdown is requested | ||
if (is_equal(packet.param1, 2.0f)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this should be in PREFLIGHT_REBOOT_SHUTDOWN |
||
// If the battery monitor in use doesn't support shutdown | ||
if (!AP::battery().shutdown()){ | ||
IanBurwell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return MAV_RESULT_FAILED; | ||
} | ||
|
||
return MAV_RESULT_ACCEPTED; | ||
} | ||
#endif | ||
|
||
if (!(is_equal(packet.param1, 1.0f) || is_equal(packet.param1, 3.0f))) { | ||
// param1 must be 1 or 3 - 1 being reboot, 3 being reboot-to-bootloader | ||
return MAV_RESULT_UNSUPPORTED; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we be returning "true" for "we can shut down all 0 batteries"?