'Ardupilot - Dronekit Setting the Current Location of the Vehicle

Hello everybody, hope you are all doing well.

I am doing in a project in which I receive GPS data (Longitude, and Latitude) from an Android device via an SQL server. What I am trying to do is to send this Longitude - Latitude data to my SITL vehicle in Ardupilot. I thought about using Dronekit Python API as such:

from dronekit import connect, VehicleMode

import time
import mysql.connector
import time

#--- Start the Software In The Loop (SITL)
import dronekit_sitl
#
sitl = dronekit_sitl.start_default()   #(sitl.start)
#connection_string = sitl.connection_string()

mydb = mysql.connector.connect(
  host="******",
  user="******",
  password="*****",
  database="koordinat"
)
mycursor = mydb.cursor()

#--- Now that we have started the SITL and we have the connection string (basically the ip and udp port)...
print("Araca bağlanılıyor")
vehicle = connect('tcp:127.0.0.1:5762', wait_ready=False, baud = 115200) 
vehicle.wait_ready(True, raise_exception=False)

#-- Read information from the autopilot:
#- Version and attributes
vehicle.wait_ready('autopilot_version')
print('Autopilot version: %s'%vehicle.version)

#- Does the firmware support the companion pc to set the attitude?
print('Supports set attitude from companion: %s'%vehicle.capabilities.set_attitude_target_local_ned)

vehicle.mode = VehicleMode("GUIDED")
vehicle.armed = True


while(True):

    mycursor.execute("SELECT * FROM koordinat WHERE 1")

    location = str(mycursor.fetchall())
    location = location.split(",")

    location[0] = location[0].replace("[", "")
    location[0] = location[0].replace("(", "")
    location[0] = location[0].replace("'", "")

    location[1] = location[1].replace("[", "")
    location[1] = location[1].replace(")", "")
    location[1] = location[1].replace("'", "")
    location[1] = location[1].replace(")", "")

    # Converting the longitude and latitude to float, before assigning to the vehicle GPS data:

    location[0] = float(location[0])
    location[1] = float(location[1])
    
    # Setting the location of the vehicle:

    vehicle.location.global_frame.lat = location[0]
    vehicle.location.global_frame.lon = location[1]

    print('Konum:', str(vehicle.location.global_frame.lat)+str(","), str(vehicle.location.global_frame.lon)+str(","), str(vehicle.location.global_frame.alt))

   
    #- When did we receive the last heartbeat
    print('Son bilgi gelişi: %s'%vehicle.last_heartbeat)


    time.sleep(1)

However, when I check from the SITL and Mission Planner (also from the print statement from my code) the location does not change; the simulator simply ignores those commands sent by the Dronekit. Is there a working method to accomplish what I am trying to do? I tried to change the sim_vehicle.py script which I use to start the simulation. But I was only able to change the starting/home location of the vehicle. I was not able to change the current location of the vehicle on SITL and Mission Planner.



Solution 1:[1]

This is incorrect. You're modifying the attribute of the vehicle object that's connected to the SITL, not sending any commands to the actual autopilot.

vehicle.location.global_frame.lat = location[0]
vehicle.location.global_frame.lon = location[1]

What you want to do is set the mode to GUIDED and use the simple_goto function in dronekit to make the drone move to lat/lon/alt coordinates.

Otherwise, you can also send this MAVLink command SET_POSITION_TARGET_GLOBAL_INT to guide it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Adrian