Drone Programming - How to Program your Drone to Fly in a Square Trajectory using DroneKit-Python?

December 28, 2021 in Aerospace, Programming by DhulkarnaynDhulkarnaynโ€”12 minutes

Embarking on the journey of programming a drone might seem daunting at first, but fear not โ€“ it’s more accessible than you might think. The expanding applications of drones across diverse industries underscore their growing significance. Dive into the realm of drone programming with a plethora of resources and guides available online, supporting multiple programming languages such as C, C++, Python, and more.

Drone programming extends beyond the realm of mere autonomous takeoff and landings. There are instances where drones are tasked with specific missions tailored to meet your requirements or those of your customers.

Video Credits: Dhulkarnayn, Elucidate Drones

Imagine wanting to program your drone to gracefully trace a triangular or square trajectory through the skies. The first step? Delve into the mathematical intricacies behind this aerial choreography before translating it into code.

As drones soar through the skies, relying heavily on the Global Position System (GPS) for outdoor navigation, it’s crucial to acknowledge the substantial mathematical groundwork underpinning their guidance and navigation. Brace yourself for an adventure where algorithms and coordinates dance in harmony with the crisp, open air.

A Guide to Program Your Drone for a Dazzling Square Trajectory

Embarking on the thrilling adventure of programming your drone to gracefully trace a square trajectory requires a rendezvous with the geometry of the square itself. Picture a square โ€“ a captivating regular quadrilateral, boasting four sides of equal length and four angles of perfect symmetry.

Square - Illustrating Side Length

Image Credits: Dhulkarnayn, Elucidate Drones

Explore the fascinating characteristics of a square, where every angle stands at attention, measuring a perfect 90ยฐ. Each side of the square proudly matches its counterparts in length, with opposing sides running parallel, akin to a symphony of geometric precision. As if choreographed, the diagonals intersect at a harmonious 90ยฐ.1


The magic behind the scenes lies in the python script employed in this article, crafted with an intricate understanding of the geometric poetry of a square.

Now, the exhilarating journey commences. Your initial task involves sculpting a path that embodies the essence of a square, transmuting its local coordinates into the captivating tapestry of geographical coordinates โ€“ latitudes and longitudes. Once the stage is set with these geographic notes, it’s time to orchestrate the drone’s flight, guiding it gracefully from point to point in a seamless dance.


Immerse yourself in the symmetrical dance of angles, where every corner echoes with the melody of 90ยฐ. Armed with the side length in meters, envision the calculation of the next geographic point โ€“ a process as elegant as incrementing the angle to 90ยฐ from the current heading of the drone.

Repeat this balletic maneuver four times, and voilร ! The airspace is now painted with the enchanting trajectory of a square, a spectacle born from the fusion of mathematical precision and airborne finesse.

Dive into the Square Mission Script with DroneKit-Python Magic

Embark on a thrilling coding adventure as we unveil the mystical script orchestrating the square mission using DroneKit-Python. To join this coding escapade, simply summon the script to your device with the following git command:

git clone


For those not acquainted with the magical powers of git, fear not! Conjure it into existence on your device by reciting the following commands in your terminal:

sudo apt-get update
sudo apt-get install git

Alternatively, if you prefer the art of copy and paste, inscribe the following incantations into a file named on your device. Let the coding symphony begin!

