Skip to content

Commit

Permalink
info: add duration since arming and takeoff
Browse files Browse the repository at this point in the history
This adds two fields:
- Duration since arming
- Duration since takeoff

And also:
- Simplifies the info plugin a bit by just requesting FLIGHT_INFORMATION
  at 1 Hz.
- Slightly cleans up the info plugin.
- Slightly cleans up the info integration test.

Signed-off-by: Julian Oes <julian@oes.ch>
  • Loading branch information
julianoes committed Apr 15, 2024
1 parent 585936c commit fe05717
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 132 deletions.
2 changes: 1 addition & 1 deletion proto
Submodule proto updated 1 files
+2 −0 protos/info/info.proto
6 changes: 2 additions & 4 deletions src/integration_tests/info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,11 @@ TEST(SitlTest, PX4Info)
EXPECT_EQ(flight_info_result.first, Info::Result::Success);

if (flight_info_result.first == Info::Result::Success) {
std::cout << "Time since boot (ms): "
<< std::to_string(flight_info_result.second.time_boot_ms) << '\n';
std::cout << "Flight UID: " << flight_info_result.second.flight_uid << '\n';
std::cout << flight_info_result.second << '\n';
} else {
LogWarn() << "Product request result: " << flight_info_result.first;
}

std::this_thread::sleep_for(std::chrono::seconds(1));
std::this_thread::sleep_for(std::chrono::seconds(2));
}
}
2 changes: 2 additions & 0 deletions src/mavsdk/plugins/info/include/plugins/info/info.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class Info : public PluginBase {
uint32_t time_boot_ms{}; /**< @brief Time since system boot */
uint64_t flight_uid{}; /**< @brief Flight counter. Starts from zero, is incremented at every
disarm and is never reset (even after reboot) */
uint32_t duration_since_arming_ms{}; /**< @brief Duration since arming in milliseconds */
uint32_t duration_since_takeoff_ms{}; /**< @brief Duration since takeoff in milliseconds */
};

