|
| 1 | +import logging |
| 2 | +from os import PathLike |
| 3 | +from pathlib import Path |
| 4 | +from typing import Type |
| 5 | + |
| 6 | +import gymnasium as gym |
| 7 | +import rcs |
| 8 | +from gymnasium.envs.registration import EnvCreator |
| 9 | +from lerobot.common.robots import make_robot_from_config |
| 10 | +from lerobot.common.robots.so101_follower.config_so101_follower import ( |
| 11 | + SO101FollowerConfig, |
| 12 | +) |
| 13 | +from lerobot.common.teleoperators.so101_leader.config_so101_leader import ( |
| 14 | + SO101LeaderConfig, |
| 15 | +) |
| 16 | +from lerobot.common.teleoperators.so101_leader.so101_leader import SO101Leader |
| 17 | +from lerobot.common.teleoperators.utils import make_teleoperator_from_config |
| 18 | +from rcs import common, sim |
| 19 | +from rcs.camera.hw import HardwareCameraSet |
| 20 | +from rcs.camera.sim import SimCameraSet |
| 21 | +from rcs.envs.base import ( |
| 22 | + CameraSetWrapper, |
| 23 | + ControlMode, |
| 24 | + GripperWrapper, |
| 25 | + RelativeActionSpace, |
| 26 | + RelativeTo, |
| 27 | + RobotEnv, |
| 28 | +) |
| 29 | +from rcs.envs.creators import RCSHardwareEnvCreator |
| 30 | +from rcs.envs.sim import CollisionGuard, GripperWrapperSim, RobotSimWrapper, SimWrapper |
| 31 | +from rcs.sim import SimCameraConfig, SimGripperConfig, SimRobotConfig |
| 32 | +from rcs_so101.hw import SO101, S0101Gripper |
| 33 | + |
| 34 | +logger = logging.getLogger(__name__) |
| 35 | +logger.setLevel(logging.INFO) |
| 36 | + |
| 37 | + |
| 38 | +class RCSSO101EnvCreator(RCSHardwareEnvCreator): |
| 39 | + def __call__( # type: ignore |
| 40 | + self, |
| 41 | + id: str, |
| 42 | + port: str, |
| 43 | + urdf_path: str, |
| 44 | + calibration_dir: PathLike | str | None = None, |
| 45 | + camera_set: HardwareCameraSet | None = None, |
| 46 | + max_relative_movement: float | tuple[float, float] | None = None, |
| 47 | + relative_to: RelativeTo = RelativeTo.LAST_STEP, |
| 48 | + ) -> gym.Env: |
| 49 | + if isinstance(calibration_dir, str): |
| 50 | + calibration_dir = Path(calibration_dir) |
| 51 | + cfg = SO101FollowerConfig(id=id, calibration_dir=calibration_dir, port=port) |
| 52 | + hf_robot = make_robot_from_config(cfg) |
| 53 | + robot = SO101(hf_robot, urdf_path=urdf_path) |
| 54 | + env: gym.Env = RobotEnv(robot, ControlMode.JOINTS) |
| 55 | + # env = FR3HW(env) |
| 56 | + |
| 57 | + gripper = S0101Gripper(hf_robot) |
| 58 | + env = GripperWrapper(env, gripper, binary=False) |
| 59 | + |
| 60 | + if camera_set is not None: |
| 61 | + camera_set.start() |
| 62 | + camera_set.wait_for_frames() |
| 63 | + logger.info("CameraSet started") |
| 64 | + env = CameraSetWrapper(env, camera_set) |
| 65 | + |
| 66 | + if max_relative_movement is not None: |
| 67 | + env = RelativeActionSpace(env, max_mov=max_relative_movement, relative_to=relative_to) |
| 68 | + |
| 69 | + return env |
| 70 | + |
| 71 | + @staticmethod |
| 72 | + def teleoperator( |
| 73 | + id: str, |
| 74 | + port: str, |
| 75 | + calibration_dir: PathLike | str | None = None, |
| 76 | + ) -> SO101Leader: |
| 77 | + if isinstance(calibration_dir, str): |
| 78 | + calibration_dir = Path(calibration_dir) |
| 79 | + cfg = SO101LeaderConfig(id=id, calibration_dir=calibration_dir, port=port) |
| 80 | + teleop = make_teleoperator_from_config(cfg) |
| 81 | + teleop.connect() |
| 82 | + return teleop |
| 83 | + |
| 84 | + |
| 85 | +class SO101SimEnvCreator(EnvCreator): |
| 86 | + def __call__( # type: ignore |
| 87 | + self, |
| 88 | + control_mode: ControlMode, |
| 89 | + robot_cfg: SimRobotConfig, |
| 90 | + urdf_path: str, |
| 91 | + mjcf: str, |
| 92 | + collision_guard: bool = False, |
| 93 | + gripper_cfg: SimGripperConfig | None = None, |
| 94 | + cameras: dict[str, SimCameraConfig] | None = None, |
| 95 | + max_relative_movement: float | tuple[float, float] | None = None, |
| 96 | + relative_to: RelativeTo = RelativeTo.LAST_STEP, |
| 97 | + sim_wrapper: Type[SimWrapper] | None = None, |
| 98 | + ) -> gym.Env: |
| 99 | + """ |
| 100 | + Creates a simulation environment for the FR3 robot. |
| 101 | + Args: |
| 102 | + control_mode (ControlMode): Control mode for the robot. |
| 103 | + robot_cfg (rcs.sim.SimRobotConfig): Configuration for the FR3 robot. |
| 104 | + collision_guard (bool): Whether to use collision guarding. If True, the same mjcf scene is used for collision guarding. |
| 105 | + gripper_cfg (rcs.sim.SimGripperConfig | None): Configuration for the gripper. If None, no gripper is used. |
| 106 | + camera_set_cfg (SimCameraSetConfig | None): Configuration for the camera set. If None, no cameras are used. |
| 107 | + max_relative_movement (float | tuple[float, float] | None): Maximum allowed movement. If float, it restricts |
| 108 | + translational movement in meters. If tuple, it restricts both translational (in meters) and rotational |
| 109 | + (in radians) movements. If None, no restriction is applied. |
| 110 | + relative_to (RelativeTo): Specifies whether the movement is relative to a configured origin or the last step. |
| 111 | + urdf_path (str | PathLike | None): Path to the URDF file. If None, the URDF file is automatically deduced |
| 112 | + which requires a UTN compatible lab scene to be present. |
| 113 | + mjcf (str | PathLike): Path to the Mujoco scene XML file. Defaults to "fr3_empty_world". |
| 114 | + sim_wrapper (Type[SimWrapper] | None): Wrapper to be applied before the simulation wrapper. This is useful |
| 115 | + for reset management e.g. resetting objects in the scene with correct observations. Defaults to None. |
| 116 | + Returns: |
| 117 | + gym.Env: The configured simulation environment for the FR3 robot. |
| 118 | + """ |
| 119 | + simulation = sim.Sim(mjcf) |
| 120 | + |
| 121 | + ik = rcs.common.IK(str(urdf_path)) |
| 122 | + robot = rcs.sim.SimRobot(simulation, ik, robot_cfg) |
| 123 | + env: gym.Env = RobotEnv(robot, control_mode) |
| 124 | + env = RobotSimWrapper(env, simulation, sim_wrapper) |
| 125 | + |
| 126 | + if cameras is not None: |
| 127 | + camera_set = SimCameraSet(simulation, cameras, physical_units=True, render_on_demand=True) |
| 128 | + env = CameraSetWrapper(env, camera_set, include_depth=True) |
| 129 | + |
| 130 | + if gripper_cfg is not None and isinstance(gripper_cfg, SimGripperConfig): |
| 131 | + gripper = sim.SimGripper(simulation, gripper_cfg) |
| 132 | + env = GripperWrapper(env, gripper, binary=False) |
| 133 | + env = GripperWrapperSim(env, gripper) |
| 134 | + |
| 135 | + if collision_guard: |
| 136 | + env = CollisionGuard.env_from_xml_paths( |
| 137 | + env, |
| 138 | + str(rcs.scenes.get(str(mjcf), mjcf)), |
| 139 | + str(urdf_path), |
| 140 | + gripper=gripper_cfg is not None, |
| 141 | + check_home_collision=False, |
| 142 | + control_mode=control_mode, |
| 143 | + tcp_offset=rcs.common.Pose(common.FrankaHandTCPOffset()), |
| 144 | + sim_gui=True, |
| 145 | + truncate_on_collision=True, |
| 146 | + ) |
| 147 | + if max_relative_movement is not None: |
| 148 | + env = RelativeActionSpace(env, max_mov=max_relative_movement, relative_to=relative_to) |
| 149 | + |
| 150 | + return env |
0 commit comments