ArtVIP / Interactive_scene /kitchen /script /fridge_control_for_0.py
x-humanoid-robomind's picture
Delete scenes
b1f397d
raw
history blame
7.97 kB
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
def calculate_stiffness(joint_state):
"""
Args:
joint_state (float): Joint angle in radians
Returns:
float: Stiffness value
"""
# Parameters configuration
max_stiffness = 20 # Maximum stiffness
min_stiffness = 1 # Minimum stiffness
decay_rate = 2 # Decay rate
threshold = math.radians(30) # Threshold angle (radians)
# Use exponential decay function to calculate stiffness
if joint_state >= threshold:
return min_stiffness
# Calculate stiffness: stiffness = max_stiffness * e^(-decay_rate * angle)
stiffness = max_stiffness * math.exp(-decay_rate * joint_state)
# Clamp stiffness value between min and max
return max(min_stiffness, min(stiffness, max_stiffness))
# 1. prim init
joint_prim = XFormPrim(find_prim_path_by_name("fridge_E_body_61"))
right_revolutejoint_path = find_prim_path_by_name("RevoluteJoint_fridge_right")
left_revolutejoint_path = find_prim_path_by_name("RevoluteJoint_fridge_left")
# 2. articulation Root prim
root_articulation_prim = find_prim_path_by_name("fridge_E_body_61")
# 3. joint name
joint_names = ["RevoluteJoint_fridge_left","RevoluteJoint_fridge_right","PrismaticJoint_fridge_left", "PrismaticJoint_fridge_right" ]
# 5. light prim
light_scopes=[
XFormPrim(find_prim_path_by_name("fridge_light"))
]
class MagneticFridgeControl(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)
# light_scopes[1].set_visibility(False)
# init_pose
# self.local_pose_button, self.local_ort_button_down = joint_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')
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
for light in light_scopes:
light.set_visibility(False)
# light_scopes[0].set_visibility(False)
# light_scopes[1].set_visibility(False)
# back to initial pose
joint_prim.set_local_pose(translation=self.local_pose_button)
pass
def on_update(self, current_time: float, delta_time: float):
if delta_time <= 0:
return
self.active_art()
self.damping_stiffness_change()
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))
def apply_behavior(self):
self.light_control()
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
# print("self.Joint1_state",self.Joint1_state)
# ่”ๅˆๅˆคๆ–ญๅŒ้—จ็Šถๆ€
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)
self.dc.set_dof_position_target(self.joint_handles[1], 0)
def damping_stiffness_change(self):
stage = omni.usd.get_context().get_stage()
# get joint state
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
revoluteJoint_fridge_right_drive_path = stage.GetPrimAtPath(right_revolutejoint_path)
revoluteJoint_fridge_left_drive_path = stage.GetPrimAtPath(left_revolutejoint_path)
revoluteJoint_fridge_left_drive = UsdPhysics.DriveAPI.Get(revoluteJoint_fridge_left_drive_path, "angular")
revoluteJoint_fridge_right_drive = UsdPhysics.DriveAPI.Get(revoluteJoint_fridge_right_drive_path, "angular")
right_joint_stiffness = revoluteJoint_fridge_right_drive.GetStiffnessAttr().Get()
right_joint_Damping = revoluteJoint_fridge_right_drive.GetDampingAttr().Get()
# print("==========")
# print("right_joint_stiffness", right_joint_stiffness)
# print("joint_Damping", joint_Damping)
# print("self.left_joint_state", self.left_joint_state)
# print("self.right_joint_state", self.right_joint_state)
# print("==========")
# if self.left_joint_state < math.radians(30):
revoluteJoint_fridge_right_drive.GetStiffnessAttr().Set(calculate_stiffness(self.right_joint_state))
revoluteJoint_fridge_left_drive.GetStiffnessAttr().Set(calculate_stiffness(abs(self.left_joint_state)))
# if self.left_joint_state < math.radians(20):
# revoluteJoint_fridge_left_drive.GetStiffnessAttr().Set(200)
# elif math.radians(20) <= self.left_joint_state < math.radians(140):
# revoluteJoint_fridge_left_drive.GetStiffnessAttr().Set(10)
def target_pose_control(self):
if self.left_joint_state > abs(math.radians(30)):
self.dc.set_dof_position_target(self.joint_handles[0], math.radians(-90))
else:
self.dc.set_dof_position_target(self.joint_handles[0], math.radians(0))
if self.right_joint_state > abs(math.radians(30)):
self.dc.set_dof_position_target(self.joint_handles[1], math.radians(90))
else:
self.dc.set_dof_position_target(self.joint_handles[1], math.radians(0))