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) {
175 if (rcode == erc::ReturnCodeValue::RETCODE_ERROR) {
176 std::cerr <<
"RETCODE_ERROR" << std::endl;
179 if (rcode == erc::ReturnCodeValue::RETCODE_UNSUPPORTED) {
180 std::cerr <<
"RETCODE_UNSUPPORTED" << std::endl;
183 if (rcode == erc::ReturnCodeValue::RETCODE_BAD_PARAMETER) {
184 std::cerr <<
"RETCODE_BAD_PARAMETER" << std::endl;
187 if (rcode == erc::ReturnCodeValue::RETCODE_PRECONDITION_NOT_MET) {
188 std::cerr <<
"RETCODE_PRECONDITION_NOT_MET" << std::endl;
191 if (rcode == erc::ReturnCodeValue::RETCODE_OUT_OF_RESOURCES) {
192 std::cerr <<
"RETCODE_OUT_OF_RESOURCES" << std::endl;
195 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ENABLED) {
196 std::cerr <<
"RETCODE_NOT_ENABLED" << std::endl;
199 if (rcode == erc::ReturnCodeValue::RETCODE_IMMUTABLE_POLICY) {
200 std::cerr <<
"RETCODE_IMMUTABLE_POLICY" << std::endl;
203 if (rcode == erc::ReturnCodeValue::RETCODE_INCONSISTENT_POLICY) {
204 std::cerr <<
"RETCODE_INCONSISTENT_POLICY" << std::endl;
207 if (rcode == erc::ReturnCodeValue::RETCODE_ALREADY_DELETED) {
208 std::cerr <<
"RETCODE_ALREADY_DELETED" << std::endl;
211 if (rcode == erc::ReturnCodeValue::RETCODE_TIMEOUT) {
212 std::cerr <<
"RETCODE_TIMEOUT" << std::endl;
215 if (rcode == erc::ReturnCodeValue::RETCODE_NO_DATA) {
216 std::cerr <<
"RETCODE_NO_DATA" << std::endl;
219 if (rcode == erc::ReturnCodeValue::RETCODE_ILLEGAL_OPERATION) {
220 std::cerr <<
"RETCODE_ILLEGAL_OPERATION" << std::endl;
223 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ALLOWED_BY_SECURITY) {
224 std::cerr <<
"RETCODE_NOT_ALLOWED_BY_SECURITY" << std::endl;
227 std::cerr <<
"UNKNOWN" << std::endl;
232 eprosima::fastrtps::rtps::InstanceHandle_t instance_handle;
233 eprosima::fastrtps::types::ReturnCode_t rcode =
_impl_info->_datawriter->write(&
_impl_info->_info, instance_handle);
234 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 SetData(seconds, nanoseconds, height, width, std::move(vector_data));
317 std_msgs::msg::Header header;
318 header.stamp(std::move(time));
321 _impl->_image.header(std::move(header));
322 _impl->_image.width(width);
323 _impl->_image.height(height);
324 _impl->_image.encoding(
"bgra8");
325 _impl->_image.is_bigendian(0);
326 _impl->_image.step(
_impl->_image.width() *
sizeof(uint8_t) * 4);
327 _impl->_image.data(std::move(data));
335 std_msgs::msg::Header header;
336 header.stamp(std::move(time));
352 if (
_impl->_datawriter)
353 _impl->_publisher->delete_datawriter(
_impl->_datawriter);
355 if (
_impl->_publisher)
356 _impl->_participant->delete_publisher(
_impl->_publisher);
359 _impl->_participant->delete_topic(
_impl->_topic);
361 if (
_impl->_participant)
362 efd::DomainParticipantFactory::get_instance()->delete_participant(
_impl->_participant);
377 efd::DomainParticipantFactory::get_instance()->delete_participant(
_impl_info->_participant);
400 _name = std::move(other._name);
401 _parent = std::move(other._parent);
402 _impl = std::move(other._impl);
408 _name = std::move(other._name);
409 _parent = std::move(other._parent);
410 _impl = std::move(other._impl);
eProsima_user_DllExport void nanosec(uint32_t _nanosec)
此函数设置成员nanosec的值。
eProsima_user_DllExport void sec(int32_t _sec)
此函数设置成员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
此类表示用户在 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::DomainParticipant * _participant
efd::DataWriter * _datawriter
efd::Publisher * _publisher
sensor_msgs::msg::Image _image