The Script
  1#!/usr/bin/env python
  4# Author  :  Saiffullah Sabir Mohamed
  5# Github  :
  6# Website :
  7# Source  :
 10# Import Necessary Packages
 11from dronekit import connect, VehicleMode, LocationGlobalRelative
 12import time, math
 14def basic_takeoff(altitude):
 16    """
 18    This function take-off the vehicle from the ground to the desired
 19    altitude by using dronekit's simple_takeoff() function.
 21    Inputs:
 22        1.  altitude            -   TakeOff Altitude
 24    """
 26    vehicle.mode = VehicleMode("GUIDED")
 27    vehicle.armed = True
 28    time.sleep(2)
 29    vehicle.simple_takeoff(altitude)
 31    while True:
 32        print("Reached Height = ", vehicle.location.global_relative_frame.alt)
 34        if vehicle.location.global_relative_frame.alt >= (altitude - 1.5):
 35            break
 37def change_mode(mode):
 39    """
 41    This function will change the mode of the Vehicle.
 43    Inputs:
 44        1.  mode            -   Vehicle's Mode
 46    """
 48    vehicle.mode = VehicleMode(mode)
 50def send_to(latitude, longitude, altitude):
 52    """
 54    This function will send the drone to desired location, when the 
 55    vehicle is in GUIDED mode.
 57    Inputs:
 58        1.  latitude            -   Destination location's Latitude
 59        2.  longitude           -   Destination location's Longitude
 60        3.  altitude            -   Vehicle's flight Altitude
 62    """
 64    if == "GUIDED":
 65        location = LocationGlobalRelative(latitude, longitude, float(altitude))
 66        vehicle.simple_goto(location)
 67        time.sleep(1)
 69def change_alt(step):
 71    """
 73    This function will increase or decrease the altitude
 74    of the vehicle based on the input.
 76    Inputs:
 77        1.  step            -   Increase 5 meters of altitude from 
 78                                current altitude when INC is passed as argument.
 80                            -   Decrease 5 meters of altitude from 
 81                                current altitude when DEC is passed as argument.
 83    """
 85    actual_altitude = int(vehicle.location.global_relative_frame.alt)
 86    changed_altitude = [(actual_altitude + 5), (actual_altitude - 5)]
 88    if step == "INC":
 89        if changed_altitude[0] <= 50:
 90            send_to(, vehicle.location.global_frame.lon, changed_altitude[0])
 91        else:
 92            print("Vehicle Reached Maximum Altitude!!!")
 94    if step == "DEC":
 95        if changed_altitude[1] >= 5:
 96            send_to(, vehicle.location.global_frame.lon, changed_altitude[1])
 97        else:
 98            print("Vehicle Reached Minimum Altitude!!!")
