Arming Drone

from dronekit import connect, VehicleMode, LocationGlobalRelative, APIException
from pymavlink import mavutil
import time
import socket
import math

# CONNECT TO VEHICLE
vehicle = connect('127.0.0.1:14550', baud=921600, wait_ready=True)

def arm():
	print("ARMING MOTORS")
	vehicle.parameters['ARMING_CHECK'] = 0 # DISABLE ARMING CHECK
	vehicle.mode = VehicleMode("STABILIZE")
	vehicle.flush()
	vehicle.armed=True
	time.sleep(5)
	print("ARMING SUCCESS")
	time.sleep(5)
	
arm()

print("COPTER WILL NOW DISARM")
vehicle.armed = False
while vehicle.armed:
	print("WAITING FOR VEHICLE TO DISARM...")
	time.sleep(1)
vehicle.flush()
vehicle.close()

print("END OF PROGRAM")

Take-off Test

from dronekit import connect, VehicleMode, LocationGlobalRelative
from pymavlink import mavutil
import time

vehicle = connect('127.0.0.1:14550', baud=921600, wait_ready=True)

def arm():
    print("ARMING MOTOR MECHANISM")
    vehicle.parameters['ARMING_CHECK'] = 0 # DISABLE ARMING CHECKS
    vehicle.mode = VehicleMode("GUIDED")
    vehicle.armed = True

    while not vehicle.armed:
        print("WAITING FOR ARMING...")
        time.sleep(5)

    print("ARMING SEQUENCE SUCCESSFULLY INITIATED")

def takeoff(aTargetAltitude):
    print("WARNING!!!")
    print("MOTOR IS STARTING TO ROTATE")
    time.sleep(1)
    print("READY TO TAKEOFF!!!")
    vehicle.simple_takeoff(aTargetAltitude)

    while True:
        print("ALTITUDE:", vehicle.location.global_relative_frame.alt)
        if vehicle.location.global_relative_frame.alt >= aTargetAltitude * 0.95:
            print("REACHED TARGET ALTITUDE")
            vehicle.mode = VehicleMode("LOITER")
            vehicle.flush()
            break
        time.sleep(7)

arm()
takeoff(2)
time.sleep(3)

print("STOPPING TAKEOFF TEST")
print("SWITCHING TO LANDING MODE")
vehicle.mode = VehicleMode("RTL")
print("COPTER WILL NOW LAND....")

vehicle.armed = False
vehicle.commands.upload()
vehicle.close()

print("END OF PROGRAM")

Moving Drone

from dronekit import connect, VehicleMode, LocationGlobalRelative
from pymavlink import mavutil
import time

# CHANGE THE UDP IP ADDRESS
vehicle = connect('127.0.0.1:14552', baud=921600, wait_ready=True)
vehicle.parameters.set('PLND_ENABLED', 1)
vehicle.parameters.set('PLND_TYPE', 1)
vehicle.parameters.set('PLND_EST_TYPE', 0)

# ARM THE DRONE
def arm():
    print("ARMING MOTOR MECHANISM")
    vehicle.parameters['ARMING_CHECK'] = 0 # DISABLE ARMING CHECKS
    vehicle.mode = VehicleMode("GUIDED")
    vehicle.armed = True

    while not vehicle.armed:
        print("WAITING FOR ARMING...")
        time.sleep(5)

    print("ARMING SEQUENCE SUCCESSFULLY INITIATED")

# TAKEOFF FUNCTION
def takeoff(aTargetAltitude):
    print("WARNING!!!")
    print("MOTOR IS STARTING TO ROTATE")
    time.sleep(1)
    print("READY TO TAKEOFF!!!")
    vehicle.simple_takeoff(aTargetAltitude)

    while True:
        print("ALTITUDE:", vehicle.location.global_relative_frame.alt)
        if vehicle.location.global_relative_frame.alt >= aTargetAltitude * 0.95:
            print("REACHED TARGET ALTITUDE")
            vehicle.mode = VehicleMode("LOITER")
            vehicle.flush()
            break
        time.sleep(1)

# FUNCTION TO MOVE THE DRONE AT VELOCITY
def send_ned_velocity(vehicle, vx, vy, vz, duration):
    vehicle.mode = VehicleMode("GUIDED")
    print("COPTER IS NOW IN TRACKING THE TARGET")
    msg = vehicle.message_factory.set_position_target_local_ned_encode(
        0,
        0, 0,
        9,
        1479,
        0, 0, 0, # POSITION m
        vx, vy, vz, # VELOCITY m/s
        0, 0, 0, # ACCELERATIONS m/s^2
        0, 0
    )
    for x in range(0, duration):
        vehicle.send_mavlink(msg)
        time.sleep(1)

arm()
takeoff(15)
time.sleep(3)

# MOVE copter 2 m/s forward for 5 sec.
vx = 2
vy = 0
vz = 0
duration = 5
send_ned_velocity(vehicle, vx, vy, vz, duration)

# MOVE backwards at 2 m/s for 5 sec.
vx = -2
vy = 0
vz = 0
duration = 5
send_ned_velocity(vehicle, vx, vy, vz, duration)

# MOVE right 2 m/s for 5 sec.
vx = 0
vy = 2
vz = 0
duration = 5
send_ned_velocity(vehicle, vx, vy, vz, duration)

# MOVE left at 2 m/s for 5 sec.
vx = 0
vy = -2
vz = 0
duration = 5
send_ned_velocity(vehicle, vx, vy, vz, duration)

time.sleep(20)
print("STOPPING DRONE TEST")
print("SWITCHING TO LANDING MODE")
vehicle.mode = VehicleMode("RTL")
print("COPTER WILL NOW LAND....")

vehicle.armed = False
vehicle.commands.upload()
vehicle.close()

print("END OF PROGRAM")

Simple-Go-To

from __future__ import print_function
import time
from dronekit import connect, VehicleMode, LocationGlobalRelative

# Connect to the Vehicle
vehicle = connect('127.0.0.1:14550', baud=921600, wait_ready=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.parameters['ARMING_CHECK'] = 0 # DISABLE ARMING CHECKS
    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
    #  (otherwise the command after Vehicle.simple_takeoff will execute
    #   immediately).
    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)

arm_and_takeoff(15)

print("Set default/target airspeed to 3")
vehicle.airspeed = 3

print("Going towards first point for 30 seconds ...")
point1 = LocationGlobalRelative(5.147264589053249, 100.49392067963265, 20)
vehicle.simple_goto(point1)

# sleep so we can see the change in map
time.sleep(30)

print("Going towards second point for 30 seconds (groundspeed set to 10 m/s) ...")
point2 = LocationGlobalRelative(5.147089856040111, 100.49368505156349, 20)
vehicle.simple_goto(point2, groundspeed=4)

# sleep so we can see the change in map
time.sleep(30)

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

# Close vehicle object before exiting script
print("Close vehicle object")
vehicle.close()