File size: 2,072 Bytes
ef6a683 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
import time
from typing import Any, Dict
from unitree_sdk2py.core.channel import ChannelFactoryInitialize
from .base_sim import BaseSimulator
def init_channel(config: Dict[str, Any]) -> None:
"""
Initialize the communication channel for simulator/robot communication.
Args:
config: Configuration dictionary containing DOMAIN_ID and optionally INTERFACE
"""
if config.get("INTERFACE", None):
ChannelFactoryInitialize(config["DOMAIN_ID"], config["INTERFACE"])
else:
ChannelFactoryInitialize(config["DOMAIN_ID"])
class SimulatorFactory:
"""Factory class for creating different types of simulators."""
@staticmethod
def create_simulator(config: Dict[str, Any], env_name: str = "default", **kwargs):
"""
Create a simulator based on the configuration.
Args:
config: Configuration dictionary containing SIMULATOR type
env_name: Environment name
**kwargs: Additional keyword arguments for specific simulators
"""
simulator_type = config.get("SIMULATOR", "mujoco")
if simulator_type == "mujoco":
return SimulatorFactory._create_mujoco_simulator(config, env_name, **kwargs)
else:
print(
f"Warning: Invalid simulator type: {simulator_type}. "
"If you are using run_sim_loop, please ignore this warning."
)
return None
@staticmethod
def _create_mujoco_simulator(config: Dict[str, Any], env_name: str = "default", **kwargs):
"""Create a MuJoCo simulator instance."""
camera_configs = kwargs.pop("camera_configs", {})
if len(camera_configs) > 0:
print(f"Debug: SimulatorFactory received {len(camera_configs)} camera config(s)")
env_kwargs = dict(
onscreen=kwargs.pop("onscreen", True),
offscreen=kwargs.pop("offscreen", False),
camera_configs=camera_configs,
)
return BaseSimulator(config=config, env_name=env_name, **env_kwargs)
|