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>
19#include <fastdds/dds/domain/qos/DomainParticipantQos.hpp>
20#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
21#include <fastdds/dds/publisher/qos/PublisherQos.hpp>
22#include <fastdds/dds/topic/qos/TopicQos.hpp>
25#include <fastrtps/attributes/ParticipantAttributes.h>
26#include <fastrtps/qos/QosPolicies.h>
27#include <fastdds/dds/publisher/qos/DataWriterQos.hpp>
28#include <fastdds/dds/publisher/DataWriterListener.hpp>
35 namespace efd = eprosima::fastdds::dds;
36 using erc = eprosima::fastrtps::types::ReturnCode_t;
50 struct CarlaCameraInfoPublisherImpl {
53 efd::Topic*
_topic {
nullptr };
85 if (
_impl->_type ==
nullptr) {
86 std::cerr <<
"Invalid TypeSupport" << std::endl;
90 efd::DomainParticipantQos pqos = efd::PARTICIPANT_QOS_DEFAULT;
92 auto factory = efd::DomainParticipantFactory::get_instance();
93 _impl->_participant = factory->create_participant(0, pqos);
94 if (
_impl->_participant ==
nullptr) {
95 std::cerr <<
"Failed to create DomainParticipant" << std::endl;
98 _impl->_type.register_type(
_impl->_participant);
100 efd::PublisherQos pubqos = efd::PUBLISHER_QOS_DEFAULT;
101 _impl->_publisher =
_impl->_participant->create_publisher(pubqos,
nullptr);
102 if (
_impl->_publisher ==
nullptr) {
103 std::cerr <<
"Failed to create Publisher" << std::endl;
107 efd::TopicQos tqos = efd::TOPIC_QOS_DEFAULT;
108 const std::string publisher_type {
"/image"};
109 const std::string base {
"rt/carla/" };
110 std::string topic_name = base;
114 topic_name += publisher_type;
115 _impl->_topic =
_impl->_participant->create_topic(topic_name,
_impl->_type->getName(), tqos);
116 if (
_impl->_topic ==
nullptr) {
117 std::cerr <<
"Failed to create Topic" << std::endl;
120 efd::DataWriterQos wqos = efd::DATAWRITER_QOS_DEFAULT;
121 wqos.endpoint().history_memory_policy = eprosima::fastrtps::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE;
123 _impl->_datawriter =
_impl->_publisher->create_datawriter(
_impl->_topic, wqos, listener);
124 if (
_impl->_datawriter ==
nullptr) {
125 std::cerr <<
"Failed to create DataWriter" << std::endl;
136 std::cerr <<
"Invalid TypeSupport" << std::endl;
140 efd::DomainParticipantQos pqos = efd::PARTICIPANT_QOS_DEFAULT;
142 auto factory = efd::DomainParticipantFactory::get_instance();
143 _impl_info->_participant = factory->create_participant(0, pqos);
145 std::cerr <<
"Failed to create DomainParticipant" << std::endl;
150 efd::PublisherQos pubqos = efd::PUBLISHER_QOS_DEFAULT;
153 std::cerr <<
"Failed to create Publisher" << std::endl;
157 efd::TopicQos tqos = efd::TOPIC_QOS_DEFAULT;
158 const std::string publisher_type {
"/camera_info"};
159 const std::string base {
"rt/carla/" };
160 std::string topic_name = base;
164 topic_name += publisher_type;
167 std::cerr <<
"Failed to create Topic" << std::endl;
170 efd::DataWriterQos wqos = efd::DATAWRITER_QOS_DEFAULT;
174 std::cerr <<
"Failed to create DataWriter" << std::endl;
190 eprosima::fastrtps::rtps::InstanceHandle_t instance_handle;
191 eprosima::fastrtps::types::ReturnCode_t rcode =
_impl->_datawriter->write(&
_impl->_image, instance_handle);
192 if (rcode == erc::ReturnCodeValue::RETCODE_OK) {
195 if (rcode == erc::ReturnCodeValue::RETCODE_ERROR) {
196 std::cerr <<
"RETCODE_ERROR" << std::endl;
199 if (rcode == erc::ReturnCodeValue::RETCODE_UNSUPPORTED) {
200 std::cerr <<
"RETCODE_UNSUPPORTED" << std::endl;
203 if (rcode == erc::ReturnCodeValue::RETCODE_BAD_PARAMETER) {
204 std::cerr <<
"RETCODE_BAD_PARAMETER" << std::endl;
207 if (rcode == erc::ReturnCodeValue::RETCODE_PRECONDITION_NOT_MET) {
208 std::cerr <<
"RETCODE_PRECONDITION_NOT_MET" << std::endl;
211 if (rcode == erc::ReturnCodeValue::RETCODE_OUT_OF_RESOURCES) {
212 std::cerr <<
"RETCODE_OUT_OF_RESOURCES" << std::endl;
215 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ENABLED) {
216 std::cerr <<
"RETCODE_NOT_ENABLED" << std::endl;
219 if (rcode == erc::ReturnCodeValue::RETCODE_IMMUTABLE_POLICY) {
220 std::cerr <<
"RETCODE_IMMUTABLE_POLICY" << std::endl;
223 if (rcode == erc::ReturnCodeValue::RETCODE_INCONSISTENT_POLICY) {
224 std::cerr <<
"RETCODE_INCONSISTENT_POLICY" << std::endl;
227 if (rcode == erc::ReturnCodeValue::RETCODE_ALREADY_DELETED) {
228 std::cerr <<
"RETCODE_ALREADY_DELETED" << std::endl;
231 if (rcode == erc::ReturnCodeValue::RETCODE_TIMEOUT) {
232 std::cerr <<
"RETCODE_TIMEOUT" << std::endl;
235 if (rcode == erc::ReturnCodeValue::RETCODE_NO_DATA) {
236 std::cerr <<
"RETCODE_NO_DATA" << std::endl;
239 if (rcode == erc::ReturnCodeValue::RETCODE_ILLEGAL_OPERATION) {
240 std::cerr <<
"RETCODE_ILLEGAL_OPERATION" << std::endl;
243 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ALLOWED_BY_SECURITY) {
244 std::cerr <<
"RETCODE_NOT_ALLOWED_BY_SECURITY" << std::endl;
247 std::cerr <<
"UNKNOWN" << std::endl;
252 eprosima::fastrtps::rtps::InstanceHandle_t instance_handle;
254 eprosima::fastrtps::types::ReturnCode_t rcode =
_impl_info->_datawriter->write(&
_impl_info->_info, instance_handle);
255 if (rcode == erc::ReturnCodeValue::RETCODE_OK) {
258 if (rcode == erc::ReturnCodeValue::RETCODE_ERROR) {
259 std::cerr <<
"RETCODE_ERROR" << std::endl;
262 if (rcode == erc::ReturnCodeValue::RETCODE_UNSUPPORTED) {
263 std::cerr <<
"RETCODE_UNSUPPORTED" << std::endl;
266 if (rcode == erc::ReturnCodeValue::RETCODE_BAD_PARAMETER) {
267 std::cerr <<
"RETCODE_BAD_PARAMETER" << std::endl;
270 if (rcode == erc::ReturnCodeValue::RETCODE_PRECONDITION_NOT_MET) {
271 std::cerr <<
"RETCODE_PRECONDITION_NOT_MET" << std::endl;
274 if (rcode == erc::ReturnCodeValue::RETCODE_OUT_OF_RESOURCES) {
275 std::cerr <<
"RETCODE_OUT_OF_RESOURCES" << std::endl;
278 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ENABLED) {
279 std::cerr <<
"RETCODE_NOT_ENABLED" << std::endl;
282 if (rcode == erc::ReturnCodeValue::RETCODE_IMMUTABLE_POLICY) {
283 std::cerr <<
"RETCODE_IMMUTABLE_POLICY" << std::endl;
286 if (rcode == erc::ReturnCodeValue::RETCODE_INCONSISTENT_POLICY) {
287 std::cerr <<
"RETCODE_INCONSISTENT_POLICY" << std::endl;
290 if (rcode == erc::ReturnCodeValue::RETCODE_ALREADY_DELETED) {
291 std::cerr <<
"RETCODE_ALREADY_DELETED" << std::endl;
294 if (rcode == erc::ReturnCodeValue::RETCODE_TIMEOUT) {
295 std::cerr <<
"RETCODE_TIMEOUT" << std::endl;
298 if (rcode == erc::ReturnCodeValue::RETCODE_NO_DATA) {
299 std::cerr <<
"RETCODE_NO_DATA" << std::endl;
302 if (rcode == erc::ReturnCodeValue::RETCODE_ILLEGAL_OPERATION) {
303 std::cerr <<
"RETCODE_ILLEGAL_OPERATION" << std::endl;
306 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ALLOWED_BY_SECURITY) {
307 std::cerr <<
"RETCODE_NOT_ALLOWED_BY_SECURITY" << std::endl;
310 std::cerr <<
"UNKNOWN" << std::endl;
315 std::vector<uint8_t> vector_data;
316 const size_t size = height * width * 4;
317 vector_data.resize(size);
318 std::memcpy(&vector_data[0], &data[0], size);
319 SetImageData(seconds, nanoseconds, height, width, std::move(vector_data));
328 std_msgs::msg::Header header;
329 header.stamp(std::move(time));
331 _impl->_image.header(header);
333 _impl->_image.width(width);
334 _impl->_image.height(height);
335 _impl->_image.encoding(
"bgra8");
336 _impl->_image.is_bigendian(0);
337 _impl->_image.step(
_impl->_image.width() *
sizeof(uint8_t) * 4);
338 _impl->_image.data(std::move(data));
346 std_msgs::msg::Header header;
347 header.stamp(std::move(time));
373 if (
_impl->_datawriter)
374 _impl->_publisher->delete_datawriter(
_impl->_datawriter);
376 if (
_impl->_publisher)
377 _impl->_participant->delete_publisher(
_impl->_publisher);
380 _impl->_participant->delete_topic(
_impl->_topic);
382 if (
_impl->_participant)
383 efd::DomainParticipantFactory::get_instance()->delete_participant(
_impl->_participant);
398 efd::DomainParticipantFactory::get_instance()->delete_participant(
_impl_info->_participant);
421 _name = std::move(other._name);
422 _parent = std::move(other._parent);
423 _impl = std::move(other._impl);
429 _name = std::move(other._name);
430 _parent = std::move(other._parent);
431 _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
std::shared_ptr< CarlaCameraInfoPublisherImpl > _impl_info
std::shared_ptr< CarlaRGBCameraPublisherImpl > _impl
void SetImageData(int32_t seconds, uint32_t nanoseconds, uint32_t height, uint32_t width, const uint8_t *data)
void SetInfoRegionOfInterest(uint32_t x_offset, uint32_t y_offset, uint32_t height, uint32_t width, bool do_rectify)
~CarlaRGBCameraPublisher()
CarlaRGBCameraPublisher(const char *ros_name="", const char *parent="")
void InitInfoData(uint32_t x_offset, uint32_t y_offset, uint32_t height, uint32_t width, float fov, bool do_rectify)
移动赋值运算符重载函数,类似移动构造函数的作用,不过是用于赋值操作时高效地转移资源所有权 CarlaRGBCameraPublisher & operator=(CarlaRGBCameraPublisher &&)
void SetCameraInfoData(int32_t seconds, uint32_t nanoseconds)
bool HasBeenInitialized() 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监听器实例。
sensor_msgs::msg::Image _image
efd::DataWriter * _datawriter
efd::Publisher * _publisher
efd::DomainParticipant * _participant