Spoof GPS with Python - SITL

Hi,
I’m trying to spoof the GPS location of my drone. I use PX4 Autopilot, jMavSim Simulator and QGroundControl (I do not have access to a physical drone). What I’m trying is to introduce an offset to the actual position. This video shows an example: https://youtu.be/llN4zfE306Y
What I see is that they used MavProxy however what I found is that PX4 does not work with the gps_input command from mavlink.
Here is an example of my attempt:

import time
from pymavlink import mavutil
from MAVProxy.modules.lib import mp_module
from MAVProxy.modules.lib import mp_settings

master = mavutil.mavlink_connection('udpin:127.0.0.1:14540') # Create the connection

master.wait_heartbeat() # Wait a heartbeat before sending commands
def get_gps_time(tnow):
        '''return gps_week and gps_week_ms for current time'''
        leapseconds = 18
        SEC_PER_WEEK = 7 * 86400
        epoch = 86400*(10*365 + (1980-1969)/4 + 1 + 6 - 2) - leapseconds
        epoch_seconds = int(tnow - epoch)
        week = int(epoch_seconds) // SEC_PER_WEEK
        t_ms = int(tnow * 1000) % 1000
        week_ms = (epoch_seconds % SEC_PER_WEEK) * 1000 + ((t_ms//200) * 200)
        return week, week_ms

FakeGPS_settings = mp_settings.MPSettings([("nsats", int, 16),
                                                    ("lat", float, 47.3977),
                                                    ("lon", float, 8.5456),
                                                    ("alt", float, 1.0),
                                                    ("rate", float, 5)])
    gps_lat = FakeGPS_settings.lat
    gps_lon = FakeGPS_settings.lon
    gps_alt = FakeGPS_settings.alt
    gps_vel = [0, 0, 0]
    gps_week, gps_week_ms = get_gps_time(now)
    time_us = int(now*1.0e6)
    nsats = FakeGPS_settings.nsats

if nsats >= 6:
    fix_type = 3
else:
    fix_type = 1

master.mav.gps_input_send(time_us, 0, 0, gps_week_ms, gps_week, fix_type,
                            int(gps_lat*1.0e7), int(gps_lon*1.0e7), gps_alt,
                            1.0, 1.0,
                            gps_vel[0], gps_vel[1], gps_vel[2],
                            0.2, 1.0, 1.0,
                            nsats)

Hi,
Did you found out why it didn’t work ?

Hey what are you actually trying to achieve by injecting fake sensor data? Are you trying to test the failure modes?

Pymavlink and MAVProxy does not support the PX4 Autopilot, have you tried using the MAVSDK-Python?

Here’s a video on how to automate the injection of failure modes from one of the maintainers