Dronekit GPS not going

I am trying to send the DRONE to GPS coordinates sent from DRONEKIT and RPI .

The drone takes off and reaches the target altitude and stays there and just LANDs back without moving towards the GPS coordinates. Please advise where I am going wrong.

-- coding: utf-8 --

from future import print_function
import time
from dronekit import connect, VehicleMode, LocationGlobalRelative, LocationGlobal

import argparse
import sys
import os
import struct
import subprocess
import numpy
import dronekit_sitl

global new
global coor
latcor=float(40.7827725)
loncor=float(-73.9653627)

Set up option parsing to get connection string

parser = argparse.ArgumentParser(description=‘Commands vehicle using vehicle.simple_goto.’)
parser.add_argument(’–connect’,
help=“Vehicle connection target string. If not specified, SITL automatically started and used.”)
args = parser.parse_args()

connection_string = args.connect
sitl = None

Start SITL if no connection string specified

if not connection_string:
sitl = dronekit_sitl.start_default()
connection_string = args.connect

Connect to the Vehicle

print(‘Connecting to vehicle on: %s’ % connection_string)
vehicle = connect(connection_string, wait_ready=False)
def ChangeMode(vehicle, mode):
#- change auto pilot mode
while vehicle.mode != VehicleMode(mode):
vehicle.mode = VehicleMode(mode)
time.sleep(0.5)
return True

def arm_and_takeoff(aTargetAltitude):
“”"
Arms vehicle and fly to aTargetAltitude.
“”"

print("Basic pre-arm checks")
# Don't try to arm until autopilot is ready
while not vehicle.is_armable:
    print(" Waiting for vehicle to initialise...")
    time.sleep(1)

print("Arming motors")
# Copter should arm in GUIDED mode
vehicle.mode = VehicleMode("GUIDED")
vehicle.armed = True

# Confirm vehicle armed before attempting to take off
while not vehicle.armed:
    print(" Waiting for arming...")
    time.sleep(1)

print("Taking off!")
vehicle.simple_takeoff(aTargetAltitude)  # Take off to target altitude

# Wait until the vehicle reaches a safe height before processing the goto
while True:
    print(" Altitude: ", vehicle.location.global_relative_frame.alt)
    # Break and return from function just below target altitude.
    if vehicle.location.global_relative_frame.alt >= aTargetAltitude * 0.95:
        print("Reached target altitude")
        break
    time.sleep(1)

ChangeMode(vehicle, “AUTO”)

coor=LocationGlobalRelative(latcor,loncor,45)
print(coor)

print(“Set default/target airspeed to 3”)
vehicle.airspeed = 3
vehicle.simple_goto(coor, groundspeed=10)

print(“Returning to Launch”)
vehicle.mode = VehicleMode(“RTL”)

arm_and_takeoff(45)

Close vehicle object before exiting script

print(“Close vehicle object”)
vehicle.close()

Shut down simulator if it was started.

if sitl:

Walt

Hi @viisshnu,

From the documentation, “There is no mechanism for notification when the target location is reached, and if another command arrives before that point that will be executed immediately.”
https://dronekit-python.readthedocs.io/en/latest/automodule.html#dronekit.Vehicle.simple_goto

I think you need to wait/sleep after vehicle.simple_goto(coor, groundspeed=10) for it to reach somewhere near the target location.

Your code will set the goto and then instantly change to RTL, the default RTL behaviour in copter is to delay for a short time and then land. Which I think matches up with the behaviour you are seeing.

hi Sparlane
As I was saying the after I execute and mavproxy and the script from RPI, drone takes off to the target altitude and hovers for a few minutes and lands back without any interjection. It doesnot go to the coordinates specified in the script. What am I missing here?

Walt

Hi @viisshnu,

You will need to add a time.sleep() after the call to simple_goto().

If possible, you should connect mission planner or qgroundcontrol to your drone at the same time as you run this script. That will allow you to monitor which mode it is in.

Hi Sparlane,
Thank you for your suggestion. I added the time.sleep(10) after the simple_goto()
and the drone started out in the direction of the coordinates.
It moved some distance and I did a RTL as I couldnt let it out of sight.

I appreciate you understanding my code.
Thank you
Walt