PX4 SITL target loses global position when the drone is being moved before takeoff

I’m spawning an Iris MC controlled by a px4_sitl_default target in gazebo on a conveyor belt which is 10m long. Then I use a MAVSDK script to make the drone arm and fly a mission as soon as it was moved 7m by the conveyor belt. Unfortunately, when the drone takes off, it reports this:

|[07:36:31|Debug] MAVLink: emergency: Navigation failure! Land and recalibrate sensors| (system_impl.cpp:239)|
|[07:36:31|Debug] MAVLink: critical: Failsafe enabled: no global position| (system_impl.cpp:239)|
|[07:36:31|Debug] MAVLink: info: Failsafe mode activated| (system_impl.cpp:239)|
|[07:36:37|Debug] MAVLink: critical: Compass needs calibration - Land now!| (system_impl.cpp:239)|

And since the global position is lost, the drone doesn’t fly the mission properly. Instead it just drifts out of sight or sometimes crashes into the ground.
The MAVSDK Script that I’m using, is basically just the fly_mission example from the MAVSDK repo. I only added a few lines to make the drone wait before taking off.
Does anyone know what is happening here? Do I need to reset the EKF somehow before taking off?

Here is a flight log of doing the same thing, but instead of using fly_mission I used takeoff_and_land , because in fly_mission no log is created, because the drone never lands.

Hi @max11gen,
On the log you shared, the drone isn’t moving horizontally (looking at the GNSS velocity report) and the local/global position is always valid. I don’t see the failure you mentioned.
The log should be there even if you don’t land.

Hi @bresch , Thanks a lot for answering. You are right, in that log there is no problem with the global position, sorry for that. And I also figured out why I couldn’t find the logs of the fly_mission flights. So here is another log showing my problem: https://logs.px4.io/plot_app?log=1a486ae1-bc59-44bd-9a21-ac72bdd5ddfa

The corresponding messages I received on the console this time are the following:

[02:31:42|Info ] MAVSDK version: v1.0.8 (mavsdk_impl.cpp:28)
Waiting to discover system...
[02:31:42|Info ] New system on: (with sysid: 1) (udp_connection.cpp:192)
[02:31:42|Debug] New: System ID: 1 Comp ID: 1 (mavsdk_impl.cpp:484)
[02:31:42|Debug] Component Autopilot (1) added. (system_impl.cpp:360)
[02:31:43|Debug] Discovered 1 component(s) (system_impl.cpp:528)
Discovered autopilot
Waiting for system to be ready
[02:31:43|Warn ] command denied (512). (mavlink_commands.cpp:180)
System ready
Creating and uploading mission
Uploading mission...
[02:31:44|Debug] Falling back to gimbal protocol v1 (mission_impl.cpp:124)
[02:31:44|Debug] MAVLink: info: Armed by external command	 (system_impl.cpp:239)
[02:31:44|Debug] current: 0, total: 6 (mission_impl.cpp:931)
Mission status update: 0 / 6
[02:31:44|Debug] MAVLink: info: Takeoff to 10.0 meters above home	 (system_impl.cpp:239)
[02:31:45|Debug] MAVLink: info: Takeoff detected	 (system_impl.cpp:239)
[02:32:06|Debug] MAVLink: critical: Compass needs calibration - Land now!	 (system_impl.cpp:239)
[02:32:12|Debug] MAVLink: critical: Compass needs calibration - Land now!	 (system_impl.cpp:239)
[02:32:36|Debug] MAVLink: critical: Failsafe enabled: no global position	 (system_impl.cpp:239)
[02:32:36|Debug] MAVLink: info: Failsafe mode activated	 (system_impl.cpp:239)
[02:32:46|Debug] MAVLink: info: Landing detected	 (system_impl.cpp:239)
[02:32:46|Debug] MAVLink: info: Failsafe mode deactivated	 (system_impl.cpp:239)
[02:32:47|Debug] MAVLink: info: Takeoff to 10.0 meters above home	 (system_impl.cpp:239)
[02:32:47|Debug] MAVLink: info: Takeoff detected	 (system_impl.cpp:239)

For this experiment I used the plain example code from MAVSDK fly_mission and just waited a couple of seconds until the conveyor had moved the drone approximately 7m before starting fly_mission. If I do not wait and just instantly launch fly_mission and the drone is still very close to the starting position, there are no problems.
Do you think uploading a screen video would help in making the problem more clear?

Here I uploaded another log of the same thing: https://logs.px4.io/plot_app?log=61889352-74cb-458c-b1dd-3c9c9a449c19 . As you can see, the drone does different things every time, but the reason seems to be a loss of the global position.

To me it looks like the drone doesn’t even takeoff at the end of the conveyor belt. The estimate seems good until that point, then the drone falls off and the collisions in the sim are probably making the whole thing go nuts. Have you tried to do the takeoff manually (i.e.: using a gamepad or virtual joysticks)?