|
| 1 | +import logging |
| 2 | +from typing import Union |
| 3 | + |
| 4 | +from rcsss.control.fr3_desk import FCI, Desk, DummyResourceManager |
| 5 | +from rcsss.control.utils import load_creds_fr3_desk |
| 6 | +from rcsss.envs.base import ControlMode, RelativeTo, RobotInstance |
| 7 | +from rcsss.envs.factories import fr3_hw_env, fr3_sim_env |
| 8 | +from rcsss.envs.utils import ( |
| 9 | + default_fr3_hw_gripper_cfg, |
| 10 | + default_fr3_hw_robot_cfg, |
| 11 | + default_fr3_sim_robot_cfg, |
| 12 | + default_mujoco_cameraset_cfg, |
| 13 | +) |
| 14 | + |
| 15 | +logger = logging.getLogger(__name__) |
| 16 | +logger.setLevel(logging.INFO) |
| 17 | + |
| 18 | +ROBOT_IP = "192.168.101.1" |
| 19 | +ROBOT_INSTANCE = RobotInstance.SIMULATION |
| 20 | + |
| 21 | + |
| 22 | +""" |
| 23 | +Create a .env file in the same directory as this file with the following content: |
| 24 | +FR3_USERNAME=<username on franka desk> |
| 25 | +FR3_PASSWORD=<password on franka desk> |
| 26 | +
|
| 27 | +When you use a real FR3 you first need to unlock its joints using the following cli script: |
| 28 | +
|
| 29 | +python -m rcsss fr3 unlock <ip> |
| 30 | +
|
| 31 | +or put it into guiding mode using: |
| 32 | +
|
| 33 | +python -m rcsss fr3 guiding-mode <ip> |
| 34 | +
|
| 35 | +When you are done you lock it again using: |
| 36 | +
|
| 37 | +python -m rcsss fr3 lock <ip> |
| 38 | +
|
| 39 | +or even shut it down using: |
| 40 | +
|
| 41 | +python -m rcsss fr3 shutdown <ip> |
| 42 | +""" |
| 43 | + |
| 44 | + |
| 45 | +def main(): |
| 46 | + resource_manger: FCI | DummyResourceManager |
| 47 | + if ROBOT_INSTANCE == RobotInstance.HARDWARE: |
| 48 | + user, pw = load_creds_fr3_desk() |
| 49 | + resource_manger = FCI(Desk(ROBOT_IP, user, pw), unlock=False, lock_when_done=False) |
| 50 | + else: |
| 51 | + resource_manger = DummyResourceManager() |
| 52 | + with resource_manger: |
| 53 | + binary_action = False |
| 54 | + if ROBOT_INSTANCE == RobotInstance.HARDWARE: |
| 55 | + env_rel = fr3_hw_env( |
| 56 | + ip=ROBOT_IP, |
| 57 | + control_mode=ControlMode.CARTESIAN_TQuart, |
| 58 | + robot_cfg=default_fr3_hw_robot_cfg(), |
| 59 | + collision_guard="lab", |
| 60 | + gripper_cfg=default_fr3_hw_gripper_cfg(), |
| 61 | + max_relative_movement=0.5, |
| 62 | + relative_to=RelativeTo.LAST_STEP, |
| 63 | + ) |
| 64 | + else: |
| 65 | + env_rel = fr3_sim_env( |
| 66 | + control_mode=ControlMode.CARTESIAN_TQuart, |
| 67 | + robot_cfg=default_fr3_sim_robot_cfg(), |
| 68 | + collision_guard=False, |
| 69 | + gripper_cfg=None, |
| 70 | + camera_set_cfg=default_mujoco_cameraset_cfg(), |
| 71 | + max_relative_movement=0.5, |
| 72 | + relative_to=RelativeTo.LAST_STEP, |
| 73 | + hand_cfg={"Binary": binary_action}, |
| 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 | + close_action: Union[int, list[float]] |
| 80 | + open_action: Union[int, list[float]] |
| 81 | + if binary_action: |
| 82 | + close_action = 0 |
| 83 | + open_action = 1 |
| 84 | + else: |
| 85 | + template = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0] |
| 86 | + value = 0.5 |
| 87 | + close_action = [value * val for val in template] |
| 88 | + open_action = [0.0] * len(template) |
| 89 | + |
| 90 | + for _ in range(10): |
| 91 | + for _ in range(10): |
| 92 | + # move 1cm in x direction (forward) and close gripper |
| 93 | + act = {"tquart": [0.01, 0, 0, 0, 0, 0, 1], "hand": close_action} |
| 94 | + obs, reward, terminated, truncated, info = env_rel.step(act) |
| 95 | + if truncated or terminated: |
| 96 | + logger.info("Truncated or terminated!") |
| 97 | + return |
| 98 | + from time import sleep |
| 99 | + |
| 100 | + sleep(5) |
| 101 | + for _ in range(10): |
| 102 | + # move 1cm in negative x direction (backward) and open gripper |
| 103 | + act = {"tquart": [-0.01, 0, 0, 0, 0, 0, 1], "hand": open_action} |
| 104 | + obs, reward, terminated, truncated, info = env_rel.step(act) |
| 105 | + if truncated or terminated: |
| 106 | + logger.info("Truncated or terminated!") |
| 107 | + return |
| 108 | + |
| 109 | + |
| 110 | +if __name__ == "__main__": |
| 111 | + main() |
0 commit comments