Skip to content

Horseman88/simLin

Repository files navigation

Python Drone Simulator with DroneKit and Ursina

Project Overview

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.

Features

  • 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.

Dependencies

Make sure you have Python 3 installed. You can install the necessary Python libraries using pip:

pip install ursina dronekit pymavlink numpy

File Structure

  • drone_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 the VirtualDrone class, which handles the core physics simulation of the drone (movement, forces, orientation).
  • mavlink_virtual_drone.py: Defines the MavlinkVirtualDrone class, which acts as a MAVLink server. It wraps the VirtualDrone instance, processes incoming MAVLink commands from DroneKit, and sends telemetry back.
  • pid_controller.py: Contains the PID class 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.

How to Run

  1. 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).

  2. 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).

Controls

DroneKit Controller (dronekit_controller.py)

  • 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 / False
    • vehicle.message_factory.set_attitude_target_send(...) for roll, pitch, yaw rates, and thrust.

Ursina Simulation Window (drone_simulation_env.py)

  • Camera: The Ursina window uses EditorCamera by 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.py has a flag enable_keyboard_test_controls. If set to True (default is False to prioritize DroneKit), it allows direct control over the drone motors using keyboard inputs for basic physics testing (this bypasses MAVLink). Refer to the handle_keyboard_input method in that file for key mappings. It's recommended to keep this False when using dronekit_controller.py.

Known Limitations / Future Work

  • 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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages