Skip to content

Commit

Permalink
AutopilotTester: Speed up polling w speed factor
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mbjd committed Feb 3, 2025
1 parent 7576725 commit e16bf22
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions test/mavsdk_tests/autopilot_tester.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,22 @@ class AutopilotTester
{
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;

Expand Down Expand Up @@ -236,7 +245,14 @@ class AutopilotTester
bool poll_condition_with_timeout(
std::function<bool()> fun, std::chrono::duration<Rep, Period> 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);

Expand Down

0 comments on commit e16bf22

Please sign in to comment.