Skip to content

A Unity-based quadcopter/drone physics simulation implementing PID control systems for testing realistic flight dynamics.

Notifications You must be signed in to change notification settings

mihasm/unity-drone-control

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Unity Drone Control Simulator

A Unity-based quadcopter/drone physics simulation implementing advanced PID control systems for realistic flight dynamics.

Features

  • Realistic Physics Simulation: Uses Unity's physics engine with Rigidbody components for accurate drone behavior
  • PID Control Systems: Multiple PID controllers for position, velocity, and angular velocity control
  • Quadcopter Dynamics: Four-propeller system with individual thrust control and torque calculations
  • 3D Flight Control: Full six degrees of freedom (6DOF) movement including translation and rotation
  • Visual Debugging: Debug visualization of force vectors, velocity projections, and control outputs
  • Modular Architecture: Multiple control implementations (basic and advanced versions)

Technical Implementation

Control Systems

Basic PID Controller (Kontrola.cs)

  • Single-axis vertical stabilization
  • PID control for height/altitude maintenance
  • Real-time velocity calculation and error correction

Advanced Quadcopter Control (Kontrola2.cs)

  • Four independent PID controllers for propeller thrust
  • Angular velocity stabilization (pitch, roll, yaw)
  • Velocity mapping and projection onto propeller axes
  • Torque compensation for rotational stability
  • Height control with integral windup protection

Euler Angle Control (Kontrola3.cs)

  • Six-degree-of-freedom control using Euler angles
  • Position and orientation stabilization
  • Advanced PID cascade for translation and rotation
  • Direct angle-based control for precise maneuvers

Kalman Filter Enhanced Control (Kontrola4.cs)

  • IMU simulation with sensor noise modeling
  • Kalman filtering for state estimation
  • Averaging filters for signal smoothing
  • Gaussian noise simulation for realistic sensor data
  • Advanced state estimation and control

Key Components

  • Force Application: Realistic thrust vector application at propeller positions
  • Torque Calculation: Angular momentum effects from propeller forces
  • Velocity Projection: Mathematical projection of drone velocity onto propeller axes
  • PID Tuning: Configurable proportional, integral, and derivative gains
  • Wind Simulation: Optional environmental forces for realistic conditions

Controls (Kontrola2.cs)

Key Action
Move Forward
Move Backward
Move Left
Move Right
Space Ascend
C Descend
K Rotate Counterclockwise
J Rotate Clockwise

Setup Instructions

Prerequisites

  • Unity 2020.3 or later
  • Windows/macOS/Linux

Installation

  1. Clone the repository:

    git clone https://github.com/mihasm/unity-drone-control.git
  2. Open the project in Unity:

    • Launch Unity Hub
    • Click "Open" and select the cloned repository folder
    • Select the appropriate Unity version
  3. Open the SampleScene:

    • Navigate to Assets/Scenes/
    • Open SampleScene.unity
  4. Configure the drone:

    • Select the drone GameObject in the scene
    • Attach one of the controller scripts:
      • Kontrola.cs: Basic vertical stabilization
      • Kontrola2.cs: Full quadcopter control (recommended)
      • Kontrola3.cs: Euler angle-based control
      • Kontrola4.cs: Advanced control with Kalman filtering
    • Adjust PID parameters in the Inspector for desired behavior

Usage

Choosing a Controller

The repository includes four different drone control implementations, each demonstrating different approaches to flight control:

  • Kontrola.cs: Start here for basic PID concepts
  • Kontrola2.cs: Production-ready quadcopter control
  • Kontrola3.cs: Alternative control using Euler angles
  • Kontrola4.cs: Advanced control with sensor simulation

Basic Setup

  1. Create a GameObject with a Rigidbody component
  2. Add a collider (BoxCollider recommended for drone body)
  3. Attach one of the controller scripts:
    • Kontrola.cs: Simple vertical control
    • Kontrola2.cs: Full quadcopter control
    • Kontrola3.cs: Euler angle control
    • Kontrola4.cs: Kalman filter enhanced control
  4. Position four child objects at propeller locations (for advanced controllers)
  5. Run the scene and use keyboard controls to fly

PID Tuning

Adjust the PID gains in the Inspector:

  • Kp (Proportional): Response to current error
  • Ki (Integral): Correction of steady-state error
  • Kd (Derivative): Prediction of future error
  • Output Attenuation: Scaling factor for control output

Debug Visualization

The advanced controller (Kontrola2.cs) provides visual debugging:

  • Red Lines: Thrust force vectors
  • Blue Lines: Projected velocity vectors
  • Green Lines: Torque vectors
  • GUI Display: Real-time PID component values

Project Structure

Assets/
├── Scenes/
│   ├── SampleScene.unity          # Main demo scene
│   ├── Kontrola.cs               # Basic PID controller
│   ├── Kontrola2.cs              # Advanced quadcopter controller
│   ├── Kontrola3.cs              # Euler angle control system
│   └── Kontrola4.cs              # Kalman filter enhanced control
└── Terrain/
    └── New Terrain.asset         # Terrain for outdoor simulation

Technical Details

Physics Calculations

  • Thrust Forces: Applied at propeller positions with realistic force magnitudes
  • Torque Effects: Counter-rotating propellers create stabilizing moments
  • Velocity Mapping: Linear algebra projections for axis-specific control
  • Time Integration: FixedUpdate-based control loop with dt calculations

PID Implementation

The controllers use standard PID formula:

output = Kp * error + Ki * ∫error dt + Kd * d(error)/dt

With additional features:

  • Integral windup prevention through clamping
  • Bias terms for baseline thrust
  • Separate PID loops for different control axes

Contributing

This project demonstrates advanced control systems and physics simulation techniques in Unity. Key learning areas include:

  • PID control theory and implementation
  • 3D rigidbody physics
  • Quaternion mathematics for rotation
  • Vector projections and transformations
  • Real-time control system design

License

This project is open source and available under the MIT License.

About

A Unity-based quadcopter/drone physics simulation implementing PID control systems for testing realistic flight dynamics.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages