CARLA
 
载入中...
搜索中...
未找到
CarlaRGBCameraPublisher.cpp
浏览该文件的文档.
1#define _GLIBCXX_USE_CXX11_ABI 0
2
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// 服务质量 (QoS)
18#include <fastdds/dds/domain/qos/DomainParticipantQos.hpp>
19#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
20#include <fastdds/dds/publisher/qos/PublisherQos.hpp>
21#include <fastdds/dds/topic/qos/TopicQos.hpp>
22
23// RTPS规范,全称为实时发布/订阅 协议DDS互操作规范(The Real-Time Publish-Subscribe Protocol (RTPS) DDS Interoperability Wire Protocol Specification)
24#include <fastrtps/attributes/ParticipantAttributes.h>
25#include <fastrtps/qos/QosPolicies.h>
26#include <fastdds/dds/publisher/qos/DataWriterQos.hpp>
27#include <fastdds/dds/publisher/DataWriterListener.hpp>
28
29
30namespace carla {
31namespace ros2 {
32
33 // eProsima Fast DDS是 DDS(数据分发服务)规范的 C++ 实现
34 // eProsima Fast DDS库提供应用程序编程接口 (API) 和通信协议,用于部署 以数据为中心的发布者-订阅者 (Data-Centric Publish-Subscribe, DCPS) 模型,目的是在实时系统之间建立高效可靠的信息分发。
35 namespace efd = eprosima::fastdds::dds;
36 using erc = eprosima::fastrtps::types::ReturnCode_t;
37
39 efd::DomainParticipant* _participant { nullptr };
40 efd::Publisher* _publisher { nullptr };
41 efd::Topic* _topic { nullptr };
42 efd::DataWriter* _datawriter { nullptr };
43 efd::TypeSupport _type { new sensor_msgs::msg::ImagePubSubType() };
46 };
47
48 struct CarlaCameraInfoPublisherImpl {
49 efd::DomainParticipant* _participant { nullptr };
50 efd::Publisher* _publisher { nullptr };
51 efd::Topic* _topic { nullptr };
52 efd::DataWriter* _datawriter { nullptr };
53 efd::TypeSupport _type { new sensor_msgs::msg::CameraInfoPubSubType() };
54 CarlaListener _listener {};
55 bool _init {false};
57 };
58
60 return _impl_info->_init;
61 }
62
63 void CarlaRGBCameraPublisher::InitInfoData(uint32_t x_offset, uint32_t y_offset, uint32_t height, uint32_t width, float fov, bool do_rectify) {
64 _impl_info->_info = std::move(sensor_msgs::msg::CameraInfo(height, width, fov));
65 SetInfoRegionOfInterest(x_offset, y_offset, height, width, do_rectify);
66 _impl_info->_init = true;
67 }
68
70 return InitImage() && InitInfo();
71 }
72
74 if (_impl->_type == nullptr) {
75 std::cerr << "Invalid TypeSupport" << std::endl;
76 return false;
77 }
78
79 efd::DomainParticipantQos pqos = efd::PARTICIPANT_QOS_DEFAULT;
80 pqos.name(_name);
81 auto factory = efd::DomainParticipantFactory::get_instance();
82 _impl->_participant = factory->create_participant(0, pqos);
83 if (_impl->_participant == nullptr) {
84 std::cerr << "Failed to create DomainParticipant" << std::endl;
85 return false;
86 }
87 _impl->_type.register_type(_impl->_participant);
88
89 efd::PublisherQos pubqos = efd::PUBLISHER_QOS_DEFAULT;
90 _impl->_publisher = _impl->_participant->create_publisher(pubqos, nullptr);
91 if (_impl->_publisher == nullptr) {
92 std::cerr << "Failed to create Publisher" << std::endl;
93 return false;
94 }
95
96 efd::TopicQos tqos = efd::TOPIC_QOS_DEFAULT;
97 const std::string publisher_type {"/image"};
98 const std::string base { "rt/carla/" };
99 std::string topic_name = base;
100 if (!_parent.empty())
101 topic_name += _parent + "/";
102 topic_name += _name;
103 topic_name += publisher_type;
104 _impl->_topic = _impl->_participant->create_topic(topic_name, _impl->_type->getName(), tqos);
105 if (_impl->_topic == nullptr) {
106 std::cerr << "Failed to create Topic" << std::endl;
107 return false;
108 }
109 efd::DataWriterQos wqos = efd::DATAWRITER_QOS_DEFAULT;
110 wqos.endpoint().history_memory_policy = eprosima::fastrtps::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE;
111 efd::DataWriterListener* listener = (efd::DataWriterListener*)_impl->_listener._impl.get();
112 _impl->_datawriter = _impl->_publisher->create_datawriter(_impl->_topic, wqos, listener);
113 if (_impl->_datawriter == nullptr) {
114 std::cerr << "Failed to create DataWriter" << std::endl;
115 return false;
116 }
117
119 return true;
120 }
121
123 if (_impl_info->_type == nullptr) {
124 std::cerr << "Invalid TypeSupport" << std::endl;
125 return false;
126 }
127
128 efd::DomainParticipantQos pqos = efd::PARTICIPANT_QOS_DEFAULT;
129 pqos.name(_name);
130 auto factory = efd::DomainParticipantFactory::get_instance();
131 _impl_info->_participant = factory->create_participant(0, pqos);
132 if (_impl_info->_participant == nullptr) {
133 std::cerr << "Failed to create DomainParticipant" << std::endl;
134 return false;
135 }
136 _impl_info->_type.register_type(_impl_info->_participant);
137
138 efd::PublisherQos pubqos = efd::PUBLISHER_QOS_DEFAULT;
139 _impl_info->_publisher = _impl_info->_participant->create_publisher(pubqos, nullptr);
140 if (_impl_info->_publisher == nullptr) {
141 std::cerr << "Failed to create Publisher" << std::endl;
142 return false;
143 }
144
145 efd::TopicQos tqos = efd::TOPIC_QOS_DEFAULT;
146 const std::string publisher_type {"/camera_info"};
147 const std::string base { "rt/carla/" };
148 std::string topic_name = base;
149 if (!_parent.empty())
150 topic_name += _parent + "/";
151 topic_name += _name;
152 topic_name += publisher_type;
153 _impl_info->_topic = _impl_info->_participant->create_topic(topic_name, _impl_info->_type->getName(), tqos);
154 if (_impl_info->_topic == nullptr) {
155 std::cerr << "Failed to create Topic" << std::endl;
156 return false;
157 }
158 efd::DataWriterQos wqos = efd::DATAWRITER_QOS_DEFAULT;
159 efd::DataWriterListener* listener = (efd::DataWriterListener*)_impl_info->_listener._impl.get();
160 _impl_info->_datawriter = _impl_info->_publisher->create_datawriter(_impl_info->_topic, wqos, listener);
161 if (_impl_info->_datawriter == nullptr) {
162 std::cerr << "Failed to create DataWriter" << std::endl;
163 return false;
164 }
165
167 return true;
168 }
169
173
175 eprosima::fastrtps::rtps::InstanceHandle_t instance_handle;
176 eprosima::fastrtps::types::ReturnCode_t rcode = _impl->_datawriter->write(&_impl->_image, instance_handle);
177 if (rcode == erc::ReturnCodeValue::RETCODE_OK) {
178 return true;
179 }
180 if (rcode == erc::ReturnCodeValue::RETCODE_ERROR) {
181 std::cerr << "RETCODE_ERROR" << std::endl;
182 return false;
183 }
184 if (rcode == erc::ReturnCodeValue::RETCODE_UNSUPPORTED) {
185 std::cerr << "RETCODE_UNSUPPORTED" << std::endl;
186 return false;
187 }
188 if (rcode == erc::ReturnCodeValue::RETCODE_BAD_PARAMETER) {
189 std::cerr << "RETCODE_BAD_PARAMETER" << std::endl;
190 return false;
191 }
192 if (rcode == erc::ReturnCodeValue::RETCODE_PRECONDITION_NOT_MET) {
193 std::cerr << "RETCODE_PRECONDITION_NOT_MET" << std::endl;
194 return false;
195 }
196 if (rcode == erc::ReturnCodeValue::RETCODE_OUT_OF_RESOURCES) {
197 std::cerr << "RETCODE_OUT_OF_RESOURCES" << std::endl;
198 return false;
199 }
200 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ENABLED) {
201 std::cerr << "RETCODE_NOT_ENABLED" << std::endl;
202 return false;
203 }
204 if (rcode == erc::ReturnCodeValue::RETCODE_IMMUTABLE_POLICY) {
205 std::cerr << "RETCODE_IMMUTABLE_POLICY" << std::endl;
206 return false;
207 }
208 if (rcode == erc::ReturnCodeValue::RETCODE_INCONSISTENT_POLICY) {
209 std::cerr << "RETCODE_INCONSISTENT_POLICY" << std::endl;
210 return false;
211 }
212 if (rcode == erc::ReturnCodeValue::RETCODE_ALREADY_DELETED) {
213 std::cerr << "RETCODE_ALREADY_DELETED" << std::endl;
214 return false;
215 }
216 if (rcode == erc::ReturnCodeValue::RETCODE_TIMEOUT) {
217 std::cerr << "RETCODE_TIMEOUT" << std::endl;
218 return false;
219 }
220 if (rcode == erc::ReturnCodeValue::RETCODE_NO_DATA) {
221 std::cerr << "RETCODE_NO_DATA" << std::endl;
222 return false;
223 }
224 if (rcode == erc::ReturnCodeValue::RETCODE_ILLEGAL_OPERATION) {
225 std::cerr << "RETCODE_ILLEGAL_OPERATION" << std::endl;
226 return false;
227 }
228 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ALLOWED_BY_SECURITY) {
229 std::cerr << "RETCODE_NOT_ALLOWED_BY_SECURITY" << std::endl;
230 return false;
231 }
232 std::cerr << "UNKNOWN" << std::endl;
233 return false;
234 }
235
237 eprosima::fastrtps::rtps::InstanceHandle_t instance_handle;
238 // 执行信息的发布动作
239 eprosima::fastrtps::types::ReturnCode_t rcode = _impl_info->_datawriter->write(&_impl_info->_info, instance_handle);
240 if (rcode == erc::ReturnCodeValue::RETCODE_OK) {
241 return true;
242 }
243 if (rcode == erc::ReturnCodeValue::RETCODE_ERROR) {
244 std::cerr << "RETCODE_ERROR" << std::endl;
245 return false;
246 }
247 if (rcode == erc::ReturnCodeValue::RETCODE_UNSUPPORTED) {
248 std::cerr << "RETCODE_UNSUPPORTED" << std::endl;
249 return false;
250 }
251 if (rcode == erc::ReturnCodeValue::RETCODE_BAD_PARAMETER) {
252 std::cerr << "RETCODE_BAD_PARAMETER" << std::endl;
253 return false;
254 }
255 if (rcode == erc::ReturnCodeValue::RETCODE_PRECONDITION_NOT_MET) {
256 std::cerr << "RETCODE_PRECONDITION_NOT_MET" << std::endl;
257 return false;
258 }
259 if (rcode == erc::ReturnCodeValue::RETCODE_OUT_OF_RESOURCES) {
260 std::cerr << "RETCODE_OUT_OF_RESOURCES" << std::endl;
261 return false;
262 }
263 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ENABLED) {
264 std::cerr << "RETCODE_NOT_ENABLED" << std::endl;
265 return false;
266 }
267 if (rcode == erc::ReturnCodeValue::RETCODE_IMMUTABLE_POLICY) {
268 std::cerr << "RETCODE_IMMUTABLE_POLICY" << std::endl;
269 return false;
270 }
271 if (rcode == erc::ReturnCodeValue::RETCODE_INCONSISTENT_POLICY) {
272 std::cerr << "RETCODE_INCONSISTENT_POLICY" << std::endl;
273 return false;
274 }
275 if (rcode == erc::ReturnCodeValue::RETCODE_ALREADY_DELETED) {
276 std::cerr << "RETCODE_ALREADY_DELETED" << std::endl;
277 return false;
278 }
279 if (rcode == erc::ReturnCodeValue::RETCODE_TIMEOUT) {
280 std::cerr << "RETCODE_TIMEOUT" << std::endl;
281 return false;
282 }
283 if (rcode == erc::ReturnCodeValue::RETCODE_NO_DATA) {
284 std::cerr << "RETCODE_NO_DATA" << std::endl;
285 return false;
286 }
287 if (rcode == erc::ReturnCodeValue::RETCODE_ILLEGAL_OPERATION) {
288 std::cerr << "RETCODE_ILLEGAL_OPERATION" << std::endl;
289 return false;
290 }
291 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ALLOWED_BY_SECURITY) {
292 std::cerr << "RETCODE_NOT_ALLOWED_BY_SECURITY" << std::endl;
293 return false;
294 }
295 std::cerr << "UNKNOWN" << std::endl;
296 return false;
297 }
298
299void CarlaRGBCameraPublisher::SetImageData(int32_t seconds, uint32_t nanoseconds, uint32_t height, uint32_t width, const uint8_t* data) {
300 std::vector<uint8_t> vector_data;
301 const size_t size = height * width * 4;
302 vector_data.resize(size);
303 std::memcpy(&vector_data[0], &data[0], size);
304 SetImageData(seconds, nanoseconds, height, width, std::move(vector_data));
305 }
306
307 void CarlaRGBCameraPublisher::SetImageData(int32_t seconds, uint32_t nanoseconds, uint32_t height, uint32_t width, std::vector<uint8_t>&& data) {
308
310 time.sec(seconds);
311 time.nanosec(nanoseconds);
312
314 header.stamp(std::move(time));
315 header.frame_id(_frame_id);
316 _impl->_image.header(header);
317
318 _impl->_image.width(width);
319 _impl->_image.height(height);
320 _impl->_image.encoding("bgra8");
321 _impl->_image.is_bigendian(0);
322 _impl->_image.step(_impl->_image.width() * sizeof(uint8_t) * 4);
323 _impl->_image.data(std::move(data));
324 }
325
326 void CarlaRGBCameraPublisher::SetCameraInfoData(int32_t seconds, uint32_t nanoseconds) {
328 time.sec(seconds);
329 time.nanosec(nanoseconds);
330
332 header.stamp(std::move(time));
333 header.frame_id(_frame_id);
334 _impl_info->_info.header(header);
335 }
336
337 void CarlaRGBCameraPublisher::SetInfoRegionOfInterest( uint32_t x_offset, uint32_t y_offset, uint32_t height, uint32_t width, bool do_rectify) {
339 roi.x_offset(x_offset);
340 roi.y_offset(y_offset);
341 roi.height(height);
342 roi.width(width);
343 roi.do_rectify(do_rectify);
344 _impl_info->_info.roi(roi);
345 }
346
347 CarlaRGBCameraPublisher::CarlaRGBCameraPublisher(const char* ros_name, const char* parent) :
348 _impl(std::make_shared<CarlaRGBCameraPublisherImpl>()),
349 _impl_info(std::make_shared<CarlaCameraInfoPublisherImpl>()) {
350 _name = ros_name;
351 _parent = parent;
352 }
353
355 if (!_impl)
356 return;
357
358 if (_impl->_datawriter)
359 _impl->_publisher->delete_datawriter(_impl->_datawriter);
360
361 if (_impl->_publisher)
362 _impl->_participant->delete_publisher(_impl->_publisher);
363
364 if (_impl->_topic)
365 _impl->_participant->delete_topic(_impl->_topic);
366
367 if (_impl->_participant)
368 efd::DomainParticipantFactory::get_instance()->delete_participant(_impl->_participant);
369
370 if (!_impl_info)
371 return;
372
373 if (_impl_info->_datawriter)
374 _impl_info->_publisher->delete_datawriter(_impl_info->_datawriter);
375
376 if (_impl_info->_publisher)
377 _impl_info->_participant->delete_publisher(_impl_info->_publisher);
378
379 if (_impl_info->_topic)
380 _impl_info->_participant->delete_topic(_impl_info->_topic);
381
382 if (_impl_info->_participant)
383 efd::DomainParticipantFactory::get_instance()->delete_participant(_impl_info->_participant);
384 }
385
387 _frame_id = other._frame_id;
388 _name = other._name;
389 _parent = other._parent;
390 _impl = other._impl;
391 _impl_info = other._impl_info;
392 }
393
395 _frame_id = other._frame_id;
396 _name = other._name;
397 _parent = other._parent;
398 _impl = other._impl;
399 _impl_info = other._impl_info;
400
401 return *this;
402 }
403
405 _frame_id = std::move(other._frame_id);
406 _name = std::move(other._name);
407 _parent = std::move(other._parent);
408 _impl = std::move(other._impl);
409 _impl_info = std::move(other._impl_info);
410 }
411
413 _frame_id = std::move(other._frame_id);
414 _name = std::move(other._name);
415 _parent = std::move(other._parent);
416 _impl = std::move(other._impl);
417 _impl_info = std::move(other._impl_info);
418
419 return *this;
420 }
421}
422}
This class represents the structure Time defined by the user in the IDL file.
eProsima_user_DllExport void nanosec(uint32_t _nanosec)
This function sets a value in member nanosec
Definition Time.cpp:161
eProsima_user_DllExport void sec(int32_t _sec)
This function sets a value in member sec
Definition Time.cpp:133
const std::string & parent() const
std::shared_ptr< CarlaCameraInfoPublisherImpl > _impl_info
std::shared_ptr< CarlaRGBCameraPublisherImpl > _impl
void SetImageData(int32_t seconds, uint32_t nanoseconds, uint32_t height, uint32_t width, const uint8_t *data)
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="")
void InitInfoData(uint32_t x_offset, uint32_t y_offset, uint32_t height, uint32_t width, float fov, bool do_rectify)
CarlaRGBCameraPublisher & operator=(const CarlaRGBCameraPublisher &)
void SetCameraInfoData(int32_t seconds, uint32_t nanoseconds)
This class represents the TopicDataType of the type CameraInfo defined by the user in the IDL file.
This class represents the structure CameraInfo defined by the user in the IDL file.
Definition CameraInfo.h:74
This class represents the TopicDataType of the type Image defined by the user in the IDL file.
This class represents the structure Image defined by the user in the IDL file.
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
This class represents the structure Header defined by the user in the IDL file.
Definition Header.h:73
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 frame_id(const std::string &_frame_id)
This function copies the value in member frame_id
Definition Header.cpp:168
eprosima::fastrtps::types::ReturnCode_t erc
This file contains definitions of common data structures used in traffic manager.
Definition Carla.cpp:133