Unable to setup Compass Sensor for simulation

I’m attempting to use an x500 with Gazebo simulator. However, I get this error when running the simulation:

INFO:gazebo.stream_logger.x500_0.px4:stdout: WARN [health_and_arming_checks] Preflight Fail: Compass Sensor 0 missing

I have added a magnetometer to the x500 model.sdf following SDF 1.9 (SDFormat Specification):

      <sensor name="magnetometer_sensor" type="magnetometer">
        <magnetometer>
          <x>
            <noise type="gaussian">
              <mean>0</mean>
              <stddev>0.00018665</stddev>
              <dynamic_bias_stddev>3.8785e-05</dynamic_bias_stddev>
              <dynamic_bias_correlation_time>1000</dynamic_bias_correlation_time>
            </noise>
          </x>
          <y>
            <noise type="gaussian">
              <mean>0</mean>
              <stddev>0.00018665</stddev>
              <dynamic_bias_stddev>3.8785e-05</dynamic_bias_stddev>
              <dynamic_bias_correlation_time>1000</dynamic_bias_correlation_time>
            </noise>
          </y>
          <z>
            <noise type="gaussian">
              <mean>0</mean>
              <stddev>0.00018665</stddev>
              <dynamic_bias_stddev>3.8785e-05</dynamic_bias_stddev>
              <dynamic_bias_correlation_time>1000</dynamic_bias_correlation_time>
            </noise>
          </z>
        </magnetometer>
      </sensor>

I have followed the same process as the IMU which seems to be working fine.
Any help would be much appreciated.

I think you don’t need to add it. The gz simulation should still work. Because currently, mag sensor data is generated using GPS data during gz simulation.(see px4 code:PX4-Autopilot/src/modules/simulation/sensor_mag_sim/SensorMagSim.cpp at 6b0ac49daf5c49aeb625ed684359fdd59b1a287d · PX4/PX4-Autopilot · GitHub)

Even if you want to add a separate geomagnetic sensor, in addition to the changes you made above. We also need to add

Mag sensor: /world/$WORLD/model/$MODEL/link/base_link/sensor/
magnetometer_sensor/magnetometer
std::string air_pressure_topic = “/world/” + _world_name + “/model/” + _model_name + “/link/base_link/sensor/magnetometer_sensor/magnetometer”;

if (!_node.Subscribe(magnetometer_topic, &GZBridge::magnetometerCallback, this)) {
return PX4_ERROR;
}

to GZBridge.cpp.

There is a similar situation here: [Bug] Sometimes topic sensor_gps not updated in gz simulation · Issue #23130 · PX4/PX4-Autopilot · GitHub



I wrote some code to implement the use of the magnetometer module in Gazebo and tested it for takeoff, and it seems to be working normaly.

Thanks for your help. The solution for me was to remove the magnetometer sensor altogether, and add the navsat plugin to the model file e.g.
<plugin name='gz::sim::systems::NavSat' filename='gz-sim-navsat-system'/>