Skip to content

Commit

Permalink
GCS_MAVLink: Check if set_message_interval is too fast
Browse files Browse the repository at this point in the history
  • Loading branch information
stephendade committed Sep 23, 2024
1 parent 44c1e9b commit a660105
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
6 changes: 6 additions & 0 deletions libraries/GCS_MAVLink/GCS.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <AP_AHRS/AP_AHRS_config.h>
#include <AP_Arming/AP_Arming_config.h>
#include <AP_Airspeed/AP_Airspeed_config.h>
#include <AP_Scheduler/AP_Scheduler.h>

#include "ap_message.h"

Expand Down Expand Up @@ -267,6 +268,11 @@ class GCS_MAVLINK
// accessor for uart
AP_HAL::UARTDriver *get_uart() { return _port; }

#if AP_SCHEDULER_ENABLED
// 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;
#endif

virtual uint8_t sysid_my_gcs() const = 0;
virtual bool sysid_enforce() const { return false; }

Expand Down
22 changes: 17 additions & 5 deletions libraries/GCS_MAVLink/GCS_Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,16 @@ 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*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)
Expand Down Expand Up @@ -1652,11 +1662,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:
Expand Down Expand Up @@ -3127,6 +3133,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 (cap_message_interval(interval_ms) > interval_ms) {
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "Requested message rate too fast. Increase SCHED_LOOP_RATE");
return MAV_RESULT_DENIED;
}
#endif
if (set_ap_message_interval(id, interval_ms)) {
return MAV_RESULT_ACCEPTED;
}
Expand Down

0 comments on commit a660105

Please sign in to comment.