当前位置: 首页 > news >正文

导入模型文件到robosuite的Demo场景,形成自己的场景

找到需要的模型文件,格式如:YCB_Dataset/ycb/lemon at main · elpis-lab/YCB_Dataset

可使用:https://github.com/elpis-lab/YCB_Dataset/tree/main/ycb/lemon复制网址下载github单个文件夹。

xml存放路径大致如下:/home/用户名/anaconda3/envs/robosuite/lib/python3.10/site-packages/robosuite/models/assets/objects

将下载的文件夹解压并放入上述文件夹中,并新建一个.xml文本

xml文件如下,自行替换路径:

这里需要根据文件夹中的textured.obj文件来自行替换下面的原点、重心等细节信息,出现模型一碰就飞或者一碰就翻面等情况大都考虑重心设置问题。

<?xml version="1.0" ?> <mujoco model="b_cup"> <asset> <texture name="cup_tex" type="2d" file="b_cup/texture_map.png"/> <material name="cup_mat" texture="cup_tex" specular="0.3" shininess="0.2"/> <mesh name="cup_visual_mesh" file="b_cup/textured.obj"/> <mesh name="cup_col_mesh_0" file="b_cup/textured_coacd_0.stl"/> <mesh name="cup_col_mesh_1" file="b_cup/textured_coacd_1.stl"/> <mesh name="cup_col_mesh_2" file="b_cup/textured_coacd_2.stl"/> <mesh name="cup_col_mesh_3" file="b_cup/textured_coacd_3.stl"/> <mesh name="cup_col_mesh_4" file="b_cup/textured_coacd_4.stl"/> </asset> <worldbody> <body> <body name="object"> <inertial pos="0 0 0.0" mass="0.15" diaginertia="0.0001 0.0001 0.0001"/> <geom type="mesh" mesh="cup_visual_mesh" material="cup_mat" contype="0" conaffinity="0" group="1"/> <geom type="mesh" mesh="cup_col_mesh_0" rgba="0 1 0 0" contype="1" conaffinity="1" group="0"/> <geom type="mesh" mesh="cup_col_mesh_1" rgba="0 1 0 0" contype="1" conaffinity="1" group="0"/> <geom type="mesh" mesh="cup_col_mesh_2" rgba="0 1 0 0" contype="1" conaffinity="1" group="0"/> <geom type="mesh" mesh="cup_col_mesh_3" rgba="0 1 0 0" contype="1" conaffinity="1" group="0"/> <geom type="mesh" mesh="cup_col_mesh_4" rgba="0 1 0 0" contype="1" conaffinity="1" group="0"/> </body> <site name="object_site" pos="0 0 0.05" size="0.005" rgba="1 0 0 1" group="2"/> <site rgba="0 0 0 0" size="0.005" pos="0 0 0.0" name="bottom_site"/> <site rgba="0 0 0 0" size="0.005" pos="0 0 0.1" name="top_site"/> <site rgba="0 0 0 0" size="0.005" pos="0.04 0.04 0.05" name="horizontal_radius_site"/> </body> </worldbody> </mujoco>

场景路径大致如下:/home/用户名/anaconda3/envs/robosuite/lib/python3.10/site-packages/robosuite/environments/manipulation

以Lift场景为例,复制一份并修改后的Lift2.py代码如下:

主要添加:开头的类、装饰器、def _load_model和def _setup_references里的定义加载和打包,打包后self.objects对原先参数的替换,以及def _setup_observables中新增传感器和夹爪距离。

