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

Adding Mujoco-Menagerie AllegroHand #605

Open
JojoSim0n opened this issue Jan 5, 2025 · 3 comments
Open

Adding Mujoco-Menagerie AllegroHand #605

JojoSim0n opened this issue Jan 5, 2025 · 3 comments
Assignees

Comments

@JojoSim0n
Copy link

System Info

Robosuit Version: v1.5.0
OS: Ubuntu 22.04
Mujoco: 3.2.5
Mujoco-py: 2.1.2.14
Python: 3.10.22

Information

I'm trying to add the allegro hand from mujoco-menagerie as a custom gripper. I created a simple tester, to verify the gripper model.

from robosuite.models.grippers import gripper_factory
from mujoco_py import MjSim, MjViewer
import os

# Set the gripper XML path and name
gripper_name = "AllegroLeftHand"  # Use the correct gripper name
gripper_xml_path = os.path.join(suite.models.assets_root, "grippers", f"{gripper_name}.xml")

# Load the gripper
print(f"Loading gripper model: {gripper_name}")
gripper = gripper_factory(gripper_name)

# Create a simulation for the gripper
print("Creating simulation...")
sim = MjSim(gripper.mujoco_model)
viewer = MjViewer(sim)

# Run the simulation
print("Starting simulation. Use SPACEBAR to pause.")
while True:
    sim.step()
    viewer.render()

which results in:
Loading gripper model: AllegroLeftHand Traceback (most recent call last): File "/home/johannes/Documents/master/robosuite/robosuite/scripts/tester.py", line 12, in <module> gripper = gripper_factory(gripper_name) File "/home/johannes/Documents/master/robosuite/robosuite/models/grippers/gripper_factory.py", line 32, in gripper_factory return GRIPPER_MAPPING[name](idn=idn) File "/home/johannes/Documents/master/robosuite/robosuite/models/grippers/allegro_hands.py", line 19, in __init__ super().__init__(xml_path_completion("grippers/left_hand.xml"), idn=idn) File "/home/johannes/Documents/master/robosuite/robosuite/models/grippers/gripper_model.py", line 21, in __init__ super().__init__(fname, idn=idn) File "/home/johannes/Documents/master/robosuite/robosuite/models/base.py", line 528, in __init__ super().__init__(fname) File "/home/johannes/Documents/master/robosuite/robosuite/models/base.py", line 47, in __init__ self._replace_defaults_inline(default_dic=default_classes) File "/home/johannes/Documents/master/robosuite/robosuite/models/base.py", line 254, in _replace_defaults_inline self._replace_defaults_inline(default_dic=default_dic, root=child) File "/home/johannes/Documents/master/robosuite/robosuite/models/base.py", line 254, in _replace_defaults_inline self._replace_defaults_inline(default_dic=default_dic, root=child) File "/home/johannes/Documents/master/robosuite/robosuite/models/base.py", line 254, in _replace_defaults_inline self._replace_defaults_inline(default_dic=default_dic, root=child) File "/home/johannes/Documents/master/robosuite/robosuite/models/base.py", line 247, in _replace_defaults_inline tag_attrs = default_dic[cls_name].get(root.tag, None) KeyError: 'visual'

the xml files and meshes are placed in the corresponding folders, the models are correctly working using the mujoco-viewer.
Also i created a gripper class:

