From 62acf660216ff78ff2b0613a26b7a9315bd0054d Mon Sep 17 00:00:00 2001 From: Mike Pehel Date: Fri, 21 Aug 2026 16:43:22 -0400 Subject: [PATCH] fix(lat_lon_alt): use fabs() in fromEcef(), std::abs() resolves to int abs() on NuttX (#28333) PX4 NuttX builds compile with -nostdinc++ and NuttX's own C++ headers, whose provides only the C int abs(int) and whose has no floating std::abs overloads. std::abs(p_ecef(2)) therefore binds to int abs(int) and the ECEF Z coordinate is truncated to whole metres on every call. LatLonAlt::fromEcef() is what SIH uses to turn its ECEF position into latitude and longitude each physics step, so on hardware the groundtruth and simulated GPS latitude advanced at sin^2(lat) of the true rate and jumped cos(lat) m every 1/cos(lat) m of northing. SITL links libstdc++ and was never affected. fabs() has only a floating signature, so the same source compiles correctly on every target. --- src/lib/lat_lon_alt/lat_lon_alt.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/lat_lon_alt/lat_lon_alt.cpp b/src/lib/lat_lon_alt/lat_lon_alt.cpp index 23746aa2726..5d0dd07a4e4 100644 --- a/src/lib/lat_lon_alt/lat_lon_alt.cpp +++ b/src/lib/lat_lon_alt/lat_lon_alt.cpp @@ -41,7 +41,7 @@ LatLonAlt LatLonAlt::fromEcef(const Vector3d &p_ecef) { // Convert position using Borkowski closed-form exact solution // P. D. Groves, "Principles of GNSS, inertial, and multisensor integrated navigation systems, 2nd edition (appendix C) - const double k1 = sqrt(1 - Wgs84::eccentricity2) * std::abs(p_ecef(2)); + const double k1 = sqrt(1 - Wgs84::eccentricity2) * fabs(p_ecef(2)); const double k2 = Wgs84::eccentricity2 * Wgs84::equatorial_radius; const double beta = sqrt(p_ecef(0) * p_ecef(0) + p_ecef(1) * p_ecef(1)); const double E = (k1 - k2) / beta;