envs.drone_race¶
lsy_drone_racing.envs.drone_race
¶
Single drone racing environments.
Classes¶
DroneRaceEnv(freq, sim_config, track, sensor_range=0.5, control_mode='state', disturbances=None, randomizations=None, seed=None, max_episode_steps=1500, device='cpu')
¶
Bases: RaceCoreEnv, Env
Single-agent drone racing environment.
Initialize the single-agent drone racing environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
freq
|
int
|
Environment step frequency. |
required |
sim_config
|
ConfigDict
|
Simulation configuration. |
required |
track
|
ConfigDict
|
Track configuration. |
required |
sensor_range
|
float
|
Sensor range. |
0.5
|
control_mode
|
Literal['state', 'attitude']
|
Control mode for the drones. See |
'state'
|
disturbances
|
ConfigDict | None
|
Disturbance configuration. |
None
|
randomizations
|
ConfigDict | None
|
Randomization configuration. |
None
|
seed
|
int | None
|
None / -1 for a generated seed or the random seed directly. |
None
|
max_episode_steps
|
int
|
Maximum number of steps per episode. |
1500
|
device
|
Literal['cpu', 'gpu']
|
Device used for the environment and the simulation. |
'cpu'
|
Source code in lsy_drone_racing/envs/drone_race.py
Methods:¶
reset(seed=None, options=None)
¶
Reset the environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
int | None
|
Random seed. |
None
|
options
|
dict | None
|
Additional reset options. Not used. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[dict, dict]
|
The initial observation and info. |
Source code in lsy_drone_racing/envs/drone_race.py
step(action)
¶
Step the environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
action
|
Array
|
Action for the drone. |
required |
Returns:
| Type | Description |
|---|---|
tuple[dict, float, bool, bool, dict]
|
Observation, reward, terminated, truncated, and info. |
Source code in lsy_drone_racing/envs/drone_race.py
VecDroneRaceEnv(num_envs, freq, sim_config, track, sensor_range=0.5, control_mode='state', disturbances=None, randomizations=None, seed=1337, max_episode_steps=1500, device='cpu')
¶
Bases: RaceCoreEnv, VectorEnv
Vectorized single-agent drone racing environment.
Initialize the vectorized single-agent drone racing environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_envs
|
int
|
Number of worlds in the vectorized environment. |
required |
freq
|
int
|
Environment step frequency. |
required |
sim_config
|
ConfigDict
|
Simulation configuration. |
required |
track
|
ConfigDict
|
Track configuration. |
required |
sensor_range
|
float
|
Sensor range. |
0.5
|
control_mode
|
Literal['state', 'attitude']
|
Control mode for the drones. See |
'state'
|
disturbances
|
ConfigDict | None
|
Disturbance configuration. |
None
|
randomizations
|
ConfigDict | None
|
Randomization configuration. |
None
|
seed
|
int
|
Random seed. |
1337
|
max_episode_steps
|
int
|
Maximum number of steps per episode. |
1500
|
device
|
Literal['cpu', 'gpu']
|
Device used for the environment and the simulation. |
'cpu'
|
Source code in lsy_drone_racing/envs/drone_race.py
Methods:¶
reset(seed=None, options=None)
¶
Reset the environment in all worlds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
int | None
|
Random seed. |
None
|
options
|
dict | None
|
Additional reset options. Not used. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[dict, dict]
|
The initial observation and info. |
Source code in lsy_drone_racing/envs/drone_race.py
step(action)
¶
Step the environment in all worlds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
action
|
Array
|
Action for all worlds, i.e., a batch of (n_envs, action_dim) arrays. |
required |
Returns:
| Type | Description |
|---|---|
tuple[dict, Array, Array, Array, dict]
|
Observation, reward, terminated, truncated, and info. |
Source code in lsy_drone_racing/envs/drone_race.py
Functions:¶
lsy_drone_racing.envs.multi_drone_race
¶
Multi-agent drone racing environments.
Classes¶
MultiDroneRaceEnv(freq, sim_config, track, sensor_range=0.5, control_mode='state', disturbances=None, randomizations=None, seed=None, max_episode_steps=1500, device='cpu')
¶
Bases: RaceCoreEnv, Env
Multi-agent drone racing environment.
This environment enables multiple agents to simultaneously compete with each other on the same track.
Initialize the multi-agent drone racing environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
freq
|
int
|
Environment step frequency. |
required |
sim_config
|
ConfigDict
|
Simulation configuration. |
required |
track
|
ConfigDict
|
Track configuration. |
required |
sensor_range
|
float
|
Sensor range. |
0.5
|
control_mode
|
Literal['state', 'attitude']
|
Control mode for the drones. See |
'state'
|
disturbances
|
ConfigDict | None
|
Disturbance configuration. |
None
|
randomizations
|
ConfigDict | None
|
Randomization configuration. |
None
|
seed
|
int | None
|
None / -1 for a generated seed or the random seed directly. |
None
|
max_episode_steps
|
int
|
Maximum number of steps per episode. |
1500
|
device
|
Literal['cpu', 'gpu']
|
Device used for the environment and the simulation. |
'cpu'
|
Source code in lsy_drone_racing/envs/multi_drone_race.py
Methods:¶
reset(seed=None, options=None)
¶
Reset the environment for all drones.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
int | None
|
Random seed. |
None
|
options
|
dict | None
|
Additional reset options. Not used. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[dict, dict]
|
Observation and info for all drones. |
Source code in lsy_drone_racing/envs/multi_drone_race.py
step(action)
¶
Step the environment for all drones.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
action
|
Array
|
Action for all drones, i.e., a batch of (n_drones, action_dim) arrays. |
required |
Returns:
| Type | Description |
|---|---|
tuple[dict, Array, Array, Array, dict]
|
Observation, reward, terminated, truncated, and info for all drones. |
Source code in lsy_drone_racing/envs/multi_drone_race.py
VecMultiDroneRaceEnv(num_envs, freq, sim_config, track, sensor_range=0.5, control_mode='state', disturbances=None, randomizations=None, seed=1337, max_episode_steps=1500, device='cpu')
¶
Bases: RaceCoreEnv, VectorEnv
Vectorized multi-agent drone racing environment.
This environment enables vectorized training of multi-agent drone racing agents.
Vectorized multi-agent drone racing environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_envs
|
int
|
Number of worlds in the vectorized environment. |
required |
freq
|
int
|
Environment step frequency. |
required |
sim_config
|
ConfigDict
|
Simulation configuration. |
required |
track
|
ConfigDict
|
Track configuration. |
required |
sensor_range
|
float
|
Sensor range. |
0.5
|
control_mode
|
Literal['state', 'attitude']
|
Control mode for the drones. See |
'state'
|
disturbances
|
ConfigDict | None
|
Disturbance configuration. |
None
|
randomizations
|
ConfigDict | None
|
Randomization configuration. |
None
|
seed
|
int
|
Random seed. |
1337
|
max_episode_steps
|
int
|
Maximum number of steps per episode. |
1500
|
device
|
Literal['cpu', 'gpu']
|
Device used for the environment and the simulation. |
'cpu'
|
Source code in lsy_drone_racing/envs/multi_drone_race.py
Methods:¶
reset(seed=None, options=None)
¶
Reset the environment for all drones.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
int | None
|
Random seed. |
None
|
options
|
dict | None
|
Additional reset options. Not used. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[dict, dict]
|
Observation and info for all drones. |
Source code in lsy_drone_racing/envs/multi_drone_race.py
step(action)
¶
Step the environment for all drones.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
action
|
Array
|
Action for all drones, i.e., a batch of (n_drones, action_dim) arrays. |
required |
Source code in lsy_drone_racing/envs/multi_drone_race.py
Functions:¶
lsy_drone_racing.envs.race_core
¶
Core environment for drone racing simulations.
This module provides the shared logic for simulating drone racing environments. It defines a core environment class that wraps our drone simulation, drone control, gate tracking, and collision detection. The module serves as the base for both single-drone and multi-drone racing environments.
The environment is designed to be configurable, supporting:
- Different control modes (state or attitude)
- Customizable tracks with gates and obstacles
- Various randomization options for robust policy training
- Disturbance modeling for realistic flight conditions
- Vectorized execution for parallel training
This module is primarily used as a base for the higher-level environments in drone_race and multi_drone_race, which provide Gymnasium-compatible interfaces for reinforcement learning, MPC and other control techniques.
Classes¶
EnvData
¶
Struct holding the data of all auxiliary variables for the environment.
This dataclass stores the dynamic and static state of the environment that is not directly part of the physics simulation. It includes information about gate progress, drone status, and environment boundaries. Static variables are initialized once and do not change during the episode.
Attributes:
| Name | Type | Description |
|---|---|---|
target_gate |
Array
|
Current target gate index for each drone in each environment |
gates_visited |
Array
|
Boolean flags indicating which gates have been visited by each drone |
obstacles_visited |
Array
|
Boolean flags indicating which obstacles have been detected |
last_drone_pos |
Array
|
Previous positions of drones, used for gate passing detection |
marked_for_reset |
Array
|
Flags indicating which environments need to be reset |
disabled_drones |
Array
|
Flags indicating which drones have crashed or are otherwise disabled |
contact_masks |
Array
|
Masks for contact detection between drones and objects |
pos_limit_low |
Array
|
Lower position limits for the environment |
pos_limit_high |
Array
|
Upper position limits for the environment |
gate_mj_ids |
Array
|
MuJoCo IDs for the gates |
obstacle_mj_ids |
Array
|
MuJoCo IDs for the obstacles |
max_episode_steps |
Array
|
Maximum number of steps per episode |
sensor_range |
Array
|
Range at which drones can detect gates and obstacles |
Methods:¶
create(n_gates, n_obstacles, contact_masks, max_episode_steps, sensor_range, pos_limit_low, pos_limit_high, nominal_gates_pos, nominal_gates_quat, nominal_obstacles_pos, sim_data, device)
staticmethod
¶
Create a new environment data struct with default values.
Source code in lsy_drone_racing/envs/race_core.py
EnvSettings
¶
Struct holding all configuration settings for the environment.
Methods:¶
create(freq, max_episode_steps, pos_limit_low, pos_limit_high, camera, cam_config, disturbances, randomizations, device, autoreset=True)
staticmethod
¶
Create a new environment settings struct from a configuration dictionary.
Source code in lsy_drone_racing/envs/race_core.py
RaceCoreEnv(n_envs, n_drones, freq, sim_config, sensor_range, track, control_mode='state', disturbances=None, randomizations=None, seed=None, max_episode_steps=1500, device='cpu')
¶
The core environment for drone racing simulations.
This environment simulates a drone racing scenario where a single drone navigates through a series of gates in a predefined track. It supports various configuration options for randomization, disturbances, and physics models.
The environment provides:
- A customizable track with gates and obstacles
- Configurable simulation and control frequencies
- Support for different physics models (e.g., identified dynamics, analytical dynamics)
- Randomization of drone properties and initial conditions
- Disturbance modeling for realistic flight conditions
- Symbolic expressions for advanced control techniques (optional)
The environment tracks the drone's progress through the gates and provides termination conditions based on gate passages and collisions.
The observation space is a dictionary with the following keys:
- pos: Drone position
- quat: Drone orientation as a quaternion (x, y, z, w)
- vel: Drone linear velocity
- ang_vel: Drone angular velocity
- gates_pos: Positions of the gates
- gates_quat: Orientations of the gates
- gates_visited: Flags indicating if the drone already was/ is in the sensor range of the gates and the true position is known
- obstacles_pos: Positions of the obstacles
- obstacles_visited: Flags indicating if the drone already was/ is in the sensor range of the obstacles and the true position is known
- target_gate: The current target gate index
The action space consists of a desired full-state command [x, y, z, vx, vy, vz, ax, ay, az, yaw, rrate, prate, yrate] that is tracked by the drone's low-level controller, or a desired collective thrust and attitude command [collective thrust, roll, pitch, yaw].
Initialize the DroneRacingEnv.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_envs
|
int
|
Number of worlds in the vectorized environment. |
required |
n_drones
|
int
|
Number of drones. |
required |
freq
|
int
|
Environment step frequency. |
required |
sim_config
|
ConfigDict
|
Configuration dictionary for the simulation. |
required |
sensor_range
|
float
|
Sensor range for gate and obstacle detection. |
required |
control_mode
|
Literal['state', 'attitude']
|
Control mode for the drones. See |
'state'
|
track
|
ConfigDict
|
Track configuration. |
required |
disturbances
|
ConfigDict | None
|
Disturbance configuration. |
None
|
randomizations
|
ConfigDict | None
|
Randomization configuration. |
None
|
seed
|
int | None
|
None / -1 for a generated seed or the random seed directly. |
None
|
max_episode_steps
|
int
|
Maximum number of steps per episode. Needs to be tracked manually for vectorized environments. |
1500
|
device
|
Literal['cpu', 'gpu']
|
Device used for the environment and the simulation. |
'cpu'
|
Source code in lsy_drone_racing/envs/race_core.py
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 | |
Attributes¶
drone_mass
property
¶
The mass of the drones in the environment.
mocap_ids
property
¶
The MuJoCo mocap IDs for the gates and obstacles.
Methods:¶
build_apply_action_fn()
¶
Build a function that applies the action to the simulation.
Source code in lsy_drone_racing/envs/race_core.py
build_contact_check_fn()
¶
Build a function that checks for contacts between drones and gates/obstacles.
Note
Passing the full mjx_data into jit-compiled functions is expensive because the tree contains many elements and is flattened before the jit boundary. To avoid this cost, we fuse mjx_data into the contact_check function and only sync the gate and obstacle poses inside the function. This way, we can only pass EnvData, which is faster to canonicalize.
Source code in lsy_drone_racing/envs/race_core.py
build_render_sync_fn()
¶
Build a function that syncs the environment data with the MuJoCo data for rendering.
Source code in lsy_drone_racing/envs/race_core.py
build_reset_fn()
¶
Build a function that resets the environment data and simulation data.
Source code in lsy_drone_racing/envs/race_core.py
build_step_fn()
¶
Build a function that steps the environment.
Source code in lsy_drone_racing/envs/race_core.py
close()
¶
render()
¶
Render the environment.
Source code in lsy_drone_racing/envs/race_core.py
Functions:¶
build_action_space(control_mode, drone_model)
¶
Create the action space for the environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
control_mode
|
Literal['state', 'attitude']
|
The control mode to use. Either "state" for full-state control or "attitude" for attitude control. |
required |
drone_model
|
str
|
Drone model of the environment. |
required |
Returns:
| Type | Description |
|---|---|
Box
|
A Box space representing the action space for the specified control mode. |
Source code in lsy_drone_racing/envs/race_core.py
build_drone_reset_fn(randomizations)
¶
Build the reset hook for the simulation.
Source code in lsy_drone_racing/envs/race_core.py
build_dynamics_disturbance_fn(fn)
¶
Build the dynamics disturbance function for the simulation.
Source code in lsy_drone_racing/envs/race_core.py
build_observation_space(n_gates, n_obstacles)
¶
Create the observation space for the environment.
The observation space is a dictionary containing the drone state, gate information, and obstacle information.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_gates
|
int
|
Number of gates in the environment. |
required |
n_obstacles
|
int
|
Number of obstacles in the environment. |
required |
Source code in lsy_drone_racing/envs/race_core.py
build_track_randomization_fn(randomizations, track)
¶
Build the track randomization function for the simulation.
Source code in lsy_drone_racing/envs/race_core.py
obs(data)
¶
Return the observation of the environment.
Source code in lsy_drone_racing/envs/race_core.py
reward(data)
¶
Compute the reward for the current state.
Note
The current sparse reward function will most likely not work directly for training an agent. If you want to use reinforcement learning, you will need to define your own reward function.
Returns:
| Type | Description |
|---|---|
Array
|
Reward for the current state. |
Source code in lsy_drone_racing/envs/race_core.py
rng_spec2fn(fn_spec)
¶
Convert a function spec to a wrapped and scaled function from jax.random.
Source code in lsy_drone_racing/envs/race_core.py
terminated(data)
¶
truncated(data, max_episode_steps)
¶
Array of booleans indicating if the episode is truncated.