This is a PROTOTYPE/RESEARCH SYSTEM - NOT PRODUCTION READY!
-
ESC Calibration Risk: The system performs automatic ESC calibration on startup by sending maximum throttle signals. NEVER run this code with propellers attached! ESCs that don't properly detect the calibration sequence may interpret this as full throttle commands, causing the drone to take off uncontrollably.
-
No Safety Features: This system lacks essential safety features found in commercial flight controllers (arming switches, failsafes, motor kill switches, etc.).
-
Research Prototype: This code was developed for academic research and testing. It requires modification and thorough testing before use on any drone.
- REMOVE ALL PROPELLERS before uploading or testing this code
- Test in a controlled environment with emergency cutoff capabilities
- Modify the code to disable
DEBUGGING_MODE 0for initial testing - Add your own safety features before flight
- Test incrementally - never fly with untested code
You assume all risks when using this code. The author is not responsible for any damage, injury, or loss.
A complete quadcopter/drone flight control system implemented on Arduino, featuring advanced sensor fusion, PID control algorithms, and multiple flight modes. This project was developed as part of a master's thesis on UAV control systems.
- Overview
- Features
- Hardware Requirements
- Software Dependencies
- Installation
- Configuration
- Flight Modes
- PID Tuning
- Architecture
- API Reference
- Troubleshooting
- Contributing
- License
- Citation
This flight control system implements a cascaded PID control architecture for quadcopter stabilization and navigation. The system integrates multiple sensors including IMU (MPU-9255) and barometric pressure sensor (BMP280) to provide robust attitude estimation and altitude control.
The project includes custom implementations of:
- PID Regulator: Cascaded PID control for attitude and rate stabilization
- Kalman Filter: Sensor fusion for altitude estimation
- Low-Pass Filter: RC input smoothing and sensor noise reduction
- Triple Filter: Advanced sensor filtering combining Kalman and LPF
- Cascaded PID Control: Separate rate and attitude control loops
- Multiple Flight Modes: Acro, Normal, and Altitude Hold modes
- Arming/Disarming: Safe RC-based arming procedure
- Failsafe Protection: Automatic disarm on signal loss
- MPU-9255 IMU: 9-DOF sensor with accelerometer, gyroscope, and magnetometer
- BMP280 Barometer: High-precision altitude estimation
- PPM Receiver: Standard RC protocol support (up to 6 channels)
- Kalman Filter: Altitude estimation with process and measurement noise tuning
- Low-Pass Filters: Configurable cutoff frequencies for different signals
- Triple Filter: Combined Kalman-LPF-Kalman filtering for IMU data
- ESC Calibration: Automated Electronic Speed Controller setup
- PWM Output: Precise motor control with 1000-2000ΞΌs range
- Quadcopter Layout: Standard X configuration
- Arduino Board: Compatible with Arduino Mega/Uno (tested on Mega)
- IMU Sensor: MPU-9255 (I2C interface)
- Barometric Sensor: BMP280 (I2C interface)
- RC Receiver: PPM output (6+ channels recommended)
- ESCs: 4x Electronic Speed Controllers (SimonK/BLHeli firmware)
- Motors: 4x Brushless DC motors (quadcopter configuration)
- Battery: 3S-4S LiPo battery with appropriate voltage regulator
- Frame: Quadcopter frame (appropriate size for motors)
Arduino Pin | Component | Purpose
------------|-----------------|---------
2 | PPM Receiver | RC input signal
3,5,9,11 | ESCs | Motor PWM output (ESC1-ESC4)
SDA (20) | MPU9255/BMP280 | I2C data line
SCL (21) | MPU9255/BMP280 | I2C clock line
GND | All sensors | Common ground
5V | Sensors/Receiver | Power supply
- Channel 1: Roll (Aileron)
- Channel 2: Pitch (Elevator)
- Channel 3: Thrust (Throttle)
- Channel 4: Yaw (Rudder)
- Channel 5: Arming switch
- Channel 6: Flight mode switch
- PPMReader: RC receiver signal decoding
- Servo: Motor control PWM generation
- BMP280_DEV: Barometric pressure sensor interface
- Wire: I2C communication protocol
- PID_regulator: PID control implementation
- KalmanFilter: Kalman filtering for sensor fusion
- LPF: Low-pass filter implementation
- TripleFilter: Combined filtering approach
For detailed installation instructions, see dependencies.md.
- Install Arduino IDE (version 1.8.0 or later)
- Install required libraries via Library Manager:
- PPMReader
- Servo
- BMP280_DEV
- Mount Arduino board on quadcopter frame
- Connect IMU and barometric sensors to I2C bus
- Connect PPM receiver to digital pin 2
- Connect ESCs to PWM pins (3, 5, 9, 11)
- Power distribution setup (separate power for Arduino and ESCs)
- Open
drone_v2.inoin Arduino IDE - Select appropriate board and port
- Configure DEBUGGING_MODE if needed
- Upload the code
- Install PlatformIO
- Open project folder in VS Code with PlatformIO extension
- Run
pio run -t uploador use the upload button - For debugging:
pio run -t upload --environment debug
π¨ DANGER: ESC calibration sends maximum throttle signals that can cause motors to spin at full speed!
CRITICAL SAFETY REQUIREMENT: REMOVE ALL PROPELLERS before performing ESC calibration. ESCs that don't properly detect calibration signals may spin motors at 100% throttle.
The system performs automatic ESC calibration on EVERY boot when DEBUGGING_MODE is disabled:
- ENSURE NO PROPELLERS ARE ATTACHED
- Power on the system
- Wait for "Starting ESC calibration sequence" message
- System automatically sends maximum (2000ΞΌs) then minimum (1000ΞΌs) throttle signals
- Wait for calibration to complete (10 seconds total)
- RE-ATTACH PROPELLERS ONLY AFTER confirming calibration worked correctly
Note: To skip calibration for testing, set DEBUGGING_MODE 1 in the code.
Edit the defines in drone_v2.ino:
// Pitch control
#define Kp_w_pitch 0.0003 // Rate PID proportional gain
#define Ki_w_pitch 0.0006 // Rate PID integral gain
#define Kd_w_pitch 0.000025 // Rate PID derivative gain
#define Kp_theta_pitch 5.0 // Attitude PID proportional gain
#define Ki_theta_pitch 8.0 // Attitude PID integral gain
#define Kd_theta_pitch 0.05 // Attitude PID derivative gain
// Roll control (similar structure)
// Yaw control (rate-only PID)
// Altitude control (single PID loop)// IMU filtering
#define Q_w 0.5 // Gyro process noise
#define R_w 2.5e-7 // Gyro measurement noise
#define Q_a 0.5 // Accel process noise
#define R_a 2.5e-7 // Accel measurement noise
// RC input filtering
#define f_c_rc 4 // RC cutoff frequency (Hz)
// Altitude filtering
#define f_c_alt 1 // Altitude cutoff frequency (Hz)
#define Q_alt 0.008 // Altitude process noise
#define R_alt 1e4 // Altitude measurement noise#define MAX_DEGREES 30 // Maximum tilt angle (Normal mode)
#define MAX_DPS_YAW 180 // Maximum yaw rate
#define MAX_DPS_PITCH_ROLL 180 // Maximum pitch/roll rate (Acro mode)
#define MAX_VERT_SPEED 0.25 // Maximum vertical speed (Altitude hold)- Direct Rate Control: RC inputs directly control rotation rates
- No Attitude Stabilization: Requires pilot skill
- Best for: Aerobatic maneuvers, experienced pilots
- Attitude Stabilization: RC inputs control angle (self-leveling)
- Cascaded PID: Rate and attitude control loops
- Best for: General flying, beginners
- Altitude Stabilization: Maintains constant altitude
- Vertical Speed Control: Thrust input controls climb/sink rate
- Best for: Photography, cruise flight
- Start with Rate PIDs (inner loop): Set I and D to zero, increase P until oscillations occur, then reduce by half
- Tune Rate I and D: Add integral for steady-state accuracy, derivative for stability
- Tune Attitude PIDs (outer loop): Start with low values, increase until responsive
- Fine-tune: Adjust all gains for smooth response
- Overshoot: Reduce P gains
- Oscillations: Reduce P or increase D gains
- Drift: Increase I gains (carefully)
- Sluggish response: Increase P gains
RC Input β Low-Pass Filter β PID Controllers β Motor Mixing β PWM Output
β
Sensor Data β Filtering β Attitude Estimation β PID Controllers
- Sensor Reading: IMU and barometric data acquisition
- Filtering: Triple filtering for IMU, Kalman for altitude
- Attitude Estimation: Complementary filter combining accel/gyro
- PID Control: Cascaded control for each axis
- Motor Mixing: Convert control outputs to individual motor commands
- PWM Generation: Send commands to ESCs
PID_regulator pid;
pid.set_parameters(Kp, Ki, Kd, bias);
float output = pid.Output(input, setpoint, dt, stop_integration);KalmanFilter kf;
kf.change_parameters(Q, R, initial_value);
float filtered = kf.Output(measurement);LPF filter;
filter.change_parameters(cutoff_freq, initial_value);
float filtered = filter.Output(input, dt);Drone won't arm:
- Check RC transmitter battery and signal
- Verify PPM receiver connection (pin 2)
- Ensure throttle is at minimum position
Unstable flight:
- Check IMU calibration (run calibrate() function)
- Verify PID gains are not too aggressive
- Check motor balance and propeller tracking
Altitude drifting:
- Recalibrate barometric sensor
- Adjust altitude PID gains
- Check for air leaks in enclosure
No sensor data:
- Verify I2C connections and pull-up resistors
- Check sensor power supply voltage
- Use I2C scanner sketch to verify addresses
Set DEBUGGING_MODE 1 to enable serial debugging:
- Real-time sensor data output
- PID controller status
- RC input monitoring
- System frequency information
We welcome contributions! This project is open source and we encourage community involvement. Here's how you can contribute:
- Report Bugs: Open issues for any bugs you find
- Feature Requests: Suggest new features or improvements
- Code Contributions: Submit pull requests with bug fixes or enhancements
- Documentation: Improve documentation, add tutorials, or fix typos
- Testing: Test on different hardware configurations and share results
- PID Tuning: Share your tuning experiences and parameter sets
- Fork the repository
- Create a feature branch:
git checkout -b feature/your-feature-name - Make your changes and test thoroughly
- Submit a pull request with a clear description
- Follow Arduino coding conventions
- Add comments for complex logic
- Test on real hardware when possible
- Update documentation for any new features
- Keep commits focused and descriptive
This project is licensed under the MIT License - see the LICENSE file for details.
If you use this code in your research or project, please cite the original master's thesis:
@mastersthesis{smrekar2022modeling,
title={Modelling and production of the control system and propellers of an unmanned aerial vehicle},
author={Smrekar, Miha},
year={2022},
school={Faculty of Energy, University of Maribor},
address={Kr\v{s}ko, Slovenia},
url={https://dk.um.si/IzpisGradiva.php?id=81118}
}- Mathematical modeling of UAV dynamics
- BEM analysis of propeller performance
- PID controller design for multi-rotor systems
- Sensor fusion techniques for attitude estimation
This is a PROTOTYPE RESEARCH SYSTEM - NOT A COMMERCIAL FLIGHT CONTROLLER!
- No Commercial Guarantees: This code lacks the safety features and testing of production flight controllers
- Research Use Only: Developed for academic purposes and requires significant modification for safe operation
- Your Responsibility: You are solely responsible for ensuring safe operation when using this code
- No Liability: The author assumes no responsibility for any damage, injury, or loss resulting from use of this software
Always fly in designated areas, maintain visual contact, and be prepared for immediate emergency cutoff. Never fly over people or property.