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>
36 namespace efd = eprosima::fastdds::dds;
40 using erc = eprosima::fastrtps::types::ReturnCode_t;
108 if (
_impl->_type ==
nullptr) {
109 std::cerr <<
"Invalid TypeSupport" << std::endl;
115 efd::DomainParticipantQos pqos = efd::PARTICIPANT_QOS_DEFAULT;
117 auto factory = efd::DomainParticipantFactory::get_instance();
118 _impl->_participant = factory->create_participant(0, pqos);
122 if (
_impl->_participant ==
nullptr) {
123 std::cerr <<
"Failed to create DomainParticipant" << std::endl;
129 _impl->_type.register_type(
_impl->_participant);
133 efd::PublisherQos pubqos = efd::PUBLISHER_QOS_DEFAULT;
137 _impl->_publisher =
_impl->_participant->create_publisher(pubqos,
nullptr);
141 if (
_impl->_publisher ==
nullptr) {
142 std::cerr <<
"Failed to create Publisher" << std::endl;
148 efd::TopicQos tqos = efd::TOPIC_QOS_DEFAULT;
152 const std::string base {
"rt/carla/" };
153 std::string topic_name = base;
160 _impl->_topic =
_impl->_participant->create_topic(topic_name,
_impl->_type->getName(), tqos);
164 if (
_impl->_topic ==
nullptr) {
165 std::cerr <<
"Failed to create Topic" << std::endl;
171 efd::DataWriterQos wqos = efd::DATAWRITER_QOS_DEFAULT;
172 wqos.endpoint().history_memory_policy = eprosima::fastrtps::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE;
180 _impl->_datawriter =
_impl->_publisher->create_datawriter(
_impl->_topic, wqos, listener);
184 if (
_impl->_datawriter ==
nullptr) {
185 std::cerr <<
"Failed to create DataWriter" << std::endl;
205 eprosima::fastrtps::rtps::InstanceHandle_t instance_handle;
207 eprosima::fastrtps::types::ReturnCode_t rcode =
_impl->_datawriter->write(&
_impl->_radar, instance_handle);
209 if (rcode == erc::ReturnCodeValue::RETCODE_OK) {
213 if (rcode == erc::ReturnCodeValue::RETCODE_ERROR) {
215 std::cerr <<
"RETCODE_ERROR" << std::endl;
218 if (rcode == erc::ReturnCodeValue::RETCODE_UNSUPPORTED) {
220 std::cerr <<
"RETCODE_UNSUPPORTED" << std::endl;
223 if (rcode == erc::ReturnCodeValue::RETCODE_BAD_PARAMETER) {
225 std::cerr <<
"RETCODE_BAD_PARAMETER" << std::endl;
228 if (rcode == erc::ReturnCodeValue::RETCODE_PRECONDITION_NOT_MET) {
230 std::cerr <<
"RETCODE_PRECONDITION_NOT_MET" << std::endl;
233 if (rcode == erc::ReturnCodeValue::RETCODE_OUT_OF_RESOURCES) {
235 std::cerr <<
"RETCODE_OUT_OF_RESOURCES" << std::endl;
238 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ENABLED) {
240 std::cerr <<
"RETCODE_NOT_ENABLED" << std::endl;
243 if (rcode == erc::ReturnCodeValue::RETCODE_IMMUTABLE_POLICY) {
245 std::cerr <<
"RETCODE_IMMUTABLE_POLICY" << std::endl;
248 if (rcode == erc::ReturnCodeValue::RETCODE_INCONSISTENT_POLICY) {
250 std::cerr <<
"RETCODE_INCONSISTENT_POLICY" << std::endl;
253 if (rcode == erc::ReturnCodeValue::RETCODE_ALREADY_DELETED) {
255 std::cerr <<
"RETCODE_ALREADY_DELETED" << std::endl;
258 if (rcode == erc::ReturnCodeValue::RETCODE_TIMEOUT) {
260 std::cerr <<
"RETCODE_TIMEOUT" << std::endl;
263 if (rcode == erc::ReturnCodeValue::RETCODE_NO_DATA) {
265 std::cerr <<
"RETCODE_NO_DATA" << std::endl;
268 if (rcode == erc::ReturnCodeValue::RETCODE_ILLEGAL_OPERATION) {
270 std::cerr <<
"RETCODE_ILLEGAL_OPERATION" << std::endl;
273 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ALLOWED_BY_SECURITY) {
275 std::cerr <<
"RETCODE_NOT_ALLOWED_BY_SECURITY" << std::endl;
279 std::cerr <<
"UNKNOWN" << std::endl;
296 std::vector<uint8_t> vector_data;
300 vector_data.resize(size);
306 for (
size_t i = 0; i < elements; ++i, ++radar_data, ++detection_data) {
308 radar_data->
x = detection_data->
depth * cosf(detection_data->
azimuth) * cosf(-detection_data->
altitude);
310 radar_data->
y = detection_data->
depth * sinf(-detection_data->
azimuth) * cosf(detection_data->
altitude);
312 radar_data->
z = detection_data->
depth * sinf(detection_data->
altitude);
317 SetData(seconds, nanoseconds, height, width, elements, std::move(vector_data));
337 std_msgs::msg::Header header;
338 header.stamp(std::move(time));
342 descriptor1.
name(
"x");
345 descriptor1.
count(1);
347 descriptor2.
name(
"y");
350 descriptor2.
count(1);
352 descriptor3.
name(
"z");
355 descriptor3.
count(1);
357 descriptor4.
name(
"velocity");
360 descriptor4.
count(1);
362 descriptor5.
name(
"azimuth");
365 descriptor5.
count(1);
367 descriptor6.
name(
"altitude");
370 descriptor6.
count(1);
372 descriptor7.
name(
"depth");
375 descriptor7.
count(1);
379 _impl->_radar.header(std::move(header));
380 _impl->_radar.width(elements);
381 _impl->_radar.height(height);
382 _impl->_radar.is_bigendian(
false);
383 _impl->_radar.fields({descriptor1, descriptor2, descriptor3, descriptor4, descriptor5, descriptor6, descriptor7});
384 _impl->_radar.point_step(point_size);
385 _impl->_radar.row_step(elements * point_size);
386 _impl->_radar.is_dense(
false);
388 _impl->_radar.data(std::move(data));
412 if (
_impl->_datawriter)
413 _impl->_publisher->delete_datawriter(
_impl->_datawriter);
415 if (
_impl->_publisher)
416 _impl->_participant->delete_publisher(
_impl->_publisher);
419 _impl->_participant->delete_topic(
_impl->_topic);
421 if (
_impl->_participant)
422 efd::DomainParticipantFactory::get_instance()->delete_participant(
_impl->_participant);
462 _name = std::move(other._name);
463 _parent = std::move(other._parent);
464 _impl = std::move(other._impl);
476 _name = std::move(other._name);
477 _parent = std::move(other._parent);
478 _impl = std::move(other._impl);
eProsima_user_DllExport void nanosec(uint32_t _nanosec)
此函数设置成员nanosec的值。
eProsima_user_DllExport void sec(int32_t _sec)
此函数设置成员sec的值。
const std::string & parent() const
CarlaRadarPublisher(const char *ros_name="", const char *parent="")
CarlaRadarPublisher 类的构造函数
std::shared_ptr< CarlaRadarPublisherImpl > _impl
void SetData(int32_t seconds, uint32_t nanoseconds, size_t height, size_t width, size_t elements, const uint8_t *data)
设置雷达数据
bool Init()
初始化CarlaRadarPublisher对象。
CarlaRadarPublisher & operator=(const CarlaRadarPublisher &)
拷贝赋值运算符
~CarlaRadarPublisher()
CarlaRadarPublisher 类的析构函数
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
CarlaRadarPublisher的内部实现结构体,封装了Fast-DDS发布雷达数据所需的对象。
sensor_msgs::msg::PointCloud2 _radar
存储雷达数据的ROS 2点云消息对象。
efd::TypeSupport _type
Fast-DDS类型支持对象,用于注册ROS 2点云消息类型。
efd::Publisher * _publisher
Fast-DDS发布者对象指针。
CarlaListener _listener
CARLA监听器对象,用于接收来自CARLA的雷达数据。
efd::Topic * _topic
Fast-DDS主题对象指针。
efd::DomainParticipant * _participant
Fast-DDS域参与者对象指针。
efd::DataWriter * _datawriter
Fast-DDS数据写入器对象指针。
carla::sensor::data::RadarDetection detection
CARLA雷达检测数据。