From 6974c4a5cbcfd082a047ab177f4a6c5ea2ac60fa Mon Sep 17 00:00:00 2001 From: Balduin Date: Mon, 3 Feb 2025 12:02:06 +0100 Subject: [PATCH] AutopilotTester: Speed up polling w speed factor If sim is running faster, we should also poll faster to keep the same timing resolution. This fixed quite a large number of test failures. --- test/mavsdk_tests/autopilot_tester.h | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/test/mavsdk_tests/autopilot_tester.h b/test/mavsdk_tests/autopilot_tester.h index 8f57f68de1..ec130a1ef7 100644 --- a/test/mavsdk_tests/autopilot_tester.h +++ b/test/mavsdk_tests/autopilot_tester.h @@ -169,13 +169,22 @@ public: { const std::chrono::microseconds duration_us(duration); + // Hopefully this is often enough not to have PX4 time out on us. + auto realtime_sleep_duration = std::chrono::milliseconds(1); + + if (speed_factor.has_value()) { + // If sim is running faster than realtime, we need to + // speed up polling (which happens w.r.t. real time) to + // maintain the check resolution w.r.t. sim time + realtime_sleep_duration /= speed_factor.value(); + } + if (_telemetry && _telemetry->attitude_quaternion().timestamp_us != 0) { const int64_t start_time_us = _telemetry->attitude_quaternion().timestamp_us; while (true) { - // Hopefully this is often enough not to have PX4 time out on us. - std::this_thread::sleep_for(std::chrono::milliseconds(1)); + std::this_thread::sleep_for(realtime_sleep_duration); const int64_t elapsed_time_us = _telemetry->attitude_quaternion().timestamp_us - start_time_us; @@ -237,7 +246,14 @@ private: bool poll_condition_with_timeout( std::function fun, std::chrono::duration duration) { - static constexpr unsigned check_resolution = 100; + unsigned check_resolution = 100; + + if (speed_factor.has_value()) { + // If sim is running faster than realtime, we need to + // speed up polling (which happens w.r.t. real time) to + // maintain the check resolution w.r.t. sim time + check_resolution *= speed_factor.value(); + } const std::chrono::microseconds duration_us(duration);