Skip to content

DaTiC0/python-ardrone-2.0

ย 
ย 

Repository files navigation

Python AR.Drone 2.0 Library

A comprehensive Python 3 library for controlling Parrot AR.Drone 2.0 quadcopters with HD video streaming, real-time navigation data, and advanced flight control capabilities.

๐ŸŒŸ Key Features

  • ๐ŸŽฅ HD Video Streaming - True 720p H.264 video with 1280x720 resolution
  • ๐Ÿ Full Python 3 Compatibility - Tested with Python 3.6+ and 3.12.3
  • ๐Ÿš Complete AR.Drone 2.0 Support - Optimized for AR.Drone 2.0 firmware 2.4.8+
  • ๐Ÿ“ก Advanced Network Management - Session management and proper codec configuration
  • ๐Ÿงฉ Modular Architecture - Clean, maintainable package structure
  • ๐ŸŽฎ Interactive Demo - Feature-rich pygame interface with HD video display
  • ๐Ÿ”ง Mock Mode - Testing capabilities without physical hardware
  • โšก Real-time Data - Live navigation data, sensor readings, and flight status

๐ŸŽฏ AR.Drone 2.0 Optimizations

This library includes comprehensive AR.Drone 2.0 specific improvements:

Video System Enhancements

  • H.264 Decoder: Native H.264 video decoding with ffmpeg integration
  • PAVE Parser: Proper Parrot Video Encapsulation frame processing
  • HD Support: Both 720p HD and 360p SD video modes
  • Session Management: Proper AR.Drone 2.0 session/profile/app ID handling
  • Multi-threading: Background video and navigation data processing

Technical Specifications

Feature HD Mode SD Mode
Resolution 1280ร—720 640ร—360
Codec H.264 (ID: 131) H.264 (ID: 129)
Bitrate 4 Mbps 1 Mbps
FPS 30 30
Format numpy arrays numpy arrays

๐Ÿ“ฆ Installation

Prerequisites

  • Python 3.6+ (Python 3.12.3 recommended)
  • ffmpeg (for H.264 video decoding)
  • AR.Drone 2.0 with firmware 2.4.8+

Quick Setup

# Clone the repository
git clone https://github.com/DaTiC0/python-ardrone-2.0.git
cd python-ardrone-2.0

# Create virtual environment
python3 -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

# Install dependencies
pip install numpy pygame

# Install ffmpeg (for video decoding)
# Ubuntu/Debian: sudo apt install ffmpeg
# macOS: brew install ffmpeg
# Windows: Download from https://ffmpeg.org/

# Test with mock mode
python demo_hd.py --mock

# Run with real drone
python demo_hd.py

๐Ÿš€ Usage

Modern API (Recommended)

from libardrone import ARDrone2

# HD video with context manager
with ARDrone2(hd_video=True) as drone:
    # Takeoff and basic flight
    drone.takeoff()
    
    # Get real-time video (numpy array)
    image = drone.get_image()  # Shape: (720, 1280, 3) for HD
    
    # Get navigation data
    navdata = drone.get_navdata()
    battery = drone.get_battery()
    altitude = drone.get_altitude()
    roll, pitch, yaw = drone.get_attitude()
    
    # Flight controls
    drone.move(roll=0.1, pitch=0.1, yaw=0.1, gaz=0.1)
    drone.hover()
    drone.land()

# Mock mode for testing
with ARDrone2(mock=True) as drone:
    print("Testing without hardware")

Legacy API (Backward Compatible)

import libardrone

# Original API still works
drone = libardrone.ARDrone()
drone.takeoff()
drone.move_forward()
drone.land()
drone.halt()

Advanced Features

from libardrone import ARDrone2, H264Decoder, PAVEParser

# Custom video processing
decoder = H264Decoder(frame_size=(1280, 720))
parser = PAVEParser()

# Advanced configuration
drone = ARDrone2(
    ip='192.168.1.1',
    hd_video=True,  # 720p HD mode
    mock=False      # Use real hardware
)

with drone:
    # Check systems status
    print(f"Video ready: {drone.is_video_ready()}")
    print(f"Flying: {drone.is_flying()}")
    print(f"Emergency: {drone.is_emergency()}")
    
    # Real-time monitoring
    while True:
        navdata = drone.get_navdata()
        if navdata:
            print(f"Battery: {navdata.vbat_flying_percentage}%")
            print(f"Altitude: {navdata.altitude}mm")
            break

