Launch 檔案
1 使用Launch檔案
2 建立Launch檔案
3 在namespace中啟動nodes
4 remapping names
5 其他的launch元素
1 使用launch檔案
Launch檔案是ROS提供的,可以同時運行多個nodes的檔案。Launch檔案以一種特殊的XML格式編寫,在ROS packages中使用廣泛。
1.1 運行launch檔案
$ roslaunch package_name launch_file_name
Eg: roslaunch turtlesim example.launch
(1)Tip1: rosrun只能運行一個nodes, roslaunch可以同時運行多個nodes.
Tip2: launch檔案可以不被包含於package中。此時,只需指出該launch檔案的絕對路徑,即可運行。
$ roslaunch completely_path
Eg:$ roslaunch ~/opt/ros/indigo/share/turtlesim/launch/example.launch
Tip3: 為便於執行,每個node最好是相互獨立的。
(2)詳細顯示(request verbosity)
$ roslaunch -v package_name launch_file_name
(3) 結束launch檔案
ctrl+c
2 建立launch檔案
(1) launch檔案一般以.launch尾碼作為檔案名稱,放在package的launch檔案夾下。最簡單的launch檔案可以僅包含幾個nodes。
(2) Launch檔案是XML檔案,每個XML檔案必須有一個root element。而launch檔案的root element由一對launch 標籤定義。
<launch>
...
</launch>
Launch檔案中的其他elements必須都在這一對tags之間。
(3) launch檔案的核心是一系列node elements,每個node element啟動一個node。Node element看起來如下:
<node
pkg=”package_name” type=”executable_name” name=”node_name”
/>
Tip1: 最後的“/”是必不可少的。
Tip2: 也可以寫成<node pkg=”..” type=”...” name=”...”></node>
如果該node中有其他tags,則必須使用這種形式。
(4) 一個node element包含三個必須的屬性:pkg, type, name.
pkg和type屬性指出ROS應該運行哪個pkg中的哪個node,注意:此處的type是可執行檔的名稱,而name則是可以任意給出的,它覆蓋了原有檔案中ros::init指定的node name。
(5) 使用匿名(anonymous name)
name=”$(anon base_name)”
(6) node 記錄檔(log file)
運行roslaunch和用rosrun運行單個節點的區別之一是,預設情況下,roslaunch啟動並執行nodes的標準輸出會重新導向到log file,不顯示在控制台。
該記錄檔的位置和名稱如下:
~/.ros/log/run_id/node_name-number-stdout.log
其中,run_id是master啟動後產生的特殊標識符,number是表示nodes數量的整數。如,turtlesim-1-stdout.log; teleop_key-3-stdout.log.
(7) 輸出到控制台
用output屬性, output=”screen”;這種方法僅顯示一個node。
若顯示所有nodes的輸出,用--screen命令列。
$ roslaunch --screen package_name launch_file_name
如果正在啟動並執行檔案沒有顯示想要對輸出,可以查看該node屬性集中是否有 output=”screen”.
(8) 要求重生(request respawning)
開啟所有nodes後,roslaunch會監視每個node,記錄那些仍然活動的nodes。對於每個node,當其終止後,我們可以要求roslaunch重啟該node,通過使用respawn屬性。
respawn=”true”
(10) 必需的nodes
required屬性與respawn相反,不能同時對同一個node使用。
required=”true”
當一個required node終止後,所有其他的nodes都會終止,並退出。這種命令有時很有用。比如,當一個很重要的node失敗後,整個會話都會被扔掉,那些加上了respawn屬性的nodes也會停止。
(11) 在獨立的視窗運行各nodes
我們在各自的termin運行rosrun node_name;但是運行roslaunch時,所有的nodes共用一個相同的terminal,這對於那些需要從控制台輸入的nodes很不方便。可以使用launch-prefix屬性。
launch-prefix=”command-prefix”
Eg:launch-prefix=”xterm -e”
等價於 xterm -e rosrun turtlesim turtle_teleop_key
xterm 命令表示建立一個terminal; -e參數告訴xterm執行剩下的命令列。
當然,launch-prefix屬性不僅僅限於xterm。它可用於調試(通過gdb或valgrind),或用於降低進程的執行順序(通過nice).
3 在namespace中執行nodes
為node設定預設的namespace的常用方法——被稱為“pushing down into a namespace”的進程,用於launch檔案,並在其node element中指定ns屬性。
ns=”namespace”
launch檔案中的node names是relative names。同一個launch檔案中,允許不同namespace中出現相同的node names。Roslaunch要求node names必須是base names——不指定任何namespaces的relative names;如果node element中出現node name為global name,則會報錯。
4 重新對應names(remapping names)
除瞭解析relative names和private names,ROS也支援重新對應,用於修改nodes當前使用的名稱。
重新對應相當於換名,每次重新對應需提供一個original name和一個new name。每次node使用它的original name, ROS client library都會將其替換為remapping name。
建立remapping name兩種方法:
1. 對於單個node,在命令列進行remapping(remap對象可以是node,topic等)。
original-name:=new-name
Eg: $ rosrun turtlesim turtlesim_node turtle1/pose:=tim
2. 在launch檔案內remap names,使用remap element
<remap from=”original_name” to “new_name”>
如果remap出現在launch檔案開頭,作為launch檔案的子項目,則該remapping將被用於隨後所有的nodes。如果remap作為某個node的子項目,則只用於該節點。
Eg:<node pkg=”turtlesim” type=”turtlesim_node” name=”turtle1”>
<remap from =”turtle1/pose” to “tim”>
</node>
注意:在ROS進行remapping之前,remaping的所有name,包括original和new names,都將被解析為global names。所以,remapping之後所有的名字通常都是relative names。
5 其他的launch elements
5.1 including其他檔案
為包含其他launch檔案,包括這些launch檔案的所有nodes和parameters,用include element。
<include file=”path-to-launch-file”>
這種情況下,file屬性必須寫出該launch檔案的全部路徑名稱,顯得很繁瑣。因此,常用
<include file=”$(find package_name)/launch_file_name”/>
注意,執行該launch檔案時,roslaunch會搜尋該package下的所有子目錄;因此,必須給出package_name。此外,include也支援ns屬性,將它的內容放進指定的namespace。
<include file=”...” ns=”namespace_name”/>
5.2 Launch arguments
為便於launch檔案重構,roslaunch支援launch arguments,也成為arguments或者args,類似於局部變數。
注意:儘管argument和parameter有時可互換,但他們在ROS中的意義完全不同。Parameters是ROS系統使用的數值,存在parameter server上,nodes可通過ros::param::get函數編程得到,使用者可通過rosparam擷取。與之不同,arguments僅在launch檔案內部有意義,nodes不能直接擷取它們的值。
(1)聲明argument
<arg name=”arg_name”>
(2)指定argument的值
Launch檔案中的每個argument都必須有指定值。賦值方法有好幾種。
第一種,在命令列賦值
$ roslaunch package_name launch_file_name arg-name:=arg_value
第二種,在聲明argument時賦值
<arg name=”arg_name” default=”arg_name”/>
<arg name=”arg_name” value=”arg_name”/>
上面兩行的區別在於,命令列參數可以覆蓋default,但是不能重寫value的值。
(3)擷取變數值
一旦聲明某個argument並賦值後,我們可以通過arg使用該argument.
$(arg arg-name)
如果該行出現,roslaunch將會用給定arg-name的值替換其左邊的值。
(4)將argument值傳給included launch檔案
<include file=”path-to-file”>
<arg name=”arg_name” value=”arg_value”/>
......
</include>
若在launch檔案中,launch檔案及其包含的launch檔案出現出現相同的arguments,則需在launch檔案及included launch檔案中同時寫:
<arg name=”arg_name” value=”$(arg arg_name)”/>
第一個arg_name表示indluded launch檔案中的argument,第二個arg_name表示launch檔案中的argument.其結果是指定的argument在launch檔案及included launch檔案中都有相同的值。
5.3 建立groups
Group element可以再大型的launch檔案中將指定的nodes組織起來。它有兩個用處:
其一,group可以將幾個nodes放進同一個namespace
<group ns=”namespace”>
<node pkg=”..” .../>
<node pkg=”..” .../>
......
</group>
注意,如果grouped node已經有它自己的namespace,並且是relative name,那麼該node的namespace是其relative name,並以group namespace為尾碼。
其二,group可以同時啟動或者終止一組nodes。
<group if=”0 or 1”>
......
</group>
如果該屬性的值是1