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

New BATTERY_STATUS_V2 #1792

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
35 changes: 35 additions & 0 deletions src/mavsdk/plugins/telemetry/telemetry_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ void TelemetryImpl::init()
[this](const mavlink_message_t& message) { process_battery_status(message); },
this);

_parent->register_mavlink_message_handler(
MAVLINK_MSG_ID_BATTERY_STATUS_V2,
[this](const mavlink_message_t& message) { process_battery_status_v2(message); },
this);

_parent->register_mavlink_message_handler(
MAVLINK_MSG_ID_HEARTBEAT,
[this](const mavlink_message_t& message) { process_heartbeat(message); },
Expand Down Expand Up @@ -1176,6 +1181,36 @@ void TelemetryImpl::process_battery_status(const mavlink_message_t& message)
}
}

void TelemetryImpl::process_battery_status_v2(const mavlink_message_t& message)
{
mavlink_battery_status_v2_t bat_status;
mavlink_msg_battery_status_v2_decode(&message, &bat_status);

_has_bat_status = true;

Telemetry::Battery new_battery;
new_battery.id = bat_status.id;
new_battery.voltage_v = (std::numeric_limits<uint32_t>::max() == bat_status.voltage) ? NAN : bat_status.voltage * 1e-3f;
new_battery.remaining_percent = (std::numeric_limits<uint8_t>::max() == bat_status.percent_remaining) ? NAN : bat_status.percent_remaining;

// To be added
// uint32_t current; /*< [mA] Battery current (through all cells/loads). UINT32_MAX: field not provided.*/
// uint32_t status_flags; /*< Fault, health, and readiness status indications.*/

// Not exposed: temperature, capacity_remaining, capacity_consumed

set_battery(new_battery);
JonasVautherin marked this conversation as resolved.
Show resolved Hide resolved

{
std::lock_guard<std::mutex> lock(_subscription_mutex);
if (_battery_subscription) {
auto callback = _battery_subscription;
auto arg = battery();
_parent->call_user_callback([callback, arg]() { callback(arg); });
}
}
}

void TelemetryImpl::process_heartbeat(const mavlink_message_t& message)
{
if (message.compid != MAV_COMP_ID_AUTOPILOT1) {
Expand Down
1 change: 1 addition & 0 deletions src/mavsdk/plugins/telemetry/telemetry_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ class TelemetryImpl : public PluginImplBase {
void process_fixedwing_metrics(const mavlink_message_t& message);
void process_sys_status(const mavlink_message_t& message);
void process_battery_status(const mavlink_message_t& message);
void process_battery_status_v2(const mavlink_message_t& message);
void process_heartbeat(const mavlink_message_t& message);
void process_rc_channels(const mavlink_message_t& message);
void process_unix_epoch_time(const mavlink_message_t& message);
Expand Down
4 changes: 3 additions & 1 deletion third_party/mavlink/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ find_package(Python3 COMPONENTS Interpreter REQUIRED)
ExternalProject_add(
mavlink
GIT_REPOSITORY https://github.com/mavlink/mavlink
GIT_TAG 3b52eac09c2e37325e4bc49cd2667ea37bf1d7d2
GIT_TAG daabb9a54d1fc3b8cc6a4f3517168d381d278678
# This GIT_TAG will need to update as it is for a temporary branch containing the new battery
# https://github.com/mavlink/mavlink/commit/daabb9a54d1fc3b8cc6a4f3517168d381d278678
PREFIX mavlink
CONFIGURE_COMMAND Python3::Interpreter
-m pymavlink.tools.mavgen
Expand Down