Skip to content

ArmanShirzad/crazyflieDemo

Repository files navigation

Crazyflie Swarm Demo

A comprehensive web-based demonstration of Crazyflie swarm control capabilities, designed to showcase first-week responsibilities for IMRCLab. Features a mock simulator with a clean migration path to production Crazyswarm2/ROS 2 integration.

Quick Start

Option 1: Local Development

# Backend (Mock Simulator) - Easy startup
cd backend
./start.sh

# Frontend (in new terminal)
cd frontend
python3 -m http.server 3000
# Open http://localhost:3000

Alternative Backend Setup:

# Manual setup if start.sh doesn't work
cd backend
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
uvicorn main:app --host 0.0.0.0 --port 8000

Option 2: Docker

# Build and run backend
docker build -t crazyflie-demo .
docker run -p 8000:8000 -e BACKEND=mock crazyflie-demo

# Serve frontend
cd frontend && python3 -m http.server 3000

Option 3: Live Demo

Design Features

Modern Color Scheme:

  • Primary: Teal/Cyan gradient (#0ea5e9 to #06b6d4)
  • Accent: Complementary blues and greens
  • Background: Light sky blue (#f0f9ff)
  • UI Elements: Consistent teal theming throughout

Visual Design:

  • Clean, modern interface inspired by current design trends
  • Responsive layout that works on desktop and mobile
  • Smooth animations and hover effects
  • Professional color palette suitable for technical demonstrations

Features Overview

Core Capabilities (60-20-10-10 Split)

Programming (60%) — Swarm Control & Coordination

  • Multi-drone Management: Create, control, and monitor drone swarms
  • Real-time Physics: 20Hz simulation with battery drain and sensor noise
  • Formation Control: Line, circle, grid, and V-shape formations
  • Collision Avoidance: Path planning with obstacle detection
  • Emergency Systems: Immediate stop and safety protocols

Flight Experiments (20%) — Automated Scenarios

  • Circular Formation: Coordinated circular flight patterns
  • Figure-8 Trajectory: Complex path following
  • Takeoff-Hover-Land: Basic flight sequence automation
  • Custom Scenarios: Extensible experiment framework
  • Data Logging: Complete trajectory and telemetry capture

Firmware Preparation (10%) — Build Tools & Documentation

  • ARM Toolchain Setup: Automated GCC installation
  • Firmware Building: Custom PID configurations
  • Flashing Tools: USB and radio deployment
  • Configuration Management: Aggressive vs. smooth flight modes
  • Troubleshooting Guides: Comprehensive debugging support

Algorithm Validation (10%) — Testing Framework

  • Path Planning: Collision avoidance algorithm testing
  • State Machine: Drone behavior validation
  • Performance Metrics: Path length, separation, timing analysis
  • Unit Tests: Comprehensive test coverage
  • Integration Testing: End-to-end system validation

API Reference

Authentication

All write operations require a Bearer token:

Authorization: Bearer demo-token

Core Endpoints

Create Swarm

POST /drones/create
Content-Type: application/json

{
  "count": 4
}

Response:
{
  "runId": "run_20231201_143022",
  "drones": ["d1", "d2", "d3", "d4"]
}

Drone Control

# Takeoff
POST /drones/d1/takeoff
{
  "height": 0.6,
  "duration": 2.0
}

# Move to Position
POST /drones/d1/goto
{
  "x": 1.0,
  "y": 0.0,
  "z": 0.6,
  "speed": 0.5
}

# Land
POST /drones/d1/land

# Get State
GET /drones/d1/state

Swarm Operations

# Set Formation
POST /swarm/formation
{
  "formation": "circle",
  "parameters": {"radius": 1.0, "height": 0.6}
}

# Emergency Stop
POST /emergency/stop

# Reset Simulation
DELETE /drones

Experiments

POST /experiments/run
{
  "scenario": "circular_formation",
  "numDrones": 4,
  "duration": 30,
  "parameters": {"radius": 1.0, "height": 0.5}
}

Data Export

# Get Logs (JSON)
GET /logs?runId=run_20231201_143022&format=json

# Get Logs (CSV)
GET /logs?runId=run_20231201_143022&format=csv

Architecture

Two-Phase Design

┌─────────────────────────────────────────────────────────────┐
│                    Frontend (Web UI)                        │
│  Three.js Visualization │ Control Panel │ Charts & Results   │
└─────────────────────────────────────────────────────────────┘
                                │
                                │ HTTP/REST API
                                │
┌─────────────────────────────────────────────────────────────┐
│                    Backend (FastAPI)                       │
│  REST API │ Simulator Layer │ Algorithm Layer │ Database   │
└─────────────────────────────────────────────────────────────┘
                                │
                                │ Configuration Switch
                                │
┌─────────────────────────────────────────────────────────────┐
│              Simulator Backends                             │
│  Phase 1: Mock Simulator    │  Phase 2: ROS 2 Integration  │
│  • Pure Python              │  • Crazyswarm2 Adapter      │
│  • 20Hz Physics Loop        │  • ROS 2 Services/Actions   │
│  • SQLite Logging           │  • Real Hardware Control     │
└─────────────────────────────────────────────────────────────┘

Phase 1: Mock Simulator (Current)

  • Backend: FastAPI with pure Python physics simulation
  • Frontend: Three.js 3D visualization with real-time updates
  • Database: SQLite for trajectory logging
  • Deployment: Railway + GitHub Pages

Phase 2: ROS 2 Integration (Future)

  • Backend: Same FastAPI with ROS 2 adapter
  • Hardware: Real Crazyflie drones via Crazyswarm2
  • Simulation: Optional Gazebo integration
  • Migration: Single environment variable switch

Migration to Production

Quick Migration

# Set environment variable
export BACKEND=crazyswarm2

# Install ROS 2 dependencies
sudo apt install ros-humble-desktop
pip install cflib crazyflie-lib-python

# Deploy with same API
uvicorn main:app --host 0.0.0.0 --port 8000

Detailed Migration Guide

See docs/MIGRATION_TO_ROS2.md for:

  • Complete ROS 2 Humble installation
  • Crazyswarm2 setup and configuration
  • Hardware preparation and firmware flashing
  • Production deployment strategies

Safety Features

Operational Safety

  • Workspace Bounds: 2×2×1 meter safe zone
  • Speed Limits: 1.0 m/s maximum velocity
  • Emergency Stop: Immediate motion cancellation
  • Rate Limiting: Prevents command flooding
  • Battery Monitoring: Automatic low-battery handling

Development Safety

  • Input Validation: All parameters bounds-checked
  • Error Handling: Graceful failure modes
  • State Management: Consistent drone state tracking
  • Collision Detection: Maintains safe separation distances

Performance Characteristics

Simulation Performance

  • Tick Rate: 20Hz (50ms intervals)
  • Update Rate: 10Hz client polling
  • Memory Usage: ~50MB for 10 drones
  • CPU Usage: ~10% on modern hardware

Network Performance

  • API Response Time: <100ms typical
  • Concurrent Users: 100+ (stateless design)
  • Data Export: CSV/JSON generation <1s

Scalability

  • Max Drones: 10 (configurable)
  • Database Size: ~1MB per hour of simulation
  • Horizontal Scaling: Load balancer ready

Testing

Unit Tests

cd backend
python -m pytest tests/ -v

Integration Tests

# Test complete system
python -m pytest tests/test_system.py::TestIntegration -v

Manual Testing

# Test API endpoints
curl -H "Authorization: Bearer demo-token" \
     http://localhost:8000/health

# Test frontend
open http://localhost:3000

Deployment

Production Deployment

  • Backend: Railway (containerized)
  • Frontend: GitHub Pages (static)
  • Database: SQLite (embedded)
  • Monitoring: Health check endpoints

Development Deployment

# Backend
cd backend
uvicorn main:app --reload

# Frontend
cd frontend
python3 -m http.server 3000

Docker Deployment

# Build image
docker build -t crazyflie-demo .

# Run container
docker run -p 8000:8000 \
  -e BACKEND=mock \
  -e BEARER_TOKEN=your-token \
  crazyflie-demo

Project Structure

crazyflie-swarm-demo/
├── backend/                    # FastAPI backend
│   ├── main.py                # Main application
│   ├── simulators/            # Simulator implementations
│   │   ├── mock_simulator.py  # Pure Python simulator
│   │   └── ros2_simulator.py  # ROS 2 adapter
│   ├── algorithms/            # Path planning algorithms
│   │   └── trajectory_planner.py
│   ├── tests/                 # Unit tests
│   └── requirements.txt       # Python dependencies
├── frontend/                  # Web frontend
│   ├── index.html            # Main HTML page
│   ├── js/                   # JavaScript modules
│   │   ├── api.js            # API client
│   │   ├── visualization.js   # Three.js visualization
│   │   ├── experiments.js    # Experiment management
│   │   └── app.js            # Main application
│   └── css/
│       └── style.css         # Styling
├── firmware/                 # Firmware build tools
│   ├── setup.sh             # ARM toolchain setup
│   ├── build.sh             # Firmware build
│   ├── flash.sh             # Firmware flashing
│   └── configs/             # PID configurations
├── docs/                     # Documentation
│   ├── ARCHITECTURE.md       # System architecture
│   ├── DEPLOYMENT.md         # Deployment guide
│   └── MIGRATION_TO_ROS2.md  # ROS 2 migration
├── Dockerfile               # Container configuration
└── README.md               # This file

Configuration

Environment Variables

BACKEND=mock                    # Simulator backend
LOG_LEVEL=info                  # Logging level
MAX_DRONES=10                   # Maximum drone count
SIMULATION_SPEED=1.0            # Simulation speed multiplier
BEARER_TOKEN=demo-token         # API authentication token

Frontend Configuration

// frontend/js/api.js
const API_BASE_URL = location.hostname === 'localhost'
  ? 'http://localhost:8000'
  : 'https://your-app.railway.app';

Troubleshooting

Common Issues

  1. Connection Problems

    # Check backend health
    curl http://localhost:8000/health
    
    # Check CORS settings
    curl -H "Origin: http://localhost:3000" \
         -H "Access-Control-Request-Method: POST" \
         -X OPTIONS http://localhost:8000/drones/create
  2. Performance Issues

    # Reduce simulation load
    export MAX_DRONES=5
    export SIMULATION_SPEED=0.5
  3. Memory Issues

    # Monitor memory usage
    docker stats
    
    # Clear old data
    curl -X DELETE http://localhost:8000/drones

Debug Tools

  • Backend Logs: FastAPI automatic logging
  • Browser Console: JavaScript error tracking
  • Network Tab: API request/response inspection
  • Database: SQLite browser for data inspection

Future Enhancements

Phase 3: Advanced Features

  • Machine Learning: Neural network path planning
  • Computer Vision: Obstacle detection and avoidance
  • Advanced Formations: Dynamic formations, leader-follower
  • Multi-Agent: Distributed decision making

Phase 4: Production Scale

  • Microservices: Split into specialized services
  • Message Queues: Redis/RabbitMQ for async processing
  • Distributed Simulation: Multiple simulation nodes
  • Real-time Analytics: Stream processing with Apache Kafka

Contributing

This is a demonstration project showcasing first-week capabilities for IMRCLab. The architecture is designed to be easily extensible for production use with real Crazyflie drones.

Development Guidelines

  • Follow PEP 8 for Python code
  • Use meaningful commit messages
  • Add tests for new features
  • Update documentation for API changes

Code Style

  • Python: Black formatter, type hints
  • JavaScript: ESLint, Prettier
  • Documentation: Markdown, clear examples

License

This project is designed as a demonstration for IMRCLab interview purposes. Please refer to individual component licenses for production use.

Acknowledgments

  • Bitcraze: Crazyflie hardware and firmware
  • USC ACTLab: Crazyswarm2 framework
  • FastAPI: Modern Python web framework
  • Three.js: 3D visualization library
  • Railway: Deployment platform

Built overnight to demonstrate understanding and initiative
Architecture mirrors Crazyswarm2 and ROS 2 patterns
Backend swap is a config flip; UI and contract remain stable
Covers 60-20-10-10 responsibilities end-to-end
Clear week-one plan to extend to real drones

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published