This project is a Python-based drone flight simulator that allows users to control a virtual drone using DroneKit. The simulation environment is rendered in 3D using the Ursina game engine. The drone's physics and MAVLink communication are handled internally, providing a platform for testing DroneKit scripts and understanding basic drone flight dynamics.
- 3D Drone Simulation: Visual environment built with Ursina Engine.
- MAVLink Interface: Simulated MAVLink vehicle that DroneKit can connect to (via UDP).
- DroneKit Control: Control the drone using standard DroneKit scripts (arm, disarm, takeoff, land, attitude targets).
- Basic Flight Physics:
- Quadratic aerodynamic drag model.
- First-order motor response lag.
- Euler angle-based rigid body dynamics.
- PID Controlled Flight:
- PID controllers for roll, pitch, and yaw rate stabilization.
- P-controllers for vertical speed during automated takeoff and landing.
- Visual Placeholder Drone: Composite 3D model made from Ursina primitives.
- On-Screen Telemetry: Displays key flight data in the Ursina window.
Make sure you have Python 3 installed. You can install the necessary Python libraries using pip:
pip install ursina dronekit pymavlink numpydrone_simulation_env.py: Main script to run the Ursina 3D simulation environment. It initializes and manages the visual aspects and the link to the MAVLink interface.virtual_drone.py: Defines theVirtualDroneclass, which handles the core physics simulation of the drone (movement, forces, orientation).mavlink_virtual_drone.py: Defines theMavlinkVirtualDroneclass, which acts as a MAVLink server. It wraps theVirtualDroneinstance, processes incoming MAVLink commands from DroneKit, and sends telemetry back.pid_controller.py: Contains thePIDclass used for attitude and rate stabilization.dronekit_controller.py: An example DroneKit script that connects to the simulation, sends commands (arm, takeoff, land, attitude control), and prints telemetry.
-
Start the Simulation Environment: Open a terminal or command prompt, navigate to the project directory, and run:
python drone_simulation_env.py
This will launch the Ursina 3D window and start the MAVLink server. You should see console messages indicating the MAVLink server is listening (e.g.,
MAVLink server listening. DroneKit should connect to 127.0.0.1:14550). -
Run the DroneKit Control Script: Open another terminal or command prompt, navigate to the project directory, and run:
python dronekit_controller.py
This script will attempt to connect to the simulation via UDP on
127.0.0.1:14550. Once connected, it will execute pre-defined flight commands (e.g., arm, takeoff, attitude maneuvers, land).
- The script automates a sequence: arm, takeoff, simple attitude maneuvers, land, disarm.
- You can modify this script to send different commands or test various DroneKit functionalities.
- Key functions to look at for sending commands:
vehicle.simple_takeoff(altitude)vehicle.mode = VehicleMode("LAND")vehicle.armed = True / Falsevehicle.message_factory.set_attitude_target_send(...)for roll, pitch, yaw rates, and thrust.
- Camera: The Ursina window uses
EditorCameraby default.- Right-click + drag: Rotate camera.
- Mouse wheel: Zoom in/out.
- Middle-click + drag (or WASDQE with right-click held for some systems): Pan camera.
- Keyboard (Direct Sim Control - if enabled):
- The
drone_simulation_env.pyhas a flagenable_keyboard_test_controls. If set toTrue(default isFalseto prioritize DroneKit), it allows direct control over the drone motors using keyboard inputs for basic physics testing (this bypasses MAVLink). Refer to thehandle_keyboard_inputmethod in that file for key mappings. It's recommended to keep thisFalsewhen usingdronekit_controller.py.
- The
- PID Tuning: While initial PID tuning has been done, the gains might need further refinement for optimal performance across different flight scenarios.
- Advanced Flight Modes: Position hold, waypoint navigation, etc., are not implemented in the
MavlinkVirtualDrone's MAVLink command processing. - Physics Model: The physics model is still relatively simple. Enhancements could include:
- More detailed aerodynamic effects (e.g., lift, body frame drag).
- Ground effect.
- Battery simulation.
- 3D Model: Uses a placeholder composite model. It can be replaced with a standard 3D model file (e.g., .obj, .glb).
- Collision Detection: Basic ground collision is present, but no obstacle collision.
- Sensor Simulation: No detailed simulation of sensors like GPS, IMU noise, barometer, etc. Telemetry is based on the "perfect" state of the
VirtualDrone.