1#define _GLIBCXX_USE_CXX11_ABI 0
11#include <fastdds/dds/domain/DomainParticipant.hpp>
12#include <fastdds/dds/publisher/Publisher.hpp>
13#include <fastdds/dds/topic/Topic.hpp>
14#include <fastdds/dds/publisher/DataWriter.hpp>
15#include <fastdds/dds/topic/TypeSupport.hpp>
17#include <fastdds/dds/domain/qos/DomainParticipantQos.hpp>
18#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
19#include <fastdds/dds/publisher/qos/PublisherQos.hpp>
20#include <fastdds/dds/topic/qos/TopicQos.hpp>
22#include <fastrtps/attributes/ParticipantAttributes.h>
23#include <fastrtps/qos/QosPolicies.h>
25#include <fastdds/dds/publisher/qos/DataWriterQos.hpp>
26#include <fastdds/dds/publisher/DataWriterListener.hpp>
36 namespace efd = eprosima::fastdds::dds;
40 using erc = eprosima::fastrtps::types::ReturnCode_t;
79 struct CarlaCameraInfoPublisherImpl {
91 efd::Topic*
_topic {
nullptr };
156 if (
_impl->_type ==
nullptr) {
157 std::cerr <<
"Invalid TypeSupport" << std::endl;
160 efd::DomainParticipantQos pqos = efd::PARTICIPANT_QOS_DEFAULT;
162 auto factory = efd::DomainParticipantFactory::get_instance();
163 _impl->_participant = factory->create_participant(0, pqos);
164 if (
_impl->_participant ==
nullptr) {
165 std::cerr <<
"Failed to create DomainParticipant" << std::endl;
168 _impl->_type.register_type(
_impl->_participant);
170 efd::PublisherQos pubqos = efd::PUBLISHER_QOS_DEFAULT;
171 _impl->_publisher =
_impl->_participant->create_publisher(pubqos,
nullptr);
172 if (
_impl->_publisher ==
nullptr) {
173 std::cerr <<
"Failed to create Publisher" << std::endl;
177 efd::TopicQos tqos = efd::TOPIC_QOS_DEFAULT;
178 const std::string publisher_type {
"/image"};
179 const std::string base {
"rt/carla/" };
180 std::string topic_name = base;
184 topic_name += publisher_type;
185 _impl->_topic =
_impl->_participant->create_topic(topic_name,
_impl->_type->getName(), tqos);
186 if (
_impl->_topic ==
nullptr) {
187 std::cerr <<
"Failed to create Topic" << std::endl;
191 efd::DataWriterQos wqos = efd::DATAWRITER_QOS_DEFAULT;
192 wqos.endpoint().history_memory_policy = eprosima::fastrtps::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE;
194 _impl->_datawriter =
_impl->_publisher->create_datawriter(
_impl->_topic, wqos, listener);
195 if (
_impl->_datawriter ==
nullptr) {
196 std::cerr <<
"Failed to create DataWriter" << std::endl;
215 std::cerr <<
"Invalid TypeSupport" << std::endl;
221 efd::DomainParticipantQos pqos = efd::PARTICIPANT_QOS_DEFAULT;
226 auto factory = efd::DomainParticipantFactory::get_instance();
227 _impl_info->_participant = factory->create_participant(0, pqos);
232 std::cerr <<
"Failed to create DomainParticipant" << std::endl;
242 efd::PublisherQos pubqos = efd::PUBLISHER_QOS_DEFAULT;
248 std::cerr <<
"Failed to create Publisher" << std::endl;
254 efd::TopicQos tqos = efd::TOPIC_QOS_DEFAULT;
255 const std::string publisher_type {
"/camera_info"};
256 const std::string base {
"rt/carla/" };
257 std::string topic_name = base;
261 topic_name += publisher_type;
270 std::cerr <<
"Failed to create Topic" << std::endl;
276 efd::DataWriterQos wqos = efd::DATAWRITER_QOS_DEFAULT;
283 std::cerr <<
"Failed to create DataWriter" << std::endl;
314 eprosima::fastrtps::rtps::InstanceHandle_t instance_handle;
315 eprosima::fastrtps::types::ReturnCode_t rcode =
_impl->_datawriter->write(&
_impl->_image, instance_handle);
317 if (rcode == erc::ReturnCodeValue::RETCODE_OK) {
320 if (rcode == erc::ReturnCodeValue::RETCODE_ERROR) {
321 std::cerr <<
"RETCODE_ERROR" << std::endl;
324 if (rcode == erc::ReturnCodeValue::RETCODE_UNSUPPORTED) {
325 std::cerr <<
"RETCODE_UNSUPPORTED" << std::endl;
328 if (rcode == erc::ReturnCodeValue::RETCODE_BAD_PARAMETER) {
329 std::cerr <<
"RETCODE_BAD_PARAMETER" << std::endl;
332 if (rcode == erc::ReturnCodeValue::RETCODE_PRECONDITION_NOT_MET) {
333 std::cerr <<
"RETCODE_PRECONDITION_NOT_MET" << std::endl;
336 if (rcode == erc::ReturnCodeValue::RETCODE_OUT_OF_RESOURCES) {
337 std::cerr <<
"RETCODE_OUT_OF_RESOURCES" << std::endl;
340 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ENABLED) {
341 std::cerr <<
"RETCODE_NOT_ENABLED" << std::endl;
344 if (rcode == erc::ReturnCodeValue::RETCODE_IMMUTABLE_POLICY) {
345 std::cerr <<
"RETCODE_IMMUTABLE_POLICY" << std::endl;
348 if (rcode == erc::ReturnCodeValue::RETCODE_INCONSISTENT_POLICY) {
349 std::cerr <<
"RETCODE_INCONSISTENT_POLICY" << std::endl;
352 if (rcode == erc::ReturnCodeValue::RETCODE_ALREADY_DELETED) {
353 std::cerr <<
"RETCODE_ALREADY_DELETED" << std::endl;
356 if (rcode == erc::ReturnCodeValue::RETCODE_TIMEOUT) {
357 std::cerr <<
"RETCODE_TIMEOUT" << std::endl;
360 if (rcode == erc::ReturnCodeValue::RETCODE_NO_DATA) {
361 std::cerr <<
"RETCODE_NO_DATA" << std::endl;
364 if (rcode == erc::ReturnCodeValue::RETCODE_ILLEGAL_OPERATION) {
365 std::cerr <<
"RETCODE_ILLEGAL_OPERATION" << std::endl;
368 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ALLOWED_BY_SECURITY) {
369 std::cerr <<
"RETCODE_NOT_ALLOWED_BY_SECURITY" << std::endl;
372 std::cerr <<
"UNKNOWN" << std::endl;
383 eprosima::fastrtps::rtps::InstanceHandle_t instance_handle;
384 eprosima::fastrtps::types::ReturnCode_t rcode =
_impl_info->_datawriter->write(&
_impl_info->_info, instance_handle);
386 if (rcode == erc::ReturnCodeValue::RETCODE_OK) {
389 if (rcode == erc::ReturnCodeValue::RETCODE_ERROR) {
390 std::cerr <<
"RETCODE_ERROR" << std::endl;
393 if (rcode == erc::ReturnCodeValue::RETCODE_UNSUPPORTED) {
394 std::cerr <<
"RETCODE_UNSUPPORTED" << std::endl;
397 if (rcode == erc::ReturnCodeValue::RETCODE_BAD_PARAMETER) {
398 std::cerr <<
"RETCODE_BAD_PARAMETER" << std::endl;
401 if (rcode == erc::ReturnCodeValue::RETCODE_PRECONDITION_NOT_MET) {
402 std::cerr <<
"RETCODE_PRECONDITION_NOT_MET" << std::endl;
405 if (rcode == erc::ReturnCodeValue::RETCODE_OUT_OF_RESOURCES) {
406 std::cerr <<
"RETCODE_OUT_OF_RESOURCES" << std::endl;
409 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ENABLED) {
410 std::cerr <<
"RETCODE_NOT_ENABLED" << std::endl;
413 if (rcode == erc::ReturnCodeValue::RETCODE_IMMUTABLE_POLICY) {
414 std::cerr <<
"RETCODE_IMMUTABLE_POLICY" << std::endl;
417 if (rcode == erc::ReturnCodeValue::RETCODE_INCONSISTENT_POLICY) {
418 std::cerr <<
"RETCODE_INCONSISTENT_POLICY" << std::endl;
421 if (rcode == erc::ReturnCodeValue::RETCODE_ALREADY_DELETED) {
422 std::cerr <<
"RETCODE_ALREADY_DELETED" << std::endl;
425 if (rcode == erc::ReturnCodeValue::RETCODE_TIMEOUT) {
426 std::cerr <<
"RETCODE_TIMEOUT" << std::endl;
429 if (rcode == erc::ReturnCodeValue::RETCODE_NO_DATA) {
430 std::cerr <<
"RETCODE_NO_DATA" << std::endl;
433 if (rcode == erc::ReturnCodeValue::RETCODE_ILLEGAL_OPERATION) {
434 std::cerr <<
"RETCODE_ILLEGAL_OPERATION" << std::endl;
437 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ALLOWED_BY_SECURITY) {
438 std::cerr <<
"RETCODE_NOT_ALLOWED_BY_SECURITY" << std::endl;
441 std::cerr <<
"UNKNOWN" << std::endl;
457 std::vector<uint8_t> vector_data;
458 const size_t size = height * width * 4;
459 vector_data.resize(size);
460 std::memcpy(&vector_data[0], &data[0], size);
461 SetData(seconds, nanoseconds, height, width, std::move(vector_data));
499 std_msgs::msg::Header header;
500 header.stamp(std::move(time));
503 _impl->_image.header(std::move(header));
504 _impl->_image.width(width);
505 _impl->_image.height(height);
506 _impl->_image.encoding(
"bgra8");
507 _impl->_image.is_bigendian(0);
508 _impl->_image.step(
_impl->_image.width() *
sizeof(uint8_t) * 4);
509 _impl->_image.data(std::move(data));
524 std_msgs::msg::Header header;
525 header.stamp(std::move(time));
552 if (
_impl->_datawriter)
553 _impl->_publisher->delete_datawriter(
_impl->_datawriter);
555 if (
_impl->_publisher)
556 _impl->_participant->delete_publisher(
_impl->_publisher);
559 _impl->_participant->delete_topic(
_impl->_topic);
561 if (
_impl->_participant)
562 efd::DomainParticipantFactory::get_instance()->delete_participant(
_impl->_participant);
577 efd::DomainParticipantFactory::get_instance()->delete_participant(
_impl_info->_participant);
620 _name = std::move(other._name);
621 _parent = std::move(other._parent);
622 _impl = std::move(other._impl);
635 _name = std::move(other._name);
636 _parent = std::move(other._parent);
637 _impl = std::move(other._impl);
eProsima_user_DllExport void nanosec(uint32_t _nanosec)
此函数设置成员nanosec的值。
eProsima_user_DllExport void sec(int32_t _sec)
此函数设置成员sec的值。
const std::string & parent() const
bool HasBeenInitialized() const
检查CarlaSSCameraPublisher是否已初始化。
std::shared_ptr< CarlaSSCameraPublisherImpl > _impl
CarlaSSCameraPublisher & operator=(const CarlaSSCameraPublisher &)
赋值运算符重载
CarlaSSCameraPublisher(const char *ros_name="", const char *parent="")
CarlaSSCameraPublisher类的构造函数
void SetCameraInfoData(int32_t seconds, uint32_t nanoseconds)
设置相机信息数据的时间戳
void SetInfoRegionOfInterest(uint32_t x_offset, uint32_t y_offset, uint32_t height, uint32_t width, bool do_rectify)
设置感兴趣区域的信息
void SetImageData(int32_t seconds, uint32_t nanoseconds, size_t height, size_t width, const uint8_t *data)
设置图像数据
std::shared_ptr< CarlaCameraInfoPublisherImpl > _impl_info
bool InitInfo()
初始化CarlaSSCameraPublisher的信息
bool PublishImage()
发布图像数据
void InitInfoData(uint32_t x_offset, uint32_t y_offset, uint32_t height, uint32_t width, float fov, bool do_rectify)
初始化相机信息数据
void SetData(int32_t seconds, uint32_t nanoseconds, size_t height, size_t width, std::vector< uint8_t > &&data)
设置图像数据
~CarlaSSCameraPublisher()
CarlaSSCameraPublisher类的析构函数
此类表示用户在 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 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
sensor_msgs::msg::CameraInfo _info
相机信息消息实例。
efd::DomainParticipant * _participant
DDS域参与者指针。
efd::TypeSupport _type
DDS类型支持,用于相机信息消息。
efd::Topic * _topic
DDS主题指针。
efd::DataWriter * _datawriter
DDS数据写入器指针。
efd::Publisher * _publisher
DDS发布者指针。
CarlaListener _listener
CARLA监听器实例。
CarlaSSCameraPublisher的内部实现结构体,包含Fast-DDS相关的资源和配置。
efd::Topic * _topic
Fast-DDS的Topic指针,定义了数据的主题。
efd::DomainParticipant * _participant
Fast-DDS的DomainParticipant指针,用于管理RTPS实体。
efd::DataWriter * _datawriter
Fast-DDS的DataWriter指针,用于写入数据到指定的主题。
sensor_msgs::msg::Image _image
存储的图像数据。
efd::Publisher * _publisher
Fast-DDS的Publisher指针,用于发布数据。
CarlaListener _listener
CarlaListener对象,用于监听CARLA的事件。
efd::TypeSupport _type
Fast-DDS的TypeSupport对象,支持特定类型的数据(这里是sensor_msgs::msg::Image)。