Skip to content

Commit

Permalink
Changed in_range obervation space variable to visited
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasschlueter committed Dec 20, 2024
1 parent 9237ee2 commit 121217e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
8 changes: 4 additions & 4 deletions lsy_drone_racing/envs/drone_racing_deploy_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ def __init__(self, config: dict | Munch):
"target_gate": spaces.Discrete(n_gates, start=-1),
"gates_pos": spaces.Box(low=-np.inf, high=np.inf, shape=(n_gates, 3)),
"gates_rpy": spaces.Box(low=-np.pi, high=np.pi, shape=(n_gates, 3)),
"gates_in_range": spaces.Box(low=0, high=1, shape=(n_gates,), dtype=np.bool_),
"gates_visited": spaces.Box(low=0, high=1, shape=(n_gates,), dtype=np.bool_),
"obstacles_pos": spaces.Box(low=-np.inf, high=np.inf, shape=(n_obstacles, 3)),
"obstacles_in_range": spaces.Box(
"obstacles_visited": spaces.Box(
low=0, high=1, shape=(n_obstacles,), dtype=np.bool_
),
}
Expand Down Expand Up @@ -259,14 +259,14 @@ def obs(self) -> dict:
self.gates_visited = np.logical_or(self.gates_visited, in_range)
gates_pos[self.gates_visited] = real_gates_pos[self.gates_visited]
gates_rpy[self.gates_visited] = real_gates_rpy[self.gates_visited]
obs["gates_in_range"] = in_range
obs["gates_visited"] = in_range

in_range = (
np.linalg.norm(real_obstacles_pos[:, :2] - drone_pos[:2], axis=1) < sensor_range
)
self.obstacles_visited = np.logical_or(self.obstacles_visited, in_range)
obstacles_pos[self.obstacles_visited] = real_obstacles_pos[self.obstacles_visited]
obs["obstacles_in_range"] = in_range
obs["obstacles_visited"] = in_range

obs["gates_pos"] = gates_pos.astype(np.float32)
obs["gates_rpy"] = gates_rpy.astype(np.float32)
Expand Down
31 changes: 22 additions & 9 deletions lsy_drone_racing/envs/drone_racing_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ class DroneRacingEnv(gymnasium.Env):
- "ang_vel": Drone angular velocity
- "gates.pos": Positions of the gates
- "gates.rpy": Orientations of the gates
- "gates.in_range": Flags indicating if the drone is in the sensor range 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.in_range": Flags indicating if the drone is in the sensor range 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
Expand Down Expand Up @@ -113,9 +113,9 @@ def __init__(self, config: dict):
"target_gate": spaces.Discrete(n_gates, start=-1),
"gates_pos": spaces.Box(low=-np.inf, high=np.inf, shape=(n_gates, 3)),
"gates_rpy": spaces.Box(low=-np.pi, high=np.pi, shape=(n_gates, 3)),
"gates_in_range": spaces.Box(low=0, high=1, shape=(n_gates,), dtype=np.bool_),
"gates_visited": spaces.Box(low=0, high=1, shape=(n_gates,), dtype=np.bool_),
"obstacles_pos": spaces.Box(low=-np.inf, high=np.inf, shape=(n_obstacles, 3)),
"obstacles_in_range": spaces.Box(
"obstacles_visited": spaces.Box(
low=0, high=1, shape=(n_obstacles,), dtype=np.bool_
),
}
Expand All @@ -125,6 +125,9 @@ def __init__(self, config: dict):
self._steps = 0
self._last_drone_pos = np.zeros(3)

self.gates_visited = np.array([False] * len(config.env.track.gates))
self.obstacles_visited = np.array([False] * len(config.env.track.obstacles))

def reset(
self, *, seed: int | None = None, options: dict | None = None
) -> tuple[dict[str, NDArray[np.floating]], dict]:
Expand Down Expand Up @@ -224,20 +227,30 @@ def obs(self) -> dict[str, NDArray[np.floating]]:
# Add the gate and obstacle poses to the info. If gates or obstacles are in sensor range,
# use the actual pose, otherwise use the nominal pose.
in_range = self.sim.in_range(gates, self.sim.drone, self.config.env.sensor_range)
self.gates_visited = np.logical_or(self.gates_visited, in_range)

gates_pos = np.stack([g["nominal.pos"] for g in gates.values()])
gates_pos[in_range] = np.stack([g["pos"] for g in gates.values()])[in_range]
gates_pos[self.gates_visited] = np.stack([g["pos"] for g in gates.values()])[
self.gates_visited
]
gates_rpy = np.stack([g["nominal.rpy"] for g in gates.values()])
gates_rpy[in_range] = np.stack([g["rpy"] for g in gates.values()])[in_range]
gates_rpy[self.gates_visited] = np.stack([g["rpy"] for g in gates.values()])[
self.gates_visited
]
obs["gates_pos"] = gates_pos.astype(np.float32)
obs["gates_rpy"] = gates_rpy.astype(np.float32)
obs["gates_in_range"] = in_range
obs["gates_visited"] = self.gates_visited

obstacles = self.sim.obstacles
in_range = self.sim.in_range(obstacles, self.sim.drone, self.config.env.sensor_range)
self.obstacles_visited = np.logical_or(self.obstacles_visited, in_range)

obstacles_pos = np.stack([o["nominal.pos"] for o in obstacles.values()])
obstacles_pos[in_range] = np.stack([o["pos"] for o in obstacles.values()])[in_range]
obstacles_pos[self.obstacles_visited] = np.stack([o["pos"] for o in obstacles.values()])[
self.obstacles_visited
]
obs["obstacles_pos"] = obstacles_pos.astype(np.float32)
obs["obstacles_in_range"] = in_range
obs["obstacles_visited"] = self.obstacles_visited

if "observation" in self.sim.disturbances:
obs = self.sim.disturbances["observation"].apply(obs)
Expand Down

0 comments on commit 121217e

Please sign in to comment.