当前位置:网站首页>Node, topic, parameter renaming and global, relative and private namespaces in ROS (example + code)
Node, topic, parameter renaming and global, relative and private namespaces in ROS (example + code)
2022-06-12 07:05:00 【Fat fat is the sun】
ROS The namespace in
We first need to understand the meaning of each part of the path :
① Take topic as an example , Three level path :

② Take nodes as an example , There are two levels of paths :

③ Take the parameter as an example , There are three levels of paths :

ROS There are three types of namespaces in : overall situation 、 relative 、 private . In fact, the namespace is the prefix of the name , The prefix represents the path , The storage location can be relative to the root directory 、 node 、 The namespace of the node handle is divided by these three :

private 、 overall situation 、 The basis for judging relative namespaces :
Private namespace | “/node_name” The node name is on the outermost side |
Global namespace | There is only one “/” The root directory is on the outermost side |
Relative namespace | “/namespace” The node handle namespace is on the outermost side |
Topic namespace settings
1. cpp Source file settings :
The topic should first be master Register in , So the function to register topic information is called by the node handle , So relative / The setting of the private namespace is done by the node handle . The relative and private namespaces are set as follows :
// Global namespace :topic=/chatter
ros::Publisher pub = nh.advertise<std_msgs::String>("/chatter",10);
// Relative namespace :topic=/namespace/chatter
ros::NodeHandle nh("dr");
ros::Publisher pub = nh.advertise<std_msgs::String>("chatter",10);
// Relative namespace :topic= /node_name/namespace/ chatter
ros::NodeHandle nh("~dr");
ros::Publisher pub = nh.advertise<std_msgs::String>("chatter",10);
// Private namespace :topic=/node_name/chatter
ros::NodeHandle nh("~");
ros::Publisher pub = nh.advertise<std_msgs::String>("chatter",10); When our topic Set to ”/chatter”( With slashes ) And the global priority is the highest , therefore topic Not subject to namspace and node_name Influence .
2. rosrun Command line settings :
besides , You can also use rosrun To the .cpp Add relative namespaces to all attributes in the file , The specific form is :rosrun package_name cpp_name __ns:="hello", The running results are as follows :

All topics in the file of this node 、 All escalation parameters 、 All node names will be added with /hello/ The prefix of .
Be careful :__ns:=”global_namespace” It's double underline .
3. Modify the environment variables of the project ROS_NAMESPACE( modify global_namespace)

In limine ROS_NAMESPACE The content of is empty , We will ROS_NAMESPACE Set to HELLO after , Running below cpp Source file , The results are shown in the following figure :

Rename the topic
The renaming of the topic is also called “ Topic mapping (remapping)“, It is proposed to prevent the following situations from happening :
1. Situation 1 :

subscriber C What is needed is the publisher A Of data1 Not the publisher B Of data2, So we need to put the publisher A/ subscriber C Between topic It maps to topic_a, Make subscribers C You can also receive publishers A Of data1, Without changing the publisher B Of topic:

2. Situation two :

We need to topic It maps to topic_a, Make publishers A With subscribers C Through the same topic Complete the connection :

Topic remapping can be done through the following two operations :
① stay rosrun Change in topic name
Form the following :rosrun package_name cpp_name topic_name:="topic_new_name", The results are shown below :

Enter the command :rosrun demo04 testServer /server/dr/chatter:="A“
② stay launch Use... In the document <remap> label
<launch>
<remap from="/server/remap/chatter" to="A"/>
</launch> Launch For the format of document preparation, see :ROS Document organization form in :launch file + Meta function pack ( Example + Code + Argument parsing )_ Super powerful blog -CSDN Blog
https://blog.csdn.net/weixin_45590473/article/details/122647788
Node namespace settings
① Use launch In file <node> Labeled ns attribute
<launch>
<node name="node_A" type="testServer" pkg="demo04" ns="hello"/>
</launch> The results are shown below :

If you use ns The path from the child tag is a relative namespace , If not used ns The path from the child tag is the global namespace .
② rosrun command
Concrete form :rosrun package_name cpp_name __ns:="hello", The running results are as follows :

All topics in the file of this node 、 All escalation parameters 、 All node names will be added with /hello/ The prefix of .
Be careful :__ns:=”global_namespace” It's double underline , The resulting path is a relative namespace .
③ Use cpp Source file settings
<launch>
<!-- overall situation :/node_A -->
<node name="node_A" type="testServer" pkg="demo04"/>
<!-- relative :/hello/node_A -->
<node name="node_A" type="testServer" pkg="demo04" ns="hello"/>
</launch> Be careful : The namespace of a node has two layers , Respectively “ relative ” and “ overall situation ”.
④ Modify the environment variables of the project ROS_NAMESPACE( modify global_namespace)

In limine ROS_NAMESPACE The content of is empty , We will ROS_NAMESPACE Set to HELLO after , Running below cpp Source file , The results are shown in the following figure :

Node rename
① Use launch File for node name remapping
<launch>
<node name="node_A" type="testServer" pkg="demo04"/>
</launch> The results are shown below :

Be careful :<node> In the label type It refers to the name of the executable file where the node is located . We found that the name of the original node did not disappear but added a new name , Therefore, this operation is a node name remapping rather than a node renaming .
② Use rosrun Make changes :
Command format :rosrun package_name cpp_name node_name:="node_new_name", The running results are as follows :

