launch文件:通过XML文件实现多节点的配置和启动(可自启动ROS Mater) launch文件语法   在这里插入图片描述   launch文件中的根元素采用标签定义 启动节点 • pkg:节点所在的功能包名称 • type:节点的可执行文件名称 • name:节点运行时的名称 • output 、respawn 、required 、ns 、args 参数设置 / 设置ROS系统运行中的参数,存储在参数服务器中。   • name:参数名 • value:参数值 加载参数文件中的多个参数:   launch文件内部的局部变量,仅限于launch文件使用 • name:参数名 • value:参数值 调用:   重映射 重映射ROS计算图资源的命名 • from:原命名 • to:映射之后的命名   嵌套 包含其他launch文件,类似C语言中的头文件包含 •file:包含的其他launch文件路径   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>
  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>
  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 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_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>
  创建功能包  
cd catkin_ws/src
catkin_create_pkg learning_launch
  创建launch文件夹
cd catkin_ws/src/learning_luanch
mkdir -p luanch
  将代码放在launch文件夹下 编译并运行launch文件 格式:roslaunch 功能包名 launch文件名  
cd ..
catkin_make
roslaunch learning_launch simple.launch
 
属性 	             属性作用
name="NODE_NAME" 	为节点指派名称,这将会覆盖掉ros::init()定义的node_name
pkg="PACKAGE_NAME" 	节点所在的包名
type="FILE_NAME" 	执行文件的名称如果是用Python编写的就填写xxx.py,如果是cpp就写编译生成的可执行文件名
output="screen" 	终端输出转储在当前的控制台上,而不是在日志文件中
respawn="true" 	    当roslaunch启动完所有该启动的节点之后,会监测每一个节点,保证它们正常的运行状态。对于任意节点,当它终止时,roslaunch 会将该节点重启
required="true" 	当被此属性标记的节点终止时,roslaunch会将其他的节点一并终止。注意此属性不可以与respawn="true"一起描述同一个节点
launch-prefix = "command-prefix" 	相当于在执行启动命令时加上一段命令前缀
ns = "NAME_SPACE" 	这个属性可以让你在自定义的命名空间里运行节点
    介绍常用的节点属性 1 节点重生属性(respawn) 当roslaunch开启所有nodes后,roslaunch会监视每个node,记录那些仍然活动的nodes。对于每个node,当其终止后,我们可以要求roslaunch重启该node,通过使用respawn属性。   2.必要节点属性(required) 当一个节点被声明为必要节点即 required="true"终止的时候,roslaunch 会终止所有其他活跃节点并退出。比如在依赖激光雷达的机器人导航中,若激光雷达节点意外退出时,roslaunch将会终止其他节点然后退出。   3 设定节点所需的参数(param) 节点中的一些参数可以直接在launch文件中设定,这样就没有必要修改源码和编译了,每次只要修改一下 launch文件就可以直接修改节点运行的参数。标签定义了在参数服务器上设置的参数,放在标签中,在这种情况下,参数被当成了私有参数。  
<launch>
<node pkg="test" type="test_node" name="test_node" output="screen" />
<node pkg="my_pkg" type="my_pkg_node" name="my_pkg_node" output="screen" >
	<param name="name" type="str" value="corvin"/>
	<param name="age" type="int" value="20"/>
	<param name="handsome" type="bool" value="true"/>
	<param name="salary" type="double" value="1234.34"/>
<node>
</launch>
  修改my_pkg.cpp  
using namespace std;
string my_name="";
int my_age=0;
bool my_handsome=false;
double my_salary=0.0;
ros::param::get("~name",my_name);
ros::param::get("~age",my_age);
ros::param::get("~handsome",my_handsome);
ros::param::get("~salary",my_salary);
ROS_INFO("get param,name:%s,age:%d,ishandsome:%d,salary:%f",my_name.c_str(),my_age,my_handsome,my_salary);
  包含另一个launch文件  
<include file="$(find third_pkg)/launch/third_pkg.launch"/>
  编写launch时的注意事项 1 roslaunch 不提供节点开始的顺序保证。这是特意的:没有办法知道哪个节点完全初始化 了,所以启动代码必须在启动顺序上鲁棒性比较强。这个行为体现了ROS哲学:每一个 节点与其他的节点都应该尽可能的独立、不相关,节点间耦合性尽可能低。   2 在开始任何一个节点前,roslaunch 将会确定 roscore是否已经在运行,如果没有则自动 启动它,因此在使用roslaunch启动节点时不用再提前启动roscore了。   3 大多数 ROS 节点在启动时连接到master节点管理器上,如果没有在launch中配置该节点 respawn属性为true运行中若连接中断,则不会尝试重新连接。因此如果 roscore被终止, 当前运行的其他节点将无法建立新的连接,即使稍后重启 roscore 也无济于事。