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

Improve support of visualizing heightmaps/hfield in the Mujoco passive viewer #202

Merged
merged 2 commits into from
Jul 12, 2024

Conversation

diegoferigo
Copy link
Member

@diegoferigo diegoferigo commented Jul 11, 2024

I couldn't make the previous logic work correctly. This PR makes sure of the following:

  • The converted MjModel is initialized with a hfield grid whose number of rows and columns can be customized.
  • The area over which the heightmap is sampled can now be defined.
  • The MJCF model is dynamically updated with the wideness and elevation of the sampled heightmap data.

Here below the result.

Note that if the heightmap would be rendered incorrectly, the sphere would not lay over the surface as it would mean that the simulated data and the visualized terrain differ. The image shows that the two match as expected.

image

MWE
import time

import jax.numpy as jnp
import jax_dataclasses
import jaxsim
import jaxsim.api as js
import numpy as np
import rod.builder.primitives
from jaxsim import typing as jtp


# Define a custom terrain:
# https://it.mathworks.com/help/matlab/ref/peaks.html
@jax_dataclasses.pytree_dataclass
class PeaksTerrain(jaxsim.terrain.Terrain):

    def height(self, x: jtp.FloatLike, y: jtp.FloatLike) -> jtp.Float:

        z = (
            3 * (1 - x) ** 2 * jnp.exp(-(x**2) - (y + 1) ** 2)
            - 10 * (x / 5 - x**3 - y**5) * jnp.exp(-(x**2) - y**2)
            - 1 / 3 * jnp.exp(-((x + 1) ** 2) - y**2)
        )

        return jnp.array(0.25 * z, dtype=float)


# Build the ROD model of a sphere.
sphere = (
    rod.builder.primitives.SphereBuilder(name="sphere", mass=1.0, radius=0.1)
    .build_model()
    .add_link(name="link")
    .add_inertial()
    .add_visual()
    .add_collision()
    .build()
)


# Build the JaxSim model.
model = js.model.JaxSimModel.build_from_model_description(
    model_description=sphere,
    terrain=PeaksTerrain(),
)

# Initialize the sphere with the origin be located over the terrain at a
# given (x, y) coordinates.
x, y = -0.42, -0.62
data = js.data.JaxSimModelData.build(
    model=model,
    base_position=jnp.array([x, y, model.terrain.height(x=x, y=y)]),
)


# ================
# 3D visualization
# ================

import jaxsim.mujoco

mjcf_string, assets = jaxsim.mujoco.RodModelToMjcf().convert(
    rod_model=model.built_from,
    heightmap=True,
    heightmap_samples_xy=(256, 256),
)

mj_model_helper = jaxsim.mujoco.MujocoModelHelper.build_from_xml(
    mjcf_description=mjcf_string,
    assets=assets,
    heightmap=lambda x, y: model.terrain.height(x=x, y=y).tolist(),
    heightmap_radius_xy=(5.0, 5.0),
)

viz = jaxsim.mujoco.MujocoVisualizer(
    model=mj_model_helper.model, data=mj_model_helper.data
)


with viz.open() as viewer:

    with viewer.lock():
        mj_model_helper.set_base_position(position=np.array(data.base_position()))
        mj_model_helper.set_base_orientation(
            orientation=np.array(data.base_orientation(dcm=False))
        )

    viz.sync(viewer=viewer)

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

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

@diegoferigo diegoferigo self-assigned this Jul 11, 2024
@diegoferigo diegoferigo requested a review from flferretti as a code owner July 11, 2024 10:32
@diegoferigo diegoferigo force-pushed the enhance_mujoco_hfield branch 2 times, most recently from 02e45e4 to b68c19e Compare July 11, 2024 11:12
@diegoferigo diegoferigo force-pushed the enhance_mujoco_hfield branch from b68c19e to dce8f7b Compare July 11, 2024 13:14
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.

LGTM, thanks!

@diegoferigo diegoferigo merged commit 86aecbd into main Jul 12, 2024
24 checks passed
@diegoferigo diegoferigo deleted the enhance_mujoco_hfield branch July 12, 2024 08:28
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.

2 participants