自定义Protobuf msg
本demo定义了一个简单的图像msg:
建立文件:my_image.proto
syntax = "proto3";
message MyImage {
uint32 width = 1; // Image width (number of columns)
uint32 height = 2; // Image height (number of rows)
bytes data = 3; // Actual data, size is (step * rows)
}
包含数据类型: uint32 bytes
msg发布者主要部分:
首先要包含自定义消息在编译之后生成的头文件:
#include "../build/my_image.pb.h"
main函数中对发布节点进行初始化:
gazebo::transport::NodePtr node(new gazebo::transport::Node());//图像发布者节点
node->Init();
建立发布者:
gazebo::transport::PublisherPtr pub = node->Advertise<MyImage>("/image");
其中"/image"
是要发布的位置 对于gazebo中的位置
<MyImage>
是在proto文件中定义的消息类型名称 在头文件中有定义
建立并填充消息:
MyImage image_message;//建立消息
image_message.set_width(image.cols);
image_message.set_height(image.rows);
image_message.set_data(image_data.data(), image_data.size());
使用set_xxx
函数对消息中的各个数据域进行填充
发布数据
pub->Publish(image_message);
发布者完整代码
#include <gazebo/gazebo.hh>
#include <gazebo/transport/transport.hh>
#include <opencv2/opencv.hpp>
#include <gazebo/physics/physics.hh>
#include <gazebo/common/common.hh>
#include <gazebo/gazebo_client.hh>
#include <iostream>
//自定义消息格式
#include "../build/my_image.pb.h"
//这个宏需要自行定义 否则会提示找不到ConstMyImagePtr定义
typedef const boost::shared_ptr<const MyImage> ConstMyImagePtr;
using namespace cv;
using namespace std;
//将OpenCV图像转换为u8 vector
void ConvertImageToBytes(const cv::Mat