diff --git a/cmake/px4_add_gtest.cmake b/cmake/px4_add_gtest.cmake index d7aa3c6e16c..c0e457d2ea5 100644 --- a/cmake/px4_add_gtest.cmake +++ b/cmake/px4_add_gtest.cmake @@ -153,7 +153,15 @@ function(px4_add_functional_gtest) endif() # whole-archive so the daemon app-map stubs link even for small # test libs, where link order would otherwise drop them - list(APPEND _FUNCTIONAL_GTEST_LIBS -Wl,--whole-archive test_stubs -Wl,--no-whole-archive) + if(APPLE) + # ld64 has no --whole-archive, -force_load takes the archive path instead. + # test_stubs is also listed as a link library so the executable relinks + # when the archive changes: a $ inside a link flag is only + # an order-only dependency and would leave stale stubs in the binary. + list(APPEND _FUNCTIONAL_GTEST_LIBS test_stubs "-Wl,-force_load,$") + else() + list(APPEND _FUNCTIONAL_GTEST_LIBS -Wl,--whole-archive test_stubs -Wl,--no-whole-archive) + endif() target_link_libraries(${TESTNAME} PRIVATE ${_FUNCTIONAL_GTEST_LIBS}) target_compile_definitions(${TESTNAME} PRIVATE MODULE_NAME="${TESTNAME}") diff --git a/src/lib/adsb/AdsbConflictTest.cpp b/src/lib/adsb/AdsbConflictTest.cpp index f99f13ba88a..6c879026afe 100644 --- a/src/lib/adsb/AdsbConflictTest.cpp +++ b/src/lib/adsb/AdsbConflictTest.cpp @@ -122,19 +122,20 @@ TEST_F(AdsbConflictTest, RejectsInvalidAircraftState) detect_and_avoid_s daa_output{}; transponder_report_s report = create_relative_report(valid_traffic); const float nan = std::numeric_limits::quiet_NaN(); + const double nan_d = std::numeric_limits::quiet_NaN(); const float inf = std::numeric_limits::infinity(); // non-finite ownship coordinate - EXPECT_FALSE(adsb_conflict.calculate_daa_output(create_daa_input(matrix::Vector2d(nan, uav_lat_lon_(1)), uav_alt_, + EXPECT_FALSE(adsb_conflict.calculate_daa_output(create_daa_input(matrix::Vector2d(nan_d, uav_lat_lon_(1)), uav_alt_, stationary_uav_vel_ned_, report), daa_output)); - EXPECT_FALSE(adsb_conflict.calculate_daa_output(create_daa_input(matrix::Vector2d(uav_lat_lon_(0), nan), uav_alt_, + EXPECT_FALSE(adsb_conflict.calculate_daa_output(create_daa_input(matrix::Vector2d(uav_lat_lon_(0), nan_d), uav_alt_, stationary_uav_vel_ned_, report), daa_output)); EXPECT_FALSE(adsb_conflict.calculate_daa_output(create_daa_input(matrix::Vector2d(90.1, uav_lat_lon_(1)), uav_alt_, stationary_uav_vel_ned_, report), daa_output)); EXPECT_FALSE(adsb_conflict.calculate_daa_output(create_daa_input(matrix::Vector2d(uav_lat_lon_(0), -180.1), uav_alt_, stationary_uav_vel_ned_, report), daa_output)); - report.lat = nan; + report.lat = nan_d; EXPECT_FALSE(adsb_conflict.calculate_daa_output(create_daa_input(uav_lat_lon_, uav_alt_, stationary_uav_vel_ned_, report), daa_output)); report = create_relative_report(valid_traffic); diff --git a/src/lib/cdev/posix/cdev_platform.cpp b/src/lib/cdev/posix/cdev_platform.cpp index da40d782723..9fbccceef95 100644 --- a/src/lib/cdev/posix/cdev_platform.cpp +++ b/src/lib/cdev/posix/cdev_platform.cpp @@ -392,9 +392,18 @@ extern "C" { if (timeout > 0) { // Get the current time struct timespec ts; +#if defined(__PX4_DARWIN) && !defined(ENABLE_LOCKSTEP_SCHEDULER) + // macOS has no pthread_condattr_setclock(), so the condition variable + // behind px4_sem_t waits on CLOCK_REALTIME (see px4_sem_init()). The + // deadline has to be taken from that same clock: CLOCK_MONOTONIC counts + // from boot, so passing it here puts the deadline decades in the past and + // px4_sem_timedwait() returns ETIMEDOUT immediately instead of waiting. + px4_clock_gettime(CLOCK_REALTIME, &ts); +#else // Note, we can't actually use CLOCK_MONOTONIC on macOS // but that's hidden and implemented in px4_clock_gettime. px4_clock_gettime(CLOCK_MONOTONIC, &ts); +#endif // Calculate an absolute time in the future const unsigned billion = (1000 * 1000 * 1000); diff --git a/src/lib/pure_pursuit/PurePursuitTest.cpp b/src/lib/pure_pursuit/PurePursuitTest.cpp index d1e1c1784b8..a0b58cfedfb 100644 --- a/src/lib/pure_pursuit/PurePursuitTest.cpp +++ b/src/lib/pure_pursuit/PurePursuitTest.cpp @@ -150,7 +150,9 @@ TEST(PurePursuitTest, OutOfLookahead) Vector2f(0.f, 0.f), Vector2f(10.f, 20.f), lookahead_distance); EXPECT_NEAR(target_bearing1, M_PI_2_F + M_PI_4_F, FLT_EPSILON); // Fallback: Bearing to closest point on path - EXPECT_NEAR(target_bearing2, -M_PI_F, FLT_EPSILON); // Fallback: Bearing to closest point on path + // -pi and +pi are the same bearing, and which side of the branch cut the arithmetic + // lands on is not portable, so compare the wrapped difference instead of the raw value. + EXPECT_NEAR(wrap_pi(target_bearing2 - (-M_PI_F)), 0.f, 1e-6f); // Fallback: Bearing to closest point on path EXPECT_NEAR(target_bearing3, M_PI_F - atan2f(10, 10), FLT_EPSILON); // Fallback: Bearing to previous waypoint EXPECT_NEAR(target_bearing4, -M_PI_F + atan2f(10, 10), FLT_EPSILON); // Fallback: Bearing to current waypoint } @@ -202,7 +204,8 @@ TEST(PurePursuitTest, CurrAndPrevSameNorthCoordinate) EXPECT_NEAR(target_bearing1, M_PI_2_F, FLT_EPSILON); EXPECT_NEAR(target_bearing2, M_PI_2_F + M_PI_4_F, FLT_EPSILON); EXPECT_NEAR(target_bearing3, -(M_PI_2_F + M_PI_4_F), FLT_EPSILON); - EXPECT_NEAR(target_bearing4, -M_PI_F, FLT_EPSILON); // Fallback: Bearing to closest point on path + // See above: compare across the +-pi branch cut with the wrapped difference. + EXPECT_NEAR(wrap_pi(target_bearing4 - (-M_PI_F)), 0.f, 1e-6f); // Fallback: Bearing to closest point on path } TEST(PurePursuitTest, CrosstrackError) diff --git a/src/modules/navigator/DetectAndAvoid/test/detect_and_avoid_test_common.h b/src/modules/navigator/DetectAndAvoid/test/detect_and_avoid_test_common.h index 1dc4b15f9e9..2b03b8c1900 100644 --- a/src/modules/navigator/DetectAndAvoid/test/detect_and_avoid_test_common.h +++ b/src/modules/navigator/DetectAndAvoid/test/detect_and_avoid_test_common.h @@ -250,7 +250,7 @@ public: _vehicle_local_position_pub.publish(l_pos); } - void publish_global_pos(const double lat, const double lon, const double alt, + void publish_global_pos(const double lat, const double lon, const float alt, const hrt_abstime timestamp = hrt_absolute_time(), const bool lat_lon_valid = true, const bool alt_valid = true) { diff --git a/src/modules/navigator/RTLPlanner/GeofenceAvoidancePlannerTest.cpp b/src/modules/navigator/RTLPlanner/GeofenceAvoidancePlannerTest.cpp index fdada85c609..322606e113f 100644 --- a/src/modules/navigator/RTLPlanner/GeofenceAvoidancePlannerTest.cpp +++ b/src/modules/navigator/RTLPlanner/GeofenceAvoidancePlannerTest.cpp @@ -281,8 +281,8 @@ TEST_F(GeofenceAvoidancePlannerTest, NanStartOrDestination) { using namespace matrix; Vector2 valid(47.3977, 8.5456); - Vector2 nan_lat(NAN, 8.5456); - Vector2 nan_lon(47.3977, NAN); + Vector2 nan_lat(static_cast(NAN), 8.5456); + Vector2 nan_lon(47.3977, static_cast(NAN)); FakeGeofence fake(nullptr, 0, NAV_CMD_FENCE_POLYGON_VERTEX_INCLUSION); _planner.updateGraphFromGeofence(fake, 0.f); diff --git a/src/modules/navigator/test/test_RTL.cpp b/src/modules/navigator/test/test_RTL.cpp index 5d0f19bc404..6d1ff451a39 100644 --- a/src/modules/navigator/test/test_RTL.cpp +++ b/src/modules/navigator/test/test_RTL.cpp @@ -71,6 +71,7 @@ namespace constexpr double kBaseLat = 47.397742; constexpr double kBaseLon = 8.545594; constexpr float kAlt = 500.f; +constexpr double kNanDouble = static_cast(NAN); constexpr float kApproachRadius = 50.f; mission_item_s makeSafePointItem(double lat, double lon, float altitude, NAV_FRAME frame, @@ -996,8 +997,8 @@ ExtractValidSafePointPositionCase{ makeSafePointItem(kBaseLat, kBaseLon, 25.f, NAV_FRAME_GLOBAL_RELATIVE_ALT), NAN, false, - NAN, - NAN, + kNanDouble, + kNanDouble, NAN, }, ExtractValidSafePointPositionCase{ @@ -1005,8 +1006,8 @@ ExtractValidSafePointPositionCase{ makeSafePointItem(kBaseLat, kBaseLon, 510.f, NAV_FRAME_MISSION), kAlt, false, - NAN, - NAN, + kNanDouble, + kNanDouble, NAN, }, ExtractValidSafePointPositionCase{ @@ -1014,17 +1015,17 @@ ExtractValidSafePointPositionCase{ makeSafePointItem(kBaseLat, kBaseLon, 510.f, NAV_FRAME_GLOBAL, NAV_CMD_WAYPOINT), kAlt, false, - NAN, - NAN, + kNanDouble, + kNanDouble, NAN, }, ExtractValidSafePointPositionCase{ "NanLatitude", - makeSafePointItem(NAN, kBaseLon, 510.f, NAV_FRAME_GLOBAL), + makeSafePointItem(kNanDouble, kBaseLon, 510.f, NAV_FRAME_GLOBAL), kAlt, false, - NAN, - NAN, + kNanDouble, + kNanDouble, NAN, }, ExtractValidSafePointPositionCase{ @@ -1032,8 +1033,8 @@ ExtractValidSafePointPositionCase{ makeSafePointItem(0.0, 0.0, 510.f, NAV_FRAME_GLOBAL), kAlt, false, - NAN, - NAN, + kNanDouble, + kNanDouble, NAN, }, ExtractValidSafePointPositionCase{ @@ -1041,8 +1042,8 @@ ExtractValidSafePointPositionCase{ makeSafePointItem(91.0, kBaseLon, 510.f, NAV_FRAME_GLOBAL), kAlt, false, - NAN, - NAN, + kNanDouble, + kNanDouble, NAN, }, ExtractValidSafePointPositionCase{ @@ -1050,8 +1051,8 @@ ExtractValidSafePointPositionCase{ makeSafePointItem(kBaseLat, 181.0, 510.f, NAV_FRAME_GLOBAL), kAlt, false, - NAN, - NAN, + kNanDouble, + kNanDouble, NAN, }), [](const ::testing::TestParamInfo ¶m_info) diff --git a/src/modules/sensors/vehicle_gps_position/gps_blending.cpp b/src/modules/sensors/vehicle_gps_position/gps_blending.cpp index ea42cd70477..817fb71ed9d 100644 --- a/src/modules/sensors/vehicle_gps_position/gps_blending.cpp +++ b/src/modules/sensors/vehicle_gps_position/gps_blending.cpp @@ -391,6 +391,15 @@ sensor_gps_s GpsBlending::gps_blend_states(float blend_weights[GPS_MAX_RECEIVERS gps_blended_state.vel_e_m_s = 0; gps_blended_state.vel_d_m_s = 0; + // Accumulate the blended timestamps in double and round once at the end: casting each + // weighted term to uint64_t truncates it, which biases the result low by up to one + // microsecond per contributing receiver. Dividing by the accumulated weight also removes + // the float rounding error in the normalised weights, so receivers reporting an identical + // timestamp blend back to exactly that timestamp. + double blended_timestamp_us = 0.0; + double blended_timestamp_sample_us = 0.0; + double sum_of_timing_weights = 0.0; + // combine the the GPS states into a blended solution using the weights calculated in calc_blend_weights() for (uint8_t i = 0; i < GPS_MAX_RECEIVERS_BLEND; i++) { // Assume blended error magnitude, DOP and sat count is equal to the best value from contributing receivers @@ -398,8 +407,10 @@ sensor_gps_s GpsBlending::gps_blend_states(float blend_weights[GPS_MAX_RECEIVERS if (blend_weights[i] > 0.0f) { // blend the timing data - gps_blended_state.timestamp += (uint64_t)((double)_gps_state[i].timestamp * (double)blend_weights[i]); - gps_blended_state.timestamp_sample += (uint64_t)((double)_gps_state[i].timestamp_sample * (double)blend_weights[i]); + const double timing_weight = (double)blend_weights[i]; + blended_timestamp_us += (double)_gps_state[i].timestamp * timing_weight; + blended_timestamp_sample_us += (double)_gps_state[i].timestamp_sample * timing_weight; + sum_of_timing_weights += timing_weight; // calculate a blended average speed and velocity vector gps_blended_state.vel_m_s += _gps_state[i].vel_m_s * blend_weights[i]; @@ -451,6 +462,11 @@ sensor_gps_s GpsBlending::gps_blend_states(float blend_weights[GPS_MAX_RECEIVERS } + if (sum_of_timing_weights > 0.0) { + gps_blended_state.timestamp = (uint64_t)llround(blended_timestamp_us / sum_of_timing_weights); + gps_blended_state.timestamp_sample = (uint64_t)llround(blended_timestamp_sample_us / sum_of_timing_weights); + } + /* * Calculate the instantaneous weighted average location using available GPS instances and store in _gps_state. * This is statistically the most likely location, but may not be stable enough for direct use by the EKF. diff --git a/src/systemcmds/tests/test_uart_send.c b/src/systemcmds/tests/test_uart_send.c index ae37aa1e9b4..ef1dd307b2d 100644 --- a/src/systemcmds/tests/test_uart_send.c +++ b/src/systemcmds/tests/test_uart_send.c @@ -80,7 +80,13 @@ int test_uart_send(int argc, char *argv[]) uint64_t start_time = hrt_absolute_time(); for (i = 0; i < 30000; i++) { - n = sprintf(sample_test_uart, "SAMPLE #%d\n", i); + n = snprintf(sample_test_uart, sizeof(sample_test_uart), "SAMPLE #%d\n", i); + + // snprintf() returns the length it would have written, clamp to what fit + if (n > (int)sizeof(sample_test_uart) - 1) { + n = (int)sizeof(sample_test_uart) - 1; + } + write(test_uart, sample_test_uart, n); }