Skip to content

durellwilson/swift-dji-drone

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🚁 Swift DJI Drone Controller

Open-source Swift package for autonomous DJI drone flight control.

✨ Features

Core Functionality

  • Drone Connection - UDP/TCP communication with DJI drones
  • Command Execution - Send flight commands
  • State Monitoring - Real-time telemetry
  • Autonomous Missions - Waypoint-based flight plans

Advanced Features

  • Computer Vision - Object detection and tracking
  • Safety Controller - Automatic safety checks
  • Mission Planning - Complex flight paths
  • Actor-based - Thread-safe Swift 6.0

πŸš€ Quick Start

Installation

dependencies: [
    .package(url: "https://github.com/durellwilson/swift-dji-drone.git", from: "1.0.0")
]

Basic Usage

import SwiftDJIDrone

// Connect to drone
let connection = DroneConnection(host: "192.168.10.1", port: 8889)
try await connection.connect()

// Check state
let state = try await connection.getState()
print("Battery: \(state.battery)%")

// Send command
let response = try await connection.sendCommand(.takeoff)

Autonomous Mission

let controller = AutonomousController(connection: connection)

let mission = Mission(
    name: "Survey Mission",
    waypoints: [
        Waypoint(action: .takeoff),
        Waypoint(action: .moveTo(x: 100, y: 0, z: 50, speed: 50)),
        Waypoint(action: .capturePhoto),
        Waypoint(action: .land),
    ]
)

try await controller.executeMission(mission)

Computer Vision

let vision = VisionController()

let objects = await vision.detectObjects(in: frame)
for object in objects {
    print("\(object.label): \(object.confidence)")
}

Safety Monitoring

let safety = SafetyController()

// Start monitoring
Task {
    await safety.startMonitoring(connection: connection)
}

// Check if safe to continue
if await safety.shouldAbortMission(state) {
    try await controller.emergencyStop()
}

🎯 Use Cases

Autonomous Inspection

let mission = Mission(
    name: "Building Inspection",
    waypoints: [
        Waypoint(action: .takeoff),
        Waypoint(action: .moveTo(x: 0, y: 0, z: 100, speed: 30)),
        Waypoint(action: .startVideo),
        Waypoint(action: .moveTo(x: 200, y: 0, z: 100, speed: 20)),
        Waypoint(action: .moveTo(x: 200, y: 200, z: 100, speed: 20)),
        Waypoint(action: .stopVideo),
        Waypoint(action: .land),
    ]
)

Object Tracking

let vision = VisionController()

while tracking {
    let objects = await vision.detectObjects(in: currentFrame)
    
    if let target = objects.first(where: { $0.label == "person" }) {
        let tracking = await vision.trackObject(target)
        
        // Adjust drone position to follow
        try await connection.sendCommand(.moveTo(
            x: Int(tracking.position.x),
            y: Int(tracking.position.y),
            z: Int(tracking.position.z)
        ))
    }
}

Aerial Photography

let mission = Mission(
    name: "Aerial Photography",
    waypoints: [
        Waypoint(action: .takeoff),
        Waypoint(action: .moveTo(x: 0, y: 0, z: 150, speed: 30)),
        Waypoint(action: .rotate(degrees: 360)),
        Waypoint(action: .capturePhoto),
        Waypoint(action: .moveTo(x: 100, y: 100, z: 150, speed: 30)),
        Waypoint(action: .capturePhoto),
        Waypoint(action: .land),
    ]
)

πŸ—οΈ Architecture

Actor-Based Concurrency

public actor DroneConnection {
    // Thread-safe drone communication
}

public actor AutonomousController {
    // Thread-safe mission execution
}

public actor VisionController {
    // Thread-safe computer vision
}

Type-Safe Commands

public enum DroneCommand: String {
    case takeoff
    case land
    case forward
    case back
    // ...
}

Sendable Types

All types conform to Sendable for Swift 6 strict concurrency.

πŸ§ͺ Testing

swift test

Tests include:

  • Connection management
  • Command execution
  • Autonomous missions
  • Computer vision
  • Safety checks

πŸ“‘ DJI SDK Integration

Tello Protocol

Compatible with DJI Tello drones using UDP protocol:

  • Command port: 8889
  • State port: 8890
  • Video port: 11111

DJI Mobile SDK

Can be extended to work with DJI Mobile SDK:

// Bridge to DJI Mobile SDK
extension DroneConnection {
    func connectDJISDK() async throws {
        // Initialize DJI SDK
        // Register app
        // Connect to aircraft
    }
}

πŸ”§ CLI Tool

# Build CLI
swift build -c release

# Run autonomous mission
.build/release/drone-cli

# Output:
# 🚁 Swift DJI Drone Controller
# βœ… Connected to drone
# πŸ“Š Battery: 85%
# 🚁 Starting mission: Survey Mission
# πŸ“ Waypoint 1/10
# βœ… Mission complete!

🌟 Advanced Features

Obstacle Avoidance

let obstacles = await vision.detectObstacles(in: frame)

for obstacle in obstacles {
    if obstacle.distance < 2.0 {
        // Stop and reroute
        try await controller.emergencyStop()
    }
}

GPS Waypoint Navigation

struct GPSWaypoint {
    let latitude: Double
    let longitude: Double
    let altitude: Double
}

extension AutonomousController {
    func navigateToGPS(_ waypoint: GPSWaypoint) async throws {
        // Convert GPS to relative coordinates
        // Navigate to position
    }
}

Real-Time Video Streaming

actor VideoStream {
    func startStream() async throws {
        // Connect to video port 11111
        // Decode H.264 stream
        // Process frames
    }
}

πŸ“Š Telemetry

public struct DroneState: Sendable {
    public let battery: Int          // 0-100%
    public let altitude: Double      // meters
    public let speed: Double         // m/s
    public let temperature: Int      // celsius
    public let isFlying: Bool
    public let gpsSignal: Int        // 0-5 satellites
}

πŸ” Safety Features

  • Battery monitoring - Auto-land at 15%
  • GPS signal check - Abort if < 2 satellites
  • Temperature monitoring - Alert at 60Β°C
  • Emergency stop - Immediate motor cutoff
  • Geofencing - Stay within boundaries
  • Collision avoidance - Detect obstacles

πŸŽ“ Examples

See Examples/ directory for:

  • Basic flight control
  • Autonomous missions
  • Computer vision tracking
  • Safety monitoring
  • Video streaming

πŸ“± Platform Support

  • βœ… iOS 18+
  • βœ… macOS 15+
  • βœ… Swift 6.0
  • βœ… Actor concurrency

🀝 Contributing

  1. Fork repository
  2. Create feature branch
  3. Add tests
  4. Submit PR

πŸ“„ License

MIT License - Open source for Detroit community

πŸ”— Resources


Built with ❀️ for autonomous drone flight in Swift

About

🚁 Open-source Swift package for autonomous DJI drone flight control with computer vision and safety features

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors