Timeout in initialising connection error



Thank you for the excellent course. Everything has gone fine with the build (and I am enjoying flying it manually), but I am having difficulty getting Dronekit to work.

I think I’ve followed all the steps in the course; have installed Dronekit 2.9.1, etc. I’ve tried playing around with various things (different ports, etc.)!, but nothing works. Please help!


PS I’ve attempted to attach a screenshot which I hope you can see here:



Hi Cannot see the screenshot. Were you successful using DK in the SITL?



Could you post your takeoff_and_land.py file?

Did you confirm dependencies are loaded and compatible DK version used?


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:

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’)
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”)

#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")

#Arm vehicle once GUIDED mode is confirmed#
while vehicle.armed==False:
	print("Waiting for vehicle to become armed.")


while True:
	print("Current Altitude: %d"%vehicle.location.global_relative_frame.alt)
	if vehicle.location.global_relative_frame.alt>=aTargetAltitude*0.95:

print("Target altitude reached")
return None


print(“About to takeoff…”)

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


print(“End of function”)
print(“Arducopter version: %s”%vehicle.version)

while True:

###End of script###


I noticed a def and while statement in your script does not have proper indentation?


I think that’s just a copying fail on my part. I can get that to run. In fact I can get the vehicle to arm, provided I set it to STABILIZE mode (Should it definitely be capable of arming in GUIDED mode?) In any event, it takes nearly a minute to get that far though. I am wondering why the response time is so slow.


It’s not the STABILIZE thing. I’ve now managed to get it to arm in GUIDED mode, but it’s very sporadic. Sometimes it still just times out, sometimes it gets to the switching to GUIDED mode code and then gets stuck, sometimes it arms but then gets stuck (the vehicle state command doesn’t recognise that it has armed, so it just says “Waiting for vehicle to become armed.” in an endless loop). The common denominator appears to be that the dronekit functions associated with the vehicle object seem to be very delayed (if they work properly at all). Any idea what might be causing this? Even if I can now get it to fly, it would be unsafe to run an autonomous mission with the command this unresponsive.


It should arm in STABILIZE.
Are you using DK ver 2.9.1?
You are using Navio 2 FC?


Yes DK 2.9.1 and yes Navio 2 FC. It seems to work, just about, but be very slow and often it gets trapped in the While checking loops.


Thinking about it, it’s almost as if it can’t retrieve the attributes at runtime if they are changed in the course of the code. For example, if I switch it from STABILIZE to GUIDED, it will execute the command, but then it will get stuck in the while loop that checks to see if it is in GUIDED mode. The next time round, it will get past that section of the code (because it has already been set to GUIDED) and it will arm (eventually) but then it gets stuck in the while loop that checks to see if it is armed.


I’m back looking around. Have you had any luck? Previously I stated it should arm in STABILIZE, but I meant to say it is “armable” in STABILIZE, but I think it needs to be in GUIDED mode to accept commands such as vehicle.armed = True. Perhaps a few other observations might be of help…

I noticed the dependencies section is incomplete:

Also - perhaps I’m unclear regarding what you did - remove the statement


in the Mission statements. It is already executed in the “arm_and_takeoff” function.


Have you tried this in the SITL or try and debug line-by-line?