【Mujoco】Python Mujoco仿真初步及琐碎问题

搭建仿真环境的过程中,记录一些关键点、资源、以及各种遇到的问题合集

使用的配置是:Python & Anaconda & Mujoco & Jupyternotebook & WSL2

环境搭建

设置环境时,遇到一些问题,在另一篇博客有讲(https://blog.csdn.net/weixin_44286143/article/details/139249782?spm=1001.2014.3001.5501)

主要是关于Anaconda底层驱动的踩坑,处理完后,一切顺利,也祝各位一切顺利

最小可行程序

在导入模型和建立对应的数据后,其实实现仿真非常吉简单

mujoco.mj_resetData(model, data) 

with mujoco.viewer.launch_passive(model, data) as viewer:
    while viewer.is_running():

        data.ctrl = [10,0,0,0]
        
        # Step physics simulation 
        mujoco.mj_step(model, data)
        
        # Update viewer
        viewer.sync()

对控制量的输入关键在于 mjData.ctrl的接口。在官方文档上有几个重要资源:

  • https://mujoco.readthedocs.io/en/stable/computation/index.html#simulation-pipeline
    这里有simulation pipeline, 具体说明了动力学的仿真计算流程;也讲了关键的Physical State
  • https://mujoco.readthedocs.io/en/stable/programming/simulation.html#simulation-loop 这里的simulation_loop 和 state_control 是关键,很推荐细看

动力学

本人项目初步在drone上实现,于是遇到困难:找不到 qpos qvel 与模型的对应关系。

总体而言,对于mujoco,有:

  • 对于常规joint的模型,一个joint自动对应qpos内相应的configuration
  • 对于ball joint和freejoint的模型,则是qpos有七维度,后四位为四元数,前三位是平移坐标;qvel有六维度,后三位为欧拉角速度,前三位是平移速度

来自

  • https://mujoco.readthedocs.io/en/stable/programming/simulation.html
    • https://www.roboti.us/forum/index.php?threads/understanding-qpos-and-qvel-of-freejoint.4074/
  • https://en.wikipedia.org/wiki/Rotation_formalisms_in_three_dimensions#Quaternion_%E2%86%94_angular_velocities

另外,一个关键的坐标系问题:
https://zhuanlan.zhihu.com/p/689855259
即,欧拉角速度为local frame的速度,非global frame

输入控制

有两种种方法输入控制:直接计算后输入,或使用回调函数(或者等价地,使用step1,step2函数)。文档中推荐使用回调函数方法,因此也这么做。

然而,在python mujoco中,并没有C中的全局回调函数指针API,因此找来找去,在python文档的callback部分发现

For each callback mjcb_foo, users can set it to a Python callable via mujoco.set_mjcb_foo(some_callable). To reset it, call mujoco.set_mjcb_foo(None). To retrieve the currently installed callback, call mujoco.get_mjcb_foo(). (The getter should not be used if the callback is not installed via the Python bindings.) The bindings automatically acquire the GIL each time the callback is entered, and release it before reentering MuJoCo. This is likely to incur a severe performance impact as callbacks are triggered several times throughout MuJoCo’s computation pipeline and is unlikely to be suitable for “production” use case. However, it is expected that this feature will be useful for prototyping complex models.

这里还提到:由于GIL (Global Interpreter Lock),它不适合用于production case。所以可能直接使用data.ctrl是一个更好的选择?

为此,发了一个问题在论坛,希望之后有人回答
https://stackoverflow.com/questions/78545321/which-is-the-best-coding-style-direct-or-callback-to-apply-control-in-python-m

另外,关于xpos和qpos的区别:

  • https://github.com/google-deepmind/mujoco/issues/889
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值