CARLA
Loading...
Searching...
No Matches
BasicSubscriber.cpp
Go to the documentation of this file.
1#include "BasicSubscriber.h"
2
6
7#include <fastdds/dds/domain/DomainParticipant.hpp>
8#include <fastdds/dds/subscriber/Subscriber.hpp>
9#include <fastdds/dds/topic/Topic.hpp>
10#include <fastdds/dds/subscriber/DataReader.hpp>
11#include <fastdds/dds/topic/TypeSupport.hpp>
12#include <fastdds/dds/subscriber/SampleInfo.hpp>
13
14#include <fastdds/dds/domain/qos/DomainParticipantQos.hpp>
15#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
16#include <fastdds/dds/subscriber/qos/SubscriberQos.hpp>
17#include <fastdds/dds/topic/qos/TopicQos.hpp>
18
19#include <fastrtps/attributes/ParticipantAttributes.h>
20#include <fastrtps/qos/QosPolicies.h>
21#include <fastdds/dds/subscriber/qos/DataReaderQos.hpp>
22#include <fastdds/dds/subscriber/DataReaderListener.hpp>
23
24
25namespace carla {
26namespace ros2 {
27
28 namespace efd = eprosima::fastdds::dds;
29 using erc = eprosima::fastrtps::types::ReturnCode_t;
30
32 efd::DomainParticipant* _participant { nullptr };
33 efd::Subscriber* _subscriber { nullptr };
34 efd::Topic* _topic { nullptr };
35 efd::DataReader* _datareader { nullptr };
36 efd::TypeSupport _type { new GenericCdrPubSubType<msg::String>() };
39 std::string _message {};
40 bool _new_message {false};
41 bool _alive {true};
42 void* _actor {nullptr};
43 };
44
46 if (_impl->_type == nullptr) {
47 std::cerr << "Invalid TypeSupport" << std::endl;
48 return false;
49 }
50
51 efd::DomainParticipantQos pqos = efd::PARTICIPANT_QOS_DEFAULT;
52 pqos.name(_name);
53 auto factory = efd::DomainParticipantFactory::get_instance();
54 _impl->_participant = factory->create_participant(0, pqos);
55 if (_impl->_participant == nullptr) {
56 std::cerr << "Failed to create DomainParticipant" << std::endl;
57 return false;
58 }
59 _impl->_type.register_type(_impl->_participant);
60
61 efd::SubscriberQos subqos = efd::SUBSCRIBER_QOS_DEFAULT;
62 _impl->_subscriber = _impl->_participant->create_subscriber(subqos, nullptr);
63 if (_impl->_subscriber == nullptr) {
64 std::cerr << "Failed to create Subscriber" << std::endl;
65 return false;
66 }
67
68 efd::TopicQos tqos = efd::TOPIC_QOS_DEFAULT;
69 const std::string base { "rt/carla/" };
70 const std::string subscriber_type {"/basic_subscriber_example"};
71 std::string topic_name = base;
72 if (!_parent.empty())
73 topic_name += _parent + "/";
74 topic_name += _name;
75 topic_name += subscriber_type;
76 _impl->_topic = _impl->_participant->create_topic(topic_name, _impl->_type->getName(), tqos);
77 if (_impl->_topic == nullptr) {
78 std::cerr << "Failed to create Topic" << std::endl;
79 return false;
80 }
81
82 efd::DataReaderQos rqos = efd::DATAREADER_QOS_DEFAULT;
83 efd::DataReaderListener* listener = (efd::DataReaderListener*)_impl->_listener._impl.get();
84 _impl->_datareader = _impl->_subscriber->create_datareader(_impl->_topic, rqos, listener);
85 if (_impl->_datareader == nullptr) {
86 std::cerr << "Failed to create DataReader" << std::endl;
87 return false;
88 }
89 return true;
90 }
91
92 void BasicSubscriber::ForwardMessage(const std::string& message) {
93 _impl->_message = message;
94 _impl->_new_message = true;
95 }
96
98 _impl->_alive = false;
99 }
100
102 _impl->_new_message = false;
103 return _impl->_message.c_str();
104 }
105
107 return _impl->_alive;
108 }
109
111 return _impl->_new_message;
112 }
113
115 return _impl->_actor;
116 }
117
118 BasicSubscriber::BasicSubscriber(void* actor, const char* ros_name, const char* parent) :
119 _impl(std::make_shared<BasicSubscriberImpl>()) {
120 _impl->_listener.SetOwner(this);
121 _impl->_actor = actor;
122 _name = ros_name;
123 _parent = parent;
124 }
125
127 if (!_impl)
128 return;
129
130 if (_impl->_datareader)
131 _impl->_subscriber->delete_datareader(_impl->_datareader);
132
133 if (_impl->_subscriber)
134 _impl->_participant->delete_subscriber(_impl->_subscriber);
135
136 if (_impl->_topic)
137 _impl->_participant->delete_topic(_impl->_topic);
138
139 if (_impl->_participant)
140 efd::DomainParticipantFactory::get_instance()->delete_participant(_impl->_participant);
141 }
142
144 _frame_id = other._frame_id;
145 _name = other._name;
146 _parent = other._parent;
147 _impl = other._impl;
148 _impl->_listener.SetOwner(this);
149 }
150
152 _frame_id = other._frame_id;
153 _name = other._name;
154 _parent = other._parent;
155 _impl = other._impl;
156 _impl->_listener.SetOwner(this);
157
158 return *this;
159 }
160
162 _frame_id = std::move(other._frame_id);
163 _name = std::move(other._name);
164 _parent = std::move(other._parent);
165 _impl = std::move(other._impl);
166 _impl->_listener.SetOwner(this);
167 }
168
170 _frame_id = std::move(other._frame_id);
171 _name = std::move(other._name);
172 _parent = std::move(other._parent);
173 _impl = std::move(other._impl);
174 _impl->_listener.SetOwner(this);
175
176 return *this;
177 }
178}
179}
std::shared_ptr< BasicSubscriberImpl > _impl
void ForwardMessage(const std::string &message)
BasicSubscriber(void *actor, const char *ros_name="", const char *parent="")
BasicSubscriber & operator=(const BasicSubscriber &)
const std::string & parent() const
Generic FastDDS TopicDataType that serializes carla::ros2::msg::* POD structs directly to CDR via Cdr...
eprosima::fastrtps::types::ReturnCode_t erc
This file contains definitions of common data structures used in traffic manager.
efd::DomainParticipant * _participant