Skip to content

Simple Multirotor flight simulator using UnrealEngine4

License

Notifications You must be signed in to change notification settings

dannykung10/MulticopterSim

 
 

Repository files navigation

About

MulticopterSim is a simple multicopter flight simulator using Unreal Engine 4. It runs on Windows.

I began this project using the V-REP platform but switched to UnrealEngine after seeing the kinds of beautiful, real-time simulations that Microsoft was able to get with its AirSim program.

MulticopterSim differs from AirSim in a few important ways:

  • MulticopterSim can be used with the same simple flight-control firmware that we fly on our real-world quadcopters.
  • MulticopterSim is tiny, under 2,000 lines of code.
  • MulticopterSim focuses exclusively on multirotor firmware, whereas AirSim supports deep learning and different kinds of vehicles.

Prerequisites

Hardware

I am currently developing MulticopterSim on an HP Z440 workstation with 32GB RAM and NVIDIA GeForce GTX 1080 Ti. It may however be possible to develop on a less “loaded” machine – see here for the minimum requirements recommended by Unreal Engine.

For a realistic flying experience, you will also likely want some sort of game controller or R/C transmitter. MulticopterSim currently supports the following controllers through the Joystick class:

  • PS4 controller
  • XBox One controller
  • XBox 360 controller
  • XBox 360 controller clone
  • PS3 controller clone
  • Logitech Extreme Pro 3D joystick
  • Logitech F310 gamepad
  • FrSky Taranis TX9 RC transmitter with mini USB cable
  • Spektrum transmitter with WS1000 wireless simulator dongle
  • Great Planes RealFlight Interlink

If you don't have a controller, MulticopterSim will use input from the numeric keypad on your keyboard (make sure that NumLock is turned on!) The key mappings are based on those used in Microsoft Flight Simulator.

Toolchain

You will need Unreal Engine 4 (UE4). I am attempting to use the latest version, which as of the time of this writing is UE4.26.1. To install UE4, follow these instructions.

Build

Although MulticopterSim is designed to work with any flight-control software you like, it easiest to get started with the Hackflight software. So to get started, you should do the following:

  1. Clone this repostitory (MulticopterSim) into your Documents/Unreal Projects folder, first creating that folder if it doesn't exist.

  2. Clone the Hackflight and RoboFirmwareToolkit (RFT) repositories into your DocumentsArduino/libraries folder, first creating that folder if it doesn't already exist. (You don't need to install Arduino; this is simply where MulticopterSim looks for the Hackflight firmware.)

  3. Right-click on the MulticopterSim.uproject file and select Generate Visual Studio project file to generate a .sln file

  4. Double-click on the resulting MulticopterSim.sln file to launch VisualStudio. The first time you do this, you may have to wait a few minutes while Visual Studio parses up all of the UE4 files needed to build the project.

  5. In Visual Studio, edit the file MulticopterSim/Source/HackflightModule/HackflightModule.Build.cs to reflect where you installed Hackflight / RFT.

  6. In VisualStudio, hit the F5 key to build the project and launch UnrealEditor.

Launch and fly!

In UnrealEditor, select one of the maps in Content/MulticopterSim/Maps. Then open the Content/C++ Classes/HackflightModule/pawns folder and drag one of the vehicle pawns into the map. Click the play button and you're ready to begin. You can use the spacebar to switch your point-of-view.

Design principles

The core of MulticopterSim is the abstract C++ FlightManager class. This class provides support for running the vehicle dynamics and the PID control regime (e.g., Hackflight) on its own thread, after it first disables the built-in physics in UE4. The dynamics we used are based directly on the model presented in this paper, written as a standalone, header-only C++ class that can be easily adapted for other simulators and applications if desired. This class also supports different frame configurations (quadcopter, hexacopter) via virtual methods. By running the FlightManager on its own thread, we are able to achieve arbitrarily fast updates of the dynamics and flight-control. It would also be possible to run the dynamics and control on separate threads, though we have not yet found it advantageous to do that.

The FlightManager API contains a single virtual getMotors method that accepts the current time and the state of the vehicle (as computed by the dynamics), and returns the current motor values. The motor values are then passed to the dynamics object, which computes the new vehicle state. On the main thread, UE4's Tick() method queries the flight manager for the current vehicle pose (location, rotation) and displays the vehicle and its environment kinematically at the 60-120Hz frame rate of the game engine. In a similar manner, the Camera class can be used to process the images collected by a simulated gimbal-mounted camera on the vehicle, using a library like OpenCV (see below). Computer-vision algorithms running in a Camera subclass can then be used as input to the PID control running in the FlightManager. The following figure illustrates this arrangement, using a traditional cascade-control (slow outer loop / fast inner loop) diagram:

In addition, an abstract, threaded C++ TargetManager class supports modeling interaction with other moving objects having their own dynamics; for example, in a predator/prey scenario.

Support for other programming languages / packages

Matlab

This folder contains an example of how you can control MulticopterSim from a Matlab session running on your computer or another computer.

Python

This folder contains an example of how you can control MulticopterSim from a Python program running on your computer or another computer.

Java

This folder contains an example of how you can control MulticopterSim from a Java program running on your computer or another computer.

OpenCV

After you build the simulator, the Content/C++ Classes/Hackflight/pawns folder will contain a pawn that will run OpenCV edge detection. By sub-classing the OpenCVCamera class you can run different kinds of machine-vision algorithms.

About

Simple Multirotor flight simulator using UnrealEngine4

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 76.9%
  • Python 8.4%
  • Java 6.2%
  • Haskell 3.2%
  • C# 2.5%
  • Makefile 0.9%
  • Other 1.9%