Skip to content

Commit 073e955

Browse files
nimrod-gileadicopybara-github
authored andcommitted
Reset physics time after settling physics in PropPlacer.
Before this change, physics.data.time would be nonzero when PropPlacer is used with settle_physics=True, leading to episodes of variable length when a time limit is set. PiperOrigin-RevId: 689823887 Change-Id: Ie759cb8331906b224b635ed3321f7dd2216dc90b
1 parent bce0621 commit 073e955

2 files changed

Lines changed: 23 additions & 16 deletions

File tree

dm_control/composer/initializers/prop_initializer.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -237,19 +237,23 @@ def place_and_settle():
237237

238238
# Step physics and check prop states.
239239
original_time = physics.data.time
240-
props_isolator = utils.JointStaticIsolator(physics, self._prop_joints)
241-
prop_joints_mj = physics.bind(self._prop_joints)
242-
while physics.data.time - original_time < self._max_settle_physics_time:
243-
with props_isolator:
244-
physics.step()
245-
max_qvel = np.max(np.abs(prop_joints_mj.qvel))
246-
max_qacc = np.max(np.abs(prop_joints_mj.qacc))
247-
if (max_qvel < self._max_qvel_tol) and (
248-
max_qacc < self._max_qacc_tol) and (
249-
physics.data.time - original_time
250-
) > self._min_settle_physics_time:
251-
return True
252-
physics.data.time = original_time
240+
try:
241+
props_isolator = utils.JointStaticIsolator(physics, self._prop_joints)
242+
prop_joints_mj = physics.bind(self._prop_joints)
243+
while (
244+
physics.data.time - original_time < self._max_settle_physics_time
245+
):
246+
with props_isolator:
247+
physics.step()
248+
max_qvel = np.max(np.abs(prop_joints_mj.qvel))
249+
max_qacc = np.max(np.abs(prop_joints_mj.qacc))
250+
if (max_qvel < self._max_qvel_tol) and (
251+
max_qacc < self._max_qacc_tol) and (
252+
physics.data.time - original_time
253+
) > self._min_settle_physics_time:
254+
return True
255+
finally:
256+
physics.data.time = original_time
253257

254258
if self._raise_exception_on_settle_failure:
255259
raise RuntimeError(

dm_control/composer/initializers/prop_initializer_test.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
# limitations under the License.
1414
# ============================================================================
1515

16-
"""Tests for prop_initializer."""
17-
1816
from absl.testing import absltest
1917
from absl.testing import parameterized
2018
from dm_control import composer
@@ -160,6 +158,8 @@ def test_ignore_contacts_with_entities(self):
160158
def test_settle_physics(self, settle_physics):
161159
radius = 0.1
162160
physics, spheres = _make_spheres(num_spheres=2, radius=radius, nconmax=1)
161+
physics_start_time = 1337.0
162+
physics.data.time = physics_start_time
163163

164164
# Only place the first sphere.
165165
prop_placer = prop_initializer.PropPlacer(
@@ -180,7 +180,10 @@ def test_settle_physics(self, settle_physics):
180180
del second_quaternion # Unused.
181181

182182
# The sphere that we were not placing should not have moved.
183-
self.assertEqual(second_position[2], 0.)
183+
self.assertEqual(second_position[2], 0.0)
184+
self.assertEqual(
185+
physics.data.time, physics_start_time, 'Physics time should be reset.'
186+
)
184187

185188
@parameterized.parameters([0, 1, 2, 3])
186189
def test_settle_physics_multiple_attempts(self, max_settle_physics_attempts):

0 commit comments

Comments
 (0)