ROS学习笔记9 —— launch文件


案例

<?xml version="1.0"?>
<launch>
	<arg name="debug" default="true"/>
	<param name="PARAMETER_NAME" value="parameter value"/>
	
	<include file="$(find gazebo_ros)/launch/empty_world.launch"> <arg name="debug" value="$(arg debug)" /> </include>
	
	<arg name="model" /> <param name="robot_description" command="$(find xacro)/xacro.py $(arg model)" />

	<node name="urdf_spawner" pkg="gazebo_ros" type="spawn_model" respawn="false" output="screen" args="-urdf -model robot1 -param robot_description -z 0.05"/>

	<node pkg="turtlesim" type="turtlesim_node" name="turtlesim" respawn="true"/>
	
	<node pkg="turtlesim" type="turtle_teleop_key" name="teleop_key" required="true" launch-prefix="xterm -e"/>

    <node
        pkg=""
        type=""
        name=""  
        respawn="true" 
        required="true"
        launch-prefix="xterm -e"
        output="screen"
        ns="some_namespace"
    />
	
      <group ns="turtlesim1">
              <node pkg="turtlesim" name="sim" type="turtlesim_node"/>
      </group>

      <group ns="turtlesim2">
              <node pkg="turtlesim" name="sim" type="turtlesim_node"/>
      </group>

      <node pkg="turtlesim" name="mimic" type="mimic">
              <remap from="input"  to="turtlesim1/turtle1" />
              <remap from="output" to="turtlesim2/turtle1" />
      </node>
</launch>

标签

<launch>                <!--根标签-->
<node>                  <!--需要启动的node及其参数-->
<include>               <!--包含其他launch-->
<machine>               <!--指定运行的机器-->
<env-loader>            <!--设置环境变量-->
<param>                 <!--定义参数到参数服务器-->
<rosparam>              <!--加载yaml文件中的参数到参数服务器-->
<arg>                   <!--定义变量-->
<remap>                 <!--设定 topic 映射-->
<group>                 <!--设定分组-->
</launch>               <!--根标签-->

注意:roslaunch不能保证node的启动顺序!!!

1.参数的定义

a. 参数设置
  • <param>:设置ROS部分参数,节点可通过ros::param::get来获取该值

    	# 法1:
    	<param name="parameter name" value="parameter value"/>
    	<param name="param_name" type="type1" value="val"/>     # type可以省略,系统自动判断
    	<param name="param_name" textfile="$(find pkg)/path/file"/>     # 读取 file 存成 string
    	<param name="param_name" command="$(find pkg)/exe '$(find pkg)/arg.txt'"/>
    	<param name="param" type="yaml" command="cat '$(find pkg)/*.yaml'"/> # command 的结果存在 param 中
    	<param name="somestring1" value="bar" />#明显看出是字符串类型
      
      <param name="somestring2" value="10" type="str" />#强行定义为字符串类型
      <param name="someinteger1" value="1" type="int" />#明显是整型,不用写type了
      <param name="someinteger2" value="2" />#明显看出是字符串类型,故省略type
    
      <param name="somefloat1" value="3.14159" type="double" />#明显是浮点型,可以不用写type了
      <param name="somefloat2" value="3.0" />#明显是浮点型,可以不用写type了
     
      <param name="configfile" textfile="$(find roslaunch)/example.xml" />#把路径中的文件直接存如configfile中,并且把这个全局参数存放在ros参数服务器中
    
      <param name="binaryfile" binfile="$(find roslaunch)/example.xml" />#把路径中的xml文件以基于64位二进制形式存入binaryfile中,并且把这个参数存放在ros服务器中
    		
    	# 法2:利用yaml,使用rosparam标签
    	<rosparam file="$YAML_DIR/config.yaml" command="load" ns="namespace you want to use"/>
    
  • <rosparam>:批量操作

    # load : 从 YAML 文件中加载一批 param
    <rosparam command="load" file="$(find rosparam)/example.yaml" />
    
    # delete: 删除某个 param
    <rosparam command="delete" param="my_param" />
    
    # 类似 <param> 的赋值操作
    ## 法1:
    <rosparam param="my_param">[1,2,3,4]</rosparam>
    ## 法2:
    <rosparam>
    a: 1
    b: 2
    </rosparam>
    
  • <arg>:launch内利用的参数

    # 定义
    <arg name="argName" default="aaaaaaa"/>
    
    # 调用
    <node name="$(arg argName)" type="$(arg argName)"/>
    
b. paramrosparam的区别
  • 只能对单个 param 操作,而且只有三种:value, textfile, command 形式,返回的是单个 param 的内容。
  • 则可以批量操作,还包括一些对参数设置的命令,如 dump, delete 等

c. defaultvalue的区别
  • default:参数的默认值,可被重写
  • value:参数的参数值,固定值,不可被重写

若执行roslaunch pkgName launchFileName argName:="setValue",可覆盖默认值default,但不能覆盖value

2. node标签的使用

<launch>
    <node
        pkg="aaa"
        type="aaa"
        name="aaa"  
        respawn="true" 
        required="true"
        launch-prefix="xterm -e"
        output="screen"
        ns="some_namespace"
    />