๐ŸŽฎ Demo Applications

HD Demo (Recommended)

# HD video with full interface
python demo_hd.py

# SD video mode
python demo_hd.py --sd

# Mock mode for testing
python demo_hd.py --mock

# Custom IP address
python demo_hd.py --ip 192.168.1.100

Demo Controls

Key Action Key Action
Space Takeoff/Land E Emergency Stop
Arrow Keys Move A/D Rotate Left/Right
W/S Up/Down Q Quit

Legacy Demo

python demo.py  # Original demo (still works)

๐Ÿ—๏ธ Architecture

Modular Package Structure

libardrone/
โ”œโ”€โ”€ __init__.py          # Main package exports
โ”œโ”€โ”€ ardrone.py           # ARDrone2 main class
โ”œโ”€โ”€ arnetwork.py         # Network management
โ”œโ”€โ”€ at.py                # AT command interface
โ”œโ”€โ”€ h264decoder.py       # H.264 video decoder
โ”œโ”€โ”€ navdata.py           # Navigation data parser
โ””โ”€โ”€ paveparser.py        # PAVE video frame parser

Key Components

  • ARDrone2: Main control class with HD video and threading
  • ARNetworkManager: Socket handling with session management
  • H264Decoder: ffmpeg-based video decoding with mock fallback
  • PAVEParser: Parrot Video Encapsulation frame extraction
  • NavDataDecoder: Complete navigation data parsing
  • AT Commands: Low-level drone communication

๐Ÿ”ง Troubleshooting

Video Issues

  • No video output: Install ffmpeg, check network connection
  • Poor video quality: Try SD mode with --sd flag
  • Video lag: Ensure strong WiFi signal, reduce bitrate if needed

Connection Issues

  • Can't connect: Verify AR.Drone WiFi network connection
  • "Not connected to drone" error: This has been fixed in the latest version - ensure you're using the updated library
  • Port conflicts: Use python cleanup_ports.py to resolve port binding issues
  • Connection timeout: Check that drone IP is 192.168.1.1 and ports 5554-5556 are accessible
  • Emergency state: Use emergency reset before takeoff

Connection Diagnostics

# Test basic connectivity
python test_connection.py

# Clean up port conflicts if needed
python cleanup_ports.py

# Run with mock mode for testing
python demo_hd.py --mock

Performance Tips

  • HD video requires good WiFi signal and processing power
  • Mock mode for development without hardware
  • SD mode for lower bandwidth/processing requirements

๐Ÿ“š API Reference

ARDrone2 Class Methods

Connection

  • connect() - Connect to drone
  • disconnect() - Disconnect and cleanup
  • Context manager support with with statement

Flight Control

  • takeoff() - Take off
  • land() - Land
  • emergency() - Emergency stop
  • move(roll, pitch, yaw, gaz) - Move with specified controls
  • hover() - Hover in place

Data Access

  • get_image() - Get latest video frame (numpy array)
  • get_navdata() - Get navigation data object
  • get_battery() - Get battery percentage
  • get_altitude() - Get altitude in mm
  • get_attitude() - Get (roll, pitch, yaw) in degrees
  • get_velocity() - Get (vx, vy, vz) in mm/s

Status Checks

  • is_flying() - Check if drone is flying
  • is_emergency() - Check emergency state
  • is_video_ready() - Check video system status

๐Ÿ—๏ธ Repository History

This repository represents the evolution of AR.Drone Python libraries:

  1. Original Library: venthur/python-ardrone

    • Created by Bastian Venthur
    • Basic AR.Drone 1.0/2.0 support
    • Python 2.x compatible
  2. AR.Drone 2.0 Fork: python-ardrone-master

    • Enhanced by Adrian Taylor and others
    • AR.Drone 2.0 specific improvements
    • H.264 decoder and HD video support
    • Advanced PAVE parsing
  3. This Repository: DaTiC0/python-ardrone-2.0

    • Integrated best features from both repositories
    • Full Python 3 compatibility
    • Modular architecture
    • Modern API design
    • Comprehensive testing and documentation

๐Ÿค Contributing

Contributions are welcome! Areas for improvement:

  • Video Codecs: Additional codec support
  • Navigation: Enhanced autonomous flight features
  • Testing: More comprehensive test coverage
  • Documentation: API examples and tutorials
  • Performance: Optimization for different hardware

๐Ÿ“„ License

This software is published under the terms of the MIT License.