100def distance_calculation(homeLattitude, homeLongitude, destinationLattitude, destinationLongitude):
102    """
104    This function returns the distance between two geographiclocations using
105    the haversine formula.
107    Inputs:
108        1.  homeLattitude          -   Home or Current Location's  Latitude
109        2.  homeLongitude          -   Home or Current Location's  Longitude
110        3.  destinationLattitude   -   Destination Location's  Latitude
111        4.  destinationLongitude   -   Destination Location's  Longitude
113    """
115    # Radius of earth in metres
116    R = 6371e3
118    rlat1, rlon1 = homeLattitude * (math.pi/180), homeLongitude * (math.pi/180)
119    rlat2, rlon2 = destinationLattitude * (math.pi/180), destinationLongitude * (math.pi/180)
120    dlat = (destinationLattitude - homeLattitude) * (math.pi/180)
121    dlon = (destinationLongitude - homeLongitude) * (math.pi/180)
123    # Haversine formula to find distance
124    a = (math.sin(dlat/2) * math.sin(dlat/2)) + (math.cos(rlat1) * math.cos(rlat2) * (math.sin(dlon/2) * math.sin(dlon/2)))
125    c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
127    # Distance (in meters)
128    distance = R * c
130    return distance
132def destination_location(homeLattitude, homeLongitude, distance, bearing):
134    """
136    This function returns the latitude and longitude of the
137    destination location, when distance and bearing is provided.
139    Inputs:
140        1.  homeLattitude       -   Home or Current Location's  Latitude
141        2.  homeLongitude       -   Home or Current Location's  Longitude
142        3.  distance            -   Distance from the home location
143        4.  bearing             -   Bearing angle from the home location
145    """
147    # Radius of earth in metres
148    R = 6371e3
150    rlat1, rlon1 = homeLattitude * (math.pi/180), homeLongitude * (math.pi/180)
152    d = distance
154    #Converting bearing to radians
155    bearing = bearing * (math.pi/180)
157    rlat2 = math.asin((math.sin(rlat1) * math.cos(d/R)) + (math.cos(rlat1) * math.sin(d/R) * math.cos(bearing)))
158    rlon2 = rlon1 + math.atan2((math.sin(bearing) * math.sin(d/R) * math.cos(rlat1)) , (math.cos(d/R) - (math.sin(rlat1) * math.sin(rlat2))))
160    #Converting to degrees
161    rlat2 = rlat2 * (180/math.pi) 
162    rlon2 = rlon2 * (180/math.pi)
164    # Lat and Long as an Array
165    location = [rlat2, rlon2]
167    return location
169def square_calculation(side_length):
171    """
173    This function will generate the geographical coordinates (latitudes & longitudes)
174    of the square path with the given side length. The origin or reference location
175    for the generation of the square trajectory is the vehicle's current location.
177    Inputs:
178        1.  side_length         -   Side length of the square
180    """
182    # Vehicle's heading and current location
183    angle          =  int(vehicle.heading)
184    loc            =  (, vehicle.location.global_frame.lon, vehicle.location.global_relative_frame.alt)
186    # Declaring a array variable to store
187    # the geogrpahical location of square points
188    final_location =  []
190    for count in range(4):
191        new_loc =  destination_location(homeLattitude = loc[0], homeLongitude = loc[1], distance = side_length, bearing = angle)
192        final_location.append((new_loc[0], new_loc[1], loc[2]))
193        loc     =  (new_loc[0], new_loc[1], loc[2])
195        # Incrementing heading angle
196        angle  +=  90
198    return final_location
200def square_mission(side_length):
202    """
204    This function retrieves the square coordinates from the square_calculation()
205    function and guides the vehicle to the retrieved points.
207    Inputs:
208        1.  side_length         -   Side length of the square
210    """
212    # Retrieving the array of the locations of the square path
213    locations  =  square_calculation(side_length = side_length)
215    for location in locations:
217        # Send vehicle to the destination
218        send_to(latitude = location[0], longitude = location[1], altitude = location[2])
220        while True:
222            # Distance between the current location of the vehicle and the destination
223            distance = distance_calculation(homeLattitude =,
224                                            homeLongitude = vehicle.location.global_frame.lon,
225                                            destinationLattitude  = location[0],
226                                            destinationLongitude = location[1])
228            if distance <= 1.8:
229                break
231            time.sleep(2)
233def main():
235    # Declaring Vehicle as global variable
236    global vehicle
238    # Connecting the Vehicle
239    vehicle = connect('udpin:', baud=115200)
241    # Setting the Heading angle constant throughout flight
242    if vehicle.parameters['WP_YAW_BEHAVIOR'] != 0:
243        vehicle.parameters['WP_YAW_BEHAVIOR'] = 0
244        print("Changed the Vehicle's WP_YAW_BEHAVIOR parameter")
246    while True:
248        # Getting Input from User
249        value = input("Enter your Input:\n").upper()
251        if value == 'TAKEOFF':
252            basic_takeoff(altitude = 5)
254        if value == 'LAND':
255            change_mode(mode = value)
257        if value == 'INC' or 'DEC':
258            change_alt(step = value)
260        if value == 'SQUARE':
261            side = int(input("Enter Side Length of the Square Path (in meters):\n"))
262            square_mission(side_length = side)
264if __name__ == "__main__":
265    main()

Source: Link


Embark on a journey to discover the mystical dance of drone yaw with the WP_YAW_BEHAVIOR parameter. This enchanted parameter holds the key to commanding the autopilot’s yaw control during missions and the Return To Launch (RTL) spell.

In our magical code realm, we’ve aligned the stars, setting the yaw behavior to 0 (Never Change Yaw). Yet, the power to alter the destiny of your drone’s dance lies in your hands. Consult the table below to decipher the meaning of each value and choreograph the perfect mission:

0Never change yaw
1Face next waypoint
2Face next waypoint except RTL
3Face along GPS course

Unleash your creativity and let your drone traverse the skies in a dance tailored to your desires! โœจ๐Ÿš๐Ÿ’ƒ


Launching the Drone Ballet

Elevate the curtains and initiate the mesmerizing drone ballet by commanding the script! Unleash the magic with a single command in your terminal:


Unveiling the Aerial Masterpiece ๐Ÿš๐ŸŒŸ

