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;
47 efd::Topic*
_topic {
nullptr };
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;
105 efd::DataWriterQos wqos = efd::DATAWRITER_QOS_DEFAULT;
106 wqos.endpoint().history_memory_policy = eprosima::fastrtps::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE;
107 efd::DataWriterListener* listener = (efd::DataWriterListener*)_impl->_listener._impl.get();
108 _impl->_datawriter = _impl->_publisher->create_datawriter(_impl->_topic, wqos, listener);
109 if (_impl->_datawriter ==
nullptr) {
110 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 std::vector<uint8_t> vector_data;
296 const size_t size = height * width * 4;
297 vector_data.resize(size);
298 std::memcpy(&vector_data[0], &data[0], size);
299 SetImageData(seconds, nanoseconds, height, width, std::move(vector_data));
309 header.
stamp(std::move(time));
311 _impl->_image.header(header);
313 _impl->_image.width(width);
314 _impl->_image.height(height);
315 _impl->_image.encoding(
"bgra8");
316 _impl->_image.is_bigendian(0);
317 _impl->_image.step(_impl->_image.width() *
sizeof(uint8_t) * 4);
318 _impl->_image.data(std::move(data));
327 header.
stamp(std::move(time));
329 _impl_info->_info.header(header);
339 _impl_info->_info.roi(roi);
353 if (
_impl->_datawriter)
354 _impl->_publisher->delete_datawriter(
_impl->_datawriter);
356 if (
_impl->_publisher)
357 _impl->_participant->delete_publisher(
_impl->_publisher);
360 _impl->_participant->delete_topic(
_impl->_topic);
362 if (
_impl->_participant)
363 efd::DomainParticipantFactory::get_instance()->delete_participant(
_impl->_participant);
378 efd::DomainParticipantFactory::get_instance()->delete_participant(
_impl_info->_participant);
401 _name = std::move(other._name);
402 _parent = std::move(other._parent);
403 _impl = std::move(other._impl);
409 _name = std::move(other._name);
410 _parent = std::move(other._parent);
411 _impl = std::move(other._impl);
efd::Publisher * _publisher
void SetInfoRegionOfInterest(uint32_t x_offset, uint32_t y_offset, uint32_t height, uint32_t width, bool do_rectify)
CarlaRGBCameraPublisher(const char *ros_name="", const char *parent="")
bool HasBeenInitialized() const
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. ...
std::shared_ptr< CarlaCameraInfoPublisherImpl > _impl_info
This class represents the structure CameraInfo defined by the user in the IDL file.
CarlaRGBCameraPublisher & operator=(const CarlaRGBCameraPublisher &)
This class represents the structure Image defined by the user in the IDL file.
eProsima_user_DllExport void width(uint32_t _width)
This function sets a value in member width.
This class represents the TopicDataType of the type Image defined by the user in the IDL file...
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 InitInfoData(uint32_t x_offset, uint32_t y_offset, uint32_t height, uint32_t width, float fov, bool do_rectify)
std::shared_ptr< CarlaRGBCameraPublisherImpl > _impl
This class represents the structure RegionOfInterest defined by the user in the IDL file...
const std::string & parent() const
This class represents the structure Time defined by the user in the IDL file.
sensor_msgs::msg::Image _image
eProsima_user_DllExport void height(uint32_t _height)
This function sets a value in member height.
eProsima_user_DllExport void y_offset(uint32_t _y_offset)
This function sets a value in member y_offset.
void SetImageData(int32_t seconds, uint32_t nanoseconds, uint32_t height, uint32_t width, const uint8_t *data)
~CarlaRGBCameraPublisher()
efd::DataWriter * _datawriter
efd::DomainParticipant * _participant
eProsima_user_DllExport void do_rectify(bool _do_rectify)
This function sets a value in member do_rectify.
void SetCameraInfoData(int32_t seconds, uint32_t nanoseconds)