|
| 1 | +import logging |
| 2 | + |
| 3 | +from rcs._core.common import RobotPlatform |
| 4 | +from rcs.control.fr3_desk import FCI, ContextManager, Desk, load_creds_fr3_desk |
| 5 | +from rcs.envs.base import ControlMode, RelativeTo |
| 6 | +from rcs.envs.creators import RCSFR3EnvCreator, RCSSimEnvCreator |
| 7 | +from rcs.envs.utils import ( |
| 8 | + default_digit, |
| 9 | + default_fr3_hw_gripper_cfg, |
| 10 | + default_fr3_hw_robot_cfg, |
| 11 | + default_fr3_sim_gripper_cfg, |
| 12 | + default_fr3_sim_robot_cfg, |
| 13 | + default_mujoco_cameraset_cfg, |
| 14 | +) |
| 15 | + |
| 16 | +logger = logging.getLogger(__name__) |
| 17 | +logger.setLevel(logging.INFO) |
| 18 | + |
| 19 | +ROBOT_IP = "192.168.101.1" |
| 20 | +ROBOT_INSTANCE = RobotPlatform.SIMULATION |
| 21 | + |
| 22 | + |
| 23 | +""" |
| 24 | +Create a .env file in the same directory as this file with the following content: |
| 25 | +FR3_USERNAME=<username on franka desk> |
| 26 | +FR3_PASSWORD=<password on franka desk> |
| 27 | +
|
| 28 | +When you use a real FR3 you first need to unlock its joints using the following cli script: |
| 29 | +
|
| 30 | +python -m rcs fr3 unlock <ip> |
| 31 | +
|
| 32 | +or put it into guiding mode using: |
| 33 | +
|
| 34 | +python -m rcs fr3 guiding-mode <ip> |
| 35 | +
|
| 36 | +When you are done you lock it again using: |
| 37 | +
|
| 38 | +python -m rcs fr3 lock <ip> |
| 39 | +
|
| 40 | +or even shut it down using: |
| 41 | +
|
| 42 | +python -m rcs fr3 shutdown <ip> |
| 43 | +""" |
| 44 | + |
| 45 | + |
| 46 | +def main(): |
| 47 | + resource_manger: FCI | ContextManager |
| 48 | + if ROBOT_INSTANCE == RobotPlatform.HARDWARE: |
| 49 | + user, pw = load_creds_fr3_desk() |
| 50 | + resource_manger = FCI(Desk(ROBOT_IP, user, pw), unlock=False, lock_when_done=False) |
| 51 | + else: |
| 52 | + resource_manger = ContextManager() |
| 53 | + with resource_manger: |
| 54 | + if ROBOT_INSTANCE == RobotPlatform.HARDWARE: |
| 55 | + env_rel = RCSFR3EnvCreator()( |
| 56 | + ip=ROBOT_IP, |
| 57 | + control_mode=ControlMode.CARTESIAN_TQuat, |
| 58 | + robot_cfg=default_fr3_hw_robot_cfg(), |
| 59 | + collision_guard="lab", |
| 60 | + gripper_cfg=default_fr3_hw_gripper_cfg(), |
| 61 | + digit_set=default_digit({"digit_0": "D21182"}), |
| 62 | + max_relative_movement=0.5, |
| 63 | + relative_to=RelativeTo.LAST_STEP, |
| 64 | + ) |
| 65 | + else: |
| 66 | + env_rel = RCSSimEnvCreator()( |
| 67 | + control_mode=ControlMode.CARTESIAN_TQuat, |
| 68 | + robot_cfg=default_fr3_sim_robot_cfg(), |
| 69 | + collision_guard=False, |
| 70 | + gripper_cfg=default_fr3_sim_gripper_cfg(), |
| 71 | + camera_set_cfg=default_mujoco_cameraset_cfg(), |
| 72 | + max_relative_movement=0.5, |
| 73 | + relative_to=RelativeTo.LAST_STEP, |
| 74 | + ) |
| 75 | + env_rel.get_wrapper_attr("sim").open_gui() |
| 76 | + |
| 77 | + env_rel.reset() |
| 78 | + print(env_rel.unwrapped.robot.get_cartesian_position()) # type: ignore |
| 79 | + |
| 80 | + for _ in range(10): |
| 81 | + for _ in range(10): |
| 82 | + # move 1cm in x direction (forward) and close gripper |
| 83 | + act = {"tquat": [0.01, 0, 0, 0, 0, 0, 1], "gripper": 0} |
| 84 | + obs, reward, terminated, truncated, info = env_rel.step(act) |
| 85 | + if truncated or terminated: |
| 86 | + logger.info("Truncated or terminated!") |
| 87 | + return |
| 88 | + for _ in range(10): |
| 89 | + # move 1cm in negative x direction (backward) and open gripper |
| 90 | + act = {"tquat": [-0.01, 0, 0, 0, 0, 0, 1], "gripper": 1} |
| 91 | + obs, reward, terminated, truncated, info = env_rel.step(act) |
| 92 | + if truncated or terminated: |
| 93 | + logger.info("Truncated or terminated!") |
| 94 | + return |
| 95 | + env_rel.close() |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == "__main__": |
| 99 | + main() |
0 commit comments