Dive into the mesmerizing spectacle of drone choreography! Witness the seamless execution of the square mission, orchestrated with precision and finesse. Behold the dance of virtual drones, gracefully gliding through the designated trajectory.


Video Credits: Dhulkarnayn, Elucidate Drones


Embark on this virtual journey not with tangible drones but with the ethereal embrace of ArduPilot’s Software In The Loop (SITL) simulation. The grand spectacle unfolds within Mission Planner, a revered MAVLink supported Ground Control Station (GCS) that gracefully conducts the drone’s symphony. Whether your realm is Windows, Linux, or Mac OS, this enchanting experience awaits, thanks to the mystical essence of Mono.

Unraveling the Drone Symphony ๐ŸŽฎโœจ

Embark on an exhilarating journey of drone mastery with the script! This Python wizardry enables you to orchestrate any MAVLink supported drone, choreographing a graceful dance in the form of a square trajectory.

After the enchanting execution of, immerse yourself in the interactive realm by experimenting with the following inputs:

incIncreases the current altitude
decDecreases the current altitude
squareStarts the square mission


To unlock the magic, ensure you have the mystical key, dronekit, installed on your device. Utter the incantation below in your terminal if it’s not already bestowed upon you:

sudo pip install dronekit

Should you encounter any mysteries during installation, seek guidance in the sacred scrolls of knowledge:

Now, let’s unveil the secrets behind the incantations in the spellbook, unraveling the essence of the core functions:

Distance Calculation - distance_calculation()

Witness the arcane powers of the distance_calculation() function! This mystical formula, rooted in the haversine tradition, unveils the secrets of distance between two sacred locations, adorned with latitude and longitude coordinates.

The incantation accepts the following parameters:

homeLattitudeHome Location’s Latitude
homeLongitudeHome Location’s Longitude
destinationLattitudeDestination Location’s Latitude
destinationLongitudeDestination Location’s Longitude

Destination Location - destination_location()

Behold the mesmerizing manifestation of the destination_location() function! It summons the geographic coordinates of the destination, aligning them with the stars of the home location as its celestial reference.

The sacred parameters for this ritual are:

homeLattitudeHome Location’s Latitude
homeLongitudeHome Location’s Longitude
distanceDistance from the home location
bearingBearing angle from the home location

Square Calculation - square_calculation()

Enter the realm of geometric sorcery with the square_calculation() function! This incantation crafts the sacred coordinates of the square’s trajectory, weaving the dance of vertices with the bestowed side length.

The magic unfolds in a loop of four iterations, invoking the destination_location() function with a change in bearing angle to 90ยฐ. The result? The formation of a celestial square trajectory.

Glimpse the arcane script in action, where the vehicle’s location and the square’s vertices unfold like an ancient tapestry.

Square Mission - Side Length of 20 meters

Image Credits: Dhulkarnayn, Elucidate Drones

Square Mission - square_mission()

As the climax approaches, the square_mission() function takes center stage! It beckons forth the array of sacred locations from the square_calculation() ritual, guiding the vehicle in a mystical pilgrimage through each consecrated point.

The enchanting while loop diligently monitors the distance between the vehicle and the next location, weaving the final threads of the ethereal square dance until the distance gracefully converges to zero.


As we wrap up this journey into the world of drone programming and the art of orchestrating a graceful square trajectory using dronekit-python, I’m thrilled to have been your guide through the skies of technology.

But, hey, the adventure doesn’t end here! Your thoughts, ideas, and feedback are the secret ingredients that make this drone odyssey truly remarkable. Let your voice soar in the comments below, and let’s spark a conversation that takes flight!

Remember, sharing the thrill of mastering drone programming is a gift that keeps on giving. So, if you found this article to be your compass in the vast skies of knowledge, why not share the joy with your friends? After all, sharing is caring, and together, we can inspire the next generation of drone enthusiasts.

Thank you for joining me on this aerial expedition.

  1. Square, Wikipedia. ↩︎

Please consider supporting this project!

If this article has been of help to you, and you feel generous at the moment, donโ€™t hesitate to buy us a coffee. It's an easy, fun and direct way to show your support โ€” any amount of coffee is highly appreciated.
Buy me a Coffee