The Ultimate Guide to MAVSDK-Python Programming [Part 6]: Analyzing MAVSDK-Python Basic Sample Programs
Hello and welcome back, university students and researchers dedicating yourselves to autonomous flight robotics and drone control algorithms! Welcome to the sixth installment of our MAVSDK-Python blog series.
In Part 5, we successfully navigated the biggest programming hurdle: understanding Python’s asynchronous programming (asyncio) syntax from the perspective of drone control. Now, we have acquired both the “vocabulary” (Classes) to issue commands to the drone and the “conversational method” (Asynchronous Processing) to communicate efficiently.
When learning a new language, the fastest way to improve is to analyze excellent sentences written by native speakers. Similarly, the fastest way to master MAVSDK-Python programming is to analyze and execute the official sample programs provided.
In this [Part 6: Analyzing MAVSDK-Python Basic Sample Programs], we will tear down the core sample codes that form the foundation of autonomous flight research. I will explain in detail and in a friendly manner how the theories we have learned so far are actually implemented in real code.
1. How to Prepare and Run Sample Programs
Before we analyze the code, you first need to download the sample programs to your PC or educational Virtual Machine (VM). Open your terminal and enter the following commands to clone the tutorial code from the GitHub repository.
cd ~
git clone https://github.com/maponarooo/mavsdk-python-tutorials
cd mavsdk-python-tutorials
(💡 Note: If you are using the educational VM environment provided by QUAD Drone Lab, the sample programs are already prepared for you in the ~/quad-dev/MAVSDK-Python/examples directory.)
Once the download is complete, ensure your SITL (Virtual Simulator) is running in the background. You can then execute the Python script below to see if the virtual vehicle moves as expected.
python3 takeoff_and_land.py

2. Basic Flight Control: takeoff_and_land.py & goto.py
Let’s start by looking at the most fundamental flight control samples. These samples all utilize the mavsdk.action.Action class to control the vehicle’s movement.
① takeoff_and_land.py (Takeoff and Landing Sample)
This code performs the alpha and omega of drone flight: taking off and landing. It calls the methods within the Action class sequentially.
- await drone.action.arm(): Starts the drone’s propellers spinning (Arming).
- await drone.action.takeoff(): Commands the drone to take off to a predefined altitude.
- await drone.action.land(): Commands the drone to descend from its current position and land.
There is a crucial point researchers must keep in mind here. If you do not issue the takeoff() command within a few seconds after the arm() command, the drone will automatically disarm itself for safety reasons.
Furthermore, if you attempt to arm() without a stable GPS signal lock, an ActionError: COMMAND_DENIED exception will occur, and the program will terminate. This is not a bug; it means the Flight Controller (FC) sensed danger and rejected the command. Therefore, you should always build a habit of using try... except blocks to handle these exceptions gracefully.
② goto.py (Move to Target Coordinates Sample)
This sample is used when you want the vehicle to fly straight to a specific GPS coordinate after taking off.
- await drone.action.goto_location(latitude_deg, longitude_deg, absolute_altitude_m, yaw_deg)
By inputting the parameters for latitude (latitude_deg), longitude (longitude_deg), absolute altitude above sea level (absolute_altitude_m), and the heading direction of the nose (yaw_deg), the vehicle will smoothly fly to that location. This is incredibly useful for research scenarios that require urgent dispatch to specific points.
3. Drone State Monitoring: telemetry.py
Monitoring the vehicle’s state in real-time and collecting data is just as important as flight control. To draw graphs for your research papers, data logging is absolutely essential. The sample responsible for this role is telemetry.py.
This sample utilizes the mavsdk.telemetry.Telemetry class and actively employs the asynchronous loop async for that we learned in the previous post.
import asyncio
from mavsdk import System
async def print_battery(drone):
# An asynchronous stream that receives battery info infinitely
async for battery in drone.telemetry.battery():
print(f"Battery Voltage: {battery.voltage_v}V, Remaining: {battery.remaining_percent * 100}%")
async def run():
drone = System()
await drone.connect()
# ... connection verification code omitted ...
# Execute the battery printing function as a background task
asyncio.ensure_future(print_battery(drone))
# Wait for 30 seconds, allowing the background task to run
await asyncio.sleep(30)
By applying this sample code, you can monitor not only battery() but also gps_info() (number of GPS satellites), in_air() (whether it is flying), and position() (latitude, longitude, altitude) all at once.
4. Waypoint Autonomous Flight: mission.py
The mission.py sample is a script that makes the drone sequentially visit multiple Waypoints to perform complex tasks. It uses the mavsdk.mission.Mission class, which is essential for implementing agricultural drone spraying routes or zigzag flight patterns for 3D mapping.
The execution flow of the code is as follows:
- Create multiple
MissionItemobjects (containing waypoint coordinates, altitude, travel speed, camera trigger flags, etc.) and bundle them into a list. - await drone.mission.upload_mission(mission_plan): Uploads the created mission plan to the vehicle.
- await drone.mission.start_mission(): Commands the vehicle to start the uploaded mission.
- await drone.mission.set_return_to_launch_after_mission(True): By enabling this option, the drone will automatically return to its original launch position (RTL: Return To Launch) after completing the last waypoint mission.
If you execute this code and check your QGroundControl (QGC) screen, you will see the exact path you programmed visualized on the QGC map, and the drone faithfully following it.
5. Advanced Control (Preview): Offboard-related Samples
If you are researching computer vision-based object tracking or real-time obstacle avoidance algorithms beyond the undergraduate level, simple GPS movements (goto) or waypoint flights (mission) won’t provide the precision you need.
You must use Offboard mode, where a companion computer injects control signals dozens of times per second to pilot the vehicle. MAVSDK provides a rich set of sample programs for this mode as well.
- offboard_position_ned.py: Commands positions based on the Local NED (North-East-Down) coordinate system. E.g., “Go 5m North and 10m East from the origin!” (
set_position_ned()). - offboard_velocity_body.py: Commands velocities based on the vehicle’s current orientation (Body frame). E.g., “Fly forward at 2m/s and right at 1m/s!” (
set_velocity_body()). This is highly intuitive when moving based on where the camera is pointing. - offboard_velocity_ned.py: Commands velocities based on global headings (NED). E.g., “Move North at a speed of 2m/s!” (
set_velocity_ned()). - Offboard_position_velocity_ned.py: An advanced control method that provides both target position and velocity information (Feed-Forward) simultaneously (
set_position_velocity_ned()). This helps the Position Controller reach the target point much faster and with greater precision.
Offboard control is immensely powerful, but it is also a dangerous mode where even a slight bug in your code can cause the vehicle to crash. Therefore, we will cover this mode in exquisite detail in a dedicated standalone chapter in our next installment.
Wrapping Up
In Part 6, we analyzed the structure and core functions of the basic sample programs (takeoff_and_land, goto, telemetry, mission, etc.), which serve as excellent educational materials provided by MAVSDK-Python.
Reading code with your eyes is entirely different from typing it out and executing it yourself. I strongly encourage you to run these samples in your own development environment and try tweaking the coordinates or takeoff altitudes to suit your taste.
In our next post, [Part 7: Understanding OFFBOARD Mode], we will dig deep into the concepts and prerequisites of Offboard mode—the crown jewel of drone control and the topic that researchers encounter and ask about the most.
If you encounter any questions or bugs while running the samples, please feel free to leave a comment anytime. I sincerely cheer for your successful research and flights!
YouTube Class

Author: maponarooo, CEO of QUAD Drone Lab
Date: March 3, 2026
