Attach GPS to PX4 sitl simulated on gz sim

Hello everyone, I’m trying to connect a GPS sensor, simulated by gz sim, to a px4 sitl instance. I already achieved the connection of imu, compass and barometer exposing the correct topic names in the gazebo simulation according to the GZ_bridge requirements. Unfortunately the connection of the GPS seems to require a different procedure since, looking at the bridge source code, it does not take into account a GPS topic.
Do you have any ideas to get it connected? Should I try to modify the bridge itself or is there an official procedure?

Bridge source code: PX4-Autopilot/src/modles/simulation/gz_bridge/GZBridge.cpp

PX4 version: 1.14.0
Gazebo garden, Ros2 humble, Ubuntu

Thank you in advance for your time

In your airframe, I believe you should set the following parameter and it should work out of the box:

param set-default SENS_EN_GPSSIM 1

What model are you trying to simulate?

I am not sure if you implemented the other sensors yourself, but there are also parameters for those sensors that should work out of the box:

param set-default SIM_GZ_EN 1

param set-default SENS_EN_GPSSIM 1

param set-default SENS_EN_BAROSIM 0

param set-default SENS_EN_MAGSIM 1

param set-default SENS_EN_ARSPDSIM 1

Thank you so much for the quick reply!
The drone I’m trying to simulate is a quadrotor and the airframe I use differ from the default one with id=4001 (x500) only for the PX4_* environment variables. So I should have set all the parameter you mentioned, except the ARSPDSIM that I don’t use.

I add some details that hopefully will help to understand my project state.
To start the simulation first I run gz sim (in play) with the model of the world (with spherical coordinates set) AND the drone. Then I run the px4 executable (build/px4_sitl_default/bin/px4) compiled with make px4_sitl, setting PX4_SYS_AUTOSTART and PX4_GZ_MODEL_NAME environment variables. Finally, opening QGroundControl everything but the gps location seems to be fine. The gps status is up, but the coordinates the sensor provides to px4 are the default ones in Zurigo even if gz topic returns the correct ones.

Home position is controller by parameters of env variables.
Take a look at

Hi Benja, thank you for the reply.
I agree with you that the code you linked suggests to use the bash variables PX4_HOME_LAT/LON/ALT to configure the global initial position of the drone. Unfortunately I already tried to use them without any success. In particular I’m using this command to start px4:
PX4_SYS_AUTOSTART=4001 PX4_GZ_MODEL_NAME=Drone PX4_HOME_LAT=XX.xxxx PX4_HOME_LON=YY.yyyy PX4_HOME_ALT=ZZZ.zzzz ./path-to-px4-repo/PX4-Autopilot/build/px4_sitl_default/bin/px4. Autostart and model name variables work correctly, but the px4 sitl has still the gps origin and home in Zurich.

I also tried to modify both home and gps origin publishing the right VehicleCommand topic, but after the modification, the drone odometry inherits the distance vector between my new origin location and Zurich, keeping the drone at Zurich.

I have found a dirty workaround for the problem: creating a custom airframe, where SIM_GZ_HOME_LAT/LON/ALT are configured, it is possible to get a single gazebo world working. But still I don’t understand why this doesn’t work using environment variables.

Anyway, I’m still looking for a clean solution that sets the global position of px4 sitl looking at the gps coordinates provided by gz sim depending on the world loaded.

I’ve stumbled on this post and figured something I’ve struggled with! thank you for saying about the parameters! it seems they are not documented in the reference page

thank you!

For simulation, I used ros_gz bridge for the x500_depth and it worked fine.

ros2 run ros_gz_bridge parameter_bridge /world/<your_world>/model/x500_depth_0/link/base_link/sensor/imu_sensor/imu@sensor_msgs/msg/Imu@gz.msgs.IMU