Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add more assets in the envs? #40

Open
GuoPingPan opened this issue Mar 25, 2023 · 2 comments
Open

How to add more assets in the envs? #40

GuoPingPan opened this issue Mar 25, 2023 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@GuoPingPan
Copy link

I have try to add some ball and cube in anymal_c_flat env.
So I modify the any_c_flat_config.py as below,

class AnymalCFlatCfg( AnymalCRoughCfg ):
...     
     # add ball_file and cube_file in assert
     class asset( AnymalCRoughCfg.asset ):
        self_collisions = 0 # 1 to disable, 0 to enable...bitwise filter
        ball_file = "{LEGGED_GYM_ROOT_DIR}/resources/objects/ball.urdf"
        cube_file = "{LEGGED_GYM_ROOT_DIR}/resources/objects/cube.urdf"

and I creat a new Robot inherited from LeggedRobot and modefiy _create_envs function

def _create_envs(self):
    """ Creates environments:
         1. loads the robot URDF/MJCF asset,
         2. For each environment
            2.1 creates the environment, 
            2.2 calls DOF and Rigid shape properties callbacks,
            2.3 create actor with these properties and add them to the env
         3. Store indices of different bodies of the robot
    """
    asset_path = self.cfg.asset.file.format(LEGGED_GYM_ROOT_DIR=LEGGED_GYM_ROOT_DIR)
    ball_asset_path = self.cfg.asset.ball_file.format(LEGGED_GYM_ROOT_DIR=LEGGED_GYM_ROOT_DIR)
    cube_asset_path = self.cfg.asset.cube_file.format(LEGGED_GYM_ROOT_DIR=LEGGED_GYM_ROOT_DIR)
    asset_root = os.path.dirname(asset_path)
    asset_file = os.path.basename(asset_path)


    objects_root = os.path.dirname(ball_asset_path)
    ball_file = os.path.basename(ball_asset_path)
    cube_file = os.path.basename(cube_asset_path)


    # asserts use default AssetOptions
    ball_asset = self.gym.load_asset(self.sim, objects_root, ball_file, gymapi.AssetOptions())
    cube_asset = self.gym.load_asset(self.sim, objects_root, cube_file, gymapi.AssetOptions())
    object_asset_spacing = 0.05


    asset_options = gymapi.AssetOptions()
    asset_options.default_dof_drive_mode = self.cfg.asset.default_dof_drive_mode
    asset_options.collapse_fixed_joints = self.cfg.asset.collapse_fixed_joints
    asset_options.replace_cylinder_with_capsule = self.cfg.asset.replace_cylinder_with_capsule
    asset_options.flip_visual_attachments = self.cfg.asset.flip_visual_attachments
    asset_options.fix_base_link = self.cfg.asset.fix_base_link
    asset_options.density = self.cfg.asset.density
    asset_options.angular_damping = self.cfg.asset.angular_damping
    asset_options.linear_damping = self.cfg.asset.linear_damping
    asset_options.max_angular_velocity = self.cfg.asset.max_angular_velocity
    asset_options.max_linear_velocity = self.cfg.asset.max_linear_velocity
    asset_options.armature = self.cfg.asset.armature
    asset_options.thickness = self.cfg.asset.thickness
    asset_options.disable_gravity = self.cfg.asset.disable_gravity


    robot_asset = self.gym.load_asset(self.sim, asset_root, asset_file, asset_options)
    self.num_dof = self.gym.get_asset_dof_count(robot_asset)
    self.num_bodies = self.gym.get_asset_rigid_body_count(robot_asset)
    dof_props_asset = self.gym.get_asset_dof_properties(robot_asset)
    rigid_shape_props_asset = self.gym.get_asset_rigid_shape_properties(robot_asset)


    # save body names from the asset
    body_names = self.gym.get_asset_rigid_body_names(robot_asset)
    # print(body_names) 
    # ['base', 'LF_HIP', 'LF_THIGH', 'LF_SHANK', 'LF_FOOT', 'LH_HIP', 
    # 'LH_THIGH', 'LH_SHANK', 'LH_FOOT', 'RF_HIP', 'RF_THIGH', 
    # 'RF_SHANK', 'RF_FOOT', 'RH_HIP', 'RH_THIGH', 'RH_SHANK', 'RH_FOOT']
    self.dof_names = self.gym.get_asset_dof_names(robot_asset)
    self.num_bodies = len(body_names)
    self.num_dofs = len(self.dof_names)
    feet_names = [s for s in body_names if self.cfg.asset.foot_name in s]
    # print(feet_names) # ['LF_FOOT', 'LH_FOOT', 'RF_FOOT', 'RH_FOOT']
    penalized_contact_names = []
    for name in self.cfg.asset.penalize_contacts_on:
        penalized_contact_names.extend([s for s in body_names if name in s])
    termination_contact_names = []
    for name in self.cfg.asset.terminate_after_contacts_on:
        termination_contact_names.extend([s for s in body_names if name in s])


    base_init_state_list = self.cfg.init_state.pos + self.cfg.init_state.rot + self.cfg.init_state.lin_vel + self.cfg.init_state.ang_vel
    self.base_init_state = to_torch(base_init_state_list, device=self.device, requires_grad=False)
    start_pose = gymapi.Transform()
    start_pose.p = gymapi.Vec3(*self.base_init_state[:3])


    self._get_env_origins()
    env_lower = gymapi.Vec3(0., 0., 0.)
    env_upper = gymapi.Vec3(0., 0., 0.)
    self.actor_handles = []
    self.envs = []
    for i in range(self.num_envs):
        # create env instance
        env_handle = self.gym.create_env(self.sim, env_lower, env_upper, int(np.sqrt(self.num_envs)))
        
        # randomly add ball and cube
        num_objects = 5
        for j in range(num_objects):
            c = 0.5 + 0.5 * np.random.random(3)
            color = gymapi.Vec3(c[0], c[1], c[2])
            object_asset = ball_asset if i%2==0 else cube_asset


            pose = gymapi.Transform()
            pose.r = gymapi.Quat(0, 0, 0, 1)
            pose.p = gymapi.Vec3(object_asset_spacing*j, object_asset_spacing*j, 0.0)


            ahandle = self.gym.create_actor(env_handle, object_asset, pose, None, i, 0)
            self.gym.set_rigid_body_color(env_handle, ahandle, 0, gymapi.MESH_VISUAL_AND_COLLISION, color)


        
        pos = self.env_origins[i].clone()
        pos[:2] += torch_rand_float(-1., 1., (2,1), device=self.device).squeeze(1)
        start_pose.p = gymapi.Vec3(*pos)
            
        rigid_shape_props = self._process_rigid_shape_props(rigid_shape_props_asset, i)
        self.gym.set_asset_rigid_shape_properties(robot_asset, rigid_shape_props)
        actor_handle = self.gym.create_actor(env_handle, robot_asset, start_pose, self.cfg.asset.name, i, self.cfg.asset.self_collisions, 0)
        dof_props = self._process_dof_props(dof_props_asset, i)
        self.gym.set_actor_dof_properties(env_handle, actor_handle, dof_props)
        body_props = self.gym.get_actor_rigid_body_properties(env_handle, actor_handle)
        body_props = self._process_rigid_body_props(body_props, i)
        self.gym.set_actor_rigid_body_properties(env_handle, actor_handle, body_props, recomputeInertia=True)
        self.envs.append(env_handle)
        self.actor_handles.append(actor_handle)


    self.feet_indices = torch.zeros(len(feet_names), dtype=torch.long, device=self.device, requires_grad=False)
    for i in range(len(feet_names)):
        self.feet_indices[i] = self.gym.find_actor_rigid_body_handle(self.envs[0], self.actor_handles[0], feet_names[i])


    self.penalised_contact_indices = torch.zeros(len(penalized_contact_names), dtype=torch.long, device=self.device, requires_grad=False)
    for i in range(len(penalized_contact_names)):
        self.penalised_contact_indices[i] = self.gym.find_actor_rigid_body_handle(self.envs[0], self.actor_handles[0], penalized_contact_names[i])


    self.termination_contact_indices = torch.zeros(len(termination_contact_names), dtype=torch.long, device=self.device, requires_grad=False)
    for i in range(len(termination_contact_names)):
        self.termination_contact_indices[i] = self.gym.find_actor_rigid_body_handle(self.envs[0], self.actor_handles[0], termination_contact_names[i])

but it will get some error in _init_buffers().
Please what is the right way to add more assert?

@GuoPingPan GuoPingPan added the bug Something isn't working label Mar 25, 2023
@XiaoWZENG
Copy link

Have you solved this problem?

@dipampatel18
Copy link

@GuoPingPan Any updates on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants