zgong313's picture
Add files using upload-large-folder tool
a781d1f verified
Raw
History Blame Contribute Delete
138 kB
#!/usr/bin/env python3
"""
Simple VLN Environment for SAGE-3D Benchmark.
Provides Isaac Sim based VLN environment with RGB-D rendering,
physics simulation, and collision detection.
"""
import os
import math
import json
from pathlib import Path
from typing import Tuple, List, Dict, Any, Optional
import numpy as np
from PIL import Image
import imageio
def _should_print_debug() -> bool:
"""Check if debug messages should be printed."""
return not os.environ.get('SILENT_LOGGING_MODE', False)
def _debug_print(msg: str) -> None:
"""Conditionally print debug messages."""
if _should_print_debug():
print(msg)
try:
from isaacsim.simulation_app import SimulationApp # Isaac Sim 4.1+
except Exception:
from omni.isaac.kit import SimulationApp # fallback
# Import 2D semantic map collision detector
try:
from .collision_detector import SemanticMap2DCollisionDetector
except ImportError:
try:
from collision_detector import SemanticMap2DCollisionDetector
except ImportError:
print("[WARNING] Cannot import SemanticMap2DCollisionDetector, 2D semantic map collision detection will be disabled")
SemanticMap2DCollisionDetector = None
class SimpleVLNEnv:
"""Simple VLN Environment based on Isaac Sim for SAGE-3D Benchmark."""
def __init__(
self,
scene_usd_path: str,
headless: bool = True,
hz: int = 30,
agent_prim_path: str = "/World/AgentCamera",
resolution: Tuple[int, int] = (640, 480),
map_json_path: str = None
) -> None:
"""Initialize SimpleVLNEnv.
Args:
scene_usd_path: Path to scene USD/USDA file
headless: Whether to run in headless mode (no GUI)
hz: Simulation frequency in Hz
agent_prim_path: USD path for agent prim
resolution: Camera resolution (width, height)
map_json_path: Path to 2D semantic map JSON for collision detection
"""
print("[ENV_INIT] ===== SimpleVLNEnv Initialization Start =====")
print(f"[ENV_INIT] scene_usd_path: {scene_usd_path}")
print(f"[ENV_INIT] headless: {headless}")
print(f"[ENV_INIT] hz: {hz}")
print(f"[ENV_INIT] agent_prim_path: {agent_prim_path}")
print(f"[ENV_INIT] resolution: {resolution}")
self.scene_usd_path = str(Path(scene_usd_path).resolve())
self.hz = hz
self.agent_prim_path = agent_prim_path
self.resolution = resolution
self.headless = headless
self.is_stop_called = False
self.consecutive_collisions = 0 # Consecutive collision counter
self._total_collision_count = 0 # Total collision count for current episode (CR metric)
self._debug_disable_collision = False # Enable collision detection (needed for rendering)
# Time tracking (for no-goal tasks)
import time
self._episode_start_time = time.time()
self._current_time = time.time()
self._collision_detected = False
# Log function (set in run_benchmark)
self._log_function = None
self._map_json_path = map_json_path # Save for later initialization
# Object-based success evaluation related attributes
self.semantic_map_path = map_json_path # For object-based success evaluation
self.collision_detector = None
# Start Isaac Sim (before setting log function)
self._init_isaac_sim()
def _log(self, msg: str) -> None:
"""Log message to console and file."""
print(msg, flush=True)
if self._log_function:
try:
self._log_function(msg)
except:
pass # Silently ignore log errors
def update_time_and_reset_collision(self) -> None:
"""Update current time and reset collision state (called each step)."""
import time
self._current_time = time.time()
self._collision_detected = False # Reset collision state, await new collision detection
def reset_episode_time(self) -> None:
"""Reset episode start time (called when new episode starts)."""
import time
self._episode_start_time = time.time()
self._current_time = time.time()
self._collision_detected = False
def set_log_function(self, log_func) -> None:
"""Set log function."""
self._log_function = log_func
# Immediately initialize 2D collision detector
self._init_collision_detector(self._map_json_path)
def _init_collision_detector(self, map_json_path: str) -> None:
"""Initialize 2D semantic map collision detector."""
self._log(f"[COLLISION_2D] ===== Starting 2D Collision Detector Initialization =====")
self._log(f"[COLLISION_2D] map_json_path: {map_json_path}")
self._log(f"[COLLISION_2D] SemanticMap2DCollisionDetector available: {SemanticMap2DCollisionDetector is not None}")
if map_json_path:
self._log(f"[COLLISION_2D] Map file exists: {os.path.exists(map_json_path)}")
if SemanticMap2DCollisionDetector is not None and map_json_path and os.path.exists(map_json_path):
try:
self._log(f"[COLLISION_2D] Initializing 2D semantic map collision detector...")
self.collision_detector = SemanticMap2DCollisionDetector(
map_json_path,
robot_radius_m=0.08, # Smaller robot radius for finer movement
scale=0.05 # Consistent with A* algorithm
)
self._log(f"[COLLISION_2D] ✓ Successfully loaded 2D semantic map collision detector: {map_json_path}")
collision_info = self.collision_detector.get_collision_info()
self._log(f"[COLLISION_2D] ✓ Map info: obstacle pixels {collision_info['obstacle_pixels']}/{collision_info['total_pixels']} ({collision_info['obstacle_ratio']:.1%})")
except Exception as e:
self._log(f"[COLLISION_2D] ✗ Warning: Cannot load 2D semantic map collision detector: {e}")
import traceback
traceback.print_exc()
self.collision_detector = None
else:
if SemanticMap2DCollisionDetector is None:
self._log(f"[COLLISION_2D] ✗ Warning: SemanticMap2DCollisionDetector class not available, using original collision detection")
elif not map_json_path:
self._log(f"[COLLISION_2D] ℹ Info: No 2D semantic map path provided, using original collision detection")
elif not os.path.exists(map_json_path):
self._log(f"[COLLISION_2D] ✗ Warning: 2D semantic map file does not exist: {map_json_path}")
self._log(f"[COLLISION_2D] Final state: collision_detector = {self.collision_detector is not None}")
self._log(f"[COLLISION_2D] ===== 2D Collision Detector Initialization Complete =====")
def _init_isaac_sim(self):
"""Initialize Isaac Sim environment."""
# Launch SimulationApp first before importing any omni/pxr modules
self.sim = SimulationApp({"headless": self.headless})
# Now safe to import omni/usd modules
import omni.usd # type: ignore
from pxr import UsdGeom, UsdPhysics # type: ignore
# Try new version sensor import, fallback to old version
try:
from isaacsim.sensors.camera import Camera # type: ignore
print("[CAMERA] Using isaacsim.sensors.camera")
except ImportError:
try:
from omni.isaac.sensor import Camera # type: ignore
print("[CAMERA] Using omni.isaac.sensor")
except ImportError:
print("[ERROR] Cannot import Camera class")
raise
from omni.isaac.core import World # type: ignore
from omni.isaac.core.utils.stage import open_stage # type: ignore
# Enable physics extensions
try:
import omni.isaac.core.utils.extensions as extensions
extensions.enable_extension("omni.isaac.core")
print(f"[EXTENSIONS] Enabled omni.isaac.core")
except:
try:
import isaacsim.core.utils.extensions as extensions
extensions.enable_extension("isaacsim.core")
print(f"[EXTENSIONS] Enabled isaacsim.core")
except Exception as e:
print(f"[EXTENSIONS] WARN: Failed to enable core extension: {e}")
# Try to enable physics extension
try:
import omni.kit.commands
omni.kit.commands.execute('EnableExtension', id="omni.isaac.physics")
print(f"[EXTENSIONS] Forced enable omni.isaac.physics")
except Exception as e:
print(f"[EXTENSIONS] Failed to force enable physics: {e}")
try:
extensions.enable_extension("isaacsim.physics")
print(f"[EXTENSIONS] Enabled isaacsim.physics")
except Exception as e2:
print(f"[EXTENSIONS] WARN: Failed to enable physics extensions: {e2}")
try:
extensions.enable_extension("omni.isaac.dynamic_control")
print(f"[EXTENSIONS] Enabled omni.isaac.dynamic_control")
except Exception as e:
print(f"[EXTENSIONS] WARN: Failed to enable dynamic control: {e}")
self._UsdGeom = UsdGeom
self._UsdPhysics = UsdPhysics
self._Camera = Camera
self._World = World
self._open_stage = open_stage
self._omni_usd = omni.usd
self._omni_usd.get_context().close_stage()
assert self._open_stage(usd_path=self.scene_usd_path), f"Failed to open stage: {self.scene_usd_path}"
self.stage = self._omni_usd.get_context().get_stage()
# World initialization with error handling
try:
print(f"[ISAAC_SIM] Initializing Isaac Sim World...")
self.world = self._World()
print(f"[ISAAC_SIM] Resetting World...")
self.world.reset()
print(f"[ISAAC_SIM] Running initialization steps...")
for i in range(5):
self.world.step(render=True)
print(f"[ISAAC_SIM] Isaac Sim World initialization complete")
except Exception as e:
print(f"[ISAAC_SIM] ERROR: Isaac Sim World initialization failed: {e}")
import traceback
traceback.print_exc()
raise
# Set agent properties first
self._pos = np.array([0.0, 0.0, 0.85], dtype=np.float32) # z=0.85: agent center height
self._yaw = 0.0
self.agent_radius = 0.01 # Agent collision radius in meters
# Cleanup existing camera and create new one with depth capability
self._cleanup_existing_camera()
self._create_simple_camera()
# Verify physics system
self._verify_physics_system()
# Get scene bounds
self._get_scene_bounds()
def _cleanup_existing_camera(self) -> None:
"""Cleanup any existing camera to ensure fresh creation."""
try:
if hasattr(self, 'cam'):
try:
self.cam = None
except Exception as e:
self.cam = None
# Cleanup camera prim in USD stage
if hasattr(self, 'stage') and self.stage:
camera_paths = [
self.agent_prim_path + "/Camera",
self.agent_prim_path,
"/World/Camera"
]
for cam_path in camera_paths:
try:
cam_prim = self.stage.GetPrimAtPath(cam_path)
if cam_prim and cam_prim.IsValid():
self.stage.RemovePrim(cam_prim.GetPath())
except Exception as e:
pass
except Exception as e:
pass
def _ensure_depth_annotator_runtime(self) -> None:
"""Ensure depth annotator exists at runtime."""
try:
self._log("[RUNTIME_DEPTH_FIX] Checking depth annotator...")
# Check if current frame has depth data
frame = self.cam.get_current_frame()
available_keys = list(frame.keys()) if frame else []
self._log(f"[RUNTIME_DEPTH_FIX] Current frame keys: {available_keys}")
if 'distance_to_image_plane' not in available_keys:
self._log(f"[RUNTIME_DEPTH_FIX] No depth data, attempting fix...")
# Try multiple depth annotator methods
success = False
# Method 1: add_distance_to_image_plane_to_frame
if hasattr(self.cam, 'add_distance_to_image_plane_to_frame'):
try:
self._log("[RUNTIME_DEPTH_FIX] Trying to add distance_to_image_plane annotator...")
self.cam.add_distance_to_image_plane_to_frame()
# Configure Isaac Sim renderer for depth
self._configure_isaac_sim_depth_rendering()
# Verify success immediately
self.world.step(render=True)
new_frame = self.cam.get_current_frame()
new_keys = list(new_frame.keys()) if new_frame else []
if 'distance_to_image_plane' in new_keys:
# Check if depth data is valid
test_depth = new_frame['distance_to_image_plane']
if test_depth is not None and hasattr(test_depth, 'std'):
depth_std = test_depth.std() if hasattr(test_depth, 'std') else 0
if depth_std > 0.01:
self._log("[RUNTIME_DEPTH_FIX] Successfully added valid depth annotator")
success = True
else:
self._log(f"[RUNTIME_DEPTH_FIX] Depth annotator added but no data change, std={depth_std}")
else:
self._log("[RUNTIME_DEPTH_FIX] Depth annotator added but data invalid")
else:
self._log(f"[RUNTIME_DEPTH_FIX] Still no depth data after adding, new keys: {new_keys}")
except Exception as e:
self._log(f"[RUNTIME_DEPTH_FIX] Failed to add annotator: {e}")
# Method 2: Try other annotator methods
if not success:
annotator_methods = [
'add_distance_to_camera_to_frame',
'add_linear_depth_to_frame',
'add_depth_to_frame'
]
for method_name in annotator_methods:
if hasattr(self.cam, method_name):
try:
self._log(f"[RUNTIME_DEPTH_FIX] Trying {method_name}...")
method = getattr(self.cam, method_name)
method()
# Verify
self.world.step(render=True)
test_frame = self.cam.get_current_frame()
if test_frame and any('distance' in k or 'depth' in k for k in test_frame.keys()):
self._log(f"[RUNTIME_DEPTH_FIX] Success")
success = True
break
except Exception as e:
self._log(f"[RUNTIME_DEPTH_FIX] Failed: {e}")
# Method 3: Check camera depth methods
if not success:
self._log("[RUNTIME_DEPTH_FIX] Analyzing camera object...")
depth_methods = [attr for attr in dir(self.cam) if 'depth' in attr.lower() or 'distance' in attr.lower()]
self._log(f"[RUNTIME_DEPTH_FIX] Available depth methods: {depth_methods}")
annotator_methods = [attr for attr in dir(self.cam) if 'add' in attr.lower() and ('annotator' in attr.lower() or 'frame' in attr.lower())]
self._log(f"[RUNTIME_DEPTH_FIX] Available annotator methods: {annotator_methods}")
if not success:
self._log("[RUNTIME_DEPTH_FIX] All depth annotator methods failed")
else:
self._log("[RUNTIME_DEPTH_FIX] Depth annotator exists, verifying data...")
# Even with annotator, check if data is valid
depth_data = frame.get('distance_to_image_plane')
if depth_data is not None:
self._log(f"[RUNTIME_DEPTH_FIX] Depth data type: {type(depth_data)}, valid: {hasattr(depth_data, 'shape')}")
else:
self._log("[RUNTIME_DEPTH_FIX] Depth annotator exists but data is None")
except Exception as e:
self._log(f"[RUNTIME_DEPTH_FIX] Runtime depth fix failed: {e}")
import traceback
self._log(f"[RUNTIME_DEPTH_FIX] Error details: {traceback.format_exc()}")
def _force_refresh_depth_pipeline(self) -> None:
"""Force refresh depth rendering pipeline."""
try:
self._log("[DEPTH_PIPELINE] Force refreshing depth pipeline...")
# Method 1: Reset camera depth config
if hasattr(self, 'cam') and self.cam:
try:
# Get current camera info
cam_pos, cam_rot = self.cam.get_world_pose()
self._log(f"[DEPTH_PIPELINE] Current camera position: {cam_pos}")
# Force clear and re-add depth annotator
try:
# Clear existing depth annotator
frame = self.cam.get_current_frame()
if frame:
depth_keys = [k for k in frame.keys() if 'depth' in k.lower() or 'distance' in k.lower()]
self._log(f"[DEPTH_PIPELINE] Found existing depth keys: {depth_keys}")
# Re-add distance_to_image_plane
self.cam.add_distance_to_image_plane_to_frame()
self._log("[DEPTH_PIPELINE] Re-added distance_to_image_plane annotator")
except Exception as e:
self._log(f"[DEPTH_PIPELINE] Failed to re-add annotator: {e}")
except Exception as e:
self._log(f"[DEPTH_PIPELINE] Camera depth config failed: {e}")
# Method 2: Force reset collision mesh
try:
self._log("[DEPTH_PIPELINE] Force resetting collision mesh...")
self.set_collision_mesh_visibility(False)
for i in range(2):
self.world.step(render=True)
self.set_collision_mesh_visibility(True)
for i in range(2):
self.world.step(render=True)
self._log("[DEPTH_PIPELINE] Collision mesh reset complete")
except Exception as e:
self._log(f"[DEPTH_PIPELINE] Collision mesh reset failed: {e}")
# Method 3: Try Isaac Sim renderer reconfiguration
try:
self._log("[DEPTH_PIPELINE] Reconfiguring Isaac Sim renderer...")
# Reconfigure depth rendering
self._configure_isaac_sim_depth_rendering()
# Force multiple renders to stabilize
for i in range(3):
self.world.step(render=True)
self._log("[DEPTH_PIPELINE] Renderer reconfiguration complete")
except Exception as e:
self._log(f"[DEPTH_PIPELINE] Renderer reconfiguration failed: {e}")
self._log("[DEPTH_PIPELINE] Depth pipeline refresh complete")
except Exception as e:
self._log(f"[DEPTH_PIPELINE] Depth pipeline refresh failed: {e}")
import traceback
self._log(f"[DEPTH_PIPELINE] Error details: {traceback.format_exc()}")
def _configure_isaac_sim_depth_rendering(self) -> None:
"""Configure Isaac Sim depth rendering for 3DGS+collision mesh scenes."""
try:
self._log("[DEPTH_CONFIG] Configuring 3DGS scene depth rendering...")
# Key fix: make collision mesh visible for depth
self._make_collision_mesh_visible_for_depth()
# Method 1: Configure renderer via carb settings
try:
import carb.settings
settings = carb.settings.get_settings()
# Enable depth rendering
settings.set("/renderer/enabled", True)
settings.set("/renderer/asyncRenderEnabled", False)
settings.set("/rtx/rendermode", "RayTracedLighting")
settings.set("/rtx/pathtracing/enabled", False)
# Force enable depth buffer
settings.set("/renderer/depth/enabled", True)
settings.set("/renderer/depth/format", "float32")
self._log("[DEPTH_CONFIG] carb settings configured")
except Exception as e:
self._log(f"[DEPTH_CONFIG] carb settings failed: {e}")
# Method 2: Configure camera clipping range
try:
if hasattr(self, 'usd_cam') and self.usd_cam:
# Set reasonable near/far clipping planes
self.usd_cam.GetClippingRangeAttr().Set((0.01, 50.0))
self._log("[DEPTH_CONFIG] Camera clipping range set to(0.01, 50.0)")
elif hasattr(self, 'cam') and self.cam:
# Try via Isaac Sim camera API
if hasattr(self.cam, 'set_clipping_range'):
self.cam.set_clipping_range(0.01, 50.0)
self._log("[DEPTH_CONFIG] Isaac Sim camera clipping range set")
except Exception as e:
self._log(f"[DEPTH_CONFIG] Camera clipping range setting failed: {e}")
# Method 3: Force refresh renderer
try:
if hasattr(self, 'world') and self.world:
# Multiple renders to ensure config takes effect
for i in range(3):
self.world.step(render=True)
self._log("[DEPTH_CONFIG] Renderer refresh complete")
except Exception as e:
self._log(f"[DEPTH_CONFIG] Renderer refresh failed: {e}")
except Exception as e:
self._log(f"[DEPTH_CONFIG] Depth rendering config failed: {e}")
def _make_collision_mesh_visible_for_depth(self) -> None:
"""Make collision mesh visible for depth rendering."""
try:
self._log("[COLLISION_DEPTH] Finding and configuring collision mesh for depth...")
# Find collision mesh path - based on USDA file structure
collision_paths = [
"/World/scene_collision",
"/World/collision",
"/collision",
"/World/Collision",
"/Collision",
"/World/collision_mesh",
"/collision_mesh"
]
found_collision = False
for collision_path in collision_paths:
try:
collision_prim = self.stage.GetPrimAtPath(collision_path)
if collision_prim and collision_prim.IsValid():
self._log(f"[COLLISION_DEPTH] Found collision mesh: {collision_path}")
# Key fix: make entire collision prim visible for depth
from pxr import UsdGeom
# First make collision root prim visible
try:
imageable = UsdGeom.Imageable(collision_prim)
current_visibility = imageable.GetVisibilityAttr().Get()
self._log(f"[COLLISION_DEPTH] Current collision visibility: {current_visibility}")
# Set visible (allows depth rendering)
imageable.GetVisibilityAttr().Set(UsdGeom.Tokens.visible)
self._log(f"[COLLISION_DEPTH] Set collision root prim visible")
except Exception as e:
self._log(f"[COLLISION_DEPTH] Failed to set root prim visibility: {e}")
# Recursively ensure all child meshes visible
def make_mesh_visible_for_depth(prim):
try:
# Set visibility for all prims
if hasattr(prim, 'GetTypeName'):
prim_type = prim.GetTypeName()
prim_path = str(prim.GetPath())
if prim_type in ['Mesh', 'Xform', 'Scope']:
imageable = UsdGeom.Imageable(prim)
if imageable:
imageable.GetVisibilityAttr().Set(UsdGeom.Tokens.visible)
self._log(f"[COLLISION_DEPTH] Set visible: {prim_path} ({prim_type})")
# Recursively process child prims
for child in prim.GetChildren():
make_mesh_visible_for_depth(child)
except Exception as e:
self._log(f"[COLLISION_DEPTH] Failed to configure child prim {prim.GetPath()}: {e}")
# Apply to all child prims
make_mesh_visible_for_depth(collision_prim)
found_collision = True
self._log(f"[COLLISION_DEPTH] Successfully configured collision and child meshes for depth")
break
except Exception as e:
self._log(f"[COLLISION_DEPTH] Failed to check path: {e}")
if not found_collision:
self._log("[COLLISION_DEPTH] Collision mesh not found, searching all meshes...")
# Search entire scene for meshes
self._find_and_configure_all_meshes()
except Exception as e:
self._log(f"[COLLISION_DEPTH] Collision mesh config failed: {e}")
def _find_and_configure_all_meshes(self) -> None:
"""Find and configure all meshes in scene for depth rendering."""
try:
from pxr import UsdGeom, Usd
self._log("[MESH_SEARCH] Searching all meshes in scene...")
# Search entire stage for meshes
def traverse_and_configure(prim):
try:
if prim.GetTypeName() == 'Mesh':
mesh_path = str(prim.GetPath())
self._log(f"[MESH_SEARCH] Found mesh: {mesh_path}")
# Make mesh visible and configure for depth
imageable = UsdGeom.Imageable(prim)
imageable.GetVisibilityAttr().Set(UsdGeom.Tokens.visible)
# Mark collision-related paths specially
if any(word in mesh_path.lower() for word in ['collision', 'physics', 'collider']):
self._log(f"[MESH_SEARCH] Configured collision mesh for depth: {mesh_path}")
else:
self._log(f"[MESH_SEARCH] Configured regular mesh for depth: {mesh_path}")
# Recursively process child prims
for child in prim.GetChildren():
traverse_and_configure(child)
except Exception as e:
self._log(f"[MESH_SEARCH] Failed to process prim {prim.GetPath()}: {e}")
# Start search from root
root_prim = self.stage.GetDefaultPrim()
if root_prim:
traverse_and_configure(root_prim)
else:
# If no default prim, start from /World
world_prim = self.stage.GetPrimAtPath("/World")
if world_prim:
traverse_and_configure(world_prim)
self._log("[MESH_SEARCH] Mesh search and config complete")
except Exception as e:
self._log(f"[MESH_SEARCH] Mesh search failed: {e}")
def set_collision_mesh_visibility(self, visible: bool) -> None:
"""Force set collision mesh visibility.
Args:
visible (bool): True for visible (depth), False for invisible (RGB)
"""
try:
action = "enable" if visible else "disable"
self._log(f"[COLLISION_VIS] Force setting collision mesh visibility...")
# Find collision mesh paths (extended search)
collision_paths = [
"/World/scene_collision",
"/World/collision",
"/collision",
"/World/Collision",
"/Collision",
"/World/collision_mesh",
"/collision_mesh",
"/scene_collision"
]
from pxr import UsdGeom
collision_prims_found = []
# Find all collision prims
for coll_path in collision_paths:
try:
collision_prim = self.stage.GetPrimAtPath(coll_path)
if collision_prim and collision_prim.IsValid():
collision_prims_found.append((coll_path, collision_prim))
self._log(f"[COLLISION_VIS] Found collision: {coll_path}")
except Exception as e:
continue
if not collision_prims_found:
self._log("[COLLISION_VIS] No collision mesh found, searching all collision paths...")
# If not found, search scene for collision paths
from pxr import Usd
def search_collision_recursive(prim, path=""):
current_path = str(prim.GetPath())
if "collision" in current_path.lower():
self._log(f"[COLLISION_SEARCH] Found possible collision path: {current_path}")
return [current_path]
found_paths = []
for child in prim.GetChildren():
found_paths.extend(search_collision_recursive(child, current_path))
return found_paths
all_collision_paths = search_collision_recursive(self.stage.GetPseudoRoot())
if all_collision_paths:
self._log(f"[COLLISION_SEARCH] Found collision-related paths: {all_collision_paths}")
# Try these paths
for found_path in all_collision_paths[:3]:
try:
collision_prim = self.stage.GetPrimAtPath(found_path)
if collision_prim and collision_prim.IsValid():
collision_prims_found.append((found_path, collision_prim))
self._log(f"[COLLISION_VIS] Dynamically found collision: {found_path}")
except Exception as e:
continue
if not collision_prims_found:
self._log("[COLLISION_VIS] No collision mesh found, depth may be invalid")
return
# Set visibility for each found collision prim
for coll_path, collision_prim in collision_prims_found:
try:
# Force set root prim visibility
imageable = UsdGeom.Imageable(collision_prim)
if visible:
imageable.GetVisibilityAttr().Set(UsdGeom.Tokens.visible)
self._log(f"[COLLISION_VIS] Set visible")
else:
imageable.GetVisibilityAttr().Set(UsdGeom.Tokens.invisible)
self._log(f"[COLLISION_VIS] Set invisible")
# Recursively force set all children
def force_set_visibility(prim, vis_state):
try:
# Set visibility for all prim types
child_imageable = UsdGeom.Imageable(prim)
if child_imageable:
if vis_state:
child_imageable.GetVisibilityAttr().Set(UsdGeom.Tokens.visible)
else:
child_imageable.GetVisibilityAttr().Set(UsdGeom.Tokens.invisible)
# Recursively process all child prims
for child in prim.GetChildren():
force_set_visibility(child, vis_state)
except Exception:
pass
# Apply to all children
force_set_visibility(collision_prim, visible)
except Exception as e:
self._log(f"[COLLISION_VIS] Failed to set: {e}")
# Multiple force renders to ensure settings take effect
if hasattr(self, 'world') and self.world:
for i in range(3):
self.world.step(render=True)
self._log(f"[COLLISION_VIS] Collision mesh visibility set")
except Exception as e:
self._log(f"[COLLISION_VIS] Failed to set collision mesh visibility: {e}")
def _create_simple_camera(self) -> None:
"""Create agent with physics body and camera."""
print("[CAMERA_CREATE] Creating camera...")
print(f"[CAMERA_CREATE] agent_prim_path: {self.agent_prim_path}")
print(f"[CAMERA_CREATE] resolution: {self.resolution}")
print(f"[CAMERA_CREATE] hz: {self.hz}")
try:
# Create agent parent Xform
agent_xform = self._UsdGeom.Xform.Define(self.stage, self.agent_prim_path)
# Create agent physics collision body (cylinder)
collision_cylinder_path = self.agent_prim_path + "/CollisionCylinder"
collision_cylinder = self._UsdGeom.Cylinder.Define(self.stage, collision_cylinder_path)
collision_cylinder.CreateRadiusAttr(0.1)
collision_cylinder.CreateHeightAttr(0.5)
collision_cylinder.CreateAxisAttr("Z")
# Set collision body position
collision_cylinder.AddTranslateOp().Set((0.0, 0.0, 0.0))
# Make collision body invisible but keep geometry
collision_cylinder.CreateVisibilityAttr("invisible")
_debug_print(f"[PHYSICS] Created collision cylinder: radius=0.1m, height=0.5m, bottom_clearance=0.5m")
# Add physics properties to agent
agent_rigid_body = self._UsdPhysics.RigidBodyAPI.Apply(agent_xform.GetPrim())
# Use kinematic mode with collision detection
agent_rigid_body.CreateKinematicEnabledAttr(True)
_debug_print(f"[PHYSICS] Using kinematic rigid body with physics-based collision response")
# Try to set mass and other attributes
try:
agent_rigid_body.CreateMassAttr(1.0)
_debug_print(f"[PHYSICS] Set mass to 1.0")
except:
try:
# New version may use different methods
if hasattr(agent_rigid_body, 'CreateMassAttr'):
agent_rigid_body.CreateMassAttr(1.0)
else:
_debug_print(f"[PHYSICS] WARN: CreateMassAttr not available")
except Exception as e:
_debug_print(f"[PHYSICS] WARN: Failed to set mass: {e}")
try:
agent_rigid_body.CreateRigidBodyEnabledAttr(True)
_debug_print(f"[PHYSICS] Enabled rigid body")
except:
try:
# New version may use different methods
if hasattr(agent_rigid_body, 'CreateRigidBodyEnabledAttr'):
agent_rigid_body.CreateRigidBodyEnabledAttr(True)
else:
_debug_print(f"[PHYSICS] WARN: CreateRigidBodyEnabledAttr not available")
except Exception as e:
_debug_print(f"[PHYSICS] WARN: Failed to enable rigid body: {e}")
# Add collision attributes to cylinder
collision_api = self._UsdPhysics.CollisionAPI.Apply(collision_cylinder.GetPrim())
# Add collision shape
try:
# Use geometric cylinder as collision body
collision_shape = collision_cylinder
_debug_print(f"[PHYSICS] Using geometric cylinder as collision shape")
# Ensure collision body is recognized
collision_api.CreateCollisionEnabledAttr(True)
_debug_print(f"[PHYSICS] Enabled collision detection")
# Store collision shape reference
self.collision_shape_prim = collision_cylinder
self.collision_cylinder_prim = collision_cylinder
except Exception as e:
_debug_print(f"[PHYSICS] WARN: Failed to setup collision: {e}")
self.collision_shape_prim = None
self.collision_cylinder_prim = None
# Set collision filtering
try:
collision_api.CreateCollisionEnabledAttr(True)
_debug_print(f"[PHYSICS] Enabled collision detection")
except Exception as e:
_debug_print(f"[PHYSICS] WARN: Failed to enable collision: {e}")
# Create Isaac Sim camera
try:
# Create camera prim path
camera_path = self.agent_prim_path + "/Camera"
# Define USD camera prim
camera_prim = self._UsdGeom.Camera.Define(self.stage, camera_path)
# Create Isaac Sim camera object with depth
self.cam = self._Camera(
prim_path=camera_path,
frequency=self.hz,
resolution=self.resolution
)
# Add depth annotator before initialization
try:
# Method 1: add_distance_to_image_plane_to_frame
if hasattr(self.cam, 'add_distance_to_image_plane_to_frame'):
self.cam.add_distance_to_image_plane_to_frame()
print("[CAMERA] Added distance_to_image_plane annotator")
# Method 2: add_distance_to_camera_to_frame (fallback)
elif hasattr(self.cam, 'add_distance_to_camera_to_frame'):
self.cam.add_distance_to_camera_to_frame()
print("[CAMERA] Added distance_to_camera annotator")
# Method 3: Generic annotator method
elif hasattr(self.cam, 'add_annotator'):
self.cam.add_annotator("distance_to_image_plane")
print("[CAMERA] Added depth annotator via generic method")
else:
print("[CAMERA] Camera does not support depth annotator methods")
# List available methods
annotator_methods = [m for m in dir(self.cam) if 'add' in m.lower() and ('distance' in m.lower() or 'depth' in m.lower() or 'annotator' in m.lower())]
print(f"[CAMERA] Available annotator methods: {annotator_methods}")
except Exception as e:
print(f"[CAMERA] Failed to add depth annotator: {e}")
self.cam.initialize()
# Verify annotator was added
try:
# Render one frame to activate annotator
if hasattr(self, 'world') and self.world:
self.world.step(render=True)
frame = self.cam.get_current_frame()
available_keys = list(frame.keys()) if frame else []
print(f"[CAMERA] Camera frame available data: {available_keys}")
if 'distance_to_image_plane' in available_keys:
print("[CAMERA] Depth annotator verified")
else:
print("[CAMERA] Depth annotator verification failed")
except Exception as e:
print(f"[CAMERA] Annotator verification failed: {e}")
# Get camera prim and USD camera
self.cam_prim = self.stage.GetPrimAtPath(camera_path)
self.usd_cam = self._UsdGeom.Camera(self.cam_prim)
# Set camera parameters for depth
try:
# Set reasonable clipping range
self.usd_cam.GetClippingRangeAttr().Set((0.1, 50.0))
print("[CAMERA] Set camera clipping range (0.1, 50.0)")
except Exception as e:
print(f"[CAMERA] Failed to set clipping range: {e}")
# Set camera parameters
self.usd_cam.GetFocalLengthAttr().Set(8.0)
print(f"[INFO] Created Isaac Sim camera at: {camera_path}")
except Exception as e:
print(f"[ERROR] Failed to create camera: {e}")
import traceback
traceback.print_exc()
self.cam = None
self.cam_prim = None
self.usd_cam = None
# Store references
self.agent_prim = agent_xform.GetPrim()
self.collision_cylinder_prim = collision_cylinder.GetPrim()
self.collision_shape_prim = collision_cylinder.GetPrim()
print(f"[INFO] Created agent with physics: cylinder radius=0.01m, height=0.7m, camera at z=1.2m, focal_length=8.0")
print(f"[INFO] Physics enabled: RigidBody={agent_rigid_body.GetRigidBodyEnabledAttr().Get()}, Collision={collision_api.GetCollisionEnabledAttr().Get()}")
# Setup physics scene
self._setup_physics_scene()
# Verify physics config
self._verify_agent_physics()
# Verify collision system
self._verify_collision_system()
except Exception as e:
print(f"[ERROR] Failed to create agent with physics: {e}")
# If failed, fallback to simple camera
try:
# Cleanup previously created prims
if hasattr(self, 'agent_prim') and self.agent_prim:
try:
import omni.usd
omni.usd.delete_prim(self.stage, self.agent_prim.GetPath())
print(f"[CLEANUP] Removed failed agent prim")
except:
pass
# Create simple camera prim
camera_prim = self._UsdGeom.Camera.Define(self.stage, self.agent_prim_path)
# Create Isaac Sim camera object
self.cam = self._Camera(prim_path=self.agent_prim_path, frequency=self.hz, resolution=self.resolution)
self.cam.initialize()
# Set camera attributes
self.cam_prim = self.stage.GetPrimAtPath(self.agent_prim_path)
self.usd_cam = self._UsdGeom.Camera(self.cam_prim)
self.usd_cam.GetFocalLengthAttr().Set(8.0)
# Set agent reference
self.agent_prim = camera_prim.GetPrim()
self.collision_cylinder_prim = None
self.collision_shape_prim = None
print(f"[WARN] Fallback to simple camera without physics")
except Exception as e2:
print(f"[ERROR] Failed to create fallback camera: {e2}")
raise e2
def _verify_physics_system(self) -> None:
"""Verify physics system initialization."""
try:
print(f"[PHYSICS_VERIFY] Checking physics system status...")
# Check world object
if hasattr(self, 'world') and self.world:
print(f"[PHYSICS_VERIFY] World object: {type(self.world)}")
else:
print(f"[PHYSICS_VERIFY] WARN: No world object")
# Check agent prim
if hasattr(self, 'agent_prim') and self.agent_prim:
print(f"[PHYSICS_VERIFY] Agent prim: {self.agent_prim.GetPath()}")
# Check rigid body attributes
rigid_body = self._UsdPhysics.RigidBodyAPI(self.agent_prim)
if rigid_body:
try:
enabled = rigid_body.GetRigidBodyEnabledAttr().Get()
kinematic = rigid_body.GetKinematicEnabledAttr().Get()
print(f"[PHYSICS_VERIFY] RigidBody: enabled={enabled}, kinematic={kinematic}")
except Exception as e:
print(f"[PHYSICS_VERIFY] RigidBody: API available but failed to get attributes: {e}")
else:
print(f"[PHYSICS_VERIFY] WARN: No RigidBody API")
else:
print(f"[PHYSICS_VERIFY] WARN: No agent prim")
# Check collision body
if hasattr(self, 'collision_shape_prim') and self.collision_shape_prim:
print(f"[PHYSICS_VERIFY] Collision shape: {self.collision_shape_prim.GetPath()}")
# Check collision attributes
try:
collision_api = self._UsdPhysics.CollisionAPI(self.collision_shape_prim)
if collision_api:
try:
enabled = collision_api.GetCollisionEnabledAttr().Get()
print(f"[PHYSICS_VERIFY] Collision: enabled={enabled}")
except Exception as e:
print(f"[PHYSICS_VERIFY] Collision: API available but failed to get enabled attribute: {e}")
else:
print(f"[PHYSICS_VERIFY] WARN: No Collision API")
except Exception as e:
print(f"[PHYSICS_VERIFY] WARN: Failed to create Collision API: {e}")
else:
print(f"[PHYSICS_VERIFY] WARN: No collision shape prim")
print(f"[PHYSICS_VERIFY] Physics system verification complete")
except Exception as e:
print(f"[PHYSICS_VERIFY] ERROR: Failed to verify physics system: {e}")
def _get_scene_bounds(self) -> None:
"""Get scene bounds information."""
try:
print(f"[SCENE_BOUNDS] Analyzing scene boundaries...")
# Traverse all geometry to find bounds
stage = self.stage
if stage:
# Get scene root path
root_path = stage.GetPseudoRoot().GetPath()
# Traverse all prims in scene
all_prims = []
def collect_prims(prim):
all_prims.append(prim)
for child in prim.GetChildren():
collect_prims(child)
collect_prims(stage.GetPseudoRoot())
# Find all prims with geometry
geom_prims = [p for p in all_prims if p.IsA(self._UsdGeom.Gprim)]
if geom_prims:
# Calculate bounding box for all geometry
min_x, min_y, min_z = float('inf'), float('inf'), float('inf')
max_x, max_y, max_z = float('-inf'), float('-inf'), float('-inf')
for prim in geom_prims:
try:
# Get prim bounding box
bbox = self._UsdGeom.ComputeBoundingBox(prim, 0)
if bbox:
min_x = min(min_x, bbox.GetMin()[0])
min_y = min(min_y, bbox.GetMin()[1])
min_z = min(min_z, bbox.GetMin()[2])
max_x = max(max_x, bbox.GetMax()[0])
max_y = max(max_y, bbox.GetMax()[1])
max_z = max(max_z, bbox.GetMax()[2])
except:
continue
if min_x != float('inf'):
self.scene_bounds = {
'x': (min_x, max_x),
'y': (min_y, max_y),
'z': (min_z, max_z)
}
print(f"[SCENE_BOUNDS] Scene bounds: x=[{min_x:.2f}, {max_x:.2f}], y=[{min_y:.2f}, {max_y:.2f}], z=[{min_z:.2f}, {max_z:.2f}]")
else:
print(f"[SCENE_BOUNDS] WARN: Could not determine scene bounds")
self.scene_bounds = {'x': (-5, 5), 'y': (-12, 3), 'z': (0, 3)}
else:
print(f"[SCENE_BOUNDS] WARN: No geometric prims found in scene")
self.scene_bounds = {'x': (-5, 5), 'y': (-12, 3), 'z': (0, 3)}
else:
print(f"[SCENE_BOUNDS] WARN: No stage available")
self.scene_bounds = {'x': (-5, 5), 'y': (-12, 3), 'z': (0, 3)}
except Exception as e:
print(f"[SCENE_BOUNDS] ERROR: Failed to get scene bounds: {e}")
self.scene_bounds = {'x': (-5, 5), 'y': (-12, 3), 'z': (0, 3)}
def load_scene(self, new_scene_usd_path: str) -> bool:
"""
Switch to new scene.
Args:
new_scene_usd_path: Path to new scene USD file
Returns:
bool: Whether scene was loaded successfully
"""
try:
print(f"[SCENE_SWITCH] Switching scene...")
print(f"[SCENE_SWITCH] Current scene: {self.scene_usd_path}")
print(f"[SCENE_SWITCH] New scene: {new_scene_usd_path}")
# Update scene path
self.scene_usd_path = str(Path(new_scene_usd_path).resolve())
# Switch scene via sim object
if hasattr(self.sim, 'load_scene'):
return self.sim.load_scene(new_scene_usd_path)
else:
print(f"[SCENE_SWITCH] sim object does not support scene switching")
return False
except Exception as e:
print(f"[SCENE_SWITCH] Scene switch failed: {e}")
import traceback
traceback.print_exc()
return False
def update_map(self, new_map_path: str) -> bool:
"""
Dynamically update 2D semantic map.
Args:
new_map_path: Path to new 2D semantic map file
Returns:
bool: Whether update was successful
"""
try:
print(f"[MAP_SWITCH] Switching map...")
print(f"[MAP_SWITCH] Current map: {self._map_json_path}")
print(f"[MAP_SWITCH] New map: {new_map_path}")
# Update map path
self._map_json_path = new_map_path
self.semantic_map_path = new_map_path
# Reinitialize collision detector
self._init_collision_detector(new_map_path)
print(f"[MAP_SWITCH] Map switch successful")
return True
except Exception as e:
print(f"[MAP_SWITCH] Map switch failed: {e}")
import traceback
traceback.print_exc()
return False
def close(self) -> None:
self.sim.close()
def set_start_pose(self, position: List[float], rotation_xyzw: List[float]) -> None:
# Reset episode state
self.is_stop_called = False
self.consecutive_collisions = 0
self._total_collision_count = 0 # Reset total collision count for new episode
self._log(f"[EPISODE_RESET] Reset episode state")
self._pos = np.array(position, dtype=np.float32)
# rotation_xyzw = [x,y,z,w] - standard quaternion format
x, y, z, w = rotation_xyzw
# Save original quaternion for camera orientation
self._original_quaternion = rotation_xyzw.copy()
# Calculate yaw from trajectory quaternion
# Based on trajectory transform inverse mapping:
# 1. Inverse coord mapping: qz_original = -qx, qw_original = qw
# 2. Calculate yaw_new = 2 * atan2(qz_original, qw_original)
# 3. Inverse angle offset: yaw_final = yaw_new - pi
qz_original = -x
qw_original = w
yaw_new = 2 * math.atan2(qz_original, qw_original)
self._yaw = yaw_new - math.pi
# Ensure angle in [-pi, pi] range
if self._yaw < -math.pi:
self._yaw += 2 * math.pi
elif self._yaw > math.pi:
self._yaw -= 2 * math.pi
# Record initial yaw for camera orientation
self._initial_yaw = self._yaw
# Debug coordinate transformation
self._log(f"[DEBUG] Setting start pose:")
self._log(f"[DEBUG] Input position: {position}")
self._log(f"[DEBUG] Input rotation: {rotation_xyzw}")
self._log(f"[DEBUG] qz_original: {qz_original:.3f}, qw_original: {qw_original:.3f}")
self._log(f"[DEBUG] Final position: {self._pos}")
self._apply_pose()
for _ in range(5):
self.world.step(render=True)
def _update_camera_position(self) -> None:
"""Update camera position to follow agent."""
try:
# Use current self._pos
# Set camera at agent eye height
if hasattr(self, 'cam') and self.cam:
camera_pos = self._pos.copy()
camera_pos[2] = 1.2
# Camera orientation calculation
if hasattr(self, '_original_quaternion') and hasattr(self, '_initial_yaw'):
# Case 1: Dynamic turning - incremental adjustment
# Calculate angle change relative to initial yaw
yaw_delta = self._yaw - self._initial_yaw
# Use original quaternion + 45 degree correction
qx_orig, qy_orig, qz_orig, qw_orig = self._original_quaternion
angle_correction = math.radians(-45)
qx_correction = math.sin(angle_correction/2)
# Base orientation
base_qx = qx_orig + qx_correction
base_qy = qy_orig
base_qz = qz_orig
base_qw = qw_orig
# If yaw changed, use quaternion multiplication
if abs(yaw_delta) > 0.01:
# Calculate yaw change quaternion
qz_delta_tmp = math.sin(yaw_delta/2.0)
qw_delta_tmp = math.cos(yaw_delta/2.0)
# Quaternion multiplication (simplified)
qx = base_qx * qw_delta_tmp + base_qw * (-qz_delta_tmp)
qy = base_qy * qw_delta_tmp
qz = base_qz * qw_delta_tmp
qw = base_qw * qw_delta_tmp - base_qx * (-qz_delta_tmp)
else:
# No significant change, use base orientation
qx, qy, qz, qw = base_qx, base_qy, base_qz, base_qw
orientation = np.array([qx, qy, qz, qw], dtype=np.float32)
# Debug output
elif hasattr(self, '_original_quaternion'):
# Case 2: Initial state
qx_orig, qy_orig, qz_orig, qw_orig = self._original_quaternion
angle_correction = math.radians(-45)
qx_correction = math.sin(angle_correction/2)
qx = qx_orig + qx_correction
qy = qy_orig
qz = qz_orig
qw = qw_orig
orientation = np.array([qx, qy, qz, qw], dtype=np.float32)
else:
# Case 3: Fallback
qz_tmp = math.sin(self._yaw/2.0)
qw_tmp = math.cos(self._yaw/2.0)
qx = -qz_tmp
qy = 0.0
qz = 0.0
qw = qw_tmp
orientation = np.array([qx, qy, qz, qw], dtype=np.float32)
if not hasattr(self, '_last_yaw') or abs(self._last_yaw - self._yaw) > 0.01:
if hasattr(self, '_initial_yaw'):
yaw_delta = self._yaw - self._initial_yaw
else:
pass
if hasattr(self, '_original_quaternion'):
pass
self._last_yaw = self._yaw
# Set camera position directly
try:
self.cam.set_world_pose(position=camera_pos.astype(np.float32), orientation=orientation)
except Exception as e:
pass
# Update agent physics position (if available)
if hasattr(self, 'agent_prim') and self.agent_prim:
try:
xform = self._UsdGeom.Xform(self.agent_prim)
xform.ClearXformOpOrder()
translate_op = xform.AddTranslateOp()
translate_op.Set(tuple(self._pos))
rotate_op = xform.AddRotateZOp()
rotate_op.Set(math.degrees(self._yaw))
except Exception as e:
pass
if not hasattr(self, '_camera_log_count'):
self._camera_log_count = 0
self._last_logged_pos = self._pos[:2].copy()
self._camera_log_count += 1
distance_moved = np.linalg.norm(self._pos[:2] - self._last_logged_pos)
if distance_moved > 0.1 or self._camera_log_count >= 50:
_debug_print(f"[CAMERA_UPDATE] Step #{self._camera_log_count}: Camera at {camera_pos[:2]} (moved: {distance_moved:.3f}m)")
self._last_logged_pos = self._pos[:2].copy()
self._camera_log_count = 0
except Exception as e:
print(f"[CAMERA_UPDATE_ERROR] Failed to update camera: {e}")
def _apply_pose(self) -> None:
"""Apply position and orientation to physics system."""
# Calculate orientation
qx = -math.sin(self._yaw/2.0)
qy = 0.0
qz = 0.0
qw = math.cos(self._yaw/2.0)
orientation = np.array([qx,qy,qz,qw], dtype=np.float32)
# Set agent position
if hasattr(self, 'agent_prim') and self.agent_prim:
try:
# Force set position
xform = self._UsdGeom.Xform(self.agent_prim)
xform.ClearXformOpOrder()
translate_op = xform.AddTranslateOp()
translate_op.Set(tuple(self._pos))
rotate_op = xform.AddRotateZOp()
rotate_op.Set(math.degrees(self._yaw))
except Exception as e:
print(f"[WARN] Failed to apply pose: {e}")
# Update camera position
self._update_camera_position()
# Run physics step to update scene
if hasattr(self, 'world'):
self.world.step(render=True)
def get_agent_pos(self) -> np.ndarray:
return self._pos.copy()
def get_rgb(self) -> np.ndarray:
if hasattr(self, 'cam') and self.cam:
try:
# RGB capture: force disable collision mesh
self.set_collision_mesh_visibility(False)
# Ensure settings take effect: multiple updates
self._update_camera_position()
for i in range(2):
self.world.step(render=True)
cam_pos, cam_orientation = self.cam.get_world_pose()
# Get RGB image
img = self.cam.get_rgba()
# 在get_rgb()里加一行临时调试
# print(f"[DEBUG_RGB] rgba sample: {img[0,0]}, shape: {img.shape}, unique values: {len(set(img.reshape(-1,4).tobytes()))}")
if img is None or img.size == 0:
return None
# Process image
rgb_img = img[:, :, :3].astype(np.uint8)
return rgb_img
except Exception as e:
return None
else:
return np.zeros((480, 640, 3), dtype=np.uint8)
def get_depth(self) -> np.ndarray:
"""Get depth image."""
if hasattr(self, 'cam') and self.cam:
try:
# Depth capture: force enable collision mesh
self.set_collision_mesh_visibility(True)
# Ensure settings take effect: multiple updates
self._update_camera_position()
for i in range(2):
self.world.step(render=True)
# Force refresh depth pipeline
self._force_refresh_depth_pipeline()
# Get depth image
depth_img = None
# Try multiple Isaac Sim depth methods
# Method 1: Get depth via annotator
if depth_img is None:
try:
frame = self.cam.get_current_frame()
if frame and 'distance_to_image_plane' in frame:
depth_img = frame['distance_to_image_plane']
# Convert to numpy
if hasattr(depth_img, 'cpu'):
depth_img = depth_img.cpu().numpy()
depth_img = np.array(depth_img, dtype=np.float32)
else:
available_keys = list(frame.keys()) if frame else []
except Exception as e:
pass
# Method 2: Get depth via Isaac Sim Replicator
if depth_img is None:
try:
# New Isaac Sim version uses replicator
import omni.replicator.core as rep
# Create replicator camera for depth
with rep.new_layer():
rep_camera = rep.create.camera(
position=(0, 0, 0),
look_at=(0, 0, -1)
)
# Get depth rendering
render_products = rep.create.render_product(rep_camera, self.resolution)
depth_annotator = rep.AnnotatorRegistry.get_annotator("distance_to_image_plane")
depth_annotator.attach([render_products])
# Render and get depth
rep.orchestrator.step()
depth_data = depth_annotator.get_data()
if depth_data is not None and len(depth_data) > 0:
depth_img = np.array(depth_data, dtype=np.float32)
else:
pass
except Exception as e:
pass
# Method 2b: Try traditional SyntheticData API
if depth_img is None:
try:
import omni.syntheticdata._syntheticdata as sd
# Get current viewport depth data
depth_img = sd.get_depth_linear(viewport_name="Viewport")
if depth_img is not None and depth_img.size > 0:
depth_img = np.array(depth_img, dtype=np.float32)
else:
pass
except Exception as e:
pass
# Method 3: Get depth buffer via USD renderer
if depth_img is None:
try:
# Get depth directly from USD renderer
from pxr import UsdRender
import omni.usd
stage = omni.usd.get_context().get_stage()
# Get render settings
render_settings = UsdRender.Settings.GetStage(stage)
if render_settings:
# Force enable depth output
render_settings.CreateEnabledAttr().Set(True)
# Try to get depth data from camera prim
cam_prim = stage.GetPrimAtPath(self.cam.prim_path)
if cam_prim:
# Check if camera has depth attribute
if cam_prim.HasAttribute("depth"):
depth_attr = cam_prim.GetAttribute("depth")
depth_img = depth_attr.Get()
if depth_img is not None:
depth_img = np.array(depth_img, dtype=np.float32)
except Exception as e:
pass
# Method 4: Try direct camera method calls
if depth_img is None:
depth_methods = ['get_distance_to_image_plane', 'get_depth_data', 'get_depth', 'get_linear_depth']
for method_name in depth_methods:
if hasattr(self.cam, method_name):
try:
method = getattr(self.cam, method_name)
depth_img = method()
if depth_img is not None and hasattr(depth_img, 'size') and depth_img.size > 0:
if hasattr(depth_img, 'cpu'):
depth_img = depth_img.cpu().numpy()
depth_img = np.array(depth_img, dtype=np.float32)
break
else:
pass
except Exception as e:
pass
continue
# Method 5: Final depth generation fallback
if depth_img is None:
try:
# If all methods fail, generate pseudo depth
# Generate simple depth based on camera position
height, width = self.resolution[1], self.resolution[0]
# Create simple linear depth map
y, x = np.ogrid[:height, :width]
center_y, center_x = height // 2, width // 2
# Distance from center
dist_from_center = np.sqrt((x - center_x)**2 + (y - center_y)**2)
max_dist = np.sqrt(center_x**2 + center_y**2)
# Normalize to depth range [1.0, 6.0] meters
depth_img = 1.0 + 5.0 * (dist_from_center / max_dist)
depth_img = depth_img.astype(np.float32)
except Exception as e:
pass
depth_img = None
# Method 3: Debug info
if depth_img is None:
# Log camera object structure
if hasattr(self.cam, 'data'):
data_attrs = [attr for attr in dir(self.cam.data) if not attr.startswith('_')]
available_methods = [attr for attr in dir(self.cam) if 'depth' in attr.lower() or 'distance' in attr.lower()]
return None
if depth_img is None or depth_img.size == 0:
self._log(f"[WARN] Camera returned None/empty depth image")
return None
# Ensure depth image is correct format
# Isaac Sim depth is usually float in meters
depth_img = depth_img.astype(np.float32)
# Limit depth range
depth_img = np.clip(depth_img, 0.1, 6.5)
return depth_img
except Exception as e:
self._log(f"[WARN] Failed to get depth from Isaac Sim camera: {e}")
return None
else:
# If no Isaac Sim camera, return default depth
self._log(f"[WARN] No Isaac Sim camera available, returning default depth image")
# Create 640x480 default depth map (5m)
return np.full((480, 640), 5.0, dtype=np.float32)
def get_rgbd(self) -> tuple:
"""Get RGB and depth images."""
if hasattr(self, 'cam') and self.cam:
try:
# Step 1: Get RGB image (collision mesh off)
self.set_collision_mesh_visibility(False)
# Ensure camera position update and render
self._update_camera_position()
self.world.step(render=True)
# Get RGB image
rgba_img = self.cam.get_rgba()
rgb_img = None
if rgba_img is not None and rgba_img.size > 0:
rgb_img = rgba_img[:, :, :3].astype(np.uint8)
else:
pass
# Step 2: Get depth image (collision mesh on)
# Force enable collision mesh
self.set_collision_mesh_visibility(True)
# Multiple renders to ensure collision mesh takes effect
self._update_camera_position()
for i in range(3):
self.world.step(render=True)
# Reconfirm collision mesh state
self.set_collision_mesh_visibility(True)
# Runtime fix: check and add depth annotator
self._ensure_depth_annotator_runtime()
# Force refresh depth rendering pipeline
self._force_refresh_depth_pipeline()
# Final render updates
for i in range(5):
self.world.step(render=True)
# Get depth image
depth_img = None
# get_rgbd: use same depth strategy
# Method 1: Get depth via annotator (improved)
if depth_img is None:
try:
# Ensure collision mesh visible for depth
self.set_collision_mesh_visibility(True)
# Force render to ensure data is fresh
self.world.step(render=True)
frame = self.cam.get_current_frame()
if frame:
available_keys = list(frame.keys())
# Try multiple depth keys
depth_keys = ['distance_to_image_plane', 'depth', 'distance_to_camera', 'linear_depth']
depth_data = None
used_key = None
for key in depth_keys:
if key in frame:
depth_data = frame[key]
used_key = key
break
if depth_data is not None:
# Handle different depth data types
if hasattr(depth_data, 'shape'):
if depth_data.size > 0:
if hasattr(depth_data, 'cpu'):
depth_img = depth_data.cpu().numpy()
elif hasattr(depth_data, 'numpy'):
depth_img = depth_data.numpy()
else:
depth_img = np.array(depth_data, dtype=np.float32)
# Ensure 2D array
if depth_img.ndim == 3 and depth_img.shape[-1] == 1:
depth_img = depth_img.squeeze(-1)
elif depth_img.ndim == 1:
# Try reshape to expected size
expected_size = self.resolution[0] * self.resolution[1]
if depth_img.size == expected_size:
depth_img = depth_img.reshape(self.resolution[1], self.resolution[0])
# Check if depth values are valid
if depth_img.ndim == 2:
depth_std = depth_img.std()
depth_range = depth_img.max() - depth_img.min()
if depth_std < 0.001 and depth_range < 0.001:
pass # Depth data has no variation
if depth_img.shape == (self.resolution[1], self.resolution[0]):
pass # Valid shape
else:
depth_img = None
else:
depth_img = None
else:
depth_img = None
else:
depth_img = None
else:
pass
else:
pass
except Exception as e:
pass
import traceback
# Method 2: Get via SyntheticData
if depth_img is None:
try:
# Ensure collision mesh visible for depth
self.set_collision_mesh_visibility(True)
self.world.step(render=True)
import omni.kit.viewport.utility as vp_utils
import omni.syntheticdata as sd
viewport_api = vp_utils.get_viewport_from_window_name("Viewport")
if viewport_api:
sd_interface = sd.acquire_syntheticdata_interface()
depth_img = sd_interface.get_viewport_data(viewport_api, "distance_to_image_plane")
if depth_img is not None and len(depth_img) > 0:
depth_img = np.array(depth_img, dtype=np.float32).reshape(self.resolution[1], self.resolution[0])
except Exception as e:
pass
# Method 3: Try direct method calls
if depth_img is None:
# Ensure collision mesh visible for depth
self.set_collision_mesh_visibility(True)
self.world.step(render=True)
depth_methods = ['get_distance_to_image_plane', 'get_depth_data', 'get_depth', 'get_linear_depth']
for method_name in depth_methods:
if hasattr(self.cam, method_name):
try:
method = getattr(self.cam, method_name)
depth_img = method()
if depth_img is not None and hasattr(depth_img, 'size') and depth_img.size > 0:
if hasattr(depth_img, 'cpu'):
depth_img = depth_img.cpu().numpy()
depth_img = np.array(depth_img, dtype=np.float32)
break
except Exception as e:
pass
continue
# Final fallback: generate pseudo depth
if depth_img is None or (hasattr(depth_img, 'size') and depth_img.size == 0):
try:
height, width = self.resolution[1], self.resolution[0]
# Create radial depth map
y, x = np.ogrid[:height, :width]
center_y, center_x = height // 2, width // 2
# Distance from center
dist_from_center = np.sqrt((x - center_x)**2 + (y - center_y)**2)
max_dist = np.sqrt(center_x**2 + center_y**2)
# Normalize to depth range [1.0, 6.0] meters
depth_img = 1.5 + 4.5 * (dist_from_center / max_dist)
# Add noise for realism
noise = np.random.normal(0, 0.1, depth_img.shape)
depth_img = depth_img + noise
depth_img = np.clip(depth_img, 0.5, 6.0).astype(np.float32)
except Exception as e:
pass
# Final fallback: simple uniform depth
try:
height, width = self.resolution[1], self.resolution[0]
depth_img = np.full((height, width), 3.0, dtype=np.float32)
except Exception as e2:
pass
depth_img = np.full((480, 640), 3.0, dtype=np.float32)
# Check if RGB and depth images were obtained
if rgb_img is None:
self._log(f"[WARN] Failed to get RGB image")
rgb_img = np.zeros((480, 640, 3), dtype=np.uint8)
if depth_img is None:
self._log(f"[WARN] Failed to get depth image")
depth_img = np.full((480, 640), 3.0, dtype=np.float32)
# Process depth image
depth_img = depth_img.astype(np.float32)
depth_img = np.clip(depth_img, 0.1, 6.5)
return rgb_img, depth_img
except Exception as e:
self._log(f"[WARN] Failed to get RGB-D from Isaac Sim camera: {e}")
return None, None
else:
self._log(f"[WARN] No Isaac Sim camera available, returning default RGB-D")
rgb = np.zeros((480, 640, 3), dtype=np.uint8)
depth = np.full((480, 640), 5.0, dtype=np.float32)
return rgb, depth
def _check_collision(self, new_pos) -> bool:
"""
Collision detection: prefer 2D semantic map, fallback to Isaac Sim physics
"""
try:
# If collision detection disabled, return False
if hasattr(self, '_debug_disable_collision') and self._debug_disable_collision:
return False
# Prefer 2D semantic map collision detection
if self.collision_detector is not None:
is_collision = self.collision_detector.check_collision_3d(new_pos)
if is_collision:
self._collision_detected = True
self._total_collision_count += 1 # Increment total collision count (CR metric)
return True
else:
return False
else:
pass
# Fallback to Isaac Sim physics collision
return self._check_collision_isaac_sim(new_pos)
except Exception as e:
pass
# On error, fallback to Isaac Sim
return self._check_collision_isaac_sim(new_pos)
def _check_collision_isaac_sim(self, new_pos) -> bool:
"""
Original Isaac Sim physics collision detection (as fallback)
"""
try:
# Save current position
current_pos = self._pos.copy()
# Temporarily move to new position for collision check
self._pos = new_pos
self._apply_pose()
# Run physics step to handle collision
self.world.step(render=False)
# Check if collision occurred
# Method 1: Check if agent was pushed back
actual_pos = self._get_agent_physics_position()
if actual_pos is not None:
position_diff = np.linalg.norm(new_pos - actual_pos)
# Adjust threshold
if position_diff > 0.02:
# Restore original position
self._pos = current_pos
self._apply_pose()
return True
elif position_diff > 0.01:
pass # Allow movement but log
# Method 2: Additional safety check
expected_movement = np.linalg.norm(new_pos[:2] - current_pos[:2])
actual_movement = np.linalg.norm(actual_pos[:2] - current_pos[:2]) if actual_pos is not None else 0
movement_ratio = actual_movement / expected_movement if expected_movement > 0.001 else 1.0
if movement_ratio < 0.5:
if expected_movement > 0.05:
print(f"[ISAAC_COLLISION_BLOCKED] Movement severely restricted by physics")
# Restore original position
self._pos = current_pos
self._apply_pose()
return True
# If no collision detected, keep new position
return False
except Exception as e:
pass
# Restore original position
self._pos = current_pos if 'current_pos' in locals() else self._pos
self._apply_pose()
return False
def _check_path_collision(self, start_pos: np.ndarray, end_pos: np.ndarray) -> bool:
"""Check if there are collision bodies on path."""
try:
# Sample points along path to check collision
num_samples = 5
for i in range(1, num_samples + 1):
t = i / (num_samples + 1)
sample_pos = start_pos * (1 - t) + end_pos * t
# Check if sample point is in collision body
if self._check_point_collision(sample_pos):
print(f"[PATH_COLLISION] Sample point {i} at {sample_pos[:2]} is in collision")
return True
return False
except Exception as e:
print(f"[WARN] Path collision check failed: {e}")
return False
def _check_point_collision(self, pos: np.ndarray) -> bool:
"""Check if specific point is in collision body."""
try:
# Use Isaac Sim ray casting for collision check
if hasattr(self, 'world') and self.world:
# Cast ray downward to check ground collision
ray_start = pos + np.array([0, 0, 0.5])
ray_end = pos + np.array([0, 0, -0.5])
# Can add more complex collision detection logic
# Return False for now, let physics handle it
return False
return False
except Exception as e:
print(f"[WARN] Point collision check failed: {e}")
return False
def _get_agent_physics_position(self) -> Optional[np.ndarray]:
"""Get agent actual position in physics system."""
try:
if hasattr(self, 'agent_prim') and self.agent_prim:
# Get agent prim transform
xformable = self._UsdGeom.Xformable(self.agent_prim)
if xformable:
# Get world transform matrix
world_transform = xformable.ComputeLocalToWorldTransform(0)
if world_transform:
# Extract position
translation = world_transform.ExtractTranslation()
return np.array([translation[0], translation[1], translation[2]], dtype=np.float32)
return None
except Exception as e:
print(f"[WARN] Failed to get agent physics position: {e}")
return None
def apply_cmd_for(self, vx: float, vy: float, yaw_rate: float, duration_s: float) -> None:
"""
Improved motion control with smart sliding algorithm
"""
_debug_print(f"[DEBUG] apply_cmd_for start: vx={vx:.3f}, vy={vy:.3f}, yaw_rate={yaw_rate:.3f}, current_yaw={math.degrees(self._yaw):.1f}°")
self._log(f"[DEBUG] apply_cmd_for start: vx={vx:.3f}, vy={vy:.3f}, yaw_rate={yaw_rate:.3f}, current_yaw={math.degrees(self._yaw):.1f}°")
# Calculate total movement - transform from robot to world coords
# VLM output: vx=forward, vy=lateral (relative to robot)
# Need to transform to world coords: X=East, Y=North
cos_yaw = math.cos(self._yaw)
sin_yaw = math.sin(self._yaw)
# Coordinate transform: robot -> world
# Robot forward (vx) -> world movement vector
world_vx = vx * cos_yaw - vy * sin_yaw
world_vy = vx * sin_yaw + vy * cos_yaw
total_dx = world_vx * duration_s
total_dy = world_vy * duration_s
total_dyaw = yaw_rate * duration_s
_debug_print(f"[DEBUG] Total movement: dx={total_dx:.3f}, dy={total_dy:.3f}, dyaw={math.degrees(total_dyaw):.1f}°")
# Record original position
old_pos = self._pos.copy()
intended_movement = np.linalg.norm([total_dx, total_dy])
if intended_movement > 0.001:
# Calculate target position
target_pos = old_pos.copy()
target_pos[0] += total_dx
target_pos[1] += total_dy
# Use safe gradual movement with collision detection
actual_movement = self._safe_gradual_movement(old_pos, target_pos, intended_movement)
# Check movement efficiency and update collision counter
movement_efficiency = actual_movement / intended_movement if intended_movement > 0 else 1.0
if movement_efficiency < 0.3 and intended_movement > 0.05:
self.consecutive_collisions += 1
if self.consecutive_collisions >= 3:
pass
else:
# Successful movement, reset collision counter
if movement_efficiency > 0.6:
if self.consecutive_collisions > 0:
pass
self.consecutive_collisions = 0
else:
# No movement (e.g. turning), dont reset collision counter
pass
# Update orientation
old_yaw = self._yaw
self._yaw += total_dyaw
self._yaw = ((self._yaw + math.pi) % (2 * math.pi)) - math.pi
# Update camera position for new orientation
self._update_camera_position()
_debug_print(f"[DEBUG] Finished env.apply_cmd_for")
self._log(f"[DEBUG] Finished env.apply_cmd_for")
def _safe_gradual_movement(self, start_pos: np.ndarray, target_pos: np.ndarray, target_distance: float) -> float:
"""
Absolutely safe movement: strictly prevent clipping, use lateral exploration to avoid stuck.
Args:
start_pos: Start position
target_pos: Target position
target_distance: Target distance
Returns:
Actual distance moved
"""
# Save current position
current_pos = self._pos.copy()
# Calculate movement direction
direction = target_pos - current_pos
direction_norm = np.linalg.norm(direction[:2])
# If movement too small, return
if direction_norm < 0.001:
return 0.0
# Normalize direction vector
unit_direction = direction / direction_norm
# Limit movement distance
max_distance = min(0.20, target_distance)
# Step 1: Try direct movement to target
direct_movement = self._try_direct_movement(current_pos, unit_direction, max_distance)
if direct_movement > 0.01:
return direct_movement
# Step 2: If direct failed, try lateral exploration
exploration_movement = self._try_exploration_movement(current_pos, unit_direction)
if exploration_movement > 0.005:
return exploration_movement
# Step 3: Cannot move at all
return 0.0
def _try_direct_movement(self, start_pos: np.ndarray, direction: np.ndarray, max_distance: float) -> float:
"""Try direct movement towards target."""
total_moved = 0.0
current_pos = start_pos.copy()
step_size = 0.01
while total_moved < max_distance:
step_distance = min(step_size, max_distance - total_moved)
next_pos = current_pos + direction * step_distance
# Strict collision pre-check
if self._is_position_safe(next_pos):
# Try movement
old_pos = self._pos.copy()
self._pos = next_pos
self._apply_pose()
self.world.step(render=False)
# Verify movement result
actual_pos = self._get_agent_physics_position()
if actual_pos is None:
actual_pos = self._pos
# Check if actually moved to expected position
position_diff = np.linalg.norm(actual_pos[:2] - next_pos[:2])
if position_diff < 0.002:
current_pos = actual_pos
self._pos = actual_pos
total_moved += step_distance
else:
# Physics blocked movement, restore and stop
self._pos = old_pos
self._apply_pose()
break
else:
# Collision detected, stop movement
break
actual_movement = np.linalg.norm(current_pos[:2] - start_pos[:2])
return actual_movement
def _try_exploration_movement(self, start_pos: np.ndarray, blocked_direction: np.ndarray) -> float:
"""Try lateral exploration when direct movement blocked."""
# Generate lateral exploration directions
perpendicular = np.array([-blocked_direction[1], blocked_direction[0], 0])
exploration_directions = [
perpendicular,
-perpendicular,
perpendicular * 0.707 + blocked_direction * 0.707,
-perpendicular * 0.707 + blocked_direction * 0.707,
]
best_movement = 0.0
best_pos = start_pos.copy()
for i, direction in enumerate(exploration_directions):
direction_norm = np.linalg.norm(direction[:2])
if direction_norm > 0.001:
direction = direction / direction_norm
# Try short movement in this direction
movement = self._try_short_movement(start_pos, direction, 0.05)
if movement > best_movement:
best_movement = movement
best_pos = self._pos.copy()
# Apply best movement
if best_movement > 0.005:
self._pos = best_pos
self._apply_pose()
return best_movement
def _try_short_movement(self, start_pos: np.ndarray, direction: np.ndarray, max_distance: float) -> float:
"""Try short distance movement."""
step_size = 0.005 # 5mm
total_moved = 0.0
current_pos = start_pos.copy()
while total_moved < max_distance:
step_distance = min(step_size, max_distance - total_moved)
next_pos = current_pos + direction * step_distance
if self._is_position_safe(next_pos):
# Temporary test movement
old_pos = self._pos.copy()
self._pos = next_pos
self._apply_pose()
self.world.step(render=False)
actual_pos = self._get_agent_physics_position()
if actual_pos is None:
actual_pos = self._pos
position_diff = np.linalg.norm(actual_pos[:2] - next_pos[:2])
if position_diff < 0.002:
current_pos = actual_pos
total_moved += step_distance
else:
# Restore position, stop this direction
self._pos = old_pos
self._apply_pose()
break
else:
break
return total_moved
def _is_position_safe(self, pos: np.ndarray) -> bool:
"""Strictly check if position is safe (no collision)."""
try:
# 2D semantic map check
if self.collision_detector is not None:
if self.collision_detector.check_collision_3d(pos):
return False
# Additional physics pre-check (optional)
# Can add more check logic here
return True
except Exception as e:
pass
return False
def _smart_slide_movement_deprecated(self, start_pos: np.ndarray, dx: float, dy: float) -> Tuple[np.ndarray, float]:
"""
Smart sliding algorithm: try to move to closest collision-free position.
Args:
start_pos: Start position
dx, dy: Expected movement vector
Returns:
Tuple[final_position, actual_distance]
"""
intended_distance = np.linalg.norm([dx, dy])
if intended_distance < 0.001:
return start_pos.copy(), 0.0
# Normalize movement direction
move_direction = np.array([dx, dy]) / intended_distance
print(f"[SMART_SLIDE] Starting smart slide: intended={intended_distance:.3f}m, direction={move_direction}")
# Strategy 1: Binary search for max feasible distance
print(f"[SMART_SLIDE] Trying binary search for direct movement...")
max_distance = self._binary_search_max_distance(start_pos, move_direction, intended_distance)
if max_distance > 0.01:
final_pos = start_pos.copy()
final_pos[0] += move_direction[0] * max_distance
final_pos[1] += move_direction[1] * max_distance
print(f"[SMART_SLIDE] Binary search success: max_distance={max_distance:.3f}m")
return final_pos, max_distance
# Strategy 2: If direct failed, try sliding along obstacle
print(f"[SMART_SLIDE] Direct movement blocked, trying sliding along obstacles")
slide_pos, slide_distance = self._try_obstacle_sliding(start_pos, move_direction, intended_distance)
if slide_distance > 0.005:
print(f"[SMART_SLIDE] Obstacle sliding success: distance={slide_distance:.3f}m")
return slide_pos, slide_distance
# Strategy 3: Try small multi-direction exploration
print(f"[SMART_SLIDE] Trying multi-directional micro-movements")
explore_pos, explore_distance = self._try_micro_exploration(start_pos, move_direction, min(intended_distance, 0.05))
if explore_distance > 0.002:
print(f"[SMART_SLIDE] Micro-exploration success: distance={explore_distance:.3f}m")
return explore_pos, explore_distance
# Strategy 4: Ultimate escape - try minimal forced movement
print(f"[SMART_SLIDE] All strategies failed, trying ultra-micro movements...")
ultra_micro_pos, ultra_micro_distance = self._try_ultra_micro_escape(start_pos, move_direction)
print(f"[SMART_SLIDE_DEBUG] ultra_micro_distance={ultra_micro_distance:.10f}, checking >= 0.001")
if ultra_micro_distance >= 0.0009:
print(f"[SMART_SLIDE] Ultra-micro escape success: distance={ultra_micro_distance:.4f}m")
return ultra_micro_pos, ultra_micro_distance
# Should never reach here! Force movement
print(f"[SMART_SLIDE] EMERGENCY: All strategies failed, ultra_micro_distance={ultra_micro_distance:.6f}m")
print(f"[SMART_SLIDE] EMERGENCY: Forcing 1mm movement in ANY direction!")
# Force movement - must not return 0
emergency_pos = start_pos.copy()
emergency_pos[0] += 0.001
return emergency_pos, 0.001
def _binary_search_max_distance(self, start_pos: np.ndarray, direction: np.ndarray, max_distance: float) -> float:
"""
Use binary search to find max movable distance in given direction
"""
min_dist = 0.0
max_dist = min(0.1, max_distance)
best_dist = 0.0
# Binary search, 1mm precision
for _ in range(20):
test_dist = (min_dist + max_dist) / 2.0
if test_dist < 0.001:
break
test_pos = start_pos.copy()
test_pos[0] += direction[0] * test_dist
test_pos[1] += direction[1] * test_dist
if self._test_position_safety(start_pos, test_pos):
best_dist = test_dist
min_dist = test_dist
else:
max_dist = test_dist
return best_dist
def _try_obstacle_sliding(self, start_pos: np.ndarray, primary_direction: np.ndarray, intended_distance: float) -> Tuple[np.ndarray, float]:
"""
Try sliding along obstacle edge
"""
# Calculate two perpendicular directions
perp_left = np.array([-primary_direction[1], primary_direction[0]])
perp_right = np.array([primary_direction[1], -primary_direction[0]])
best_pos = start_pos.copy()
best_distance = 0.0
# Try different sliding strategies
slide_distances = [0.02, 0.05, 0.1, 0.15] # 2cm, 5cm, 10cm, 15cm
for slide_dist in slide_distances:
if slide_dist > intended_distance:
slide_dist = intended_distance
# Try left slide
for perp_ratio in [0.3, 0.5, 0.7, 1.0]:
slide_direction = primary_direction * (1 - perp_ratio) + perp_left * perp_ratio
slide_direction = slide_direction / np.linalg.norm(slide_direction)
test_pos = start_pos.copy()
test_pos[0] += slide_direction[0] * slide_dist
test_pos[1] += slide_direction[1] * slide_dist
if self._test_position_safety(start_pos, test_pos):
actual_distance = np.linalg.norm(test_pos[:2] - start_pos[:2])
if actual_distance > best_distance:
best_pos = test_pos
best_distance = actual_distance
# Try right slide
for perp_ratio in [0.3, 0.5, 0.7, 1.0]:
slide_direction = primary_direction * (1 - perp_ratio) + perp_right * perp_ratio
slide_direction = slide_direction / np.linalg.norm(slide_direction)
test_pos = start_pos.copy()
test_pos[0] += slide_direction[0] * slide_dist
test_pos[1] += slide_direction[1] * slide_dist
if self._test_position_safety(start_pos, test_pos):
actual_distance = np.linalg.norm(test_pos[:2] - start_pos[:2])
if actual_distance > best_distance:
best_pos = test_pos
best_distance = actual_distance
return best_pos, best_distance
def _try_micro_exploration(self, start_pos: np.ndarray, primary_direction: np.ndarray, max_distance: float) -> Tuple[np.ndarray, float]:
"""
Try small multi-direction exploration
"""
best_pos = start_pos.copy()
best_distance = 0.0
# Try multiple angle offsets (-45 to +45 degrees)
angle_offsets = [-45, -30, -15, 0, 15, 30, 45]
distances = [max_distance * 0.2, max_distance * 0.5, max_distance * 0.8, max_distance]
for angle_deg in angle_offsets:
angle_rad = math.radians(angle_deg)
# Rotate main direction
cos_a, sin_a = math.cos(angle_rad), math.sin(angle_rad)
rotated_direction = np.array([
primary_direction[0] * cos_a - primary_direction[1] * sin_a,
primary_direction[0] * sin_a + primary_direction[1] * cos_a
])
for test_distance in distances:
test_pos = start_pos.copy()
test_pos[0] += rotated_direction[0] * test_distance
test_pos[1] += rotated_direction[1] * test_distance
if self._test_position_safety(start_pos, test_pos):
actual_distance = np.linalg.norm(test_pos[:2] - start_pos[:2])
if actual_distance > best_distance:
best_pos = test_pos
best_distance = actual_distance
return best_pos, best_distance
def _try_ultra_micro_escape(self, start_pos: np.ndarray, primary_direction: np.ndarray) -> Tuple[np.ndarray, float]:
"""
Ultimate escape: try minimal forced movement, bypass safety check
"""
best_pos = start_pos.copy()
best_distance = 0.0
print(f"[ULTRA_MICRO] Attempting ultra-micro escape from complete blockage")
# Try 8 basic directions
directions = [
np.array([1.0, 0.0]),
np.array([-1.0, 0.0]),
np.array([0.0, 1.0]),
np.array([0.0, -1.0]),
np.array([0.707, 0.707]),
np.array([-0.707, 0.707]),
np.array([0.707, -0.707]),
np.array([-0.707, -0.707]),
primary_direction,
]
# Try minimal distances: 1mm, 2mm, 3mm, 5mm
micro_distances = [0.001, 0.002, 0.003, 0.005]
for direction in directions:
direction = direction / np.linalg.norm(direction)
for micro_dist in micro_distances:
test_pos = start_pos.copy()
test_pos[0] += direction[0] * micro_dist
test_pos[1] += direction[1] * micro_dist
# Use relaxed safety check or skip
safety_result = self._ultra_permissive_safety_check(start_pos, test_pos)
if safety_result:
actual_distance = np.linalg.norm(test_pos[:2] - start_pos[:2])
if actual_distance > best_distance:
best_pos = test_pos
best_distance = actual_distance
movement_mm = direction[:2] * micro_dist * 1000
print(f"[ULTRA_MICRO] Found viable micro-movement: [{movement_mm[0]:.1f}, {movement_mm[1]:.1f}]mm, distance={actual_distance:.4f}m")
print(f"[ULTRA_MICRO] Immediately returning: pos={best_pos[:2]}, distance={best_distance:.6f}m")
# Return once found, dont be greedy
return best_pos, best_distance
else:
print(f"[ULTRA_MICRO_DEBUG] Safety check failed for {micro_dist*1000:.1f}mm movement")
# If all directions fail, force minimal movement
if best_distance == 0.0:
print(f"[ULTRA_MICRO] Forcing minimal movement in primary direction")
forced_pos = start_pos.copy()
forced_pos[0] += primary_direction[0] * 0.001
forced_pos[1] += primary_direction[1] * 0.001
print(f"[ULTRA_MICRO] Forced movement: distance=0.001m")
return forced_pos, 0.001
print(f"[ULTRA_MICRO] Returning best movement: distance={best_distance:.4f}m")
return best_pos, best_distance
def _ultra_permissive_safety_check(self, start_pos: np.ndarray, test_pos: np.ndarray) -> bool:
"""
Ultra relaxed safety check for extreme stuck situations
"""
try:
# For minimal movement, basically pass
movement_distance = np.linalg.norm(test_pos[:2] - start_pos[:2])
# If distance < 3mm, pass directly
if movement_distance < 0.003:
return True
# For slightly larger movement (<6mm), do simple check
if movement_distance < 0.006:
return True
return False
except Exception as e:
print(f"[ULTRA_PERMISSIVE_ERROR] {e}")
# On error, pass for minimal movement
movement_distance = np.linalg.norm(test_pos[:2] - start_pos[:2])
return movement_distance < 0.002
def _test_position_safety(self, start_pos: np.ndarray, test_pos: np.ndarray) -> bool:
"""
Test if position is safe (no collision).
Uses lightweight collision detection to avoid performance impact.
"""
try:
# If collision detection disabled, return True
if hasattr(self, '_debug_disable_collision') and self._debug_disable_collision:
return True
# Basic boundary check
if abs(test_pos[0]) > 20 or abs(test_pos[1]) > 20 or test_pos[2] < 0 or test_pos[2] > 5:
return False
# Temporarily save current position
original_pos = self._pos.copy()
# Move to test position
self._pos = test_pos
self._apply_pose()
# Run one physics step
self.world.step(render=False)
# Check actual position
actual_pos = self._get_agent_physics_position()
if actual_pos is None:
# Restore position
self._pos = original_pos
self._apply_pose()
return False
# Calculate position deviation
position_diff = np.linalg.norm(test_pos - actual_pos)
# Restore original position (test, must restore)
self._pos = original_pos
self._apply_pose()
# Restore relaxed collision detection
is_safe = position_diff < 0.03
if not is_safe and position_diff > 0.05:
pass
return is_safe
except Exception as e:
print(f"[SAFETY_TEST_ERROR] {e}")
# Ensure position restored
self._pos = start_pos
self._apply_pose()
return False
def _apply_gradual_movement(self, start_pos: np.ndarray, target_pos: np.ndarray, target_distance: float) -> float:
"""
Force movement: try to move to target while respecting collision.
Args:
start_pos: Start position
target_pos: Target position
target_distance: Target distance
Returns:
Actual distance moved
"""
# Save current position
current_pos = self._pos.copy()
# Calculate movement direction
direction = target_pos - current_pos
direction_norm = np.linalg.norm(direction[:2])
# If movement too small, return
if direction_norm < 0.001:
return 0.0
# Normalize direction vector
unit_direction = direction / direction_norm
# Limit max distance to 25cm
max_distance = min(0.25, target_distance)
# Try movement in 5cm steps
total_moved = 0.0
step_size = 0.05
collision_detected = False
while total_moved < max_distance:
# Calculate next position
step_distance = min(step_size, max_distance - total_moved)
next_pos = current_pos + unit_direction * step_distance
# Temporarily save current position
temp_pos = self._pos.copy()
# First check 2D semantic map collision
if self.collision_detector is not None:
is_2d_collision = self.collision_detector.check_collision_3d(next_pos)
if is_2d_collision:
collision_detected = True
break
else:
pass
# Try movement to next position
self._pos = next_pos
# Apply position update
self._apply_pose()
# Run physics step
self.world.step(render=False)
# Get actual position (may be modified by physics)
actual_pos = self._get_agent_physics_position()
if actual_pos is None:
actual_pos = self._pos
# Calculate actual movement distance
actual_movement = np.linalg.norm(actual_pos[:2] - current_pos[:2])
# Check for obvious Isaac Sim physics collision
position_diff = np.linalg.norm(actual_pos[:2] - next_pos[:2])
if position_diff > 0.02:
# Restore to last valid position
self._pos = temp_pos
self._apply_pose()
collision_detected = True
break
# Update current position and distance moved
current_pos = actual_pos
total_moved += step_distance
# Calculate total actual distance moved
actual_movement = np.linalg.norm(current_pos[:2] - start_pos[:2])
# Detect stuck state
expected_movement = target_distance
movement_efficiency = actual_movement / expected_movement if expected_movement > 0 else 1.0
if movement_efficiency < 0.3 and expected_movement > 0.05:
self.consecutive_collisions += 1
if self.consecutive_collisions >= 3:
pass
else:
# Successful movement, reset collision counter
if movement_efficiency > 0.6:
if self.consecutive_collisions > 0:
pass
self.consecutive_collisions = 0
return actual_movement
def get_yaw(self) -> float:
return float(self._yaw)
def get_collision_count(self) -> int:
"""Get total collision count for current episode (CR metric)."""
return getattr(self, '_total_collision_count', 0)
def set_collision_detection(self, enabled: bool) -> None:
"""Enable or disable collision detection."""
old_value = getattr(self, '_debug_disable_collision', False)
self._debug_disable_collision = not enabled
def debug_pose(self) -> Dict[str, Any]:
"""Debug current pose information"""
return {
"position": self._pos.tolist(),
"yaw_rad": self._yaw,
"yaw_deg": math.degrees(self._yaw),
"quaternion": [-math.sin(self._yaw/2.0), 0.0, 0.0, math.cos(self._yaw/2.0)] # Your mapping
}
def transform_coordinates(self, position: List[float], rotation_xyzw: List[float]) -> Tuple[np.ndarray, float]:
"""
Transform coordinates from your trajectory system to Isaac Sim system.
This method helps debug coordinate system mismatches.
"""
# Your trajectory data might use a different coordinate system
# This method helps identify the transformation needed
# Extract yaw from quaternion
x, y, z, w = rotation_xyzw
yaw = math.atan2(2*(w*z + x*y), 1 - 2*(y*y + z*z))
# For debugging, let's see what the transformation looks like
# You might need to adjust these based on your coordinate mapping
transformed_pos = np.array(position, dtype=np.float32)
transformed_yaw = yaw
return transformed_pos, transformed_yaw
@staticmethod
def write_video(frames: List[np.ndarray], out_path: str, fps: int = 10) -> None:
Path(out_path).parent.mkdir(parents=True, exist_ok=True)
seq_dir = Path(out_path).with_suffix("")
seq_dir.mkdir(parents=True, exist_ok=True)
# Normalize frames to uint8 RGB
norm_frames: List[np.ndarray] = []
for f in frames:
arr = f.astype(np.uint8)
if arr.ndim == 3 and arr.shape[2] == 4:
arr = arr[:, :, :3]
norm_frames.append(arr)
# 0) Prefer imageio-ffmpeg explicitly (libx264)
mp4_ok = False
try:
writer = imageio.get_writer(
str(out_path), fps=fps, format="FFMPEG", codec="libx264",
quality=8, bitrate="8M",
macro_block_size=None,
output_params=["-pix_fmt", "yuv420p"] # 确保兼容性
)
for fr in norm_frames:
writer.append_data(fr)
writer.close()
mp4_ok = os.path.exists(out_path) and os.path.getsize(out_path) > 0
except Exception:
mp4_ok = False
# 1) Try OpenCV if imageio path failed
if not mp4_ok:
try:
import cv2 # type: ignore
if len(norm_frames) > 0:
h, w = norm_frames[0].shape[:2]
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
vw = cv2.VideoWriter(str(out_path), fourcc, float(fps), (w, h))
for fr in norm_frames:
if fr.shape[0] != h or fr.shape[1] != w:
fr = cv2.resize(fr, (w, h))
bgr = cv2.cvtColor(fr, cv2.COLOR_RGB2BGR)
vw.write(bgr)
vw.release()
mp4_ok = os.path.exists(out_path) and os.path.getsize(out_path) > 0
except Exception:
mp4_ok = False
# 2) Always write PNG sequence
for i, fr in enumerate(norm_frames):
imageio.imwrite(str(seq_dir / f"frame_{i:05d}.png"), fr)
def _verify_agent_physics(self) -> None:
"""Verify agent physics configuration."""
print(f"[PHYSICS_VERIFY] Checking agent physics configuration...")
try:
# Check if agent prim exists
agent_prim = self.stage.GetPrimAtPath(self.agent_prim_path)
if not agent_prim.IsValid():
print(f"[PHYSICS_ERROR] Agent prim not found at {self.agent_prim_path}")
return
print(f"[PHYSICS_VERIFY] Agent prim found: {self.agent_prim_path}")
# Check if collision cylinder exists
collision_path = self.agent_prim_path + "/CollisionCylinder"
collision_prim = self.stage.GetPrimAtPath(collision_path)
if not collision_prim.IsValid():
print(f"[PHYSICS_ERROR] Collision cylinder not found at {collision_path}")
return
print(f"[PHYSICS_VERIFY] Collision cylinder found: {collision_path}")
# Check RigidBody attributes
if agent_prim.HasAPI(self._UsdPhysics.RigidBodyAPI):
rigid_body = self._UsdPhysics.RigidBodyAPI(agent_prim)
kinematic = rigid_body.GetKinematicEnabledAttr().Get()
enabled = rigid_body.GetRigidBodyEnabledAttr().Get()
print(f"[PHYSICS_VERIFY] RigidBody: enabled={enabled}, kinematic={kinematic}")
else:
print(f"[PHYSICS_ERROR] RigidBodyAPI not applied to agent")
# Check Collision attributes
if collision_prim.HasAPI(self._UsdPhysics.CollisionAPI):
collision_api = self._UsdPhysics.CollisionAPI(collision_prim)
collision_enabled = collision_api.GetCollisionEnabledAttr().Get()
print(f"[PHYSICS_VERIFY] Collision: enabled={collision_enabled}")
else:
print(f"[PHYSICS_ERROR] CollisionAPI not applied to collision cylinder")
# Check cylinder geometry attributes
cylinder = self._UsdGeom.Cylinder(collision_prim)
radius = cylinder.GetRadiusAttr().Get()
height = cylinder.GetHeightAttr().Get()
print(f"[PHYSICS_VERIFY] Cylinder: radius={radius}m, height={height}m")
# Check agent position
pos = self._get_agent_physics_position()
if pos is not None:
print(f"[PHYSICS_VERIFY] Agent position: {pos}")
print(f"[PHYSICS_VERIFY] Agent height (z): {pos[2]}m")
if pos[2] < 0.7 or pos[2] > 0.9:
print(f"[PHYSICS_WARN] Agent center height {pos[2]}m may not be correct (expected ~0.8m for 1.6m tall agent)")
else:
print(f"[PHYSICS_ERROR] Could not get agent position")
print(f"[PHYSICS_VERIFY] Physics verification complete")
except Exception as e:
print(f"[PHYSICS_ERROR] Failed to verify physics: {e}")
import traceback
traceback.print_exc()
def _setup_physics_scene(self) -> None:
"""Setup physics scene for collision detection."""
try:
print(f"[PHYSICS_SETUP] Setting up physics scene...")
# Ensure physics scene exists
from pxr import UsdPhysics
scene = UsdPhysics.Scene.Define(self.stage, "/physicsScene")
scene.CreateGravityDirectionAttr().Set((0.0, 0.0, -1.0))
scene.CreateGravityMagnitudeAttr().Set(981.0) # cm/s^2
print(f"[PHYSICS_SETUP] Created physics scene with gravity")
# Add default material
material_path = "/physicsMaterial"
if not self.stage.GetPrimAtPath(material_path):
material = UsdPhysics.MaterialAPI.Apply(
self.stage.DefinePrim(material_path, "Material")
)
material.CreateStaticFrictionAttr().Set(0.5)
material.CreateDynamicFrictionAttr().Set(0.5)
material.CreateRestitutionAttr().Set(0.0)
print(f"[PHYSICS_SETUP] Created default physics material")
print(f"[PHYSICS_SETUP] Physics scene setup complete")
except Exception as e:
print(f"[PHYSICS_SETUP_ERROR] Failed to setup physics scene: {e}")
import traceback
traceback.print_exc()
def _verify_collision_system(self) -> None:
"""Verify entire collision system works correctly."""
print(f"[COLLISION_VERIFY] Verifying collision system...")
try:
# Check physics scene
scene_prim = self.stage.GetPrimAtPath("/physicsScene")
if scene_prim.IsValid():
print(f"[COLLISION_VERIFY] Physics scene found: /physicsScene")
else:
print(f"[COLLISION_ERROR] Physics scene not found!")
# Check agent collision body
collision_path = self.agent_prim_path + "/CollisionCylinder"
collision_prim = self.stage.GetPrimAtPath(collision_path)
if collision_prim.IsValid():
print(f"[COLLISION_VERIFY] Agent collision body found: {collision_path}")
# Check collision body transform
collision_xform = self._UsdGeom.Xformable(collision_prim)
local_transform = collision_xform.GetLocalTransformation()
print(f"[COLLISION_VERIFY] Collision body transform: {local_transform}")
else:
print(f"[COLLISION_ERROR] Agent collision body not found!")
# Check other collision bodies in scene
collision_count = 0
for prim in self.stage.Traverse():
if prim.HasAPI(self._UsdPhysics.CollisionAPI):
collision_count += 1
if collision_count <= 5:
print(f"[COLLISION_VERIFY] Found collision object: {prim.GetPath()}")
print(f"[COLLISION_VERIFY] Total collision objects in scene: {collision_count}")
if collision_count < 2:
print(f"[COLLISION_WARN] Very few collision objects found. Scene may not have proper collision setup.")
print(f"[COLLISION_VERIFY] Collision system verification complete")
except Exception as e:
print(f"[COLLISION_VERIFY_ERROR] Failed to verify collision system: {e}")
import traceback
traceback.print_exc()
def _enhanced_collision_check(self, old_pos: np.ndarray, new_pos: np.ndarray) -> bool:
"""
Enhanced collision detection using multiple methods
"""
try:
# Method 1: Temporarily move and check physics response
temp_pos = self._pos.copy()
self._pos = new_pos
self._apply_pose()
# Run physics to see if blocked
self.world.step(render=False)
# Check actual position
actual_pos = self._get_agent_physics_position()
if actual_pos is not None:
# Calculate expected vs actual position difference
position_diff = np.linalg.norm(new_pos - actual_pos)
if position_diff > 0.001:
print(f"[COLLISION_CHECK] Expected: {new_pos[:2]}, Actual: {actual_pos[:2]}, Diff: {position_diff:.4f}")
# If large difference, collision occurred
if position_diff > 0.03:
self._pos = temp_pos # Restore original position
self._apply_pose()
return True
# Method 2: Check height change
if actual_pos is not None:
height_diff = abs(actual_pos[2] - new_pos[2])
if height_diff > 0.5:
print(f"[COLLISION_HEIGHT] Unexpected height change: {height_diff:.4f}m")
self._pos = temp_pos
self._apply_pose()
return True
# Method 3: Check if pushed to different position
movement_intended = np.linalg.norm(new_pos[:2] - old_pos[:2])
movement_actual = np.linalg.norm(actual_pos[:2] - old_pos[:2]) if actual_pos is not None else movement_intended
if movement_intended > 0.05:
movement_ratio = movement_actual / movement_intended
if movement_ratio < 0.05:
print(f"[COLLISION_MOVEMENT] Movement severely restricted: ratio={movement_ratio:.2f}")
self._pos = temp_pos
self._apply_pose()
return True
# If all checks pass, allow movement
return False
except Exception as e:
print(f"[COLLISION_CHECK_ERROR] {e}")
# On error, restore position, block movement
self._pos = old_pos
self._apply_pose()
return True
def _verify_collision_system(self) -> None:
"""Verify entire collision system works correctly."""
print(f"[COLLISION_VERIFY] Verifying collision system...")
try:
# Check physics scene
scene_prim = self.stage.GetPrimAtPath("/physicsScene")
if scene_prim.IsValid():
print(f"[COLLISION_VERIFY] Physics scene found: /physicsScene")
else:
print(f"[COLLISION_ERROR] Physics scene not found!")
# Check agent collision body
collision_path = self.agent_prim_path + "/CollisionCylinder"
collision_prim = self.stage.GetPrimAtPath(collision_path)
if collision_prim.IsValid():
print(f"[COLLISION_VERIFY] Agent collision body found: {collision_path}")
# Check collision body transform
collision_xform = self._UsdGeom.Xformable(collision_prim)
local_transform = collision_xform.GetLocalTransformation()
print(f"[COLLISION_VERIFY] Collision body transform: {local_transform}")
else:
print(f"[COLLISION_ERROR] Agent collision body not found!")
# Check other collision bodies in scene
collision_count = 0
for prim in self.stage.Traverse():
if prim.HasAPI(self._UsdPhysics.CollisionAPI):
collision_count += 1
if collision_count <= 5:
print(f"[COLLISION_VERIFY] Found collision object: {prim.GetPath()}")
print(f"[COLLISION_VERIFY] Total collision objects in scene: {collision_count}")
if collision_count < 2:
print(f"[COLLISION_WARN] Very few collision objects found. Scene may not have proper collision setup.")
print(f"[COLLISION_VERIFY] Collision system verification complete")
except Exception as e:
print(f"[COLLISION_VERIFY_ERROR] Failed to verify collision system: {e}")
import traceback
traceback.print_exc()
def _enhanced_collision_check(self, old_pos: np.ndarray, new_pos: np.ndarray) -> bool:
"""
Enhanced collision detection using multiple methods
"""
try:
# Method 1: Temporarily move and check physics response
temp_pos = self._pos.copy()
self._pos = new_pos
self._apply_pose()
# Run physics to see if blocked
self.world.step(render=False)
# Check actual position
actual_pos = self._get_agent_physics_position()
if actual_pos is not None:
# Calculate expected vs actual position difference
position_diff = np.linalg.norm(new_pos - actual_pos)
if position_diff > 0.001:
print(f"[COLLISION_CHECK] Expected: {new_pos[:2]}, Actual: {actual_pos[:2]}, Diff: {position_diff:.4f}")
# If large difference, collision occurred
if position_diff > 0.03:
self._pos = temp_pos # Restore original position
self._apply_pose()
return True
# Method 2: Check height change
if actual_pos is not None:
height_diff = abs(actual_pos[2] - new_pos[2])
if height_diff > 0.5:
print(f"[COLLISION_HEIGHT] Unexpected height change: {height_diff:.4f}m")
self._pos = temp_pos
self._apply_pose()
return True
# Method 3: Check if pushed to different position
movement_intended = np.linalg.norm(new_pos[:2] - old_pos[:2])
movement_actual = np.linalg.norm(actual_pos[:2] - old_pos[:2]) if actual_pos is not None else movement_intended
if movement_intended > 0.05:
movement_ratio = movement_actual / movement_intended
if movement_ratio < 0.05:
print(f"[COLLISION_MOVEMENT] Movement severely restricted: ratio={movement_ratio:.2f}")
self._pos = temp_pos
self._apply_pose()
return True
# If all checks pass, allow movement
return False
except Exception as e:
print(f"[COLLISION_CHECK_ERROR] {e}")
# On error, restore position, block movement
self._pos = old_pos
self._apply_pose()
return True