-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
Ardupilot-MAVlink battery monitor #27354
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#include "AP_BattMonitor_config.h" | ||
|
||
#if AP_BATTERY_MAVLINK_ENABLED | ||
|
||
#include "AP_BattMonitor_MAVLink.h" | ||
|
||
|
||
// Constructor | ||
AP_BattMonitor_MAVLink::AP_BattMonitor_MAVLink(AP_BattMonitor &mon, | ||
AP_BattMonitor::BattMonitor_State &mon_state, | ||
AP_BattMonitor_Params ¶ms) | ||
: AP_BattMonitor_Backend(mon, mon_state, params) | ||
{ | ||
_state.healthy = true; | ||
} | ||
|
||
void AP_BattMonitor_MAVLink::handle_msg(const mavlink_message_t &msg) | ||
{ | ||
// decode MAVlink message | ||
mavlink_battery_status_t packet; | ||
mavlink_msg_battery_status_decode(&msg, &packet); | ||
|
||
_state.voltage = 0.0; | ||
// copy independent cells voltage from MAVLink msg | ||
for (uint8_t i = 0; i < MAVLINK_MSG_BATTERY_STATUS_FIELD_VOLTAGES_LEN; i++) | ||
{ | ||
_state.cell_voltages.cells[i] = packet.voltages[i]; | ||
_state.voltage += packet.voltages[i] * 0.001; | ||
} | ||
|
||
// copy battery readings from the MAVLink message | ||
_state.current_amps = packet.current_battery; | ||
_state.consumed_mah = packet.current_consumed; | ||
_state.temperature = packet.temperature * 0.1; | ||
_state.time_remaining = packet.time_remaining; | ||
_state.last_time_micros = AP_HAL::micros(); | ||
_remaining_pct = packet.battery_remaining; | ||
} | ||
|
||
void AP_BattMonitor_MAVLink::read() | ||
{ | ||
update_health(); | ||
} | ||
|
||
// update battery health flag | ||
void AP_BattMonitor_MAVLink::update_health() | ||
{ | ||
uint32_t current_time_micros = AP_HAL::micros(); | ||
if (current_time_micros - _state.last_time_micros > AP_BATTMONITOR_MAVLINK_TIMEOUT_MICROS) | ||
{ | ||
_state.healthy = false; | ||
_have_info = false; | ||
return; | ||
} | ||
_state.healthy = true; | ||
_have_info = true; | ||
} | ||
|
||
// capacity_remaining_pct - returns true if the battery % is available and writes to the percentage argument | ||
bool AP_BattMonitor_MAVLink::capacity_remaining_pct(uint8_t &percentage) const | ||
{ | ||
if (_have_info) | ||
{ | ||
percentage = _remaining_pct; | ||
} | ||
return _have_info; | ||
} | ||
|
||
#endif // AP_BATTERY_MAVLINK_ENABLED |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#pragma once | ||
#if AP_BATTERY_MAVLINK_ENABLED | ||
#include "AP_BattMonitor.h" | ||
#include "AP_BattMonitor_Backend.h" | ||
#define AP_BATTMONITOR_MAVLINK_TIMEOUT_MICROS 5000000 // sensor becomes unhealthy if no successful readings for 5 seconds | ||
|
||
// Base SUI class | ||
class AP_BattMonitor_MAVLink : public AP_BattMonitor_Backend | ||
{ | ||
public: | ||
// Constructor | ||
AP_BattMonitor_MAVLink(AP_BattMonitor &mon, | ||
AP_BattMonitor::BattMonitor_State &mon_state, | ||
AP_BattMonitor_Params ¶ms); | ||
|
||
void handle_msg(const mavlink_message_t &msg) override; | ||
|
||
// read the battery voltage and current. Should be called at 10hz | ||
void read() override; | ||
|
||
// returns true if battery monitor provides current info | ||
bool has_current() const override { return _have_info; } | ||
|
||
// returns true if battery monitor provides individual cell voltage | ||
bool has_cell_voltages() const override { return _have_info; } | ||
|
||
// returns true if battery monitor provides temperature | ||
bool has_temperature() const override { return _have_info; }; | ||
|
||
// capacity_remaining_pct - returns true if the battery % is available and writes to the percentage argument | ||
bool capacity_remaining_pct(uint8_t &percentage) const override WARN_IF_UNUSED; | ||
|
||
private: | ||
bool _have_info; // flag is true if battery MAVlink message is received | ||
uint8_t _remaining_pct; // battery remaining capacity in percentage | ||
void update_health(); // function to update battery health status based on the last message received from the battery | ||
}; | ||
|
||
#endif // AP_BATTERY_MAVLINK_ENABLED |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,3 +119,7 @@ | |
#ifndef AP_BATTERY_SCRIPTING_ENABLED | ||
#define AP_BATTERY_SCRIPTING_ENABLED (AP_SCRIPTING_ENABLED && AP_BATTERY_BACKEND_DEFAULT_ENABLED) | ||
#endif | ||
|
||
#ifndef AP_BATTERY_MAVLINK_ENABLED | ||
#define AP_BATTERY_MAVLINK_ENABLED AP_BATTERY_BACKEND_DEFAULT_ENABLED | ||
#endif | ||
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. miss empty line at end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
you can put this in the loop, that is cpp