ROS採用rosrun命令可以啟動一個節點,如果需要同時啟動節點管理器(master)和多個節點,就需要採用launch檔案來配置。launch檔案是一種特殊的XML格式檔案,通常以.launch作為檔案尾碼。每個launch檔案都必須要包含一個根項目。
roslaunch的使用方法為:
$ roslaunch pkg-name launch-file-name
下面以一個典型的launch檔案舉例說明:
<launch> <!-- these are the arguments you can pass this launch file, for example paused:=true --> <arg name="debug" default="true"/> <!-- We resume the logic in empty_world.launch, changing only the name of the world to be launched --> <include file="$(find gazebo_ros)/launch/empty_world.launch"> <arg name="debug" value="$(arg debug)" /> </include> <!-- Load the URDF into the ROS Parameter Server --> <arg name="model" /> <param name="robot_description" command="$(find xacro)/xacro.py $(arg model)" /> <!-- Run a python script to the send a service call to gazebo_ros to spawn a URDF robot --> <node name="urdf_spawner" pkg="gazebo_ros" type="spawn_model" respawn="false" output="screen" args="-urdf -model robot1 -param robot_description -z 0.05"/> </launch>
launch
每個launch檔案都必須且只能包含一個根項目。根項目由一對launch標籤定義,其他所有元素標籤都應該包含在這兩個標籤之內。
<launch>...</launch>
arg
roslaunch支援啟動參數arg,可以通過設定arg來改變程式的運行。name為啟動參數的名稱,default為該參數的預設值,value為該參數的參數值。
default與 value兩者的唯一區別在於命令列參數roslaunch pkg-name launch-file-name arg-name:=”set-value”可以覆蓋預設值default,但是不能覆蓋參數值 value。在launch檔案中出現$(arg arg-name)的地方,運行時roslaunch 會將它替換成參數值。並且可以在include元素標籤內使用arg來設定所包含的launch檔案中的參數值。
param
在ROS中prarmeter和argument 是不同的,雖然翻譯一樣。parameter是運行中的ROS系統使用的數值,儲存在參數伺服器(parameter server)中,每個活躍的節點都可以通過 ros::param::get 函數來擷取parameter的值,使用者也可以通過rosparam來獲得parameter的值而argument只在開機檔案內才有意義他們的值是不能被節點直接擷取的。
include
在launch檔案中複用其他launch檔案可以減少代碼編寫的工作量,提高檔案的簡潔性。使用包含元素include在launch檔案中可包含其他launch檔案中所有的節點和參數。
<include file="$(find pkg-name)/launch/launch-file-name"> <arg name="arg_name" value="set-value"/> </include>
node
節點的形式為:
<node name="node-name" pkg="pkg-name" type="executable-name" />
node的三個屬性分別為節點名字、程式包名字和 可執行檔的名字。 name屬性給節點指派了名稱,它將覆蓋任何通過調用 ros::init來賦予節點的名稱。另外node標籤內也可以用過arg設定節點參數值。如果node標籤有children標籤,就需要顯式標籤來定義。即末尾為/node>