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