Mujoco 基本教程

Mujoco XML reference

XML Reference — MuJoCo documentation

参考文档:

MuJoCo自定义机器人建模指南 - 知乎

Mujoco——xml文档参考_LEGEND_KL的博客-优快云博客

MuJoCo的机器人建模 - 知乎

基本结构

<mujoco model='robot'>

    <include file ="  .xml">
    <!-- include 可以包含 mujocoinclude文件 -->
    <! 但是需要注意的是所有文件中compiler的 angle,coordinate等必须是相同的,因为编译时是将文件都整合在一起之后进行编译,所以compiler中的部分属性必须保持相同;其他属性会出现覆盖>
    <! mujocoinclude文件的好处在于 能够将不同模块分组,可以通过不同模块组成不同的主文件,比如生成多个含有机器人的不同环境,可以将不同的环境,机器人都形成不同模块,然后进行组合>


    <compiler angle='radian' meshdir='meshes'>
    <! compiler的作用在于 将 MJCF文件转换成 低水平的信息文件>

    <!-- load the stl files -->
    <assert>
        <mesh file='base.STL'/>
        <texture />
        <material />
    </assert>
    <! 用于导入 需要的文件>
    
    <! -- 设置默认参数,将不同类的文件的默认设置进行设置,减少下列设置的重复>
    <default>
    </default>

    <! -- 计算相关的参数设定 -->
    <option> 
    </option>

    <! -- 建模模型的主体部分-->
    <! 不同的mujocoinclude可以生成多个worldbody文件,在编译时将其融合在一起>
    <worldbody>

        <!设置环境的灯光>
        <!-- lighting -->
        <light directional='true' pos="-0.5 0.5 3" dir="0 0 -1"/>

        <! geom设置>
        <!-- floor -->
        <geom name='floor' pos='0 0 0' size='1 1 1' type='plane' rgba='1 0.83 0.61 1' />
        <! body设置>
        <body>
            <!-- it could contain many sub-bodies -->
        </body>

        <site>
        </site>

    </worldbody>


    <! -- 设置驱动-->
    <actuator>
    </actuator>

    <! -- 设置传感器-->
    <sensor>
    </sensor>

    <! -- 设置接触副 Mujoco1.5中无法识别contact--, mujodo2.0以上可以>
    <contact>
    </contact>

    <keyframe>
    </keyframe>


</mujoco>

1. mujoco    the top-level element

<mujoco model=''>
</mujoco>

2. include

"supplement1.xml文件"

<mujocoinclude>


</mujocoinclude>


"主文件"

<include file = 'supplement1.xml'>

3.. compiler

<compiler>
    
    <! -- 将设置worldbody之外的其他body的最小质量,防止有些body没有质量 -->
    <boundmass: "0" /> 

    <! -- 将设置worldbody之外的其他body的最小对角线惯性,防止有些body没有惯性 -->
    <boundinertia: "0" />

    <! -- 将设置所有body的总质量,会将模型中的质量进行scale -->
    <settotalmass: "-1"/>

    <! -- 防止inertia被错误设置,与物理事实不符,会自动调整inertia(平均)-->
    <balanceinertia: "false"  ["false","true"]/>

    <! -- 将去除xml文件中的文件具体路径,这样就可以在其他电脑上便于使用 -->
    <strippath: "false"  ['false',"true"]/>

    <! -- 将确定子部件和父部件之间的转换是通过local坐标还是global坐标,一般使用 local -->
    <coordinate: "local" ['local','global'] />

    <! -- 只决定XML文件中采用弧度值,还是角度值; 在mujoco仿真中都是弧度值-->
    <angle: "degree" ["degree","radian"] />

    <! -- 将mesh替换成 沿轴方向的包裹的box, 否则转换成 等惯性的box-->
    <fitaabb: "false">

    <! -- 说明欧拉角的顺序 -->
    <eulerseq: "xyz" />

    <! -- 说明stl文件夹的路径, 后续文件导入可以减少路径书写 -->
    <meshdir: " " />

    <! -- 说明材质文件夹的路径,后续文件导入可以减少路径书写 -->
    <texturedir: "" />

    <! -- 舍弃掉仅可见的几何体( contype和conaffinity被设置为0 ) -->
    <discardvisual: "false" ["false","true"]>

    <! -- 将静态的几何体与父部件融合 -->
    <fusestatic: "false" ["false","true"]/>

    <! -- 将通过质量和几何体的形状自动计算inertia, 我一般设置为true,对于均质物体 -->
    <! -- auto会将intertia缺失的部件进行计算inertia -->
    <inertiafromgeom: "auto" [false, true, auto]>

    <! -- 将在此区间内的group进行inertia计算,范围外的会忽略(仅可见的几何体) -->
    <inertiagrouprange: "0 5">

    <! -- 设置驱动器的长度范围等等 -->
    <lengthrange: />

