ROS进阶学习手记6.1 -- Launch File

ROS进阶学习手记6.1 – Launch File

在以后的ROS运用中,我们逐渐需要运行多个nodes,而launch file的编写是ROS高级运用的基础。

官方的launch file wiki文档好像不是很好用:wiki.ros.org/roslaunch/

这里我主要参考:A Gentle Introduction to ROS by JasonM. O’Kane  的 Chapter6

先来尝尝鲜:
   

接着就是好好读书。

这里只会写一点总结文字。 :)

== roslaunch - Commands and launch file ==

1. How to usd launch file:

  Command:

    $ roslaunch package-name launch-file-name
    $ roslaunch ~/{workspace}/src/{pkgname}/example.launch

  All of the nodes in a launch file are started at roughly the same time.

  Request verbose output:

    $ roslaunch -v package-name launch-file-name

2. Creating launch files:

2.1 Where to place launch files

    {pkg directory}/launch/launchfile.launch

2.2 Basic ingredients

    The simplest launch files consist of a root element containing several node elements.

    <launch>
      <node pkg="package-name" type="executable-name" name="node-name" />
    </launch>

    You can also also write the closing tag explicitly:

      <node pkg="..." type="... " name="... "></node>

    In fact, this explicit closing tag is needed if the node has children, such as remap or
paramelements.

    pkg 和 type 两个属性指定了ROS应该运行哪个程序来启动这个节点。相当于两行rosrun命令,一行指定pkg name,一行指定excutabel name。
    name 属性给这个node分配了一个名称。它会覆盖node代码中ros::init段给自己分配的那个name.
    看下面这个例子:

  <node
    pkg="turtlesim"
    type="turtle_teleop_key"
    name="teleop_key "
    required="true "
    launch −prefix="xterm −e"
    ns="sim1 "
  />    

默认下,$ROS_HOME/log会存储roslaunch运行后的log文件。通过output=“log|screen”(optional) 来改。

    roslaunch --screen package-name launch-file-name

   output=“screen” or “log”

   Requesting respawning:   respawn=“true”
   Requiring nodes:              required=“true”
   Launching nodes in their own windows:

     launch-prefix="command-prefix"
     launch-prefix="xterm -e"
     xterm -e rosrun turtlesim turtle_teleop_key

   node标签还有一些属性,也非常有用. 更多请见:
   http://wiki.ros.org/roslaunch/XML/node#Attributes
   

2.3 Launching nodes inside a namespace

   ROS supports relative names, which utilize the concept of a default namespace. The usual way to set the default namespace for a node—a process often called pushing down into a namespace—is to use a launch file, and assign the ns attribute in its node element:
   ns=“namespace”

   e.g.
   ———————————

1. <launch>
2. <node
3. name="turtlesim_node"
4. pkg="turtlesim"
5. type="turtlesim_node"
6. ns="sim1"
7. />
8. <node
9. pkg="turtlesim"
10. type="turtle_teleop_key"
11. name="teleop_key"
12. required="true"
13. launch−prefix="xterm−e"
14. ns="sim1"
15. />
16. <node
17. name="turtlesim_node"
18. pkg="turtlesim"
19. type="turtlesim_node"
20. ns="sim2"
21. />
22. <node
23. pkg="agitr"
24. type="pubvel"
25. name="velocity_publisher"
26. ns="sim2"
27. />
28. </launch>
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)

 —————————-
    In this case, both nodes have the same relative name, turtlesim_node. Such identical relative names
are not a problem, however, because the global names to which they are resolved, namely /sim1/turtlesim_node and /sim2/turtlesim_node, are different.

3. Remapping names

   任何node的名字都可以被重映射,当它在命令行中launched时。这是一个ROS的强大功能。它可以让你通过命令行在多种配置下,运行同样的node,而不冲突。还可以为private node prarmeter 提供 assignment. 这个可以让你区别复杂的名字(defer complex name assignments to the actual runtime loading of the system. )在系统运行期间。
   http://wiki.ros.org/Remapping%20Arguments
   

3.1 Creating remappings

   There are two ways create remappings when starting a node.
     original-name:=new-name
   or:
    

3.2 Reversing a turtle

   写一个broadcaster,收听cmd_vel,翻转,发布一个叫做cmd_vel_reversed的topic。然后让turtlesim_node remapping到cmd_vel_reversed上。
   

4. Other launch file elements

   我们以下面这个launch file为例,说几个Other launch file elements
   =========================

1. 1 <launch>
2. 2    <include
3. 3    file="$(findagitr)/doublesim.launch"
4. 4    />
5. 5    <arg
6. 6    name="use_sim3"
7. 7    default="0"
8. 8    />
9. 9
10. 10    <groupns="sim3"if="$(arguse_sim3)">
11. 11    <node
12. 12    name="turtlesim_node"
13. 13    pkg="turtlesim"
14. 14    type="turtlesim_node"
15. 15    />
16. 16    <node
17. 17    pkg="turtlesim"
18. 18    type="turtle_teleop_key"
19. 19    name="teleop_key"
20. 20    required="true"
21. 21    launch−prefix="xterm−e"
22. 22    />
23. 23    </group>
24. 24</launch>
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)

   =========================

4.1 Including other files

   <include file="path-to-launch-file" />
   <include file="$(find package-name)/launch-file-name" />
   <include file=". . . " ns="namespace" />

4.2 Launch arguments

   让launch files 是可以配置的, roslaunch命令支持launch arguments, 也叫做arguments or args.
   这有点像可执行文件的局部变量。好处是比如每次运行时,有些小地方的不同,通过使用一些arguments,你可以避开代码的重复。
   
   Declaring arguments
   To declare the existence of an argument, use an arg element:
    

   Assigning argument values
   launch file里的argument必须要赋值,You can provide a value on the roslaunch command line:

      $ roslaunch package-name launch-file-name arg-name:=arg-value

   Alternatively, in your launchfile, using one of these two syntaxes:

      <arg name="arg-name" default="arg-value" />
      <arg name="arg-name" value="arg-value" />

   注意:argument values set by “value” cannot be changed.

   Accessing argument values
   Once an argument is declared and a value assigned to it, you can use its value using an arg substitution, like this:
      $(arg arg-name)
   
Sending argument values to included launch files
     <include file=“path-to-launch-file”>
      
       . . .
    

4.3 Creating groups

   One final launch file feature is the group element, which provides a convenient way to organize nodes in a large launch file.
   Groups can push several nodes into the same namespace.
    
      . . .
    

   Every node within the group starts with the given default namespace.
   Group还可以加一些条件,使某些节点使能或使不能。更多详情请参见教材。。

总结

   这里我们看到通过launch file和 roslaunch 命令,如何实现 启动nodes, 加上复杂的配置。具体的内容参见教材。
   《A Gentle Introduction to ROS by JasonM. O’Kane》南卡罗来纳州立大学教授写的。免费pdf
 

Published At