from collections import OrderedDict import numpy as np from robosuite.environments.manipulation.manipulation_env import ManipulationEnv from robosuite.models.arenas import TableArena from robosuite.models.objects import BoxObject, MujocoXMLObject from robosuite.models.tasks import ManipulationTask from robosuite.utils.mjcf_utils import CustomMaterial, xml_path_completion from robosuite.utils.observables import Observable, sensor from robosuite.utils.placement_samplers import UniformRandomSampler from robosuite.utils.transform_utils import convert_quat from robosuite.environments.base import register_env # ----------------- 【步骤 A:声明 YCB 杯子的 Python 类】 ----------------- class YCBCupObject(MujocoXMLObject): def __init__(self, name): super().__init__( xml_path_completion("objects/b_cup.xml"), # 指向你的 cup.xml name=name, joints=[{"type": "free", "name": "object_joint"}], obj_type="all", ) @register_env # 👈 关键:加上这个装饰器,系统会自动把它吃进 REGISTERED_ENVS 字典中! class Lift2(ManipulationEnv): """ This class corresponds to the lifting task for a single robot arm. Args: robots (str or list of str): Specification for specific robot arm(s) to be instantiated within this env (e.g: "Sawyer" would generate one arm; ["Panda", "Panda", "Sawyer"] would generate three robot arms) Note: Must be a single single-arm robot! env_configuration (str): Specifies how to position the robots within the environment (default is "default"). For most single arm environments, this argument has no impact on the robot setup. controller_configs (str or list of dict): If set, contains relevant controller parameters for creating a custom controller. Else, uses the default controller for this specific task. Should either be single dict if same controller is to be used for all robots or else it should be a list of the same length as "robots" param gripper_types (str or list of str): type of gripper, used to instantiate gripper models from gripper factory. Default is "default", which is the default grippers(s) associated with the robot(s) the 'robots' specification. None removes the gripper, and any other (valid) model overrides the default gripper. Should either be single str if same gripper type is to be used for all robots or else it should be a list of the same length as "robots" param base_types (None or str or list of str): type of base, used to instantiate base models from base factory. Default is "default", which is the default base associated with the robot(s) the 'robots' specification. None results in no base, and any other (valid) model overrides the default base. Should either be single str if same base type is to be used for all robots or else it should be a list of the same length as "robots" param initialization_noise (dict or list of dict): Dict containing the initialization noise parameters. The expected keys and corresponding value types are specified below: :`'magnitude'`: The scale factor of uni-variate random noise applied to each of a robot's given initial joint positions. Setting this value to `None` or 0.0 results in no noise being applied. If "gaussian" type of noise is applied then this magnitude scales the standard deviation applied, If "uniform" type of noise is applied then this magnitude sets the bounds of the sampling range :`'type'`: Type of noise to apply. Can either specify "gaussian" or "uniform" Should either be single dict if same noise value is to be used for all robots or else it should be a list of the same length as "robots" param :Note: Specifying "default" will automatically use the default noise settings. Specifying None will automatically create the required dict with "magnitude" set to 0.0. table_full_size (3-tuple): x, y, and z dimensions of the table. table_friction (3-tuple): the three mujoco friction parameters for the table. use_camera_obs (bool): if True, every observation includes rendered image(s) use_object_obs (bool): if True, include object (cube) information in the observation. reward_scale (None or float): Scales the normalized reward function by the amount specified. If None, environment reward remains unnormalized reward_shaping (bool): if True, use dense rewards. placement_initializer (ObjectPositionSampler): if provided, will be used to place objects on every reset, else a UniformRandomSampler is used by default. has_renderer (bool): If true, render the simulation state in a viewer instead of headless mode. has_offscreen_renderer (bool): True if using off-screen rendering render_camera (str): Name of camera to render if `has_renderer` is True. Setting this value to 'None' will result in the default angle being applied, which is useful as it can be dragged / panned by the user using the mouse render_collision_mesh (bool): True if rendering collision meshes in camera. False otherwise. render_visual_mesh (bool): True if rendering visual meshes in camera. False otherwise. render_gpu_device_id (int): corresponds to the GPU device id to use for offscreen rendering. Defaults to -1, in which case the device will be inferred from environment variables (GPUS or CUDA_VISIBLE_DEVICES). control_freq (float): how many control signals to receive in every second. This sets the amount of simulation time that passes between every action input. lite_physics (bool): Whether to optimize for mujoco forward and step calls to reduce total simulation overhead. Set to False to preserve backward compatibility with datasets collected in robosuite <= 1.4.1. horizon (int): Every episode lasts for exactly @horizon timesteps. ignore_done (bool): True if never terminating the environment (ignore @horizon). hard_reset (bool): If True, re-loads model, sim, and render object upon a reset call, else, only calls sim.reset and resets all robosuite-internal variables camera_names (str or list of str): name of camera to be rendered. Should either be single str if same name is to be used for all cameras' rendering or else it should be a list of cameras to render. :Note: At least one camera must be specified if @use_camera_obs is True. :Note: To render all robots' cameras of a certain type (e.g.: "robotview" or "eye_in_hand"), use the convention "all-{name}" (e.g.: "all-robotview") to automatically render all camera images from each robot's camera list). camera_heights (int or list of int): height of camera frame. Should either be single int if same height is to be used for all cameras' frames or else it should be a list of the same length as "camera names" param. camera_widths (int or list of int): width of camera frame. Should either be single int if same width is to be used for all cameras' frames or else it should be a list of the same length as "camera names" param. camera_depths (bool or list of bool): True if rendering RGB-D, and RGB otherwise. Should either be single bool if same depth setting is to be used for all cameras or else it should be a list of the same length as "camera names" param. camera_segmentations (None or str or list of str or list of list of str): Camera segmentation(s) to use for each camera. Valid options are: `None`: no segmentation sensor used `'instance'`: segmentation at the class-instance level `'class'`: segmentation at the class level `'element'`: segmentation at the per-geom level If not None, multiple types of segmentations can be specified. A [list of str / str or None] specifies [multiple / a single] segmentation(s) to use for all cameras. A list of list of str specifies per-camera segmentation setting(s) to use. Raises: AssertionError: [Invalid number of robots specified] """ def __init__( self, robots, env_configuration="default", controller_configs=None, gripper_types="default", base_types="default", initialization_noise="default", table_full_size=(0.8, 0.8, 0.05), table_friction=(1.0, 5e-3, 1e-4), use_camera_obs=True, use_object_obs=True, reward_scale=1.0, reward_shaping=False, placement_initializer=None, has_renderer=False, has_offscreen_renderer=True, render_camera="frontview", render_collision_mesh=False, render_visual_mesh=True, render_gpu_device_id=-1, control_freq=20, lite_physics=True, horizon=1000, ignore_done=False, hard_reset=True, camera_names="agentview", camera_heights=256, camera_widths=256, camera_depths=False, camera_segmentations=None, # {None, instance, class, element} renderer="mjviewer", renderer_config=None, seed=None, ): # settings for table top self.table_full_size = table_full_size self.table_friction = table_friction self.table_offset = np.array((0, 0, 0.8)) # reward configuration self.reward_scale = reward_scale self.reward_shaping = reward_shaping # whether to use ground-truth object states self.use_object_obs = use_object_obs # object placement initializer self.placement_initializer = placement_initializer super().__init__( robots=robots, env_configuration=env_configuration, controller_configs=controller_configs, base_types="default", gripper_types=gripper_types, initialization_noise=initialization_noise, use_camera_obs=use_camera_obs, has_renderer=has_renderer, has_offscreen_renderer=has_offscreen_renderer, render_camera=render_camera, render_collision_mesh=render_collision_mesh, render_visual_mesh=render_visual_mesh, render_gpu_device_id=render_gpu_device_id, control_freq=control_freq, lite_physics=lite_physics, horizon=horizon, ignore_done=ignore_done, hard_reset=hard_reset, camera_names=camera_names, camera_heights=camera_heights, camera_widths=camera_widths, camera_depths=camera_depths, camera_segmentations=camera_segmentations, renderer=renderer, renderer_config=renderer_config, seed=seed, ) def reward(self, action=None): """ Reward function for the task. Sparse un-normalized reward: - a discrete reward of 2.25 is provided if the cube is lifted Un-normalized summed components if using reward shaping: - Reaching: in [0, 1], to encourage the arm to reach the cube - Grasping: in {0, 0.25}, non-zero if arm is grasping the cube - Lifting: in {0, 1}, non-zero if arm has lifted the cube The sparse reward only consists of the lifting component. Note that the final reward is normalized and scaled by reward_scale / 2.25 as well so that the max score is equal to reward_scale Args: action (np array): [NOT USED] Returns: float: reward value """ reward = 0.0 # sparse completion reward if self._check_success(): reward = 2.25 # use a shaping reward elif self.reward_shaping: # reaching reward dist = self._gripper_to_target( gripper=self.robots[0].gripper, target=self.cube.root_body, target_type="body", return_distance=True ) reaching_reward = 1 - np.tanh(10.0 * dist) reward += reaching_reward # grasping reward if self._check_grasp(gripper=self.robots[0].gripper, object_geoms=self.cube): reward += 0.25 # Scale reward if requested if self.reward_scale is not None: reward *= self.reward_scale / 2.25 return reward def _load_model(self): """ Loads an xml model, puts it in self.model """ super()._load_model() # Adjust base pose accordingly xpos = self.robots[0].robot_model.base_xpos_offset["table"](self.table_full_size[0]) self.robots[0].robot_model.set_base_xpos(xpos) # load model for table top workspace mujoco_arena = TableArena( table_full_size=self.table_full_size, table_friction=self.table_friction, table_offset=self.table_offset, ) # Arena always gets set to zero origin mujoco_arena.set_origin([0, 0, 0]) # initialize objects of interest tex_attrib = { "type": "cube", } mat_attrib = { "texrepeat": "1 1", "specular": "0.4", "shininess": "0.1", } redwood = CustomMaterial( texture="WoodRed", tex_name="redwood", mat_name="redwood_mat", tex_attrib=tex_attrib, mat_attrib=mat_attrib, ) self.cube = BoxObject( name="cube", size_min=[0.020, 0.020, 0.020], # [0.015, 0.015, 0.015], size_max=[0.022, 0.022, 0.022], # [0.018, 0.018, 0.018]) rgba=[1, 0, 0, 1], material=redwood, rng=self.rng, ) # 2. 新增:加载自定义的 YCB 杯子 self.cup = YCBCupObject(name="b_cup") # 3. 将两个物体打包成一个列表放入采样器,让它们在桌面上随机排布 self.objects = [self.cube, self.cup] # Create placement initializer if self.placement_initializer is not None: self.placement_initializer.reset() self.placement_initializer.add_objects(self.objects) else: self.placement_initializer = UniformRandomSampler( name="ObjectSampler", mujoco_objects=self.objects, x_range=[-0.10, 0.10], y_range=[-0.10, 0.10], rotation=None, ensure_object_boundary_in_range=True, ensure_valid_placement=True, reference_pos=self.table_offset, z_offset=0.15, rng=self.rng, ) # task includes arena, robot, and objects of interest self.model = ManipulationTask( mujoco_arena=mujoco_arena, mujoco_robots=[robot.robot_model for robot in self.robots], mujoco_objects=self.objects, ) def _setup_references(self): """ Sets up references to important components. A reference is typically an index or a list of indices that point to the corresponding elements in a flatten array, which is how MuJoCo stores physical simulation data. """ super()._setup_references() # Additional object references from this env self.cube_body_id = self.sim.model.body_name2id(self.cube.root_body) self.cup_body_id = self.sim.model.body_name2id(self.cup.root_body) def _setup_observables(self): """ Sets up observables to be used for this environment. Creates object-based observables if enabled Returns: OrderedDict: Dictionary mapping observable names to its corresponding Observable object """ observables = super()._setup_observables() # low-level object information if self.use_object_obs: # define observables modality modality = "object" # cube-related observables @sensor(modality=modality) def cube_pos(obs_cache): return np.array(self.sim.data.body_xpos[self.cube_body_id]) @sensor(modality=modality) def cube_quat(obs_cache): return convert_quat(np.array(self.sim.data.body_xquat[self.cube_body_id]), to="xyzw") # ------- 2. 新增:杯子的传感器群 ------- @sensor(modality=modality) def cup_pos(obs_cache): return np.array(self.sim.data.body_xpos[self.cup_body_id]) @sensor(modality=modality) def cup_quat(obs_cache): return convert_quat(np.array(self.sim.data.body_xquat[self.cup_body_id]), to="xyzw") sensors = [cube_pos, cube_quat, cup_pos, cup_quat] arm_prefixes = self._get_arm_prefixes(self.robots[0], include_robot_name=False) full_prefixes = self._get_arm_prefixes(self.robots[0]) # gripper to cube position sensor; one for each arm sensors += [ self._get_obj_eef_sensor(full_pf, "cube_pos", f"{arm_pf}gripper_to_cube_pos", modality) for arm_pf, full_pf in zip(arm_prefixes, full_prefixes) ] # 新增:夹爪到杯子的距离 sensors += [ self._get_obj_eef_sensor(full_pf, "cup_pos", f"{arm_pf}gripper_to_cup_pos", modality) for arm_pf, full_pf in zip(arm_prefixes, full_prefixes) ] names = [s.__name__ for s in sensors] # Create observables for name, s in zip(names, sensors): observables[name] = Observable( name=name, sensor=s, sampling_rate=self.control_freq, ) return observables def _reset_internal(self): """ Resets simulation internal configurations. """ super()._reset_internal() # Reset all object positions using initializer sampler if we're not directly loading from an xml if not self.deterministic_reset: # Sample from the placement initializer for all objects object_placements = self.placement_initializer.sample() # Loop through all objects and reset their positions for obj_pos, obj_quat, obj in object_placements.values(): print(f"=== {obj.name} ===") print(f"joints: {obj.joints}") print(f"joints[0]: {obj.joints[0] if obj.joints else 'EMPTY!'}") print(f"placement pos: {obj_pos}") if obj.joints: self.sim.data.set_joint_qpos(obj.joints[0], np.concatenate([np.array(obj_pos), np.array(obj_quat)])) else: print(f"WARNING: {obj.name} has no joints!") def visualize(self, vis_settings): """ In addition to super call, visualize gripper site proportional to the distance to the cube. Args: vis_settings (dict): Visualization keywords mapped to T/F, determining whether that specific component should be visualized. Should have "grippers" keyword as well as any other relevant options specified. """ # Run superclass method first super().visualize(vis_settings=vis_settings) # Color the gripper visualization site according to its distance to the cube if vis_settings["grippers"]: self._visualize_gripper_to_target(gripper=self.robots[0].gripper, target=self.cube) def _check_success(self): """ Check if cube has been lifted. Returns: bool: True if cube has been lifted """ cube_height = self.sim.data.body_xpos[self.cube_body_id][2] table_height = self.model.mujoco_arena.table_offset[2] # cube is higher than the table top above a margin return cube_height > table_height + 0.04
http://www.jsqmd.com/news/1081352/

