-
Notifications
You must be signed in to change notification settings - Fork 27
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
Changes from 29 commits
bb36e88
f5bb042
1018cc6
e1bd9db
fe7817d
886ec03
e54c99a
4704519
aa4cda6
6b1f9b9
99f017c
da73bda
c3a8d20
244bdcc
269e232
ff22059
70d8936
5af5a31
a366749
1dccf45
0a7f3a2
99cc9be
2403be4
8126d4d
ca3f33b
2a8c9cf
d9325ac
7c727f1
5c64008
ceda8a0
c822bb4
da7638f
7945058
b16d26e
60bd119
4670393
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| 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: | ||
| try: | ||
| from mujoco import MjModel | ||
| except ImportError: | ||
| return False | ||
| return isinstance(obj, MjModel) | ||
|
|
||
|
|
||
| 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 isinstance(description, (str, pathlib.Path)): | ||
| return URDFModelFactory(path=description, math=math) | ||
|
|
||
| raise ValueError( | ||
| "Unsupported model description. Pass a URDF path/string or a MuJoCo MjModel." | ||
|
||
| ) | ||
| 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) | ||
Uh oh!
There was an error while loading. Please reload this page.