Skip to content

Commit

Permalink
Merge branch 'master_new_binding' of github.com:ARISE-Initiative/robo…
Browse files Browse the repository at this point in the history
…suite-dev into master_new_binding
  • Loading branch information
yukezhu committed Nov 1, 2022
2 parents dc196c4 + 6aea7bc commit 646782f
Show file tree
Hide file tree
Showing 270 changed files with 6,013,224 additions and 434 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,7 @@ mjkey.txt
*.jpg
.idea

.pytest_cache/
.pytest_cache/

# private macros
macros_private.py
4 changes: 2 additions & 2 deletions docs/algorithms/sim2real.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ By default, Mujoco sensors are deterministic and delay-free, which is often an u
}
```

Note that for memory efficiency the `image-state` is not returned by default (this can be toggled in `robosuite/utils/macros.py`).
Note that for memory efficiency the `image-state` is not returned by default (this can be toggled in `robosuite/macros.py`).

We showcase how the `Observable` functionality can be used to model sensor corruption and delay via [demo_sensor_corruption.py](../demos.html#sensor-realism). We also highlight that each of the `sensor`, `corrupter`, and `filter` functions can be arbitrarily specified to suit the end-user's usage. For example, a common use case for these observables is to keep track of sampled values from a sensor operating at a higher frequency than the environment step (control) frequency. In this case, the `filter` function can be leveraged to keep track of the real-time sensor values as they're being sampled. We provide a minimal script showcasing this ability below:

Expand Down Expand Up @@ -154,4 +154,4 @@ print(f"\nPolicy Frequency: {control_freq}, Observable Sampling Frequency: {obs_
print(f"Number of recorded samples after 1 policy step: {buffer._size}\n")
for i in range(buffer._size):
print(f"Recorded value {i}: {buffer.buf[i]}")
```
```
2 changes: 1 addition & 1 deletion docs/modules/sensors.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ Cameras bundle a name to a set of properties to render images of the environment

For more information on the vision ground-truth sensors supported, please see the [Renderer](./renderers) section.

Note that for memory efficiency the `image-state` is not returned by default (this can be toggled in `robosuite/utils/macros.py`).
Note that for memory efficiency the `image-state` is not returned by default (this can be toggled in `robosuite/macros.py`).

Observables can also be used to model sensor corruption and delay, and refer the reader to the [Sensor Randomization](../algorithms/sim2real.html#sensors) section for additional information.
4 changes: 2 additions & 2 deletions docs/source/robosuite.utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ robosuite.utils.input\_utils module
:undoc-members:
:show-inheritance:

robosuite.utils.macros module
robosuite.macros module
-----------------------------

.. automodule:: robosuite.utils.macros
.. automodule:: robosuite.macros
:members:
:undoc-members:
:show-inheritance:
Expand Down
17 changes: 5 additions & 12 deletions robosuite/controllers/base_controller.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import abc
from collections.abc import Iterable
import numpy as np

import robosuite.utils.macros as macros
import mujoco
import numpy as np

if macros.USE_DM_BINDING:
import mujoco
else:
import mujoco_py
import robosuite.macros as macros


class Controller(object, metaclass=abc.ABCMeta):
Expand Down Expand Up @@ -155,12 +152,8 @@ def update(self, force=False):
self.J_ori = np.array(self.sim.data.get_site_jacr(self.eef_name).reshape((3, -1))[:, self.qvel_index])
self.J_full = np.array(np.vstack([self.J_pos, self.J_ori]))

if macros.USE_DM_BINDING:
mass_matrix = np.ndarray(shape=(self.sim.model.nv, self.sim.model.nv), dtype=np.float64, order='C')
mujoco.mj_fullM(self.sim.model._model, mass_matrix, self.sim.data.qM)
else:
mass_matrix = np.ndarray(shape=(len(self.sim.data.qvel) ** 2,), dtype=np.float64, order='C')
mujoco_py.cymj._mj_fullM(self.sim.model, mass_matrix, self.sim.data.qM)
mass_matrix = np.ndarray(shape=(self.sim.model.nv, self.sim.model.nv), dtype=np.float64, order="C")
mujoco.mj_fullM(self.sim.model._model, mass_matrix, self.sim.data.qM)
mass_matrix = np.reshape(mass_matrix, (len(self.sim.data.qvel), len(self.sim.data.qvel)))
self.mass_matrix = mass_matrix[self.qvel_index, :][:, self.qvel_index]

Expand Down
2 changes: 1 addition & 1 deletion robosuite/demos/demo_domain_randomization.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Script to showcase domain randomization functionality.
"""

import robosuite.utils.macros as macros
import robosuite.macros as macros
from robosuite.controllers import load_controller_config
from robosuite.utils.input_utils import *
from robosuite.wrappers import DomainRandomizationWrapper
Expand Down
2 changes: 1 addition & 1 deletion robosuite/demos/demo_video_recording.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import imageio
import numpy as np

import robosuite.utils.macros as macros
import robosuite.macros as macros
from robosuite import make

# Set the image convention to opencv so that the images are automatically rendered "right side up" when using imageio
Expand Down
58 changes: 11 additions & 47 deletions robosuite/environments/base.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
from collections import OrderedDict

import numpy as np
from mujoco_py import MjRenderContextOffscreen, MjSim, load_model_from_xml

import robosuite.utils.macros as macros
import robosuite.macros as macros
from robosuite.models.base import MujocoModel
from robosuite.renderers.base import load_renderer_config
from robosuite.utils import SimulationError, XMLError

if macros.USE_DM_BINDING:
from robosuite.utils import PygameRenderer, OpenCVRenderer
from robosuite.utils.binding_utils import MjSim, MjRenderContextOffscreen
else:
from robosuite.renderers.mujoco.mujoco_py_renderer import MujocoPyRenderer
from mujoco_py import MjSim, MjRenderContextOffscreen, load_model_from_xml
from robosuite.utils import OpenCVRenderer, PygameRenderer, SimulationError, XMLError
from robosuite.utils.binding_utils import MjRenderContextOffscreen, MjSim

REGISTERED_ENVS = {}

Expand Down Expand Up @@ -103,18 +96,11 @@ def __init__(
renderer="mujoco",
renderer_config=None,
):
if not macros.USE_DM_BINDING:
# First, verify that both the on- and off-screen renderers are not being used simultaneously
if has_renderer is True and has_offscreen_renderer is True:
raise ValueError("the onscreen and offscreen renderers cannot be used simultaneously.")

# Rendering-specific attributes
self.has_renderer = has_renderer
if macros.USE_DM_BINDING:
# offscreen renderer needed for on-screen rendering
self.has_offscreen_renderer = (has_renderer or has_offscreen_renderer)
else:
self.has_offscreen_renderer = has_offscreen_renderer
# offscreen renderer needed for on-screen rendering
self.has_offscreen_renderer = has_renderer or has_offscreen_renderer
self.render_camera = render_camera
self.render_collision_mesh = render_collision_mesh
self.render_visual_mesh = render_visual_mesh
Expand Down Expand Up @@ -243,16 +229,9 @@ def _initialize_sim(self, xml_string=None):
Args:
xml_string (str): If specified, creates MjSim object from this filepath
"""
if macros.USE_DM_BINDING:
xml = xml_string if xml_string else self.model.get_xml()
# Create the simulation instance
self.sim = MjSim.from_xml_string(xml)
else:
# if we have an xml string, use that to create the sim. Otherwise, use the local model
self.mjpy_model = load_model_from_xml(xml_string) if xml_string else self.model.get_model(mode="mujoco_py")

# Create the simulation instance
self.sim = MjSim(self.mjpy_model)
xml = xml_string if xml_string else self.model.get_xml()
# Create the simulation instance
self.sim = MjSim.from_xml_string(xml)

# run a single step to make sure changes have propagated through sim state
self.sim.forward()
Expand Down Expand Up @@ -310,20 +289,8 @@ def _reset_internal(self):

# create visualization screen or renderer
if self.has_renderer and self.viewer is None:
if macros.USE_DM_BINDING:
# self.viewer = PygameRenderer(self.sim)
self.viewer = OpenCVRenderer(self.sim)
else:
self.viewer = MujocoPyRenderer(self.sim)
self.viewer.viewer.vopt.geomgroup[0] = (1 if self.render_collision_mesh else 0)
self.viewer.viewer.vopt.geomgroup[1] = (1 if self.render_visual_mesh else 0)

# hiding the overlay speeds up rendering significantly
self.viewer.viewer._hide_overlay = True

# make sure mujoco-py doesn't block rendering frames
# (see https://github.com/StanfordVL/robosuite/issues/39)
self.viewer.viewer._render_every_frame = True
# self.viewer = PygameRenderer(self.sim)
self.viewer = OpenCVRenderer(self.sim)

# Set the camera angle for viewing
if self.render_camera is not None:
Expand All @@ -333,9 +300,6 @@ def _reset_internal(self):
if self.has_offscreen_renderer:
if self.sim._render_context_offscreen is None:
render_context = MjRenderContextOffscreen(self.sim, device_id=self.render_gpu_device_id)
if not macros.USE_DM_BINDING:
# TODO: we kept this line for consistency with old code with old binding, but should probably remove it
self.sim.add_render_context(render_context)
self.sim._render_context_offscreen.vopt.geomgroup[0] = 1 if self.render_collision_mesh else 0
self.sim._render_context_offscreen.vopt.geomgroup[1] = 1 if self.render_visual_mesh else 0

Expand Down Expand Up @@ -696,7 +660,7 @@ def _destroy_sim(self):
"""
Destroys the current MjSim instance if it exists
"""
if macros.USE_DM_BINDING and (self.sim is not None):
if self.sim is not None:
self.sim.free()
self.sim = None

Expand Down
2 changes: 1 addition & 1 deletion robosuite/environments/robot_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import numpy as np

import robosuite.utils.macros as macros
import robosuite.macros as macros
from robosuite.controllers import reset_controllers
from robosuite.environments.base import MujocoEnv
from robosuite.robots import ROBOT_CLASS_MAPPING
Expand Down
17 changes: 13 additions & 4 deletions robosuite/utils/macros.py → robosuite/macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@
To make sure global reference is maintained, should import these settings as:
`import robosuite.utils.macros as macros`
`import robosuite.macros as macros`
"""

# Global Mujoco Simulation Parameters
SIMULATION_TIMESTEP = 0.002 # Internal simulation timestep (in seconds)

# Whether to use the new DeepMind MuJoCo python binding instead of OpenAI mujoco-py binding (default)
USE_DM_BINDING = True

# Instance Randomization
# Used if we want to randomize geom groups uniformly per instance -- e.g.: entire robot arm, vs. per-joint geom
# This should get set to True in your script BEFORE an environment is created or the DR wrapper is used
Expand All @@ -34,3 +31,15 @@
# In general, observations are concatenated together by modality. However, image observations are expensive memory-wise,
# so we skip concatenating all images together by default, unless this flag is set to True
CONCATENATE_IMAGES = False


try:
from robosuite.macros_private import *
except ImportError:
from robosuite.utils.log_utils import log_warning

log_warning(
"No private macro file found!"
"\nIt is recommended to use a private macro file"
"\nTo setup, run: python robosuite/scripts/setup_macros.py"
)
4 changes: 1 addition & 3 deletions robosuite/models/assets/arenas/bins_arena.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@
<geom pos="-0.15 -0.2 -0.4" size="0.01 0.4" type="cylinder" conaffinity="0" contype="0" group="1" name="bin2_leg3_visual" material="table_legs_metal"/>
<geom pos="0.15 -0.2 -0.4" size="0.01 0.4" type="cylinder" conaffinity="0" contype="0" group="1" name="bin2_leg4_visual" material="table_legs_metal"/>
</body>

<light diffuse=".8 .8 .8" dir="0 -.15 -1" directional="false" pos="1 1 4.0" specular="0.3 0.3 0.3" castshadow="false"/>
<light diffuse=".8 .8 .8" dir="0 -.15 -1" directional="false" pos="-3. -3. 4.0" specular="0.3 0.3 0.3" castshadow="false"/>
<light pos="1.0 1.0 1.5" dir="-0.2 -0.2 -1" specular="0.3 0.3 0.3" directional="true" castshadow="false"/>
<!-- front view -->
<camera mode="fixed" name="frontview" pos="1.6 0 1.45" quat="0.56 0.43 0.43 0.56"/>
<!-- bird view -->
Expand Down
3 changes: 1 addition & 2 deletions robosuite/models/assets/arenas/empty_arena.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
<geom pos="1.25 -3 1.5" quat="0.7071 -0.7071 0 0" size="1.75 1.5 0.01" type="box" conaffinity="0" contype="0" group="1" name="wall_right_visual" material="walls_mat"/>
<geom pos="-2 0 1.5" quat="0.5 0.5 0.5 0.5" size="1.5 1.5 0.01" type="box" conaffinity="0" contype="0" group="1" name="wall_rear_visual" material="walls_mat"/>
<geom pos="3 0 1.5" quat="0.5 0.5 -0.5 -0.5" size="3 1.5 0.01" type="box" conaffinity="0" contype="0" group="1" name="wall_front_visual" material="walls_mat"/>
<light diffuse=".8 .8 .8" dir="0 -.15 -1" directional="false" pos="1 1 4.0" specular="0.3 0.3 0.3" castshadow="false"/>
<light diffuse=".8 .8 .8" dir="0 -.15 -1" directional="false" pos="-3. -3. 4.0" specular="0.3 0.3 0.3" castshadow="false"/>
<light pos="1.0 1.0 1.5" dir="-0.2 -0.2 -1" specular="0.3 0.3 0.3" directional="true" castshadow="false"/>
<!-- front view -->
<camera mode="fixed" name="frontview" pos="1.6 0 1.45" quat="0.56 0.43 0.43 0.56"/>
<!-- bird view -->
Expand Down
4 changes: 1 addition & 3 deletions robosuite/models/assets/arenas/pegs_arena.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@
<geom pos="0 0 0" size="0.02 0.1" type="cylinder" group="0" friction="1 0.005 0.0001"/>
<geom pos="0 0 0" size="0.02 0.1" type="cylinder" conaffinity="0" contype="0" group="1" material="smetal" />
</body>

<light diffuse=".8 .8 .8" dir="0 -.15 -1" directional="false" pos="1 1 4.0" specular="0.3 0.3 0.3" castshadow="false"/>
<light diffuse=".8 .8 .8" dir="0 -.15 -1" directional="false" pos="-3. -3. 4.0" specular="0.3 0.3 0.3" castshadow="false"/>
<light pos="1.0 1.0 1.5" dir="-0.2 -0.2 -1" specular="0.3 0.3 0.3" directional="true" castshadow="false"/>
<!-- front view -->
<camera mode="fixed" name="frontview" pos="1.6 0 1.45" quat="0.56 0.43 0.43 0.56"/>
<!-- bird view -->
Expand Down
7 changes: 3 additions & 4 deletions robosuite/models/assets/arenas/table_arena.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@
<geom pos="0 0 0" size="0.05 0.1" type="cylinder" conaffinity="0" contype="0" group="1" name="table_leg3_visual" material="table_legs_metal"/>
<geom pos="0 0 0" size="0.05 0.1" type="cylinder" conaffinity="0" contype="0" group="1" name="table_leg4_visual" material="table_legs_metal"/>
</body>

<light name="light1" diffuse=".8 .8 .8" dir="0 -.15 -1" directional="false" pos="1 1 4.0" specular="0.3 0.3 0.3" castshadow="false"/>
<light name="light2" diffuse=".8 .8 .8" dir="0 -.15 -1" directional="false" pos="-3. -3. 4.0" specular="0.3 0.3 0.3" castshadow="false"/>
<light pos="1.0 1.0 1.5" dir="-0.2 -0.2 -1" specular="0.3 0.3 0.3" directional="true" castshadow="false"/>
<!-- front view -->
<camera mode="fixed" name="frontview" pos="1.6 0 1.45" quat="0.56 0.43 0.43 0.56"/>
<!-- bird view -->
Expand All @@ -49,5 +47,6 @@
<camera mode="fixed" name="agentview" pos="0.5 0 1.35" quat="0.653 0.271 0.271 0.653"/>
<!-- side view -->
<camera mode="fixed" name="sideview" pos="-0.05651774593317116 1.2761224129427358 1.4879572214102434" quat="0.009905065491771751 0.006877963156909582 0.5912228352893879 0.806418094001364" />

</worldbody>
</mujoco>
</mujoco>
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@

<tendon>
<!--Middlefinger tendons-->
<fixed name="thumb_cpl" range="-5 5" stiffness="3.0" springlength="0.2" frictionloss="0.4">
<fixed name="thumb_cpl" range="-5 5" stiffness="3.0" springlength="0.2" frictionloss="0.4" limited="true">
<joint joint="joint_thumb" coef="0.4"/>
<joint joint="joint_thumb_distal" coef="-0.4"/>
</fixed>

<!--finger2 tendons-->
<fixed name="index_12_cpl" range="-5 5" stiffness="3.0" springlength="0.2" frictionloss="0.4">
<fixed name="index_12_cpl" range="-5 5" stiffness="3.0" springlength="0.2" frictionloss="0.4" limited="true">
<joint joint="joint_index" coef="0.4"/>
<joint joint="joint_index_distal" coef="-0.4"/>
</fixed>

<!--Finger1 tendons-->
<fixed name="pinky_12_cpl" range="-5 5" stiffness="3.0" springlength="0.2" frictionloss="0.4">
<fixed name="pinky_12_cpl" range="-5 5" stiffness="3.0" springlength="0.2" frictionloss="0.4" limited="true">
<joint joint="joint_pinky" coef="0.4"/>
<joint joint="joint_pinky_distal" coef="-0.4"/>
</fixed>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Blender 3.3.1 MTL File: 'None'
# www.blender.org

newmtl Material_001
Ns 250.000000
Ka 1.000000 1.000000 1.000000
Kd 0.001651 0.001651 0.001651
Ks 0.500000 0.500000 0.500000
Ke 0.100000 0.100000 0.100000
Ni 1.000000
d 1.000000
illum 2
Loading

0 comments on commit 646782f

Please sign in to comment.