Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug Report] Quaternion order incorrect in Ant environment observation #971

Open
1 task done
ghost opened this issue Mar 19, 2024 · 6 comments
Open
1 task done
Labels
bug Something isn't working

Comments

@ghost
Copy link

ghost commented Mar 19, 2024

Describe the bug

In the documentation for the Ant environment, it is claimed that the quaternion representation of the torso orientation is returned in the (x, y, z, w) order. Upon inspection of a trajectory of the ant and comparing these values with the true behavior visually, it seems to me that the actual order is (w, x, y, z). These values seem to come directly from the qpos variable in Mujoco. I couldn't find any proper documentation of this variable, but the site https://mujoco.readthedocs.io/en/stable/overview.html states "For ball joints, the quaternion saved in mjModel.qpos0 is always (1,0,0,0) which corresponds to the null rotation", which corresponds to the (w, x, y, z) order.

This error might also persist in the documentations of other environments.

Code example

No response

System info

No response

Additional context

No response

Checklist

  • I have checked that there is no similar issue in the repo
@ghost ghost added the bug Something isn't working label Mar 19, 2024
@Kallinteris-Andreas
Copy link
Collaborator

Kallinteris-Andreas commented Mar 20, 2024

Hello, I have tested it, and it appears to be correct in the documentation.

qpos[0] Is the x coordinate
qpos[1] is the y coordinate

import gymnasium
import numpy as np
import time

RENDER_MODE = "human"

env = gymnasium.make('Ant-v5', render_mode=RENDER_MODE)


action = env.action_space.sample()

for ep in range(100):
    env.reset()
    for step in range(10000):
        #env.unwrapped.data.qpos[0] = (step % 2)  # x cord
        env.unwrapped.data.qpos[1] = (step % 2)  # y cord

        obs, rew, terminal, truncated, info = env.step(action)
        time.sleep(0.200)

@ghost
Copy link
Author

ghost commented Mar 20, 2024

Thanks for the reply. qpos[0:3] indeed describe the x-, y- and z-position, respectively. I was talking about the orientation of the torso, which is described by the quaternion qpos[3:7]. Here, it seems that the first element (qpos[3]) is the real part (w), followed by the x, y, and z orientations. The documentation however states that the order of these elements is (x, y, z, w).

@onnoeberhard
Copy link

Commenting because I switched account (I opened this issue).

@Kallinteris-Andreas
Copy link
Collaborator

Hey, @onnoeberhard sorry for taking a while to respond, I had to study how Quaternions work :-)

all material on quaternions I could find denote them as $a + bi + cj + dk$ (and therefore the values are a, b, c, d), it is not clear to me how these map to x, y, z, w rotation

@onnoeberhard
Copy link

This is just a different convention, the real part is $w$, so that the quaternion is $w + xi + yj + zk$. See also the following paragraph from the MuJoCo documentation: https://mujoco.readthedocs.io/en/2.2.1/programming.html#:~:text=To%20represent%203D,quaternions%20in%20MJCF.

@Kallinteris-Andreas
Copy link
Collaborator

Kallinteris-Andreas commented Apr 3, 2024

Here is the relevant documentation page in the latest version of MuJoCo Library (3.1.3) https://mujoco.readthedocs.io/en/3.1.3/programming/simulation.html#data-layout-and-buffer-allocation
you are correct, the order is (w, x, y, z)

The affected environment's documentation is
Ant, Humanoid HumanoidStandup
besides the "Observation Space" section, the "Starting State" section also needs to be updated

@onnoeberhard do you want to make a PR to fix this issue? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants