当前位置:网站首页>18、 ROS topic name setting
18、 ROS topic name setting
2022-07-28 14:53:00 【Rock magnon】
List of articles
1. ROS Topic name remapping
1.1 brief introduction
stay ROS in , Sometimes the topic names are duplicated , Lead to information release , Reception mismatch ; Sometimes nodes with different topic names need to communicate with each other . At this time , We need to use the method of topic name remapping to solve the problem
1.2 Implementation method
stay ROS Operating system , Provides ros-noetic-teleop-twist-keyboard Keyboard control package , Topic name is cmd_vel, stay turtlesim Function pack , Its keyboard control topic is /turtle1/cmd_vel, Through topic remapping , To realize the communication between the two
1.2.1 rosrun Set topic remapping
- Format : rosrun Function pack The node name Topic name := New node roll call
1.2.1.1 Scheme 1
- The goal is : /cmd_vel Mapping to /turtle1/cmd_vel
- Code implementation :
bash1: Start the tortoise node rosrun turtlesim turtlesim_node bash2: Start keyboard control node , And map rosrun teleop_twist_keyboard teleop_twist_keyboard.py /cmd_vel:=/turtle1/cmd_vel
1.2.1.2 Option two
- The goal is :/turtle1/cmd_vel Mapping to /cmd_vel
- Code implementation :
bash1: Start keyboard control node rosrun teleop_twist_keyboard teleop_twist_keyboard.py bash2: Start the tortoise node , And carry out topic mapping rosrun turtlesim turtlesim_node /turtle1/cmd_vel:=/cmd_vel
1.2.2 launch File set topic remapping
- Format :
<node pkg="" type="" name=""> <remap from=" The original topic " to=" New topics "/> </node>
1.2.2.1 Scheme 1
- The goal is : take teleop_twist_keyboard The topic of the node is set to /turtle1/cmd_vel
- Code implementation :
<launch> <node pkg="turtlesim" type="turtlesim_node" name="t1" /> <node pkg="teleop_twist_keyboard" type="teleop_twist_keyboard.py" name="t2"> <remap from="/cmd_vel" to="/turtle1/cmd_vel" /> </node> </launch>
1.2.2.2 Option two
- The goal is : take turtlesim The topic name of the node is set to /cmd_vel
- Code implementation
<launch> <node pkg="teleop_twist_keyboard" type="teleop_twist_keyboard.py" name="t1" /> <node pkg="turtlesim" type="turtlesim_node" name="t2"> <remap from="/turtle1/cmd_vel" to="/cmd_vel" /> </node> </launch>
1.2.3 Code and set the topic name
- The name and namespace of the topic , The name of the node is related , According to the relationship , Topic names can be roughly divided into three categories ( It can be understood that the prefixes added before the topic are different ):
- overall situation : Refer to the whole ROS System , Level with namespace
- relative : Reference namespace , Level with node name
- private : Reference node name , Is the child of the node
1.2.3.1 C++ Realization
- Global topics
- Format : has / start
- Code implementation :
#include"ros/ros.h" #include"std_msgs/String.h" int main(int argc,char *argv[]){ ros::init(argc,argv,"topic_name"); ros::NodeHandle nh; // Global topics : Add / ros::Publisher pub = nh.advertise<std_msgs::String>("/chtter",1000); // Set your own namespace ros::Publisher pub1 = nh.advertise<std_msgs::String>("/xxx/chtter",1000); while (ros::ok()) { } return 0; } - Running results :
bash1: Start node rosrun rename_01 topic_name bash2: Check the list of topics rostopic list /chtter // Global topics /rosout /rosout_agg /xxx/chtter // Added its own namespace
- Relative topics
- Format : Not / The first topic
- Code implementation :
#include"ros/ros.h" #include"std_msgs/String.h" int main(int argc,char *argv[]){ ros::init(argc,argv,"topic_name"); ros::NodeHandle nh; // Global topics : Add / ros::Publisher pub = nh.advertise<std_msgs::String>("chatter",1000); // Set your own namespace ros::Publisher pub1 = nh.advertise<std_msgs::String>("yyy/chatter",1000); while (ros::ok()) { } return 0; } - Running results :
bash1: Start node , And add the namespace rosrun rename_01 topic_name __ns:=xxx bash2: Check the topic rostopic list /rosout /rosout_agg /xxx/chatter /xxx/yyy/chatter
- Private topics
- Format : ros::NodeHandle nh(“~”);
- Code implementation :
#include"ros/ros.h" #include"std_msgs/String.h" int main(int argc,char *argv[]){ ros::init(argc,argv,"topic_name"); // Private topics , And NH Use a combination of , Add after ~ ros::NodeHandle nh("~"); ros::Publisher pub = nh.advertise<std_msgs::String>("chatter",1000); // If the / start , Is the overall topic ros::Publisher pub1 = nh.advertise<std_msgs::String>("/chatter",1000); while (ros::ok()) { } return 0; } - Running results :
bash1: Start node rosrun rename_01 topic_name __ns:=xxx bash2: View the list of nodes rostopic list /chatter /rosout /rosout_agg /xxx/topic_name/chatter
1.2.3.2 Python Realization
- Code implementation :
#! /usr/bin/env python from ast import Str import rospy from std_msgs.msg import String if __name__ == "__main__": rospy.init_node("topic_name_p") # 1. overall situation pub1 = rospy.Publisher("/chatter1",String,queue_size=10) # Set your own namespace , Not subject to __ns:=xxx Influence pub2 = rospy.Publisher("/zzz/chatter2",String,queue_size=10) # 2. relative pub3 = rospy.Publisher("chatter3",String,queue_size=10) # Set your own namespace pub4 = rospy.Publisher("yyy/chatter4",String,queue_size=10) # 3. private pub5 = rospy.Publisher("~chatter5",String,queue_size=10) # Set your own namespace pub6 = rospy.Publisher("~ns/chatter6",String,queue_size=10) while not rospy.is_shutdown(): pass - Running results :
bash1: Start node rosrun rename_01 topic_name_p.py __ns:=xxx bash2: View the list of nodes rostopic list /chatter1 /rosout /rosout_agg /xxx/chatter3 /xxx/topic_name_p/chatter5 /xxx/topic_name_p/ns/chatter6 /xxx/yyy/chatter4 /zzz/chatter2
边栏推荐
猜你喜欢

