当前位置:网站首页>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 了 .
边栏推荐
- 287. Find duplicates
- The role of applet in industrial Internet
- 同宇新材冲刺深交所:年营收9.47亿 张驰与苏世国为实控人
- If you have any problems, you can contact me. A rookie ~
- UFIDA OA vulnerability learning - ncfindweb directory traversal vulnerability
- Method of accessing mobile phone storage location permission under non root condition
- Numerical analysis: least squares and ridge regression (pytoch Implementation)
- Nuc11 cheetah Canyon setting U disk startup
- Atcoder a mountaineer
- 监控界的最强王者,没有之一!
猜你喜欢
Docker installation redis
Optical blood pressure estimation based on PPG and FFT neural network [translation]
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
测试行业的小伙伴,有问题可以找我哈。菜鸟一枚~
How are you in the first half of the year occupied by the epidemic| Mid 2022 summary
朗坤智慧冲刺科创板:年营收4亿 拟募资7亿
Multithreading Basics: basic concepts of threads and creation of threads
From 2022 to 2024, the list of cifar azrieli global scholars was announced, and 18 young scholars joined 6 research projects
多线程基础:线程基本概念与线程的创建
A wearable arm device for night and sleeveless blood pressure measurement [translation]
随机推荐
Docker installation redis
[depth first search] Ji suanke: find numbers
Example of implementing web server with stm32+enc28j60+uip protocol stack
Medical image segmentation
徐翔妻子应莹回应“股评”:自己写的!
Penetration test information collection - basic enterprise information
Implementation of AVL tree
关于静态类型、动态类型、id、instancetype
R语言dplyr包进行数据分组聚合统计变换(Aggregating transforms)、计算dataframe数据的分组均值(mean)
About NPM install error 1
用于远程医疗的无创、无袖带血压测量【翻译】
Penetration test information collection - App information
Atcoder a mountaineer
node の SQLite
pychrm社区版调用matplotlib.pyplot.imshow()函数图像不弹出的解决方法
[Matlab] Simulink 同一模块的输入输出的变量不能同名
Noninvasive and cuff free blood pressure measurement for telemedicine [translation]
If you have any problems, you can contact me. A rookie ~
Optical blood pressure estimation based on PPG and FFT neural network [translation]
[paper notes] transunet: transformers make strongencoders for medical image segmentation