Error:ValueError: Buffer has wrong number of dimensions (expected 1, got 2)

859 Views Asked by At

I wanted to call the function : emptymatrix=np.zeros((sim.data.nv,sim.data.nv))
mjp.cymj._mj_fullM(model, emptymatrix, sim.data.qM) in mujoco so that I can Convert sparse inertia matrix M into full matrix so that I can calclulate the torque but I have this error:raceback (most recent call last): File "kuka.py", line 58, in mjp.cymj._mj_fullM(sim.model,emptymatrix ,sim.data.qM) File ".local/lib/python3.8/site-packages/mujoco_py/generated/wrappers.pxi", line 5061, in mujoco_py.cymj._mj_fullM
ValueError: Buffer has wrong number of dimensions (expected 1, got 2)
If someone could help me I ll be so grateful.

1

There are 1 best solutions below

0
On

mujoco_py's implementation of mj_fullM expects your emptymatrix to be a vector of length nv*nv, rather than a square matrix.

See these lines from robosuite:

mass_matrix = np.ndarray(shape=(len(self.sim.data.qvel) ** 2,), dtype=np.float64, order="C")
mujoco_py.cymj._mj_fullM(self.sim.model, mass_matrix, self.sim.data.qM)
mass_matrix = np.reshape(mass_matrix, (len(self.sim.data.qvel), len(self.sim.data.qvel)))
self.mass_matrix = mass_matrix[self.qvel_index, :][:, self.qvel_index]

If you make use of the new MuJoCo python bindings (pip install mujoco), mujoco.mj_fullM takes the square matrix as you'd expect.