</compiler>

4. option

设置了求解器相关的参数

<option>  
    
    <! -- 每一步的时间 -->
    <timestep = "0.0025"/>

    <! -- 求解器迭代的次数-->
    <iteration = '50'/>

    <! -- 可容忍的误差 -->
    <tolerance = "1e-10"/> 

    <! -- 求解器类型 -->
    <solver = "Newton"/>

    <! -- 积分器类型 -->
    <integrator = "Euler" ["Euler","RK4"] />

    <! -- 雅可比矩阵 求解 -->
    <jacobian = "dense"/> 

    <! -- 接触摩擦   elliptic更准确  -->
    <cone = "elliptic"/>

    <gravity = "0 0 -9.81"/>
    <viscosity />
    <magnetic />

    <! -- 设置碰撞的类型: 预先定义的 predefined 还是实际生成的 dynamics -->
    <collision = "all" [all, predefined, dynamic]/>

    <!-- 设置所有的可接触的geom的margin,如果该项为0,则使用文件中具体的定义;如果不为0,则覆盖所有的margin>
    <o_margin:"0" />

    <! "elliptic比较准确",但是计算速度较慢>
    <cone = "pyramidal" ["pyramidal","elliptic"]/>
   
</option>

5. size

<size>

    <! 仿真过程中能执行的最大的限制数量>
    <njmax:"-1" />
    
    <! 接触力的数量 >
    <nconmax:"-1">

    <分配的stack数量>
    <nstack:"-1">


</size>

6. visual

可以设置 几何体的可见性,相机的参数,灯光的参数,头灯等等

<visual>

    <! 相机的fovy>
    <fovy:"45" />

    <! 相机瞳孔间距>
    <ipd:"0.068" />

    <! off-screen rendering>
    <offwidth:"640"/>

    <offheight:"480" />

</visual>

asset

添加外部文件,比如stl文件,同时 设置材料和外观 (texture, materials)

<asset>
    <! --文件路径在complier的meshdir中添加,这里只设置名称 添加文件后在后续可以直接使用 -->
    <mesh file="base.stl"/>

    <! -- 设置材质 -->
    <texture name="fourcolor" type="cube" file="four_color.png" gridsize="1 1" width="330" height="330"/>

    <! -- 和材质联合使用 ,不涉及摩擦力,只涉及外观 -->
    <material name="color_plane" texture="fourcolor" texuniform="true"/>
</asset>

5. default   设置 body中 geom, joint, sensor 等等的 默认参数

default存在继承属性,套在类中的类属于子类,包含父类的所有属性,如果属性重合,那就覆盖该属性。

default 的规则:

1. 顶层的default默认为class,针对所有未标签(同时之前的祖先也都未标签)的body

2. 如果当前body有标签,那么该body使用当前类,同时该body的子类也使用该类,直到出现了新的类标签。(也就是 body使用自己给定的类标签,或者距离最近的有类标签的祖先的类)

另外类中的值只用于初始化,仿真开始就不再重新赋值。

<default class='main'>

    <! -- for main class   default-->
    <geom rgba='1 0 0 1'>

    <! -- joint -->
    <joint>
        <! -- 关节默认设置为有角度限制 -->
        <limited = "true">

        <! -- 设置关节的角度范围 complier中angule设置为degree,则使用角度制-->
        <range = "-120 120">
        
        <| -- 设置关节的阻尼 与速度有关-->
        <damping = "0.1">
        

    </joint>

    <! -- for sub class -->
    <default class='sub'>
        <geom rgba'0 1 0 1'>
    </default>

</default>



IN body

<body childclass='sub'>
     <!-- for sub class -->
     <geom type="sphere" rgba="0 0 1 0"/>

     <!-- for main class-->
     <geom type="cylinder" class="main"/>
</body>

6. worldbody   "world"  不可设置质量惯性等,是body的坐标与背景,代表整个空间

body, geom, site的name可以不用指定,如果不需要引用的话。 如果在后续需要使用,必须要设置name。

<! -- worldbody -->

<worldbody>

    <light directional='true' pos='-0.5 0.5 3' dir='0 0 -1'/>
    <camera />
    <site />

    <geom />

    <body /> # 可以设置多个相同层次的body
        <joint /> # 一个body内可以设置多个joint,甚至可以在同约束方向设置两个joint,两个是独立的

    <composite /> 
</worldbody>




<! -- body -->

<body 
name='base',optional  

pos='0 0 0',optional  (对应的是local/global coordinate要根据complier中coordinate的设置,默认是 local)

quat:real(4)四元数 / axisangle:real(4) (x,y,z,angle) / euler:real(3) / xyaxes / zaxis

