fix(gz_bridge): use the corrected Gazebo magnetometer frame and units (#28877)

* fix(gz_bridge): use the corrected Gazebo magnetometer frame and units

The Gazebo magnetometer system defaults to a legacy mode: it computes the
field in NED, places it into the ENU world frame without converting, and
reports gauss. The bridge swapped body axes to compensate, but no body-axis
swap undoes a mirrored world-frame field at every attitude. At the default
world location this left the heading about 6 deg off on the ground.

gz-sim 8.6 added use_units_gauss and use_earth_frame_ned (gz-sim#2460).
Turn both off in server.config, so the field is in tesla and physically
correct in the sensor's FLU frame, and convert it like the IMU: FLU -> FRD,
tesla -> gauss.

Other ways of starting gz (standalone mode, gz-sim < 8.6, worlds with their
own systems) still deliver the legacy output. Detect it from the field
magnitude, keep the previous mapping for it and warn once.

Needs the x500 (and other) model magnetometer noise in tesla, see
PX4-gazebo-models.

* docs(gz_bridge): describe the legacy magnetometer frame accurately

NED components on ENU axes is a 180 degree rotation about the horizontal
axis between north and east, not a mirror.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Jacob Dahl <dahl.jakejacob@gmail.com>

* build(simulation): bump gz models for magnetometer noise in tesla

The gz_bridge server.config now runs the Magnetometer system in tesla, so
the models need their noise in tesla too, otherwise it is 1 gauss and PX4
refuses to arm. Also pulls in the advanced_plane parachute plugin and the
default world elevation fix.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Jacob Dahl <dahl.jakejacob@gmail.com>

---------

Signed-off-by: Jacob Dahl <dahl.jakejacob@gmail.com>
Co-authored-by: matteo <197973584+megavedl@users.noreply.github.com>
Co-authored-by: Jacob Dahl <dahl.jakejacob@gmail.com>
This commit is contained in:
authored and GitHub committed 2026-09-30 16:45:43 -06:00
1 parent c7db242da4
commit 0462e3a31e
3 files changed
+33 -6

No files matched your search

+27 -4
View File
@@ -400,10 +400,33 @@ void GZBridge::magnetometerCallback(const gz::msgs::Magnetometer &msg)
_px4_mag.set_temperature(_temperature); // this will be static if no airspeed sensor is on the model.
// FIXME: once we're on jetty or later
// The magnetometer plugin publishes in units of gauss and in a weird left handed coordinate system
// https://github.com/gazebosim/gz-sim/pull/2460
_px4_mag.update(timestamp, -msg.field_tesla().y(), -msg.field_tesla().x(), msg.field_tesla().z());
// The field is in tesla and in the sensor's FLU frame if the Magnetometer system is loaded with
// use_units_gauss=false and use_earth_frame_ned=false (server.config, gz-sim >= 8.6).
// Other setups (standalone gz, older gz-sim, worlds with their own systems) still get the legacy
// output: gauss, with the NED field components placed on the ENU world axes.
// Earth's field is 2.2e-5 to 6.7e-5 T (0.22 to 0.67 G), so a magnitude above 1e-2 can only be gauss.
const gz::math::Vector3d field(msg.field_tesla().x(), msg.field_tesla().y(), msg.field_tesla().z());
if (field.Length() > 1e-2) {
static bool legacy_warned = false;
if (!legacy_warned) {
PX4_WARN("gz magnetometer is in legacy mode (gauss, NED components on ENU axes), heading will be wrong. "
"Set use_units_gauss and use_earth_frame_ned to false for the Magnetometer system");
legacy_warned = true;
}
_px4_mag.update(timestamp, -field.Y(), -field.X(), field.Z());
return;
}
// Rotate FLU to FRD like the IMU and convert tesla to gauss
static const auto q_FLU_to_FRD = gz::math::Quaterniond(0, 1, 0, 0);
static constexpr double TESLA_TO_GAUSS = 1e4;
const gz::math::Vector3d field_frd = q_FLU_to_FRD.RotateVector(field) * TESLA_TO_GAUSS;
_px4_mag.update(timestamp, field_frd.X(), field_frd.Y(), field_frd.Z());
}
void GZBridge::airPressureCallback(const gz::msgs::FluidPressure &msg)
@@ -10,7 +10,11 @@
<plugin entity_name="*" entity_type="world" filename="gz-sim-apply-link-wrench-system" name="gz::sim::systems::ApplyLinkWrench"/>
<plugin entity_name="*" entity_type="world" filename="gz-sim-wind-effects-system" name="gz::sim::systems::WindEffects"/>
<plugin entity_name="*" entity_type="world" filename="gz-sim-navsat-system" name="gz::sim::systems::NavSat"/>
<plugin entity_name="*" entity_type="world" filename="gz-sim-magnetometer-system" name="gz::sim::systems::Magnetometer"/>
<plugin entity_name="*" entity_type="world" filename="gz-sim-magnetometer-system" name="gz::sim::systems::Magnetometer">
<!-- Field in tesla, in the sensor frame with a correct earth frame (gz-sim PR #2460) -->
<use_units_gauss>false</use_units_gauss>
<use_earth_frame_ned>false</use_earth_frame_ned>
</plugin>
<plugin entity_name="*" entity_type="world" filename="gz-sim-sensors-system" name="gz::sim::systems::Sensors">
<render_engine>ogre2</render_engine>
</plugin>