定义消息
在功能包内新建msg文件夹
然后新建一个.msg结尾的文本
# MY MSG
std_msgs/Header header
geometry_msgs/Pose pose
float64 accuracy
std_msgs/Header header用来放时间戳等
geometry_msgs/Pose pose 用来放位姿
float64表示double类型
消息配置
1.在cmakelist.txt配置:
1.find_package内加入
std_msgs
geometry_msgs
2.add_message_files内加入
MapPose.msg //自己的消息名
3.generate_messages(DEPENDENCIES
std_msgs
geometry_msgs
)
2.在package.xml配置:
加入:
<build_depend>std_msgs</build_depend>
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>
赋值使用
1.先编译,在devel/include中会生成头文件MapPose.h
2. 程序中引用头文件
#include "my_cloud_map/MapPose.h" // 包名/消息名
3.程序中定义消息与赋值
my_cloud_map::MapPose pos;
pos.header.stamp = time;
pos.header.frame_id = "map";
pos.pose.position.x = ny;
pos.pose.position.y = nx;
pos.pose.position.z = -nz;
pos.pose.orientation.x = x;
pos.pose.orientation.y = y;
pos.pose.orientation.z = z;
pos.pose.orientation.w = w;
pos.accuracy = acc;
4.发布
pos_pub = n.advertise<my_cloud_map::MapPose>("map/row_pos", 1);
5.查看
参考:https://blog.csdn.net/m0_46278925/article/details/115486166?spm=1001.2101.3001.6650.3&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-3.highlightwordscore&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-3.highlightwordscore