param name="robot_description" command=" $(find xacro)/xacro --inorder ' $(arg model)' 到底什么意思

今天看到个代码,让我一脸懵逼。才发现这么常见的一句话,我竟然还不怎么清楚它的含义。经过探索,发现两个xacro使用大法。

  <param name="robot_description" command="
    $(find xacro)/xacro --inorder '$(arg model)'
    enable_logging:=$(arg enable_logging)
    enable_ground_truth:=$(arg enable_ground_truth)
    enable_mavlink_interface:=$(arg enable_mavlink_interface)
    log_file:=$(arg log_file)
    wait_to_record_bag:=$(arg wait_to_record_bag)
    mav_name:=$(arg mav_name)
    namespace:=$(arg namespace)"
  />

已知的

这句话的目的是给 robot_description赋值,从而使得rviz通过robo_description获得机器人模型。
赋的值为:’$ (find xacro)/xacro --inorder’ 在输入参数’为’$ (arg model)’ 情况下的的运行结果。类似于命令:

rosrun xacro xacro --inorder file.xacro > /tmp/new.xml

【注】- - inorder命令是jade版本才出现的,能够匹配jade及之后的xacro。

然后后面那一堆参数是什么呢???

探索

在ros官网发现了端倪:
在这里插入图片描述
就是说,后面的几个参数都是传递给xacro文件的。
然后我们再看看xacro文件

<robot name="firefly" xmlns:xacro="http://ros.org/wiki/xacro">
  <xacro:include filename="$(find rotors_description)/urdf/component_snippets.xacro" />
  <!-- Instantiate firefly "mechanics" -->
  <xacro:include filename="$(find rotors_description)/urdf/firefly.xacro" />

  <!-- Instantiate a controller. -->
  <xacro:controller_plugin_macro namespace="${namespace}" imu_sub_topic="imu" />

  <xacro:if value="$(arg enable_mavlink_interface)">
    <!-- Instantiate mavlink telemetry interface. -->
    <xacro:default_mavlink_interface namespace="${namespace}" imu_sub_topic="imu" rotor_count="6" />
  </xacro:if>

  <!-- Mount an ADIS16448 IMU. -->
  <xacro:default_imu namespace="${namespace}" parent_link="${namespace}/base_link" />

  <xacro:if value="$(arg enable_ground_truth)">
    <xacro:ground_truth_imu_and_odometry namespace="${namespace}" parent_link="${namespace}/base_link" />
  </xacro:if>

  <xacro:if value="$(arg enable_logging)">
    <!-- Instantiate a logger -->
    <xacro:bag_plugin_macro
      namespace="${namespace}"
      bag_file="$(arg log_file)"
      rotor_velocity_slowdown_sim="${rotor_velocity_slowdown_sim}" 
      wait_to_record_bag="$(arg wait_to_record_bag)" />
  </xacro:if>

</robot>

从中可以总结出两个使用大法:

命名空间和文件路径

将文件按照命名空间分类存放,依此索引文件。如

parent_link="${namespace}/base_link" 

模型组合

通过xacro文件里的< xacro:if value="$ (arg xxxx) ">和roslaunch里的xacro参数输入,实现
模型和功能的组合。

  <xacro:if value="$(arg enable_logging)">
 	...
  </xacro:if>
<param name="robot_description" command="
    $(find xacro)/xacro --inorder '$(arg model)'
    enable_logging:=$(arg enable_logging)   />

备忘

参考官网,把到目前为止见过的xacro语法,稍加扩展记录在这里,以备忘。有一些用法是jade及之后版本才有的。

<!----------------------property------------------------>
<xacro:property name="the_radius" value="2.1" />
<xacro:property name="the_length" value="4.5" />

<geometry type="cylinder" radius="${the_radius}" length="${the_length}" />

<!--------------------property blocks-------------------->
<xacro:property name="front_left_origin">
  <origin xyz="0.3 0 0" rpy="0 0 0" />
</xacro:property>

<pr2_wheel name="front_left_wheel">
  <xacro:insert_block name="front_left_origin" />
</pr2_wheel>

<!------------------------math--------------------------->
<xacro:property name="R" value="2" />
<xacro:property name="alpha" value="${30/180*pi}" />
<circle circumference="${2 * pi * R}" pos="${sin(alpha)} ${cos(alpha)}" /> 
<limit lower="${radians(-90)}" upper="${radians(90)}" effort="0" velocity="${radians(75)}" />

<!-----------------conditional blocks-------------------->
<xacro:if value="<expression>">
  <... some xml code here ...>
</xacro:if>
<xacro:unless value="<expression>">
  <... some xml code here ...>
</xacro:unless>

<xacro:property name="var" value="useit"/>
<xacro:if value="${var == 'useit'}"/>
<xacro:if value="${var.startswith('use') and var.endswith('it')}"/>

<xacro:property name="allowed" value="${[1,2,3]}"/>
<xacro:if value="${1 in allowed}"/>

<!-------------------rospack commands-------------------->
<foo value="$(find xacro)" />

<xacro:arg name="myvar" default="false"/>
<!--run as-->
<param name="robot_description" command="$(find xacro)/xacro.py $(arg model) myvar:=true" />

<!------------------------macros------------------------->
<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: -->
<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>

<!--------------------default parameters--------------------->
<xacro:macro name="foo" params="x:=${x} y:=${2*y} z:=0"/>
<xacro:macro name="foo" params="p1 p2:=expr_a p3:=^ p4:=^|expr_b">
<!--The caret ^ indicates to use the outer-scope property (with same name). 
The pipe | indicates to use the given fallback if the property is not defined in outer scope. -->

<!--------------Including other xacro files------------------>
<xacro:include filename="$(find package)/other_file.xacro" />
<xacro:include filename="other_file.xacro" ns="namespace"/>

<!-----------------------yaml support------------------------>
<xacro:property name="props" value="${dict(a=1, b=2, c=3)}"/>
<xacro:property name="numbers" value="${[1,2,3,4]}"/>

<xacro:property name="yaml_file" value="$(find package)/config/props.yaml" />
<xacro:property name="props" value="${load_yaml(yaml_file)}"/>
  • 17
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值