当前位置:网站首页>ROS custom message publishing subscription example
ROS custom message publishing subscription example
2022-07-06 18:56:00 【Adunn】
ROS Example of custom message publishing and subscription
This paper mainly explains from the following aspects :
- 1. Create custom msg news
- 2. Release msg - Other packages call customization msg type
- 3. Subscription customization msg
1. Create custom msg news
First create an empty package Separate storage msg type ( Of course, it can also be in any package Custom in msg type )
For the convenience of explanation , Create a wts_rviz_msgs My bag , Used to customize msg Examples of usage of types .
$ cd catkin_ws/src // This is just an example , It is actually the project root directory (src、devel Under the same category )
$ catkin_create_pkg wts_rviz_msgs
1.1. newly build msg file
stay wts_rviz_msgs Created in msg Folder , stay msg Create the following message type file in the folder :
- PointInfo.msg
- ParkingSpot.msg
- ParkingSpotList.msg
$ cd wts_rviz_msgs
$ mkdir msg
For basic types, please refer to :
- common_msgs – http://wiki.ros.org/common_msgs
- std_msgs – http://wiki.ros.org/std_msgs
PointInfo.msg Content :
float32 dPointX
float32 dPointY
ParkingSpot.msg Content :
string parking_spot_name
bool publish_state
geometry_msgs/Pose2D pose
PointInfo[] point_list
ParkingSpotList.msg Content :
ParkingSpot[] parking_spot_list
1.2. modify package.xml
need message_generation Generate C++ or Python Usable code , need message_runtime Provide runtime support , therefore package.xml Add the following two sentences
ROS1 in :
<build_depend>message_generation</build_depend>
<run_depend>message_runtime</run_depend>
ROS2 in :
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>
1.3. modify CMakeLists.txt
CMakeLists.txt Pay attention to four places
(1) Set up find_package
First call find_package Find dependent packages , Necessary are roscpp rospy message_generation, Others are added according to specific types , Like the one above msg The document uses geometry_msgs/Pose pose type , Then you must find geometry_msgs
find_package(catkin REQUIRED COMPONENTS roscpp rospy message_generation std_msgs geometry_msgs)
(2) Set up add_message_files
And then there was add_message_files , Appoint msg file
add_message_files(
FILES
PointInfo.msg
ParkingSpot.msg
ParkingSpotList.msg
)
(3) Set up generate_messages
And then there was generate_messages , Specify the dependencies when generating the message file , For example, other message types are nested above geometry_msgs, Then you must indicate
#generate_messages Must be in catkin_package front
generate_messages(
DEPENDENCIES
std_msgs
geometry_msgs
)
(4) Set up catkin_package
And then there was catkin_package Set run dependency
catkin_package(
CATKIN_DEPENDS message_runtime
)
(5) compile
$ cd catkin_ws // This is just an example , It is actually the project root directory (src、devel Under the same category )
$ catkin_make -j4
(6) see rosmsg
New here msg type wts_rviz_msgs/ParkingSpotList You can use it , Now compile this package , And then use it rosmsg show Command view .
$ cd Project root // And devel At the same directory
$ source devel/setup.bash # Be careful : Must be implemented source Installation , Otherwise, the message will not be recognized
$ rosmsg show wts_rviz_msgs/ParkingSpotList
2. Release msg - Other packages call customization msg type
To use a custom message type, you must source Customize the workspace where the message is located , otherwise rosmsg show wts_rviz_msgs/ParkingSpotList t and rostopic echo /wts_topic_rviz_msgs(/wts_rviz_msgs Is the custom message type used in the node wts_rviz_msgs/ParkingSpotList Of topic) All will report wrong. , Because no source In this case, the custom message type is invisible , Is considered an undefined type
A typical mistake is to delete devel and build Folder and recompile the code , At this time, because there is no source Customize the workspace where the message is located , Even if the code using the custom message and the custom message are in the same package, they cannot be found , The header file , You need to run source devel/setup.bash Then recompile it .
If it's in test_msgs Call... From the node in the package test_msgs/Test type , Only need .cpp Call the following in the file
#include "wts_rviz_msgs/ParkingSpotList.h"
wts_rviz_msgs::ParkingSpotList msg_parking_spot_list_published;
If it is called in other packages wts_rviz_msgs/ParkingSpotList Type needs to be modified package.xml and CMakeLists.txt, For example, in the workspace catkin_ws There is a named interactive_parking_spot My bag , We can write a node in this package , Use the message type we just customized wts_rviz_msgs/ParkingSpotList , as follows :
**(1) modify package.xml **
Develop good habits , Maintain the update of software package list , So that others can install various dependencies before using your software , Of course, this file does not affect program compilation
ROS1 in :
<build_depend>roscpp</build_depend>
<run_depend>roscpp</run_depend>
<build_depend>wts_rviz_msgs</build_depend>
<run_depend>wts_rviz_msgs</run_depend>
ROS2 in :
<build_depend>roscpp</build_depend>
<run_depend>roscpp</run_depend>
<build_depend>wts_rviz_msgs</build_depend>
<exec_depend>wts_rviz_msgs</exec_depend>
**(2) modify CMakeLists.txt **
Calling a custom message type mainly modifies two places , Here's the point :
One is find_package You need to declare to find the package containing this message type ;
Two is add_dependencies Note the dependence of the message , Other places are the same as ordinary nodes
#: Be careful , Because geometry_msgs/Pose2D type , The compiled version requires Greater than C++11
add_compile_options(-std=c++17)
find_package(catkin REQUIRED COMPONENTS
roscpp
geometry_msgs
wts_rviz_msgs
)
add_executable(interactive_parking_spot src/main.cpp)
target_link_libraries(interactive_parking_spot
${catkin_LIBRARIES}
)
# When calling a custom message type in the same workspace, indicate the dependency , Prevent the error that the header file cannot be found
add_dependencies(interactive_parking_spot wts_rviz_msgs_gencpp)
If there is a lack of add_dependencies Chinese vs wts_rviz_msgs_gencpp A dependency statement for , When compiling, if you compile first interactive_parking_spot Package recompilation wts_rviz_msgs/ParkingSpotList The following error will appear in the package (ROS The compilation order of each software package in the workspace is random ), Because the header file wts_rviz_msgs/ParkingSpotList.h Not generated yet
fatal error: wts_rviz_msgs/ParkingSpotList.h: There is no file or directory
#include “wts_rviz_msgs/ParkingSpotList.h”
2.1. Write message publishing code : src/main.cpp
#include "wts_rviz_msgs/ParkingSpotList.h"
int main(int argc, char** argv)
{
ros::init(argc, argv, "simple_marker");
ros::NodeHandle n;
ros::Publisher chatter_pub = n.advertise<wts_rviz_msgs::ParkingSpotList>("wts_topic_msg_parking_spots_info", 1000);
ros::Rate loop_rate(1);
while(ros::ok())
{
int iNum = parkingSpot.m_mapParkingSpot.size();
wts_rviz_msgs::ParkingSpotList msg_parking_spot_list_published;
std::map<std::string, WtsRvizParkingSpot>::iterator iter;
int iIndex = 0;
for(iter = parkingSpot.m_mapParkingSpot.begin(); iter!=parkingSpot.m_mapParkingSpot.end(); iter++)
{
wts_rviz_msgs::ParkingSpot parking_spot;
parking_spot.parking_spot_name = "ParkingSpotName";
parking_spot.publish_state = true
parking_spot.pose.x = 2.0082790851593018;
parking_spot.pose.x = 0;
parking_spot.pose.theta = 0.7217559218406677;
for(int iIndex = 0; iIndex < iter->second.vctrParkingSpotPoints.size(); iIndex++)
{
wts_rviz_msgs::PointInfo point_info;
point_info.dPointX = -1.6596179008483887;
point_info.dPointY = 2.2936594486236572;
parking_spot.point_list.push_back(point_info);
}
msg_parking_spot_list_published.parking_spot_list.push_back(parking_spot);
iIndex++;
}
chatter_pub.publish(msg_parking_spot_list_published);
ros::spinOnce();
loop_rate.sleep();
}
}
3. Subscription customization msg
Create a new accept message node mylisten.cpp, Add the following code :bash
#include <ros/ros.h>
#include <geometry_msgs/Pose2D.h>
#include "wts_rviz_msgs/ParkingSpotList.h"
void msgCallback(const wts_rviz_msgs::ParkingSpotList::ConstPtr &P)
{
ROS_INFO("I recevied the topic: ");
for(std::vector<wts_rviz_msgs::ParkingSpotList>::const_iterator it = P->path.begin(); it != P->path.end(); ++it)
{
//... ...
}
}
int main(int argc,char ** argv)
{
ros::init(argc,argv,"test1");
ros::NodeHandle n;
ros::Subscriber msg_sub = n.subscribe("wts_topic_msg_parking_spots_info", 100, msgCallback);
ros::spin();
return 0;
}
Follow the previous release msg An example of , modify CMakeLists.txt、package.xml, Then catkin_make Recompile , Just OK 了 .
边栏推荐
- pytorch常见损失函数
- Blue Bridge Cup real question: one question with clear code, master three codes
- From 2022 to 2024, the list of cifar azrieli global scholars was announced, and 18 young scholars joined 6 research projects
- openmv4 学习笔记1----一键下载、图像处理背景知识、LAB亮度-对比度
- Estimate blood pressure according to PPG using spectral spectrum time depth neural network [turn]
- Understanding disentangling in β- VAE paper reading notes
- 提前解锁 2 大直播主题!今天手把手教你如何完成软件包集成?|第 29-30 期
- Collection of penetration test information -- use with nmap and other tools
- Use cpolar to build a business website (1)
- QPushButton绑定快捷键的注意事项
猜你喜欢
When visual studio code starts, it prompts "the code installation seems to be corrupt. Please reinstall." Solution to displaying "unsupported" information in the title bar
44 colleges and universities were selected! Publicity of distributed intelligent computing project list
监控界的最强王者,没有之一!
应用使用Druid连接池经常性断链问题分析
Video based full link Intelligent Cloud? This article explains in detail what Alibaba cloud video cloud "intelligent media production" is
Reptiles have a good time. Are you full? These three bottom lines must not be touched!
2022-2024年CIFAR Azrieli全球学者名单公布,18位青年学者加入6个研究项目
基于ppg和fft神经网络的光学血压估计【翻译】
AUTOCAD——中心线绘制、CAD默认线宽是多少?可以修改吗?
爬虫玩得好,牢饭吃到饱?这3条底线千万不能碰!
随机推荐
Jdbc driver, c3p0, druid and jdbctemplate dependent jar packages
Master Xuan joined hands with sunflower to remotely control enabling cloud rendering and GPU computing services
上海部分招工市場對新冠陽性康複者拒絕招錄
同宇新材冲刺深交所:年营收9.47亿 张驰与苏世国为实控人
[depth first search] Ji suanke: a joke of replacement
Stm32+esp8266+mqtt protocol connects onenet IOT platform
一种用于夜间和无袖测量血压手臂可穿戴设备【翻译】
青龙面板最近的库
test about BinaryTree
helm部署etcd集群
Penetration test information collection - WAF identification
渲大师携手向日葵,远控赋能云渲染及GPU算力服务
Stm32+hc05 serial port Bluetooth design simple Bluetooth speaker
Unlock 2 live broadcast themes in advance! Today, I will teach you how to complete software package integration Issues 29-30
能源行业的数字化“新”运维
AvL树的实现
wx小程序学习笔记day01
徐翔妻子应莹回应“股评”:自己写的!
[Sun Yat sen University] information sharing of postgraduate entrance examination and re examination
When visual studio code starts, it prompts "the code installation seems to be corrupt. Please reinstall." Solution to displaying "unsupported" information in the title bar