Dexterous hands for the Wonik Allegro Hand.
"""
import numpy as np

from robosuite.models.grippers.gripper_model import GripperModel
from robosuite.utils.mjcf_utils import xml_path_completion


class AllegroLeftHand(GripperModel):
    """
    Dexterous left hand of the Wonik Allegro Hand.

    Args:
        idn (int or str): Number or some other unique identification string for this gripper instance.
    """

    def __init__(self, idn=0):
        super().__init__(xml_path_completion("grippers/left_hand.xml"), idn=idn)

    def format_action(self, action):
        """
        Format the action to align with the actuator configuration of the Allegro Left Hand.
        - Thumb has 4 actuated joints.
        - Index, Middle, and Ring fingers each have 4 actuated joints.
        """
        assert len(action) == self.dof, "Action dimension mismatch!"
        return np.clip(action, self.action_range[0], self.action_range[1])

    @property
    def init_qpos(self):
        """
        Initial joint positions for the Allegro Left Hand.
        """
        return np.zeros(self.dof)

    @property
    def speed(self):
        """
        Speed of the Allegro Left Hand actuators.
        """
        return 0.15

    @property
    def dof(self):
        """
        Degrees of freedom for the Allegro Left Hand.
        - Thumb: 4 joints
        - Index: 4 joints
        - Middle: 4 joints
        - Ring: 4 joints
        """
        return 16

    @property
    def action_range(self):
        """
        Define the valid range for actuator controls based on the XML joint limits.
        """
        # Joint limits based on the XML file
        return np.array([
            -0.47, -0.196, -0.174, -0.227,  # Ring finger
            -0.47, -0.196, -0.174, -0.227,  # Middle finger
            -0.47, -0.196, -0.174, -0.227,  # Index finger
            0.263, -0.105, -0.189, -0.162   # Thumb
        ]), np.array([
            0.47, 1.61, 1.709, 1.618,       # Ring finger
            0.47, 1.61, 1.709, 1.618,       # Middle finger
            0.47, 1.61, 1.709, 1.618,       # Index finger
            1.396, 1.163, 1.644, 1.719      # Thumb
        ])

    @property
    def _important_geoms(self):
        """
        Define important geometries for collision and visualization.
        """
        return {
            "left_finger": [
                "rf_proximal_collision",
                "rf_medial_collision",
                "rf_distal_collision",
                "mf_proximal_collision",
                "mf_medial_collision",
                "mf_distal_collision",
                "ff_proximal_collision",
                "ff_medial_collision",
                "ff_distal_collision",
                "th_proximal_collision",
                "th_medial_collision",
                "th_distal_collision",
            ],
            "left_fingerpad": [
                "rf_tip_collision",
                "mf_tip_collision",
                "ff_tip_collision",
                "th_tip_collision",
            ],
        }


"""
Dexterous hands for the Wonik Allegro Hand (Right).
"""
class AllegroRightHand(GripperModel):
    """
    Dexterous right hand of the Wonik Allegro Hand.

    Args:
        idn (int or str): Number or some other unique identification string for this gripper instance.
    """

    def __init__(self, idn=0):
        super().__init__(xml_path_completion("grippers/right_hand.xml"), idn=idn)

    def format_action(self, action):
        """
        Format the action to align with the actuator configuration of the Allegro Right Hand.
        - Thumb has 4 actuated joints.
        - Index, Middle, and Ring fingers each have 4 actuated joints.
        """
        assert len(action) == self.dof, "Action dimension mismatch!"
        return np.clip(action, self.action_range[0], self.action_range[1])

    @property
    def init_qpos(self):
        """
        Initial joint positions for the Allegro Right Hand.
        """
        return np.zeros(self.dof)

    @property
    def speed(self):
        """
        Speed of the Allegro Right Hand actuators.
        """
        return 0.15

    @property
    def dof(self):
        """
        Degrees of freedom for the Allegro Right Hand.
        - Thumb: 4 joints
        - Index: 4 joints
        - Middle: 4 joints
        - Ring: 4 joints
        """
        return 16

    @property
    def action_range(self):
        """
        Define the valid range for actuator controls based on the XML joint limits.
        """
        # Joint limits based on the XML file
        return np.array([
            -0.47, -0.196, -0.174, -0.227,  # Index finger
            -0.47, -0.196, -0.174, -0.227,  # Middle finger
            -0.47, -0.196, -0.174, -0.227,  # Ring finger
            0.263, -0.105, -0.189, -0.162   # Thumb
        ]), np.array([
            0.47, 1.61, 1.709, 1.618,       # Index finger
            0.47, 1.61, 1.709, 1.618,       # Middle finger
            0.47, 1.61, 1.709, 1.618,       # Ring finger
            1.396, 1.163, 1.644, 1.719      # Thumb
        ])

    @property
    def _important_geoms(self):
        """
        Define important geometries for collision and visualization.
        """
        return {
            "right_finger": [
                "ff_proximal_collision",
                "ff_medial_collision",
                "ff_distal_collision",
                "mf_proximal_collision",
                "mf_medial_collision",
                "mf_distal_collision",
                "rf_proximal_collision",
                "rf_medial_collision",
                "rf_distal_collision",
                "th_proximal_collision",
                "th_medial_collision",
                "th_distal_collision",
            ],
            "right_fingerpad": [
                "ff_tip_collision",
                "mf_tip_collision",
                "rf_tip_collision",
                "th_tip_collision",
            ],
        }

I'm not quiet sure how to resolve this and get the custom gripper to work as expected.

Reproduction

Steps to reproduce:

  1. place the left and right hand .xml files as well as the assets folder under models/assets/grippers
  2. add the allegrohands.py file under /models/grippers
  3. run the tester script

Expected behavior

No response

@kevin-thankyou-lin
Copy link
Contributor

@abhihjoshi if you've a chance to take a look!

@JojoSim0n
Copy link
Author

i think i managed implementing it by myself. However is there a way of verification? since the gripper tester is not really working on any dexterous hand, since the binary input is directly compared to dof (which most likely seem to fail everytime?)

@abhihjoshi
Copy link
Contributor

Hey @JojoSim0n, sorry for delayed reply. Do you mind elaborating what your current issue with testing the gripper is? If you have some starter code that would be helpful for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants