1#define _GLIBCXX_USE_CXX11_ABI 0
12#include <fastdds/dds/domain/DomainParticipant.hpp>
13#include <fastdds/dds/publisher/Publisher.hpp>
14#include <fastdds/dds/topic/Topic.hpp>
15#include <fastdds/dds/publisher/DataWriter.hpp>
16#include <fastdds/dds/topic/TypeSupport.hpp>
18#include <fastdds/dds/domain/qos/DomainParticipantQos.hpp>
19#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
20#include <fastdds/dds/publisher/qos/PublisherQos.hpp>
21#include <fastdds/dds/topic/qos/TopicQos.hpp>
23#include <fastrtps/attributes/ParticipantAttributes.h>
24#include <fastrtps/qos/QosPolicies.h>
25#include <fastdds/dds/publisher/qos/DataWriterQos.hpp>
26#include <fastdds/dds/publisher/DataWriterListener.hpp>
39 namespace efd = eprosima::fastdds::dds;
43 using erc = eprosima::fastrtps::types::ReturnCode_t;
157 if (
_impl->_type ==
nullptr) {
161 std::cerr <<
"Invalid TypeSupport" << std::endl;
165 efd::DomainParticipantQos pqos = efd::PARTICIPANT_QOS_DEFAULT;
167 auto factory = efd::DomainParticipantFactory::get_instance();
168 _impl->_participant = factory->create_participant(0, pqos);
169 if (
_impl->_participant ==
nullptr) {
170 std::cerr <<
"Failed to create DomainParticipant" << std::endl;
174 _impl->_type.register_type(
_impl->_participant);
176 efd::PublisherQos pubqos = efd::PUBLISHER_QOS_DEFAULT;
177 _impl->_publisher =
_impl->_participant->create_publisher(pubqos,
nullptr);
178 if (
_impl->_publisher ==
nullptr) {
179 std::cerr <<
"Failed to create Publisher" << std::endl;
183 efd::TopicQos tqos = efd::TOPIC_QOS_DEFAULT;
184 const std::string publisher_type {
"/image"};
185 const std::string base {
"rt/carla/" };
186 std::string topic_name = base;
190 topic_name += publisher_type;
191 _impl->_topic =
_impl->_participant->create_topic(topic_name,
_impl->_type->getName(), tqos);
192 if (
_impl->_topic ==
nullptr) {
193 std::cerr <<
"Failed to create Topic" << std::endl;
197 efd::DataWriterQos wqos = efd::DATAWRITER_QOS_DEFAULT;
198 wqos.endpoint().history_memory_policy = eprosima::fastrtps::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE;
200 _impl->_datawriter =
_impl->_publisher->create_datawriter(
_impl->_topic, wqos, listener);
201 if (
_impl->_datawriter ==
nullptr) {
202 std::cerr <<
"Failed to create DataWriter" << std::endl;
219 std::cerr <<
"Invalid TypeSupport" << std::endl;
223 efd::DomainParticipantQos pqos = efd::PARTICIPANT_QOS_DEFAULT;
225 auto factory = efd::DomainParticipantFactory::get_instance();
226 _impl_info->_participant = factory->create_participant(0, pqos);
228 std::cerr <<
"Failed to create DomainParticipant" << std::endl;
234 efd::PublisherQos pubqos = efd::PUBLISHER_QOS_DEFAULT;
237 std::cerr <<
"Failed to create Publisher" << std::endl;
241 efd::TopicQos tqos = efd::TOPIC_QOS_DEFAULT;
242 const std::string publisher_type {
"/camera_info"};
243 const std::string base {
"rt/carla/" };
244 std::string topic_name = base;
248 topic_name += publisher_type;
251 std::cerr <<
"Failed to create Topic" << std::endl;
255 efd::DataWriterQos wqos = efd::DATAWRITER_QOS_DEFAULT;
259 std::cerr <<
"Failed to create DataWriter" << std::endl;
288 eprosima::fastrtps::rtps::InstanceHandle_t instance_handle;
291 eprosima::fastrtps::types::ReturnCode_t rcode =
_impl->_datawriter->write(&
_impl->_image, instance_handle);
293 if (rcode == erc::ReturnCodeValue::RETCODE_OK) {
297 if (rcode == erc::ReturnCodeValue::RETCODE_ERROR) {
299 std::cerr <<
"RETCODE_ERROR" << std::endl;
302 if (rcode == erc::ReturnCodeValue::RETCODE_UNSUPPORTED) {
304 std::cerr <<
"RETCODE_UNSUPPORTED" << std::endl;
307 if (rcode == erc::ReturnCodeValue::RETCODE_BAD_PARAMETER) {
309 std::cerr <<
"RETCODE_BAD_PARAMETER" << std::endl;
312 if (rcode == erc::ReturnCodeValue::RETCODE_PRECONDITION_NOT_MET) {
314 std::cerr <<
"RETCODE_PRECONDITION_NOT_MET" << std::endl;
317 if (rcode == erc::ReturnCodeValue::RETCODE_OUT_OF_RESOURCES) {
319 std::cerr <<
"RETCODE_OUT_OF_RESOURCES" << std::endl;
322 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ENABLED) {
324 std::cerr <<
"RETCODE_NOT_ENABLED" << std::endl;
327 if (rcode == erc::ReturnCodeValue::RETCODE_IMMUTABLE_POLICY) {
329 std::cerr <<
"RETCODE_IMMUTABLE_POLICY" << std::endl;
332 if (rcode == erc::ReturnCodeValue::RETCODE_INCONSISTENT_POLICY) {
334 std::cerr <<
"RETCODE_INCONSISTENT_POLICY" << std::endl;
337 if (rcode == erc::ReturnCodeValue::RETCODE_ALREADY_DELETED) {
339 std::cerr <<
"RETCODE_ALREADY_DELETED" << std::endl;
342 if (rcode == erc::ReturnCodeValue::RETCODE_TIMEOUT) {
344 std::cerr <<
"RETCODE_TIMEOUT" << std::endl;
347 if (rcode == erc::ReturnCodeValue::RETCODE_NO_DATA) {
349 std::cerr <<
"RETCODE_NO_DATA" << std::endl;
352 if (rcode == erc::ReturnCodeValue::RETCODE_ILLEGAL_OPERATION) {
354 std::cerr <<
"RETCODE_ILLEGAL_OPERATION" << std::endl;
357 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ALLOWED_BY_SECURITY) {
359 std::cerr <<
"RETCODE_NOT_ALLOWED_BY_SECURITY" << std::endl;
363 std::cerr <<
"UNKNOWN" << std::endl;
376 eprosima::fastrtps::rtps::InstanceHandle_t instance_handle;
378 eprosima::fastrtps::types::ReturnCode_t rcode =
_impl_info->_datawriter->write(&
_impl_info->_info, instance_handle);
380 if (rcode == erc::ReturnCodeValue::RETCODE_OK) {
384 if (rcode == erc::ReturnCodeValue::RETCODE_ERROR) {
386 std::cerr <<
"RETCODE_ERROR" << std::endl;
389 if (rcode == erc::ReturnCodeValue::RETCODE_UNSUPPORTED) {
391 std::cerr <<
"RETCODE_UNSUPPORTED" << std::endl;
394 if (rcode == erc::ReturnCodeValue::RETCODE_BAD_PARAMETER) {
396 std::cerr <<
"RETCODE_BAD_PARAMETER" << std::endl;
399 if (rcode == erc::ReturnCodeValue::RETCODE_PRECONDITION_NOT_MET) {
401 std::cerr <<
"RETCODE_PRECONDITION_NOT_MET" << std::endl;
404 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) {
411 std::cerr <<
"RETCODE_NOT_ENABLED" << std::endl;
414 if (rcode == erc::ReturnCodeValue::RETCODE_IMMUTABLE_POLICY) {
416 std::cerr <<
"RETCODE_IMMUTABLE_POLICY" << std::endl;
419 if (rcode == erc::ReturnCodeValue::RETCODE_INCONSISTENT_POLICY) {
421 std::cerr <<
"RETCODE_INCONSISTENT_POLICY" << std::endl;
424 if (rcode == erc::ReturnCodeValue::RETCODE_ALREADY_DELETED) {
426 std::cerr <<
"RETCODE_ALREADY_DELETED" << std::endl;
429 if (rcode == erc::ReturnCodeValue::RETCODE_TIMEOUT) {
431 std::cerr <<
"RETCODE_TIMEOUT" << std::endl;
434 if (rcode == erc::ReturnCodeValue::RETCODE_NO_DATA) {
436 std::cerr <<
"RETCODE_NO_DATA" << std::endl;
439 if (rcode == erc::ReturnCodeValue::RETCODE_ILLEGAL_OPERATION) {
441 std::cerr <<
"RETCODE_ILLEGAL_OPERATION" << std::endl;
444 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ALLOWED_BY_SECURITY) {
446 std::cerr <<
"RETCODE_NOT_ALLOWED_BY_SECURITY" << std::endl;
450 std::cerr <<
"UNKNOWN" << std::endl;
465 const size_t size = height * width * 4;
466 vector_data.resize(size);
467 std::memcpy(&vector_data[0], &data[0], size);
468 SetData(seconds, nanoseconds,height, width, std::move(vector_data));
506 std_msgs::msg::Header header;
507 header.stamp(std::move(time));
510 _impl->_image.header(std::move(header));
511 _impl->_image.width(width);
512 _impl->_image.height(height);
513 _impl->_image.encoding(
"bgra8");
514 _impl->_image.is_bigendian(0);
515 _impl->_image.step(
_impl->_image.width() *
sizeof(uint8_t) * 4);
516 _impl->_image.data(std::move(data));
531 std_msgs::msg::Header header;
532 header.stamp(std::move(time));
559 if (
_impl->_datawriter)
560 _impl->_publisher->delete_datawriter(
_impl->_datawriter);
562 if (
_impl->_publisher)
563 _impl->_participant->delete_publisher(
_impl->_publisher);
566 _impl->_participant->delete_topic(
_impl->_topic);
568 if (
_impl->_participant)
569 efd::DomainParticipantFactory::get_instance()->delete_participant(
_impl->_participant);
584 efd::DomainParticipantFactory::get_instance()->delete_participant(
_impl_info->_participant);
626 _name = std::move(other._name);
627 _parent = std::move(other._parent);
628 _impl = std::move(other._impl);
642 _name = std::move(other._name);
643 _parent = std::move(other._parent);
644 _impl = std::move(other._impl);
eProsima_user_DllExport void nanosec(uint32_t _nanosec)
此函数设置成员nanosec的值。
eProsima_user_DllExport void sec(int32_t _sec)
此函数设置成员sec的值。
用于在ROS 2中发布CARLA深度相机数据的类。 这个类继承自CarlaPublisher,专门用于初始化、设置和发布深度相机数据。
bool Publish()
发布深度图像和相机信息。
bool InitInfo()
初始化相机信息发布者的DDS相关组件。
bool PublishInfo()
发布深度相机信息
~CarlaDepthCameraPublisher()
析构函数,用于销毁CarlaDepthCameraPublisher对象。
CarlaDepthCameraPublisher & operator=(const CarlaDepthCameraPublisher &)
拷贝赋值运算符。
void SetInfoRegionOfInterest(uint32_t x_offset, uint32_t y_offset, uint32_t height, uint32_t width, bool do_rectify)
设置感兴趣区域(ROI)信息
CarlaDepthCameraPublisher(const char *ros_name="", const char *parent="")
构造函数,用于创建CarlaDepthCameraPublisher对象。
bool InitImage()
初始化深度图像数据。
void SetImageData(int32_t seconds, uint32_t nanoseconds, size_t height, size_t width, const uint8_t *data)
设置图像数据
bool Init()
初始化函数,用于设置ROS节点和发布者。
void SetData(int32_t seconds, uint32_t nanoseconds, size_t height, size_t width, std::vector< uint8_t > &&data)
设置图像数据及其元数据
std::shared_ptr< CarlaDepthCameraPublisherImpl > _impl
bool PublishImage()
发布深度图像
void InitInfoData(uint32_t x_offset, uint32_t y_offset, uint32_t height, uint32_t width, float fov, bool do_rectify)
初始化深度相机信息数据。
std::shared_ptr< CarlaCameraInfoPublisherImpl > _impl_info
bool HasBeenInitialized() const
检查深度相机发布者是否已初始化。
void SetCameraInfoData(int32_t seconds, uint32_t nanoseconds)
设置相机信息数据的时间戳
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 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监听器实例。
efd::TypeSupport _type
DDS类型支持,用于深度图像消息。
efd::Topic * _topic
DDS主题指针。
efd::DataWriter * _datawriter
DDS数据写入器指针。
efd::Publisher * _publisher
DDS发布者指针。
sensor_msgs::msg::Image _image
深度图像消息实例。
efd::DomainParticipant * _participant
DDS域参与者指针。
CarlaListener _listener
CARLA监听器实例。