Download Interactive_scene/kitchen/interactive_objects/script/oven_control.py from X-Humanoid/ArtVIP: direct link, hf CLI and curl.
- Browser
- Download file 7.8 kB
-
https://huggingface.co/datasets/X-Humanoid/ArtVIP/resolve/refs%2Fpr%2F4/Interactive_scene/kitchen/interactive_objects/script/oven_control.py
- Command line
-
hf download hf://datasets/X-Humanoid/ArtVIP@refs/pr/4/Interactive_scene/kitchen/interactive_objects/script/oven_control.py
-
curl -L -o oven_control.py https://huggingface.co/datasets/X-Humanoid/ArtVIP/resolve/refs%2Fpr%2F4/Interactive_scene/kitchen/interactive_objects/script/oven_control.py
7.8 kB
| import carb | |
| import math | |
| import torch | |
| import numpy as np | |
| import omni.usd | |
| from omni.kit.scripting import BehaviorScript | |
| from isaacsim.core.prims import Articulation | |
| from omni.isaac.core.prims import XFormPrim | |
| from isaacsim.core.utils.types import ArticulationActions | |
| from pxr import UsdPhysics | |
| def find_prim_path_by_name(prim_name: str, root_path: str = "/") -> str: | |
| """Recursively find the full path by Prim name""" | |
| stage = omni.usd.get_context().get_stage() | |
| prim = stage.GetPrimAtPath(root_path) | |
| def _find_prim(current): | |
| if current.GetName() == prim_name: | |
| return current.GetPath().pathString | |
| for child in current.GetChildren(): | |
| result = _find_prim(child) | |
| if result: | |
| return result | |
| return None | |
| found_path = _find_prim(prim) | |
| if not found_path: | |
| raise RuntimeError(f"Prim '{prim_name}' not found under {root_path}") | |
| return found_path | |
| POSSIBLE_ASSET_ROOT_NAMES = ["oven_E_body_7"] | |
| JOINT_NAMES = ["PrismaticJoint_oven_down", "RevoluteJoint_oven_up"] | |
| LIGHT_PRIM_NAME = "oven_light" | |
| BUTTON_PRIM_NAME = "oven_E_button_5" | |
| REVOLUTE_JOINT_NAME = "RevoluteJoint_oven_up" | |
| STIFFNESS_MAX = 0.8 | |
| STIFFNESS_MIN = 0.08 | |
| BUTTON_PRESS_THRESHOLD = -0.001 | |
| DOOR_OPEN_ANGLE_1 = math.radians(-50) | |
| DOOR_OPEN_ANGLE_2 = math.radians(-78) | |
| DOOR_OPEN_THRESHOLD = math.radians(-30) | |
| DOOR_CLOSE_THRESHOLD = math.radians(-20) | |
| DOOR_CLOSED_THRESHOLD = math.radians(5) | |
| DOOR_ANGLE_TOLERANCE = math.radians(0.5) | |
| class MicrowaveOverControl(BehaviorScript): | |
| def on_init(self): | |
| articulation_root_path = None | |
| for root_name in POSSIBLE_ASSET_ROOT_NAMES: | |
| try: | |
| articulation_root_path = find_prim_path_by_name(root_name) | |
| print(f"Found articulation root: {articulation_root_path}") | |
| break | |
| except RuntimeError: | |
| continue | |
| if articulation_root_path is None: | |
| raise RuntimeError(f"Could not find articulation root with names: {POSSIBLE_ASSET_ROOT_NAMES}") | |
| self.articulation_root_path = articulation_root_path | |
| self.art = None | |
| self.initialized = False | |
| self.joint_names = JOINT_NAMES | |
| self.joint_indices = None | |
| self.use_torch = None | |
| self.light_scopes = [XFormPrim(find_prim_path_by_name(LIGHT_PRIM_NAME))] | |
| self.light_scopes[0].set_visibility(False) | |
| self.Stiffness_max = STIFFNESS_MAX | |
| self.Stiffness_min = STIFFNESS_MIN | |
| self.door_locked = True | |
| self.button_prim = XFormPrim(find_prim_path_by_name(BUTTON_PRIM_NAME)) | |
| print("button_prim", self.button_prim.prim_path) | |
| self.local_pose_button, self.local_ort_drawer_down = self.button_prim.get_local_pose() | |
| stage = omni.usd.get_context().get_stage() | |
| revolutejoint_path = find_prim_path_by_name(REVOLUTE_JOINT_NAME) | |
| self.revoluteJoint_drive_path = stage.GetPrimAtPath(revolutejoint_path) | |
| self.revoluteJoint_drive = UsdPhysics.DriveAPI.Get(self.revoluteJoint_drive_path, "angular") | |
| def on_play(self): | |
| self._initialize_articulation() | |
| def on_stop(self): | |
| self.light_scopes[0].set_visibility(False) | |
| self.door_locked = True | |
| if self.revoluteJoint_drive: | |
| self.revoluteJoint_drive.GetStiffnessAttr().Set(self.Stiffness_max) | |
| carb.log_info(f"{type(self).__name__}.on_stop()->{self.prim_path}") | |
| def on_update(self, current_time: float, delta_time: float): | |
| if delta_time <= 0: | |
| return | |
| if not self.initialized: | |
| self._initialize_articulation() | |
| if not self.initialized: | |
| return | |
| self.apply_behavior() | |
| def apply_behavior(self): | |
| if not self.initialized or self.art is None: | |
| return | |
| self.oven_control() | |
| def _initialize_articulation(self): | |
| """Initialize the articulation object and get joint information""" | |
| if self.initialized: | |
| return | |
| try: | |
| self.art = Articulation(self.articulation_root_path) | |
| if self.art is None: | |
| return | |
| # Get joint indices | |
| self.joint_indices = [] | |
| for joint_name in self.joint_names: | |
| idx = self.art.get_dof_index(joint_name) | |
| if idx is not None: | |
| self.joint_indices.append(idx) | |
| else: | |
| print(f"Warning: Could not find joint index for {joint_name}") | |
| if not self.joint_indices or len(self.joint_indices) < 2: | |
| print("Warning: No valid joint indices found") | |
| return | |
| # Detect if using torch | |
| positions = self.art.get_joint_positions() | |
| self.use_torch = isinstance(positions, torch.Tensor) | |
| print(f"Detected platform: {'Isaac Lab (torch)' if self.use_torch else 'Isaac Sim (numpy/math)'}") | |
| self.initialized = True | |
| except Exception as e: | |
| print(f"Error initializing articulation: {e}") | |
| self.initialized = False | |
| def _get_scalar_value(self, value): | |
| """Convert a value (potentially a torch.Tensor) to a scalar float""" | |
| if isinstance(value, torch.Tensor): | |
| return value.item() | |
| elif isinstance(value, (list, tuple, np.ndarray)): | |
| return float(value[0] if len(value) > 0 else 0.0) | |
| else: | |
| return float(value) | |
| def oven_control(self): | |
| positions = self.art.get_joint_positions().squeeze() | |
| button_state = self._get_scalar_value(positions[self.joint_indices[0]]) | |
| door_state = self._get_scalar_value(positions[self.joint_indices[1]]) | |
| targets = [] | |
| indices = [] | |
| # 按钮被按下且门处于锁定状态 | |
| if button_state < BUTTON_PRESS_THRESHOLD and self.door_locked: | |
| targets.append(DOOR_OPEN_ANGLE_1) | |
| indices.append(self.joint_indices[1]) | |
| # 门开到30度后开灯,按钮归位 | |
| if door_state < DOOR_OPEN_THRESHOLD: | |
| self.door_locked = False | |
| self.light_scopes[0].set_visibility(True) | |
| targets.append(0.0) | |
| indices.append(self.joint_indices[0]) | |
| else: | |
| # Keep button target | |
| targets.append(button_state) | |
| indices.append(self.joint_indices[0]) | |
| # 门到达第一个目标角度后,继续开到第二个角度 | |
| if abs(door_state - DOOR_OPEN_ANGLE_1) < DOOR_ANGLE_TOLERANCE: | |
| targets.append(DOOR_OPEN_ANGLE_2) | |
| indices.append(self.joint_indices[1]) | |
| if self.revoluteJoint_drive: | |
| self.revoluteJoint_drive.GetStiffnessAttr().Set(self.Stiffness_min) | |
| # 检测手动关门 | |
| if door_state > DOOR_CLOSE_THRESHOLD and not self.door_locked: | |
| targets.append(0.0) | |
| indices.append(self.joint_indices[1]) | |
| self.door_locked = True | |
| if door_state < DOOR_CLOSED_THRESHOLD: | |
| # 锁定门 | |
| self.door_locked = True | |
| self.light_scopes[0].set_visibility(False) | |
| if self.revoluteJoint_drive: | |
| self.revoluteJoint_drive.GetStiffnessAttr().Set(self.Stiffness_max) | |
| if targets: | |
| if self.use_torch: | |
| device = positions.device | |
| targets_tensor = torch.tensor(targets, device=device) | |
| indices_tensor = torch.tensor(indices, device=device, dtype=torch.long) | |
| action = ArticulationActions(joint_positions=targets_tensor, joint_indices=indices_tensor) | |
| else: | |
| action = ArticulationActions(targets, joint_indices=indices) | |
| self.art.apply_action(action) | |