launch文件标签说明
turtle.launch
<launch>
<!--ns表示在指定命名空间运行节点(可以理解为改rostopic list中的节点名称),交互的节点间要在同一ns下,下面这俩必须在同ns下-->
<!-- pkg包名 type节点名 name自定义名 output输出方式 respawn起死回生 respawn_delay起死回生所需要的时间,默认0 -->
<node pkg="turtlesim" type="turtlesim_node" name="turtletest" output="screen" respawn="true" respawn_delay="3" ns="turtle"/>
<!--required是必需的,死后全死-->
<node pkg="turtlesim" type="turtle_teleop_key" name="mycontrol" required="true" output="screen" ns="turtle"/>
</launch>
turtlekeyboard.launch
<launch>
<!--rosrun teleop_twist_keyboard teleop_twist_keyboard.py启动时topic是/cmd_vel
想要转化话题名称就在后面添加参数 /cmd_vel:=/turtle1/cmd_vel 就将topic从/cmd_vel转化成了/turtle1/cmd_vel-->
<rosparam command="load" file="$(find launchpackage)/launch/rosparamtest.yaml"/>
<!--remap标签teleop_twist_keyboard teleop_twist_keyboard.py节点话题为cmd_vel,
所以为了让两种方式都能控制turtletest,就将下面的两个节点话题都变成cmd_vel-->
<!--ns参数限制node节点名称和话题两个地方
就是在ns=turtle name=turtletest 下的param为:/turtle/turtletest/background_r
下的topic 位置是/turtle/turtle1/pose 移动话题是:/turtle/turtle1/cmd_vel <=> /cmd_vel remap转换了-->
<node pkg="turtlesim" type="turtlesim_node" name="turtletest_1" output="screen" respawn="true" respawn_delay="3" ns="turtle">
<remap from="/turtle/turtle1/cmd_vel" to="/cmd_vel"/>
<param name="canshu" type="int" value="10"/><!--rosparam list 存在 /turtletest/canshu-->
</node>
<node pkg="turtlesim" type="turtle_teleop_key" name="mycontrol" required="true" output="screen" ns="turtle">
<remap from="/turtle/turtle1/cmd_vel" to="/cmd_vel"/>
</node>
<node pkg="turtlesim" type="turtlesim_node" name="turtletest_2" output="screen" respawn="true" respawn_delay="3" ns="turtle">
<remap from="/turtle/turtle1/cmd_vel" to="/cmd_vel"/>
</node>
<!--执行顺序可能在设置rosparam前运行了,所以不行,一般令写launch文件(rosparamtest.launch)-->
<!--<rosparam command="dump" file="$(find launchpackage)/launch/rosparamtestoutput.yaml"/> -->
<group ns="grouptest"><!--该标签下的所有话题和参数均在grouptest下-->
<node pkg="turtlesim" type="turtlesim_node" name="groupturtle" />
</group>
<!--范型参数-->
<arg name="argcanshu" default="0.2"/>
<param name="thisarg" value="$(arg argcanshu)"/>
</launch>
rosparmtest.launch
<!--提前运行turtlekeyboard.launch-->
<launch>
<!--保存和删除 rosparam标签只有三个参数 保存:dump 删除:delete 加载:load
添加是使用param标签 详见turtlekeyboard.launch文件-->
<rosparam command="dump" file="$(find launchpackage)/launch/rosparamtestoutput.yaml"/>
<rosparam command="delete" param="/turtle/turtletest/background_r"/>
</launch>
turtlecopyfile.launch
<launch>
<!--file="$(find 包名)/launch/launch文件名"-->
<include file="$(find launchpackage)/launch/turtle.launch" />
</launch>
终端启动时设置参数
话题权限
rosrun设置
launch文件设置参数
通过 launch 文件设置参数的方式,可以在 node 标签外,或 node 标签中通过 param 或 rosparam 来设置参数。在 node 标签外设置的参数是全局性质的,参考的是 / ,在 node 标签中设置的参数是私有性质的,参考的是 /命名空间/节点名称(ns/name)。
C++程序设置
#include "ros/ros.h"
int main(int argc, char *argv[])
{
ros::param::set("/set_A", 100); //全局,和命名空间以及节点名称无关
ros::param::set("set_B", 100); //相对,参考命名空间
ros::param::set("~set_C", 100); //私有,参考命名空间与节点名称
ros::NodeHandle nh;
nh.setParam("/nh_A", 100); //全局,和命名空间以及节点名称无关
nh.setParam("nh_B", 100); //相对,参考命名空间
ros::NodeHandle nh_private("~");
nh_private.setParam("nh_C", 100); //私有,参考命名空间与节点名称
/*
运行时,假设设置的 namespace 为 xxx,节点名称为 yyy,使用 rosparam list 查看:
/set_A
/xxx/set_B
/xxx/yyy/set_C
/nh_A
/xxx/nh_B
/xxx/yyy/nh_C
*/
return 0;
}