ROS系统 launch启动文件的使用方法

launch文件:通过XML文件实现多节点的配置和启动(可以自动启动ROS Master)

使用步骤

  • 选定功能包右击 —> 添加 launch 文件夹
  • 选定 launch 文件夹右击 —> 添加 launch 文件
  • 编辑 launch 文件内容
  • 运行 launch 文件
  • 运行结果: 一次性启动了多个节点
launch文件语法:

<launch> launch文件中的根元素采用<llaunch>标签定义

启动节点
<node pkg=“package-name” type=“executable-name” name=“node-name”>

  • pkg :节点所在的功能包名称
  • type:节点的可执行文件名称
  • name:节点运行时的名称
  • output:设置日志的输出目标
  • respawn、required、ns、args
参数设置
<lparam> 和 <rosparam>

设置ROS系统运行中的参数,存储在参数服务器中。
<lparam name=“output_frame” value=“odom” />

  • name:参数名
  • value:参数值

加载参数文件中的多个参数:
<rosparam file=“params.yaml” command=“load” ns=“params” />

<arg>

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)" />

重映射
<remap>

重映射ROS计算图资源的命名
<remap from=“/turtlebotcmd_vel” to="/cmd_vel" />

  • from:原命名
  • to:映射之后的命名
嵌套
<include>

包含其他launch文件,类似C语言中的头文件包含。
<include file="$(dirname)/other.launch" />

  • file:包含的其他launch文件路径

更多标签参考:http://wiki.ros.org/roslaunch/XML

使用 launch 标签启动两个节点。

代码实现话题消息的定义:
https://blog.csdn.net/qq_44989881/article/details/118574750

创建 learning_launch 工程包

cd ~/catkin_ws/src
catkin_create_pkg learning_launch

在这里插入图片描述

cd ~/catkin_ws/src/learning_launch

创建 launch 文件夹

mkdir launch

在这里插入图片描述

cd launch

创建 simple.launch 文件

touch 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>

配置完成后,对工程目录进行编译

cd ~/catkin_ws
catkin_make

在这里插入图片描述

启动 launch 文件
指令格式:

roslaunch [工程包名] [launch文件名]

例如:

roslaunch learning_launch simple.launch

在这里插入图片描述


使用 launch 标签读取 yaml 文件的配置
cd ~/catkin_ws/src/learning_launch/launch
touch turtlesim_parameter_config.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>
cd ~/catkin_ws/src/learning_launch
mkdir config
cd config
touch param.yaml

在 param.yaml 文件中添加以下代码:

A: 123
B: "hello"

group:
  C: 456
  D: "hello"
roslaunch learning_launch turtlesim_parameter_config.launch
rosparam list

在这里插入图片描述


tf坐标系广播与监听,用launch文件配置启动

用代码创建tf坐标系广播与监听的方法: https://blog.csdn.net/qq_44989881/article/details/118603453

cd ~/catkin_ws/src/learning_launch/launch
touch start_tf_demo_c++.launch

在 start_tf_demo_c++.launch 文件中添加以下代码:

 <launch>

    <!-- 海龟仿真器-->
    <node pkg="turtlesim" type="turtlesim_node" name="sim"/>

	<!-- 键盘控制-->
    <node pkg="turtlesim" type="turtle_teleop_key" name="teleop" output="screen"/>

	<!-- 两只海龟的tf广播-->
    <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" />

	<!-- 监听tf广播,并且控制turtle2移动-->
    <node pkg="learning_tf" type="turtle_tf_listener" name="listener" />

  </launch>

启动节点

roslaunch learning_launch start_tf_demo_c++.launch

在这里插入图片描述

touch start_tf_demo_py.launch

在 start_tf_demo_py.launch 文件中添加以下代码:

<launch>

	<!-- 海龟仿真器-->
	<node pkg="turtlesim" type="turtlesim_node" name="sim"/>
	
	<!-- 键盘控制-->
	<node pkg="turtlesim" type="turtle_teleop_key" name="teleop" output="screen"/>

	<!-- 两只海龟的tf广播-->
	<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>

	<!-- 监听tf广播,并且控制turtle2移动-->
    <node pkg="learning_tf" type="turtle_tf_listener.py" name="listener" />

</launch>

启动节点

roslaunch learning_launch start_tf_demo_py.launch

可直接在该窗口控制海龟的移动。
在这里插入图片描述


使用 launch 标签给话题名称 起个别名
cd ~/catkin_ws/src/learning_launch/launch
touch turtlesim_remap.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>

启动节点

roslaunch learning_launch turtlesim_remap.launch

<remap from="/turtle1/cmd_vel" to="/cmd_vel"/>
注:用 /cmd_vel 替换了 /turtle1/cmd_vel,相当于起了个别名。
在这里插入图片描述

踩坑

RLException: [start_turtle.launch] is neither a launch file in package [hello_world_c] nor is [hello_world_c] a launch file name
The traceback for the exception was written to the log file

解决方法:
错误原因:环境变量设置有问题,重新添加环境变量后再执行roslaunch

source ~/工作空间名/devel/setup.bash
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值