envs.real_drone_race¶
lsy_drone_racing.envs.real_race_env
¶
Real-world drone racing environments.
This module contains the environments for controlling a single or multiple drones in a real-world race track. It mirrors the drone_race module as closely as possible, but uses data from real-world observations from motion capture systems and sends actions to the real drones.
Classes¶
EnvData(target_gate, gates_visited, obstacles_visited, last_drone_pos, taken_off=False)
dataclass
¶
Struct holding the data of all auxiliary variables for the environment.
Methods:¶
create(n_drones, n_gates, n_obstacles)
classmethod
¶
Create an instance of the EnvData class.
Source code in lsy_drone_racing/envs/real_race_env.py
reset(last_drone_pos)
¶
Reset the environment data.
Source code in lsy_drone_racing/envs/real_race_env.py
RealDroneRaceEnv(drones, freq, track, randomizations, sensor_range=0.5, control_mode='state')
¶
Bases: RealRaceCoreEnv, Env
A Gymnasium environment for controlling a real Crazyflie drone in a physical race track.
This environment provides a standardized interface for deploying drone racing algorithms on physical hardware. It handles communication with the drone through the cflib2 library and tracks the drone's position using a motion capture system via ROS2.
The environment maintains the same observation and action space as its simulation counterpart, allowing for seamless transition from simulation to real-world deployment. It processes sensor data, handles gate passing detection, and manages the drone's state throughout the race.
Features: - Interfaces with physical Crazyflie drones through radio communication - Tracks drone position and orientation using motion capture data via ROS2 - Supports both state-based and attitude-based control modes - Provides sensor range simulation for gates and obstacles - Handles automatic return-to-home behavior when the race is completed
Note
This environment is designed for single-drone racing. For multi-drone racing, use the RealMultiDroneRaceEnv class instead.
Initialize the multi-drone environment.
Action space
The action space is a single action vector for the drone with the environment rank. See RealRaceCoreEnv for more information. Depending on the control mode, it is either a 13D desired drone state setpoint, or a 4D desired attitude and collective thrust setpoint.
Observation space
The observation space is a dictionary containing the state of all drones in the race. It mimics exactly the observation space of lsy_drone_racing.envs.drone_race.DroneRaceEnv.
Note
rclpy must be initialized before creating this environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
drones
|
list[dict[str, int]]
|
List of all drones in the race, including their channel and id. |
required |
freq
|
int
|
Environment step frequency. |
required |
track
|
ConfigDict
|
Track configuration (see |
required |
randomizations
|
ConfigDict
|
Randomization configuration. |
required |
sensor_range
|
float
|
Sensor range. Determines at which distance the exact position of the gates and obstacles is reveiled. |
0.5
|
control_mode
|
Literal['state', 'attitude']
|
Control mode of the drone. |
'state'
|
Source code in lsy_drone_racing/envs/real_race_env.py
Methods:¶
reset(*, seed=None, options=None)
¶
Reset the environment and return the initial observation and info.
Source code in lsy_drone_racing/envs/real_race_env.py
step(action)
¶
Perform a step in the environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
action
|
NDArray
|
Action to be taken by 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/real_race_env.py
RealMultiDroneRaceEnv(drones, rank, freq, track, randomizations, sensor_range=0.5, control_mode='state')
¶
Bases: RealRaceCoreEnv, Env
A Gymnasium environment for controlling a specific drone in a multi-drone physical race.
This environment extends the functionality of RealRaceCoreEnv to support multi-drone racing
scenarios. Each instance of this environment controls a single drone identified by its rank, but
maintains awareness of all drones in the race. This allows for coordinated multi-drone
deployments where each drone runs in a separate process with its own controller.
The environment handles communication with the specific drone through cflib2 and tracks all drones' positions using a motion capture system via ROS2. It provides observations that include the state of all drones, allowing controllers to implement collision avoidance or cooperative strategies.
Features: - Controls a specific drone in a multi-drone race based on its rank - Tracks all drones' positions and states via ROS2 - Supports both state-based and attitude-based control modes - Provides sensor range simulation for gates and obstacles - Handles automatic return-to-home behavior when the race is completed
Action space
The action space is a single action vector for the drone with the environment rank. See RealRaceCoreEnv for more information.
Warning
The action space differs from the action space of the simulated counterpart. This deviation is necessary to run different controller types at different frequencies that asynchronously publish ther commands to the drone.
Observation space
The observation space is a dictionary containing the state of all drones in the race. It mimics exactly the observation space of lsy_drone_racing.envs.multi_drone_race.MultiDroneRaceEnv.
Note
Each instance of this environment controls only one drone (specified by rank), but provides observations for all drones in the race. This allows us to run controllers at different frequencies for different drones. Consequently the step method applies actions only to the
Initialize the multi-drone environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
drones
|
list[dict[str, int]]
|
List of all drones in the race, including their channel and id. |
required |
rank
|
int
|
Rank of the drone that is controlled by this environment. |
required |
freq
|
int
|
Environment step frequency. |
required |
track
|
ConfigDict
|
Track configuration (see |
required |
randomizations
|
ConfigDict
|
Randomization configuration. |
required |
sensor_range
|
float
|
Sensor range. Determines at which distance the exact position of the gates and obstacles is reveiled. |
0.5
|
control_mode
|
Literal['state', 'attitude']
|
Control mode of the drone. |
'state'
|
Source code in lsy_drone_racing/envs/real_race_env.py
Methods:¶
reset(*, seed=None, options=None)
¶
Reset the environment and return the initial observation and info.
step(action)
¶
Perform a step in the environment.
Note
The action is applied only to the drone with the environment rank!
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
action
|
NDArray
|
Action to be taken by 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/real_race_env.py
RealRaceCoreEnv(drones, rank, freq, track, randomizations, sensor_range=0.5, control_mode='state')
¶
Deployable version of the (multi-agent) drone racing environments.
This class acts as a generic core implementation of the environment logic that can be reused for both single-agent and multi-agent deployments.
Create a deployable version of the drone racing environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
drones
|
list[dict[str, int]]
|
List of all drones in the race, including their channel and id. |
required |
rank
|
int
|
Rank of the drone that is controlled by this environment. |
required |
freq
|
int
|
Environment step frequency. |
required |
track
|
ConfigDict
|
Track configuration (see |
required |
randomizations
|
ConfigDict
|
Randomization configuration. |
required |
sensor_range
|
float
|
Sensor range. Determines at which distance the exact position of the gates and obstacles is reveiled. |
0.5
|
control_mode
|
Literal['state', 'attitude']
|
Control mode of the drone. |
'state'
|
Source code in lsy_drone_racing/envs/real_race_env.py
Methods:¶
close()
¶
Close the environment.
If the drone has finished the track, it will try to return to the start position. Irrespective of succeeding or not, the drone will be stopped immediately afterwards or in case of errors, and close the connections to the ROSConnector.
Source code in lsy_drone_racing/envs/real_race_env.py
info()
¶
obs()
¶
Return the observation of the environment.
Source code in lsy_drone_racing/envs/real_race_env.py
reward()
¶
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 |
|---|---|
float
|
Reward for the current state. |
Source code in lsy_drone_racing/envs/real_race_env.py
terminated()
¶
Check if the episode is terminated.