Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework camera logic #232

Open
wants to merge 40 commits into
base: dev-v1.2.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
ee8bab1
change conaffinity for base terrain (by default not colliding)
stimpfli Feb 27, 2024
de23c7c
Get max floor height from the arenas
stimpfli Jul 13, 2024
cf23081
functionning zstabilized camera
stimpfli Jul 13, 2024
ab0ba31
add config for new cameras, change the model to remove cameras, modif…
stimpfli Jul 13, 2024
668c7c8
added new camera handling zstab, yawonly, gravityaligned and normal
stimpfli Jan 8, 2025
ed40a4a
remove cameras from xml
stimpfli Jan 8, 2025
698342f
accommodate the fly and simulation with the new camera handling
stimpfli Jan 8, 2025
0cf248a
fixing non decomposed forces with no perspective arrow length
stimpfli Jan 8, 2025
964b0d0
remove prints
stimpfli Jan 8, 2025
340fcfb
fix bugs in gravity aligned and yaw only cameras
stimpfli Jan 13, 2025
64928da
clean up and doc
stimpfli Jan 13, 2025
c5f29c1
Remove old cameras and unecessary xmls
stimpfli Jan 13, 2025
be3831d
remove old camera simulaiton code + fix YawOnlyCamera when used with …
stimpfli Jan 13, 2025
e7dd3e4
modif and test all locomotion examples
stimpfli Jan 13, 2025
b00a532
modif and test olfaction
stimpfli Jan 13, 2025
7910781
modif and test path int
stimpfli Jan 13, 2025
34502df
changed and test head stabilization
stimpfli Jan 14, 2025
80a1e7e
revert testing changes
stimpfli Jan 14, 2025
4dd33b1
implement and test cameras vision
stimpfli Jan 14, 2025
2a79e58
update and test notebooks
stimpfli Jan 14, 2025
0797620
black reformating
stimpfli Jan 14, 2025
0d944ae
black reformating
stimpfli Jan 14, 2025
8553f54
black reformating
stimpfli Jan 14, 2025
e2c7349
remove old xmls from config
stimpfli Jan 15, 2025
5a6a743
removing ruff
stimpfli Jan 15, 2025
79d60a4
fixing xml
stimpfli Jan 15, 2025
2aec0da
fixing xml
stimpfli Jan 15, 2025
d06a056
passing the tests
stimpfli Jan 15, 2025
eb3f862
removing convexhull for mujoco 3.2.5 compatibility
stimpfli Jan 15, 2025
ad7205d
recent mujoco release breaking dm_control
stimpfli Jan 15, 2025
e941968
modify unwated changes
stimpfli Jan 15, 2025
4dc34c5
checkout main
stimpfli Jan 15, 2025
322f5b9
black and fix path exploration test
stimpfli Jan 15, 2025
01b42ff
solving merge conflicts
stimpfli Jan 15, 2025
8312cfa
fixed typo in merge
stimpfli Jan 16, 2025
96ffb41
improper camera.py merging
stimpfli Jan 16, 2025
10b9340
correct typos
tkclam Jan 16, 2025
67cbec9
rename ZStabCamera to ZStabilizedCamera and rename targeted_flies_id …
tkclam Jan 16, 2025
f870ba9
make methods private
tkclam Jan 16, 2025
21c62d8
code style changes
tkclam Jan 16, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion flygym/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .simulation import Simulation, SingleFlySimulation
from .fly import Fly
from .camera import Camera, NeckCamera
from .camera import Camera, YawOnlyCamera, ZStabilizedCamera, GravityAlignedCamera
from .util import get_data_path, load_config
from dm_control.rl.control import PhysicsError

Expand All @@ -9,3 +9,17 @@
is_rendering_skipped = (
"SKIP_RENDERING" in environ and environ["SKIP_RENDERING"] == "true"
)

__all__ = [
"Simulation",
"SingleFlySimulation",
"Fly",
"Camera",
"YawOnlyCamera",
"ZStabilizedCamera",
"GravityAlignedCamera",
"PhysicsError",
"get_data_path",
"load_config",
"is_rendering_skipped",
]
23 changes: 23 additions & 0 deletions flygym/arena/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,19 @@ def step(self, dt: float, physics: mjcf.Physics, *args, **kwargs) -> None:
"""
return

@abstractmethod
def _get_max_floor_height(self) -> float:
"""Get the height of the floor of the arena. This is useful for
camera rendering. The camera should be placed at a height that is
slightly above the floor to avoid rendering artifacts.

Returns
-------
float
The height of the floor of the arena in mm.
"""
pass


class FlatTerrain(BaseArena):
"""Flat terrain with no obstacles.
Expand Down Expand Up @@ -247,6 +260,7 @@ def __init__(
material=grid,
size=ground_size,
friction=friction,
conaffinity=0,
)
self.friction = friction
if scale_bar_pos:
Expand All @@ -263,3 +277,12 @@ def get_spawn_position(
self, rel_pos: np.ndarray, rel_angle: np.ndarray
) -> tuple[np.ndarray, np.ndarray]:
return rel_pos, rel_angle

