当前位置:网站首页>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 了 .
边栏推荐
- 同宇新材冲刺深交所:年营收9.47亿 张驰与苏世国为实控人
- How are you in the first half of the year occupied by the epidemic| Mid 2022 summary
- 三年Android开发,2022疫情期间八家大厂的Android面试经历和真题整理
- Jushan database was among the first batch of financial information innovation solutions!
- There is a sound prompt when inserting a USB flash disk under win10 system, but the drive letter is not displayed
- 监控界的最强王者,没有之一!
- Certains marchés de l'emploi de Shanghai refusent d'embaucher des personnes qui se rétablissent positives à Xinguan
- helm部署etcd集群
- Picture zoom Center
- Docker installation redis
猜你喜欢
Multithreading Basics: basic concepts of threads and creation of threads
Video based full link Intelligent Cloud? This article explains in detail what Alibaba cloud video cloud "intelligent media production" is
图之广度优先遍历
CSRF vulnerability analysis
Noninvasive and cuff free blood pressure measurement for telemedicine [translation]
裕太微冲刺科创板:拟募资13亿 华为与小米基金是股东
Binary search tree
应用使用Druid连接池经常性断链问题分析
基于ppg和fft神经网络的光学血压估计【翻译】
爬虫玩得好,牢饭吃到饱?这3条底线千万不能碰!
随机推荐
手写一个的在线聊天系统(原理篇1)
Penetration test information collection - site architecture and construction
RedisSystemException:WRONGTYPE Operation against a key holding the wrong kind of value
Interpreting cloud native technology
多线程基础:线程基本概念与线程的创建
Using block to realize the traditional values between two pages
From 2022 to 2024, the list of cifar azrieli global scholars was announced, and 18 young scholars joined 6 research projects
Video based full link Intelligent Cloud? This article explains in detail what Alibaba cloud video cloud "intelligent media production" is
Blue Bridge Cup real question: one question with clear code, master three codes
Nuc11 cheetah Canyon setting U disk startup
Stm32+mfrc522 completes IC card number reading, password modification, data reading and writing
Understanding disentangling in β- VAE paper reading notes
R语言ggplot2可视化:使用ggpubr包的ggstripchart函数可视化分组点状条带图(dot strip plot)、设置add参数为不同水平点状条带图添加箱图
If you have any problems, you can contact me. A rookie ~
用于远程医疗的无创、无袖带血压测量【翻译】
How to improve website weight
AutoCAD - what is the default lineweight for centerline drawing and CAD? Can I modify it?
The role of applet in industrial Internet
Introduction to the use of SAP Fiori application index tool and SAP Fiori tools
AcWing 3537. Tree lookup complete binary tree