7.Macros
The main feature of xacro is its support for macros. Define macros with the macro tag, and specify the macro name and the list of parameters. The list of parameters should be whitespace separated. They become macro-local properties.
xacro的主要特性是它对宏的支持。 使用宏标签定义宏,并指定宏名称和参数列表。 参数列表应以空格分隔。 它们变成宏观本地属性。
<xacro:macro name="pr2_caster" params="suffix *origin **content **anothercontent">
<joint name="caster_${suffix}_joint">
<axis xyz="0 0 1" />
</joint>
<link name="caster_${suffix}">
<xacro:insert_block name="origin" />
<xacro:insert_block name="content" />
<xacro:insert_block name="anothercontent" />
</link>
</xacro:macro>
<xacro:pr2_caster suffix="front_left">
<pose xyz="0 1 0" rpy="0 0 0" />
<container>
<color name="yellow"/>
<mass>0.1</mass>
</container>
<another>
<inertial>
<origin xyz="0 0 0.5" rpy="0 0 0"/>
<mass value="1"/>
<inertia ixx="100" ixy="0" ixz="0" iyy="100" iyz="0" izz="100" />
</inertial>
</another>
</xacro:pr2_caster>
The example declares a macro “pr2_caster”, which takes two parameters: suffix and origin. Note that “origin” is starred. This indicates that origin is a block parameter instead of a simple text parameter. Look ahead to the use of pr2_caster. The suffix property is defined in the pr2_caster tag as an attribute, but no origin property is defined. Instead, origin refers to the first element inside (the “pose” block, in this case). The double-starred version (“content”, “anothercontent”) allows to insert an arbitrary number of elements that are passed within elements subsequently available (“container”, “another” respectively in the example above). This example expands to the following:
该示例声明了一个宏“pr2_caster”,它有两个参数:suffix和origin。 请注意,“origin”已加星标。 这表明origin是一个块参数,而不是一个简单的文本参数。 向前看pr2_caster的使用。 后缀属性在pr2_caster标记中定义为属性,但没有定义origin属性。 相反,origin指的是第一个元素(在这种情况下是“pose”块)。 双星号版本(“content”,“anothercontent”)允许插入在随后可用的元素(在上面的示例中分别是“container”,“another”)中传递的任意数量的元素。 此示例扩展为以下内容:
<joint name="caster_front_left_joint">
<axis xyz="0 0 1" />
</joint>
<link name="caster_front_left">
<pose xyz="0 1 0" rpy="0 0 0" />
<color name="yellow" />
<mass>0.1</mass>
<inertial>
<origin xyz="0 0 0.5" rpy="0 0 0"/>
<mass value="1"/>
<inertia ixx="100" ixy="0" ixz="0" iyy="100" iyz="0" izz="100" />
</inertial>
</link>
Multiple block parameters will be processed in the specified order:
将按指定的顺序处理多个块参数:
<xacro:macro name="reorder" params="*first *second">
<xacro:insert_block name="second"/>
<xacro:insert_block name="first"/>
</xacro:macro>
<reorder>
<first/>
<second/>
</reorder>