def _get_max_floor_height(self) -> float:
geom = self.root_element.find("geom", "ground")
try:
plane_height = geom.pos[2]
except TypeError:
plane_height = 0.0

return plane_height
20 changes: 20 additions & 0 deletions flygym/arena/complex_terrain.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ def get_spawn_position(
adj_pos = rel_pos + np.array([0, 0, self.gap_depth / 2])
return adj_pos, rel_angle

def _get_max_floor_height(self):
return self.gap_depth / 2


class BlocksTerrain(BaseArena):
"""Terrain formed by blocks at random heights.
Expand Down Expand Up @@ -185,6 +188,8 @@ def __init__(
self.height_range = height_range
rand_state = np.random.RandomState(rand_seed)

self.max_height = -np.inf

x_centers = np.arange(x_range[0] + block_size / 2, x_range[1], block_size)
y_centers = np.arange(y_range[0] + block_size / 2, y_range[1], block_size)
for i, x_pos in enumerate(x_centers):
Expand All @@ -197,6 +202,8 @@ def __init__(
else:
height = 0.1 + rand_state.uniform(*height_range)

self.max_height = max(self.max_height, height)

self.root_element.worldbody.add(
"geom",
type="box",
Expand Down Expand Up @@ -231,6 +238,9 @@ def get_spawn_position(
adj_pos = rel_pos + np.array([0, 0, 0.1])
return adj_pos, rel_angle

def _get_max_floor_height(self):
return self.max_height


class MixedTerrain(BaseArena):
"""A mixture of flat, blocks, and gaps terrains.
Expand Down Expand Up @@ -289,6 +299,8 @@ def __init__(

self._height_expected_value = np.mean([*height_range])

self._max_block_height = -np.inf

# 3 repetitions, each consisting of a block part, 2 gaps, and a flat part
for x_range in [(-4, 5), (5, 14), (14, 23)]:
# block part
Expand Down Expand Up @@ -316,6 +328,10 @@ def __init__(
y_pos,
height / 2 - block_size / 2 - self._height_expected_value - 0.1,
)
self._max_block_height = max(
self._max_block_height,
height - self._height_expected_value - 0.1,
)
self.root_element.worldbody.add(
"geom",
type="box",
Expand Down Expand Up @@ -391,3 +407,7 @@ def get_spawn_position(
) -> tuple[np.ndarray, np.ndarray]:
adj_pos = rel_pos + np.array([0, 0, -1 * self._height_expected_value])
return adj_pos, rel_angle

def _get_max_floor_height(self):
# The floor and gap tops are at z=0
return max(0, self._max_block_height)
32 changes: 9 additions & 23 deletions flygym/arena/sensory_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ class OdorArena(BaseArena):
The function that, given a distance from the odor source, returns
the relative intensity of the odor. By default, this is an inverse
square relationship.
birdeye_cam : dm_control.mujoco.Camera
MuJoCo camera that gives a birdeye view of the arena.
birdeye_cam_zoom : dm_control.mujoco.Camera
MuJoCo camera that gives a birdeye view of the arena, zoomed in
toward the fly.

Parameters
----------
Expand Down Expand Up @@ -119,24 +114,6 @@ def __init__(
)
self.diffuse_func = diffuse_func

# Add birdeye camera
self.birdeye_cam = self.root_element.worldbody.add(
"camera",
name="birdeye_cam",
mode="fixed",
pos=(self.odor_source[:, 0].max() / 2, 0, 35),
euler=(0, 0, 0),
fovy=45,
)
self.birdeye_cam_zoom = self.root_element.worldbody.add(
"camera",
name="birdeye_cam_zoom",
mode="fixed",
pos=(11, 0, 29),
euler=(0, 0, 0),
fovy=45,
)

# Add markers at the odor sources
if marker_colors is None:
color_cycle_rgb = load_config()["color_cycle_rgb"]
Expand Down Expand Up @@ -209,3 +186,12 @@ def get_olfaction(self, antennae_pos: np.ndarray) -> np.ndarray:
@property
def odor_dimensions(self) -> int:
return self.peak_odor_intensity.shape[1]

def _get_max_floor_height(self) -> float:
geom = self.root_element.find("geom", "ground")
try:
plane_height = geom.pos[2]
except TypeError:
plane_height = 0.0

return plane_height
6 changes: 6 additions & 0 deletions flygym/arena/tethered.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ def spawn_entity(
"joint", name="prismatic_support_1", limited=True, range=(0, 1e-10)
)

def _get_max_floor_height(self) -> float:
raise NotImplementedError


class Ball(Tethered):
"""Fly tethered on a spherical treadmill.
Expand Down Expand Up @@ -126,3 +129,6 @@ def __init__(
"joint", name="treadmill_joint", type="ball", limited="false"
)
treadmill_body.add("inertial", pos=[0, 0, 0], mass=mass)

def _get_max_floor_height(self) -> float:
raise NotImplementedError
Loading
Loading