robotenv 0.2.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- robotenv/__init__.py +21 -0
- robotenv/_canon.py +136 -0
- robotenv/config/__init__.py +93 -0
- robotenv/config/base_env.py +557 -0
- robotenv/config/control.py +161 -0
- robotenv/config/defaults.py +29 -0
- robotenv/config/env.py +107 -0
- robotenv/config/kinematics.py +121 -0
- robotenv/config/mount.py +172 -0
- robotenv/config/providers.py +171 -0
- robotenv/config/robot_providers.py +140 -0
- robotenv/config/sim.py +152 -0
- robotenv/config/specs.py +111 -0
- robotenv/config/sweep.py +100 -0
- robotenv/config/task.py +183 -0
- robotenv/envs/__init__.py +34 -0
- robotenv/envs/spaces.py +38 -0
- robotenv/errors.py +35 -0
- robotenv/robot/__init__.py +30 -0
- robotenv/robot/arm.py +319 -0
- robotenv/robot/external.py +234 -0
- robotenv/robot/generators.py +138 -0
- robotenv/robot/gripper.py +82 -0
- robotenv/robot/pose.py +49 -0
- robotenv/robot/providers/__init__.py +5 -0
- robotenv/robot/providers/menagerie/__init__.py +127 -0
- robotenv/robot/providers/menagerie/_describe.py +130 -0
- robotenv/robot/providers/menagerie/cards.json +37 -0
- robotenv/simulators/__init__.py +51 -0
- robotenv/simulators/base.py +70 -0
- robotenv/simulators/mujoco/__init__.py +38 -0
- robotenv/simulators/mujoco/arm_spec.py +137 -0
- robotenv/simulators/mujoco/compile.py +242 -0
- robotenv/simulators/mujoco/env.py +257 -0
- robotenv/simulators/mujoco/external.py +443 -0
- robotenv/simulators/mujoco/mount.py +65 -0
- robotenv/simulators/mujoco/render.py +158 -0
- robotenv/simulators/mujoco/scenes.py +35 -0
- robotenv/simulators/mujoco/sim.py +174 -0
- robotenv/simulators/warp/__init__.py +100 -0
- robotenv/simulators/warp/env.py +235 -0
- robotenv/simulators/warp/sim.py +332 -0
- robotenv/tasks/__init__.py +10 -0
- robotenv/tasks/base.py +147 -0
- robotenv/tasks/empty/__init__.py +42 -0
- robotenv/tasks/empty/params.py +33 -0
- robotenv/tasks/empty/task.py +27 -0
- robotenv/tasks/robosuite/__init__.py +133 -0
- robotenv/tasks/robosuite/_compat.py +165 -0
- robotenv/tasks/robosuite/_introspect.py +65 -0
- robotenv/tasks/robosuite/params.py +152 -0
- robotenv/tasks/robosuite/scene.py +140 -0
- robotenv/tasks/robosuite/task.py +145 -0
- robotenv/viz/__init__.py +22 -0
- robotenv/viz/_deps.py +16 -0
- robotenv/viz/camera.py +98 -0
- robotenv/viz/clip.py +180 -0
- robotenv/viz/frames.py +93 -0
- robotenv/viz/record.py +141 -0
- robotenv-0.2.1.dist-info/METADATA +136 -0
- robotenv-0.2.1.dist-info/RECORD +63 -0
- robotenv-0.2.1.dist-info/WHEEL +4 -0
- robotenv-0.2.1.dist-info/licenses/LICENSE +339 -0
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"""robosuite as a source of task assets: arenas, objects and their placement. Imports neither robosuite nor MuJoCo.
|
|
2
|
+
|
|
3
|
+
``robosuite/<EnvName>`` loads the arena and the objects of the robosuite environment ``<EnvName>`` into the
|
|
4
|
+
simulation named by ``SimConfig.simulator``. robosuite does not simulate anything here: its physics settings,
|
|
5
|
+
controllers, robots and rewards are not used. See ``params.py`` for what the task config holds.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
from ..._canon import canonical_json
|
|
13
|
+
from ...errors import Problem
|
|
14
|
+
from ...robot import Pose
|
|
15
|
+
from ._compat import CompatReport, apply_compat
|
|
16
|
+
from .params import RESERVED_ENV_KWARGS, LiftObject, ReachObject, RobosuiteParams
|
|
17
|
+
|
|
18
|
+
__all__ = [
|
|
19
|
+
"PROVIDER",
|
|
20
|
+
"CompatReport",
|
|
21
|
+
"LiftObject",
|
|
22
|
+
"ReachObject",
|
|
23
|
+
"RobosuiteParams",
|
|
24
|
+
"RobosuiteTasks",
|
|
25
|
+
"apply_compat",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
SCENE = "empty" # the arena brings its own floor, so the simulator adds none
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class RobosuiteTasks:
|
|
32
|
+
SIMULATORS = frozenset({"mujoco", "warp"})
|
|
33
|
+
|
|
34
|
+
def task_names(self) -> tuple[str, ...]:
|
|
35
|
+
return ("<EnvName>",)
|
|
36
|
+
|
|
37
|
+
def params_class(self, name: str) -> type | None:
|
|
38
|
+
return RobosuiteParams if name else None
|
|
39
|
+
|
|
40
|
+
def mount_frame(self, params: Any) -> Pose:
|
|
41
|
+
return _mount_frame(params.name, dict(params.kwargs))
|
|
42
|
+
|
|
43
|
+
def validate(self, cfg: Any, simulator: str) -> list[Problem]:
|
|
44
|
+
if simulator not in self.SIMULATORS:
|
|
45
|
+
return [Problem("sim.simulator", f"task {cfg.task.id!r} runs on {sorted(self.SIMULATORS)}, not on {simulator!r}")]
|
|
46
|
+
try:
|
|
47
|
+
apply_compat()
|
|
48
|
+
import robosuite # noqa: F401
|
|
49
|
+
except ImportError:
|
|
50
|
+
return [Problem("task.params.name", "robosuite is not installed; install robotenv[robosuite]")]
|
|
51
|
+
|
|
52
|
+
from . import _introspect as intro
|
|
53
|
+
|
|
54
|
+
problems: list[Problem] = []
|
|
55
|
+
if getattr(cfg.robot, "end_effector", True) is None: # an ExternalRobot without an end effector
|
|
56
|
+
problems.append(Problem("robot.end_effector", f"task {cfg.task.id!r} reads the end effector, the robot has none"))
|
|
57
|
+
if cfg.sim.scene != SCENE:
|
|
58
|
+
problems.append(
|
|
59
|
+
Problem("sim.scene", f"must be {SCENE!r} for robosuite tasks: the arena brings its own floor")
|
|
60
|
+
)
|
|
61
|
+
params = cfg.task.params
|
|
62
|
+
name = params.name
|
|
63
|
+
cls = intro.env_class(name)
|
|
64
|
+
if cls is None:
|
|
65
|
+
problems.append(
|
|
66
|
+
Problem("task.params.name", f"robosuite has no environment {name!r}; single-arm ones: {intro.single_arm_env_names()}")
|
|
67
|
+
)
|
|
68
|
+
return problems
|
|
69
|
+
if not intro.is_single_arm_manipulation(cls):
|
|
70
|
+
problems.append(
|
|
71
|
+
Problem(
|
|
72
|
+
"task.params.name",
|
|
73
|
+
f"{name!r} needs more than one arm; single-arm environments: {intro.single_arm_env_names()}",
|
|
74
|
+
)
|
|
75
|
+
)
|
|
76
|
+
return problems
|
|
77
|
+
if name.endswith("Single") or params.kwargs.get("single_object_mode") == 1:
|
|
78
|
+
problems.append(
|
|
79
|
+
Problem(
|
|
80
|
+
"task.params.name",
|
|
81
|
+
f"{name!r} picks a different object every episode, so its observation keys change; fix the "
|
|
82
|
+
"object instead: PickPlace with single_object_mode=2 and object_type, or NutAssemblySquare / "
|
|
83
|
+
"NutAssemblyRound",
|
|
84
|
+
)
|
|
85
|
+
)
|
|
86
|
+
known = set(intro.constructor_params(cls))
|
|
87
|
+
unknown = sorted(set(params.kwargs) - known)
|
|
88
|
+
if unknown:
|
|
89
|
+
accepted = sorted(known - RESERVED_ENV_KWARGS)
|
|
90
|
+
problems.append(
|
|
91
|
+
Problem("task.params.env_kwargs", f"{name} does not accept {unknown}; accepted: {accepted}")
|
|
92
|
+
)
|
|
93
|
+
if not problems:
|
|
94
|
+
from .scene import load_scene
|
|
95
|
+
|
|
96
|
+
try:
|
|
97
|
+
names = [o.name for o in load_scene(name, params.kwargs, cfg.sim.seed).objects if not o.visual]
|
|
98
|
+
except Exception as e: # robosuite's own checks, for example an unsupported option combination
|
|
99
|
+
return [Problem("task", f"robosuite could not build {name!r}: {type(e).__name__}: {e}")]
|
|
100
|
+
if params.objective.object not in names:
|
|
101
|
+
problems.append(
|
|
102
|
+
Problem(
|
|
103
|
+
"task.params.objective.object",
|
|
104
|
+
f"{params.objective.object!r} is not an object of {name!r}; objects in the scene: {names}",
|
|
105
|
+
)
|
|
106
|
+
)
|
|
107
|
+
return problems
|
|
108
|
+
|
|
109
|
+
def build(self, cfg: Any, simulator: str) -> Any:
|
|
110
|
+
from .task import RobosuiteTask
|
|
111
|
+
|
|
112
|
+
return RobosuiteTask(cfg)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
PROVIDER = RobosuiteTasks()
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
_FRAMES: dict[tuple[str, str], Pose] = {}
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def _mount_frame(env_name: str, env_kwargs: dict[str, Any]) -> Pose:
|
|
122
|
+
"""Where robosuite puts the stock robot's root body in ``env_name`` built with ``env_kwargs`` (world frame).
|
|
123
|
+
|
|
124
|
+
It follows the scene's parameters (a larger table moves it) and not ``sim.seed``: robosuite derives it from the
|
|
125
|
+
arena and the robot model. Memoised per (environment, canonical ``env_kwargs``), so a scene costs at most one extra
|
|
126
|
+
build. Needs robosuite.
|
|
127
|
+
"""
|
|
128
|
+
key = (env_name, canonical_json(env_kwargs))
|
|
129
|
+
if key not in _FRAMES:
|
|
130
|
+
from .scene import load_scene
|
|
131
|
+
|
|
132
|
+
_FRAMES[key] = load_scene(env_name, env_kwargs).mount
|
|
133
|
+
return _FRAMES[key]
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
"""Compatibility patches that let robosuite 1.5.x run on current MuJoCo.
|
|
2
|
+
|
|
3
|
+
robosuite 1.5.x declares ``mujoco>=3.3.0`` with no upper bound, but two of its code paths break on newer MuJoCo:
|
|
4
|
+
|
|
5
|
+
``joint-address-enum``
|
|
6
|
+
``MjModel.get_joint_qpos_addr`` and ``get_joint_qvel_addr`` test ``np.int32 in (mjJNT_HINGE, mjJNT_SLIDE)``.
|
|
7
|
+
From MuJoCo 3.12 (pybind11 3.1 enum comparison) that is False, so every hinge or slide joint fails an assert
|
|
8
|
+
during the first reset.
|
|
9
|
+
|
|
10
|
+
``mass-matrix``
|
|
11
|
+
``Controller.update`` calls ``mujoco.mj_fullM(model, dst, data.qM)``. MuJoCo 3.10 changed the signature to
|
|
12
|
+
``mj_fullM(model, data, dst)`` and 3.11 removed ``MjData.qM``.
|
|
13
|
+
|
|
14
|
+
Upstream reports: ARISE-Initiative/robosuite issue 831 (version pin) and pull request 832 (enum comparison).
|
|
15
|
+
|
|
16
|
+
:func:`apply_compat` is idempotent. It probes each problem (the joint-address check runs robosuite's own method,
|
|
17
|
+
the mass-matrix check reads robosuite's source and tries the old MuJoCo call) and patches only what fails, so it
|
|
18
|
+
does nothing on old MuJoCo and switches itself off once robosuite fixes the code. Each patch is narrow: the
|
|
19
|
+
joint-address methods are replaced by int-comparing versions, and the mass-matrix call is routed through a
|
|
20
|
+
stand-in that supplies the ``MjData`` the new signature needs.
|
|
21
|
+
|
|
22
|
+
The patches live in the current process. Call :func:`apply_compat` in every worker that starts a fresh interpreter
|
|
23
|
+
(``spawn`` or ``forkserver``); forked workers inherit them.
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
from __future__ import annotations
|
|
27
|
+
|
|
28
|
+
import threading
|
|
29
|
+
from dataclasses import dataclass
|
|
30
|
+
|
|
31
|
+
__all__ = ["CompatReport", "apply_compat"]
|
|
32
|
+
|
|
33
|
+
_PROBE_XML = "<mujoco><worldbody><body><joint name='j' type='hinge'/><geom size='.1'/></body></worldbody></mujoco>"
|
|
34
|
+
|
|
35
|
+
_LOCK = threading.Lock()
|
|
36
|
+
_REPORT: CompatReport | None = None
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@dataclass(frozen=True, slots=True)
|
|
40
|
+
class CompatReport:
|
|
41
|
+
"""What :func:`apply_compat` found: patches applied now, and checks that passed without a patch."""
|
|
42
|
+
|
|
43
|
+
mujoco_version: str
|
|
44
|
+
robosuite_version: str
|
|
45
|
+
applied: tuple[str, ...]
|
|
46
|
+
not_needed: tuple[str, ...]
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class _LegacyMassMatrix:
|
|
50
|
+
"""Stand-in for the removed ``MjData.qM``. It only remembers which ``MjData`` it came from."""
|
|
51
|
+
|
|
52
|
+
__slots__ = ("data",)
|
|
53
|
+
|
|
54
|
+
def __init__(self, data):
|
|
55
|
+
self.data = data
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class _MujocoProxy:
|
|
59
|
+
"""Replaces the ``mujoco`` name inside robosuite's controller module. Forwards everything but ``mj_fullM``."""
|
|
60
|
+
|
|
61
|
+
def __init__(self, real):
|
|
62
|
+
object.__setattr__(self, "_real", real)
|
|
63
|
+
|
|
64
|
+
def __getattr__(self, name):
|
|
65
|
+
return getattr(self._real, name)
|
|
66
|
+
|
|
67
|
+
def mj_fullM(self, model, dst, qm):
|
|
68
|
+
if isinstance(qm, _LegacyMassMatrix):
|
|
69
|
+
return self._real.mj_fullM(model, qm.data, dst) # MuJoCo >= 3.10 signature
|
|
70
|
+
return self._real.mj_fullM(model, dst, qm) # old signature, untouched
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _joint_address_broken(mujoco, binding_utils) -> bool:
|
|
74
|
+
model = binding_utils.MjModel(mujoco.MjModel.from_xml_string(_PROBE_XML))
|
|
75
|
+
try:
|
|
76
|
+
model.get_joint_qpos_addr("j")
|
|
77
|
+
model.get_joint_qvel_addr("j")
|
|
78
|
+
except AssertionError:
|
|
79
|
+
return True
|
|
80
|
+
return False
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def _mass_matrix_broken(mujoco, controller_module) -> bool:
|
|
84
|
+
"""True when robosuite still reads ``data.qM`` and the installed MuJoCo no longer supports that call."""
|
|
85
|
+
import ast
|
|
86
|
+
import inspect
|
|
87
|
+
import textwrap
|
|
88
|
+
|
|
89
|
+
import numpy as np
|
|
90
|
+
|
|
91
|
+
try:
|
|
92
|
+
tree = ast.parse(textwrap.dedent(inspect.getsource(controller_module.Controller.update)))
|
|
93
|
+
uses_old_call = any(isinstance(node, ast.Attribute) and node.attr == "qM" for node in ast.walk(tree))
|
|
94
|
+
except (OSError, TypeError, SyntaxError): # source unavailable (for example a zipped install): assume unfixed
|
|
95
|
+
uses_old_call = True
|
|
96
|
+
if not uses_old_call:
|
|
97
|
+
return False # robosuite was fixed upstream
|
|
98
|
+
model = mujoco.MjModel.from_xml_string(_PROBE_XML)
|
|
99
|
+
data = mujoco.MjData(model)
|
|
100
|
+
mujoco.mj_forward(model, data)
|
|
101
|
+
dst = np.zeros((model.nv, model.nv))
|
|
102
|
+
try:
|
|
103
|
+
mujoco.mj_fullM(model, dst, data.qM) # exactly the call in robosuite's Controller.update
|
|
104
|
+
except (AttributeError, TypeError):
|
|
105
|
+
return True
|
|
106
|
+
return False
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def _patch_joint_address(mujoco, binding_utils) -> None:
|
|
110
|
+
free, ball = int(mujoco.mjtJoint.mjJNT_FREE), int(mujoco.mjtJoint.mjJNT_BALL)
|
|
111
|
+
one_dof = (int(mujoco.mjtJoint.mjJNT_HINGE), int(mujoco.mjtJoint.mjJNT_SLIDE))
|
|
112
|
+
|
|
113
|
+
def make(address_field: str, free_dims: int, ball_dims: int):
|
|
114
|
+
def get_address(self, name):
|
|
115
|
+
joint_id = self.joint_name2id(name)
|
|
116
|
+
joint_type = int(self.jnt_type[joint_id]) # int() is the fix: np.int32 vs enum membership fails >= 3.12
|
|
117
|
+
address = getattr(self, address_field)[joint_id]
|
|
118
|
+
if joint_type == free:
|
|
119
|
+
ndim = free_dims
|
|
120
|
+
elif joint_type == ball:
|
|
121
|
+
ndim = ball_dims
|
|
122
|
+
else:
|
|
123
|
+
assert joint_type in one_dof
|
|
124
|
+
ndim = 1
|
|
125
|
+
return address if ndim == 1 else (address, address + ndim)
|
|
126
|
+
|
|
127
|
+
return get_address
|
|
128
|
+
|
|
129
|
+
binding_utils.MjModel.get_joint_qpos_addr = make("jnt_qposadr", 7, 4)
|
|
130
|
+
binding_utils.MjModel.get_joint_qvel_addr = make("jnt_dofadr", 6, 3)
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def _patch_mass_matrix(mujoco, binding_utils, controller_module) -> None:
|
|
134
|
+
binding_utils.MjData.qM = property(lambda self: _LegacyMassMatrix(self._data))
|
|
135
|
+
controller_module.mujoco = _MujocoProxy(mujoco)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def apply_compat() -> CompatReport:
|
|
139
|
+
"""Patch robosuite where the installed MuJoCo breaks it. Safe to call repeatedly and from several threads."""
|
|
140
|
+
global _REPORT
|
|
141
|
+
with _LOCK:
|
|
142
|
+
if _REPORT is not None:
|
|
143
|
+
return _REPORT
|
|
144
|
+
import mujoco
|
|
145
|
+
import robosuite
|
|
146
|
+
from robosuite.controllers.parts import controller as controller_module
|
|
147
|
+
from robosuite.utils import binding_utils
|
|
148
|
+
|
|
149
|
+
applied: list[str] = []
|
|
150
|
+
not_needed: list[str] = []
|
|
151
|
+
|
|
152
|
+
if _joint_address_broken(mujoco, binding_utils):
|
|
153
|
+
_patch_joint_address(mujoco, binding_utils)
|
|
154
|
+
applied.append("joint-address-enum")
|
|
155
|
+
else:
|
|
156
|
+
not_needed.append("joint-address-enum")
|
|
157
|
+
|
|
158
|
+
if _mass_matrix_broken(mujoco, controller_module):
|
|
159
|
+
_patch_mass_matrix(mujoco, binding_utils, controller_module)
|
|
160
|
+
applied.append("mass-matrix")
|
|
161
|
+
else:
|
|
162
|
+
not_needed.append("mass-matrix")
|
|
163
|
+
|
|
164
|
+
_REPORT = CompatReport(mujoco.__version__, robosuite.__version__, tuple(applied), tuple(not_needed))
|
|
165
|
+
return _REPORT
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"""What robotenv needs to know about robosuite's environment classes, read from the library itself."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import inspect
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from .params import RESERVED_ENV_KWARGS
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def env_class(name: str) -> type | None:
|
|
12
|
+
from robosuite.environments.base import REGISTERED_ENVS
|
|
13
|
+
|
|
14
|
+
return REGISTERED_ENVS.get(name)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def is_single_arm_manipulation(cls: type) -> bool:
|
|
18
|
+
from robosuite.environments.manipulation.manipulation_env import ManipulationEnv
|
|
19
|
+
from robosuite.environments.manipulation.two_arm_env import TwoArmEnv
|
|
20
|
+
|
|
21
|
+
return issubclass(cls, ManipulationEnv) and not issubclass(cls, TwoArmEnv)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def single_arm_env_names() -> list[str]:
|
|
25
|
+
from robosuite.environments.base import REGISTERED_ENVS
|
|
26
|
+
|
|
27
|
+
return sorted(n for n, c in REGISTERED_ENVS.items() if is_single_arm_manipulation(c))
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def constructor_params(cls: type) -> dict[str, inspect.Parameter]:
|
|
31
|
+
"""The named arguments ``cls(...)`` accepts, following ``**kwargs`` forwarding up the class chain.
|
|
32
|
+
|
|
33
|
+
A class contributes its own ``__init__`` arguments; its parent's are added only if it takes ``**kwargs``
|
|
34
|
+
(robosuite's task variants do, for example ``NutAssemblyRound``; ``Lift`` lists everything it accepts).
|
|
35
|
+
The most derived class wins where several declare the same argument, so its default is the effective one.
|
|
36
|
+
"""
|
|
37
|
+
params: dict[str, inspect.Parameter] = {}
|
|
38
|
+
for klass in cls.__mro__:
|
|
39
|
+
init = klass.__dict__.get("__init__")
|
|
40
|
+
if init is None or klass is object:
|
|
41
|
+
continue
|
|
42
|
+
forwards = False
|
|
43
|
+
for name, p in inspect.signature(init).parameters.items():
|
|
44
|
+
if p.kind is p.VAR_KEYWORD:
|
|
45
|
+
forwards = True
|
|
46
|
+
elif name != "self" and p.kind is not p.VAR_POSITIONAL:
|
|
47
|
+
params.setdefault(name, p)
|
|
48
|
+
if not forwards:
|
|
49
|
+
break
|
|
50
|
+
return params
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def resolved_env_kwargs(cls: type, user: dict[str, Any]) -> dict[str, Any]:
|
|
54
|
+
"""Every task argument the environment will run with: its own defaults, overridden by ``user``.
|
|
55
|
+
|
|
56
|
+
Arguments robotenv sets itself are left out. Defaults that are not plain values are shown as ``repr``.
|
|
57
|
+
"""
|
|
58
|
+
out: dict[str, Any] = {}
|
|
59
|
+
for name, p in constructor_params(cls).items():
|
|
60
|
+
if name in RESERVED_ENV_KWARGS or p.default is p.empty:
|
|
61
|
+
continue
|
|
62
|
+
out[name] = p.default
|
|
63
|
+
out.update(user)
|
|
64
|
+
plain = (bool, int, float, str, type(None), list, tuple, dict)
|
|
65
|
+
return {k: (v if isinstance(v, plain) else repr(v)) for k, v in sorted(out.items())}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
"""Parameters of ``robosuite/<EnvName>`` tasks. numpy only; robosuite is not imported here.
|
|
2
|
+
|
|
3
|
+
robosuite supplies the assets of the task: the arena, the objects and the rule that places them at reset. Success and
|
|
4
|
+
reward are defined on our side, by the ``objective``, so every task runs through the same simulation and control path.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from dataclasses import dataclass, fields
|
|
10
|
+
from typing import Any, ClassVar, Mapping
|
|
11
|
+
|
|
12
|
+
from ..._canon import as_bool, as_float, as_str, canonicalize, check_keys, freeze, thaw, within
|
|
13
|
+
from ...errors import ConfigError
|
|
14
|
+
|
|
15
|
+
# Constructor arguments of a robosuite environment that robotenv sets itself (the environment is built only to
|
|
16
|
+
# read its assets, with a stock robot and no rendering) or that have no meaning for assets. They must not appear in
|
|
17
|
+
# ``RobosuiteParams.env_kwargs``.
|
|
18
|
+
RESERVED_ENV_KWARGS = frozenset(
|
|
19
|
+
{
|
|
20
|
+
"robots", "controller_configs", "gripper_types", "base_types", "mount_types", "initial_qpos",
|
|
21
|
+
"initialization_noise", "control_freq", "horizon", "hard_reset", "ignore_done", "seed", "lite_physics",
|
|
22
|
+
"has_renderer", "has_offscreen_renderer", "use_camera_obs", "camera_names", "camera_heights",
|
|
23
|
+
"camera_widths", "camera_depths", "camera_segmentations", "renderer", "renderer_config", "render_camera",
|
|
24
|
+
"render_collision_mesh", "render_visual_mesh", "render_gpu_device_id",
|
|
25
|
+
}
|
|
26
|
+
) # fmt: skip
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@dataclass(frozen=True, slots=True)
|
|
30
|
+
class LiftObject:
|
|
31
|
+
"""Success when the centre of ``object`` is at least ``height`` metres above the world floor plane (z)."""
|
|
32
|
+
|
|
33
|
+
object: str
|
|
34
|
+
height: float
|
|
35
|
+
|
|
36
|
+
def __post_init__(self):
|
|
37
|
+
as_str(self.object, "object")
|
|
38
|
+
object.__setattr__(self, "height", as_float(self.height, "height"))
|
|
39
|
+
|
|
40
|
+
def to_dict(self) -> dict[str, Any]:
|
|
41
|
+
return {"kind": "lift_object", "object": self.object, "height": self.height}
|
|
42
|
+
|
|
43
|
+
@classmethod
|
|
44
|
+
def from_dict(cls, d: Mapping[str, Any]) -> "LiftObject":
|
|
45
|
+
check_keys(d, {"kind", "object", "height"}, "")
|
|
46
|
+
return cls(object=d["object"], height=d["height"])
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@dataclass(frozen=True, slots=True)
|
|
50
|
+
class ReachObject:
|
|
51
|
+
"""Success when the end effector is closer than ``tolerance`` metres to the centre of ``object``."""
|
|
52
|
+
|
|
53
|
+
object: str
|
|
54
|
+
tolerance: float
|
|
55
|
+
|
|
56
|
+
def __post_init__(self):
|
|
57
|
+
as_str(self.object, "object")
|
|
58
|
+
tol = as_float(self.tolerance, "tolerance")
|
|
59
|
+
if tol <= 0.0:
|
|
60
|
+
raise ConfigError("tolerance", "must be > 0")
|
|
61
|
+
object.__setattr__(self, "tolerance", tol)
|
|
62
|
+
|
|
63
|
+
def to_dict(self) -> dict[str, Any]:
|
|
64
|
+
return {"kind": "reach_object", "object": self.object, "tolerance": self.tolerance}
|
|
65
|
+
|
|
66
|
+
@classmethod
|
|
67
|
+
def from_dict(cls, d: Mapping[str, Any]) -> "ReachObject":
|
|
68
|
+
check_keys(d, {"kind", "object", "tolerance"}, "")
|
|
69
|
+
return cls(object=d["object"], tolerance=d["tolerance"])
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
Objective = LiftObject | ReachObject
|
|
73
|
+
_OBJECTIVES = {"lift_object": LiftObject, "reach_object": ReachObject}
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def objective_from_dict(d: Mapping[str, Any]) -> Objective:
|
|
77
|
+
if not isinstance(d, Mapping) or "kind" not in d:
|
|
78
|
+
raise ConfigError("", "expected a mapping with a 'kind' key")
|
|
79
|
+
cls = _OBJECTIVES.get(d["kind"])
|
|
80
|
+
if cls is None:
|
|
81
|
+
raise ConfigError("kind", f"unknown objective {d['kind']!r}; expected one of {sorted(_OBJECTIVES)}")
|
|
82
|
+
return cls.from_dict(d)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
@dataclass(frozen=True, slots=True)
|
|
86
|
+
class RobosuiteParams:
|
|
87
|
+
"""Parameters of the task ``robosuite/<EnvName>``; ``name`` is ``<EnvName>``.
|
|
88
|
+
|
|
89
|
+
``env_kwargs`` are passed to the robosuite environment when its assets are loaded (``table_full_size``,
|
|
90
|
+
``single_object_mode``, ...); everything not listed takes robosuite's own default. ``objective`` says what counts
|
|
91
|
+
as success and what the reward measures. The robot, its gripper, the controller, rendering and seeding come from
|
|
92
|
+
the other config sections and cannot be given here.
|
|
93
|
+
"""
|
|
94
|
+
|
|
95
|
+
provider: ClassVar[str] = "robosuite"
|
|
96
|
+
uses_goal: ClassVar[bool] = False # the objective defines success; TaskConfig takes no target region
|
|
97
|
+
|
|
98
|
+
name: str
|
|
99
|
+
env_kwargs: Any
|
|
100
|
+
objective: Any
|
|
101
|
+
reset_noise: float
|
|
102
|
+
terminate_on_success: bool
|
|
103
|
+
|
|
104
|
+
def __post_init__(self):
|
|
105
|
+
if not as_str(self.name, "name"):
|
|
106
|
+
raise ConfigError("name", "must name a robosuite environment, for example 'Lift'")
|
|
107
|
+
raw = self.env_kwargs
|
|
108
|
+
if isinstance(raw, tuple) and raw and raw[0] == "__map__":
|
|
109
|
+
raw = thaw(raw)
|
|
110
|
+
if not isinstance(raw, Mapping):
|
|
111
|
+
raise ConfigError("env_kwargs", f"expected a mapping, got {type(raw).__name__}")
|
|
112
|
+
reserved = sorted(set(raw) & RESERVED_ENV_KWARGS)
|
|
113
|
+
if reserved:
|
|
114
|
+
raise ConfigError(
|
|
115
|
+
"env_kwargs", f"{reserved} are set from the robot, control, sim and task config; remove them"
|
|
116
|
+
)
|
|
117
|
+
for key in raw:
|
|
118
|
+
if not isinstance(key, str) or not key.isidentifier():
|
|
119
|
+
raise ConfigError("env_kwargs", f"keys must be argument names, got {key!r}")
|
|
120
|
+
object.__setattr__(self, "env_kwargs", freeze(dict(raw), "env_kwargs"))
|
|
121
|
+
if not isinstance(self.objective, (LiftObject, ReachObject)):
|
|
122
|
+
raise ConfigError("objective", "expected LiftObject or ReachObject")
|
|
123
|
+
noise = as_float(self.reset_noise, "reset_noise")
|
|
124
|
+
if noise < 0.0:
|
|
125
|
+
raise ConfigError("reset_noise", "must be >= 0")
|
|
126
|
+
object.__setattr__(self, "reset_noise", noise)
|
|
127
|
+
as_bool(self.terminate_on_success, "terminate_on_success")
|
|
128
|
+
|
|
129
|
+
@property
|
|
130
|
+
def kwargs(self) -> dict[str, Any]:
|
|
131
|
+
"""The environment keyword arguments as a plain dict."""
|
|
132
|
+
return thaw(self.env_kwargs)
|
|
133
|
+
|
|
134
|
+
def to_dict(self) -> dict[str, Any]:
|
|
135
|
+
return {
|
|
136
|
+
"name": self.name,
|
|
137
|
+
"env_kwargs": canonicalize(self.kwargs),
|
|
138
|
+
"objective": self.objective.to_dict(),
|
|
139
|
+
"reset_noise": self.reset_noise,
|
|
140
|
+
"terminate_on_success": self.terminate_on_success,
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
@classmethod
|
|
144
|
+
def from_dict(cls, d: Mapping[str, Any]) -> "RobosuiteParams":
|
|
145
|
+
check_keys(d, {f.name for f in fields(cls)}, "")
|
|
146
|
+
return cls(
|
|
147
|
+
name=d["name"],
|
|
148
|
+
env_kwargs=d["env_kwargs"],
|
|
149
|
+
objective=within("objective", objective_from_dict, d["objective"]),
|
|
150
|
+
reset_noise=d["reset_noise"],
|
|
151
|
+
terminate_on_success=d["terminate_on_success"],
|
|
152
|
+
)
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"""Load the assets of a robosuite environment: its arena, its objects and the rule that places them.
|
|
2
|
+
|
|
3
|
+
The robosuite environment is built once, with a stock robot and without rendering, only to read those assets; it is
|
|
4
|
+
closed before this function returns and its simulation is never stepped. Needs robosuite and MuJoCo.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import xml.etree.ElementTree as ET
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
from typing import Any
|
|
12
|
+
|
|
13
|
+
import numpy as np
|
|
14
|
+
|
|
15
|
+
from ...robot import Pose
|
|
16
|
+
from ._compat import apply_compat
|
|
17
|
+
from ._introspect import constructor_params, env_class
|
|
18
|
+
|
|
19
|
+
PREFIX = "rs_" # names of everything attached from the scene start with this, in the compiled model
|
|
20
|
+
MOUNT_SITE = "mount" # the marker for the scene's mount frame; ``rs_mount`` in the compiled model
|
|
21
|
+
|
|
22
|
+
# Top-level sections that are simulator settings: ``SimConfig`` and the simulator options decide them, not robosuite.
|
|
23
|
+
_DROP_SECTIONS = ("option", "size", "visual")
|
|
24
|
+
_IK_TARGET_SUFFIX = "_eef_target" # mocap bodies robosuite's IK controllers move; nothing here uses them
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@dataclass(frozen=True)
|
|
28
|
+
class ObjectInfo:
|
|
29
|
+
name: str
|
|
30
|
+
root_body: str
|
|
31
|
+
joints: tuple[str, ...]
|
|
32
|
+
visual: bool # a marker without collision geoms (for example a target bin); not observed
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@dataclass(frozen=True)
|
|
36
|
+
class SceneAssets:
|
|
37
|
+
xml: str # arena and objects as one MJCF document, without simulator settings
|
|
38
|
+
objects: tuple[ObjectInfo, ...]
|
|
39
|
+
sampler: Any # robosuite's object placement sampler, or None
|
|
40
|
+
mount: Pose # where robosuite puts the stock robot's root body in this environment (world frame): the scene's mount frame
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def clean_xml(xml: str) -> str:
|
|
44
|
+
"""Remove simulator settings and IK mocap bodies from a robosuite MJCF document."""
|
|
45
|
+
root = ET.fromstring(xml)
|
|
46
|
+
for tag in _DROP_SECTIONS:
|
|
47
|
+
for el in root.findall(tag):
|
|
48
|
+
root.remove(el)
|
|
49
|
+
world = root.find("worldbody")
|
|
50
|
+
if world is not None:
|
|
51
|
+
for body in list(world.findall("body")):
|
|
52
|
+
if body.get("mocap") == "true" and (body.get("name") or "").endswith(_IK_TARGET_SUFFIX):
|
|
53
|
+
world.remove(body)
|
|
54
|
+
return ET.tostring(root, encoding="unicode")
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def matrix_to_quat(R: np.ndarray) -> tuple[float, float, float, float]:
|
|
58
|
+
"""A rotation matrix as a unit quaternion ``(w, x, y, z)``, ``w >= 0``."""
|
|
59
|
+
R = np.asarray(R, dtype=float).reshape(3, 3)
|
|
60
|
+
t = np.trace(R)
|
|
61
|
+
if t > 0.0:
|
|
62
|
+
s = 2.0 * np.sqrt(t + 1.0)
|
|
63
|
+
q = np.array([0.25 * s, (R[2, 1] - R[1, 2]) / s, (R[0, 2] - R[2, 0]) / s, (R[1, 0] - R[0, 1]) / s])
|
|
64
|
+
else:
|
|
65
|
+
i = int(np.argmax(np.diag(R)))
|
|
66
|
+
j, k = (i + 1) % 3, (i + 2) % 3
|
|
67
|
+
s = 2.0 * np.sqrt(R[i, i] - R[j, j] - R[k, k] + 1.0)
|
|
68
|
+
q = np.zeros(4)
|
|
69
|
+
q[0] = (R[k, j] - R[j, k]) / s
|
|
70
|
+
q[1 + i] = 0.25 * s
|
|
71
|
+
q[1 + j] = (R[j, i] + R[i, j]) / s
|
|
72
|
+
q[1 + k] = (R[k, i] + R[i, k]) / s
|
|
73
|
+
q = q / np.linalg.norm(q)
|
|
74
|
+
return tuple(float(x) for x in (q if q[0] >= 0.0 else -q))
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def add_mount_marker(xml: str, pose: Pose) -> str:
|
|
78
|
+
"""Add the mount marker, a site at ``pose`` in the world body of an MJCF document (static: it is under no moving body)."""
|
|
79
|
+
root = ET.fromstring(xml)
|
|
80
|
+
world = root.find("worldbody")
|
|
81
|
+
if world is None:
|
|
82
|
+
world = ET.SubElement(root, "worldbody")
|
|
83
|
+
ET.SubElement(world, "site", {
|
|
84
|
+
"name": MOUNT_SITE,
|
|
85
|
+
"pos": " ".join(repr(float(x)) for x in pose.pos),
|
|
86
|
+
"quat": " ".join(repr(float(x)) for x in pose.quat),
|
|
87
|
+
"size": "0.01 0.01 0.01",
|
|
88
|
+
"rgba": "0 0 0 0",
|
|
89
|
+
})
|
|
90
|
+
return ET.tostring(root, encoding="unicode")
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def load_scene(env_name: str, env_kwargs: dict[str, Any], seed: int = 0) -> SceneAssets:
|
|
94
|
+
"""Build the robosuite environment ``env_name`` once and keep its assets.
|
|
95
|
+
|
|
96
|
+
robosuite draws some of them at construction, for example the size of the cube in ``Lift``. They are drawn from
|
|
97
|
+
``seed``, so the same config gives the same scene.
|
|
98
|
+
"""
|
|
99
|
+
apply_compat()
|
|
100
|
+
import robosuite
|
|
101
|
+
from robosuite.models.tasks import ManipulationTask
|
|
102
|
+
|
|
103
|
+
seed = int(seed) % 2**32
|
|
104
|
+
cls = env_class(env_name)
|
|
105
|
+
extra = {"seed": seed} if cls is not None and "seed" in constructor_params(cls) else {} # robosuite >= 1.5.2
|
|
106
|
+
state = np.random.get_state()
|
|
107
|
+
np.random.seed(seed) # robosuite 1.5.1 draws from the global stream while it builds the environment
|
|
108
|
+
try:
|
|
109
|
+
env = robosuite.make(
|
|
110
|
+
env_name,
|
|
111
|
+
robots="Panda",
|
|
112
|
+
has_renderer=False,
|
|
113
|
+
has_offscreen_renderer=False,
|
|
114
|
+
use_camera_obs=False,
|
|
115
|
+
**extra,
|
|
116
|
+
**env_kwargs,
|
|
117
|
+
)
|
|
118
|
+
finally:
|
|
119
|
+
np.random.set_state(state)
|
|
120
|
+
try:
|
|
121
|
+
model = env.model
|
|
122
|
+
objects = list(getattr(model, "mujoco_objects", None) or [])
|
|
123
|
+
xml = ManipulationTask(mujoco_arena=model.mujoco_arena, mujoco_robots=[], mujoco_objects=objects).get_xml()
|
|
124
|
+
infos = tuple(
|
|
125
|
+
ObjectInfo(o.name, o.root_body, tuple(o.joints or ()), getattr(o, "obj_type", "all") == "visual")
|
|
126
|
+
for o in objects
|
|
127
|
+
)
|
|
128
|
+
robot = env.robots[0]
|
|
129
|
+
mount = Pose(
|
|
130
|
+
tuple(float(x) for x in np.asarray(robot.base_pos, dtype=float)),
|
|
131
|
+
matrix_to_quat(np.asarray(robot.base_ori, dtype=float)),
|
|
132
|
+
)
|
|
133
|
+
return SceneAssets(
|
|
134
|
+
xml=add_mount_marker(clean_xml(xml), mount),
|
|
135
|
+
objects=infos,
|
|
136
|
+
sampler=getattr(env, "placement_initializer", None),
|
|
137
|
+
mount=mount,
|
|
138
|
+
)
|
|
139
|
+
finally:
|
|
140
|
+
env.close()
|