pyparrot
repository·master·Indexed 43 days ago
https://github.com/amymcgovern/pyparrotA Python library providing an interface to control Parrot Mambo and Bebop 2 drones, designed for autonomous flight programming and educational STEM applications. It supports WiFi and Bluetooth Low Energy (BLE) connections, offering functionality for flight control (takeoff, landing, direct and relative movement), camera management, and real-time sensor data handling via callbacks.
What's inside pyparrot
- pyparrot is a Python interface designed for programming Parrot Mambo and Parrot Bebop 2 drones. It is primarily used for teaching STEM concepts (programming, math, etc.) through autonomous drone flight. It supports both WiFi and Bluetooth Low Energy (BLE) connections.
Overview of the pyparrot package structure
masterThe
pyparrotpackage is organized into several subpackages and modules designed for drone control, vision, and networking.Subpackages
pyparrot.commandsandsensors: Likely contains interfaces for drone commands and sensor data handling.pyparrot.networking: Handles network communication protocols.pyparrot.scripts: Contains executable scripts or utility scripts.pyparrot.utils: General purpose utility functions.
Core Modules
pyparrot.Bebop: Interface for controlling Parrot Bebop drones.pyparrot.DroneVision: Provides drone vision capabilities.pyparrot.DroneVisionGUI: Graphical User Interface for drone vision tasks.pyparrot.Minidrone: Interface for controlling Minidrone hardware.pyparrot.VisionServer: Provides server-side capabilities for vision data streaming or processing.
Use the pyparrot.networking package for drone communication
masterThe
pyparrot.networkingpackage provides submodules for establishing network connections with drones. It supports two primary communication methods:- Bluetooth Low Energy (BLE): Handled by the
pyparrot.networking.bleConnectionmodule. - Wi-Fi: Handled by the
pyparrot.networking.wifiConnectionmodule.
Developers should use these modules to abstract the underlying transport layer when sending commands or receiving telemetry from a drone.
- Bluetooth Low Energy (BLE): Handled by the
Use smart_sleep to prevent BLE disconnection
masterWhen using BLE, you must NEVER use
time.sleep(), as it will cause the BLE connection to disconnect regularly. Instead, usesmart_sleep(seconds)(wheresecondscan be a float). This method sleeps while remaining awake for BLE or WiFi notifications. If you are using WiFi,time.sleep()is acceptable, butsmart_sleep()is compatible with both.# Correct way to pause in a BLE-connected script import pyparrot mambo.smart_sleep(2.5)Hardware and Connection Compatibility
masterThe required interface depends on your specific Parrot drone model:
Drone Model Interface OS Support Parrot Mambo FPV WiFi Mac, Linux, Windows Parrot Mambo Fly/Code BLE Linux only Parrot Swing BLE Linux only Parrot Bebop 2 WiFi Any WiFi enabled device Parrot Bebop 1 WiFi Any WiFi enabled device Parrot Anafi SDK (Pending) TBD Use libVLC for low-latency vision
masterThe
DroneVisionGUIclass provides a vision approach using thelibVLClibrary. This method processes video entirely in memory, minimizing delay compared to theffmpegapproach. It is ideal for real-time vision tasks where you do not need to save the full image stream to disk.Requirement: You MUST install the VLC client version 3.0.1 or greater. Installing only
libvlcis insufficient; the full VLC client installs the extra software required by thelibvlcPython interface.To control the drone while vision is running, you must provide a function to the
user_code_to_runparameter in theDroneVisionGUIconstructor.mamboVision = DroneVisionGUI(mambo, is_bebop=False, buffer_size=200, user_code_to_run=demo_mambo_user_vision_function, user_args=(mambo, ))Handle Bebop sensor data with callbacks
masterSensor data is stored in a
BebopSensorsobject. The most efficient way to consume sensor updates is to register a callback function that triggers every time the sensors are updated (typically at 10Hz via WiFi).Use
set_user_sensor_callback(function, args)to register your handler.Key Sensor Attributes
battery: Battery level (defaults to 100 until real data arrives).flying_state: Current state. Possible values:"landed","takingoff","hovering","flying","landing","emergency","usertakeoff","motor_ramping","emergency_landing".sensors_dict: A dictionary containing all other sensor data defined in the drone's XML configuration.
def my_sensor_handler(sensor_data, user_arg): print(f"Drone state: {sensor_data.flying_state}") print(f"Battery: {sensor_data.battery}%") # Register the callback drone.set_user_sensor_callback(my_sensor_handler, "my_custom_arg")Establish a connection via Wi-Fi
masterUse thepyparrot.networking.wifiConnectionmodule to manage connections to drones over a Wi-Fi network. This module provides the interface for establishing and managing socket-based or protocol-specific communication over Wi-Fi.Use ffmpeg for drone vision
masterTo avoid unreliable RTSP/RTP streams in OpenCV,
pyparrotprovides a vision approach usingffmpeg. This method usesffmpegto decode the raw video stream (RTSP for Mambo, RTP for Bebop) and convert it into PNG images that OpenCV can process.Key Characteristics:
- Pros: More reliable stream handling than direct OpenCV.
- Cons: Introduces a slight delay (approx. 0.5s on high-end SSDs) because
ffmpegmust write images to disk. - Storage: Images are saved to a directory named
imagesinside thepyparrotpackage. You must manually clean this folder after each flight. - Requirements: Requires both
ffmpegsoftware and theopencv(cv2) package installed on your system. - Visualization: This approach does not show a live stream; use
VisionServer.pyto visualize the images.
# Requirements: ffmpeg software and opencv packageEstablish a connection via Bluetooth Low Energy (BLE)
masterUse thepyparrot.networking.bleConnectionmodule to manage connections to drones via BLE. This module contains the necessary classes and methods to initialize a BLE session and maintain communication with the drone's BLE interface.Connect to a Bebop drone
masterTo use the
pyparrotlibrary with a Bebop, you must first connect your controlling device (laptop, computer, etc.) to the drone's Wi-Fi network. Look for a network namedBebop_number(wherenumbervaries by drone). Once connected, use theBebop.connect()method to establish a connection via the library.from pyparrot.Bebop import Bebop bebop = Bebop() success = bebop.connect(10)Install required software dependencies
masterPyparrot requires several core packages and connection-specific libraries.
Core Requirements (All Drones):
untangle: Used to parse XML files in the Parrot SDK.Python 3: Pyparrot only works with Python 3.
Connection-Specific Requirements:
- WiFi Connection: Requires
zeroconf. - BLE Connection (Linux only, no camera): Requires
pybluezand potentiallybluepy. - Vision (Camera processing): Requires
opencvand eitherffmpegorVLC(VLC must be version 3.0.1 or greater).
# Core requirement pip install untangle # WiFi connection requirement pip install zeroconf # BLE connection requirements (Linux only) sudo apt-get install bluetooth sudo apt-get install bluez sudo apt-get install python-bluez sudo apt-get install python-pip libglib2.0-dev sudo pip install bluepy sudo apt-get update