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