google-deepmind / mujoco

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

MJX external force Bug #2181

Closed lucattyy closed 3 weeks ago

lucattyy commented 3 weeks ago

Intro

Hi!

I am a student and I am using MJX for my research on Robotics.

My setup

I am using mujoco-mjx version is 3.1.3 on Ubuntu22.04

What's happening? What did you expect?

I need to apply external force to robot and I discover that parametre 'xfrc_applied' can be checked in mjx, but something wrong happened when I try to modify them.

Traceback (most recent call last): File "/home/pyproject/mjx/scripts/env.py", line 66, in state = jit_step(state, ctrl) File "/home/pyproject/mjx/scripts/../env/env.py", line 255, in step state.pipeline_state.xfrc_applied = state.pipeline_state.xfrc_applied.at[1, 2].set(75) File "", line 4, in setattr dataclasses.FrozenInstanceError: cannot assign to field 'xfrc_applied'

It seems that 'xfrc_applied ' has been frozen. Is there any method can help apply external force to robot in MJX?

Confirmations

kevinzakka commented 3 weeks ago

You’re trying to mutate a frozen field. You need to use the .at(idx).set(val) semantics.

lucattyy commented 3 weeks ago

That's exactly what I did. The .at(idx).set(val) semantics can be used to modify value of MjData.ctrl, but still cannot solve the problem. state.pipeline_state.xfrc_applied = state.pipeline_state.xfrc_applied.at[1, 2].set(75) dataclasses.FrozenInstanceError: cannot assign to field 'xfrc_applied'

Do you mean that MJX do not support external force applied through xfrc_applied currently?

kevinzakka commented 3 weeks ago

Oh forgot to add that you also need to use the replace method of the dataclass:


pipeline_state = pipeline_state.replace(xfrc_applied=xfrc_applied.at[1, 2].set(75))
lucattyy commented 3 weeks ago

Thx! It really help a lot!