ROS實踐手冊(九)Launch文件啓動

筆者根據 古月居 · ROS入門21講 學習整理,並參考《ROS機器人開發實踐》一書。
相關課件及源碼可參考 Github/huchunxu/ros_21_tutorials

Launch文件常用語法

  • : 根元素標籤 <launch> </launch>
  • : 啓動節點 <node pkg="package-name" type="executable-name" name="node-name" />
    • pkg: 節點所在功能包名稱
    • type: 節點的可執行文件名稱
    • name: 節點運行時的名稱
    • 可選屬性
      • output: 是否在終端打印日誌信息
      • respawn: 節點若發生故障是否重啓
      • required: 節點是否必須啓動
      • ns: 命名空間namespace避免命名衝突
      • args: 節點所需的運行參數
  • : 設置參數 `` * name: 參數名 * value: 參數值
  • : 加載參數文件中的多個參數 <rosparam file="params.yaml" command="load" ns="params" />
    • file: yaml參數配置文件
    • command: 加載指令
      • list:列出當前所有參數
      • dump:保存參數到文件
      • load:從文件讀取參數
    • ns: 命名空間namespace避免命名衝突
  • : launch文件內部的局部變量,僅限於launch文件使用 <arg name="arg-name" default="arg-value" />
    • name: 參數名
    • value: 參數值
    • 調用
      • <param name="foo" value="$(arg arg-name)" />
      • <node name="node" pkg="package" type="type" args="$(arg arg-name)" />
  • : 重映射ROS計算圖資源 <remap from="/turtlebot/cmd_vel" to="/cmd_vel" />
    • from: 原命名
    • to: 映射之後的命名
  • : 嵌套包含其他launch文件,類似C語言中的頭文件包含 <include file="$(dirname)/other.launch" />
    • file: 包含的其他launch文件的路徑
  • 更多標籤可參見:http://wiki.ros.org/roslaunch/XML

simple.launch實踐

  • 右鍵點擊 ROS 工作區下的 “catkin_ws/src” ,選擇 “新建ROS包” ,輸入包名稱及其依賴包的名稱 learning_launch ,回車後,會創建名爲 “learning_launch” 無依賴的 ROS 包。
  • 右鍵點擊 catkin_ws/src/learning_launch/launch 文件夾,點擊 “新建launch文件” 輸入文件名simple.launch
  • 編輯 simple.launch 文件如下。
<launch>
    <node pkg="learning_topic" type="person_subscriber" name="talker" output="screen" />
    <node pkg="learning_topic" type="person_publisher" name="listener" output="screen" /> 
</launch>

  • 在主界面左上角,修改資源管理器旁的構建方式爲“Debug”,並點擊小錘子進行構建
  • 無需啓動roscore,因爲launch文件啓動方式默認啓動roscore。
  • 在主界面右側下方點擊“+”創建新終端,輸入$ source devel/setup.bash。輸入$ roslaunch learning_launch simple.launch啓動

其他launch實例文件

start_tf_demo_c++.launch

<launch>
    <!-- Turtlesim Node-->
    <node pkg="turtlesim" type="turtlesim_node" name="sim"/>
    <node pkg="turtlesim" type="turtle_teleop_key" name="teleop" output="screen"/>
    <node pkg="learning_tf" type="turtle_tf_broadcaster" args="/turtle1" name="turtle1_tf_broadcaster" />
    <node pkg="learning_tf" type="turtle_tf_broadcaster" args="/turtle2" name="turtle2_tf_broadcaster" />
    <node pkg="learning_tf" type="turtle_tf_listener" name="listener" />
 </launch>

start_tf_demo_py.launch

<launch>
    <!-- Turtlesim Node-->
    <node pkg="turtlesim" type="turtlesim_node" name="sim"/>
    <node pkg="turtlesim" type="turtle_teleop_key" name="teleop" output="screen"/>
    <node name="turtle1_tf_broadcaster" pkg="learning_tf" type="turtle_tf_broadcaster.py">
    	<param name="turtle" type="string" value="turtle1" />
    </node>
    <node name="turtle2_tf_broadcaster" pkg="learning_tf" type="turtle_tf_broadcaster.py">
    	<param name="turtle" type="string" value="turtle2" /> 
    </node>
    <node pkg="learning_tf" type="turtle_tf_listener.py" name="listener" />
</launch>

turtlesim_parameter_config.launch

<launch>
    <param name="/turtle_number"   value="2"/>
    <node pkg="turtlesim" type="turtlesim_node" name="turtlesim_node">
        <param name="turtle_name1"   value="Tom"/>
        <param name="turtle_name2"   value="Jerry"/>
        <rosparam file="$(find learning_launch)/config/param.yaml" command="load"/>
    </node>
    <node pkg="turtlesim" type="turtle_teleop_key" name="turtle_teleop_key" output="screen"/>
</launch>

turtlesim_remap.launch

<launch>
    <include file="$(find learning_launch)/launch/simple.launch" />
    <node pkg="turtlesim" type="turtlesim_node" name="turtlesim_node">
        <remap from="/turtle1/cmd_vel" to="/cmd_vel"/>
    </node>
</launch>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章