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

AP_BattMonitor: Adds smart battery shutdown framework and TI BQ battery monitor #27288

Closed
wants to merge 2 commits into from

Conversation

IanBurwell
Copy link
Contributor

@IanBurwell IanBurwell commented Jun 12, 2024

Adds ability to command shutdown of a smart-battery system via PREFLIGHT_REBOOT_SHUTDOWN, with an example BattMonitor backend that supports being commanded to poweroff.
We use the TI BQ40Z60 and BQ40Z80 chips at GreenSight in our Dreamer line of drones, and use this shutdown functionality to allow full system shutdown from a companion computer.
Note that the TI BQ BattMonitor backend also includes logic to reset the chip's emergency shutdown flag upon successful boot. This is used to ensure the battery stays on after charging is initiated.

Similar functionality was also implemented in a sibling PR that uses LUA but which lacks the broader framework for battery shutdown.

@IanBurwell
Copy link
Contributor Author

Sibling PR: #27287

@IanBurwell IanBurwell marked this pull request as draft June 12, 2024 15:09
@IanBurwell IanBurwell force-pushed the ti_bq_pr branch 6 times, most recently from 6bc529a to 6da890b Compare June 13, 2024 17:17
@IanBurwell IanBurwell marked this pull request as ready for review June 13, 2024 18:33
Copy link
Member

@IamPete1 IamPete1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should hook into the existing battery power off stuff somehow.

// Check's each smart battery instance for its powering off state and broadcasts notifications
void AP_BattMonitor::checkPoweringOff(void)

That is for the shutdown signal coming in from the battery rather than the other way round. But we probably should send that mavlink MAV_CMD_POWER_OFF_INITIATED in this case too.

libraries/AP_BattMonitor/AP_BattMonitor_SMBus_TIBQ.cpp Outdated Show resolved Hide resolved
libraries/AP_BattMonitor/AP_BattMonitor_SMBus_TIBQ.h Outdated Show resolved Hide resolved
libraries/GCS_MAVLink/GCS_Common.cpp Outdated Show resolved Hide resolved
libraries/GCS_MAVLink/GCS_Common.cpp Outdated Show resolved Hide resolved
libraries/GCS_MAVLink/GCS_Common.cpp Outdated Show resolved Hide resolved
libraries/AP_BattMonitor/AP_BattMonitor.cpp Outdated Show resolved Hide resolved
libraries/AP_BattMonitor/AP_BattMonitor.cpp Outdated Show resolved Hide resolved
libraries/AP_BattMonitor/AP_BattMonitor.cpp Outdated Show resolved Hide resolved
libraries/AP_BattMonitor/AP_BattMonitor.cpp Outdated Show resolved Hide resolved
libraries/AP_BattMonitor/AP_BattMonitor.cpp Outdated Show resolved Hide resolved
Copy link
Member

@IamPete1 IamPete1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the shutdown functionality unique to the TI BQ? Or is the new driver really just a flag to say that its SMBus battery that can shutdown?

libraries/AP_BattMonitor/AP_BattMonitor.cpp Outdated Show resolved Hide resolved
libraries/AP_BattMonitor/AP_BattMonitor_SMBus_TIBQ.cpp Outdated Show resolved Hide resolved
libraries/GCS_MAVLink/GCS_Common.cpp Outdated Show resolved Hide resolved
libraries/GCS_MAVLink/GCS_Common.cpp Show resolved Hide resolved
Copy link
Collaborator

@Hwurzburg Hwurzburg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to update

// @Values: 0:Disabled,3:Analog Voltage Only,4:Analog Voltage and Current,5:Solo,6:Bebop,7:SMBus-Generic,8:DroneCAN-BatteryInfo,9:ESC,10:Sum Of Selected Monitors,11:FuelFlow,12:FuelLevelPWM,13:SMBUS-SUI3,14:SMBUS-SUI6,15:NeoDesign,16:SMBus-Maxell,17:Generator-Elec,18:Generator-Fuel,19:Rotoye,20:MPPT,21:INA2XX,22:LTC2946,23:Torqeedo,24:FuelLevelAnalog,25:Synthetic Current and Analog Voltage,26:INA239_SPI,27:EFI,28:AD7091R5,29:Scripting

@Hwurzburg Hwurzburg added the WikiNeeded needs wiki update label Jul 11, 2024
@IanBurwell
Copy link
Contributor Author

I believe the functionality to command shutdown is not unique to the BQ, but that the back-end interface is chipset specific. Shutting down a TI BQ40Z chip is initiated via SMBus ManufactureAccess() which is not consistent between different SMBus batteries. The bootup logic to clear the EMSHUT flag on the other hand is a BQ-specific requirement, which is why I didn't include any sort of abstraction in AP_BatteryMonitor for that sequence.

Copy link
Contributor

@tridge tridge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please look at how MAV_CMD_BATTERY_RESET is done, this should be similar

@@ -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)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should be in PREFLIGHT_REBOOT_SHUTDOWN
I think we need MAV_CMD_BATTERY_CONTROL command, which takes a bitmask of batterys to apply this to, and which allows for both shutdown and startup

// Semaphore is needed in case this is called from another thread
WITH_SEMAPHORE(_dev->get_semaphore());

uint8_t cmd[] = {0x00, 0x10, 0x00};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
uint8_t cmd[] = {0x00, 0x10, 0x00};
static const uint8_t cmd[] {0x00, 0x10, 0x00};

WITH_SEMAPHORE(_dev->get_semaphore());

uint8_t cmd[] = {0x00, 0x10, 0x00};
if (!_dev->transfer(cmd, 3, nullptr, 0)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!_dev->transfer(cmd, 3, nullptr, 0)) {
if (!_dev->transfer(cmd, sizeof(cmd), nullptr, 0)) {

Comment on lines +43 to +45
} else {
_state.is_powering_off = true;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} else {
_state.is_powering_off = true;
}
}
_state.is_powering_off = 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};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
uint8_t cmd[] = {0x00, 0xA7, 0x23};
static const uint8_t cmd[] {0x00, 0xA7, 0x23};

if (_exit_emshut) {
// Exit EMERGENCY SHUTDOWN state in case it was engaged on the last poweroff:
uint8_t cmd[] = {0x00, 0xA7, 0x23};
if (_dev->transfer(cmd, 3, nullptr, 0)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (_dev->transfer(cmd, 3, nullptr, 0)) {
if (_dev->transfer(cmd, sizeof(cmd), nullptr, 0)) {

// Attempts to shut down all batteries that support doing so
bool AP_BattMonitor::shutdown()
{
if (!can_shutdown() || _num_instances == 0) {
Copy link
Contributor

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"?

@tridge
Copy link
Contributor

tridge commented Jul 15, 2024

discussed on dev call, decided the lua approach is better
#27287

@IanBurwell
Copy link
Contributor Author

Closing as it was decided that this would better fit as a script, which is implemented in #27287

@IanBurwell IanBurwell closed this Jul 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
WikiNeeded needs wiki update
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants