x-humanoid-robomind's picture
Update ArtVIP-v1.0
d579cc3
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 PhysxSchema, UsdGeom, UsdPhysics
import math
from omni.isaac.core import SimulationContext, World
# from isaacsim.core.prims import XFormPrim
# stiffness :0.8 Damping: 0.05
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
# # 1. prim init
# button_prim = XFormPrim("/sm_warehouse_a01_h10m_cornerout_01/micro_wave_oven_2/Xform")
# door_prim = XFormPrim("/sm_warehouse_a01_h10m_cornerout_01/micro_wave_oven_2/oven/oven_door")
# # 2. articulation Root prim
# root_articulation_prim = "/sm_warehouse_a01_h10m_cornerout_01/micro_wave_oven_2/oven/oven_base"
# # 3. joint name
# joint_names = ["PrismaticJoint", "RevoluteJoint"]
# # 4. joint num
# num_arm_dof = 2
# light_scopes=[XFormPrim("/sm_warehouse_a01_h10m_cornerout_01/micro_wave_oven_2/Lights_on"),
# XFormPrim("/sm_warehouse_a01_h10m_cornerout_01/micro_wave_oven_2/Lights_on_1"),
# ]
# for i in range(0, light_scopes.__len__()):
# light_scopes[i].set_visibility(False)
# index = random.randint(0, len(light_scopes)-1)
# light_scopes[index].set_visibility(True)
button_prim = XFormPrim(find_prim_path_by_name("oven_E_button_5"))
print("button_prim", button_prim.prim_path)
# door_prim = XFormPrim(find_prim_path_by_name("oven_E_door_4"))
# print("door_prim", door_prim.prim_path)
root_articulation_prim = find_prim_path_by_name("oven_E_body_7")
print("root_articulation_prim", root_articulation_prim)
joint_names = ["PrismaticJoint_oven_down", "RevoluteJoint_oven_up"]
light_scopes=[XFormPrim(find_prim_path_by_name("oven_light"))
]
revolutejoint_path = find_prim_path_by_name("RevoluteJoint_oven_up")
class MicrowaveOverControl(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.Stiffness_max = 0.8
self.Stiffness_min = 0.08
# 初始化门的锁定状态
self.door_locked = True
# init_pose
self.local_pose_button, self.local_ort_drawer_down = button_prim.get_local_pose()
# self.local_pose_door, self.local_ort_drawer_up = door_prim.get_local_pose()
# Find Root
self.art = self.dc.get_articulation(root_articulation_prim)
if self.art == _dynamic_control.INVALID_HANDLE:
print('the prim is not an articulation')
stage = omni.usd.get_context().get_stage()
self.revoluteJoint_drive_path = stage.GetPrimAtPath(revolutejoint_path)
self.revoluteJoint_drive = UsdPhysics.DriveAPI.Get(self.revoluteJoint_drive_path, "angular")
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)
# back to initial pose
# button_prim.set_local_pose(translation=self.local_pose_button)
# door_prim.set_local_pose(translation=self.local_pose_door)
self.door_locked = True
carb.log_info(f"{type(self).__name__}.on_stop()->{self.prim_path}")
pass
def on_update(self, current_time: float, delta_time: float):
if delta_time <= 0:
return
self.dc = _dynamic_control.acquire_dynamic_control_interface()
self.active_art()
self.apply_behavior()
def active_art(self):
# Find Root
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)
# Find joint
self.joint_handles = []
for joint_name in joint_names:
self.joint_handles.append( self.dc.find_articulation_dof(self.art, joint_name))
# print("self.joint_handles",self.joint_handles)
def apply_behavior(self):
self.oven_control()
# def oven_control(self):
# self.Joint0_state = self.dc.get_dof_state(self.joint_handles[0], _dynamic_control.STATE_ALL).pos
# self.Joint1_state = self.dc.get_dof_state(self.joint_handles[1], _dynamic_control.STATE_ALL).pos
# # print("self.Joint0_state", self.Joint0_state)
# # print("self.Joint1_state", self.Joint1_state)
# # 按钮被按下且门处于锁定状态
# if self.Joint0_state < -0.001 and self.door_locked:
# self.dc.set_dof_position_target(self.joint_handles[1], math.radians(-90))
# # if self.Joint1_state > math.radians(100):
# # self.door_locked = False # 解除锁定
# # 门开到30度后开灯,按钮归位s
# if self.Joint1_state < math.radians(-30) :
# self.door_locked = False # 解除锁定
# light_scopes[0].set_visibility(True)
# self.dc.set_dof_position_target(self.joint_handles[0], 0)
# # 检测手动关门
# if self.Joint1_state > math.radians(-20) and not self.door_locked: # 当门接近关闭位置时
# self.dc.set_dof_position_target(self.joint_handles[1], 0) # 设置目标位置为0
# self.door_locked = True
# if self.Joint1_state < math.radians(5): # 当门完全关闭时
# # 锁定门
# self.door_locked = True
# light_scopes[0].set_visibility(False) # 关闭灯
def oven_control(self):
self.Joint0_state = self.dc.get_dof_state(self.joint_handles[0], _dynamic_control.STATE_ALL).pos
self.Joint1_state = self.dc.get_dof_state(self.joint_handles[1], _dynamic_control.STATE_ALL).pos
# print("self.Joint0_state", self.Joint0_state)
# print("self.Joint1_state", self.Joint1_state)
# 按钮被按下且门处于锁定状态
if self.Joint0_state < -0.001 and self.door_locked:
self.dc.set_dof_position_target(self.joint_handles[1], math.radians(-50))
# if self.Joint1_state > math.radians(100):
# self.door_locked = False # 解除锁定
# 门开到30度后开灯,按钮归位s
if self.Joint1_state < math.radians(-30) :
self.door_locked = False # 解除锁定
light_scopes[0].set_visibility(True)
self.dc.set_dof_position_target(self.joint_handles[0], 0)
if abs(self.Joint1_state - math.radians(-50)) < math.radians(0.5) :
self.dc.set_dof_position_target(self.joint_handles[1], math.radians(-78))
self.revoluteJoint_drive.GetStiffnessAttr().Set(self.Stiffness_min)
# 检测手动关门
if self.Joint1_state > math.radians(-20) and not self.door_locked: # 当门接近关闭位置时
self.dc.set_dof_position_target(self.joint_handles[1], 0) # 设置目标位置为0
self.door_locked = True
if self.Joint1_state < math.radians(5): # 当门完全关闭时
# 锁定门
self.door_locked = True
light_scopes[0].set_visibility(False) # 关闭灯
self.revoluteJoint_drive.GetStiffnessAttr().Set(self.Stiffness_max)