|
| 1 | +# Copyright 2023 DeepMind Technologies Limited |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================== |
| 15 | +"""Tests for inverse dynamics functions.""" |
| 16 | +from absl.testing import absltest |
| 17 | +from absl.testing import parameterized |
| 18 | +import jax |
| 19 | +from jax import numpy as jp |
| 20 | +import mujoco |
| 21 | +from mujoco import mjx |
| 22 | +from mujoco.mjx._src import support |
| 23 | +from mujoco.mjx._src import test_util |
| 24 | +import numpy as np |
| 25 | + |
| 26 | +# tolerance for difference between MuJoCo and MJX calculations - mostly |
| 27 | +# due to float precision |
| 28 | +_TOLERANCE = 1e-5 |
| 29 | + |
| 30 | + |
| 31 | +def _assert_eq(a, b, name, tol=_TOLERANCE): |
| 32 | + tol = tol * 10 # avoid test noise |
| 33 | + err_msg = f'mismatch: {name}' |
| 34 | + np.testing.assert_allclose(a, b, err_msg=err_msg, atol=tol, rtol=tol) |
| 35 | + |
| 36 | + |
| 37 | +class InverseTest(parameterized.TestCase): |
| 38 | + |
| 39 | + @parameterized.parameters( |
| 40 | + (mujoco.mjtIntegrator.mjINT_EULER, False, False), |
| 41 | + (mujoco.mjtIntegrator.mjINT_EULER, False, True), |
| 42 | + (mujoco.mjtIntegrator.mjINT_EULER, True, False), |
| 43 | + (mujoco.mjtIntegrator.mjINT_EULER, True, True), |
| 44 | + (mujoco.mjtIntegrator.mjINT_IMPLICITFAST, False, False), |
| 45 | + (mujoco.mjtIntegrator.mjINT_IMPLICITFAST, True, False), |
| 46 | + ) |
| 47 | + def test_forward_inverse_match(self, integrator, invdiscrete, eulerdamp): |
| 48 | + m = mujoco.MjModel.from_xml_string(""" |
| 49 | + <mujoco> |
| 50 | + <option timestep=".005" gravity="-1 -1 -10"/> |
| 51 | + <worldbody> |
| 52 | + <geom type="plane" size="10 10 .001"/> |
| 53 | + <body pos="0 0 1"> |
| 54 | + <geom type="sphere" size=".1" pos=".1 .2 .3"/> |
| 55 | + <joint name="jnt1" type="hinge" axis="0 1 0" stiffness=".25" damping=".125"/> |
| 56 | + <body pos="0 0 1"> |
| 57 | + <geom type="sphere" size=".1" pos=".1 .2 .3"/> |
| 58 | + <joint name="jnt2" type="hinge" axis="0 1 0" stiffness=".6" damping=".3"/> |
| 59 | + </body> |
| 60 | + </body> |
| 61 | + </worldbody> |
| 62 | + <actuator> |
| 63 | + <motor joint="jnt1"/> |
| 64 | + </actuator> |
| 65 | + <equality> |
| 66 | + <joint joint1="jnt1" joint2="jnt2"/> |
| 67 | + </equality> |
| 68 | + </mujoco> |
| 69 | + """) |
| 70 | + m.opt.integrator = integrator |
| 71 | + if invdiscrete: |
| 72 | + m.opt.enableflags |= mujoco.mjtEnableBit.mjENBL_INVDISCRETE |
| 73 | + if not eulerdamp: |
| 74 | + m.opt.disableflags |= mujoco.mjtDisableBit.mjDSBL_EULERDAMP |
| 75 | + |
| 76 | + d = mujoco.MjData(m) |
| 77 | + d.qvel = np.random.uniform(low=-0.01, high=0.01, size=d.qvel.shape) |
| 78 | + d.ctrl = np.random.uniform(low=-0.01, high=0.01, size=d.ctrl.shape) |
| 79 | + d.qfrc_applied = np.random.uniform( |
| 80 | + low=-0.01, high=0.01, size=d.qfrc_applied.shape |
| 81 | + ) |
| 82 | + d.xfrc_applied = np.random.uniform( |
| 83 | + low=-0.01, high=0.01, size=d.xfrc_applied.shape |
| 84 | + ) |
| 85 | + mujoco.mj_step(m, d, 100) |
| 86 | + |
| 87 | + mx = mjx.put_model(m) |
| 88 | + dx = mjx.put_data(m, d) |
| 89 | + dx_next = mjx.step(mx, dx) |
| 90 | + qacc_fd = (dx_next.qvel - dx.qvel) / mx.opt.timestep |
| 91 | + |
| 92 | + dx = mjx.forward(mx, dx) |
| 93 | + |
| 94 | + if invdiscrete: |
| 95 | + dx = dx.replace(qacc=qacc_fd) |
| 96 | + |
| 97 | + dxinv = mjx.inverse(mx, dx) |
| 98 | + |
| 99 | + fwdinv0 = jp.linalg.norm( |
| 100 | + dxinv.qfrc_constraint - dx.qfrc_constraint, ord=np.inf |
| 101 | + ) |
| 102 | + fwdinv1 = jp.linalg.norm( |
| 103 | + dxinv.qfrc_inverse |
| 104 | + - ( |
| 105 | + dx.qfrc_applied + dx.qfrc_actuator + support.xfrc_accumulate(mx, dx) |
| 106 | + ), |
| 107 | + ord=np.inf, |
| 108 | + ) |
| 109 | + |
| 110 | + self.assertLess(fwdinv0, 1.0e-3) |
| 111 | + self.assertLess(fwdinv1, 1.0e-3) |
| 112 | + _assert_eq(dxinv.qacc, dx.qacc, 'qacc') |
| 113 | + |
| 114 | + def test_tendon_force_clamp(self): |
| 115 | + m = test_util.load_test_file('actuator/tendon_force_clamp.xml') |
| 116 | + d = mujoco.MjData(m) |
| 117 | + mx = mjx.put_model(m) |
| 118 | + dx = mjx.put_data(m, d) |
| 119 | + |
| 120 | + dx = dx.replace(ctrl=jp.array([1.0, 1.0, 1.0, -1.0, 1.0, -20.0, 5.0, -5.0])) |
| 121 | + dx = mjx.forward(mx, dx) |
| 122 | + |
| 123 | + _assert_eq( |
| 124 | + dx.actuator_force, |
| 125 | + jp.array([1.0, 1.0, 1.0, -1.0, 1.0, -10.0, 5.0, -5.0]), |
| 126 | + 'actuator_force', |
| 127 | + ) |
| 128 | + |
| 129 | + |
| 130 | +if __name__ == '__main__': |
| 131 | + absltest.main() |
0 commit comments