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

C++ ways to get MjDataBodyViews #251

Closed
Trinkle23897 opened this issue Apr 23, 2022 · 4 comments
Closed

C++ ways to get MjDataBodyViews #251

Trinkle23897 opened this issue Apr 23, 2022 · 4 comments

Comments

@Trinkle23897
Copy link

Trinkle23897 commented Apr 23, 2022

Hi,

First, thanks for this great project!

I'm going to translate the existing python bindings in gym to C++, to get the best performance for vectorized environments envpool and minimize the overhead. openai/gym#2762 has made huge progress in integrating the newest version of mujoco. However, I cannot find a related document for MjDataBodyViews.

In short, https://github.com/openai/gym/blob/0e2077bb636fbac979a4edd0c8608235742a8f2a/gym/envs/mujoco/mujoco_env.py#L268-L272

    def get_body_com(self, body_name):
        if self._mujoco_bindings.__name__ == "mujoco_py":
            return self.data.get_body_xpos(body_name)
        else:
            return self.data.body(body_name).xpos  # <-- this line

self.data is a mjData structure. But I cannot find a related pybind binding function of mjData that has a body method. Later on I expand the indexers.[h|cc] to see the code after expansion, by

g++ -E indexers.cc -I ../../include/ $(python3 -m pybind11 --includes) > /tmp/indexer_expand.cc

indexer_expand.txt

However, after going through the expanded code, I'm still quite confused about how to write the same functionality (data.body(body_name).xpos) in C++. Could you please kindly provide some help? Thanks in advance!

Update:

For Ant Env (sail-sg/envpool#74):

  • data.xpos[1] is data.body("torso").xpos, mj_name2id(model_, mjOBJ_BODY, "torso")

For Reacher Env (sail-sg/envpool#81):

  • data.xpos[-2] is data.body("fingertip").xpos, mj_name2id(model_, mjOBJ_BODY, "fingertip")
  • data.xpos[-1] is data.body("target").xpos, mj_name2id(model_, mjOBJ_BODY, "target")

For Pusher Env (sail-sg/envpool#82):

  • data.xpos[-3] is data.body("tips_arm").xpos, mj_name2id(model_, mjOBJ_BODY, "tips_arm")
  • data.xpos[-2] is data.body("object").xpos, mj_name2id(model_, mjOBJ_BODY, "object")
  • data.xpos[-1] is data.body("goal").xpos, mj_name2id(model_, mjOBJ_BODY, "goal")
@saran-t
Copy link
Member

saran-t commented Apr 26, 2022

I'm not 100% clear on what you're asking here. Are you trying to replicate the Python API of the mujoco package in C++?

@Trinkle23897
Copy link
Author

Trinkle23897 commented Apr 26, 2022

I'm trying to replicate gym's way of using mujoco in C++. For example, this is Ant-v4 env written in pure C++: https://github.com/Trinkle23897/mjc-mwe/blob/master/cc/ant.cc

@saran-t
Copy link
Member

saran-t commented Apr 26, 2022

In that case my current recommendation would be to code against MuJoCo's C API directly. You can use mj_name2id to look up the numerical index that corresponds to any given entity name.

If you want nice syntactic sugar for named access (e.g. data.body("torso").xpos) in C++ you'll need to roll your own class that implements this interface. The code in python/indexers.cc shows one example on how to go about it.

@Trinkle23897
Copy link
Author

Thanks for the answer!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants