Mujoco xml模型
- 一個例子
- compiler
- option
- asset
- mesh
- default
- 基本使用
- childclass與class
- 多個class
- worldbody
- body關系
- inertial
- joint
- geom
XML主要分為以下三個部分:
- < asset> : 用 tag導入STL文件;
- < worldbody>:用tag定義了所有的模擬器組件,包括燈光、地板以及你的機器人;
- < acutator>:定義可以執行運動的關節。定義的順序需要按照運動學順序來,比如多關節串聯機器人以工具坐標附近的最后一個關節為joint0,依此類推。
一個例子
mujoco官方文檔給了xml文件能寫的內容
XML Reference
<mujoco model="example"><!-- set some defaults for units and lighting --><compiler angle="radian" meshdir="meshes"/><!-- 導入STL文件 --><asset><mesh file="base.STL" /><mesh file="link1.STL" /><mesh file="link2.STL" /></asset><!-- 定義所有模擬器組件 --><worldbody><!-- 燈光 --><light directional="true" pos="-0.5 0.5 3" dir="0 0 -1" /><!-- 添加地板,這樣我們就不會凝視深淵 --><geom name="floor" pos="0 0 0" size="1 1 1" type="plane" rgba="1 0.83 0.61 0.5"/><!-- the ABR Control Mujoco interface expects a hand mocap --><body name="hand" pos="0 0 0" mocap="true"><geom type="box" size=".01 .02 .03" rgba="0 .9 0 .5" contype="2"/></body><!-- 構建串聯機器人 --><body name="base" pos="0 0 0"><geom name="link0" type="mesh" mesh="base" pos="0 0 0"/><inertial pos="0 0 0" mass="0" diaginertia="0 0 0"/><!-- nest each child piece inside the parent body tags --><body name="link1" pos="0 0 1"><!-- this joint connects link1 to the base --><joint name="joint0" axis="0 0 1" pos="0 0 0"/><geom name="link1" type="mesh" mesh="link1" pos="0 0 0" euler="0 3.14 0"/><inertial pos="0 0 0" mass="0.75" diaginertia="1 1 1"/><body name="link2" pos="0 0 1"><!-- this joint connects link2 to link1 --><joint name="joint1" axis="0 0 1" pos="0 0 0"/><geom name="link2" type="mesh" mesh="link2" pos="0 0 0" euler="0 3.14 0"/><inertial pos="0 0 0" mass="0.75" diaginertia="1 1 1"/><!-- the ABR Control Mujoco interface uses the EE body to --><!-- identify the end-effector point to control with OSC--><body name="EE" pos="0 0.2 0.2"><inertial pos="0 0 0" mass="0" diaginertia="0 0 0" /></body></body></body></body></worldbody><!-- 定義關節上的執行器 --><actuator><motor name="joint0_motor" joint="joint0"/><motor name="joint1_motor" joint="joint1"/></actuator></mujoco>
compiler
option
asset
mesh
<mesh name="base_link" file="base_link.obj"/><!--使用上-->
<geom type="mesh" mesh="base_link"/> <!-- 使用該網格 -->
default
基本使用
< default> 定義的屬性模板通過 ??class 屬性?? 在后續的 < body>, < joint>, < geom> 等元素中引用。
基本引用規則??:
- ??class="模板名"??:在元素中添加該屬性即可繼承模板中定義的所有屬性。
- 繼承邏輯??:子類會繼承父類的所有屬性,同名屬性會被子類覆蓋,顯式寫了屬性就會覆蓋類的屬性。
<default><!-- 根默認類 --><default class="robot"> <!-- 主模板 --><geom friction="0.6" margin="0.001"/> <!-- 所有幾何體默認摩擦系數 --><joint damping="2"/> <!-- 所有關節默認阻尼 --><motor ctrlrange="-23.7 23.7"/> <!-- 所有電機默認控制范圍 --><!-- 子類:髖關節模板 --><default class="abduction"> <!-- 繼承自 robot --><joint axis="1 0 0" range="-1.0472 1.0472"/> <!-- 覆蓋 axis 和 range --></default><!-- 子類:膝關節模板 --><default class="knee"> <!-- 繼承自 robot --><joint range="-2.7227 -0.83776"/> <!-- 覆蓋 range --><motor ctrlrange="-45.43 45.43"/> <!-- 覆蓋電機的控制范圍 --></default></default>
</default>
根類使用
<body name="base" childclass="robot"> <!-- 所有子元素默認繼承robot類 --><geom type="box" size="0.1 0.1 0.1"/> <!-- 自動應用 friction="0.6" --><joint name="free_joint"/> <!-- 自動應用 damping="2" -->
</body>
子類abduction使用
<body name="FL_hip"><joint name="FL_hip_joint" class="abduction"/> <!-- 繼承以下屬性:axis="1 0 0" range="-1.0472 1.0472" damping="2" (從父類robot繼承) -->
</body>
所以也就是說使用了 < joint class=“abduction”>的,有damping=“2"屬性,也有axis=“1 0 0” range=”-1.0472 1.0472"屬性。
顯式覆蓋
<default><default class="aa"><geom rgba="1 0 0 1" friction="0.8"/> <!-- 紅色+高摩擦 --><joint damping="3"/> <!-- 高阻尼 --></default>
</default><body name="parent" childclass="aa"><!-- 子剛體自動繼承類 aa --><body name="child1"><geom type="box"/> <!-- 自動應用 rgba="1 0 0 1" 和 friction="0.8" --><joint type="hinge"/> <!-- 自動應用 damping="3" --></body><!-- 子剛體顯式覆蓋屬性 --><body name="child2"><geom type="sphere" rgba="0 1 0 1"/> <!-- 覆蓋為綠色 --><joint type="slide" damping="1"/> <!-- 覆蓋阻尼 --></body>
</body>
childclass與class
childclass 的作用范圍?僅作用于 < body> 標簽內??直接嵌套??的子 < body>,當前剛體不使用這個屬性,子剛體的子剛體(孫子級)不會自動繼承該 childclass,除非它們也被顯式設置。
<body name="parent" childclass="aa"><geom/> <!-- 不會應用 aa 的 geom 默認屬性 --><!-- 子剛體(直接繼承 aa) --><body name="child1"> <geom/> <!-- 自動應用 aa 的 geom 默認屬性 --><!-- 孫子剛體(不繼承 aa,除非 child1 也設置 childclass) --><body name="grandchild"><geom/> <!-- 不使用 aa 的屬性 --></body></body>
</body><!-- 逐層傳遞 -->
<body name="parent" childclass="aa"><body name="child1" childclass="aa"> <!-- 顯式傳遞 --><body name="grandchild"> <geom/> <!-- 現在繼承 aa --></body></body>
</body>
class僅作用于當前 < body> 自身??,不會自動傳遞給任何子剛體,??子剛體若需繼承??,必須顯式設置自己的 class 或 childclass。
<default><default class="robot_part"><geom rgba="1 0 0 1"/><joint damping="5"/></default>
</default><body name="parent" class="robot_part"> <!-- 當前body繼承robot_part的屬性 --><geom type="box"/> <!-- 紅色方塊 --><body name="child"><!-- 子剛體不受影響!使用全局默認屬性 --><geom type="sphere"/> <!-- 默認顏色 --></body>
</body>
多個class
類名是要唯一的
??同級 < default> 類名不能重復??:在相同嵌套層級下,class 名稱必須唯一。
<default><default class="a">...</default><default class="b">...</default> <!-- 允許,不同名 --><default class="a">...</default> <!-- 錯誤!與第一個 "a" 沖突 -->
</default>
子類只是會繼承父類的屬性,并不是嵌套使用的關系,body能直接使用aa和bb,所以aa和bb不能同名
<default class="a"> <!-- 父類 --><default class="aa">...</default> <!-- 子類,繼承 "a" 的屬性 -->
</default>
<default class="b"><default class="bb">...</default> <!-- 子類,繼承 "b" 的屬性 -->
</default>
worldbody
body關系
跟urdf的link和joint寫法不一樣,但是邏輯是一樣的。
兩個 < body>(剛體)通過 ??關節(< joint>)?? 實現物理連接,關節定義了它們之間的運動約束關系。
父子結構??:子 < body> 必須嵌套在父 < body> 內,嵌套關系表達了父子關節,joint就不用寫父子關節了。
關節定義位置??:關節 < joint> 必須定義在 ??子 < body> 內部??。
??默認行為??:若無關節,子剛體會與父剛體??剛性固定??。
??關節作用??:添加關節后,子剛體可??相對父剛體運動??(旋轉或平移)。
<!--定義父剛體??-->
<body name="parent_body" pos="0 0 0"><!-- 父剛體的幾何、慣性等屬性 -->
</body>
<!--在父剛體內嵌套子剛體??-->
<body name="parent_body"><body name="child_body" pos="0 0 0.1"> <!-- 初始位置偏移 --><!-- 子剛體的關節、幾何等 --></body>
</body>
<!--在子剛體中添加關節??-->
<body name="parent_body"><body name="child_body" pos="0 0 0.1"><joint name="child_joint" type="hinge" axis="0 1 0"/><geom type="box" size="0.1 0.1 0.1"/></body>
</body>
類似下面的urdf
<!-- URDF 的鏈式結構(明確指定父子關系) -->
<link name="parent_link"/>
<joint name="child_joint" type="revolute"><parent link="parent_link"/><child link="child_link"/><axis xyz="0 1 0"/>
</joint>
<link name="child_link"/>
特性 | URDF | MuJoCo |
---|---|---|
??連接方式 ?? | 顯式通過 < parent> 和 標簽 | 隱式通過 < body> 嵌套層級 |
??關節位置 | ?? 獨立于 < link> 之外 | 必須定義在??子 < body> 內部?? |
??初始偏移?? | 在 < joint> 的 中定義 | 在子 < body> 的 pos 屬性中定義 |
??幾何體定義?? | 在 < link> 內通過 < visual>/< collision> | 直接在 < body> 內通過 < geom> |
inertial
joint
能寫的屬性
屬性 | 類型 | 描述 |
---|---|---|
name | 字符串 | 關節的唯一標識符。 |
type | 字符串 | 關節的類型(如hinge、slide、ball等),決定了關節的運動方式。 |
pos | 數組 | 關節相對于其父體的位置,默認為(0,0,0)。 |
axis | 數組 | 運動軸向量,對于hinge和slide關節類型,這是旋轉或滑動發生的方向。 |
range | 數組 | 關節運動的范圍,通常為一個最小值和一個最大值的數組。限制了關節可以達到的最小和最大位置。 |
damping | 浮點數 | 關節的阻尼系數,用于模擬運動中的能量損失。 |
stiffness | 浮點數 | 關節的剛度,用于模擬彈簧效果,影響關節回到初始位置的力度。 |
armature | 浮點數 | 附加到關節軸上的虛擬質量,用于穩定仿真。可以認為是增加關節慣性的一種方式,有助于防止數值問題。 |
limited | 布爾值 | 指示關節是否受到range屬性定義的范圍限制。默認為true,表示受限。 |
關節類型:Hinge(鉸鏈關節),Slide(滑動關節),Ball(球形關節),free(自由關節)