Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions reaf/simulation/torch/_src/core/torch_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,10 @@ def __init__(
model_wrapper.actuator_ctrladr[self.id],
model_wrapper.actuator_ctrlnum[self.id],
)
self.act_id = _span_indices(
model_wrapper.actuator_actadr[self.id],
model_wrapper.actuator_actnum[self.id],
)
self.out_id = _actuator_out_indices(model_wrapper, self.id)

def __getname(self, name: str): # pylint: disable=unused-private-member
Expand All @@ -848,10 +852,10 @@ def __getname(self, name: str): # pylint: disable=unused-private-member
if self.prefix == "sensor_":
return name
raise AttributeError("sensordata is not available for this type")
if name == "ctrl":
if name in ("ctrl", "act", "act_dot"):
if self.prefix == "actuator_":
return name
raise AttributeError("ctrl is not available for this type")
raise AttributeError(f"{name} is not available for this type")
if (
name == "qpos"
or name == "qvel"
Expand All @@ -875,6 +879,8 @@ def _get_indices(self, name: str) -> torch.Tensor:
return self.dof_id
if name == "ctrl":
return self.ctrl_id
if name in ("act", "act_dot"):
return self.act_id
if self.prefix == "actuator_" and name in ("length", "velocity", "force"):
# Map actuator IDs to output spans (length, velocity, force).
return self.out_id
Expand Down
86 changes: 86 additions & 0 deletions reaf/simulation/torch/_src/core/torch_wrapper_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,92 @@ def test_mimo_actuators_bind(self):
)
self.assertEqual(d.ctrl[5], 99.0)

@parameterized.parameters(types.Impl.CPP, types.Impl.WARP)
def test_actuator_act_bind(self, backend: types.Impl):
xml = """
<mujoco>
<worldbody>
<body>
<joint name="j0" type="hinge"/>
<geom name="g0" size="0.1"/>
</body>
<body>
<joint name="j1" type="hinge" range="-1 1"/>
<geom name="g1" size="0.1"/>
</body>
<body>
<joint name="j2" type="hinge"/>
<geom name="g2" size="0.1"/>
</body>
</worldbody>
<actuator>
<motor name="m0" joint="j0"/>
<muscle name="mus1" joint="j1"/>
<general name="u2" joint="j2" dyntype="user" actdim="2"/>
</actuator>
</mujoco>
"""
spec = mujoco.MjSpec.from_string(xml)
m = spec.compile()
d = mujoco.MjData(m)
d.act[:] = [0.25, 0.5, 0.75]
d.act_dot[:] = [1.5, -0.5, 2.0]

nworld = 2 if backend == types.Impl.WARP else 1
mw = wrap_model(backend, m, nworld=nworld)
dw = wrap_data(backend, m, d, nworld=nworld)
device = dw.act.device

self.assertEqual(dw.bind(mw, spec.actuators[0]).act.shape, (nworld, 0))
np.testing.assert_allclose(
dw.bind(mw, spec.actuators[1]).act.cpu().numpy(),
np.full((nworld,), 0.25),
)
np.testing.assert_allclose(
dw.bind(mw, spec.actuators[2]).act.cpu().numpy(),
np.broadcast_to([0.5, 0.75], (nworld, 2)),
)
np.testing.assert_allclose(
dw.bind(mw, spec.actuators).act.cpu().numpy(),
np.broadcast_to([0.25, 0.5, 0.75], (nworld, 3)),
)
np.testing.assert_allclose(
dw.bind(mw, spec.actuators).act_dot.cpu().numpy(),
np.broadcast_to([1.5, -0.5, 2.0], (nworld, 3)),
)

dw.bind(mw, spec.actuators[1]).set(
"act",
torch.full((nworld,), 0.9, dtype=dw.act.dtype, device=device),
)
np.testing.assert_allclose(
dw.bind(mw, spec.actuators[1]).act.cpu().numpy(),
np.full((nworld,), 0.9),
)

if backend == types.Impl.WARP:
env_mask = torch.tensor([True, False], device=device)
dw.bind(mw, spec.actuators[2]).set(
"act",
torch.tensor(
[[0.1, 0.2], [0.8, 0.9]], dtype=dw.act.dtype, device=device
),
env_mask=env_mask,
)
np.testing.assert_allclose(
dw.bind(mw, spec.actuators[2]).act.cpu().numpy(),
[[0.1, 0.2], [0.5, 0.75]],
)

with self.assertRaisesRegex(
AttributeError, "act is not available for this type"
):
_ = dw.bind(mw, spec.geoms).act
with self.assertRaisesRegex(
AttributeError, "act_dot is not available for this type"
):
_ = dw.bind(mw, spec.geoms).act_dot


def _dtype_matches(mj_dtype: torch.dtype, mjw_dtype: torch.dtype) -> bool:
"""Checks dtypes match, allowing 64-bit CPU vs 32-bit GPU."""
Expand Down
Loading