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> 31 namespace efd = eprosima::fastdds::dds;
32 using erc = eprosima::fastrtps::types::ReturnCode_t;
56 return _impl_info->_init;
61 SetInfoRegionOfInterest(x_offset, y_offset, height, width, do_rectify);
62 _impl_info->_init =
true;
66 return InitImage() && InitInfo();
70 if (_impl->_type ==
nullptr) {
71 std::cerr <<
"Invalid TypeSupport" << std::endl;
75 efd::DomainParticipantQos pqos = efd::PARTICIPANT_QOS_DEFAULT;
77 auto factory = efd::DomainParticipantFactory::get_instance();
78 _impl->_participant = factory->create_participant(0, pqos);
79 if (_impl->_participant ==
nullptr) {
80 std::cerr <<
"Failed to create DomainParticipant" << std::endl;
83 _impl->_type.register_type(_impl->_participant);
85 efd::PublisherQos pubqos = efd::PUBLISHER_QOS_DEFAULT;
86 _impl->_publisher = _impl->_participant->create_publisher(pubqos,
nullptr);
87 if (_impl->_publisher ==
nullptr) {
88 std::cerr <<
"Failed to create Publisher" << std::endl;
92 efd::TopicQos tqos = efd::TOPIC_QOS_DEFAULT;
93 const std::string publisher_type {
"/image"};
94 const std::string base {
"rt/carla/" };
95 std::string topic_name = base;
97 topic_name += _parent +
"/";
99 topic_name += publisher_type;
100 _impl->_topic = _impl->_participant->create_topic(topic_name, _impl->_type->getName(), tqos);
101 if (_impl->_topic ==
nullptr) {
102 std::cerr <<
"Failed to create Topic" << std::endl;
106 efd::DataWriterQos wqos = efd::DATAWRITER_QOS_DEFAULT;
107 wqos.endpoint().history_memory_policy = eprosima::fastrtps::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE;
108 efd::DataWriterListener* listener = (efd::DataWriterListener*)_impl->_listener._impl.get();
109 _impl->_datawriter = _impl->_publisher->create_datawriter(_impl->_topic, wqos, listener);
110 if (_impl->_datawriter ==
nullptr) {
111 std::cerr <<
"Failed to create DataWriter" << std::endl;
119 if (_impl_info->_type ==
nullptr) {
120 std::cerr <<
"Invalid TypeSupport" << std::endl;
124 efd::DomainParticipantQos pqos = efd::PARTICIPANT_QOS_DEFAULT;
126 auto factory = efd::DomainParticipantFactory::get_instance();
127 _impl_info->_participant = factory->create_participant(0, pqos);
128 if (_impl_info->_participant ==
nullptr) {
129 std::cerr <<
"Failed to create DomainParticipant" << std::endl;
132 _impl_info->_type.register_type(_impl_info->_participant);
134 efd::PublisherQos pubqos = efd::PUBLISHER_QOS_DEFAULT;
135 _impl_info->_publisher = _impl_info->_participant->create_publisher(pubqos,
nullptr);
136 if (_impl_info->_publisher ==
nullptr) {
137 std::cerr <<
"Failed to create Publisher" << std::endl;
141 efd::TopicQos tqos = efd::TOPIC_QOS_DEFAULT;
142 const std::string publisher_type {
"/camera_info"};
143 const std::string base {
"rt/carla/" };
144 std::string topic_name = base;
145 if (!_parent.empty())
146 topic_name += _parent +
"/";
148 topic_name += publisher_type;
149 _impl_info->_topic = _impl_info->_participant->create_topic(topic_name, _impl_info->_type->getName(), tqos);
150 if (_impl_info->_topic ==
nullptr) {
151 std::cerr <<
"Failed to create Topic" << std::endl;
154 efd::DataWriterQos wqos = efd::DATAWRITER_QOS_DEFAULT;
155 efd::DataWriterListener* listener = (efd::DataWriterListener*)_impl_info->_listener._impl.get();
156 _impl_info->_datawriter = _impl_info->_publisher->create_datawriter(_impl_info->_topic, wqos, listener);
157 if (_impl_info->_datawriter ==
nullptr) {
158 std::cerr <<
"Failed to create DataWriter" << std::endl;
167 return PublishImage() && PublishInfo();
172 eprosima::fastrtps::types::ReturnCode_t rcode = _impl->_datawriter->write(&_impl->_image, instance_handle);
173 if (rcode == erc::ReturnCodeValue::RETCODE_OK) {
176 if (rcode == erc::ReturnCodeValue::RETCODE_ERROR) {
177 std::cerr <<
"RETCODE_ERROR" << std::endl;
180 if (rcode == erc::ReturnCodeValue::RETCODE_UNSUPPORTED) {
181 std::cerr <<
"RETCODE_UNSUPPORTED" << std::endl;
184 if (rcode == erc::ReturnCodeValue::RETCODE_BAD_PARAMETER) {
185 std::cerr <<
"RETCODE_BAD_PARAMETER" << std::endl;
188 if (rcode == erc::ReturnCodeValue::RETCODE_PRECONDITION_NOT_MET) {
189 std::cerr <<
"RETCODE_PRECONDITION_NOT_MET" << std::endl;
192 if (rcode == erc::ReturnCodeValue::RETCODE_OUT_OF_RESOURCES) {
193 std::cerr <<
"RETCODE_OUT_OF_RESOURCES" << std::endl;
196 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ENABLED) {
197 std::cerr <<
"RETCODE_NOT_ENABLED" << std::endl;
200 if (rcode == erc::ReturnCodeValue::RETCODE_IMMUTABLE_POLICY) {
201 std::cerr <<
"RETCODE_IMMUTABLE_POLICY" << std::endl;
204 if (rcode == erc::ReturnCodeValue::RETCODE_INCONSISTENT_POLICY) {
205 std::cerr <<
"RETCODE_INCONSISTENT_POLICY" << std::endl;
208 if (rcode == erc::ReturnCodeValue::RETCODE_ALREADY_DELETED) {
209 std::cerr <<
"RETCODE_ALREADY_DELETED" << std::endl;
212 if (rcode == erc::ReturnCodeValue::RETCODE_TIMEOUT) {
213 std::cerr <<
"RETCODE_TIMEOUT" << std::endl;
216 if (rcode == erc::ReturnCodeValue::RETCODE_NO_DATA) {
217 std::cerr <<
"RETCODE_NO_DATA" << std::endl;
220 if (rcode == erc::ReturnCodeValue::RETCODE_ILLEGAL_OPERATION) {
221 std::cerr <<
"RETCODE_ILLEGAL_OPERATION" << std::endl;
224 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ALLOWED_BY_SECURITY) {
225 std::cerr <<
"RETCODE_NOT_ALLOWED_BY_SECURITY" << std::endl;
228 std::cerr <<
"UNKNOWN" << std::endl;
234 eprosima::fastrtps::types::ReturnCode_t rcode = _impl_info->_datawriter->write(&_impl_info->_info, instance_handle);
235 if (rcode == erc::ReturnCodeValue::RETCODE_OK) {
238 if (rcode == erc::ReturnCodeValue::RETCODE_ERROR) {
239 std::cerr <<
"RETCODE_ERROR" << std::endl;
242 if (rcode == erc::ReturnCodeValue::RETCODE_UNSUPPORTED) {
243 std::cerr <<
"RETCODE_UNSUPPORTED" << std::endl;
246 if (rcode == erc::ReturnCodeValue::RETCODE_BAD_PARAMETER) {
247 std::cerr <<
"RETCODE_BAD_PARAMETER" << std::endl;
250 if (rcode == erc::ReturnCodeValue::RETCODE_PRECONDITION_NOT_MET) {
251 std::cerr <<
"RETCODE_PRECONDITION_NOT_MET" << std::endl;
254 if (rcode == erc::ReturnCodeValue::RETCODE_OUT_OF_RESOURCES) {
255 std::cerr <<
"RETCODE_OUT_OF_RESOURCES" << std::endl;
258 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ENABLED) {
259 std::cerr <<
"RETCODE_NOT_ENABLED" << std::endl;
262 if (rcode == erc::ReturnCodeValue::RETCODE_IMMUTABLE_POLICY) {
263 std::cerr <<
"RETCODE_IMMUTABLE_POLICY" << std::endl;
266 if (rcode == erc::ReturnCodeValue::RETCODE_INCONSISTENT_POLICY) {
267 std::cerr <<
"RETCODE_INCONSISTENT_POLICY" << std::endl;
270 if (rcode == erc::ReturnCodeValue::RETCODE_ALREADY_DELETED) {
271 std::cerr <<
"RETCODE_ALREADY_DELETED" << std::endl;
274 if (rcode == erc::ReturnCodeValue::RETCODE_TIMEOUT) {
275 std::cerr <<
"RETCODE_TIMEOUT" << std::endl;
278 if (rcode == erc::ReturnCodeValue::RETCODE_NO_DATA) {
279 std::cerr <<
"RETCODE_NO_DATA" << std::endl;
282 if (rcode == erc::ReturnCodeValue::RETCODE_ILLEGAL_OPERATION) {
283 std::cerr <<
"RETCODE_ILLEGAL_OPERATION" << std::endl;
286 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ALLOWED_BY_SECURITY) {
287 std::cerr <<
"RETCODE_NOT_ALLOWED_BY_SECURITY" << std::endl;
290 std::cerr <<
"UNKNOWN" << std::endl;
295 const size_t size = height * width * 4;
296 vector_data.resize(size);
297 std::memcpy(&vector_data[0], &data[0], size);
298 SetData(seconds, nanoseconds,height, width, std::move(vector_data));
308 _impl_info->_info.roi(roi);
317 header.
stamp(std::move(time));
320 _impl->_image.header(std::move(header));
321 _impl->_image.width(width);
322 _impl->_image.height(height);
323 _impl->_image.encoding(
"bgra8");
324 _impl->_image.is_bigendian(0);
325 _impl->_image.step(_impl->_image.width() *
sizeof(uint8_t) * 4);
326 _impl->_image.data(std::move(data));
335 header.
stamp(std::move(time));
337 _impl_info->_info.header(header);
351 if (
_impl->_datawriter)
352 _impl->_publisher->delete_datawriter(
_impl->_datawriter);
354 if (
_impl->_publisher)
355 _impl->_participant->delete_publisher(
_impl->_publisher);
358 _impl->_participant->delete_topic(
_impl->_topic);
360 if (
_impl->_participant)
361 efd::DomainParticipantFactory::get_instance()->delete_participant(
_impl->_participant);
376 efd::DomainParticipantFactory::get_instance()->delete_participant(
_impl_info->_participant);
399 _name = std::move(other._name);
400 _parent = std::move(other._parent);
401 _impl = std::move(other._impl);
408 _name = std::move(other._name);
409 _parent = std::move(other._parent);
410 _impl = std::move(other._impl);
void SetData(int32_t seconds, uint32_t nanoseconds, size_t height, size_t width, std::vector< uint8_t > &&data)
efd::DataWriter * _datawriter
eprosima::fastrtps::rtps::InstanceHandle_t InstanceHandle_t
eprosima::fastrtps::types::ReturnCode_t erc
This file contains definitions of common data structures used in traffic manager. ...
void SetCameraInfoData(int32_t seconds, uint32_t nanoseconds)
This class represents the structure CameraInfo defined by the user in the IDL file.
This class represents the structure Image defined by the user in the IDL file.
~CarlaDepthCameraPublisher()
eProsima_user_DllExport void width(uint32_t _width)
This function sets a value in member width.
CarlaDepthCameraPublisher & operator=(const CarlaDepthCameraPublisher &)
This class represents the TopicDataType of the type Image defined by the user in the IDL file...
void SetImageData(int32_t seconds, uint32_t nanoseconds, size_t height, size_t width, const uint8_t *data)
eProsima_user_DllExport void x_offset(uint32_t _x_offset)
This function sets a value in member x_offset.
eProsima_user_DllExport void sec(int32_t _sec)
This function sets a value in member sec.
This class represents the TopicDataType of the type CameraInfo defined by the user in the IDL file...
eProsima_user_DllExport void nanosec(uint32_t _nanosec)
This function sets a value in member nanosec.
void SetInfoRegionOfInterest(uint32_t x_offset, uint32_t y_offset, uint32_t height, uint32_t width, bool do_rectify)
This class represents the structure RegionOfInterest defined by the user in the IDL file...
CarlaDepthCameraPublisher(const char *ros_name="", const char *parent="")
const std::string & parent() const
This class represents the structure Time defined by the user in the IDL file.
void InitInfoData(uint32_t x_offset, uint32_t y_offset, uint32_t height, uint32_t width, float fov, bool do_rectify)
efd::Publisher * _publisher
eProsima_user_DllExport void height(uint32_t _height)
This function sets a value in member height.
std::shared_ptr< CarlaDepthCameraPublisherImpl > _impl
efd::DomainParticipant * _participant
eProsima_user_DllExport void y_offset(uint32_t _y_offset)
This function sets a value in member y_offset.
std::shared_ptr< CarlaCameraInfoPublisherImpl > _impl_info
bool HasBeenInitialized() const
sensor_msgs::msg::Image _image
eProsima_user_DllExport void do_rectify(bool _do_rectify)
This function sets a value in member do_rectify.