Skip to content

Commit 977c04e

Browse files
committed
refactor(sim envs): simple and lab pick up share more code
- update models - refactored random cube pos lab wrapper into random cube pos wrapper - refactored fr3 lab pick up sim success wrapper into pick cube success wrapper - refactored sim env creation for pick up tasks into own method - fix cam robot: does not register itself to convergence callback anymore
1 parent 2d905eb commit 977c04e

File tree

4 files changed

+44
-122
lines changed

4 files changed

+44
-122
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ __pycache__
88
.ruff_cache
99
.mypy_cache
1010
dist
11-
.venv
11+
*venv*
1212
config.yaml
1313
MUJOCO_LOG.TXT
1414
src/pybind/mujoco

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ FetchContent_MakeAvailable(libfranka rl pybind11)
9898
if (${INCLUDE_UTN_MODELS})
9999
if (DEFINED GITLAB_MODELS_TOKEN)
100100
include(download_scenes)
101-
set(ref 48c88f05aa34730acbe80bf4b50e7d5a789b927a)
101+
set(ref 00869d446940260ec233469182a1ed2290cc60e3)
102102
FetchContent_Declare(
103103
scenes
104104
URL "https://gitos.rrze.fau.de/api/v4/projects/1100/repository/archive?path=scenes&sha=${ref}"

python/rcsss/envs/factories.py

Lines changed: 40 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@
2121
from rcsss.envs.hw import FR3HW
2222
from rcsss.envs.sim import (
2323
CollisionGuard,
24-
FR3LabPickUpSimSuccessWrapper,
2524
FR3Sim,
26-
FR3SimplePickUpSimSuccessWrapper,
25+
PickCubeSuccessWrapper,
2726
RandomCubePos,
28-
RandomCubePosLab,
2927
SimWrapper,
3028
)
3129
from rcsss.envs.space_utils import Vec7Type
@@ -211,11 +209,37 @@ def __call__( # type: ignore
211209
)
212210

213211

212+
def make_sim_task_envs(
213+
mjcf: str,
214+
render_mode: str = "human",
215+
control_mode: ControlMode = ControlMode.CARTESIAN_TRPY,
216+
delta_actions: bool = True,
217+
camera_cfg: SimCameraConfig | None = None,
218+
) -> gym.Env:
219+
220+
env_rel = fr3_sim_env(
221+
control_mode=control_mode,
222+
robot_cfg=default_fr3_sim_robot_cfg(mjcf),
223+
collision_guard=False,
224+
gripper_cfg=default_fr3_sim_gripper_cfg(),
225+
camera_set_cfg=camera_cfg,
226+
max_relative_movement=(0.2, np.deg2rad(45)) if delta_actions else None,
227+
relative_to=RelativeTo.LAST_STEP,
228+
mjcf=mjcf,
229+
sim_wrapper=RandomCubePos,
230+
)
231+
env_rel = PickCubeSuccessWrapper(env_rel)
232+
if render_mode == "human":
233+
env_rel.get_wrapper_attr("sim").open_gui()
234+
235+
return env_rel
236+
237+
214238
class FR3SimplePickUpSim(EnvCreator):
215239
def __call__( # type: ignore
216240
self,
217241
render_mode: str = "human",
218-
control_mode: str = "xyzrpy",
242+
control_mode: ControlMode = ControlMode.CARTESIAN_TRPY,
219243
resolution: tuple[int, int] | None = None,
220244
frame_rate: int = 10,
221245
delta_actions: bool = True,
@@ -239,33 +263,14 @@ def __call__( # type: ignore
239263
frame_rate=frame_rate,
240264
physical_units=True,
241265
)
242-
env_rel = fr3_sim_env(
243-
control_mode=(
244-
ControlMode.CARTESIAN_TRPY
245-
if control_mode == "xyzrpy"
246-
else ControlMode.JOINTS if control_mode == "joints" else ControlMode.CARTESIAN_TQuart
247-
),
248-
robot_cfg=default_fr3_sim_robot_cfg(),
249-
collision_guard=False,
250-
gripper_cfg=default_fr3_sim_gripper_cfg(),
251-
camera_set_cfg=camera_cfg,
252-
max_relative_movement=(0.2, np.deg2rad(45)) if delta_actions else None,
253-
relative_to=RelativeTo.LAST_STEP,
254-
mjcf="fr3_simple_pick_up",
255-
sim_wrapper=RandomCubePos,
256-
)
257-
env_rel = FR3SimplePickUpSimSuccessWrapper(env_rel)
258-
if render_mode == "human":
259-
env_rel.get_wrapper_attr("sim").open_gui()
260-
261-
return env_rel
266+
return make_sim_task_envs("simple_pick_up", render_mode, control_mode, delta_actions, camera_cfg)
262267

263268

264269
class FR3SimplePickUpSimDigitHand(EnvCreator):
265270
def __call__( # type: ignore
266271
self,
267272
render_mode: str = "human",
268-
control_mode: str = "xyzrpy",
273+
control_mode: ControlMode = ControlMode.CARTESIAN_TRPY,
269274
resolution: tuple[int, int] | None = None,
270275
frame_rate: int = 10,
271276
delta_actions: bool = True,
@@ -282,27 +287,7 @@ def __call__( # type: ignore
282287
frame_rate=frame_rate,
283288
physical_units=True,
284289
)
285-
286-
env_rel = fr3_sim_env(
287-
control_mode=(
288-
ControlMode.CARTESIAN_TRPY
289-
if control_mode == "xyzrpy"
290-
else ControlMode.JOINTS if control_mode == "joints" else ControlMode.CARTESIAN_TQuart
291-
),
292-
robot_cfg=default_fr3_sim_robot_cfg("fr3_simple_pick_up_digit_hand"),
293-
collision_guard=False,
294-
gripper_cfg=default_fr3_sim_gripper_cfg(),
295-
camera_set_cfg=camera_cfg,
296-
max_relative_movement=(0.2, np.deg2rad(45)) if delta_actions else None,
297-
relative_to=RelativeTo.LAST_STEP,
298-
mjcf="fr3_simple_pick_up_digit_hand",
299-
sim_wrapper=RandomCubePos,
300-
)
301-
env_rel = FR3SimplePickUpSimSuccessWrapper(env_rel)
302-
if render_mode == "human":
303-
env_rel.get_wrapper_attr("sim").open_gui()
304-
305-
return env_rel
290+
return make_sim_task_envs("fr3_simple_pick_up_digit_hand", render_mode, control_mode, delta_actions, camera_cfg)
306291

307292

308293
class CamRobot(gym.Wrapper):
@@ -312,7 +297,7 @@ def __init__(self, env, cam_robot_joints: Vec7Type):
312297
self.unwrapped: FR3Env
313298
assert isinstance(self.unwrapped.robot, sim.FR3), "Robot must be a sim.FR3 instance."
314299
self.sim = env.get_wrapper_attr("sim")
315-
self.cam_robot = rcsss.sim.FR3(self.sim, "1", env.unwrapped.robot.get_ik())
300+
self.cam_robot = rcsss.sim.FR3(self.sim, "1", env.unwrapped.robot.get_ik(), register_convergence_callback=False)
316301
self.cam_robot.set_parameters(default_fr3_sim_robot_cfg("lab_simple_pick_up_digit_hand"))
317302
self.cam_robot_joints = cam_robot_joints
318303

@@ -332,7 +317,7 @@ def __call__( # type: ignore
332317
self,
333318
cam_robot_joints: Vec7Type,
334319
render_mode: str = "human",
335-
control_mode: str = "xyzrpy",
320+
control_mode: ControlMode = ControlMode.CARTESIAN_TRPY,
336321
resolution: tuple[int, int] | None = None,
337322
frame_rate: int = 10,
338323
delta_actions: bool = True,
@@ -352,25 +337,11 @@ def __call__( # type: ignore
352337
frame_rate=frame_rate,
353338
physical_units=True,
354339
)
355-
356-
env_rel = fr3_sim_env(
357-
control_mode=(
358-
ControlMode.CARTESIAN_TRPY
359-
if control_mode == "xyzrpy"
360-
else ControlMode.JOINTS if control_mode == "joints" else ControlMode.CARTESIAN_TQuart
361-
),
362-
robot_cfg=default_fr3_sim_robot_cfg("lab_simple_pick_up_digit_hand"),
363-
collision_guard=False,
364-
gripper_cfg=default_fr3_sim_gripper_cfg(),
365-
camera_set_cfg=camera_cfg,
366-
max_relative_movement=(0.2, np.deg2rad(45)) if delta_actions else None,
367-
relative_to=RelativeTo.LAST_STEP,
368-
mjcf="lab_simple_pick_up_digit_hand",
369-
sim_wrapper=RandomCubePosLab,
340+
env_rel = make_sim_task_envs(
341+
"lab_simple_pick_up_digit_hand",
342+
render_mode,
343+
control_mode,
344+
delta_actions,
345+
camera_cfg,
370346
)
371-
env_rel = FR3LabPickUpSimSuccessWrapper(env_rel)
372-
env_rel = CamRobot(env_rel, cam_robot_joints)
373-
if render_mode == "human":
374-
sim = env_rel.get_wrapper_attr("sim")
375-
sim.open_gui()
376-
return env_rel
347+
return CamRobot(env_rel, cam_robot_joints)

python/rcsss/envs/sim.py

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def env_from_xml_paths(
176176
)
177177

178178

179-
class RandomCubePosLab(SimWrapper):
179+
class RandomCubePos(SimWrapper):
180180
"""Wrapper to randomly place cube in the lab environments."""
181181

182182
def reset(
@@ -197,25 +197,7 @@ def reset(
197197
return obs, info
198198

199199

200-
class RandomCubePos(SimWrapper):
201-
"""Wrapper to randomly place cube in the FR3SimplePickUpSim environment."""
202-
203-
def reset(
204-
self, seed: int | None = None, options: dict[str, Any] | None = None
205-
) -> tuple[dict[str, Any], dict[str, Any]]:
206-
obs, info = super().reset(seed=seed, options=options)
207-
self.sim.step(1)
208-
iso_cube = [0.498, 0.0, 0.226]
209-
pos_z = 0.03
210-
pos_x = iso_cube[0] + np.random.random() * 0.2 - 0.1
211-
pos_y = iso_cube[1] + np.random.random() * 0.2 - 0.1
212-
213-
self.sim.data.joint("yellow-box-joint").qpos[:3] = [pos_x, pos_y, pos_z]
214-
215-
return obs, info
216-
217-
218-
class FR3SimplePickUpSimSuccessWrapper(gym.Wrapper):
200+
class PickCubeSuccessWrapper(gym.Wrapper):
219201
"""Wrapper to check if the cube is successfully picked up in the FR3SimplePickUpSim environment."""
220202

221203
EE_HOME = np.array([0.34169773, 0.00047028, 0.4309004])
@@ -241,34 +223,3 @@ def step(self, action: dict[str, Any]):
241223
reward = -diff_cube_home - diff_ee_cube
242224

243225
return obs, reward, success, truncated, info
244-
245-
246-
class FR3LabPickUpSimSuccessWrapper(gym.Wrapper):
247-
"""
248-
Wrapper to check if the cube is successfully picked up in the LabPickupSim environments.
249-
This is also used to handle the reset of the second robot.
250-
"""
251-
252-
EE_HOME = np.array([0.34169773, 0.00047028, 0.4309004])
253-
254-
def __init__(self, env):
255-
super().__init__(env)
256-
self.unwrapped: FR3Env
257-
assert isinstance(self.unwrapped.robot, sim.FR3), "Robot must be a sim.FR3 instance."
258-
self.sim = env.get_wrapper_attr("sim")
259-
260-
def step(self, action: dict[str, Any]):
261-
obs, reward, done, truncated, info = super().step(action)
262-
263-
success = (
264-
self.sim.data.joint("yellow-box-joint").qpos[2] > 0.3
265-
and obs["gripper"] == GripperWrapper.BINARY_GRIPPER_CLOSED
266-
)
267-
diff_ee_cube = np.linalg.norm(
268-
self.sim.data.joint("yellow-box-joint").qpos[:3]
269-
- self.unwrapped.robot.get_cartesian_position().translation()
270-
)
271-
diff_cube_home = np.linalg.norm(self.sim.data.joint("yellow-box-joint").qpos[:3] - self.EE_HOME)
272-
reward = -diff_cube_home - diff_ee_cube
273-
274-
return obs, reward, success, truncated, info

0 commit comments

Comments
 (0)