当前位置:网站首页>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 .

原网站

版权声明
本文为[Fat fat is the sun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010559536269.html