CARLA
CarlaRadarPublisher.cpp
Go to the documentation of this file.
1 #define _GLIBCXX_USE_CXX11_ABI 0
2 
3 #include "CarlaRadarPublisher.h"
4 
5 #include <string>
6 
10 
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>
16 
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>
21 
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>
26 
27 
28 namespace carla {
29 namespace ros2 {
30 
31  namespace efd = eprosima::fastdds::dds;
32  using erc = eprosima::fastrtps::types::ReturnCode_t;
33 
35  efd::DomainParticipant* _participant { nullptr };
36  efd::Publisher* _publisher { nullptr };
37  efd::Topic* _topic { nullptr };
38  efd::DataWriter* _datawriter { nullptr };
39  efd::TypeSupport _type { new sensor_msgs::msg::PointCloud2PubSubType() };
42  };
43 
45  float x;
46  float y;
47  float z;
49  };
50 
52  if (_impl->_type == nullptr) {
53  std::cerr << "Invalid TypeSupport" << std::endl;
54  return false;
55  }
56 
57  efd::DomainParticipantQos pqos = efd::PARTICIPANT_QOS_DEFAULT;
58  pqos.name(_name);
59  auto factory = efd::DomainParticipantFactory::get_instance();
60  _impl->_participant = factory->create_participant(0, pqos);
61  if (_impl->_participant == nullptr) {
62  std::cerr << "Failed to create DomainParticipant" << std::endl;
63  return false;
64  }
65  _impl->_type.register_type(_impl->_participant);
66 
67  efd::PublisherQos pubqos = efd::PUBLISHER_QOS_DEFAULT;
68  _impl->_publisher = _impl->_participant->create_publisher(pubqos, nullptr);
69  if (_impl->_publisher == nullptr) {
70  std::cerr << "Failed to create Publisher" << std::endl;
71  return false;
72  }
73 
74  efd::TopicQos tqos = efd::TOPIC_QOS_DEFAULT;
75  const std::string base { "rt/carla/" };
76  std::string topic_name = base;
77  if (!_parent.empty())
78  topic_name += _parent + "/";
79  topic_name += _name;
80  _impl->_topic = _impl->_participant->create_topic(topic_name, _impl->_type->getName(), tqos);
81  if (_impl->_topic == nullptr) {
82  std::cerr << "Failed to create Topic" << std::endl;
83  return false;
84  }
85 
86  efd::DataWriterQos wqos = efd::DATAWRITER_QOS_DEFAULT;
87  wqos.endpoint().history_memory_policy = eprosima::fastrtps::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE;
88  efd::DataWriterListener* listener = (efd::DataWriterListener*)_impl->_listener._impl.get();
89  _impl->_datawriter = _impl->_publisher->create_datawriter(_impl->_topic, wqos, listener);
90  if (_impl->_datawriter == nullptr) {
91  std::cerr << "Failed to create DataWriter" << std::endl;
92  return false;
93  }
94  _frame_id = _name;
95  return true;
96  }
97 
100  eprosima::fastrtps::types::ReturnCode_t rcode = _impl->_datawriter->write(&_impl->_radar, instance_handle);
101  if (rcode == erc::ReturnCodeValue::RETCODE_OK) {
102  return true;
103  }
104  if (rcode == erc::ReturnCodeValue::RETCODE_ERROR) {
105  std::cerr << "RETCODE_ERROR" << std::endl;
106  return false;
107  }
108  if (rcode == erc::ReturnCodeValue::RETCODE_UNSUPPORTED) {
109  std::cerr << "RETCODE_UNSUPPORTED" << std::endl;
110  return false;
111  }
112  if (rcode == erc::ReturnCodeValue::RETCODE_BAD_PARAMETER) {
113  std::cerr << "RETCODE_BAD_PARAMETER" << std::endl;
114  return false;
115  }
116  if (rcode == erc::ReturnCodeValue::RETCODE_PRECONDITION_NOT_MET) {
117  std::cerr << "RETCODE_PRECONDITION_NOT_MET" << std::endl;
118  return false;
119  }
120  if (rcode == erc::ReturnCodeValue::RETCODE_OUT_OF_RESOURCES) {
121  std::cerr << "RETCODE_OUT_OF_RESOURCES" << std::endl;
122  return false;
123  }
124  if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ENABLED) {
125  std::cerr << "RETCODE_NOT_ENABLED" << std::endl;
126  return false;
127  }
128  if (rcode == erc::ReturnCodeValue::RETCODE_IMMUTABLE_POLICY) {
129  std::cerr << "RETCODE_IMMUTABLE_POLICY" << std::endl;
130  return false;
131  }
132  if (rcode == erc::ReturnCodeValue::RETCODE_INCONSISTENT_POLICY) {
133  std::cerr << "RETCODE_INCONSISTENT_POLICY" << std::endl;
134  return false;
135  }
136  if (rcode == erc::ReturnCodeValue::RETCODE_ALREADY_DELETED) {
137  std::cerr << "RETCODE_ALREADY_DELETED" << std::endl;
138  return false;
139  }
140  if (rcode == erc::ReturnCodeValue::RETCODE_TIMEOUT) {
141  std::cerr << "RETCODE_TIMEOUT" << std::endl;
142  return false;
143  }
144  if (rcode == erc::ReturnCodeValue::RETCODE_NO_DATA) {
145  std::cerr << "RETCODE_NO_DATA" << std::endl;
146  return false;
147  }
148  if (rcode == erc::ReturnCodeValue::RETCODE_ILLEGAL_OPERATION) {
149  std::cerr << "RETCODE_ILLEGAL_OPERATION" << std::endl;
150  return false;
151  }
152  if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ALLOWED_BY_SECURITY) {
153  std::cerr << "RETCODE_NOT_ALLOWED_BY_SECURITY" << std::endl;
154  return false;
155  }
156  std::cerr << "UNKNOWN" << std::endl;
157  return false;
158  }
159 
160 void CarlaRadarPublisher::SetData(int32_t seconds, uint32_t nanoseconds, size_t height, size_t width, size_t elements, const uint8_t* data) {
161 
162  std::vector<uint8_t> vector_data;
163  const size_t size = elements * sizeof(RadarDetectionWithPosition);
164  vector_data.resize(size);
165  RadarDetectionWithPosition* radar_data = (RadarDetectionWithPosition*)&vector_data[0];
167  for (size_t i = 0; i < elements; ++i, ++radar_data, ++detection_data) {
168  radar_data->x = detection_data->depth * cosf(detection_data->azimuth) * cosf(-detection_data->altitude);
169  radar_data->y = detection_data->depth * sinf(-detection_data->azimuth) * cosf(detection_data->altitude);
170  radar_data->z = detection_data->depth * sinf(detection_data->altitude);
171  radar_data->detection = *detection_data;
172  }
173 
174  SetData(seconds, nanoseconds, height, width, elements, std::move(vector_data));
175  }
176 
177  void CarlaRadarPublisher::SetData(int32_t seconds, uint32_t nanoseconds, size_t height, size_t width, size_t elements, std::vector<uint8_t>&& data) {
179  time.sec(seconds);
180  time.nanosec(nanoseconds);
181 
182  std_msgs::msg::Header header;
183  header.stamp(std::move(time));
184  header.frame_id(_frame_id);
185 
186  sensor_msgs::msg::PointField descriptor1;
187  descriptor1.name("x");
188  descriptor1.offset(0);
190  descriptor1.count(1);
191  sensor_msgs::msg::PointField descriptor2;
192  descriptor2.name("y");
193  descriptor2.offset(4);
195  descriptor2.count(1);
196  sensor_msgs::msg::PointField descriptor3;
197  descriptor3.name("z");
198  descriptor3.offset(8);
200  descriptor3.count(1);
201  sensor_msgs::msg::PointField descriptor4;
202  descriptor4.name("velocity");
203  descriptor4.offset(12);
205  descriptor4.count(1);
206  sensor_msgs::msg::PointField descriptor5;
207  descriptor5.name("azimuth");
208  descriptor5.offset(16);
210  descriptor5.count(1);
211  sensor_msgs::msg::PointField descriptor6;
212  descriptor6.name("altitude");
213  descriptor6.offset(20);
215  descriptor6.count(1);
216  sensor_msgs::msg::PointField descriptor7;
217  descriptor7.name("depth");
218  descriptor7.offset(24);
220  descriptor7.count(1);
221 
222  const size_t point_size = sizeof(RadarDetectionWithPosition);
223  _impl->_radar.header(std::move(header));
224  _impl->_radar.width(elements);
225  _impl->_radar.height(height);
226  _impl->_radar.is_bigendian(false);
227  _impl->_radar.fields({descriptor1, descriptor2, descriptor3, descriptor4, descriptor5, descriptor6, descriptor7});
228  _impl->_radar.point_step(point_size);
229  _impl->_radar.row_step(elements * point_size);
230  _impl->_radar.is_dense(false);
231  _impl->_radar.data(std::move(data));
232  }
233 
234  CarlaRadarPublisher::CarlaRadarPublisher(const char* ros_name, const char* parent) :
235  _impl(std::make_shared<CarlaRadarPublisherImpl>()) {
236  _name = ros_name;
237  _parent = parent;
238  }
239 
241  if (!_impl)
242  return;
243 
244  if (_impl->_datawriter)
245  _impl->_publisher->delete_datawriter(_impl->_datawriter);
246 
247  if (_impl->_publisher)
248  _impl->_participant->delete_publisher(_impl->_publisher);
249 
250  if (_impl->_topic)
251  _impl->_participant->delete_topic(_impl->_topic);
252 
253  if (_impl->_participant)
254  efd::DomainParticipantFactory::get_instance()->delete_participant(_impl->_participant);
255  }
256 
258  _frame_id = other._frame_id;
259  _name = other._name;
260  _parent = other._parent;
261  _impl = other._impl;
262  }
263 
265  _frame_id = other._frame_id;
266  _name = other._name;
267  _parent = other._parent;
268  _impl = other._impl;
269 
270  return *this;
271  }
272 
274  _frame_id = std::move(other._frame_id);
275  _name = std::move(other._name);
276  _parent = std::move(other._parent);
277  _impl = std::move(other._impl);
278  }
279 
281  _frame_id = std::move(other._frame_id);
282  _name = std::move(other._name);
283  _parent = std::move(other._parent);
284  _impl = std::move(other._impl);
285 
286  return *this;
287  }
288 }
289 }
This class represents the structure PointCloud2 defined by the user in the IDL file.
Definition: PointCloud2.h:73
CarlaRadarPublisher & operator=(const CarlaRadarPublisher &)
sensor_msgs::msg::PointCloud2 _radar
std::shared_ptr< CarlaRadarPublisherImpl > _impl
This class represents the structure PointField defined by the user in the IDL file.
Definition: PointField.h:79
const uint8_t PointField__FLOAT32
Definition: PointField.h:72
eProsima_user_DllExport void name(const std::string &_name)
This function copies the value in member name.
Definition: PointField.cpp:150
eprosima::fastrtps::rtps::InstanceHandle_t InstanceHandle_t
CarlaRadarPublisher(const char *ros_name="", const char *parent="")
eprosima::fastrtps::types::ReturnCode_t erc
void SetData(int32_t seconds, uint32_t nanoseconds, size_t height, size_t width, size_t elements, const uint8_t *data)
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
This class represents the structure Header defined by the user in the IDL file.
Definition: Header.h:72
eProsima_user_DllExport void stamp(const builtin_interfaces::msg::Time &_stamp)
This function copies the value in member stamp.
Definition: Header.cpp:131
eProsima_user_DllExport void offset(uint32_t _offset)
This function sets a value in member offset.
Definition: PointField.cpp:188
eProsima_user_DllExport void datatype(uint8_t _datatype)
This function sets a value in member datatype.
Definition: PointField.cpp:216
This class represents the TopicDataType of the type PointCloud2 defined by the user in the IDL file...
carla::sensor::data::RadarDetection detection
eProsima_user_DllExport void sec(int32_t _sec)
This function sets a value in member sec.
Definition: Time.cpp:133
eProsima_user_DllExport void count(uint32_t _count)
This function sets a value in member count.
Definition: PointField.cpp:244
eProsima_user_DllExport void nanosec(uint32_t _nanosec)
This function sets a value in member nanosec.
Definition: Time.cpp:161
const std::string & parent() const
This class represents the structure Time defined by the user in the IDL file.
eProsima_user_DllExport void frame_id(const std::string &_frame_id)
This function copies the value in member frame_id.
Definition: Header.cpp:168