How to set GPS coordinates via MAVLink?


How to set GPS coordinates (lat, lon, alt, speed, etc) via MAVLink for px4? I’d like to set position for px4 without gps-reciever. Can I use for this purpose HIL_GPS message in real MAV?


1 Like

You can’t use HIL_GPS.

Depending on how you are providing position source, you might use the ATT_POS_MOCAP message - see and


Thanks, @hamishwillee .

I am also curious about this question. Can you explain why we can’t use HIL_GPS?

I noticed something interesting that in the Simulation page:

All simulators communicate with PX4 using the Simulator MAVLink API. This API defines a set of MAVLink messages that supply sensor data from the simulated world to PX4 and return motor and actuator values from the flight code that will be applied to the simulated vehicle. The image below shows the message flow.

I am curious why we can’t pass GPS coordinates through MAVLink?

  • SITL Simulation Environment

PX4 uses a simulation-specific module to listen on UDP port 14560. Simulators connect to this port, then exchange information using the Simulator MAVLink API described above. PX4 on SITL and the simulator can run on either the same computer or different computers on the same network.

As we know, the QGC can set GPS, so why can’t we use the simulator to set GPS ?

Thanks for your attention.


You can’t pass those messages via mavlink because they are enabled for simulation only. The simulator handler file that deals with this message in the diagram above is called simulator_mavlink.cpp - that isn’t even built into PX4 on real hardware. In a HITL build a message handler does exist but all the PWM outputs are disabled.

As we know, the QGC can set GPS, so why can’t we use the simulator to set GPS ?

We don’t know that.

What use case are you trying to meet - ie why do you need to do this? I ask because the normal reason is for indoor flight, and if that is the case then the right way to do this is normally via the messages I pointed you to.


Hello, I am doing pretty similar think. At this moment I have a model, that uses GPS until it goes inside of a sphere where no GPS data are available because of the noise source. I am searching for the solution how to get a switchable position source, for example: at mission or offboard control to have a possibility to switch at one point from GPS to vision estimation or something similar to that idea. I know how to make it for hardware purpose (I think I know), but for presentation of SITL I would like to obtain the possibility of switching. Could you propose me a solution or the idea where to start digging? I am using PX4/MAVROS/DroneCode SDK with OFFBoard control for mission control


@arcematrix For SITL, isn’t GPS always provided even when you’re nominally flying underground? i.e. are you sure this is really a problem for presentation purposes?