Problems when verifing the rigid bodies dynamics of Mujoco #2590
-
IntroHi! I am a student at BIT, I use MuJoCo for my research on rigid bodies dynamics. My setupMujoco 3.3.1 My questionWhen I verified the rigid body dynamics of Mujoco as per the official documentation, I ran into a problem: I set up a cube with six degrees of freedom in a gravity-free environment, and at the same time, I applied a force of 0.05N to the x-axis and y-axis of the cube in the first 1 second of the experiment, after which the cube was no longer subjected to any external force. After that, I recorded the joint acceleration qacc, joint velocity qvel, and the inertia matrix I of the cube to attempt to verify the rigid body dynamics. I expect the formula to be validated as: The first term of the formula can be regarded as the inertial force However, there is always a gap in the results. Thank you very much for your answer! Minimal model and/or code that explain my questionIf you encountered the issue in a complex model, please simplify it as much as possible (while still reproducing the issue). Model: minimal XML<mujoco>
<option gravity="0 0 0" timestep="0.00001" />
<compiler angle="radian" autolimits="true" eulerseq="xyz" />
<worldbody>
<body name="test_cube" pos="0 0 0" quat="1 0 0 0">
<joint name="slide_x_joint" type="slide" axis="1 0 0" damping="0.001"/>
<joint name="slide_y_joint" type="slide" axis="0 1 0" damping="0.01"/>
<joint name="slide_z_joint" type="slide" axis="0 0 1" damping="0.001"/>
<joint name="tbase_rotx" axis="1 0 0" damping="0.0000" />
<joint name="tbase_roty" axis="0 1 0" damping="0.0000" />
<joint name="tbase_rotz" axis="0 0 1" damping="0.0000" />
<geom name="test_cube" type="box" size="0.1 0.1 0.1" />
<!-- <inertial pos="0 0 0" diaginertia="0.001667 0.001667 0.001667" mass="1"/> -->
<site name="body_site" />
</body>
</worldbody>
<sensor>
<framequat name="robot_base_quat" objtype="site" objname="body_site"/>
<!-- <framepos name="robot_base_pos" body="test_cube"/> -->
</sensor>
</mujoco>
Code: import mujoco
import mujoco.viewer
import numpy as np
def compute_inertial(I, qacc):
return I @ qacc
def compute_coriolis(I, v):
Iv = I @ v
omega=v[3:]
vl = v[:3]
return -np.concatenate([
np.cross(omega, Iv[:3]) + np.cross(vl, Iv[3:]),
np.cross(omega, Iv[3:])
])
# THIS PATH SHOULD BE YOUR PATH OF THE MODEL
model = mujoco.MjModel.from_xml_path("/home/zha/1workspace/mujoco/test/MujocoTutorials/new_task/test/dynamic_test/dynamic.xml")
data = mujoco.MjData(model)
viewer= mujoco.viewer.launch_passive(model=model, data=data)
mujoco.mj_resetData(model, data) # Reset state and time.
mujoco.mj_resetData(model, data)
mujoco.mjv_defaultFreeCamera(model, viewer.cam)
joint_id = mujoco.mj_name2id(model, mujoco.mjtObj.mjOBJ_JOINT, "tbase_rotx")
print_interval = 0.1
next_print_time = 0.0
nv = model.nv
dst = np.zeros((nv, nv), dtype=np.float64)
while viewer.is_running():
if data.time >= next_print_time:
# print the qacc and qvel and q
print(data.qpos[3:6],data.qvel[3:6],data.qacc[3:6])
# compute the inertial matrix
mujoco.mj_fullM(model, dst, data.qM)
print("Dense inertia matrix:")
for i in range(nv):
print(dst[i,:])
# compute the inertial force
qfrc_inertia = compute_inertial(dst, data.qacc)
# compute the coriolis + centripedal force
qfrc_coriolis = compute_coriolis(dst, data.qvel)
print("intertia",qfrc_inertia)
print("coriolis", qfrc_coriolis)
print("sum",qfrc_inertia + qfrc_coriolis)
next_print_time += print_interval
if(data.time < 1):
data.qfrc_applied[joint_id] = 0.001
data.qfrc_applied[joint_id+1] = 0.001
data.qfrc_applied[joint_id+2] = 0.00
else:
data.qfrc_applied[joint_id] = 0
data.qfrc_applied[joint_id+1] = 0
data.qfrc_applied[joint_id+2] = 0
mujoco.mj_step(model, data)
viewer.sync()
Confirmations
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
Hi, I am a MuJoCo user. I tried debugging your code, but it seems a bit tough to me. Here is a simpler code on the lines of your code that verifies the conservation of angular momentum. Torques are applied for the first five seconds, then it is set to zero. The rate of change of angular momentum goes to zero after 5s, as expected.
|
Beta Was this translation helpful? Give feedback.
-
Gist: Three When you define hinges one after another, you perform sequential body-fixed rotations, each in different frames, making them Euler angles. The rate of change of Euler angles is not the body's angular velocity. You can find the mapping Jacobian between the rate of change of Euler angles and the angular velocity, e.g., see: 4.8 in Spong, M. W., Hutchinson, S., & Vidyasagar, M. (2006). Robot Modeling and Control. Wiley. But, I guess that is not the point here since you want to verify the conservation of angular momentum. You need the angular velocity of the body. You can get that by defining a suitable sensor. Or, since our model is just a single body---> If you define a I am not sure where to point in the documentation for further reading, see issue #691 or Floating objects. |
Beta Was this translation helpful? Give feedback.
-
Hello, |
Beta Was this translation helpful? Give feedback.
Gist: Three
slide
joints followed by threehinge
joints will produce the same effect as afreejoint
. But their time rates are not the same.When you define hinges one after another, you perform sequential body-fixed rotations, each in different frames, making them Euler angles. The rate of change of Euler angles is not the body's angular velocity. You can find the mapping Jacobian between the rate of change of Euler angles and the angular velocity, e.g., see: 4.8 in Spong, M. W., Hutchinson, S., & Vidyasagar, M. (2006). Robot Modeling and Control. Wiley.
But, I guess that is not the point here since you want to verify the conservation of angular momentum. You need the angular velocity of …