google-deepmind / mujoco

Multi-Joint dynamics with Contact. A general purpose physics simulator.
https://mujoco.org
Apache License 2.0
7.47k stars 734 forks source link

Theoretically calculated contact forces for a dynamical problem disagree with Mujocos contact forces in a simple dynamical example? #1743

Closed kurtenkera closed 2 hours ago

kurtenkera commented 1 week ago

I'm trying to verify the accuracy of the contact forces calculated by MuJoCo in dynamical scenarios (i.e., not just an object at rest on a plane) using a simple example of a box falling (see video below) and Newton's laws.

https://github.com/google-deepmind/mujoco/assets/127472392/58253fd2-aebc-404a-b511-4fcc29d776da

Specifically, I have a $2 \times 2 \times 2$ metre box (volume is $8 m^3$) with a density $\rho = 65 kg/m^3$ whose initial position at $t = 0$ is $(x_0, y_0, z_0) = (0, 0, 4)$. Note that since the centre of the box is located at this initial position $(0, 0, 4)$, the box free-falls $3m$ under the acceleration of gravity ($g = -9.81 m/s^2$) before impacting the ground.

I calculated the velocity of the box prior to the collision as follows:

$$ \begin{align} {v_f}^2 &= v_i^2 + 2 a d \ &= 0 + 2 9.81 3 \nonumber \end{align} $$

which implies that $v_f = 7.67 m/s$ prior to contact with the ground. Now, assuming the box decelerates at a constant rate (this is an approximation) when it comes into contact with the ground and comes to a stop (i.e., $v_f = 0 m/s$), we can calculate the magnitude of the box's deceleration using the equation above. In this case, $v_i = 7.67 m/s$, $v_f = 0$, and $d = 0.05m$ is the distance over which the box decelerates (this is the minimum penetration distance between the box and the ground returned by MuJoCo). Substituting these numbers into the equation above and solving for $a$, we get:

$$ \begin{align} |a| &= |-(v_i)^2/2d| \nonumber \ &= 588.29 m/s^2 \nonumber \end{align} $$

Finally, the theoretical net force $F$ on the box is given as $F = ma = 65 8 588.29 = 305, 910.8N$. But MuJoCo tells me the forces on each corner upon impact is $F_{corner} = 107, 302.75N$, so the net force (sum of all four forces) on the box is $F = 429, 211 N$. This is about 100,000 Newtons greater than what I calculated using my theoretical approach. Which is correct?

The plot below shows the velocity (magnitude) of the box over the simulation.

image

It contacts the ground at $t = 0.782s$, and judging by the graph it takes about 0.2 seconds to come to a complete stop. This provides another way to calculate/sanity-check/verify the force on the box:

$$ \begin{align} F &= m (\Delta v / \Delta t) \nonumber \ &= 520 (7.67 / 0.2) \nonumber \ &= 19, 942N \nonumber \end{align} $$

But $19,942N$ is grossly less than what MuJoCo calculates, which was $F = 429, 211 N$. Are my calculations just too rough/approximate here? How can I verify the accuracy of the contact forces in MuJoCo moving forward? I have attached my XML file below in case people want to verify my experimental results:

<mujoco>

  <option gravity = "0 0 -9.81" />

  <worldbody>
    <light diffuse=".5 .5 .5" pos="0 0 3" dir="0 0 -1"/>

    <geom type="plane" name = "ground_floor" size="5 5 0.1" rgba="1 0 1 1"/>

    <body pos="0 0 4.0" name = "green_box" euler = "0 0 0">
      <joint type="free"/>
      <geom type="box" name = "green_box" pos = "0 0 0" size = "1 1 1" rgba = "0 1 0 1" density = "65"/>
    </body>

  </worldbody>  

</mujoco>
yuvaltassa commented 1 week ago

I haven't looked carefully yet, but could this be a duplicate of #1560 ? (please try with sphere)

kurtenkera commented 1 week ago

I just read #1560 and the solutions you provided in that post were for a equilibrium case of a box/sphere resting on a plane. I have no issues with these scenarios, as it is simple to theoretically calculate the contact forces of a box/sphere at rest on a plane, and confirm that they sum up to mass * gravity (I have already done this with various different geoms).

My issue is that when I attempt to theoretically calculate contact forces in a dynamical/non-equilibrium scenario (e.g., a falling box, or a running robot), the more complex method of using Newton's laws (which is absolutely necessary) to sanity check Mujoco's dynamical contact forces does not work?

I will try with a sphere as you've asked, but I think it is still important to resolve (or better understand) the issue with the box as I will be scaling up to complex robots, and need to reliably calculate contact forces on these robots and have methods to sanity check these contact forces if possible!

yuvaltassa commented 1 week ago

Okay, I read the OP a bit more carefully. I feel the biggest issue is in

assuming the box decelerates at a constant rate

Why would this be a reasonable approximation? For MuJoCo's (and any other reasonable) contact model this is bound to be very, very wrong. If you like, you can calculate the total impulse (integral of force*time) during the collision, but looking at the force at a specific point in time, or the maximum force or whatever is not something that you can recover without re-implementing the entire model. The force at a given time is a non-trivial function of the penetration depth and velocity.