树莓派基础 | 总结记录树莓派学习过程中的一些操作

Redis redis use in jedis

OKR与GRAD

Penguin side: why not recommend using select *?

企鹅一面:为什么不建议使用SELECT * ?

@DS('slave') 多数据源兼容事务问题解决方案

The 35 required questions in MySQL interview are illustrated, which is too easy to understand

基于 MinIO 对象存储保障 Rancher 数据

爆肝整理JVM十大模块知识点总结,不信你还不懂

Redis persistence
随机推荐
Redis-配置文件讲解
Qtableview in QT sets three methods of paging display [easy to understand]
面试官:ThreadLocal使用场景有哪些?内存泄露问题如何避免?
Redis-Redis在Jedis中的使用
[thread safety] what risks may multithreading bring?
VTK notes - picker picker summary
(function(global,factory){
Focus on differentiated product design, intelligent technology efficiency improvement and literacy education around new citizen Finance
Multi merchant mall system function disassembly lecture 17 - platform side order list
Create a table under swiftui with table
Why is it reverse to convert from other formats to BMP
JS instantiation method
String转为long 类型报错原因:要转为long必须是int、double、float型[通俗易懂]
Animation mechanism of swiftui
Log management platform of infrastructure and nail & email alarm notification
Vtkcellpicker picking triangular patches
MITK create module
Simple data analysis using Weka and excel
Redis configuration file explanation
468 product planning and promotion plan (150 copies)