childclass='sub/main',optional

mocap:'false/true','false'(只能针对在worldbody下,并且无joint的body,每次step 都可通过mjData.mocap_pos and mjData.mocap_quat, 设置位置和角度)

>

<inertial 
pos:'0 0 0',optional  
quat, axisangle, xyaxes, zaxis, euler
mass
diaginertia: real(3), optional (inertia 二选一,或者complier中根据形状确定惯性)
fullinertia: real(6), optional
/>

<joint
name:string,optional

class:string,optional

type:[free,ball,slide,hinge],'hinge'  
(free: six freedom ; ball:three rotational freedom ; slide ; hinge) 

group:int,'0', optional

pos:real(3),'0 0 0', optional

axis: real(3),'0 0 1' for hinge and slide

springdamper: real(2)  time constant,damping ratio

stiffness: real, “0”

limited: [false, true], “false” (joint is limited)

range: real(2), “0 0”
/>


 #同一个Body可以生成多个geom
<geom
name:string,optional

class:string.optional

type:[plane, hfield, sphere, capsule, ellipsoid, cylinder, box, mesh]

# size,fromto的设置取决于不同的type
size
fromto

material:string, optional

# 可以用于将geom进行分组,用于render时 屏蔽或者开启部分的显示 ;或者用于 自定义的计算。 
group: “0”

# 最后一个数值表示 透明度, 数值越小越透明
rgba: real(4), “0.5 0.5 0.5 1”

mass: real, optional

density: real, “1000”

pos: real(3), “0 0 0”

quat, axisangle, xyaxes, zaxis, euler

hfield: string, optional   # for hfield type

mesh: string, optional


 # contact related

# 对于需要相互碰撞的物体, 两个物体的 contype 和 conaffinity 都要相同的值。 
contype: "1"
conaffinity: "1"

# condim 说明了接触的类型:  1:无摩擦接触  3: 只有切向的摩擦力  4: 还有 rotational frcition around the contact normal   6: 3个轴方向的rotational friction都有 

condim: "3"

# 第一个值: 切向的滑动摩擦系数,  第二个: torsional friction parameter, 第三个:  rolling friction
friction: real(3), “1 0.005 0.0001”

# priority 的数值决定了 在有接触力时 以哪个物体的参数为准。选择priority数值大的那个。
priority:"0"

表示 两个geom的距离低于该数值,则被认为是 contact
margin: "0"

/>


</body>

四元数,欧拉角:

干货整理:欧拉角、旋转矩阵、四元数合辑 - 知乎

contact

equality

tendon

actuator

<!-- actuator -->
<actuator>

    <motor
    name=
    joint= /  hinge= / slide-  / ball=  string

    ctrllimited=[false,true],'false'
    forcelimited=[false,true],'false'
    ctrlrange=real(2),'0 0'
    forcerange=real(2),'0 0'

    lengthrange=real(2),'0 0'

    pos:
     />

    <position />

    <velocity />

    <cylinder />

    <muscle />

</actuator>

 对于一个joint只有一个actuator可以起作用。如果定义了多个actuator,那么会出现覆盖。

<!-- sensor -->

<sensor>
    <touch 
    name

    noise
    cutoff

    site
    />

    <accelerometer
    name:

    noise:

    site:    
    />

    <velocimeter
    name:

    noise:

    site:
    />

    <force
    name:
    noise:    
    site:
    />


    <jointpos (jointvel)
    name:
    noise:
    joint:

    />





</sensor>

Mujoco env construction

import mujoco_py

# load the model
model = mujoco_py.load_model_from_xml()
#from xml不太会用,但是path这个可以直接根据路径导进来
model = mujoco_py.load_model_from_path()

# create the simulation
sim = mujoco_py.MjSim(model,data=None,nsubsteps=1,udd_callback=None)


# Method

### get the state 
sim.get_state() 

### render the view
sim.render(width,height,camera_name(if None, the free),depth(if True, return the depth buffer))

### reset 
sim.reset()

### save
sim.save(file,format)

### set_state()
sim.set_state(desired_value)


# PyMjData
class mujoco_py.PyMjData

# render
## 对于 mode=human ,需要开启GUI,则是:
viewer = mujoco_py.MjViewer(sim)   # 会生成 viewer 
viewer.render()        # 会生成 GUI图片 


##  对于 mode=rgb_array , 不需要开启GUI,则
viewer = mujoco_py.MjRenderContextOffscreen(sim, device_id=-1) # 
data = viewer.render(width,height)        # 会生成 image matrix
data = data[::-1,:,:] # 对于上下颠倒的图片,则需要该操作 ::-1 表示 index倒序



PjMjData:

API reference — mujoco-py 1.50.1.0 documentation

Compuation:

Computation — MuJoCo documentation

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值