当前位置:网站首页>[ROS] handwritten message publisher and subscriber
[ROS] handwritten message publisher and subscriber
2022-07-20 23:35:00 【qq_ twenty-nine million seven hundred and fifty thousand four h】
This article records handwriting ROS The process of message publisher and subscriber ,(C++ edition )
development environment :VSCode Ubuntu18.04
Pre development preparation
1 Import the library files required for development
Need to prepare for development ROS Package library
2 Open established ros Development folder , For the establishment process, see Workspace creation and Function package creation
And then I found out VSCode Automatically import the created folder , below src In the catalog test_tutorials For feature packs created in the workspace , stay /test_tutorials/src Under the new talker.cpp
Suppose the environment is ready , Next is the process of development .
Handwriting publisher
1 The code is as follows
#include "ros/ros.h"
#include "std_msgs/String.h"
#include <sstream>
// At a certain frequency (10hz) Release information to fixed topics (hello world + count)
int main(int argc,char **argv){
// initialization ros The name of the node only
ros::init(argc,argv,"talker");
ros::NodeHandle n;
// Declare publisher With std_msgs::String Form orientation chatter Topic name 1000 Cache for topics
ros::Publisher chatter_pub=n.advertise<std_msgs::String>("chatter",1000);
// The cycle frequency is 10hz
ros::Rate loop_rate(10);
int count=0;
// ros::ok() If the node is always on true otherwise by false
while(ros::ok()){
std_msgs::String msg;
std::stringstream ss;
ss<<"hello world "<<count;
msg.data=ss.str(); // Get message data
ROS_INFO("%s",msg.data.c_str());// Print message data
chatter_pub.publish(msg);// Post topic news
ros::spinOnce();//ros Callback function General plus
loop_rate.sleep();// Call the frequency function rest 10hz
++count; // Counter increment
}
return 0 ;
}
2 To write CmakeLists.txt Content
Pay attention to the open CmakeLists.txt Directory structure of
Then add the following
# Import and stock in documents
include_directories(include ${catkin_INCLUDE_DIRS})
# Generate execution file
add_executable(talker src/talker.cpp)
# The generated talker File link to catkin_LIBRARIES
target_link_libraries(talker ${catkin_LIBRARIES})
3 compile
Open the terminal and switch to the workspace folder , Input
catkin_make
Compile
Compile successfully
4 Run to check whether the publisher is successful
Open a terminal start-up roscore
roscore
Open another terminal , Input
rosrun test_tutorials talker // test_tutorials Name the function package ,talker For Publisher
Open another terminal Look at the chart
Check if... Exists first chatter topic of conversation
rostopic list
Just open roscore
open roscore and talker node
Look at the chart
rosrun rqt_graph rqt_graph
It means that the handwriting publisher is successful .
Handwriting subscriber
1 Code
#include "ros/ros.h"
#include "std_msgs/String.h"
//#include <sstream>
// The callback function parameters are consistent with the publisher information type
void chatterCallback(const std_msgs::String::ConstPtr& msg){
// Print the information heard by the listener
ROS_INFO("I hear [%s]",msg->data.c_str());
}
int main(int argc,char **argv){
// initialization ros node The name is unique
ros::init(argc,argv,"listener");
// Statement ros Handle
ros::NodeHandle n;
// Declare a subscriber 1000 Subscriber cache And call the callback function
ros::Subscriber sub=n.subscribe("chatter",1000,chatterCallback);
// Blocking callback Be similar to
/* while(ros::ok()){ros::spinOnce();} */
ros::spin();
return 0;
}
2 To write CmakeLists.txt
# Import and stock in documents
include_directories(include ${catkin_INCLUDE_DIRS})
# Generate execution file
add_executable(talker src/talker.cpp)
# The generated talker File link to catkin_LIBRARIES
target_link_libraries(talker ${catkin_LIBRARIES})
# Generate execution file
add_executable(listener src/listener.cpp)
# The generated talker File link to catkin_LIBRARIES
target_link_libraries(listener ${catkin_LIBRARIES})
3 compile catkin_make
4 And commissioning talker equally , open listener
rosrun test_tutorials listener
This is Publisher talker
listener
Calculation chart
The above is the process of writing simple message publishers and subscribers , I hope it will be of some help to you .
边栏推荐
- 数组把key相同值,合并value值(整理)
- 【Flink】转换算子 map
- [cloud based co creation] Huawei cloud helps accelerate the construction of enterprise data assets and data governance production lines
- [cloud native] IVX low code development was introduced into Tencent map and previewed online
- 自定义View注意事项
- MySQL5.7 参数详解
- 300.最长递增子序列
- display属性
- 【Flink】富函数
- 如何手写一个js工具库?同时发布到npm上
猜你喜欢
A combination idea of developing applet +app) with fluent
【Flink】提交jar包到Flink集群并运行
cnpm安装
简单斐波那契
Localization Distillation for Dense Object Detection(用于密集目标检测的定位蒸馏)CVPR2022
GIS技术在医疗行业的应用:利用切片地图发布技术解决dmetrix数字病理切片在线浏览
Traditional architecture of enterprise digital transformation to medium platform micro service architecture
深度学习从零构建一个模型的方法
300.最长递增子序列
ECCV 2022 | semantic novelty detection based on relational reasoning
随机推荐
Success factors of software R & D effectiveness measurement
【大规模训练】transformer 中的张量模型并行
JVM memory model
教你使用CANN将照片一键转换成卡通风格
深度学习从零构建一个模型的方法
【ROS】话题基础使用
ECCV 2022 | 基于关系推理的语义新颖性检测
Analysis of QR code scanning and login in IM instant messaging development
力扣第三题
.NET6之MiniAPI(四):配置
Offline installation: how to build a secure enterprise class harbor service? The content is too detailed.
华为无线设备配置同一业务VLAN的AP间快速漫游
The listing of coinbase will open a bull market, rising in the second half 2021-04-03
力扣刷题第六天
【1】Harbor在线安装:3分钟体验Harbor
[cloud native] IVX low code development was introduced into Tencent map and previewed online
Rust开发——Trait的概念与应用示例
云图说丨数字资产链:您的数字资产产权保护神
144. 二叉树的前序遍历
Stock account opening fee and account opening commission are the lowest, the most economical and cost-effective. Is online account opening safe