This document provides a comprehensive overview of the drone simulation system implemented for ESP32. The system is designed to simulate a quadcopter's flight control system with multiple concurrent tasks for sensor management, stabilization, motor control, and command handling.
The system is built around FreeRTOS on ESP32, utilizing multiple tasks to achieve concurrent operation. The architecture follows a modular design with clear separation of concerns.
-
Main Tasks
MainSensorManagerTask: Manages all sensor data collection and processingMainStabilizerTask: Implements PID control for flight stabilizationMainMotorOutputTask: Controls motor outputs based on PID calculationsMainCommandTask: Handles incoming flight commandsMainFailsafeTask: Monitors system health and triggers failsafe procedures
-
Sensor Sub-Tasks
SubSensorManager_IMUTask: Simulates IMU data (accelerometer, gyroscope)SubSensorManager_BarometerTask: Simulates barometric pressure/altitude dataSubSensorManager_GPSTask: Simulates GPS position dataSubSensorManager_MagnetometerTask: Simulates magnetometer (compass) data
-
Data Structures
PIDOutput: Hovers roll, pitch, yaw, and altitude control outputsAttitude: Tracks current orientation (pitch, roll, yaw) and altitudeSensorData: Aggregates all sensor readingsMotorOutput: Controls individual motor speedsPIDController: Implements PID control algorithm
graph TD
A[MainCommandTask] -->|Command Queue| B[MainSensorManagerTask]
B -->|Sensor Data| C[MainStabilizerTask]
C -->|PID Output| D[MainMotorOutputTask]
D -->|Motor Commands| E[Motors]
E -->|Feedback| F[MainFailsafeTask]
F -->|Error Events| A
G[SubSensorManager_IMUTask] -->|IMU Data| B
H[SubSensorManager_BarometerTask] -->|Baro Data| B
I[SubSensorManager_GPSTask] -->|GPS Data| B
J[SubSensorManager_MagnetometerTask] -->|Mag Data| B
-
Command Reception
- Commands received via serial interface or network
- Added to command queue for processing
- Handled by
MainCommandTask
-
Sensor Data Collection
- Multiple sensor tasks collect data in parallel
- Data is protected by mutexes for thread safety
- Aggregated by
MainSensorManagerTask
-
Stabilization
MainStabilizerTaskreads current attitude and target- PID controllers calculate required corrections
- Outputs motor speed adjustments
-
Motor Control
MainMotorOutputTaskreceives PID outputs- Mixes controls for quadcopter movement
- Applies motor speed limits and safety checks
- Multi-tasking: Utilizes FreeRTOS for concurrent operations
- Thread Safety: Mutexes protect shared resources
- Modular Design: Clear separation between components
- Simulation Ready: Simulated sensor data for testing
- Flight Modes: Supports multiple flight phases (IDLE, TAKEOFF, HOVER, LAND)
// Default PID values
PIDController pitchPID = {2.0f, 0.5f, 1.0f}; // KP, KI, KD
PIDController rollPID = {2.0f, 0.5f, 1.0f};
PIDController yawPID = {1.0f, 0.1f, 0.5f};
PIDController altPID = {10.0f, 0.1f, 5.0f};- Max climb rate: 1.0 m/s
- Max tilt angle: 30 degrees
- Altitude hold tolerance: ±0.1m
- Upload the firmware to ESP32
- Connect via serial monitor (115200 baud)
- Send commands:
TAKEOFF: Start takeoff sequenceHOVER: Maintain current positionLAND: Begin landing sequenceEMERGENCY_STOP: Immediately stop all motors
System prints status updates including:
- Task status and CPU usage
- Current flight phase
- Sensor readings
- PID outputs
- Motor commands
- Arduino Framework
- FreeRTOS
- ESP32 Board Support Package
- ArduinoJSON (for network communication)
- Motor arming sequence
- Emergency stop command
- Battery voltage monitoring
- Communication watchdog
- Automatic failsafe on signal loss
- Waypoint navigation
- Computer vision integration
- Advanced flight modes
- Remote telemetry via WiFi
- Automated testing framework