๐Ÿ™ Acknowledgments

  • Bastian Venthur (venthur) - Original python-ardrone implementation
  • Adrian Taylor - AR.Drone 2.0 enhancements, H.264 decoder, and PAVE parser
  • Andreas Bresser - AR.Drone 2.0 improvements and testing
  • AR.Drone Community - Continued support and development
  • Contributors - Everyone who helped modernize this library for Python 3 import libardrone import time

drone = libardrone.ARDrone() drone.enable_video_stream() time.sleep(3) # Wait for initialization

Check video status

if 'drone_state' in drone.navdata: state = drone.navdata['drone_state'] print(f"Video enabled: {state.get('video_mask', 0)}") print(f"Video thread: {state.get('video_thread_on', 0)}")

Check for video data

if drone.image: print(f"Video working! Frame size: {len(drone.image)} bytes") else: print("No video data received")

drone.halt()


**For complete documentation, installation, and API reference, see the full README content below.**

---

## ๐Ÿ“ฆ Installation

### Quick Start

```bash
# Clone the repository
git clone https://github.com/DaTiC0/python-ardrone-2.0.git
cd python-ardrone-2.0

# Create virtual environment
python3 -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

# Install dependencies
pip install pygame

# Run the demo
python demo.py

Requirements

  • Python 3.12+ (Python 2 no longer supported)
  • Pygame 2.x (for the demo application)
  • AR.Drone with firmware 1.5.1 (unmodified)

๐ŸŽฎ Usage

Basic API

import libardrone

# Connect to drone
drone = libardrone.ARDrone()

# Basic flight operations
drone.takeoff()    # Automatically resets emergency state
drone.hover()
drone.move_forward()
drone.turn_left()
drone.land()
drone.halt()       # Clean shutdown

# Access live data
image = drone.image    # Latest camera frame
navdata = drone.navdata  # Navigation/telemetry data

Demo Application Features

Run the demo with: python demo.py

Key Action
RETURN Takeoff (with auto emergency reset)
SPACE Land
BACKSPACE Manual emergency reset
W/S Move forward/backward
A/D Move left/right
โ†‘/โ†“ Adjust altitude
โ†/โ†’ Turn left/right
1-0 Set speed (0.1 to 1.0)
T Toggle test pattern (video testing)
ESC Exit application

๐Ÿ”ง Troubleshooting

No Video Output?

  1. Test the video system: Press T in the demo to show test pattern
  2. Check connection: Ensure you're connected to AR.Drone WiFi
  3. Verify IP: Drone should be accessible at 192.168.1.1
  4. Check diagnostics: Demo shows connection status and data availability

Takeoff Issues?

  • The library now automatically resets emergency state before takeoff
  • If takeoff still fails: Press BACKSPACE then RETURN
  • Emergency landing can be triggered with BACKSPACE

Port Conflicts?

  • The library now uses SO_REUSEADDR for better port handling
  • Use Ctrl+C for graceful shutdown to prevent port conflicts

๐Ÿ”— Connection Setup

  1. Connect to AR.Drone WiFi network (usually named ardrone2_...)
  2. Verify drone IP: Should be 192.168.1.1
  3. Ensure ports 5554-5556 are not blocked by firewall
  4. Run the demo: python demo.py

๐Ÿ“‹ What Was Fixed

Python 2 โ†’ 3 Migration

  • Fixed all print statements to use parentheses
  • Changed raw_input() to input()
  • Fixed integer division operators (/ โ†’ //)
  • Updated socket communication to handle bytes properly
  • Fixed string/bytes handling in struct operations

Enhanced Features

  • Auto-reset takeoff: Eliminates manual emergency reset needs
  • Better error handling: Graceful degradation when drone not connected
  • Improved demo: Shows instructions and connection status
  • Test patterns: Video system verification without real drone
  • Diagnostic info: Real-time connection and data status

๐Ÿ—๏ธ Repository

This repository is a modernized fork of the original AR.Drone Python library:


๐Ÿ“„ License

This software is published under the terms of the MIT License.


๐Ÿค Contributing

Contributions are welcome! Please feel free to:

  • Report bugs and issues
  • Suggest new features
  • Submit pull requests
  • Improve documentation

๐Ÿ™ Acknowledgments

  • Thanks to venthur for the original implementation
  • AR.Drone community for continued support and development
  • Contributors who helped modernize this library for Python 3

About

Python library for the AR.Drone 2.0

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%