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>
24#include <fastdds/dds/publisher/qos/DataWriterQos.hpp>
25#include <fastdds/dds/publisher/DataWriterListener.hpp>
30 namespace efd = eprosima::fastdds::dds;
31 using erc = eprosima::fastrtps::types::ReturnCode_t;
43 struct CarlaCameraInfoPublisherImpl {
46 efd::Topic*
_topic {
nullptr };
69 if (
_impl->_type ==
nullptr) {
70 std::cerr <<
"Invalid TypeSupport" << std::endl;
73 efd::DomainParticipantQos pqos = efd::PARTICIPANT_QOS_DEFAULT;
75 auto factory = efd::DomainParticipantFactory::get_instance();
76 _impl->_participant = factory->create_participant(0, pqos);
77 if (
_impl->_participant ==
nullptr) {
78 std::cerr <<
"Failed to create DomainParticipant" << std::endl;
81 _impl->_type.register_type(
_impl->_participant);
83 efd::PublisherQos pubqos = efd::PUBLISHER_QOS_DEFAULT;
84 _impl->_publisher =
_impl->_participant->create_publisher(pubqos,
nullptr);
85 if (
_impl->_publisher ==
nullptr) {
86 std::cerr <<
"Failed to create Publisher" << std::endl;
90 efd::TopicQos tqos = efd::TOPIC_QOS_DEFAULT;
91 const std::string publisher_type {
"/image"};
92 const std::string base {
"rt/carla/" };
93 std::string topic_name = base;
97 topic_name += publisher_type;
98 _impl->_topic =
_impl->_participant->create_topic(topic_name,
_impl->_type->getName(), tqos);
99 if (
_impl->_topic ==
nullptr) {
100 std::cerr <<
"Failed to create Topic" << std::endl;
104 efd::DataWriterQos wqos = efd::DATAWRITER_QOS_DEFAULT;
105 wqos.endpoint().history_memory_policy = eprosima::fastrtps::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE;
107 _impl->_datawriter =
_impl->_publisher->create_datawriter(
_impl->_topic, wqos, listener);
108 if (
_impl->_datawriter ==
nullptr) {
109 std::cerr <<
"Failed to create DataWriter" << std::endl;
118 std::cerr <<
"Invalid TypeSupport" << std::endl;
122 efd::DomainParticipantQos pqos = efd::PARTICIPANT_QOS_DEFAULT;
124 auto factory = efd::DomainParticipantFactory::get_instance();
125 _impl_info->_participant = factory->create_participant(0, pqos);
127 std::cerr <<
"Failed to create DomainParticipant" << std::endl;
132 efd::PublisherQos pubqos = efd::PUBLISHER_QOS_DEFAULT;
135 std::cerr <<
"Failed to create Publisher" << std::endl;
139 efd::TopicQos tqos = efd::TOPIC_QOS_DEFAULT;
140 const std::string publisher_type {
"/camera_info"};
141 const std::string base {
"rt/carla/" };
142 std::string topic_name = base;
146 topic_name += publisher_type;
149 std::cerr <<
"Failed to create Topic" << std::endl;
152 efd::DataWriterQos wqos = efd::DATAWRITER_QOS_DEFAULT;
156 std::cerr <<
"Failed to create DataWriter" << std::endl;
169 eprosima::fastrtps::rtps::InstanceHandle_t instance_handle;
170 eprosima::fastrtps::types::ReturnCode_t rcode =
_impl->_datawriter->write(&
_impl->_image, instance_handle);
171 if (rcode == erc::ReturnCodeValue::RETCODE_OK) {
174 if (rcode == erc::ReturnCodeValue::RETCODE_ERROR) {
175 std::cerr <<
"RETCODE_ERROR" << std::endl;
178 if (rcode == erc::ReturnCodeValue::RETCODE_UNSUPPORTED) {
179 std::cerr <<
"RETCODE_UNSUPPORTED" << std::endl;
182 if (rcode == erc::ReturnCodeValue::RETCODE_BAD_PARAMETER) {
183 std::cerr <<
"RETCODE_BAD_PARAMETER" << std::endl;
186 if (rcode == erc::ReturnCodeValue::RETCODE_PRECONDITION_NOT_MET) {
187 std::cerr <<
"RETCODE_PRECONDITION_NOT_MET" << std::endl;
190 if (rcode == erc::ReturnCodeValue::RETCODE_OUT_OF_RESOURCES) {
191 std::cerr <<
"RETCODE_OUT_OF_RESOURCES" << std::endl;
194 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ENABLED) {
195 std::cerr <<
"RETCODE_NOT_ENABLED" << std::endl;
198 if (rcode == erc::ReturnCodeValue::RETCODE_IMMUTABLE_POLICY) {
199 std::cerr <<
"RETCODE_IMMUTABLE_POLICY" << std::endl;
202 if (rcode == erc::ReturnCodeValue::RETCODE_INCONSISTENT_POLICY) {
203 std::cerr <<
"RETCODE_INCONSISTENT_POLICY" << std::endl;
206 if (rcode == erc::ReturnCodeValue::RETCODE_ALREADY_DELETED) {
207 std::cerr <<
"RETCODE_ALREADY_DELETED" << std::endl;
210 if (rcode == erc::ReturnCodeValue::RETCODE_TIMEOUT) {
211 std::cerr <<
"RETCODE_TIMEOUT" << std::endl;
214 if (rcode == erc::ReturnCodeValue::RETCODE_NO_DATA) {
215 std::cerr <<
"RETCODE_NO_DATA" << std::endl;
218 if (rcode == erc::ReturnCodeValue::RETCODE_ILLEGAL_OPERATION) {
219 std::cerr <<
"RETCODE_ILLEGAL_OPERATION" << std::endl;
222 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ALLOWED_BY_SECURITY) {
223 std::cerr <<
"RETCODE_NOT_ALLOWED_BY_SECURITY" << std::endl;
226 std::cerr <<
"UNKNOWN" << std::endl;
231 eprosima::fastrtps::rtps::InstanceHandle_t instance_handle;
232 eprosima::fastrtps::types::ReturnCode_t rcode =
_impl_info->_datawriter->write(&
_impl_info->_info, instance_handle);
233 if (rcode == erc::ReturnCodeValue::RETCODE_OK) {
236 if (rcode == erc::ReturnCodeValue::RETCODE_ERROR) {
237 std::cerr <<
"RETCODE_ERROR" << std::endl;
240 if (rcode == erc::ReturnCodeValue::RETCODE_UNSUPPORTED) {
241 std::cerr <<
"RETCODE_UNSUPPORTED" << std::endl;
244 if (rcode == erc::ReturnCodeValue::RETCODE_BAD_PARAMETER) {
245 std::cerr <<
"RETCODE_BAD_PARAMETER" << std::endl;
248 if (rcode == erc::ReturnCodeValue::RETCODE_PRECONDITION_NOT_MET) {
249 std::cerr <<
"RETCODE_PRECONDITION_NOT_MET" << std::endl;
252 if (rcode == erc::ReturnCodeValue::RETCODE_OUT_OF_RESOURCES) {
253 std::cerr <<
"RETCODE_OUT_OF_RESOURCES" << std::endl;
256 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ENABLED) {
257 std::cerr <<
"RETCODE_NOT_ENABLED" << std::endl;
260 if (rcode == erc::ReturnCodeValue::RETCODE_IMMUTABLE_POLICY) {
261 std::cerr <<
"RETCODE_IMMUTABLE_POLICY" << std::endl;
264 if (rcode == erc::ReturnCodeValue::RETCODE_INCONSISTENT_POLICY) {
265 std::cerr <<
"RETCODE_INCONSISTENT_POLICY" << std::endl;
268 if (rcode == erc::ReturnCodeValue::RETCODE_ALREADY_DELETED) {
269 std::cerr <<
"RETCODE_ALREADY_DELETED" << std::endl;
272 if (rcode == erc::ReturnCodeValue::RETCODE_TIMEOUT) {
273 std::cerr <<
"RETCODE_TIMEOUT" << std::endl;
276 if (rcode == erc::ReturnCodeValue::RETCODE_NO_DATA) {
277 std::cerr <<
"RETCODE_NO_DATA" << std::endl;
280 if (rcode == erc::ReturnCodeValue::RETCODE_ILLEGAL_OPERATION) {
281 std::cerr <<
"RETCODE_ILLEGAL_OPERATION" << std::endl;
284 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ALLOWED_BY_SECURITY) {
285 std::cerr <<
"RETCODE_NOT_ALLOWED_BY_SECURITY" << std::endl;
288 std::cerr <<
"UNKNOWN" << std::endl;
293 std::vector<uint8_t> vector_data;
294 const size_t size = height * width * 4;
295 vector_data.resize(size);
296 std::memcpy(&vector_data[0], &data[0], size);
297 SetData(seconds, nanoseconds, height, width, std::move(vector_data));
316 header.
stamp(std::move(time));
319 _impl->_image.header(std::move(header));
320 _impl->_image.width(width);
321 _impl->_image.height(height);
322 _impl->_image.encoding(
"bgra8");
323 _impl->_image.is_bigendian(0);
324 _impl->_image.step(
_impl->_image.width() *
sizeof(uint8_t) * 4);
325 _impl->_image.data(std::move(data));
334 header.
stamp(std::move(time));
350 if (
_impl->_datawriter)
351 _impl->_publisher->delete_datawriter(
_impl->_datawriter);
353 if (
_impl->_publisher)
354 _impl->_participant->delete_publisher(
_impl->_publisher);
357 _impl->_participant->delete_topic(
_impl->_topic);
359 if (
_impl->_participant)
360 efd::DomainParticipantFactory::get_instance()->delete_participant(
_impl->_participant);
375 efd::DomainParticipantFactory::get_instance()->delete_participant(
_impl_info->_participant);
398 _name = std::move(other._name);
399 _parent = std::move(other._parent);
400 _impl = std::move(other._impl);
406 _name = std::move(other._name);
407 _parent = std::move(other._parent);
408 _impl = std::move(other._impl);
This class represents the structure Time defined by the user in the IDL file.
eProsima_user_DllExport void nanosec(uint32_t _nanosec)
This function sets a value in member nanosec
eProsima_user_DllExport void sec(int32_t _sec)
This function sets a value in member sec
CarlaISCameraPublisher & operator=(const CarlaISCameraPublisher &)
~CarlaISCameraPublisher()
void SetData(int32_t seconds, uint32_t nanoseconds, size_t height, size_t width, std::vector< uint8_t > &&data)
bool HasBeenInitialized() const
void SetInfoRegionOfInterest(uint32_t x_offset, uint32_t y_offset, uint32_t height, uint32_t width, bool do_rectify)
std::shared_ptr< CarlaCameraInfoPublisherImpl > _impl_info
void InitInfoData(uint32_t x_offset, uint32_t y_offset, uint32_t height, uint32_t width, float fov, bool do_rectify)
std::shared_ptr< CarlaISCameraPublisherImpl > _impl
void SetImageData(int32_t seconds, uint32_t nanoseconds, size_t height, size_t width, const uint8_t *data)
CarlaISCameraPublisher(const char *ros_name="", const char *parent="")
void SetCameraInfoData(int32_t seconds, uint32_t nanoseconds)
const std::string & parent() const
This class represents the TopicDataType of the type CameraInfo defined by the user in the IDL file.
This class represents the structure CameraInfo defined by the user in the IDL file.
This class represents the TopicDataType of the type Image defined by the user in the IDL file.
This class represents the structure Image defined by the user in the IDL file.
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
This file contains definitions of common data structures used in traffic manager.
sensor_msgs::msg::CameraInfo _info
efd::DomainParticipant * _participant
efd::DataWriter * _datawriter
efd::Publisher * _publisher
efd::DomainParticipant * _participant
efd::DataWriter * _datawriter
efd::Publisher * _publisher
sensor_msgs::msg::Image _image