CARLA
 
载入中...
搜索中...
未找到
CarlaEgoVehicleControlSubscriber.cpp
浏览该文件的文档.
1#define _GLIBCXX_USE_CXX11_ABI 0
2
4
8
9#include <fastdds/dds/domain/DomainParticipant.hpp>
10#include <fastdds/dds/subscriber/Subscriber.hpp>
11#include <fastdds/dds/topic/Topic.hpp>
12#include <fastdds/dds/subscriber/DataReader.hpp>
13#include <fastdds/dds/topic/TypeSupport.hpp>
14#include <fastdds/dds/subscriber/SampleInfo.hpp>
15
16#include <fastdds/dds/domain/qos/DomainParticipantQos.hpp>
17#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
18#include <fastdds/dds/subscriber/qos/SubscriberQos.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/subscriber/qos/DataReaderQos.hpp>
24#include <fastdds/dds/subscriber/DataReaderListener.hpp>
25
26
27namespace carla {
28namespace ros2 {
29
30 namespace efd = eprosima::fastdds::dds;
31 using erc = eprosima::fastrtps::types::ReturnCode_t;
32
34 efd::DomainParticipant* _participant { nullptr };
35 efd::Subscriber* _subscriber { nullptr };
36 efd::Topic* _topic { nullptr };
37 efd::DataReader* _datareader { nullptr };
42 bool _new_message {false};
43 bool _alive {true};
44 void* _vehicle {nullptr};
45 };
46
48 if (_impl->_type == nullptr) {
49 std::cerr << "Invalid TypeSupport" << std::endl;
50 return false;
51 }
52
53 efd::DomainParticipantQos pqos = efd::PARTICIPANT_QOS_DEFAULT;
54 pqos.name(_name);
55 auto factory = efd::DomainParticipantFactory::get_instance();
56 _impl->_participant = factory->create_participant(0, pqos);
57 if (_impl->_participant == nullptr) {
58 std::cerr << "Failed to create DomainParticipant" << std::endl;
59 return false;
60 }
61 _impl->_type.register_type(_impl->_participant);
62
63 efd::SubscriberQos subqos = efd::SUBSCRIBER_QOS_DEFAULT;
64 _impl->_subscriber = _impl->_participant->create_subscriber(subqos, nullptr);
65 if (_impl->_subscriber == nullptr) {
66 std::cerr << "Failed to create Subscriber" << std::endl;
67 return false;
68 }
69
70 efd::TopicQos tqos = efd::TOPIC_QOS_DEFAULT;
71 const std::string base { "rt/carla/" };
72 const std::string publisher_type {"/vehicle_control_cmd"};
73 std::string topic_name = base;
74 if (!_parent.empty())
75 topic_name += _parent + "/";
76 topic_name += _name;
77 topic_name += publisher_type;
78 _impl->_topic = _impl->_participant->create_topic(topic_name, _impl->_type->getName(), tqos);
79 if (_impl->_topic == nullptr) {
80 std::cerr << "Failed to create Topic" << std::endl;
81 return false;
82 }
83
84 efd::DataReaderQos rqos = efd::DATAREADER_QOS_DEFAULT;
85 efd::DataReaderListener* listener = (efd::DataReaderListener*)_impl->_listener._impl.get();
86 _impl->_datareader = _impl->_subscriber->create_datareader(_impl->_topic, rqos, listener);
87 if (_impl->_datareader == nullptr) {
88 std::cerr << "Failed to create DataReader" << std::endl;
89 return false;
90 }
91 return true;
92 }
93
95 efd::SampleInfo info;
96 eprosima::fastrtps::types::ReturnCode_t rcode = _impl->_datareader->take_next_sample(&_impl->_event, &info);
97 if (rcode == erc::ReturnCodeValue::RETCODE_OK) {
98 return true;
99 }
100 if (rcode == erc::ReturnCodeValue::RETCODE_ERROR) {
101 std::cerr << "RETCODE_ERROR" << std::endl;
102 return false;
103 }
104 if (rcode == erc::ReturnCodeValue::RETCODE_UNSUPPORTED) {
105 std::cerr << "RETCODE_UNSUPPORTED" << std::endl;
106 return false;
107 }
108 if (rcode == erc::ReturnCodeValue::RETCODE_BAD_PARAMETER) {
109 std::cerr << "RETCODE_BAD_PARAMETER" << std::endl;
110 return false;
111 }
112 if (rcode == erc::ReturnCodeValue::RETCODE_PRECONDITION_NOT_MET) {
113 std::cerr << "RETCODE_PRECONDITION_NOT_MET" << std::endl;
114 return false;
115 }
116 if (rcode == erc::ReturnCodeValue::RETCODE_OUT_OF_RESOURCES) {
117 std::cerr << "RETCODE_OUT_OF_RESOURCES" << std::endl;
118 return false;
119 }
120 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ENABLED) {
121 std::cerr << "RETCODE_NOT_ENABLED" << std::endl;
122 return false;
123 }
124 if (rcode == erc::ReturnCodeValue::RETCODE_IMMUTABLE_POLICY) {
125 std::cerr << "RETCODE_IMMUTABLE_POLICY" << std::endl;
126 return false;
127 }
128 if (rcode == erc::ReturnCodeValue::RETCODE_INCONSISTENT_POLICY) {
129 std::cerr << "RETCODE_INCONSISTENT_POLICY" << std::endl;
130 return false;
131 }
132 if (rcode == erc::ReturnCodeValue::RETCODE_ALREADY_DELETED) {
133 std::cerr << "RETCODE_ALREADY_DELETED" << std::endl;
134 return false;
135 }
136 if (rcode == erc::ReturnCodeValue::RETCODE_TIMEOUT) {
137 std::cerr << "RETCODE_TIMEOUT" << std::endl;
138 return false;
139 }
140 if (rcode == erc::ReturnCodeValue::RETCODE_NO_DATA) {
141 std::cerr << "RETCODE_NO_DATA" << std::endl;
142 return false;
143 }
144 if (rcode == erc::ReturnCodeValue::RETCODE_ILLEGAL_OPERATION) {
145 std::cerr << "RETCODE_ILLEGAL_OPERATION" << std::endl;
146 return false;
147 }
148 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ALLOWED_BY_SECURITY) {
149 std::cerr << "RETCODE_NOT_ALLOWED_BY_SECURITY" << std::endl;
150 return false;
151 }
152 std::cerr << "UNKNOWN" << std::endl;
153 return false;
154 }
155
157 _impl->_control = control;
158 _impl->_new_message = true;
159 }
160
164
166 _impl->_new_message = false;
167 return _impl->_control;
168 }
169
171 return _impl->_alive;
172 }
173
175 return _impl->_new_message;
176 }
177
179 return _impl->_vehicle;
180 }
181
182 CarlaEgoVehicleControlSubscriber::CarlaEgoVehicleControlSubscriber(void* vehicle, const char* ros_name, const char* parent) :
183 _impl(std::make_shared<CarlaEgoVehicleControlSubscriberImpl>()) {
184 _impl->_listener.SetOwner(this);
185 _impl->_vehicle = vehicle;
186 _name = ros_name;
187 _parent = parent;
188 }
189
191 if (!_impl)
192 return;
193
194 if (_impl->_datareader)
195 _impl->_subscriber->delete_datareader(_impl->_datareader);
196
197 if (_impl->_subscriber)
198 _impl->_participant->delete_subscriber(_impl->_subscriber);
199
200 if (_impl->_topic)
201 _impl->_participant->delete_topic(_impl->_topic);
202
203 if (_impl->_participant)
204 efd::DomainParticipantFactory::get_instance()->delete_participant(_impl->_participant);
205 }
206
208 _frame_id = other._frame_id;
209 _name = other._name;
210 _parent = other._parent;
211 _impl = other._impl;
212 _impl->_listener.SetOwner(this);
213 }
214
216 _frame_id = other._frame_id;
217 _name = other._name;
218 _parent = other._parent;
219 _impl = other._impl;
220 _impl->_listener.SetOwner(this);
221
222 return *this;
223 }
224
226 _frame_id = std::move(other._frame_id);
227 _name = std::move(other._name);
228 _parent = std::move(other._parent);
229 _impl = std::move(other._impl);
230 _impl->_listener.SetOwner(this);
231 }
232
234 _frame_id = std::move(other._frame_id);
235 _name = std::move(other._name);
236 _parent = std::move(other._parent);
237 _impl = std::move(other._impl);
238 _impl->_listener.SetOwner(this);
239
240 return *this;
241 }
242}
243}
CarlaEgoVehicleControlSubscriber & operator=(const CarlaEgoVehicleControlSubscriber &)
CarlaEgoVehicleControlSubscriber(void *vehicle, const char *ros_name="", const char *parent="")
std::shared_ptr< CarlaEgoVehicleControlSubscriberImpl > _impl
const std::string & parent() const
This class represents the TopicDataType of the type CarlaEgoVehicleControl defined by the user in the...
This class represents the structure CarlaEgoVehicleControl defined by the user in the IDL file.
eprosima::fastrtps::types::ReturnCode_t erc
This file contains definitions of common data structures used in traffic manager.
Definition Carla.cpp:133