【XML模型】Mujoco代码解读-2(humanoid.xml)
文章基于Mujoco210中model下的humanoid.xml文件作为例程
目录
例程代码
该代码在/mujoco/model/humanoid下
<mujoco model="Humanoid">
<compiler inertiafromgeom="true" angle="degree"/>
<default>
<joint limited="true" damping="1" armature="0"/>
<geom condim="1" material="matgeom"/>
<motor ctrlrange="-.4 .4" ctrllimited="true"/>
</default>
<option timestep="0.005" iterations="50" tolerance="1e-10" solver="Newton" jacobian="dense" cone="pyramidal"/>
<size nconmax="50" njmax="200" nstack="10000"/>
<visual>
<map force="0.1" zfar="30"/>
<rgba haze="0.15 0.25 0.35 1"/>
<quality shadowsize="2048"/>
<global offwidth="800" offheight="800"/>
</visual>
<asset>
<texture type="skybox" builtin="gradient" rgb1="0.3 0.5 0.7" rgb2="0 0 0" width="512" height="512"/>
<texture name="texplane" type="2d" builtin="checker" rgb1=".2 .3 .4" rgb2=".1 0.15 0.2" width="512" height="512" mark="cross" markrgb=".8 .8 .8"/>
<texture name="texgeom" type="cube" builtin="flat" mark="cross" width="127" height="1278"
rgb1="0.8 0.6 0.4" rgb2="0.8 0.6 0.4" markrgb="1 1 1" random="0.01"/>
<material name="matplane" reflectance="0.3" texture="texplane" texrepeat="1 1" texuniform="true"/>
<material name="matgeom" texture="texgeom" texuniform="true" rgba="0.8 0.6 .4 1"/>
</asset>
<worldbody>
<geom name="floor" pos="0 0 0" size="0 0 .25" type="plane" material="matplane" condim="3"/>
<light directional="false" diffuse=".2 .2 .2" specular="0 0 0" pos="0 0 5" dir="0 0 -1" castshadow="false"/>
<light mode="targetbodycom" target="torso" directional="false" diffuse=".8 .8 .8" specular="0.3 0.3 0.3" pos="0 0 4.0" dir="0 0 -1"/>
<body name="torso" pos="0 0 1.4">
<freejoint name="root"/>
<geom name="torso1" type="capsule" fromto="0 -.07 0 0 .07 0" size="0.07"/>
<geom name="head" type="sphere" pos="0 0 .19" size=".09"/>
<geom name="uwaist" type="capsule" fromto="-.01 -.06 -.12 -.01 .06 -.12" size="0.06"/>
<body name="lwaist" pos="-.01 0 -0.260" quat="1.000 0 -0.002 0" >
<geom name="lwaist" type="capsule" fromto="0 -.06 0 0 .06 0" size="0.06" />
<joint name="abdomen_z" type="hinge" pos="0 0 0.065" axis="0 0 1" range="-45 45" damping="5" stiffness="20" armature="0.02" />
<joint name="abdomen_y" type="hinge" pos="0 0 0.065" axis="0 1 0" range="-75 30" damping="5" stiffness="10" armature="0.02" />
<body name="pelvis" pos="0 0 -0.165" quat="1.000 0 -0.002 0" >
<joint name="abdomen_x" type="hinge" pos="0 0 0.1" axis="1 0 0" range="-35 35" damping="5" stiffness="10" armature="0.02" />
<geom name="butt" type="capsule" fromto="-.02 -.07 0 -.02 .07 0" size="0.09" />
<body name="right_thigh" pos="0 -0.1 -0.04" >
<joint name="right_hip_x" type="hinge" pos="0 0 0" axis="1 0 0" range="-25 5" damping="5" stiffness="10" armature="0.01" />
<joint name="right_hip_z" type="hinge" pos="0 0 0" axis="0 0 1" range="-60 35" damping="5" stiffness="10" armature="0.01" />
<joint name="right_hip_y" type="hinge" pos="0 0 0" axis="0 1 0" range="-120 20" damping="5" stiffness="20" armature="0.01" />
<geom name="right_thigh1" type="capsule" fromto="0 0 0 0 0.01 -.34" size="0.06" />
<body name="right_shin" pos="0 0.01 -0.403" >
<joint name="right_knee" type="hinge" pos="0 0 .02" axis="0 -1 0" range="-160 -2" stiffness="1" armature="0.0060" />
<geom name="right_shin1" type="capsule" fromto="0 0 0 0 0 -.3" size="0.049" />
<body name="right_foot" pos="0 0 -.39" >
<joint name="right_ankle_y" type="hinge" pos="0 0 0.08" axis="0 1 0" range="-50 50" stiffness="4" armature="0.0008" />
<joint name="right_ankle_x" type="hinge" pos="0 0 0.04" axis="1 0 0.5" range="-50 50" stiffness="1" armature="0.0006" />
<geom name="right_foot_cap1" type="capsule" fromto="-.07 -0.02 0 0.14 -0.04 0" size="0.027" />
<geom name="right_foot_cap2" type="capsule" fromto="-.07 0 0 0.14 0.02 0" size="0.027" />
</body>
</body>
</body>
<body name="left_thigh" pos="0 0.1 -0.04" >
<joint name="left_hip_x" type="hinge" pos="0 0 0" axis="-1 0 0" range="-25 5" damping="5" stiffness="10" armature="0.01" />
<joint name="left_hip_z" type="hinge" pos="0 0 0" axis="0 0 -1" range="-60 35" damping="5" stiffness="10" armature="0.01" />
<joint name="left_hip_y" type="hinge" pos="0 0 0" axis="0 1 0" range="-120 20" damping="5" stiffness="20" armature="0.01" />
<geom name="left_thigh1" type="capsule" fromto="0 0 0 0 -0.01 -.34" size="0.06" />
<body name="left_shin" pos="0 -0.01 -0.403" >
<joint name="left_knee" type="hinge" pos="0 0 .02" axis="0 -1 0" range="-160 -2" stiffness="1" armature="0.0060" />
<geom name="left_shin1" type="capsule" fromto="0 0 0 0 0 -.3" size="0.049" />
<body name="left_foot" pos="0 0 -.39" >
<joint name="left_ankle_y" type="hinge" pos="0 0 0.08" axis="0 1 0" range="-50 50" stiffness="4" armature="0.0008" />
<joint name="left_ankle_x" type="hinge" pos="0 0 0.04" axis="1 0 0.5" range="-50 50" stiffness="1" armature="0.0006" />
<geom name="left_foot_cap1" type="capsule" fromto="-.07 0.02 0 0.14 0.04 0" size="0.027" />
<geom name="left_foot_cap2" type="capsule" fromto="-.07 0 0 0.14 -0.02 0" size="0.027" />
</body>
</body>
</body>
</body>
</body>
<body name="right_upper_arm" pos="0 -0.17 0.06" >
<joint name="right_shoulder1" type="hinge" pos="0 0 0" axis="2 1 1" range="-85 60" stiffness="1" armature="0.0068" />
<joint name="right_shoulder2" type="hinge" pos="0 0 0" axis="0 -1 1" range="-85 60" stiffness="1" armature="0.0051" />
<geom name="right_uarm1" type="capsule" fromto="0 0 0 .16 -.16 -.16" size="0.04 0.16" />
<body name="right_lower_arm" pos=".18 -.18 -.18" >
<joint name="right_elbow" type="hinge" pos="0 0 0" axis="0 -1 1" range="-90 50" stiffness="0" armature="0.0028" />
<geom name="right_larm" type="capsule" fromto="0.01 0.01 0.01 .17 .17 .17" size="0.031" />
<geom name="right_hand" type="sphere" pos=".18 .18 .18" size="0.04"/>
</body>
</body>
<body name="left_upper_arm" pos="0 0.17 0.06" >
<joint name="left_shoulder1" type="hinge" pos="0 0 0" axis="2 -1 1" range="-60 85" stiffness="1" armature="0.0068" />
<joint name="left_shoulder2" type="hinge" pos="0 0 0" axis="0 1 1" range="-60 85" stiffness="1" armature="0.0051" />
<geom name="left_uarm1" type="capsule" fromto="0 0 0 .16 .16 -.16" size="0.04 0.16" />
<body name="left_lower_arm" pos=".18 .18 -.18" >
<joint name="left_elbow" type="hinge" pos="0 0 0" axis="0 -1 -1" range="-90 50" stiffness="0" armature="0.0028" />
<geom name="left_larm" type="capsule" fromto="0.01 -0.01 0.01 .17 -.17 .17" size="0.031" />
<geom name="left_hand" type="sphere" pos=".18 -.18 .18" size="0.04"/>
</body>
</body>
</body>
</worldbody>
<actuator>
<motor name="abdomen_y" gear="200" joint="abdomen_y" />
<motor name="abdomen_z" gear="200" joint="abdomen_z" />
<motor name="abdomen_x" gear="200" joint="abdomen_x" />
<motor name="right_hip_x" gear="200" joint="right_hip_x" />
<motor name="right_hip_z" gear="200" joint="right_hip_z" />
<motor name="right_hip_y" gear="600" joint="right_hip_y" />
<motor name="right_knee" gear="400" joint="right_knee" />
<motor name="right_ankle_x" gear="100" joint="right_ankle_x" />
<motor name="right_ankle_y" gear="100" joint="right_ankle_y" />
<motor name="left_hip_x" gear="200" joint="left_hip_x" />
<motor name="left_hip_z" gear="200" joint="left_hip_z" />
<motor name="left_hip_y" gear="600" joint="left_hip_y" />
<motor name="left_knee" gear="400" joint="left_knee" />
<motor name="left_ankle_x" gear="100" joint="left_ankle_x" />
<motor name="left_ankle_y" gear="100" joint="left_ankle_y" />
<motor name="right_shoulder1" gear="100" joint="right_shoulder1" />
<motor name="right_shoulder2" gear="100" joint="right_shoulder2" />
<motor name="right_elbow" gear="200" joint="right_elbow" />
<motor name="left_shoulder1" gear="100" joint="left_shoulder1" />
<motor name="left_shoulder2" gear="100" joint="left_shoulder2" />
<motor name="left_elbow" gear="200" joint="left_elbow" />
</actuator>
</mujoco>
代码解读
geom
<geom name="floor" pos="0 0 0" size="0 0 .25" type="plane" material="matplane" condim="3"/>
这段代码定义了仿真环境中的地面几何体(geom
)。
下面是元素及其属性的详细解释:
<geom>
标签
name="floor"
:给这个几何体命名为"floor",方便引用。pos="0 0 0"
:设置地面几何体的位置在世界坐标系的原点。size="0 0 .25"
:对于type="plane"
的几何体,size
属性通常定义平面的"半厚",这里设置为0.25。虽然平面类型的几何体被视为无限大,但这个值有时用于其他计算。type="plane"
:指定几何体的类型为平面,常用于表示仿真环境中的地面。material="matplane"
:指定几何体使用名为"matplane"的材料,这个材料需要在<asset>
部分定义。condim="3"
:设置接触维度为3,这是用于更复杂的接触模型,可以处理滑动、滚动和扭转摩擦。
light
<light directional="false" diffuse=".2 .2 .2" specular="0 0 0" pos="0 0 5" dir="0 0 -1" castshadow="false"/>
<light mode="targetbodycom" target="torso" directional="false" diffuse=".8 .8 .8" specular="0.3 0.3 0.3" pos="0 0 4.0" dir="0 0 -1"/>
这段代码定义了仿真环境中的两种不同类型的光源(light
)。
下面是元素及其属性的详细解释:
<light>
标签(第一个光源)
directional="false"
:指定这是一个非定向光源,即点光源或聚光灯,它从一个特定位置照射而不是像定向光那样从无限远处照射。diffuse=".2 .2 .2"
:设置光源的漫反射颜色,较暗的灰色,影响大部分物体表面的颜色。specular="0 0 0"
:设置光源的镜面反射颜色,这里为黑色,意味着光源不产生镜面高光。pos="0 0 5"
:光源的位置,位于原点正上方5单位。dir="0 0 -1"
:光源的照射方向,指向下方。castshadow="false"
:指定光源不产生阴影。
<light>
标签(第二个光源)
mode="targetbodycom"
:这个模式使得光源照射到指定target
的质心上。这通常用于聚焦特定物体。target="torso"
:光源的目标,需要在模型中有一个名为"torso"的体素。directional="false"
:同样指定为非定向光源。diffuse=".8 .8 .8"
:较亮的灰色漫反射光,对物体表面颜色有较大影响。specular="0.3 0.3 0.3"
:较淡的灰色镜面反射光,可以产生微弱的高光效果。pos="0 0 4.0"
:光源位置,位于原点正上方4单位。dir="0 0 -1"
:光源的照射方向,指向下方。
通过这些设置,可以在仿真环境中创建出有地面和具有不同光照效果的场景,增加场景的真实感和视觉效果。
body
<body name="torso" pos="0 0 1.4">
<freejoint name="root"/>
<geom name="torso1" type="capsule" fromto="0 -.07 0 0 .07 0" size="0.07"/>
<geom name="head" type="sphere" pos="0 0 .19" size=".09"/>
<geom name="uwaist" type="capsule" fromto="-.01 -.06 -.12 -.01 .06 -.12" size="0.06"/>
<body name="lwaist" pos="-.01 0 -0.260" quat="1.000 0 -0.002 0" >
<geom name="lwaist" type="capsule" fromto="0 -.06 0 0 .06 0" size="0.06" />
<joint name="abdomen_z" type="hinge" pos="0 0 0.065" axis="0 0 1" range="-45 45" damping="5" stiffness="20" armature="0.02" />
<joint name="abdomen_y" type="hinge" pos="0 0 0.065" axis="0 1 0" range="-75 30" damping="5" stiffness="10" armature="0.02" />
<body name="pelvis" pos="0 0 -0.165" quat="1.000 0 -0.002 0" >
<joint name="abdomen_x" type="hinge" pos="0 0 0.1" axis="1 0 0" range="-35 35" damping="5" stiffness="10" armature="0.02" />
<geom name="butt" type="capsule" fromto="-.02 -.07 0 -.02 .07 0" size="0.09" />
<body name="right_thigh" pos="0 -0.1 -0.04" >
<joint name="right_hip_x" type="hinge" pos="0 0 0" axis="1 0 0" range="-25 5" damping="5" stiffness="10" armature="0.01" />
<joint name="right_hip_z" type="hinge" pos="0 0 0" axis="0 0 1" range="-60 35" damping="5" stiffness="10" armature="0.01" />
<joint name="right_hip_y" type="hinge" pos="0 0 0" axis="0 1 0" range="-120 20" damping="5" stiffness="20" armature="0.01" />
<geom name="right_thigh1" type="capsule" fromto="0 0 0 0 0.01 -.34" size="0.06" />
<body name="right_shin" pos="0 0.01 -0.403" >
<joint name="right_knee" type="hinge" pos="0 0 .02" axis="0 -1 0" range="-160 -2" stiffness="1" armature="0.0060" />
<geom name="right_shin1" type="capsule" fromto="0 0 0 0 0 -.3" size="0.049" />
<body name="right_foot" pos="0 0 -.39" >
<joint name="right_ankle_y" type="hinge" pos="0 0 0.08" axis="0 1 0" range="-50 50" stiffness="4" armature="0.0008" />
<joint name="right_ankle_x" type="hinge" pos="0 0 0.04" axis="1 0 0.5" range="-50 50" stiffness="1" armature="0.0006" />
<geom name="right_foot_cap1" type="capsule" fromto="-.07 -0.02 0 0.14 -0.04 0" size="0.027" />
<geom name="right_foot_cap2" type="capsule" fromto="-.07 0 0 0.14 0.02 0" size="0.027" />
</body>
</body>
</body>
<body name="left_thigh" pos="0 0.1 -0.04" >
<joint name="left_hip_x" type="hinge" pos="0 0 0" axis="-1 0 0" range="-25 5" damping="5" stiffness="10" armature="0.01" />
<joint name="left_hip_z" type="hinge" pos="0 0 0" axis="0 0 -1" range="-60 35" damping="5" stiffness="10" armature="0.01" />
<joint name="left_hip_y" type="hinge" pos="0 0 0" axis="0 1 0" range="-120 20" damping="5" stiffness="20" armature="0.01" />
<geom name="left_thigh1" type="capsule" fromto="0 0 0 0 -0.01 -.34" size="0.06" />
<body name="left_shin" pos="0 -0.01 -0.403" >
<joint name="left_knee" type="hinge" pos="0 0 .02" axis="0 -1 0" range="-160 -2" stiffness="1" armature="0.0060" />
<geom name="left_shin1" type="capsule" fromto="0 0 0 0 0 -.3" size="0.049" />
<body name="left_foot" pos="0 0 -.39" >
<joint name="left_ankle_y" type="hinge" pos="0 0 0.08" axis="0 1 0" range="-50 50" stiffness="4" armature="0.0008" />
<joint name="left_ankle_x" type="hinge" pos="0 0 0.04" axis="1 0 0.5" range="-50 50" stiffness="1" armature="0.0006" />
<geom name="left_foot_cap1" type="capsule" fromto="-.07 0.02 0 0.14 0.04 0" size="0.027" />
<geom name="left_foot_cap2" type="capsule" fromto="-.07 0 0 0.14 -0.02 0" size="0.027" />
</body>
</body>
</body>
</body>
</body>
<body name="right_upper_arm" pos="0 -0.17 0.06" >
<joint name="right_shoulder1" type="hinge" pos="0 0 0" axis="2 1 1" range="-85 60" stiffness="1" armature="0.0068" />
<joint name="right_shoulder2" type="hinge" pos="0 0 0" axis="0 -1 1" range="-85 60" stiffness="1" armature="0.0051" />
<geom name="right_uarm1" type="capsule" fromto="0 0 0 .16 -.16 -.16" size="0.04 0.16" />
<body name="right_lower_arm" pos=".18 -.18 -.18" >
<joint name="right_elbow" type="hinge" pos="0 0 0" axis="0 -1 1" range="-90 50" stiffness="0" armature="0.0028" />
<geom name="right_larm" type="capsule" fromto="0.01 0.01 0.01 .17 .17 .17" size="0.031" />
<geom name="right_hand" type="sphere" pos=".18 .18 .18" size="0.04"/>
</body>
</body>
<body name="left_upper_arm" pos="0 0.17 0.06" >
<joint name="left_shoulder1" type="hinge" pos="0 0 0" axis="2 -1 1" range="-60 85" stiffness="1" armature="0.0068" />
<joint name="left_shoulder2" type="hinge" pos="0 0 0" axis="0 1 1" range="-60 85" stiffness="1" armature="0.0051" />
<geom name="left_uarm1" type="capsule" fromto="0 0 0 .16 .16 -.16" size="0.04 0.16" />
<body name="left_lower_arm" pos=".18 .18 -.18" >
<joint name="left_elbow" type="hinge" pos="0 0 0" axis="0 -1 -1" range="-90 50" stiffness="0" armature="0.0028" />
<geom name="left_larm" type="capsule" fromto="0.01 -0.01 0.01 .17 -.17 .17" size="0.031" />
<geom name="left_hand" type="sphere" pos=".18 -.18 .18" size="0.04"/>
</body>
</body>
</body>
这段代码定义了一个复杂的多体系统,它使用了嵌套的<body>
标签来构建一个具有多个关节和几何体的人形模型。以下是对代码主要部分的解读:
躯干(“torso”)
name="torso"
:定义了模型的主体部分,名称为"torso"。pos="0 0 1.4"
:设置了"torso"的位置,在世界坐标系中距离地面1.4单位高。<freejoint name="root"/>
:为"torso"提供了一个自由关节作为其与世界的连接。这种类型的关节允许在所有方向上的平移和旋转。
"torso"的子几何体
- 几何体:使用
<geom>
标签定义了多种形状,如胶囊(“capsule”)和球体(“sphere”),来模拟人体的不同部位(如躯干、头部和腰部)。
下级身体部分
- 腰部(“lwaist”)及以下:通过嵌套的
<body>
标签进一步构建了模型的下半身,包括腰部、骨盆、大腿、小腿和脚。每个部分都通过特定的关节与上一级身体部分相连,如铰链关节(“hinge”)用于模拟膝盖和腰部的运动。 - 关节属性:为每个关节指定了类型、位置、旋转轴和运动范围等属性。此外,还设置了阻尼、刚度和装甲(增加关节的质量)参数,这些参数影响关节的运动特性和相互作用。
上肢部分
- 上臂(“right_upper_arm"和"left_upper_arm”):定义了模型的上臂部分,包括肩关节的复杂运动范围和旋转轴设置。
- 下臂和手:通过进一步嵌套的
<body>
标签构建了模型的前臂和手,使用铰链关节模拟肘部和腕部的运动。
几何体和关节的详细配置
- 每个身体部分:通过特定的
<geom>
标签定义了形状、尺寸和位置,来准确模拟人体的相应部位。 - 关节设置:详细定义了每个关节的运动轴、范围和物理属性,如阻尼和刚度,以模拟现实中的人体运动特性。
整体而言,这段代码展示了如何在MuJoCo中构建一个详细的人形模型,通过精细地设置关节和几何体的参数,来模拟复杂的人体动态和运动行为。
actuator
<actuator>
<motor name="abdomen_y" gear="200" joint="abdomen_y" />
<motor name="abdomen_z" gear="200" joint="abdomen_z" />
<motor name="abdomen_x" gear="200" joint="abdomen_x" />
<motor name="right_hip_x" gear="200" joint="right_hip_x" />
<motor name="right_hip_z" gear="200" joint="right_hip_z" />
<motor name="right_hip_y" gear="600" joint="right_hip_y" />
<motor name="right_knee" gear="400" joint="right_knee" />
<motor name="right_ankle_x" gear="100" joint="right_ankle_x" />
<motor name="right_ankle_y" gear="100" joint="right_ankle_y" />
<motor name="left_hip_x" gear="200" joint="left_hip_x" />
<motor name="left_hip_z" gear="200" joint="left_hip_z" />
<motor name="left_hip_y" gear="600" joint="left_hip_y" />
<motor name="left_knee" gear="400" joint="left_knee" />
<motor name="left_ankle_x" gear="100" joint="left_ankle_x" />
<motor name="left_ankle_y" gear="100" joint="left_ankle_y" />
<motor name="right_shoulder1" gear="100" joint="right_shoulder1" />
<motor name="right_shoulder2" gear="100" joint="right_shoulder2" />
<motor name="right_elbow" gear="200" joint="right_elbow" />
<motor name="left_shoulder1" gear="100" joint="left_shoulder1" />
<motor name="left_shoulder2" gear="100" joint="left_shoulder2" />
<motor name="left_elbow" gear="200" joint="left_elbow" />
</actuator>
这段代码定义了MuJoCo模型中的<actuator>
部分,用于控制模型中各个关节的动作。在MuJoCo中,<motor>
标签用于创建一个驱动器,它可以施加力矩或力来驱动关节的运动。每个<motor>
元素针对一个特定的关节,并定义了如何控制该关节。以下是每个属性的解释:
name
:驱动器的名称,通常与它控制的关节或动作相关。gear
:增益系数,表示驱动器施加的力矩(或力)与控制信号之间的比例系数。较高的gear
值意味着驱动器能施加更大的力矩。joint
:指定驱动器作用的关节名称,该名称应与模型中定义的关节名称相匹配。
在这个具体的代码段中,定义了一系列驱动器来控制人形模型的各个主要关节,包括腹部(abdomen)、髋关节(hip)、膝盖(knee)、踝关节(ankle)以及肩关节(shoulder)和肘关节(elbow)。gear
值的不同反映了不同关节需要的驱动力矩不同,例如,髋关节的驱动器中right_hip_y
和left_hip_y
的gear
值为600,这可能是因为这些关节需要更大的力矩来支持模型的立立和行走动作。而踝关节的驱动器gear
值较小,表明这些关节所需的力矩较小。
通过这些设置,可以精细地控制模型的动作,模拟复杂的人体运动,如走路、跑步或其他活动。
— 转载需要注明出处,违者必究。