The order is :rosrun demo04 testServer /server:="node_B", There are results to know “ This operation is the node name rename ”. If we still feel too much trouble ( The above command must know the node name OK), Then we can also use the following command format :rosrun package_name cpp_name __name:="node_new_name"
Be careful : Here is also a double underline !
Parameter namespace settings
There are three ways to report parameters : Command line 、 Source file 、launch file .
① rosrun Add additional parameters after
Format :rosrun package_name cpp_name _param:=value
among , The parameter name must be preceded by _ An underline , We report parameters by analogy 、 Topic rename 、 The node is renamed 、 Add the namespace corresponding to four different operations rosrun The form of appended parameters :
// Add relative namespaces to all parameters
rosrun package_name cpp_name __ns:="hello"
// Change the topic name
rosrun package_name cpp_name topic_name:="topic_new_name"
// Change node name ( Two forms )
rosrun package_name cpp_name node_name:="node_new_name"
rosrun package_name cpp_name __name:="node_new_name"
// Reporting parameters
rosrun package_name cpp_name _ParamName:=value We use rosrun Command to add parameters to the parameter server , This parameter is actually a private parameter belonging to the node :

command :rosrun demo04 testServer _A:=2
We can see from the result that : Parameters A Prefixed , The prefix is the node name server, So we passed rosrun The namespace of the parameter reported to the parameter server is private .
② Source file
There are two schemes to report parameters to the parameter server through the source file :
1. ros Under the namespace param function
// Private namespace :/node_name/param_A
ros::param::set("~param_A","A");
// Global namespace :/param_B
ros::param::set("/param_B","B");
// Relative namespace :/param_C
ros::param::set("param_C","C"); The running results are as follows :

Relative namespaces are relative to global_namespace Speaking of , We mentioned earlier : have access to rosrun Command line mode settings global_namespace:

command :rosrun demo04 testServer __ns:="hello", We see that the global namespace is /param_name, Whatever you set will not change , Change is only private / Relative namespace .
2. Node handle setparam Member functions
because setparam yes NodeHandle Member function of , So we just need to change NodeHandle The same changes can be made to the parameters in the same namespace .
// Private namespace :/node_name/Param_D
ros::NodeHandle nh("~");
nh.setParam("Param_D","D");
// Relative namespace :/namespace/Param_E
ros::NodeHandle nh1("dr");
nh1.setParam("Param_E","E");
// Private namespace :/node_name/namespace/Param_F
ros::NodeHandle nh2("~dr");
nh2.setParam("Param_F","F");
// Global namespace :/Param_G
ros::NodeHandle nh3;
nh3.setParam("Param_G","G"); The running results are as follows :
/server/Param_D
/dr/Param_E
/server/dr/Param_F
/Param_G 3. Use launch File settings parameters
<launch>
<!— overall situation :/Param_W -->
<param name="Param_W" type="string" value="W"/>
<!— relative :/hello/Param_J -->
<param name="/hello/Param_J" type="string" value="J"/>
<node name="node_A" type="testServer" pkg="demo04">
<!— private :/node_A/Param_S -->
<param name="Param_S" type="string" value="S"/>
</node>
</launch> 4. Modify the environment variables of the project ROS_NAMESPACE( modify global_namespace)

In limine ROS_NAMESPACE The content of is empty , We will ROS_NAMESPACE Set to HELLO after , Running below cpp Source file , The results are shown in the following figure :

If you want to get rid of this global_namespace, Then you can get rid of it by assignment :

We see that after the null value is assigned ,ROS_NAMESPACE The value of is empty .
边栏推荐
- leetcode:890. Find and replace mode [two dict records set]
- Oracle Database
- Recommend 17 "wheels" to improve development efficiency
- June 9th training day - bit operation
- Torch models trained in higher versions report errors in lower versions
- C language pointer
- esp32 hosted
- 初中学历,从不到3K,到月薪30K+,不设限的人生有多精彩
- 8. 表单标签
- esp32 hosted
猜你喜欢

CL210OpenStack操作的故障排除--章節實驗

lambda 函数完美使用指南

Codeforces Round #793 (Div. 2) A B C

Postman splice replacement parameter loop call interface

Interview intelligence questions

Lambda function perfect use guide
![Leetcode: offer 60 Points of N dice [math + level DP + cumulative contribution]](/img/2b/41bd6a213892062f4c12721b5d4e8d.png)
Leetcode: offer 60 Points of N dice [math + level DP + cumulative contribution]

Elegantly spliced XML

esp32 hosted

Scons编译IMGUI
随机推荐
NOI openjudge 计算2的N次方
库里扛起了勇士对凯尔特人的第四场
Putty installation and use
五、EL 表达式& JSTL 标签库
A journey of database full SQL analysis and audit system performance optimization
数据库全量SQL分析与审计系统性能优化之旅
5 statement
Leetcode: offer 60 Points of N dice [math + level DP + cumulative contribution]
Error mcrypt in php7 version of official encryption and decryption library of enterprise wechat_ module_ Open has no method defined and is discarded by PHP. The solution is to use OpenSSL
8. 表单标签
[image detection] SAR image change detection based on depth difference and pcanet with matlab code
8 IO Library
3 strings, containers, and arrays
PowerDesigner connects to entity database to generate physical model in reverse
企业微信官方 加解密库 PHP7版本报错 mcrypt_module_open 未定义方法 并且被PHP抛弃 解决方法使用 openssl解决
C language pointer
The eighth day of June training - prefix and
Problems encountered in learning go
Zhang Chi: is process a panacea?
五月集训(第28天)——动态规划