CARLA
CarlaClockPublisher.cpp
Go to the documentation of this file.
1 #define _GLIBCXX_USE_CXX11_ABI 0
2 
3 #include "CarlaClockPublisher.h"
4 
5 #include <string>
6 
9 
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>
15 
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>
20 
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>
25 
26 
27 namespace carla {
28 namespace ros2 {
29  namespace efd = eprosima::fastdds::dds;
30  using erc = eprosima::fastrtps::types::ReturnCode_t;
31 
33  efd::DomainParticipant* _participant { nullptr };
34  efd::Publisher* _publisher { nullptr };
35  efd::Topic* _topic { nullptr };
36  efd::DataWriter* _datawriter { nullptr };
37  efd::TypeSupport _type { new rosgraph::msg::ClockPubSubType() };
40  };
41 
43  if (_impl->_type == nullptr) {
44  std::cerr << "Invalid TypeSupport" << std::endl;
45  return false;
46  }
47  efd::DomainParticipantQos pqos = efd::PARTICIPANT_QOS_DEFAULT;
48  pqos.name(_name);
49  auto factory = efd::DomainParticipantFactory::get_instance();
50  _impl->_participant = factory->create_participant(0, pqos);
51  if (_impl->_participant == nullptr) {
52  std::cerr << "Failed to create DomainParticipant" << std::endl;
53  return false;
54  }
55  _impl->_type.register_type(_impl->_participant);
56 
57  efd::PublisherQos pubqos = efd::PUBLISHER_QOS_DEFAULT;
58  _impl->_publisher = _impl->_participant->create_publisher(pubqos, nullptr);
59  if (_impl->_publisher == nullptr) {
60  std::cerr << "Failed to create Publisher" << std::endl;
61  return false;
62  }
63 
64  efd::TopicQos tqos = efd::TOPIC_QOS_DEFAULT;
65  const std::string topic_name { "rt/clock" };
66  _impl->_topic = _impl->_participant->create_topic(topic_name, _impl->_type->getName(), tqos);
67  if (_impl->_topic == nullptr) {
68  std::cerr << "Failed to create Topic" << std::endl;
69  return false;
70  }
71 
72  efd::DataWriterQos wqos = efd::DATAWRITER_QOS_DEFAULT;
73  efd::DataWriterListener* listener = (efd::DataWriterListener*)_impl->_listener._impl.get();
74  _impl->_datawriter = _impl->_publisher->create_datawriter(_impl->_topic, wqos, listener);
75  if (_impl->_datawriter == nullptr) {
76  std::cerr << "Failed to create DataWriter" << std::endl;
77  return false;
78  }
79  _frame_id = _name;
80  return true;
81  }
82 
85  eprosima::fastrtps::types::ReturnCode_t rcode = _impl->_datawriter->write(&_impl->_clock, instance_handle);
86  if (rcode == erc::ReturnCodeValue::RETCODE_OK) {
87  return true;
88  }
89  if (rcode == erc::ReturnCodeValue::RETCODE_ERROR) {
90  std::cerr << "RETCODE_ERROR" << std::endl;
91  return false;
92  }
93  if (rcode == erc::ReturnCodeValue::RETCODE_UNSUPPORTED) {
94  std::cerr << "RETCODE_UNSUPPORTED" << std::endl;
95  return false;
96  }
97  if (rcode == erc::ReturnCodeValue::RETCODE_BAD_PARAMETER) {
98  std::cerr << "RETCODE_BAD_PARAMETER" << std::endl;
99  return false;
100  }
101  if (rcode == erc::ReturnCodeValue::RETCODE_PRECONDITION_NOT_MET) {
102  std::cerr << "RETCODE_PRECONDITION_NOT_MET" << std::endl;
103  return false;
104  }
105  if (rcode == erc::ReturnCodeValue::RETCODE_OUT_OF_RESOURCES) {
106  std::cerr << "RETCODE_OUT_OF_RESOURCES" << std::endl;
107  return false;
108  }
109  if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ENABLED) {
110  std::cerr << "RETCODE_NOT_ENABLED" << std::endl;
111  return false;
112  }
113  if (rcode == erc::ReturnCodeValue::RETCODE_IMMUTABLE_POLICY) {
114  std::cerr << "RETCODE_IMMUTABLE_POLICY" << std::endl;
115  return false;
116  }
117  if (rcode == erc::ReturnCodeValue::RETCODE_INCONSISTENT_POLICY) {
118  std::cerr << "RETCODE_INCONSISTENT_POLICY" << std::endl;
119  return false;
120  }
121  if (rcode == erc::ReturnCodeValue::RETCODE_ALREADY_DELETED) {
122  std::cerr << "RETCODE_ALREADY_DELETED" << std::endl;
123  return false;
124  }
125  if (rcode == erc::ReturnCodeValue::RETCODE_TIMEOUT) {
126  std::cerr << "RETCODE_TIMEOUT" << std::endl;
127  return false;
128  }
129  if (rcode == erc::ReturnCodeValue::RETCODE_NO_DATA) {
130  std::cerr << "RETCODE_NO_DATA" << std::endl;
131  return false;
132  }
133  if (rcode == erc::ReturnCodeValue::RETCODE_ILLEGAL_OPERATION) {
134  std::cerr << "RETCODE_ILLEGAL_OPERATION" << std::endl;
135  return false;
136  }
137  if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ALLOWED_BY_SECURITY) {
138  std::cerr << "RETCODE_NOT_ALLOWED_BY_SECURITY" << std::endl;
139  return false;
140  }
141  std::cerr << "UNKNOWN" << std::endl;
142  return false;
143  }
144 
145  void CarlaClockPublisher::SetData(int32_t sec, uint32_t nanosec) {
146  _impl->_clock.clock().sec(sec);
147  _impl->_clock.clock().nanosec(nanosec);
148  }
149 
150  CarlaClockPublisher::CarlaClockPublisher(const char* ros_name, const char* parent) :
151  _impl(std::make_shared<CarlaClockPublisherImpl>()) {
152  _name = ros_name;
153  _parent = parent;
154  }
155 
157  if (!_impl)
158  return;
159 
160  if (_impl->_datawriter)
161  _impl->_publisher->delete_datawriter(_impl->_datawriter);
162 
163  if (_impl->_publisher)
164  _impl->_participant->delete_publisher(_impl->_publisher);
165 
166  if (_impl->_topic)
167  _impl->_participant->delete_topic(_impl->_topic);
168 
169  if (_impl->_participant)
170  efd::DomainParticipantFactory::get_instance()->delete_participant(_impl->_participant);
171  }
172 
174  _frame_id = other._frame_id;
175  _name = other._name;
176  _parent = other._parent;
177  _impl = other._impl;
178  }
179 
181  _frame_id = other._frame_id;
182  _name = other._name;
183  _parent = other._parent;
184  _impl = other._impl;
185 
186  return *this;
187  }
188 
190  _frame_id = std::move(other._frame_id);
191  _name = std::move(other._name);
192  _parent = std::move(other._parent);
193  _impl = std::move(other._impl);
194  }
195 
197  _frame_id = std::move(other._frame_id);
198  _name = std::move(other._name);
199  _parent = std::move(other._parent);
200  _impl = std::move(other._impl);
201 
202  return *this;
203  }
204 }
205 }
eprosima::fastrtps::rtps::InstanceHandle_t InstanceHandle_t
eprosima::fastrtps::types::ReturnCode_t erc
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
std::shared_ptr< CarlaClockPublisherImpl > _impl
CarlaClockPublisher(const char *ros_name="", const char *parent="")
This class represents the TopicDataType of the type Clock defined by the user in the IDL file...
This class represents the structure Clock defined by the user in the IDL file.
Definition: Clock.h:72
const std::string & parent() const
CarlaClockPublisher & operator=(const CarlaClockPublisher &)
void SetData(int32_t sec, uint32_t nanosec)