From 9a563e222f9f321d3d88d0564750ad63598ecc57 Mon Sep 17 00:00:00 2001 From: Stephen Dade Date: Wed, 18 Sep 2024 20:34:04 +1000 Subject: [PATCH] GCS_MAVLink: Check if set_message_interval is too fast --- libraries/GCS_MAVLink/GCS.h | 3 +++ libraries/GCS_MAVLink/GCS_Common.cpp | 26 +++++++++++++++++++++----- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/libraries/GCS_MAVLink/GCS.h b/libraries/GCS_MAVLink/GCS.h index fc677dfec0..8edbc60474 100644 --- a/libraries/GCS_MAVLink/GCS.h +++ b/libraries/GCS_MAVLink/GCS.h @@ -267,6 +267,9 @@ public: // accessor for uart AP_HAL::UARTDriver *get_uart() { return _port; } + // cap the MAVLink message rate. It can't be greater than 0.8 * SCHED_LOOP_RATE + uint16_t cap_message_interval(uint16_t interval_ms) const; + virtual uint8_t sysid_my_gcs() const = 0; virtual bool sysid_enforce() const { return false; } diff --git a/libraries/GCS_MAVLink/GCS_Common.cpp b/libraries/GCS_MAVLink/GCS_Common.cpp index a15c5a6e8a..743b8b7e61 100644 --- a/libraries/GCS_MAVLink/GCS_Common.cpp +++ b/libraries/GCS_MAVLink/GCS_Common.cpp @@ -225,6 +225,20 @@ void GCS_MAVLINK::send_power_status(void) hal.analogin->power_status_flags()); } +#if AP_SCHEDULER_ENABLED +// cap the MAVLink message rate. It can't be greater than 0.8 * SCHED_LOOP_RATE +uint16_t GCS_MAVLINK::cap_message_interval(uint16_t interval_ms) const +{ + if (interval_ms == 0) { + return 0; + } + if (interval_ms*800 < AP::scheduler().get_loop_period_us()) { + return AP::scheduler().get_loop_period_us()/800.0f; + } + return interval_ms; +} +#endif + #if HAL_WITH_MCU_MONITORING // report MCU voltage/temperature status void GCS_MAVLINK::send_mcu_status(void) @@ -1657,11 +1671,7 @@ bool GCS_MAVLINK::set_ap_message_interval(enum ap_message id, uint16_t interval_ } #if AP_SCHEDULER_ENABLED - // send messages out at most 80% of main loop rate - if (interval_ms != 0 && - interval_ms*800 < AP::scheduler().get_loop_period_us()) { - interval_ms = AP::scheduler().get_loop_period_us()/800.0f; - } + interval_ms = cap_message_interval(interval_ms); #endif // check if it's a specially-handled message: @@ -3143,6 +3153,12 @@ MAV_RESULT GCS_MAVLINK::set_message_interval(uint32_t msg_id, int32_t interval_u } else { interval_ms = interval_us / 1000; } +#if AP_SCHEDULER_ENABLED + if (interval_ms != 0 && cap_message_interval(interval_ms) > interval_ms) { + GCS_SEND_TEXT(MAV_SEVERITY_INFO, "Requested rate for message ID %u too fast. Increase SCHED_LOOP_RATE", (unsigned int)msg_id); + return MAV_RESULT_DENIED; + } +#endif if (set_ap_message_interval(id, interval_ms)) { return MAV_RESULT_ACCEPTED; }