pyparrot

repository·master·Indexed 43 days ago

https://github.com/amymcgovern/pyparrot

A 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.

Tokens
11.6K
Snippets
39
Records
56
Agent score
54%

What's inside pyparrot

  1. Overview of pyparrot

    master
    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.
  2. Overview of the pyparrot package structure

    master

    The pyparrot package 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.
  3. Use the pyparrot.networking package for drone communication

    master

    The pyparrot.networking package provides submodules for establishing network connections with drones. It supports two primary communication methods:

    1. Bluetooth Low Energy (BLE): Handled by the pyparrot.networking.bleConnection module.
    2. Wi-Fi: Handled by the pyparrot.networking.wifiConnection module.

    Developers should use these modules to abstract the underlying transport layer when sending commands or receiving telemetry from a drone.

  4. Use smart_sleep to prevent BLE disconnection

    master

    When using BLE, you must NEVER use time.sleep(), as it will cause the BLE connection to disconnect regularly. Instead, use smart_sleep(seconds) (where seconds can be a float). This method sleeps while remaining awake for BLE or WiFi notifications. If you are using WiFi, time.sleep() is acceptable, but smart_sleep() is compatible with both.

    # Correct way to pause in a BLE-connected script
    import pyparrot
    mambo.smart_sleep(2.5)
  5. Hardware and Connection Compatibility

    master

    The required interface depends on your specific Parrot drone model:

    Drone ModelInterfaceOS Support
    Parrot Mambo FPVWiFiMac, Linux, Windows
    Parrot Mambo Fly/CodeBLELinux only
    Parrot SwingBLELinux only
    Parrot Bebop 2WiFiAny WiFi enabled device
    Parrot Bebop 1WiFiAny WiFi enabled device
    Parrot AnafiSDK (Pending)TBD
  6. Use libVLC for low-latency vision

    master

    The DroneVisionGUI class provides a vision approach using the libVLC library. This method processes video entirely in memory, minimizing delay compared to the ffmpeg approach. 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 libvlc is insufficient; the full VLC client installs the extra software required by the libvlc Python interface.

    To control the drone while vision is running, you must provide a function to the user_code_to_run parameter in the DroneVisionGUI constructor.

    mamboVision = DroneVisionGUI(mambo, is_bebop=False, buffer_size=200, 
                                 user_code_to_run=demo_mambo_user_vision_function, 
                                 user_args=(mambo, ))
  7. Handle Bebop sensor data with callbacks

    master

    Sensor data is stored in a BebopSensors object. 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")
  8. Use ffmpeg for drone vision

    master

    To avoid unreliable RTSP/RTP streams in OpenCV, pyparrot provides a vision approach using ffmpeg. This method uses ffmpeg to 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 ffmpeg must write images to disk.
    • Storage: Images are saved to a directory named images inside the pyparrot package. You must manually clean this folder after each flight.
    • Requirements: Requires both ffmpeg software and the opencv (cv2) package installed on your system.
    • Visualization: This approach does not show a live stream; use VisionServer.py to visualize the images.
    # Requirements: ffmpeg software and opencv package
  9. Connect to a Bebop drone

    master

    To use the pyparrot library with a Bebop, you must first connect your controlling device (laptop, computer, etc.) to the drone's Wi-Fi network. Look for a network named Bebop_number (where number varies by drone). Once connected, use the Bebop.connect() method to establish a connection via the library.

    from pyparrot.Bebop import Bebop
    
    bebop = Bebop()
    success = bebop.connect(10)
  10. Install required software dependencies

    master

    Pyparrot 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 pybluez and potentially bluepy.
    • Vision (Camera processing): Requires opencv and either ffmpeg or VLC (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