| 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 |
|
|
|
|
| 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 = ["switch_E_body_28"] |
| JOINT_NAMES = ["RevoluteJoint_switch"] |
| LIGHT_PRIM_NAME = "kitchen_all_light" |
| BUTTON_PRIM_NAME = "switch_E_button_09" |
|
|
| SWITCH_ON_THRESHOLD = math.radians(11) |
| SWITCH_ON_POSITION = math.radians(13) |
| SWITCH_OFF_POSITION = math.radians(0) |
|
|
|
|
| class RoomLightControl(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.joint_prim = XFormPrim(find_prim_path_by_name(BUTTON_PRIM_NAME)) |
| print("joint_prim", self.joint_prim.prim_path) |
| self.local_pose_button, self.local_ort_button_down = self.joint_prim.get_local_pose() |
|
|
| def on_play(self): |
| self._initialize_articulation() |
|
|
| def on_stop(self): |
| for light in self.light_scopes: |
| light.set_visibility(False) |
| self.joint_prim.set_local_pose(translation=self.local_pose_button) |
|
|
| 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.light_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 |
|
|
| |
| 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: |
| print("Warning: No valid joint indices found") |
| return |
|
|
| |
| 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 light_control(self): |
| try: |
| positions = self.art.get_joint_positions() |
| |
| |
| if isinstance(positions, torch.Tensor): |
| if positions.ndim == 0: |
| |
| joint_state = self._get_scalar_value(positions) |
| else: |
| |
| joint_state = self._get_scalar_value(positions[self.joint_indices[0]]) |
| elif isinstance(positions, np.ndarray): |
| if positions.ndim == 0: |
| |
| joint_state = self._get_scalar_value(positions) |
| else: |
| |
| joint_state = self._get_scalar_value(positions[self.joint_indices[0]]) |
| else: |
| |
| joint_state = self._get_scalar_value(positions[self.joint_indices[0]]) |
| current_visibility = self.light_scopes[0].get_visibility() |
|
|
| if joint_state < SWITCH_ON_THRESHOLD: |
| |
| if not current_visibility: |
| |
| for light in self.light_scopes: |
| light.set_visibility(True) |
| |
| target = SWITCH_OFF_POSITION |
| else: |
| target = None |
| else: |
| |
| if current_visibility: |
| |
| for light in self.light_scopes: |
| light.set_visibility(False) |
| |
| target = SWITCH_ON_POSITION |
| else: |
| target = None |
|
|
| if target is not None: |
| if self.use_torch: |
| |
| if isinstance(positions, torch.Tensor): |
| device = positions.device |
| else: |
| |
| device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') |
| targets_tensor = torch.tensor([target], device=device) |
| indices_tensor = torch.tensor([self.joint_indices[0]], device=device, dtype=torch.long) |
| action = ArticulationActions(joint_positions=targets_tensor, joint_indices=indices_tensor) |
| else: |
| action = ArticulationActions([target], joint_indices=[self.joint_indices[0]]) |
| self.art.apply_action(action) |
|
|
| except Exception as e: |
| carb.log_error(f"Error in light control: {str(e)}") |
| |
| for light in self.light_scopes: |
| light.set_visibility(False) |
|
|