Issue with Force/Contact sensing #1962
Replies: 4 comments
-
Your issue is that the site is too small. Usually the size/shape of a site is meaningless, but not for touch sensors. They only reports contacts that happen inside. Is the documentation unclear? https://mujoco.readthedocs.io/en/latest/XMLreference.html#sensor-touch |
Beta Was this translation helpful? Give feedback.
-
Apologies, though I have commented that I have been using a touch sensor, I have used a force sensor to measure touch(in the XML), as I did note this behaviour from the docs. If I am not wrong, this behaviour should not extend to force sensing. |
Beta Was this translation helpful? Give feedback.
-
@IntrovertInDisguise Force sensor outputs the interaction force between a child and parent body. As explained in the docs https://mujoco.readthedocs.io/en/stable/XMLreference.html#sensor-force You can create a dummy welded (no joints) body as a child to your body with very small mass. The interaction force between the two bodies would be equivalent to the force you are trying to measure on the "parent" body. The discussion here might help #399 |
Beta Was this translation helpful? Give feedback.
-
@tawjaw I tried this idea, but there was no apparent change in output. Does this implementation look fine? `import mujoco MJCF model as a stringmjcf_model = """
Write the MJCF model to a temporary filewith tempfile.NamedTemporaryFile(suffix=".xml", delete=False) as tmp_file: Load the model and create a simulationmodel = mujoco.MjModel.from_xml_path(tmp_filepath) Create a rendererrenderer = mujoco.Renderer(model) Function to compute the weight of the cuboiddef compute_cuboid_weight(): Initialize arrays to store contact forces and weightscontact_forces = [] Initialize list to store framesframes = [] options = mujoco.MjvOption() Simulation parameterssim_duration = 10 # duration in seconds Step the simulation to let the cuboid settle on the groundfor _ in range(int(sim_duration / model.opt.timestep)):
Convert to numpy arrays for plottingcontact_forces = np.array(contact_forces) Plot the contact force and computed weighttime = np.arange(len(contact_forces)) * model.opt.timestep Check if the measured contact force matches the computed weighttolerance = 1e-2 # Tolerance for comparison Show the video using mediapymedia.show_video(frames, fps=framerate) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I am a student working on a few simulations involving contact force measurements with the ground plane. The net contact force experienced does not match the weight of the object in a lot of simulations, even in static cases. How do I fix this?
I have coded up a minimal cuboid example seen below :
`import mujoco
import numpy as np
import matplotlib.pyplot as plt
import tempfile
import mediapy as media
MJCF model as a string
mjcf_model = """
Function to run MuJoCo simulation and capture contact forces
def run_simulation_and_capture_contact(model, sim_duration=10, framerate=30):
data = mujoco.MjData(model)
renderer = mujoco.Renderer(model)
Write the MJCF model to a temporary file
with tempfile.NamedTemporaryFile(suffix=".xml", delete=False) as tmp_file:
tmp_file.write(mjcf_model.encode())
tmp_filepath = tmp_file.name
Load the model
model = mujoco.MjModel.from_xml_path(tmp_filepath)
Run simulation and capture contact forces
contact_forces, frames = run_simulation_and_capture_contact(model)
weights = np.array(weights)
Plot the contact force over time
time = np.arange(len(contact_forces)) * model.opt.timestep
plt.figure()
plt.plot(time, contact_forces, label='Measured Contact Force (N)')
plt.plot(time, weights[:, 2], label='Computed Weight (N)', linestyle='dashed')
plt.xlabel('Time (s)')
plt.ylabel('Force (N)')
plt.legend()
plt.title('Contact Force Over Time')
plt.show()
Show the video using mediapy
media.show_video(frames, fps=30)

`
Here are my outputs:
cuboidex.mp4
Am I reading the force sensors right?
Beta Was this translation helpful? Give feedback.
All reactions