Download train_fast/source/env.py from robochad/shadow-hand-structured-v3: direct link, hf CLI and curl.
- Browser
- Download file 5.85 kB
-
https://huggingface.co/datasets/robochad/shadow-hand-structured-v3/resolve/main/train_fast/source/env.py
- Command line
-
hf download hf://datasets/robochad/shadow-hand-structured-v3/train_fast/source/env.py
-
curl -L -o env.py https://huggingface.co/datasets/robochad/shadow-hand-structured-v3/resolve/main/train_fast/source/env.py
5.85 kB
| """Isaac Lab 1.2 environment. Import only after AppLauncher starts.""" | |
| from __future__ import annotations | |
| import numpy as np | |
| import torch | |
| from omni.isaac.lab.sensors import ContactSensor, ContactSensorCfg | |
| from omni.isaac.lab_tasks.direct.inhand_manipulation import InHandManipulationEnv | |
| from omni.isaac.lab_tasks.direct.shadow_hand.shadow_hand_env_cfg import ShadowHandEnvCfg | |
| FINGERS = ("FF", "MF", "RF", "LF") | |
| class ResearchShadowEnv(InHandManipulationEnv): | |
| """No automatic resets/goals; collection controls episode boundaries explicitly. | |
| Actions supplied to advance are physical joint-position targets in runtime | |
| actuator order. Distal joints are never commanded. The original gym task | |
| remains registered and unchanged. | |
| """ | |
| def _setup_scene(self): | |
| super()._setup_scene() | |
| for finger in (*FINGERS, "TH"): | |
| key = finger.lower() | |
| self.scene.sensors[key] = ContactSensor(ContactSensorCfg( | |
| prim_path=f"/World/envs/env_.*/Robot/robot0_{key}distal", | |
| update_period=0.0, | |
| filter_prim_paths_expr=["/World/envs/env_.*/object"], | |
| )) | |
| def _get_dones(self): | |
| zeros = torch.zeros(self.num_envs, dtype=torch.bool, device=self.device) | |
| return zeros, zeros.clone() | |
| def _get_rewards(self): | |
| return torch.zeros(self.num_envs, device=self.device) | |
| def _get_observations(self): | |
| return {"policy": torch.cat((self.hand.data.joint_pos, self.hand.data.joint_vel), -1)} | |
| def configure_research(self, mode="free"): | |
| self.mode = mode | |
| self.goal_markers.set_visibility(False) | |
| self.active = list(self.actuated_dof_indices) | |
| self.passive = [self.hand.joint_names.index(f"robot0_{f}J0") for f in FINGERS] | |
| self.middle = [self.hand.joint_names.index(f"robot0_{f}J1") for f in FINGERS] | |
| assert self.hand.num_joints == 24 and len(self.active) == 20 | |
| assert not set(self.active).intersection(self.passive) | |
| self.action_names = [self.hand.joint_names[i] for i in self.active] | |
| self.low = self.hand_dof_lower_limits[0, self.active].clone() | |
| self.high = self.hand_dof_upper_limits[0, self.active].clone() | |
| self.sim_time = 0.0 | |
| self.last_target = torch.zeros((self.num_envs, 20), device=self.device) | |
| self.reset_physical() | |
| def reset_physical(self, q=None): | |
| self.reset() | |
| q = self.hand.data.default_joint_pos.clone() if q is None else q.clone() | |
| if q.ndim == 1: | |
| q = q.repeat(self.num_envs, 1) | |
| self.hand.write_joint_state_to_sim(q, torch.zeros_like(q)) | |
| self.last_target = q[:, self.active].clone() | |
| self.hand.set_joint_position_target(self.last_target, joint_ids=self.active) | |
| obj = self.object.data.default_root_state.clone() | |
| obj[:, :3] += self.scene.env_origins | |
| if self.mode == "free": | |
| obj[:, 0] += 0.45 | |
| self.object.write_root_state_to_sim(obj) | |
| self.scene.reset() | |
| self.scene.write_data_to_sim() | |
| self.sim_time = 0.0 | |
| def snapshot(self): | |
| """CPU arrays for all environments; time is the completed physics boundary.""" | |
| def cpu(x): | |
| return x.detach().cpu().numpy().copy() | |
| net = torch.stack([self.scene[f.lower()].data.net_forces_w[:, 0] for f in (*FINGERS, "TH")], 1) | |
| obj_force = torch.stack([ | |
| self.scene[f.lower()].data.force_matrix_w[:, 0, 0] for f in (*FINGERS, "TH") | |
| ], 1) | |
| return { | |
| "joint_pos": cpu(self.hand.data.joint_pos), | |
| "joint_vel": cpu(self.hand.data.joint_vel), | |
| "object_state": cpu(self.object.data.root_state_w), | |
| "hand_root_state": cpu(self.hand.data.root_state_w), | |
| "fingertip_state": cpu(self.hand.data.body_state_w[:, self.finger_bodies]), | |
| "fingertip_net_force": cpu(net), | |
| "fingertip_object_force": cpu(obj_force), | |
| "time": np.full(self.num_envs, self.sim_time, dtype=np.float64), | |
| } | |
| def advance(self, target, record_physics=False): | |
| if target.ndim == 1: | |
| target = target.repeat(self.num_envs, 1) | |
| target = torch.maximum(torch.minimum(target, self.high), self.low) | |
| assert torch.isfinite(target).all() | |
| self.last_target = target.clone() | |
| self.hand.set_joint_position_target(target, joint_ids=self.active) | |
| records = [] | |
| for _ in range(self.cfg.decimation): | |
| self.scene.write_data_to_sim() | |
| self.sim.step(render=False) | |
| self.scene.update(self.physics_dt) | |
| self.sim_time += self.physics_dt | |
| if record_physics: | |
| records.append(self.snapshot()) | |
| return self.snapshot(), records | |
| def make_cfg(args): | |
| cfg = ShadowHandEnvCfg() | |
| cfg.scene.num_envs = args.num_envs | |
| cfg.num_observations = 48 | |
| cfg.sim.device = args.device | |
| cfg.seed = args.seed | |
| cfg.sim.dt = 1.0 / args.physics_hz | |
| control_hz = getattr(args, "control_hz", 60) | |
| if args.physics_hz % control_hz: | |
| raise ValueError("Physics rate must be an integer multiple of the control rate") | |
| cfg.decimation = args.physics_hz // control_hz | |
| cfg.sim.render_interval = cfg.decimation | |
| cfg.episode_length_s = 100000 | |
| cfg.reset_position_noise = 0.0 | |
| cfg.reset_dof_pos_noise = 0.0 | |
| cfg.reset_dof_vel_noise = 0.0 | |
| cfg.robot_cfg.spawn.activate_contact_sensors = True | |
| cfg.robot_cfg.spawn.rigid_props.disable_gravity = True # baseline asset; manifest records this | |
| if getattr(args, "mode", "free") == "fixture": | |
| cfg.object_cfg.spawn.rigid_props.kinematic_enabled = True | |
| cfg.object_cfg.init_state.pos = (0.0, -0.425, 0.54) | |
| cfg.viewer.eye = (0.55, -0.8, 0.9) | |
| cfg.viewer.lookat = (0.0, -0.32, 0.6) | |
| cfg.viewer.resolution = (800, 640) | |
| return cfg | |