Skip to content

Commit f2e246b

Browse files
saran-tcopybara-github
authored andcommitted
Make sure that contact_force always returns the correct values.
The `physics.step` function in `dm_control` runs `mj_step2` followed by `mj_step1`. This leaves the physics in a state where `mj_fwdPosition` has been called, and therefore the contact frames have been updated, but`mj_fwdConstraint` has not, and therefore the contact forces are stale. This change forces the portion of `mj_step2` that is needed to guarantee correct contact forces to be always be run as part of the `contact_force` function. Fixes google-deepmind/mujoco#13. PiperOrigin-RevId: 490922782 Change-Id: I0a3998ff4759aee70321a636c189e44de2f3eca5
1 parent e3d4c0a commit f2e246b

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

dm_control/mujoco/wrapper/core.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,12 @@ def contact_force(self, contact_id):
521521
if not 0 <= contact_id < self.ncon:
522522
raise ValueError(_CONTACT_ID_OUT_OF_RANGE
523523
.format(max_valid=self.ncon-1, actual=contact_id))
524+
525+
# Run the portion of `mj_step2` that are needed for correct contact forces.
526+
mujoco.mj_fwdActuation(self._model.ptr, self._data)
527+
mujoco.mj_fwdAcceleration(self._model.ptr, self._data)
528+
mujoco.mj_fwdConstraint(self._model.ptr, self._data)
529+
524530
wrench = np.empty(6, dtype=np.float64)
525531
mujoco.mj_contactForce(self._model.ptr, self._data, contact_id, wrench)
526532
return wrench.reshape(2, 3)

0 commit comments

Comments
 (0)