|
|
import carb |
|
|
from omni.kit.scripting import BehaviorScript |
|
|
from omni.isaac.dynamic_control import _dynamic_control |
|
|
from enum import Enum |
|
|
from pxr import Gf |
|
|
from threading import Thread |
|
|
import marshal,json |
|
|
import omni.kit.commands |
|
|
from omni.isaac.core.prims import XFormPrim |
|
|
import numpy as np |
|
|
from pxr import Gf, Sdf, Usd, UsdGeom |
|
|
from omni.isaac.core import SimulationContext, World |
|
|
import math |
|
|
from omni.replicator.core import utils |
|
|
from pxr import PhysxSchema, UsdGeom, UsdPhysics |
|
|
import math |
|
|
|
|
|
|
|
|
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(prim): |
|
|
if prim.GetName() == prim_name: |
|
|
return prim.GetPath().pathString |
|
|
for child in prim.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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
root_articulation_prim = find_prim_path_by_name("fridge_E_body_61") |
|
|
stage = omni.usd.get_context().get_stage() |
|
|
|
|
|
joint_names = ["RevoluteJoint_fridge_left","RevoluteJoint_fridge_right"] |
|
|
joint_threshold = 0.4 |
|
|
|
|
|
joints_info = {} |
|
|
for joint_name in joint_names: |
|
|
joint_prim_path = find_prim_path_by_name(joint_name) |
|
|
joint_prim_obj = stage.GetPrimAtPath(joint_prim_path) |
|
|
joint = UsdPhysics.RevoluteJoint(joint_prim_obj) |
|
|
|
|
|
joints_info[joint_name] = { |
|
|
'prim': joint, |
|
|
'lower_limit': joint.GetLowerLimitAttr().Get(), |
|
|
'upper_limit': joint.GetUpperLimitAttr().Get() |
|
|
} |
|
|
|
|
|
|
|
|
joint_limits = {} |
|
|
joint_limit_diffs = {} |
|
|
|
|
|
for i, joint_name in enumerate(joint_names): |
|
|
joint_limits[joint_name] = { |
|
|
'lower': joints_info[joint_name]['lower_limit'], |
|
|
'upper': joints_info[joint_name]['upper_limit'] |
|
|
} |
|
|
joint_limit_diffs[joint_name] = ( |
|
|
joints_info[joint_name]['upper_limit'] - joints_info[joint_name]['lower_limit'] |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
light_scopes=[ |
|
|
XFormPrim(find_prim_path_by_name("fridge_light")) |
|
|
] |
|
|
|
|
|
class CupboardControl(BehaviorScript): |
|
|
def on_init(self): |
|
|
self.phsx_freq = 120 |
|
|
self.joint_handles = [] |
|
|
self.dc = _dynamic_control.acquire_dynamic_control_interface() |
|
|
light_scopes[0].set_visibility(False) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.art = self.dc.get_articulation(root_articulation_prim) |
|
|
if self.art == _dynamic_control.INVALID_HANDLE: |
|
|
print('the prim is not an articulation') |
|
|
|
|
|
|
|
|
def on_destroy(self): |
|
|
pass |
|
|
|
|
|
def on_play(self): |
|
|
|
|
|
self.simulation_context = SimulationContext( |
|
|
stage_units_in_meters=1.0, |
|
|
physics_dt=1.0/120, |
|
|
rendering_dt=1.0/120 |
|
|
) |
|
|
|
|
|
def on_pause(self): |
|
|
pass |
|
|
|
|
|
def on_stop(self): |
|
|
self.init_start=True |
|
|
|
|
|
|
|
|
light_scopes[0].set_visibility(False) |
|
|
|
|
|
|
|
|
|
|
|
pass |
|
|
|
|
|
def on_update(self, current_time: float, delta_time: float): |
|
|
if delta_time <= 0: |
|
|
return |
|
|
self.active_art() |
|
|
self.apply_behavior() |
|
|
|
|
|
def active_art(self): |
|
|
|
|
|
self.art = self.dc.get_articulation(root_articulation_prim) |
|
|
if self.art == _dynamic_control.INVALID_HANDLE: |
|
|
print('the prim is not an articulation') |
|
|
self.dc.wake_up_articulation(self.art) |
|
|
|
|
|
|
|
|
self.joint_handles = [] |
|
|
for joint_name in joint_names: |
|
|
self.joint_handles.append( self.dc.find_articulation_dof(self.art, joint_name)) |
|
|
|
|
|
def apply_behavior(self): |
|
|
self.light_control() |
|
|
self.damping_stiffness_change() |
|
|
self.target_pose_control() |
|
|
|
|
|
|
|
|
def light_control(self): |
|
|
self.left_joint_state = self.dc.get_dof_state(self.joint_handles[0], _dynamic_control.STATE_ALL).pos |
|
|
self.right_joint_state = self.dc.get_dof_state(self.joint_handles[1], _dynamic_control.STATE_ALL).pos |
|
|
|
|
|
|
|
|
doors_open = [ |
|
|
self.left_joint_state > math.radians(30), |
|
|
self.right_joint_state > math.radians(30) |
|
|
] |
|
|
|
|
|
|
|
|
current_visibility = light_scopes[0].get_visibility() |
|
|
if any(doors_open): |
|
|
if not current_visibility: |
|
|
for light in light_scopes: |
|
|
light.set_visibility(True) |
|
|
else: |
|
|
if current_visibility: |
|
|
for light in light_scopes: |
|
|
light.set_visibility(False) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def damping_stiffness_change(self): |
|
|
|
|
|
for i, joint_name in enumerate(joint_names): |
|
|
|
|
|
joint_state = self.dc.get_dof_state(self.joint_handles[i], _dynamic_control.STATE_ALL).pos |
|
|
|
|
|
joint_drive_path = stage.GetPrimAtPath(find_prim_path_by_name(joint_name)) |
|
|
joint_drive = UsdPhysics.DriveAPI.Get(joint_drive_path, "angular") |
|
|
|
|
|
|
|
|
joint_limit_diff = joint_limit_diffs[joint_names[i]] |
|
|
joint_drive.GetStiffnessAttr().Set(self.calculate_stiffness(joint_state)) |
|
|
|
|
|
|
|
|
|
|
|
def calculate_stiffness(self,joint_state): |
|
|
"""Calculate stiffness value based on joint angle |
|
|
|
|
|
Args: |
|
|
joint_state (float): Joint angle (radians) |
|
|
joint_limit (float): Joint limit value |
|
|
|
|
|
Returns: |
|
|
float: Stiffness value |
|
|
""" |
|
|
max_stiffness = 20 |
|
|
|
|
|
min_stiffness = 2 |
|
|
decay_rate = 3 |
|
|
|
|
|
if abs(joint_state) >= abs(math.radians(joint_threshold * (joints_info[joint_names[i]]['upper_limit']+joints_info[joint_names[i]]['lower_limit']))): |
|
|
return min_stiffness |
|
|
|
|
|
|
|
|
stiffness = max_stiffness * math.exp(-decay_rate * abs(joint_state)) |
|
|
|
|
|
|
|
|
return max(min_stiffness, min(stiffness, max_stiffness)) |
|
|
|
|
|
def target_pose_control(self): |
|
|
for i, joint_handle in enumerate(self.joint_handles): |
|
|
|
|
|
joint_state = self.dc.get_dof_state(joint_handle, _dynamic_control.STATE_ALL).pos |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if abs(joints_info[joint_names[i]]['lower_limit']) <= math.radians(abs(joints_info[joint_names[i]]['upper_limit'])): |
|
|
if joint_state > math.radians(joint_threshold * (joints_info[joint_names[i]]['upper_limit']+joints_info[joint_names[i]]['lower_limit'])): |
|
|
|
|
|
self.dc.set_dof_position_target(joint_handle, math.radians(joints_info[joint_names[i]]['upper_limit'])) |
|
|
else: |
|
|
self.dc.set_dof_position_target(joint_handle, math.radians(joints_info[joint_names[i]]['lower_limit'])) |
|
|
else: |
|
|
if joint_state < math.radians(joint_threshold * (joints_info[joint_names[i]]['upper_limit']+joints_info[joint_names[i]]['lower_limit'])): |
|
|
self.dc.set_dof_position_target(joint_handle, math.radians(joints_info[joint_names[i]]['lower_limit'])) |
|
|
else: |
|
|
self.dc.set_dof_position_target(joint_handle, math.radians(joints_info[joint_names[i]]['upper_limit'])) |
|
|
|
|
|
|