先附上两端代码来作为我们的示例进行介绍:
<launch>
<!--need to use start_turtle.launch for many times and in different files-->
<include file="$(find launch_1)/launch/start_turtle.launch" />
</launch>
<launch>
<!--<launch deprecated>-->
<!-- give up this launch file-->
<group ns="first">
<param name="param_A" type="int" value="100" />
<rosparam command="load" file="$(find launch_1)/launch/params.yaml" />
<node pkg ="turtlesim" type="turtlesim_node" name="my_turtle" output="screen">
<remap from="/turtle1/cmd_vel" to="/cmd_vel" />
<param name="param_B" type="double" value="3.14" />
<rosparam command="dump" file="$(find launch_1)/launch/params_out.yaml" />
</node>
<node pkg ="turtlesim" type="turtle_teleop_key" name="my_key" output="screen"/>
</group>
<!--With keyword "group" we can start the nodes with same name and they won't cover each other.(If without "group" the node started forward will be covered.) -->
<group ns="second">
<param name="param_A" type="int" value="100" />
<rosparam command="load" file="$(find launch_1)/launch/params.yaml" />
<node pkg ="turtlesim" type="turtlesim_node" name="my_turtle" output="screen">
<remap from="/turtle1/cmd_vel" to="/cmd_vel" />
<param name="param_B" type="double" value="3.14" />
<rosparam command="dump" file="$(find launch_1)/launch/params_out.yaml" />
</node>
<node pkg ="turtlesim" type="turtle_teleop_key" name="my_key" output="screen"/>
</group>
<!-- when we wanna set several parameters with same value, then we use arg -->
<arg name="car_length" default="0.55" />
<param name="A" value="$(arg car_length)" />
<param name="B" value="$(arg car_length)" />
<param name="C" value="$(arg car_length)" />
</launch>
接下来我们将逐个介绍这些属性:
1.include:包含我们需要的launch1文件,格式为<include file="$(find pkg_name)/launch/XXX.launch" /> (XXX.launch 为需要包含的launch文件名)。如果我们需要多次调用另外launch文件中的代码,这个功能会很好用。
2.remap:这个是修改相应的文件名称的,比如我们的teleop_twist_keyboard.py(如果显示文件不存在,则输入以下命令来下载相应文件:sudo apt-get install ros-melodic-teleop-twist-keyboard(melodic改为相应的版本))我们会发现没有办法通过这个文件进行键盘控制乌龟,因为这个文件是通过/cmd_vel去订阅命令的,而我们的乌龟是通过/turtle1_cmd_vel发布,则我们就通过这个命令进行名字修改,然后进行发布,则程序就能顺利运行。效果如图:
3.param 我们可以通过这个在服务器内添加参数,我们可以在launch下node外添加,也可以在node下添加,区别是这个参数的从属,我们使用rosparam get时,我们对于param_A,我们直接写rosparam get /param_A,而我们寻找param_B时,我们需要写出他所在的文件处:rosparam get /my_turtle/param_B。
4.rosparam:这个属性是专门针对于.yaml文件的,比如我们可以先写一个params.yaml文件,在文件中写入“bg_B= 100(\n) bg_R= 100 (\n) bg_G= 100”然后我们使用load命令,加载入参数服务器,如果我们需要写入,就用"dump"文件。
5.group这个属性是对代码进行分组,作用是之前我们加载同名节点时候,后面的会覆盖前面的节点,但分组之后就不会发生这种情况。
6.arg :我们需要多次将同一键值写入多个参数时,我们就利用这个,格式为:1.先定义<arg name="XXX" default="YYY" />;2.写入:<param name="XYX" value="$(arg XXX)" />