当前位置:网站首页>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 了 .
边栏推荐
- Stm32+mfrc522 completes IC card number reading, password modification, data reading and writing
- 渲大师携手向日葵,远控赋能云渲染及GPU算力服务
- Visual Studio Code启动时提示“Code安装似乎损坏。请重新安装。”、标题栏显示“不受支持”信息的解决办法
- Multithreading Basics: basic concepts of threads and creation of threads
- Human bone point detection: top-down (part of the theory)
- 十、进程管理
- 关于npm install 报错问题 error 1
- R语言ggplot2可视化时间序列柱形图:通过双色渐变配色颜色主题可视化时间序列柱形图
- pytorch常见损失函数
- Airiot IOT platform enables the container industry to build [welding station information monitoring system]
猜你喜欢
Optical blood pressure estimation based on PPG and FFT neural network [translation]
涂鸦智能在香港双重主板上市:市值112亿港元 年营收3亿美元
Describe the process of key exchange
Medical image segmentation
C#/VB.NET 给PDF文档添加文本/图像水印
How are you in the first half of the year occupied by the epidemic| Mid 2022 summary
Self supervised heterogeneous graph neural network with CO comparative learning
抽象类与抽象方法
基于蝴蝶种类识别
人体骨骼点检测:自顶向下(部分理论)
随机推荐
图之广度优先遍历
Introduction to the use of SAP Fiori application index tool and SAP Fiori tools
[matlab] Simulink the input and output variables of the same module cannot have the same name
node の SQLite
Use map function and split function to type multiple elements in one line
Openmv4 learning notes 1 --- one click download, background knowledge of image processing, lab brightness contrast
JDBC驱动器、C3P0、Druid和JDBCTemplate相关依赖jar包
Helm deploy etcd cluster
Summary of performance knowledge points
Nuc11 cheetah Canyon setting U disk startup
二叉搜索树
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
Implementation of AVL tree
Digital "new" operation and maintenance of energy industry
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
Precautions for binding shortcut keys of QPushButton
Estimate blood pressure according to PPG using spectral spectrum time depth neural network [turn]
Crawling data encounters single point login problem
UFIDA OA vulnerability learning - ncfindweb directory traversal vulnerability