参考链接:
ROS学习之路04:编写基于自定义消息(.msg)进行通信的节点
(1)创建包包含message_generation message_runtime
```bash
catkin_create_pkg custom_msg_topic roscpp std_msgs message_generation message_runtime
(2)需要在CMakeList.txt修改如下代码才能编译通过,并且可以通过rosmsg查看:
find_package(catkin REQUIRED COMPONENTS
roscpp
std_msgs
message_generation
)
add_message_files(
FILES
custom_msg.msg
)
generate_messages(
DEPENDENCIES
std_msgs
)
(3)代码中订阅发布消息的类型:
//引入头文件 custom_msg_topic/custom_msg.h
#include "custom_msg_topic/custom_msg.h"
//发布消息的类型 custom_msg_topic::custom_msg
ros::Publisher custom_msg_pub = nh.advertise<custom_msg_topic::custom_msg>("student_info", 1000);
//订阅消息的类型 custom_msg_topic::custom_msg
void Callback(const custom_msg_topic::custom_msgConstPtr &msg)