/**
Expand Down
6 changes: 5 additions & 1 deletion src/mavsdk/plugins/info/info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ std::pair<Info::Result, double> Info::get_speed_factor() const

bool operator==(const Info::FlightInfo& lhs, const Info::FlightInfo& rhs)
{
return (rhs.time_boot_ms == lhs.time_boot_ms) && (rhs.flight_uid == lhs.flight_uid);
return (rhs.time_boot_ms == lhs.time_boot_ms) && (rhs.flight_uid == lhs.flight_uid) &&
(rhs.duration_since_arming_ms == lhs.duration_since_arming_ms) &&
(rhs.duration_since_takeoff_ms == lhs.duration_since_takeoff_ms);
}

std::ostream& operator<<(std::ostream& str, Info::FlightInfo const& flight_info)
Expand All @@ -57,6 +59,8 @@ std::ostream& operator<<(std::ostream& str, Info::FlightInfo const& flight_info)
str << "flight_info:" << '\n' << "{\n";
str << " time_boot_ms: " << flight_info.time_boot_ms << '\n';
str << " flight_uid: " << flight_info.flight_uid << '\n';
str << " duration_since_arming_ms: " << flight_info.duration_since_arming_ms << '\n';
str << " duration_since_takeoff_ms: " << flight_info.duration_since_takeoff_ms << '\n';
str << '}';
return str;
}
Expand Down
96 changes: 36 additions & 60 deletions src/mavsdk/plugins/info/info_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,58 +46,27 @@ void InfoImpl::deinit()

void InfoImpl::enable()
{
// We can't rely on System to request the autopilot_version,
// so we do it here, anyway.
_system_impl->send_autopilot_version_request();
_system_impl->send_flight_information_request();

// We're going to retry until we have the version.
_system_impl->add_call_every([this]() { request_version_again(); }, 1.0f, &_call_every_cookie);

// We're going to periodically ask for the flight information
_system_impl->add_call_every(
[this]() { request_flight_information(); }, 1.0f, &_flight_info_call_every_cookie);
[this]() { _system_impl->send_autopilot_version_request(); }, 1.0f, &_call_every_cookie);

// We're hoping to get flight information regularly to update flight time.
_system_impl->set_msg_rate(MAVLINK_MSG_ID_FLIGHT_INFORMATION, 1.0);
}

void InfoImpl::disable()
{
_system_impl->remove_call_every(_call_every_cookie);
_system_impl->remove_call_every(_flight_info_call_every_cookie);

{
std::lock_guard<std::mutex> lock(_mutex);
_information_received = false;
_flight_information_received = false;
}
}

void InfoImpl::request_version_again()
{
{
std::lock_guard<std::mutex> lock(_mutex);
if (_information_received) {
_system_impl->remove_call_every(_call_every_cookie);
return;
}
}

_system_impl->send_autopilot_version_request();
}

void InfoImpl::request_flight_information()
{
// We will request new flight information from the autopilot only if
// we go from an armed to disarmed state or if we haven't received any
// information yet
if ((_was_armed && !_system_impl->is_armed()) || !_flight_information_received) {
_system_impl->send_flight_information_request();
}

_was_armed = _system_impl->is_armed();
std::lock_guard<std::mutex> lock(_mutex);
_flight_information_received = false;
_identification_received = false;
}

void InfoImpl::process_autopilot_version(const mavlink_message_t& message)
{
_system_impl->remove_call_every(_call_every_cookie);

std::lock_guard<std::mutex> lock(_mutex);

mavlink_autopilot_version_t autopilot_version;
Expand All @@ -123,20 +92,6 @@ void InfoImpl::process_autopilot_version(const mavlink_message_t& message)
_version.os_sw_minor = (autopilot_version.os_sw_version >> (8 * 2)) & 0xFF;
_version.os_sw_patch = (autopilot_version.os_sw_version >> (8 * 1)) & 0xFF;

// Debug() << "flight version: "
// << _version.flight_sw_major
// << "."
// << _version.flight_sw_minor
// << "."
// << _version.flight_sw_patch;

// Debug() << "os version: "
// << _version.os_sw_major
// << "."
// << _version.os_sw_minor
// << "."
// << _version.os_sw_patch;

_version.os_sw_git_hash = swap_and_translate_binary_to_str(
autopilot_version.os_custom_version, sizeof(autopilot_version.os_custom_version));

Expand All @@ -151,7 +106,7 @@ void InfoImpl::process_autopilot_version(const mavlink_message_t& message)

_identification.legacy_uid = autopilot_version.uid;

_information_received = true;
_identification_received = true;
}

Info::Version::FlightSoftwareVersionType
Expand Down Expand Up @@ -187,6 +142,21 @@ void InfoImpl::process_flight_information(const mavlink_message_t& message)

_flight_info.time_boot_ms = flight_information.time_boot_ms;
_flight_info.flight_uid = flight_information.flight_uuid;
// The fields are called UTC but are actually since boot
const auto arming_time_ms = flight_information.arming_time_utc / 1000;
const auto takeoff_time_ms = flight_information.takeoff_time_utc / 1000;

if (arming_time_ms > 0 && arming_time_ms < flight_information.time_boot_ms) {
_flight_info.duration_since_arming_ms = flight_information.time_boot_ms - arming_time_ms;
} else {
_flight_info.duration_since_arming_ms = 0;
}

if (takeoff_time_ms > 0 && takeoff_time_ms < flight_information.time_boot_ms) {
_flight_info.duration_since_takeoff_ms = flight_information.time_boot_ms - takeoff_time_ms;
} else {
_flight_info.duration_since_takeoff_ms = 0;
}

_flight_information_received = true;
}
Expand Down Expand Up @@ -222,7 +192,8 @@ std::pair<Info::Result, Info::Identification> InfoImpl::get_identification() con

std::lock_guard<std::mutex> lock(_mutex);
return std::make_pair<>(
(_information_received ? Info::Result::Success : Info::Result::InformationNotReceivedYet),
(_identification_received ? Info::Result::Success :
Info::Result::InformationNotReceivedYet),
_identification);
}

Expand All @@ -233,7 +204,8 @@ std::pair<Info::Result, Info::Version> InfoImpl::get_version() const
std::lock_guard<std::mutex> lock(_mutex);

return std::make_pair<>(
(_information_received ? Info::Result::Success : Info::Result::InformationNotReceivedYet),
(_identification_received ? Info::Result::Success :
Info::Result::InformationNotReceivedYet),
_version);
}

Expand All @@ -243,7 +215,8 @@ std::pair<Info::Result, Info::Product> InfoImpl::get_product() const
std::lock_guard<std::mutex> lock(_mutex);

return std::make_pair<>(
(_information_received ? Info::Result::Success : Info::Result::InformationNotReceivedYet),
(_identification_received ? Info::Result::Success :
Info::Result::InformationNotReceivedYet),
_product);
}

Expand Down Expand Up @@ -321,8 +294,11 @@ void InfoImpl::wait_for_information() const
{
// Wait 1.5 seconds max
for (unsigned i = 0; i < 150; ++i) {
if (_information_received) {
break;
{
std::lock_guard<std::mutex> lock(_mutex);
if (_identification_received) {
break;
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
Expand Down
7 changes: 2 additions & 5 deletions src/mavsdk/plugins/info/info_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ class InfoImpl : public PluginImplBase {
InfoImpl& operator=(const InfoImpl&) = delete;

private:
void request_version_again();
void request_flight_information();
void process_heartbeat(const mavlink_message_t& message);
void process_autopilot_version(const mavlink_message_t& message);
void process_flight_information(const mavlink_message_t& message);
Expand All @@ -48,13 +46,12 @@ class InfoImpl : public PluginImplBase {
Info::Version _version{};
Info::Product _product{};
Info::Identification _identification{};
bool _identification_received{false};

Info::FlightInfo _flight_info{};
std::atomic<bool> _information_received{false};
bool _flight_information_received{false};
bool _was_armed{false};

void* _call_every_cookie{nullptr};
void* _flight_info_call_every_cookie{nullptr};

struct SpeedFactorMeasurement {
double simulated_duration_s{0.0};
Expand Down
Loading

0 comments on commit fe05717

Please sign in to comment.