当前位置:网站首页>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
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-KPYytd15-1656918643941)(vx_images/396065792889745.png)]](/img/d4/fddc87647af4cb5a5a0769d7eb943b.png)
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 了 .
边栏推荐
- Penetration test information collection - App information
- Stm32+mfrc522 completes IC card number reading, password modification, data reading and writing
- Certains marchés de l'emploi de Shanghai refusent d'embaucher des personnes qui se rétablissent positives à Xinguan
- Noninvasive and cuff free blood pressure measurement for telemedicine [translation]
- C#/VB.NET 给PDF文档添加文本/图像水印
- Stm32+esp8266+mqtt protocol connects onenet IOT platform
- Interpreting cloud native technology
- Installation and management procedures
- Example of implementing web server with stm32+enc28j60+uip protocol stack
- QPushButton绑定快捷键的注意事项
猜你喜欢

wx小程序学习笔记day01
![[matlab] Simulink the input and output variables of the same module cannot have the same name](/img/99/adfe50075010916439cd053b8f04c7.png)
[matlab] Simulink the input and output variables of the same module cannot have the same name

How are you in the first half of the year occupied by the epidemic| Mid 2022 summary

Introduction to the use of SAP Fiori application index tool and SAP Fiori tools

裕太微冲刺科创板:拟募资13亿 华为与小米基金是股东

Reptiles have a good time. Are you full? These three bottom lines must not be touched!

2022-2024年CIFAR Azrieli全球学者名单公布,18位青年学者加入6个研究项目

徐翔妻子应莹回应“股评”:自己写的!

C#/VB.NET 给PDF文档添加文本/图像水印

测试行业的小伙伴,有问题可以找我哈。菜鸟一枚~
随机推荐
[depth first search] Ji suanke: a joke of replacement
2022/02/12
R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图
Binary search tree
Summary of performance knowledge points
Certains marchés de l'emploi de Shanghai refusent d'embaucher des personnes qui se rétablissent positives à Xinguan
2022-2024年CIFAR Azrieli全球学者名单公布,18位青年学者加入6个研究项目
Huawei 0 foundation - image sorting
Docker installation redis
Penetration test information collection - site architecture and construction
能源行业的数字化“新”运维
From 2022 to 2024, the list of cifar azrieli global scholars was announced, and 18 young scholars joined 6 research projects
Penetration test information collection - basic enterprise information
A method of sequentially loading Unity Resources
Nuc11 cheetah Canyon setting U disk startup
44 colleges and universities were selected! Publicity of distributed intelligent computing project list
pytorch常见损失函数
QLabel 跑马灯文字显示
Multithreading Basics: basic concepts of threads and creation of threads
With the implementation of MapReduce job de emphasis, a variety of output folders