From cbce03d550293d63ba13aa785a92773e4372043d Mon Sep 17 00:00:00 2001 From: Nick Exton Date: Wed, 8 Nov 2023 17:04:35 +1100 Subject: [PATCH] GCS_MAVLink: Add FLIGHT_INFORMATION message --- libraries/GCS_MAVLink/GCS.h | 7 ++++ libraries/GCS_MAVLink/GCS_Common.cpp | 55 ++++++++++++++++++++++++++++ libraries/GCS_MAVLink/ap_message.h | 1 + 3 files changed, 63 insertions(+) diff --git a/libraries/GCS_MAVLink/GCS.h b/libraries/GCS_MAVLink/GCS.h index 71725275b3e9ed..5af6e0c1672fbc 100644 --- a/libraries/GCS_MAVLink/GCS.h +++ b/libraries/GCS_MAVLink/GCS.h @@ -402,6 +402,13 @@ class GCS_MAVLINK void send_uavionix_adsb_out_status() const; void send_autopilot_state_for_gimbal_device() const; + struct { + MAV_LANDED_STATE last_landed_state; + uint64_t takeoff_time_us; + } flight_info; + + void send_flight_information(); + // lock a channel, preventing use by MAVLink void lock(bool _lock) { _locked = _lock; diff --git a/libraries/GCS_MAVLink/GCS_Common.cpp b/libraries/GCS_MAVLink/GCS_Common.cpp index 61daff9dc3d299..f3b49273e7de16 100644 --- a/libraries/GCS_MAVLink/GCS_Common.cpp +++ b/libraries/GCS_MAVLink/GCS_Common.cpp @@ -5964,6 +5964,56 @@ void GCS_MAVLINK::send_autopilot_state_for_gimbal_device() const #endif // AP_AHRS_ENABLED } +void GCS_MAVLINK::send_flight_information() +{ + const uint32_t time_boot_us = AP_HAL::millis64(); + + // This field is misnamed as `arming_time_utc` in MAVLink. However, it is + // not a UTC time, it is the microseconds since boot. + const uint64_t arm_time_us = AP::arming().arm_time_us(); + + const MAV_LANDED_STATE current_landed_state = landed_state(); + if (flight_info.last_landed_state != current_landed_state) { + switch (current_landed_state) { + case MAV_LANDED_STATE_IN_AIR: + case MAV_LANDED_STATE_TAKEOFF: + case MAV_LANDED_STATE_LANDING: { + if (!flight_info.takeoff_time_us) { + flight_info.takeoff_time_us = time_boot_us; + } + break; + } + + case MAV_LANDED_STATE_ON_GROUND: { + flight_info.takeoff_time_us = 0; + break; + } + + case MAV_LANDED_STATE_UNDEFINED: + case MAV_LANDED_STATE_ENUM_END: { + break; + } + } + + flight_info.last_landed_state = current_landed_state; + } + + // This field is misnamed as `takeoff_time_utc` in MAVLink. However, it is + // not a UTC time, it is the microseconds since boot. + uint64_t takeoff_time_us = flight_info.takeoff_time_us; + + // This field is misnamed as `flight_uuid` in MAVLink. + const uint64_t flight_number = 0; + + mavlink_msg_flight_information_send( + chan, + time_boot_us, + arm_time_us, + takeoff_time_us, + flight_number + ); +} + void GCS_MAVLINK::send_received_message_deprecation_warning(const char * message) { // we're not expecting very many of these ever, so a tiny bit of @@ -6395,6 +6445,11 @@ bool GCS_MAVLINK::try_send_message(const enum ap_message id) break; #endif + case MSG_FLIGHT_INFORMATION: + CHECK_PAYLOAD_SIZE(FLIGHT_INFORMATION); + send_flight_information(); + break; + default: // try_send_message must always at some stage return true for // a message, or we will attempt to infinitely retry the diff --git a/libraries/GCS_MAVLink/ap_message.h b/libraries/GCS_MAVLink/ap_message.h index 4923317266025c..8d1f3d6133a1ac 100644 --- a/libraries/GCS_MAVLink/ap_message.h +++ b/libraries/GCS_MAVLink/ap_message.h @@ -97,5 +97,6 @@ enum ap_message : uint8_t { #if AP_MAVLINK_MSG_HIGHRES_IMU_ENABLED MSG_HIGHRES_IMU, #endif + MSG_FLIGHT_INFORMATION, MSG_LAST // MSG_LAST must be the last entry in this enum };