当前位置:网站首页>3、 Topic communication: create your own information format
3、 Topic communication: create your own information format
2022-07-26 17:53:00 【Rock magnon】
List of articles
1、 technological process
1. Create... Under the function pack msg Folder , Used to hold .msg file
2. Definition Person.msg file
string name
uint16 age
float64 height
3. Edit profile
- package.xml file
Add two dependent packages , The first is the dependent package at compile time , The second is the runtime dependent package<build_depend>message_generation</build_depend> <exec_depend>message_runtime</exec_depend> - CMakeLists.txt file
To configure msg Source filefind_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs message_generation )
Add dependencies when generating messagesadd_message_files( FILES Person.msg )
Add execution time dependenciesgenerate_messages( DEPENDENCIES std_msgs )catkin_package( # INCLUDE_DIRS include # LIBRARIES public_massage CATKIN_DEPENDS roscpp rospy std_msgs message_runtime # DEPENDS system_lib )
4. compile
After compilation , Some middleware will be generated , These middleware will be used in later programming , Include :
C++ Middleware invoked in : working space /devel/include/ Package name /XXX.h
Python Middleware invoked in : working space /devel/lib/python3/dist-packages/ Package name /msg
2、 Code implementation
0.vscode To configure
For the convenience of code prompt , Need to be in .vscode/c_cpp_properties.json Medium includepath Add head Path to file
{
"configurations": [
{
"browse": {
"databaseFilename": "${workspaceFolder}/.vscode/browse.vc.db",
"limitSymbolsToIncludedHeaders": false
},
"includePath": [
"/opt/ros/noetic/include/**",
"/usr/include/**",
"/media/d102/EPAN/Desktop/code_study_ubuntu/rosdemo_04/devel/include/**"
],
"name": "ROS",
"intelliSenseMode": "gcc-x64",
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu11",
"cppStandard": "c++17"
}
],
"version": 4
}
1.C++ Realization
- Issued by
#include"ros/ros.h"
//public_massage Bit package name , Specific path workspace/devel/include/ Package name
#include"public_massage/Person.h"
int main(int argc,char *argv[]){
setlocale(LC_ALL,"");
//1. initialization ROS node , Name the node
ros::init(argc,argv,"talker");
//2. establish ROS Handle
ros::NodeHandle nh;
//3. Create publisher object
ros::Publisher pub = nh.advertise<public_massage::Person>("chatter",1000);
//4. Organize the release of information
public_massage::Person p;
p.name = "xiaomage";
p.age = 24;
p.height = 1.78;
// Set the release frequency
ros::Rate r(1);
// When the node is alive
while (ros::ok())
{
pub.publish(p);
p.age += 1;
ROS_INFO("my name is:%s,i am %d years old,height is %.2f meter!",p.name.c_str(),p.age,p.height);
r.sleep();
ros::spinOnce();
}
return 0;
}
- Subscriber
#include"ros/ros.h"
#include"public_massage/Person.h"
void doPerson(const public_massage::Person::ConstPtr& person_p){
ROS_INFO(" Subscriber information :%s, %d, %.2f",person_p->name.c_str(),person_p->age,person_p->height);
}
int main(int argc, char *argv[]){
setlocale(LC_ALL,"");
//1. initialization ROS node
ros::init(argc,argv,"listener");
//2. establish ROS Handle
ros::NodeHandle nh;
//3. Create subscription object
ros::Subscriber sub = nh.subscribe<public_massage::Person>("chatter",10,doPerson);
//4. Callback function Person
//5.ros::spin()
ros::spin();
return 0;
}
- To configure CMakeLists.txt file
Be careful to addadd_dependencies
add_executable(person_talker src/person_talker.cpp)
add_executable(person_listener src/person_listener.cpp)
add_dependencies(person_talker ${
PROJECT_NAME}_generate_messages_cpp)
add_dependencies(person_listener ${
PROJECT_NAME}_generate_messages_cpp)
target_link_libraries(person_talker
${
catkin_LIBRARIES}
)
target_link_libraries(person_listener
${
catkin_LIBRARIES}
)
2.Python Realization
- To configure .vscode Medium settings.json file , Add the path of message data type
{
"python.autoComplete.extraPaths": [
"/opt/ros/noetic/lib/python3/dist-packages",
"~/home/d102/anaconda3/envs/python36/lib/python3.6/site-packages/mediapipe/python",
"~/home/d102/anaconda3/envs/python36/lib/python3.6/site-packages",
"/home/d102/catkin_ws/devel/lib/python2.7/dist-packages",
"/media/d102/EPAN/Desktop/code_study_ubuntu/rosdemo_04/devel/lib/python3/dist-packages"
]
}
- Issued by
#! /usr/bin/env python
import rospy
# Introduce message data type
from public_massage.msg import Person
if __name__ == "__main__":
#1. initialization ROS node
rospy.init_node("talker_p")
#2. Create publisher object
pub = rospy.Publisher("chatter_p",Person,queue_size=10)
#3. Organize messages
p = Person()
p.name = "xiaomage"
p.age = 24
p.height = 1.78
#4. Write release logic
rate = rospy.Rate(1)
while not rospy.is_shutdown():
pub.publish(p)
rate.sleep()
rospy.loginfo(" full name :%s, Age :%d, height :%.2f",p.name,p.age,p.height)
- Subscriber
#! /usr/bin/env python
import rospy
from public_massage.msg import Person
def doperson(p):
rospy.loginfo(" Received personal information :%s, %d, %.2f",p.name,p.age,p.height)
if __name__ == "__main__":
rospy.init_node("listener_p")
sub = rospy.Subscriber("chatter_p",Person,doperson,queue_size=10)
rospy.spin()
- Add executable rights
stay scripts Open the terminal under the folder , performchmod +x *.py - To configure CMakeLists.txt file
catkin_install_python(PROGRAMS
scripts/talker_p.py
scripts/listener_p.py
DESTINATION ${
CATKIN_PACKAGE_BIN_DESTINATION}
)
边栏推荐
- [training Day1] spy dispatch
- Spark unified memory partition
- 得不偿失!博士骗领210万元、硕士骗领3万元人才补贴,全被判刑了!
- pip安装模块,报错
- Comparison between agile development and Devops
- JS recursive Fibonacci sequence deep cloning
- 基本的SELECT语句
- ACL实验演示(Huawei路由器设备配置)
- 如何组装一个注册中心?
- 6-19 vulnerability exploitation -nsf to obtain the target password file
猜你喜欢

Spark统一内存划分

A detailed explanation of throughput, QPS, TPS, concurrency and other high concurrency indicators

Come on developer! Not only for the 200000 bonus, try the best "building blocks" for a brainstorming!

图的遍历的定义以及深度优先搜索和广度优先搜索(一)

【集训Day3】Reconstruction of roads

Heavy announcement! Icml2022 Awards: 15 outstanding papers, selected by Fudan University, Xiamen University and Shanghai Jiaotong University

Asemi rectifier bridge kbpc3510, kbpc3510 package, kbpc3510 application

Cross Site Request Forgery (CSRF)

Week 17 free intrusion pointer exercise - output maximum

How to set IP for layer 2 management switches
随机推荐
Kudu design tablet
中国聚异丁烯市场研究与投资价值报告(2022版)
AI sky covering DL multilayer perceptron
【集训Day1】Spy dispatch
GAN (Generative Adversarial Network,GAN)生成式对抗网络
[training Day1] spy dispatch
点云目标检测KITTI数据集bin文件可视化,一站式解决
天翼云Web应用防火墙(边缘云版)支持检测和拦截Apache Spark shell命令注入漏洞
[machine learning] principle and code of mean shift
树形dp问题
ASEMI整流桥KBPC2510,KBPC2510参数,KBPC2510规格书
一文详解吞吐量、QPS、TPS、并发数等高并发指标
Tree DP problem
SQL中去去重的三种方式
CCS TM4C123新建工程
AI遮天传 ML-集成学习
常用超好用正则表达式!
A detailed explanation of throughput, QPS, TPS, concurrency and other high concurrency indicators
【云原生】 iVX 低代码开发 引入腾讯地图并在线预览
线性表的顺序存储结构——顺序表