1#define _GLIBCXX_USE_CXX11_ABI 0
10#include <fastdds/dds/domain/DomainParticipant.hpp>
11#include <fastdds/dds/publisher/Publisher.hpp>
12#include <fastdds/dds/topic/Topic.hpp>
13#include <fastdds/dds/publisher/DataWriter.hpp>
14#include <fastdds/dds/topic/TypeSupport.hpp>
16#include <fastdds/dds/domain/qos/DomainParticipantQos.hpp>
17#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
18#include <fastdds/dds/publisher/qos/PublisherQos.hpp>
19#include <fastdds/dds/topic/qos/TopicQos.hpp>
21#include <fastrtps/attributes/ParticipantAttributes.h>
22#include <fastrtps/qos/QosPolicies.h>
23#include <fastdds/dds/publisher/qos/DataWriterQos.hpp>
24#include <fastdds/dds/publisher/DataWriterListener.hpp>
34 namespace efd = eprosima::fastdds::dds;
38 using erc = eprosima::fastrtps::types::ReturnCode_t;
82 if (
_impl->_type ==
nullptr) {
83 std::cerr <<
"Invalid TypeSupport" << std::endl;
87 efd::DomainParticipantQos pqos = efd::PARTICIPANT_QOS_DEFAULT;
90 auto factory = efd::DomainParticipantFactory::get_instance();
91 _impl->_participant = factory->create_participant(0, pqos);
92 if (
_impl->_participant ==
nullptr) {
93 std::cerr <<
"Failed to create DomainParticipant" << std::endl;
97 _impl->_type.register_type(
_impl->_participant);
99 efd::PublisherQos pubqos = efd::PUBLISHER_QOS_DEFAULT;
100 _impl->_publisher =
_impl->_participant->create_publisher(pubqos,
nullptr);
101 if (
_impl->_publisher ==
nullptr) {
102 std::cerr <<
"Failed to create Publisher" << std::endl;
106 efd::TopicQos tqos = efd::TOPIC_QOS_DEFAULT;
107 const std::string base {
"rt/carla/" };
108 std::string topic_name = base;
112 _impl->_topic =
_impl->_participant->create_topic(topic_name,
_impl->_type->getName(), tqos);
113 if (
_impl->_topic ==
nullptr) {
114 std::cerr <<
"Failed to create Topic" << std::endl;
118 efd::DataWriterQos wqos = efd::DATAWRITER_QOS_DEFAULT;
119 wqos.endpoint().history_memory_policy = eprosima::fastrtps::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE;
122 _impl->_datawriter =
_impl->_publisher->create_datawriter(
_impl->_topic, wqos, listener);
123 if (
_impl->_datawriter ==
nullptr) {
124 std::cerr <<
"Failed to create DataWriter" << std::endl;
139 eprosima::fastrtps::rtps::InstanceHandle_t instance_handle;
143 eprosima::fastrtps::types::ReturnCode_t rcode =
_impl->_datawriter->write(&
_impl->_lidar, instance_handle);
145 if (rcode == erc::ReturnCodeValue::RETCODE_OK) {
149 if (rcode == erc::ReturnCodeValue::RETCODE_ERROR) {
151 std::cerr <<
"RETCODE_ERROR" << std::endl;
154 if (rcode == erc::ReturnCodeValue::RETCODE_UNSUPPORTED) {
156 std::cerr <<
"RETCODE_UNSUPPORTED" << std::endl;
159 if (rcode == erc::ReturnCodeValue::RETCODE_BAD_PARAMETER) {
161 std::cerr <<
"RETCODE_BAD_PARAMETER" << std::endl;
164 if (rcode == erc::ReturnCodeValue::RETCODE_PRECONDITION_NOT_MET) {
166 std::cerr <<
"RETCODE_PRECONDITION_NOT_MET" << std::endl;
169 if (rcode == erc::ReturnCodeValue::RETCODE_OUT_OF_RESOURCES) {
171 std::cerr <<
"RETCODE_OUT_OF_RESOURCES" << std::endl;
174 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ENABLED) {
176 std::cerr <<
"RETCODE_NOT_ENABLED" << std::endl;
179 if (rcode == erc::ReturnCodeValue::RETCODE_IMMUTABLE_POLICY) {
181 std::cerr <<
"RETCODE_IMMUTABLE_POLICY" << std::endl;
184 if (rcode == erc::ReturnCodeValue::RETCODE_INCONSISTENT_POLICY) {
186 std::cerr <<
"RETCODE_INCONSISTENT_POLICY" << std::endl;
189 if (rcode == erc::ReturnCodeValue::RETCODE_ALREADY_DELETED) {
191 std::cerr <<
"RETCODE_ALREADY_DELETED" << std::endl;
194 if (rcode == erc::ReturnCodeValue::RETCODE_TIMEOUT) {
196 std::cerr <<
"RETCODE_TIMEOUT" << std::endl;
199 if (rcode == erc::ReturnCodeValue::RETCODE_NO_DATA) {
201 std::cerr <<
"RETCODE_NO_DATA" << std::endl;
204 if (rcode == erc::ReturnCodeValue::RETCODE_ILLEGAL_OPERATION) {
206 std::cerr <<
"RETCODE_ILLEGAL_OPERATION" << std::endl;
209 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ALLOWED_BY_SECURITY) {
211 std::cerr <<
"RETCODE_NOT_ALLOWED_BY_SECURITY" << std::endl;
215 std::cerr <<
"UNKNOWN" << std::endl;
233 float*
end = &data[height * width];
234 for (++it; it <
end; it += 4) {
237 std::vector<uint8_t> vector_data;
238 const size_t size = height * width *
sizeof(float);
239 vector_data.resize(size);
240 std::memcpy(&vector_data[0], &data[0], size);
242 SetData(seconds, nanoseconds, height, width, std::move(vector_data));
261 std_msgs::msg::Header header;
262 header.stamp(std::move(time));
266 descriptor1.
name(
"x");
269 descriptor1.
count(1);
271 descriptor2.
name(
"y");
274 descriptor2.
count(1);
276 descriptor3.
name(
"z");
279 descriptor3.
count(1);
281 descriptor4.
name(
"intensity");
284 descriptor4.
count(1);
286 const size_t point_size = 4 *
sizeof(float);
287 _impl->_lidar.header(std::move(header));
288 _impl->_lidar.width(width / 4);
289 _impl->_lidar.height(height);
290 _impl->_lidar.is_bigendian(
false);
291 _impl->_lidar.fields({descriptor1, descriptor2, descriptor3, descriptor4});
292 _impl->_lidar.point_step(point_size);
293 _impl->_lidar.row_step(width *
sizeof(
float));
294 _impl->_lidar.is_dense(
false);
295 _impl->_lidar.data(std::move(data));
319 if (
_impl->_datawriter)
320 _impl->_publisher->delete_datawriter(
_impl->_datawriter);
322 if (
_impl->_publisher)
323 _impl->_participant->delete_publisher(
_impl->_publisher);
326 _impl->_participant->delete_topic(
_impl->_topic);
328 if (
_impl->_participant)
329 efd::DomainParticipantFactory::get_instance()->delete_participant(
_impl->_participant);
369 _name = std::move(other._name);
370 _parent = std::move(other._parent);
371 _impl = std::move(other._impl);
384 _name = std::move(other._name);
385 _parent = std::move(other._parent);
386 _impl = std::move(other._impl);
auto end() const noexcept
eProsima_user_DllExport void nanosec(uint32_t _nanosec)
此函数设置成员nanosec的值。
eProsima_user_DllExport void sec(int32_t _sec)
此函数设置成员sec的值。
~CarlaLidarPublisher()
CarlaLidarPublisher 类的析构函数
void SetData(int32_t seconds, uint32_t nanoseconds, size_t height, size_t width, float *data)
设置激光雷达数据,处理并转换数据类型
bool Init()
初始化CarlaLidarPublisher。
CarlaLidarPublisher & operator=(const CarlaLidarPublisher &)
拷贝赋值运算符重载
std::shared_ptr< CarlaLidarPublisherImpl > _impl
CarlaLidarPublisher(const char *ros_name="", const char *parent="")
CarlaLidarPublisher 类的构造函数
const std::string & parent() const
This class represents the TopicDataType of the type PointCloud2 defined by the user in the IDL file.
This class represents the structure PointCloud2 defined by the user in the IDL file.
此类表示用户在 IDL 文件中定义的结构 PointField。 <>
eProsima_user_DllExport void count(uint32_t _count)
此函数设置成员 count 的值
eProsima_user_DllExport void offset(uint32_t _offset)
此函数设置成员 offset 的值
eProsima_user_DllExport void name(const std::string &_name)
此函数复制成员 name 的值
eProsima_user_DllExport void datatype(uint8_t _datatype)
此函数设置成员 datatype 的值
eprosima::fastrtps::types::ReturnCode_t erc
@using erc
const uint8_t PointField__FLOAT32
CarlaLidarPublisher的内部实现结构体,封装了DDS通信所需的资源。
efd::Topic * _topic
DDS主题指针。
sensor_msgs::msg::PointCloud2 _lidar
存储激光雷达数据的PointCloud2消息。
efd::TypeSupport _type
DDS类型支持,用于PointCloud2消息。
efd::Publisher * _publisher
DDS发布者指针。
CarlaListener _listener
CARLA监听器,用于接收CARLA模拟器的数据。
efd::DataWriter * _datawriter
DDS数据写入器指针。
efd::DomainParticipant * _participant
DDS域参与者指针。