From fa3b3964e8c3501171a5af168d0797464434421f Mon Sep 17 00:00:00 2001 From: Peter Barker Date: Thu, 22 Mar 2018 20:38:37 +1100 Subject: [PATCH] Sub: factor vehicle's mavlink send_heartbeat --- ArduSub/GCS_Mavlink.cpp | 47 ++++++++++++++++++++++++----------------- ArduSub/GCS_Mavlink.h | 4 ++++ ArduSub/Sub.h | 4 ++++ 3 files changed, 36 insertions(+), 19 deletions(-) diff --git a/ArduSub/GCS_Mavlink.cpp b/ArduSub/GCS_Mavlink.cpp index 3b12b0dc80..a448652ffe 100644 --- a/ArduSub/GCS_Mavlink.cpp +++ b/ArduSub/GCS_Mavlink.cpp @@ -25,16 +25,14 @@ void Sub::gcs_send_deferred(void) * pattern below when adding any new messages */ -NOINLINE void Sub::send_heartbeat(mavlink_channel_t chan) +MAV_TYPE GCS_MAVLINK_Sub::frame_type() const { - uint8_t base_mode = MAV_MODE_FLAG_CUSTOM_MODE_ENABLED; - uint8_t system_status = motors.armed() ? MAV_STATE_STANDBY : MAV_STATE_ACTIVE; - uint32_t custom_mode = control_mode; + return MAV_TYPE_SUBMARINE; +} - // set system as critical if any failsafe have triggered - if (failsafe.pilot_input || battery.has_failsafed() || failsafe.gcs || failsafe.ekf || failsafe.terrain) { - system_status = MAV_STATE_CRITICAL; - } +MAV_MODE GCS_MAVLINK_Sub::base_mode() const +{ + uint8_t base_mode = MAV_MODE_FLAG_STABILIZE_ENABLED; // work out the base_mode. This value is not very useful // for APM, but we calculate it as best we can so a generic @@ -44,8 +42,7 @@ NOINLINE void Sub::send_heartbeat(mavlink_channel_t chan) // only get useful information from the custom_mode, which maps to // the APM flight mode and has a well defined meaning in the // ArduPlane documentation - base_mode = MAV_MODE_FLAG_STABILIZE_ENABLED; - switch (control_mode) { + switch (sub.control_mode) { case AUTO: case GUIDED: case CIRCLE: @@ -63,21 +60,33 @@ NOINLINE void Sub::send_heartbeat(mavlink_channel_t chan) // override if stick mixing is enabled base_mode |= MAV_MODE_FLAG_MANUAL_INPUT_ENABLED; - // we are armed if we are not initialising - if (motors.armed()) { + if (sub.motors.armed()) { base_mode |= MAV_MODE_FLAG_SAFETY_ARMED; } // indicate we have set a custom mode base_mode |= MAV_MODE_FLAG_CUSTOM_MODE_ENABLED; - uint8_t mav_type; - mav_type = MAV_TYPE_SUBMARINE; + return (MAV_MODE)base_mode; +} - gcs().chan(chan-MAVLINK_COMM_0).send_heartbeat(mav_type, - base_mode, - custom_mode, - system_status); +uint32_t GCS_MAVLINK_Sub::custom_mode() const +{ + return sub.control_mode; +} + +MAV_STATE GCS_MAVLINK_Sub::system_status() const +{ + // set system as critical if any failsafe have triggered + if (sub.any_failsafe_triggered()) { + return MAV_STATE_CRITICAL; + } + + if (sub.motors.armed()) { + return MAV_STATE_ACTIVE; + } + + return MAV_STATE_STANDBY; } NOINLINE void Sub::send_attitude(mavlink_channel_t chan) @@ -500,7 +509,7 @@ bool GCS_MAVLINK_Sub::try_send_message(enum ap_message id) case MSG_HEARTBEAT: CHECK_PAYLOAD_SIZE(HEARTBEAT); last_heartbeat_time = AP_HAL::millis(); - sub.send_heartbeat(chan); + send_heartbeat(); sub.send_info(chan); break; diff --git a/ArduSub/GCS_Mavlink.h b/ArduSub/GCS_Mavlink.h index 93f1ea56dc..6763d1702a 100644 --- a/ArduSub/GCS_Mavlink.h +++ b/ArduSub/GCS_Mavlink.h @@ -38,4 +38,8 @@ private: void handle_change_alt_request(AP_Mission::Mission_Command &cmd) override; bool try_send_message(enum ap_message id) override; + MAV_TYPE frame_type() const override; + MAV_MODE base_mode() const override; + uint32_t custom_mode() const override; + MAV_STATE system_status() const override; }; diff --git a/ArduSub/Sub.h b/ArduSub/Sub.h index d1f09d888e..b8d90c4ec1 100644 --- a/ArduSub/Sub.h +++ b/ArduSub/Sub.h @@ -285,6 +285,10 @@ private: uint8_t sensor_health : 1; // true if at least one sensor has triggered a failsafe (currently only used for depth in depth enabled modes) } failsafe; + bool any_failsafe_triggered() const { + return (failsafe.pilot_input || battery.has_failsafed() || failsafe.gcs || failsafe.ekf || failsafe.terrain); + } + // sensor health for logging struct { uint8_t baro : 1; // true if any single baro is healthy