Skip to content

Commit

Permalink
Add ctrlrange == jntrange test.
Browse files Browse the repository at this point in the history
  • Loading branch information
vaxenburg committed Mar 8, 2024
1 parent 932aa87 commit 0d02fc4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
2 changes: 1 addition & 1 deletion tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ def test_can_create_and_run_environment():
assert timestep.reward is not None

# For local testing only.
if os.environ['MUJOCO_GL'] == 'egl':
if 'MUJOCO_GL' in os.environ and os.environ['MUJOCO_GL'] == 'egl':
_ = env.physics.render()
36 changes: 22 additions & 14 deletions tests/test_flymodel.py → tests/test_flybare.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"""Test fly body parameters and pure physics (outside of RL environment)."""
"""Test stand-alone fly model outside of RL environment."""

import os
import numpy as np
from dm_control import mjcf

from dm_control import mjcf
import flybody

xml_path = os.path.join(os.path.dirname(__file__),
'../flybody/fruitfly/assets/fruitfly.xml')
flybody_path = os.path.dirname(flybody.__file__)
xml_path = os.path.join(flybody_path, 'fruitfly/assets/fruitfly.xml')

expect = {
'nq': 109, # Generalized coordinates.
Expand All @@ -29,30 +30,37 @@


def test_model_parameters():

mjcf_model = mjcf.from_path(xml_path)
physics = mjcf.Physics.from_mjcf_model(mjcf_model)

physics = mjcf.Physics.from_xml_path(xml_path)
for k, v in expect.items():
assert getattr(physics.model, k) == v

assert np.isclose(
physics.named.model.body_subtreemass['thorax'],
expect_close['fly_mass'])


def test_can_compile_and_step_simulation():
def test_control_ranges_match_joint_ranges():
physics = mjcf.Physics.from_xml_path(xml_path)
m = physics.model
for i in range(physics.model.nu):
# Consider only joint actuators that are also position actuators.
if m.actuator_trntype[i] == 0 and m.actuator_biastype[i] == 1:
actuator_name = m.id2name(i, 'actuator')
joint_name = m.id2name(m.actuator_trnid[i][0], 'joint')
joint_id = m.actuator_trnid[i][0]
ctrl_range = m.actuator_ctrlrange[i]
joint_range = m.jnt_range[joint_id]
assert actuator_name == joint_name
assert all(ctrl_range == joint_range)

mjcf_model = mjcf.from_path(xml_path)
physics = mjcf.Physics.from_mjcf_model(mjcf_model)

def test_can_compile_and_step_simulation():
physics = mjcf.Physics.from_xml_path(xml_path)
physics.reset()
for _ in range(100):
physics.data.ctrl[:] = np.random.uniform(-.2, .2, physics.model.nu)
physics.step()
assert isinstance(physics.data.sensordata, np.ndarray)

# For local testing only.
if os.environ['MUJOCO_GL'] == 'egl':
if 'MUJOCO_GL' in os.environ and os.environ['MUJOCO_GL'] == 'egl':
_ = physics.render()

2 changes: 1 addition & 1 deletion tests/test_flywalker.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
'force', 'touch', 'accelerometer', 'gyro', 'velocimeter',
'actuator_activation', 'appendages_pos']
# For local testing only.
if os.environ['MUJOCO_GL'] == 'egl':
if 'MUJOCO_GL' in os.environ and os.environ['MUJOCO_GL'] == 'egl':
OBS_NAMES = OBS_NAMES + ['right_eye', 'left_eye']

# Prepare all possible configurations to test fly walker with.
Expand Down

0 comments on commit 0d02fc4

Please sign in to comment.