</launch>
  • pkg:功能包的名称
  • type:功能包中的可执行文件或者源文件编译后的可执行文件名称
  • name:节点启动后的名字,将覆盖ros::init设置的节点名称,不可重复,除非不在同一命名空间下。
  • respawn:若该节点关闭,是否自动重新启动
  • required:若该节点关闭,是否关闭其他所有节点
  • launch-prefix: 是否新开一个窗口执行。例如,需要通过窗口进行机器人移动控制的时候,应该为控制 node 新开一个窗口;或者当 node 有些信息输出,不希望与其他 node 信息混杂在一起的时候。
  • output:默认情况下,launch 启动 node 的信息会存在/.ros/log/run_id/node_name-number-stdout.log中,通过此处参数设置,可令信息显示在屏幕上
  • ns:将 node归入不同的namespace,即在node name前边加 ns指定的前缀。 注意:此时在源文件中定义node nametopic name时应使用相对名称,即不加/

3. 接口重映射

使用接口重映射改变topic名称:

<node pkg="aaa" type="aaa" name="aaa">
    <remap from="origin" to="new" />
</node>

4. group标签

<group ns="wg2">
    <remap from="chatter" to="talker"/> # 对该 group 中后续所有 node 都有效
    <node ... />
    <node ... >
        <remap from="chatter" to="talker1"/> # 各个 node 中可以重新设置 remap
    </node>
</group>

5. 嵌套复用

<include file="launch-file-name.launch"/>

# 增强可移植性
<include file="$(find package-name)/launch-file-name" />

# 为了放入某一命名空间
<include file="$(find package-name)/launch-file-name " ns="namespace_name" />

6. 其他XML format

  • env:替换当前环境中的一个变量的值;如果环境中没有设置该值,则该launch文件启动失败

    $(env ENVIRONMENT_VARIABLE)
    
  • optenv:如果环境变量有这个值,则替换;如果提供了默认值,且环境变量中没有该值,则添加该值;如果没有个默认值,则会使用一个空字符串;(默认值可以是以空格分开的多个单词)

    $(optenv ENVIRONMENT_VARIABLE) 
    $(optenv ENVIRONMENT_VARIABLE default_value)
    
    <param name="foo" value="$(optenv NUM_CPUS 1)" />
    <param name="foo" value="$(optenv CONFIG_PATH /home/marvin/ros_workspace)" />
    <param name="foo" value="$(optenv VARIABLE ros rocks)" />
    
    
  • $(find rospy)

     $(find rospy)/manifest.xml#获取rospy包下的manifest.xml的路径
    
  • $(anon name)

     $(anon name)#在name后面添加你的主机名+一串数字;以产生一个独一无二的名字
    
    <node
        name="$(anon joint_state_publisher)"
        pkg="joint_state_publisher"
        type="joint_state_publisher" />
    
  • doc:用来描述一个标签

      <arg name="robot_ip" doc="IP of the controller" />
    
  • $(eval )

      <arg name="radius" value="5"/>
      <arg name="pi" value="3.141592653"/>
      <param name="circumference" value="$(eval 2*arg('pi')*arg('radius'))"/>#调用参数的值必须在参数名上加单引号
    
  • unless

      <param name="other_launch" value="I have be launched" unless="0"/> #可直接认为unless是取反,unless为假是执行该指令
    
  • if

      <group if="$(arg include)">#如果include参数是正,才会执行group标签中的内容
      <param name="binaryfile" binfile="$(find gripper)/package.xml" />
      <include file="$(find gripper)/launch/other.launch" />
      </group>
    

7. 以urdf和xacro为基础的launch文件案例

#######################      urdf     ############################
<launch>
	<!-- 通过定义全局变量,告知launch文件启动时把全局变量robot_description中存储的模型文件加载到rviz中 -->
	<param name="robot_description" textfile="$(find mbot_description)/urdf/urdf/mbot_base.urdf" />

	<!-- 设置GUI参数,显示关节控制插件(可以把这个插件同下面节点看成一个整体,用于控制关节运动) -->
	<param name="use_gui" value="true"/>
	
	<!-- 运行joint_state_publisher节点,发布机器人的关节状态(显示关节旋转了多少度等等)  -->
	<node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher" />
	
	<!-- 运行robot_state_publisher节点,发布tf(根据上面的关节状态,创建整个机器人的tf关系,并发布到系统中)-->
	<node name="robot_state_publisher" pkg="robot_state_publisher" type="state_publisher" />
	
	<!-- 运行rviz可视化界面(args的参数作用类似于自定义rviz中的显示设置) -->
	<node name="rviz" pkg="rviz" type="rviz" args="-d $(find mbot_description)/config/mbot_urdf.rviz" required="true" />
</launch>

#######################      xacro     ############################
<launch>
	<arg name="model" default="$(find xacro)/xacro --inorder '$(find mbot_description)/urdf/xacro/mbot.xacro'" />
	<arg name="gui" default="true" />

	<param name="robot_description" command="$(arg model)" />

    <!-- 设置GUI参数,显示关节控制插件 -->
	<param name="use_gui" value="$(arg gui)"/>

    <!-- 运行joint_state_publisher节点,发布机器人的关节状态  -->
	<node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher" />

	<!-- 运行robot_state_publisher节点,发布tf  -->
	<node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher" />

    <!-- 运行rviz可视化界面 -->
	<node name="rviz" pkg="rviz" type="rviz" args="-d $(find mbot_description)/config/mbot_urdf.rviz" required="true" />

</launch>


参考文献:

  • 6
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值