Flightmare Quadrotor Simulator

repository·master·Indexed 23 days ago

https://github.com/uzh-rpg/flightmare

A modular quadrotor simulator optimized for reinforcement learning and robotics research. It features a decoupled architecture consisting of a Unity-based rendering engine and a physics engine, supporting parallel simulations, multi-modal sensor suites with 3D point-cloud extraction, and VR headset integration. The simulator supports integration with the Open Motion Planning Library (OMPL) for advanced trajectory generation and obstacle avoidance.

Tokens
15K
Snippets
31
Records
86
Agent score
79%

What's inside Flightmare

  1. Overview of Flightmare simulator

    master

    Flightmare is a modular quadrotor simulator consisting of two decoupled components: a configurable rendering engine (built on Unity) and a flexible physics engine for dynamics simulation.

    Key features include:

    • A large multi-modal sensor suite (including 3D point-cloud extraction).
    • An API for reinforcement learning capable of simulating hundreds of quadrotors in parallel.
    • Virtual-reality (VR) headset integration for environmental interaction.
    • Support for various applications like path-planning, visual-inertial odometry, and deep learning.
  2. Overview of Flightmare architecture

    master

    Flightmare is a modular quadrotor simulator designed for applications such as reinforcement learning, path-planning, and visual-inertial odometry. It consists of two decoupled components that can run independently:

    1. Rendering Engine: A configurable engine built on Unity.
    2. Physics Engine: A flexible engine for dynamics simulation.

    Key features include a multi-modal sensor suite (with 3D point-cloud extraction), an API for parallel reinforcement learning (simulating hundreds of quadrotors), and VR headset integration.

  3. Understand the Flightmare software components

    master

    Flightmare is organized into several specialized software components:

    • flightlib: The core Flightmare Library. It handles quadrotor dynamics, sensor simulation, the Unity bridge, and provides a Python wrapper.
    • flightrender: The Flightmare Rendering Engine. It provides photo-realistic 3D environments and outputs RGB images, depth, and segmentation data.
    • flightrl: Reinforcement Learning module. Contains deep RL algorithms (e.g., PPO) and examples like quadrotor control.
    • flightros: ROS Wrapper. Interfaces the Flightmare Library with ROS, including examples for PID controller quadrotor control and RGB camera simulation.
  4. How Sensors and data collection work

    master

    Sensors are actors that must be attached to a parent quadrotor. Once attached, they follow the quadrotor and gather data from the surroundings after each rendering step.

    To use a sensor, you must provide a function that defines how to handle the retrieved data.

    Available Sensors:

    • Cameras:
      • RGB
      • Depth
      • Semantic segmentation
    • Collision detector

    Note: IMU, Lidar raycast, Optical flow, and Event-based cameras are planned for future updates.

  5. Calculate the camera intrinsic calibration matrix

    master

    The calibration matrix is not extracted from Unity; it must be calculated on the client-side (e.g., in ROS or C++). Because image dimensions (width x height) and the Field of View (FOV) are user-defined, you must compute the focal length manually.

    1. Calculate focal length (f): f = (image.height / 2.0) / tan((M_PI * FOV / 180.0) / 2.0)

    2. Construct the intrinsic matrix: Since fx = fy, the matrix is:

      [ [fx, 0, image.width/2], 
        [0, fy, image.height/2], 
        [0, 0, 1] ]
  6. How the Flightmare Server and Client architecture works

    master

    Flightmare operates using a client-server architecture to separate simulation logic from rendering:

    • Client: The module run by the user to model dynamics and control the simulation. It communicates with the server via a specific IP and port.
    • Server: The rendering engine that represents the simulation. It provides the main API methods to spawn quadrotors, modify the environment, and retrieve the current state of the simulation.

    This separation allows the user to run complex control logic in a client while the server handles the heavy lifting of physics and rendering.

  7. Understand the Top_Level_Scene GameObjects

    master

    When editing the Unity project, load the Top_Level_Scene. It contains three essential GameObjects required for the Flightmare environment:

    • HD_Camera: The main camera responsible for rendering images.
    • Camera_Controller: The central controller used for loading different scenes, moving the camera, retrieving camera poses, and publishing images.
    • splash_window: The UI interface.

    When running the application in the Unity Editor (clicking Play), the application opens two TCP sockets: one for publishing images and one for subscribing to camera poses.

  8. Spawn and move a quadrotor in Flightmare

    master

    To use a quadrotor in a Flightmare simulation, you must initialize a Quadrotor object, reset it with an initial QuadState, and register it with the UnityBridge. Once connected to a Unity scene, you can update the quadrotor's pose by modifying the QuadState and calling setState().

    #include "flightlib/bridges/unity_bridge.hpp"
    #include "flightlib/common/quad_state.hpp"
    #include "flightlib/common/types.hpp"
    #include "flightlib/objects/quadrotor.hpp"
    
    using namespace flightlib;
    
    // Initialize quadrotor
    std::shared_ptr<Quadrotor> quad_ptr_ = std::make_shared<Quadrotor>();
    QuadState quad_state_;
    quad_state_.setZero();
    quad_ptr_->reset(quad_state_);
    
    // Initialize Unity bridge
    std::shared_ptr<UnityBridge> unity_bridge_ptr_;
    unity_bridge_ptr_ = UnityBridge::getInstance();
    
    // Add quadrotor
    unity_bridge_ptr_->addQuadrotor(quad_ptr_);
    bool unity_ready_ = unity_bridge_ptr_->connectUnity(UnityScene::WAREHOUSE);
    
    // ... later, to update pose ...
    quad_state_.x[QS::POSX] = (Scalar)position.x;
    quad_state_.x[QS::POSY] = (Scalar)position.y;
    quad_state_.x[QS::POSZ] = (Scalar)position.z;
    quad_state_.x[QS::ATTW] = (Scalar)orientation.w;
    quad_state_.x[QS::ATTX] = (Scalar)orientation.x;
    quad_state_.x[QS::ATTY] = (Scalar)orientation.y;
    quad_state_.x[QS::ATTZ] = (Scalar)orientation.z;
    
    quad_ptr_->setState(quad_state_);
    
    // Render next frame
    unity_bridge_ptr_->getRender(0);
    unity_bridge_ptr_->handleOutput();