相关文章:

  • 嵌入式OpenCL/OpenVX内存优化与性能调优实战
  • Azure OpenAI生产级部署实操指南:从零到可用的7步落地
  • MEMS振荡器引脚与焊接工艺全解析:从设计到量产避坑指南
  • AMD锐龙SDT调试工具终极指南:从新手到专家的完整解决方案
  • 本地化医学大模型微调:4-bit量化+LoRA实战指南
  • 华为设备Bootloader解锁新纪元:PotatoNV工具深度解析
  • 4G_Lora土壤氮磷钾监测系统设计与实现
  • MCP14T0517推挽变压器驱动器:集成方案简化隔离电源设计
  • 插件加载失败、XML跳转失灵、@Select注解不提示……MyBatis插件异常排查全链路,15种报错日志对照速查表
  • 高精度RTC芯片PCF2127T/PCF2129AT与OM13513评估板深度实操指南
  • 免费解锁iOS设备:AppleRa1n激活锁绕过工具完全指南
  • 双稳态触发器
  • 600V半桥栅极驱动器MCP14H2184:原理、设计与LLC谐振变换器应用
  • 构建Web应用主动防御体系:从代码到服务器的三层安全实战
  • NXP RW61x Wi-Fi RF测试模式实战指南:从原理到自动化测试
  • 模电实验板模块化设计解析与教学应用实践
  • WeMod破解工具:两种模式解锁专业版功能的完整指南
  • Zotero中文文献管理终极指南:用Jasminum插件一键解决元数据难题
  • MTKClient终极指南:掌握联发科设备底层操作的7大核心能力
  • 嵌入式开发必备:软件分析工具从原理到实战全解析
  • 如何高效安装拆分APK:SAI安装器从入门到精通的完整手册
  • Llama 3生产落地指南:架构特性、量化部署与场景化调优
  • ZigBee网络配置实战:从绑定、组到场景的ZeD工具全解析
  • 【IDEA插件黄金三角法则】:20年Java架构师总结——仅3个插件即可提升37%编码效率
  • vSphere高可用性配置失效真相(HA故障根因深度拆解):83%集群宕机源于这2个被忽视的检查项
  • MC68HC16Y3芯片选择与I/O端口配置:从原理到实战的嵌入式硬件设计指南
  • 在线教程丨UC伯克利/英伟达等发布3DGS开源库gsplat,节省4倍显存,训练时间缩短10%
  • 汽车线控系统核心:飞思卡尔56F8300在转向、制动与智能传感器中的应用与开发实战
  • LPC2800寄存器编程实战:从时钟配置到外设驱动的嵌入式开发指南
  • 2026年湖南vi设计企业选择要点与评估标准分析