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

Add helper to convert JaxSimModelData to mujoco.mjData #264

Merged
merged 4 commits into from
Oct 14, 2024

Conversation

diegoferigo
Copy link
Member

@diegoferigo diegoferigo commented Oct 14, 2024

The main benefit of using this helper w.r.t. plain usage of MujocoModelHelper is the logic to initialize the positions of removed joints. An example usage is the following:

import time

import jax.numpy as jnp
import jaxsim.api as js
import jaxsim.mujoco
import numpy as np
import requests

repo = "icub-tech-iit/ergocub-gazebo-simulations"
url = f"https://raw.githubusercontent.com/{repo}/master/models/stickBot/model.urdf"

# Download and read file.
urdf_string = requests.get(url).text

# Build the model.
model = js.model.JaxSimModel.build_from_model_description(model_description=urdf_string)

# Reduce the model by specifying custom elbow angles.
model = js.model.reduce(
    model=model,
    considered_joints=tuple(
        j
        for j in model.joint_names()
        # Remove upper body and sensors.
        if "neck" not in j
        and "wrist" not in j
        and "torso" not in j
        and "elbow" not in j
        and "shoulder" not in j
        and "lidar" not in j
        and "camera" not in j
    ),
    locked_joint_positions={
        "r_elbow": np.deg2rad(45),
        "l_elbow": np.deg2rad(45),
    },
)

# Create the model data.
data = js.data.JaxSimModelData.build(
    model=model,
    base_position=jnp.array([0, 0, 2.0]),
)

# ===================
# Open the visualizer
# ===================

# Convert the model to MJCF.
mjcf_string, assets = jaxsim.mujoco.UrdfToMjcf().convert(urdf=model.built_from)

# Create the Mujoco model helper.
mj_model_helper = jaxsim.mujoco.MujocoModelHelper.build_from_xml(
    mjcf_description=mjcf_string,
    assets=assets,
)

# Store in the model helper the data initialized from the JaxSim model.
# This is helpful the first time, so that the removed joint positions are
# stored in the Mujoco data object.
mj_model_helper.data = jaxsim.mujoco.mujoco_data_from_jaxsim(
    mujoco_model=mj_model_helper.model,
    jaxsim_model=model,
    jaxsim_data=data,
)

# Create the visualizer.
viz = jaxsim.mujoco.MujocoVisualizer(
    model=mj_model_helper.model, data=mj_model_helper.data
)


with viz.open(
    lookat=np.array(data.base_position()), elevation=-10, distance=5, azimut=180 - 30
) as viewer:

    # Here you can loop over a simulated trajectory.
    # It is not necessary to use the jaxsim.mujoco.mujoco_data_from_jaxsim helper.

    # for ...:
    #     with viewer.lock():
    #         mj_model_helper.set_base_position(position=np.array(...))
    #         mj_model_helper.set_base_orientation(orientation=np.array(...))
    #
    #         if model.dofs() > 0:
    #             mj_model_helper.set_joint_positions(
    #                 joint_names=list(model.joint_names()), positions=np.array(...)
    #             )

    viz.sync(viewer=viewer)

    while viewer.is_running():
        time.sleep(0.500)

📚 Documentation preview 📚: https://jaxsim--264.org.readthedocs.build//264/

@diegoferigo diegoferigo self-assigned this Oct 14, 2024
@diegoferigo diegoferigo marked this pull request as ready for review October 14, 2024 09:34
@diegoferigo
Copy link
Member Author

cc @xela-95

@diegoferigo diegoferigo force-pushed the improve_jaxsim_mujoco branch from a1a6c71 to ad3d691 Compare October 14, 2024 09:39
Copy link
Collaborator

@flferretti flferretti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot Diego! I left a couple comments

@xela-95
Copy link
Member

xela-95 commented Oct 14, 2024

This is cool thanks! 🚀

Copy link
Collaborator

@flferretti flferretti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Diego! Good to go after the last suggestion

Co-authored-by: Filippo Luca Ferretti <[email protected]>
@diegoferigo diegoferigo force-pushed the improve_jaxsim_mujoco branch from 573b35a to 9ea5c86 Compare October 14, 2024 11:41
@diegoferigo diegoferigo enabled auto-merge October 14, 2024 11:41
@diegoferigo diegoferigo merged commit c8d238f into main Oct 14, 2024
24 checks passed
@diegoferigo diegoferigo deleted the improve_jaxsim_mujoco branch October 14, 2024 11:56
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

Successfully merging this pull request may close these issues.

3 participants