Skip to content

Instantly share code, notes, and snippets.

@dbaldwin
Created August 5, 2016 21:46
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 24 You must be signed in to fork a gist
  • Save dbaldwin/9185b702091148580fa836c1911f8735 to your computer and use it in GitHub Desktop.
Save dbaldwin/9185b702091148580fa836c1911f8735 to your computer and use it in GitHub Desktop.
Basic takeoff to 20m and land with DroneKit, Raspberry Pi and Pixhawk
from dronekit import connect, VehicleMode, LocationGlobalRelative
from pymavlink import mavutil
import time
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--connect', default='127.0.0.1:14550')
args = parser.parse_args()
# Connect to the Vehicle
print 'Connecting to vehicle on: %s' % args.connect
vehicle = connect(args.connect, baud=57600, wait_ready=True)
# Function to arm and then takeoff to a user specified altitude
def arm_and_takeoff(aTargetAltitude):
print "Basic pre-arm checks"
# Don't let the user 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
while not vehicle.armed:
print " Waiting for arming..."
time.sleep(1)
print "Taking off!"
vehicle.simple_takeoff(aTargetAltitude) # Take off to target altitude
# Check that vehicle has reached takeoff altitude
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)
# Initialize the takeoff sequence to 20m
arm_and_takeoff(20)
print("Take off complete")
# Hover for 10 seconds
time.sleep(10)
print("Now let's land")
vehicle.mode = VehicleMode("LAND")
# Close vehicle object
vehicle.close()
@BryanBetancur
Copy link

Hello, the execution does not exceed Waiting for vehicle to initialise..., can you help me, please

@EngmaherAljehani
Copy link

what is the problem

Connecting to vehicle on: 127.0.0.1:14550

Link timeout, no heartbeat in last 5 seconds !

@luuckiest
Copy link

Is the transmitter being used when you run the code?

@jianhuaixie
Copy link

Link timeout, no heartbeat in last 5 seconds
No heartbeat in 30 seconds, aborting.
Traceback (most recent call last):
File "takeoff_and_land.py", line 12, in
vehicle = connect(args.connect, baud=921600, wait_ready=True)
File "/usr/local/lib/python2.7/dist-packages/dronekit/init.py", line 2845, in connect
vehicle.initialize(rate=rate, heartbeat_timeout=heartbeat_timeout)
File "/usr/local/lib/python2.7/dist-packages/dronekit/init.py", line 2117, in initialize
raise APIException('Timeout in initializing connection.')
dronekit.APIException: Timeout in initializing connection.

@mariotavarez
Copy link

Probably do you have two connections
The same happened to me a few minutes ago, which is that we do the manual communication to send commands such as turning on rotors and then executing the connect method, manually remove the connection and it was solved, using this type of connection
vehicle = connect ('/ dev / ttyS1 ', wait_ready = False, baud = 57600)

@moonsummer
Copy link

Exception in message handler for HEARTBEAT
mode 0 not available on mavlink definition
Exception in message handler for HEARTBEAT
mode 0 not available on mavlink definition
Exception in message handler for HEARTBEAT
mode 0 not available on mavlink definition
Exception in message handler for HEARTBEAT
mode 0 not available on mavlink definition
Exception in message handler for HEARTBEAT
mode 0 not available on mavlink definition
Exception in message handler for HEARTBEAT
mode 0 not available on mavlink definition

@hassanyousufx
Copy link

Hey! I am stuck at " Waiting for vehicle to initialise...". What could be the reason?

@LU-Jiale
Copy link

I am using raspberry pi 3 and PX4, possible reasons for 'Waiting for vehicle to initialise':

  1. Safety switch off
  2. No GPS
  3. No RC signal

@ArinalFM
Copy link

if you follow https://www.youtube.com/watch?v=DGAB34fJQFc&t=10s and successed, change 127.0.0.1:14550 from the code with /dev/serial0, it works well for me

@vedantkandoi
Copy link

I used sitl, but can't get it to takeoff again after landing.
What I did was:
first takeoff to 1.5m
then land
then takeoff to 1.5 again but it gets stuck here. Do you know anything about it?

@supericeandfire
Copy link

This script can arm my quadrotor, but cannot make it take off. Does anyone know why?
My pixhawk is 2.4.6. Firmware is ardu v4.0.4 .
Thanks~

@pkrana0005
Copy link

See my github repo wiki for details. I successfully tested this code last year.
In my repo wiki i wrote all the steps which worked for me.

https://github.com/pkrana0005/Controlling-Pixhawk-with-RaspberryPi3

Watch the real time execution on drone.

https://photos.google.com/share/AF1QipM5C2qyx0phCbj3SAALhkd8C3qC64bHEPcr74lFLiVR6S2pLrGMiW75hXa5ynb6aQ/photo/AF1QipMwRckurRbGTI2Egz2SZJsWQpwNkVPsgNYZyRpf?key=S181U0hUMUxBX2dXZjF3VHdWVEcxNDZRZW1EVTh3

@pkrana0005
Copy link

Hey! I am stuck at " Waiting for vehicle to initialise...". What could be the reason?

Make sure that GPS is locked. Without GPS getting locked, it will not work for this code

@pkrana0005
Copy link

Is the transmitter being used when you run the code?

No, telemetry is not required for this operation. However using it will be beneficial to check for errors.

@pkrana0005
Copy link

Link timeout, no heartbeat in last 5 seconds
No heartbeat in 30 seconds, aborting.
Traceback (most recent call last):
File "takeoff_and_land.py", line 12, in
vehicle = connect(args.connect, baud=921600, wait_ready=True)
File "/usr/local/lib/python2.7/dist-packages/dronekit/init.py", line 2845, in connect
vehicle.initialize(rate=rate, heartbeat_timeout=heartbeat_timeout)
File "/usr/local/lib/python2.7/dist-packages/dronekit/init.py", line 2117, in initialize
raise APIException('Timeout in initializing connection.')
dronekit.APIException: Timeout in initializing connection.

Check the connections between Flight controller and Rpi. See my wiki

@pkrana0005
Copy link

Hey! I am stuck at " Waiting for vehicle to initialise...". What could be the reason?

GPS should get locked. Try in open

@pkrana0005
Copy link

This script can arm my quadrotor, but cannot make it take off. Does anyone know why?
My pixhawk is 2.4.6. Firmware is ardu v4.0.4 .
Thanks~

possibility that code may have been edited and resulted in error. Try with code in my repo, it is fully tested.

@supericeandfire
Copy link

@pkrana0005 Thanks for your reply.
I found that the biggest difference is baudrate.
Your baudrate is 921600. A lot of website say 57600.
How do you calculate 921600?

@supericeandfire
Copy link

This script can arm my quadrotor, but cannot make it take off. Does anyone know why?
My pixhawk is 2.4.6. Firmware is ardu v4.0.4 .
Thanks~

possibility that code may have been edited and resulted in error. Try with code in my repo, it is fully tested.
What do you mean the code have been edited and resulted in error?

@pkrana0005
Copy link

Its been a year now since I worked on that, I don't exactly remember why I used that baudrate but generally whatever baudrate is used, it should be same in RPi code and Flight controller, then also communication is possible. The greater the supported baudrate, greater is the speed of communication. So check with all the baudrates.

@pkrana0005
Copy link

This script can arm my quadrotor, but cannot make it take off. Does anyone know why?
My pixhawk is 2.4.6. Firmware is ardu v4.0.4 .
Thanks~

possibility that code may have been edited and resulted in error. Try with code in my repo, it is fully tested.
What do you mean the code have been edited and resulted in error?

I mean to say that any change in code may also result in some error or may result in non-working.

@supericeandfire
Copy link

This script can arm my quadrotor, but cannot make it take off. Does anyone know why?
My pixhawk is 2.4.6. Firmware is ardu v4.0.4 .
Thanks~

possibility that code may have been edited and resulted in error. Try with code in my repo, it is fully tested.
What do you mean the code have been edited and resulted in error?

I mean to say that any change in code may also result in some error or may result in non-working.

I see. I delete vehicle.is_armable in my code. So this change may cause error.
Thanks I will keep trying.

@Maarginean
Copy link

Maarginean commented Jun 21, 2021

Hello, I used this code but my quadcopter don`t enter in hover mode, only take off and after reached the altitude land
Can somone help me with this problem?

@angela804
Copy link

This script can arm my quadrotor, but cannot make it take off. Does anyone know why?
My pixhawk is 2.4.6. Firmware is ardu v4.0.4 .
Thanks~

possibility that code may have been edited and resulted in error. Try with code in my repo, it is fully tested.
What do you mean the code have been edited and resulted in error?

I mean to say that any change in code may also result in some error or may result in non-working.

I see. I delete vehicle.is_armable in my code. So this change may cause error. Thanks I will keep trying.

even if you delete that part of the code, i think you won't get through vehicle.armed=True because gps lock is still a requirement.

@angela804
Copy link

Hello, I used this code but my quadcopter don`t enter in hover mode, only take off and after reached the altitude land Can somone help me with this problem?

have u tried increasing the time.sleep part of the code after the take off is completed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment