The script is taken from the Dojo For Drones course (which I copy and paste below). I am actually now able to get further than the connect vehicle command, but the code tends to stall at different stages, e.g. switching vehicle mode to GUIDED (sometimes it just gets stuck here) and then arming. Through experimentation, I think I have learnt that: (a) the vehicle can’t be made armable unless the RC is switch on (I think this is something to do with the FAILSAFE state); (b) I am not sure the vehicle can be armed in GUIDED mode (I have actually now managed to arm by adapting the script to put it in STABILIZE mode). The main issue appears to be the fact that it is extremely laggy. I am not sure if I could run a safe mission with this, even if I could get the motors to arm each time. Any advice very welcome. Script below:
#####Dependencies#####
from dronekit import connect, VehicleMode, LocationGlobalRelative
import time
import socket
import exceptions
import argparse
###Function definitions for mission###
###Function to connect script to drone###
def connectMyCopter():
parser = argparse.ArgumentParser(description=‘commands’)
parser.add_argument(’–connect’)
args = parser.parse_args()
connection_string = args.connect
vehicle = connect(connection_string, wait_ready=True)
return vehicle
###Function to arm the drone and takeoff into the air###
def arm_and_takeoff(aTargetAltitude):
while not vehicle.is_armable:
print(“Waiting for vehicle to become armable”)
time.sleep(1)
#Switch vehicle to GUIDED mode and wait for change#
vehicle.mode = VehicleMode("GUIDED")
while vehicle.mode!="GUIDED":
print("Waiting for vehicle to enter GUIDED mode")
time.sleep(1)
#Arm vehicle once GUIDED mode is confirmed#
vehicle.armed=True
while vehicle.armed==False:
print("Waiting for vehicle to become armed.")
time.sleep(1)
vehicle.simple_takeoff(aTargetAltitude)
while True:
print("Current Altitude: %d"%vehicle.location.global_relative_frame.alt)
if vehicle.location.global_relative_frame.alt>=aTargetAltitude*0.95:
break
time.sleep(1)
print("Target altitude reached")
return None
###Mission###
vehicle=connectMyCopter()
print(“About to takeoff…”)
vehicle.mode=VehicleMode(“GUIDED”)
arm_and_takeoff(2) #tell drone to fly 2 metres in the sky
vehicle.mode=VehicleMode(“LAND”) #Once drone reaches altitude, tell it to land
time.sleep(2)
print(“End of function”)
print(“Arducopter version: %s”%vehicle.version)
while True:
time.sleep(2)
vehicle.close()
###End of script###