-
Notifications
You must be signed in to change notification settings - Fork 26
Add MujocoModelFactory #145
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
Merged
Merged
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
bb36e88
Add MujocoModelFactory
Giulero f5bb042
Add KinDynFactoryMixin for instantiating KinDyn classes from URDF and…
Giulero 1018cc6
Add factory functions for building URDF and Mujoco model factories
Giulero e1bd9db
Add build_model_factory import and MujocoModelFactory to __init__.py
Giulero fe7817d
Avoid circular imports
Giulero 886ec03
Refactor KinDynComputations classes to use build_model_factory for UR…
Giulero e54c99a
Add tests for Mujoco KinDyn computations
Giulero 4704519
Support actuator-based joint naming
Giulero aa4cda6
Rename file
Giulero 6b1f9b9
Move in a different location
Giulero 99f017c
Update import path for MujocoModelFactory in build_model_factory func…
Giulero da73bda
Update import path for MujocoModelFactory
Giulero c3a8d20
Refactor cache path handling
Giulero 244bdcc
Add mujoco and robot_descriptions to dependencies in environment files
Giulero 269e232
Add ROBOT_DESCRIPTIONS_CACHE env. Not sure this is the best option
Giulero ff22059
Remove robot description caching
Giulero 70d8936
Add MuJoCo interface section to README with usage examples
Giulero 5af5a31
Update `urdfstring` parameter description to indicate deprecation and…
Giulero a366749
Update src/adam/model/mj_factory/mujoco_model.py
Giulero 1dccf45
Update src/adam/model/mj_factory/mujoco_model.py
Giulero 0a7f3a2
Update src/adam/model/mj_factory/mujoco_model.py
Giulero 99cc9be
Fix MuJoCo naming
Giulero 2403be4
Fix MuJoCo model factory parameter naming and update doc
Giulero 8126d4d
Remove unused imports
Giulero ca3f33b
Refactor import logic
Giulero 2a8c9cf
Add docs for KinDynFactoryMixin
Giulero d9325ac
Format with black
Giulero 7c727f1
Update README.md
Giulero 5c64008
Refactor _is_mujoco_model to use isinstance for better type checking
Giulero ceda8a0
Renaming variables and switch assertion args
Giulero c822bb4
Change robot_descriptions source (pypi -> conda-forge)
Giulero da7638f
Update mujoco_setup fixture to test also a quadruped
Giulero 7945058
Refactor _is_mujoco_model and add _is_urdf method
Giulero b16d26e
Format with black
Giulero 60bd119
Remove pip from dependencies
Giulero 4670393
Fix missing var
Giulero File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,3 +21,5 @@ dependencies: | |
| - requests | ||
| - array-api-compat | ||
| - liecasadi | ||
| - mujoco | ||
| - robot_descriptions | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| from .abc_factories import Inertia, Inertial, Joint, Limits, Link, ModelFactory, Pose | ||
| from .factory import build_model_factory | ||
| from .model import Model | ||
| from .std_factories.std_joint import StdJoint | ||
| from .std_factories.std_link import StdLink | ||
| from .mj_factory.mujoco_model import MujocoModelFactory | ||
| from .std_factories.std_model import URDFModelFactory | ||
| from .tree import Node, Tree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import pathlib | ||
| from typing import Any | ||
|
|
||
| from adam.model.abc_factories import ModelFactory | ||
| from adam.model.std_factories.std_model import URDFModelFactory | ||
|
|
||
|
|
||
| def _is_mujoco_model(obj: Any) -> bool: | ||
| """Check if obj is a MuJoCo MjModel without importing mujoco.""" | ||
| cls = obj.__class__ | ||
| cls_name = getattr(cls, "__name__", "") | ||
| cls_module = getattr(cls, "__module__", "") | ||
| if cls_name != "MjModel" or "mujoco" not in cls_module: | ||
| return False | ||
| return all(hasattr(obj, attr) for attr in ("nq", "nv", "nu", "nbody")) | ||
|
|
||
|
|
||
| def _is_urdf(obj: Any) -> bool: | ||
| """Check if obj is a URDF.""" | ||
| if isinstance(obj, pathlib.Path): | ||
| return obj.suffix.lower() == ".urdf" | ||
|
|
||
| if isinstance(obj, str): | ||
| s = obj.lstrip() | ||
| if s.startswith("<") and "<robot" in s[:2048].lower(): | ||
| return True | ||
| try: | ||
| return pathlib.Path(obj).suffix.lower() == ".urdf" | ||
| except Exception: | ||
| return False | ||
|
|
||
| return False | ||
|
|
||
|
|
||
| def build_model_factory(description: Any, math) -> ModelFactory: | ||
| """Return a ModelFactory from a URDF string/path or a MuJoCo model.""" | ||
|
|
||
| if _is_mujoco_model(description): | ||
|
|
||
| from adam.model.mj_factory.mujoco_model import MujocoModelFactory | ||
|
|
||
| return MujocoModelFactory(mj_model=description, math=math) | ||
|
|
||
| if _is_urdf(description): | ||
| return URDFModelFactory(path=description, math=math) | ||
|
|
||
| raise ValueError( | ||
| f"Unsupported model description. Expected a URDF path/string or a mujoco.MjModel. " | ||
| f"Got: {type(description)!r}" | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import Any | ||
|
|
||
|
|
||
| class KinDynFactoryMixin: | ||
| """Shared helpers to instantiate KinDyn* classes from different model sources.""" | ||
|
|
||
| @classmethod | ||
| def from_urdf(cls: type[KinDynFactoryMixin], urdfstring: Any, *args, **kwargs): | ||
| """Instantiate using a URDF path/string. | ||
| Args: | ||
| urdfstring (str): path/string of a URDF | ||
| Returns: | ||
| KinDynFactoryMixin: An instance of the class initialized with the provided URDF and arguments. | ||
| """ | ||
| return cls(urdfstring, *args, **kwargs) | ||
|
|
||
| @classmethod | ||
| def from_mujoco_model( | ||
| cls: type[KinDynFactoryMixin], | ||
| mujoco_model: Any, | ||
| use_mujoco_actuators: bool = False, | ||
| *args, | ||
| **kwargs, | ||
| ): | ||
| """Instantiate using a MuJoCo model. | ||
| Args: | ||
| mujoco_model (MjModel): MuJoCo model instance | ||
| use_mujoco_actuators (bool): If True, use the names of joints under the <actuator> tags in the MuJoCo XML as joint names | ||
| (i.e., set 'joints_name_list' to actuator joint names). This is useful when you want the | ||
| KinDyn* instance to use actuator joint names instead of the default joint names in the xml. | ||
| Default is False. | ||
Giulero marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Returns: | ||
| KinDynFactoryMixin: An instance of the class initialized with the provided MuJoCo model | ||
| """ | ||
| if use_mujoco_actuators: | ||
| # use as joint names the names of joints under the <actuator> tags in the mujoco xml | ||
Giulero marked this conversation as resolved.
Show resolved
Hide resolved
Giulero marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| actuator_names = [ | ||
| mujoco_model.actuator(a).name for a in range(mujoco_model.nu) | ||
| ] | ||
| kwargs.setdefault("joints_name_list", actuator_names) | ||
| return cls(mujoco_model, *args, **kwargs) | ||
Empty file.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.