1#define _GLIBCXX_USE_CXX11_ABI 0
15#include <fastdds/dds/domain/DomainParticipant.hpp>
16#include <fastdds/dds/publisher/Publisher.hpp>
17#include <fastdds/dds/topic/Topic.hpp>
18#include <fastdds/dds/publisher/DataWriter.hpp>
19#include <fastdds/dds/topic/TypeSupport.hpp>
21#include <fastdds/dds/domain/qos/DomainParticipantQos.hpp>
22#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
23#include <fastdds/dds/publisher/qos/PublisherQos.hpp>
24#include <fastdds/dds/topic/qos/TopicQos.hpp>
26#include <fastrtps/attributes/ParticipantAttributes.h>
27#include <fastrtps/qos/QosPolicies.h>
28#include <fastdds/dds/publisher/qos/DataWriterQos.hpp>
29#include <fastdds/dds/publisher/DataWriterListener.hpp>
40 namespace efd = eprosima::fastdds::dds;
44 using erc = eprosima::fastrtps::types::ReturnCode_t;
83 struct CarlaCameraInfoPublisherImpl {
95 efd::Topic*
_topic {
nullptr };
188 if (
_impl->_type ==
nullptr) {
189 std::cerr <<
"Invalid TypeSupport" << std::endl;
193 efd::DomainParticipantQos pqos = efd::PARTICIPANT_QOS_DEFAULT;
195 auto factory = efd::DomainParticipantFactory::get_instance();
196 _impl->_participant = factory->create_participant(0, pqos);
197 if (
_impl->_participant ==
nullptr) {
198 std::cerr <<
"Failed to create DomainParticipant" << std::endl;
201 _impl->_type.register_type(
_impl->_participant);
203 efd::PublisherQos pubqos = efd::PUBLISHER_QOS_DEFAULT;
204 _impl->_publisher =
_impl->_participant->create_publisher(pubqos,
nullptr);
205 if (
_impl->_publisher ==
nullptr) {
206 std::cerr <<
"Failed to create Publisher" << std::endl;
210 efd::TopicQos tqos = efd::TOPIC_QOS_DEFAULT;
211 const std::string publisher_type {
"/image"};
212 const std::string base {
"rt/carla/" };
213 std::string topic_name = base;
217 topic_name += publisher_type;
218 _impl->_topic =
_impl->_participant->create_topic(topic_name,
_impl->_type->getName(), tqos);
219 if (
_impl->_topic ==
nullptr) {
220 std::cerr <<
"Failed to create Topic" << std::endl;
224 efd::DataWriterQos wqos = efd::DATAWRITER_QOS_DEFAULT;
225 wqos.endpoint().history_memory_policy = eprosima::fastrtps::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE;
227 _impl->_datawriter =
_impl->_publisher->create_datawriter(
_impl->_topic, wqos, listener);
228 if (
_impl->_datawriter ==
nullptr) {
229 std::cerr <<
"Failed to create DataWriter" << std::endl;
241 if (
_info->_type ==
nullptr) {
242 std::cerr <<
"Invalid TypeSupport" << std::endl;
246 efd::DomainParticipantQos pqos = efd::PARTICIPANT_QOS_DEFAULT;
249 auto factory = efd::DomainParticipantFactory::get_instance();
250 _info->_participant = factory->create_participant(0, pqos);
252 if (
_info->_participant ==
nullptr) {
254 std::cerr <<
"Failed to create DomainParticipant" << std::endl;
258 _info->_type.register_type(
_info->_participant);
260 efd::PublisherQos pubqos = efd::PUBLISHER_QOS_DEFAULT;
262 _info->_publisher =
_info->_participant->create_publisher(pubqos,
nullptr);
263 if (
_info->_publisher ==
nullptr) {
265 std::cerr <<
"Failed to create Publisher" << std::endl;
269 efd::TopicQos tqos = efd::TOPIC_QOS_DEFAULT;
271 const std::string publisher_type {
"/camera_info"};
272 const std::string base {
"rt/carla/" };
273 std::string topic_name = base;
277 topic_name += publisher_type;
279 _info->_topic =
_info->_participant->create_topic(topic_name,
_info->_type->getName(), tqos);
280 if (
_info->_topic ==
nullptr) {
282 std::cerr <<
"Failed to create Topic" << std::endl;
286 efd::DataWriterQos wqos = efd::DATAWRITER_QOS_DEFAULT;
290 _info->_datawriter =
_info->_publisher->create_datawriter(
_info->_topic, wqos, listener);
291 if (
_info->_datawriter ==
nullptr) {
293 std::cerr <<
"Failed to create DataWriter" << std::endl;
308 std::cerr <<
"Invalid TypeSupport" << std::endl;
312 efd::DomainParticipantQos pqos = efd::PARTICIPANT_QOS_DEFAULT;
315 auto factory = efd::DomainParticipantFactory::get_instance();
317 _point_cloud->_participant = factory->create_participant(0, pqos);
319 std::cerr <<
"Failed to create DomainParticipant" << std::endl;
325 efd::PublisherQos pubqos = efd::PUBLISHER_QOS_DEFAULT;
328 std::cerr <<
"Failed to create Publisher" << std::endl;
332 efd::TopicQos tqos = efd::TOPIC_QOS_DEFAULT;
333 const std::string publisher_type {
"/point_cloud"};
334 const std::string base {
"rt/carla/" };
335 std::string topic_name = base;
339 topic_name += publisher_type;
342 std::cerr <<
"Failed to create Topic" << std::endl;
346 efd::DataWriterQos wqos = efd::DATAWRITER_QOS_DEFAULT;
347 wqos.endpoint().history_memory_policy = eprosima::fastrtps::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE;
351 std::cerr <<
"Failed to create DataWriter" << std::endl;
374 eprosima::fastrtps::rtps::InstanceHandle_t instance_handle;
375 eprosima::fastrtps::types::ReturnCode_t rcode =
_impl->_datawriter->write(&
_impl->_image, instance_handle);
376 if (rcode == erc::ReturnCodeValue::RETCODE_OK) {
379 if (rcode == erc::ReturnCodeValue::RETCODE_ERROR) {
380 std::cerr <<
"RETCODE_ERROR" << std::endl;
383 if (rcode == erc::ReturnCodeValue::RETCODE_UNSUPPORTED) {
384 std::cerr <<
"RETCODE_UNSUPPORTED" << std::endl;
387 if (rcode == erc::ReturnCodeValue::RETCODE_BAD_PARAMETER) {
388 std::cerr <<
"RETCODE_BAD_PARAMETER" << std::endl;
391 if (rcode == erc::ReturnCodeValue::RETCODE_PRECONDITION_NOT_MET) {
392 std::cerr <<
"RETCODE_PRECONDITION_NOT_MET" << std::endl;
395 if (rcode == erc::ReturnCodeValue::RETCODE_OUT_OF_RESOURCES) {
396 std::cerr <<
"RETCODE_OUT_OF_RESOURCES" << std::endl;
399 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ENABLED) {
400 std::cerr <<
"RETCODE_NOT_ENABLED" << std::endl;
403 if (rcode == erc::ReturnCodeValue::RETCODE_IMMUTABLE_POLICY) {
404 std::cerr <<
"RETCODE_IMMUTABLE_POLICY" << std::endl;
407 if (rcode == erc::ReturnCodeValue::RETCODE_INCONSISTENT_POLICY) {
408 std::cerr <<
"RETCODE_INCONSISTENT_POLICY" << std::endl;
411 if (rcode == erc::ReturnCodeValue::RETCODE_ALREADY_DELETED) {
412 std::cerr <<
"RETCODE_ALREADY_DELETED" << std::endl;
415 if (rcode == erc::ReturnCodeValue::RETCODE_TIMEOUT) {
416 std::cerr <<
"RETCODE_TIMEOUT" << std::endl;
419 if (rcode == erc::ReturnCodeValue::RETCODE_NO_DATA) {
420 std::cerr <<
"RETCODE_NO_DATA" << std::endl;
423 if (rcode == erc::ReturnCodeValue::RETCODE_ILLEGAL_OPERATION) {
424 std::cerr <<
"RETCODE_ILLEGAL_OPERATION" << std::endl;
427 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ALLOWED_BY_SECURITY) {
428 std::cerr <<
"RETCODE_NOT_ALLOWED_BY_SECURITY" << std::endl;
431 std::cerr <<
"UNKNOWN" << std::endl;
441 eprosima::fastrtps::rtps::InstanceHandle_t instance_handle;
442 eprosima::fastrtps::types::ReturnCode_t rcode =
_info->_datawriter->write(&
_info->_ci, instance_handle);
443 if (rcode == erc::ReturnCodeValue::RETCODE_OK) {
446 if (rcode == erc::ReturnCodeValue::RETCODE_ERROR) {
447 std::cerr <<
"RETCODE_ERROR" << std::endl;
450 if (rcode == erc::ReturnCodeValue::RETCODE_UNSUPPORTED) {
451 std::cerr <<
"RETCODE_UNSUPPORTED" << std::endl;
454 if (rcode == erc::ReturnCodeValue::RETCODE_BAD_PARAMETER) {
455 std::cerr <<
"RETCODE_BAD_PARAMETER" << std::endl;
458 if (rcode == erc::ReturnCodeValue::RETCODE_PRECONDITION_NOT_MET) {
459 std::cerr <<
"RETCODE_PRECONDITION_NOT_MET" << std::endl;
462 if (rcode == erc::ReturnCodeValue::RETCODE_OUT_OF_RESOURCES) {
463 std::cerr <<
"RETCODE_OUT_OF_RESOURCES" << std::endl;
466 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ENABLED) {
467 std::cerr <<
"RETCODE_NOT_ENABLED" << std::endl;
470 if (rcode == erc::ReturnCodeValue::RETCODE_IMMUTABLE_POLICY) {
471 std::cerr <<
"RETCODE_IMMUTABLE_POLICY" << std::endl;
474 if (rcode == erc::ReturnCodeValue::RETCODE_INCONSISTENT_POLICY) {
475 std::cerr <<
"RETCODE_INCONSISTENT_POLICY" << std::endl;
478 if (rcode == erc::ReturnCodeValue::RETCODE_ALREADY_DELETED) {
479 std::cerr <<
"RETCODE_ALREADY_DELETED" << std::endl;
482 if (rcode == erc::ReturnCodeValue::RETCODE_TIMEOUT) {
483 std::cerr <<
"RETCODE_TIMEOUT" << std::endl;
486 if (rcode == erc::ReturnCodeValue::RETCODE_NO_DATA) {
487 std::cerr <<
"RETCODE_NO_DATA" << std::endl;
490 if (rcode == erc::ReturnCodeValue::RETCODE_ILLEGAL_OPERATION) {
491 std::cerr <<
"RETCODE_ILLEGAL_OPERATION" << std::endl;
494 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ALLOWED_BY_SECURITY) {
495 std::cerr <<
"RETCODE_NOT_ALLOWED_BY_SECURITY" << std::endl;
498 std::cerr <<
"UNKNOWN" << std::endl;
508 eprosima::fastrtps::rtps::InstanceHandle_t instance_handle;
509 eprosima::fastrtps::types::ReturnCode_t rcode =
_point_cloud->_datawriter->write(&
_point_cloud->_pc, instance_handle);
510 if (rcode == erc::ReturnCodeValue::RETCODE_OK) {
513 if (rcode == erc::ReturnCodeValue::RETCODE_ERROR) {
514 std::cerr <<
"RETCODE_ERROR" << std::endl;
517 if (rcode == erc::ReturnCodeValue::RETCODE_UNSUPPORTED) {
518 std::cerr <<
"RETCODE_UNSUPPORTED" << std::endl;
521 if (rcode == erc::ReturnCodeValue::RETCODE_BAD_PARAMETER) {
522 std::cerr <<
"RETCODE_BAD_PARAMETER" << std::endl;
525 if (rcode == erc::ReturnCodeValue::RETCODE_PRECONDITION_NOT_MET) {
526 std::cerr <<
"RETCODE_PRECONDITION_NOT_MET" << std::endl;
529 if (rcode == erc::ReturnCodeValue::RETCODE_OUT_OF_RESOURCES) {
530 std::cerr <<
"RETCODE_OUT_OF_RESOURCES" << std::endl;
533 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ENABLED) {
534 std::cerr <<
"RETCODE_NOT_ENABLED" << std::endl;
537 if (rcode == erc::ReturnCodeValue::RETCODE_IMMUTABLE_POLICY) {
538 std::cerr <<
"RETCODE_IMMUTABLE_POLICY" << std::endl;
541 if (rcode == erc::ReturnCodeValue::RETCODE_INCONSISTENT_POLICY) {
542 std::cerr <<
"RETCODE_INCONSISTENT_POLICY" << std::endl;
545 if (rcode == erc::ReturnCodeValue::RETCODE_ALREADY_DELETED) {
546 std::cerr <<
"RETCODE_ALREADY_DELETED" << std::endl;
549 if (rcode == erc::ReturnCodeValue::RETCODE_TIMEOUT) {
550 std::cerr <<
"RETCODE_TIMEOUT" << std::endl;
553 if (rcode == erc::ReturnCodeValue::RETCODE_NO_DATA) {
554 std::cerr <<
"RETCODE_NO_DATA" << std::endl;
557 if (rcode == erc::ReturnCodeValue::RETCODE_ILLEGAL_OPERATION) {
558 std::cerr <<
"RETCODE_ILLEGAL_OPERATION" << std::endl;
561 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ALLOWED_BY_SECURITY) {
562 std::cerr <<
"RETCODE_NOT_ALLOWED_BY_SECURITY" << std::endl;
565 std::cerr <<
"UNKNOWN" << std::endl;
582 std::vector<uint8_t> im_data;
583 const size_t im_size = width * height * 3;
584 im_data.resize(im_size);
586 for (
size_t i = 0; i < elements; ++i, ++vec_event) {
588 size_t index = (vec_event->
y * width + vec_event->
x) * 3 + (
static_cast<int>(vec_event->
pol) * 2);
589 im_data[index] = 255;
592 SetData(seconds, nanoseconds, height, width, std::move(im_data));
611 std_msgs::msg::Header header;
612 header.stamp(std::move(time));
614 _impl->_image.header(header);
615 _info->_ci.header(header);
618 _impl->_image.width(width);
619 _impl->_image.height(height);
620 _impl->_image.encoding(
"bgr8");
621 _impl->_image.is_bigendian(0);
622 _impl->_image.step(
_impl->_image.width() *
sizeof(uint8_t) * 3);
623 _impl->_image.data(std::move(data));
638 std_msgs::msg::Header header;
639 header.stamp(std::move(time));
674 std::vector<uint8_t> vector_data;
675 const size_t size = height * width;
676 vector_data.resize(size);
677 std::memcpy(&vector_data[0], &data[0], size);
680 descriptor1.
name(
"x");
683 descriptor1.
count(1);
685 descriptor2.
name(
"y");
688 descriptor2.
count(1);
690 descriptor3.
name(
"t");
693 descriptor3.
count(1);
695 descriptor3.
name(
"pol");
698 descriptor3.
count(1);
704 _point_cloud->_pc.fields({descriptor1, descriptor2, descriptor3, descriptor4});
734 if (
_impl->_datawriter)
735 _impl->_publisher->delete_datawriter(
_impl->_datawriter);
737 if (
_impl->_publisher)
738 _impl->_participant->delete_publisher(
_impl->_publisher);
741 _impl->_participant->delete_topic(
_impl->_topic);
743 if (
_impl->_participant)
744 efd::DomainParticipantFactory::get_instance()->delete_participant(
_impl->_participant);
749 if (
_info->_datawriter)
750 _info->_publisher->delete_datawriter(
_info->_datawriter);
752 if (
_info->_publisher)
753 _info->_participant->delete_publisher(
_info->_publisher);
756 _info->_participant->delete_topic(
_info->_topic);
758 if (
_info->_participant)
759 efd::DomainParticipantFactory::get_instance()->delete_participant(
_info->_participant);
774 efd::DomainParticipantFactory::get_instance()->delete_participant(
_point_cloud->_participant);
818 _name = std::move(other._name);
819 _parent = std::move(other._parent);
820 _impl = std::move(other._impl);
821 _info = std::move(other._info);
834 _name = std::move(other._name);
835 _parent = std::move(other._parent);
836 _impl = std::move(other._impl);
837 _info = std::move(other._info);
eProsima_user_DllExport void nanosec(uint32_t _nanosec)
此函数设置成员nanosec的值。
eProsima_user_DllExport void sec(int32_t _sec)
此函数设置成员sec的值。
void SetPointCloudData(size_t height, size_t width, size_t elements, const uint8_t *data)
设置点云数据。
CarlaDVSCameraPublisher & operator=(const CarlaDVSCameraPublisher &)
赋值运算符重载。
CarlaDVSCameraPublisher(const char *ros_name="", const char *parent="")
CarlaDVSCameraPublisher的构造函数。
std::shared_ptr< CarlaCameraInfoPublisherImpl > _info
std::shared_ptr< CarlaDVSCameraPublisherImpl > _impl
bool Publish()
发布图像、信息和点云数据。
void SetImageData(int32_t seconds, uint32_t nanoseconds, size_t elements, size_t height, size_t width, const uint8_t *data)
设置图像数据,用于DVS(动态视觉传感器)相机发布者。
void InitInfoData(uint32_t x_offset, uint32_t y_offset, uint32_t height, uint32_t width, float fov, bool do_rectify)
初始化相机信息数据
bool InitPointCloud()
初始化点云发布相关资源
void SetCameraInfoData(int32_t seconds, uint32_t nanoseconds)
设置相机信息数据的时间戳和帧ID。
bool InitInfo()
初始化相机信息发布相关资源
bool InitImage()
初始化图像发布相关资源
bool HasBeenInitialized() const
检查是否已初始化
bool PublishInfo()
发布信息数据。
void SetData(int32_t seconds, uint32_t nanoseconds, size_t height, size_t width, std::vector< uint8_t > &&data)
设置图像数据,包括时间戳、图像尺寸和图像数据。
bool PublishPointCloud()
发布点云数据。
void SetInfoRegionOfInterest(uint32_t x_offset, uint32_t y_offset, uint32_t height, uint32_t width, bool do_rectify)
设置相机信息中的感兴趣区域(Region Of Interest, ROI)。
bool PublishImage()
发布图像数据。
bool Init()
初始化CarlaDVSCameraPublisher
std::shared_ptr< CarlaPointCloudPublisherImpl > _point_cloud
~CarlaDVSCameraPublisher()
CarlaDVSCameraPublisher的析构函数。
const std::string & parent() const
此类表示用户在 IDL 文件中定义的 CameraInfo 类型的 TopicDataType。 <>
This class represents the structure CameraInfo defined by the user in the IDL file....
此类表示用户在IDL文件中定义的Image类型的主题数据类型。
This class represents the structure Image defined by the user in the IDL file.这个类表示在 IDL(接口定义语言)文件中由用...
This class represents the TopicDataType of the type PointCloud2 defined by the user in the IDL file.
This class represents the structure PointCloud2 defined by the user in the IDL file.
此类表示用户在 IDL 文件中定义的结构 PointField。 <>
eProsima_user_DllExport void count(uint32_t _count)
此函数设置成员 count 的值
eProsima_user_DllExport void offset(uint32_t _offset)
此函数设置成员 offset 的值
eProsima_user_DllExport void name(const std::string &_name)
此函数复制成员 name 的值
eProsima_user_DllExport void datatype(uint8_t _datatype)
此函数设置成员 datatype 的值
This class represents the structure RegionOfInterest defined by the user in the IDL file.
eProsima_user_DllExport void y_offset(uint32_t _y_offset)
This function sets a value in member y_offset
eProsima_user_DllExport void width(uint32_t _width)
This function sets a value in member width
eProsima_user_DllExport void height(uint32_t _height)
This function sets a value in member height
eProsima_user_DllExport void x_offset(uint32_t _x_offset)
This function sets a value in member x_offset
eProsima_user_DllExport void do_rectify(bool _do_rectify)
This function sets a value in member do_rectify
eprosima::fastrtps::types::ReturnCode_t erc
@using erc
const uint8_t PointField__UINT16
const uint8_t PointField__INT8
const uint8_t PointField__FLOAT64
efd::DomainParticipant * _participant
DDS域参与者指针。
efd::TypeSupport _type
DDS类型支持,用于相机信息消息。
efd::Topic * _topic
DDS主题指针。
efd::DataWriter * _datawriter
DDS数据写入器指针。
efd::Publisher * _publisher
DDS发布者指针。
sensor_msgs::msg::CameraInfo _ci
待发布的相机信息数据。
CarlaListener _listener
CARLA监听器实例。
efd::Topic * _topic
Fast-DDS主题指针。
sensor_msgs::msg::Image _image
待发布的图像数据。
efd::TypeSupport _type
Fast-DDS类型支持,用于图像数据。
efd::DataWriter * _datawriter
Fast-DDS数据写入器指针。
efd::DomainParticipant * _participant
Fast-DDS域参与者指针。
CarlaListener _listener
CARLA监听器实例,用于处理回调。
efd::Publisher * _publisher
Fast-DDS发布者指针。
efd::DataWriter * _datawriter
Fast-DDS数据写入器指针。
CarlaListener _listener
CARLA监听器实例,用于处理回调。
efd::TypeSupport _type
Fast-DDS类型支持,用于点云数据。
efd::Publisher * _publisher
Fast-DDS发布者指针。
efd::DomainParticipant * _participant
Fast-DDS域参与者指针。
efd::Topic * _topic
Fast-DDS主题指针。
sensor_msgs::msg::PointCloud2 _pc
待发布的点云数据。