CARLA
 
载入中...
搜索中...
未找到
CarlaSpeedometerSensor.cpp
浏览该文件的文档.
1#define _GLIBCXX_USE_CXX11_ABI 0
2
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
27namespace carla {
28namespace 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 std_msgs::msg::Float32PubSubType() };
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 base { "rt/carla/" };
66 std::string topic_name = base;
67 if (!_parent.empty())
68 topic_name += _parent + "/";
69 topic_name += _name;
70 _impl->_topic = _impl->_participant->create_topic(topic_name, _impl->_type->getName(), tqos);
71 if (_impl->_topic == nullptr) {
72 std::cerr << "Failed to create Topic" << std::endl;
73 return false;
74 }
75
76 efd::DataWriterQos wqos = efd::DATAWRITER_QOS_DEFAULT;
77 wqos.endpoint().history_memory_policy = eprosima::fastrtps::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE;
78 efd::DataWriterListener* listener = (efd::DataWriterListener*)_impl->_listener._impl.get();
79 _impl->_datawriter = _impl->_publisher->create_datawriter(_impl->_topic, wqos, listener);
80 if (_impl->_datawriter == nullptr) {
81 std::cerr << "Failed to create DataWriter" << std::endl;
82 return false;
83 }
85 return true;
86 }
87
89 eprosima::fastrtps::rtps::InstanceHandle_t instance_handle;
90 eprosima::fastrtps::types::ReturnCode_t rcode = _impl->_datawriter->write(&_impl->_float, instance_handle);
91 if (rcode == erc::ReturnCodeValue::RETCODE_OK) {
92 return true;
93 }
94 if (rcode == erc::ReturnCodeValue::RETCODE_ERROR) {
95 std::cerr << "RETCODE_ERROR" << std::endl;
96 return false;
97 }
98 if (rcode == erc::ReturnCodeValue::RETCODE_UNSUPPORTED) {
99 std::cerr << "RETCODE_UNSUPPORTED" << std::endl;
100 return false;
101 }
102 if (rcode == erc::ReturnCodeValue::RETCODE_BAD_PARAMETER) {
103 std::cerr << "RETCODE_BAD_PARAMETER" << std::endl;
104 return false;
105 }
106 if (rcode == erc::ReturnCodeValue::RETCODE_PRECONDITION_NOT_MET) {
107 std::cerr << "RETCODE_PRECONDITION_NOT_MET" << std::endl;
108 return false;
109 }
110 if (rcode == erc::ReturnCodeValue::RETCODE_OUT_OF_RESOURCES) {
111 std::cerr << "RETCODE_OUT_OF_RESOURCES" << std::endl;
112 return false;
113 }
114 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ENABLED) {
115 std::cerr << "RETCODE_NOT_ENABLED" << std::endl;
116 return false;
117 }
118 if (rcode == erc::ReturnCodeValue::RETCODE_IMMUTABLE_POLICY) {
119 std::cerr << "RETCODE_IMMUTABLE_POLICY" << std::endl;
120 return false;
121 }
122 if (rcode == erc::ReturnCodeValue::RETCODE_INCONSISTENT_POLICY) {
123 std::cerr << "RETCODE_INCONSISTENT_POLICY" << std::endl;
124 return false;
125 }
126 if (rcode == erc::ReturnCodeValue::RETCODE_ALREADY_DELETED) {
127 std::cerr << "RETCODE_ALREADY_DELETED" << std::endl;
128 return false;
129 }
130 if (rcode == erc::ReturnCodeValue::RETCODE_TIMEOUT) {
131 std::cerr << "RETCODE_TIMEOUT" << std::endl;
132 return false;
133 }
134 if (rcode == erc::ReturnCodeValue::RETCODE_NO_DATA) {
135 std::cerr << "RETCODE_NO_DATA" << std::endl;
136 return false;
137 }
138 if (rcode == erc::ReturnCodeValue::RETCODE_ILLEGAL_OPERATION) {
139 std::cerr << "RETCODE_ILLEGAL_OPERATION" << std::endl;
140 return false;
141 }
142 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ALLOWED_BY_SECURITY) {
143 std::cerr << "RETCODE_NOT_ALLOWED_BY_SECURITY" << std::endl;
144 return false;
145 }
146 std::cerr << "UNKNOWN" << std::endl;
147 return false;
148 }
149
151 _impl->_float.data(data);
152 }
153
154 CarlaSpeedometerSensor::CarlaSpeedometerSensor(const char* ros_name, const char* parent) :
155 _impl(std::make_shared<CarlaSpeedometerSensorImpl>()) {
156 _name = ros_name;
157 _parent = parent;
158 }
159
161 if (!_impl)
162 return;
163
164 if (_impl->_datawriter)
165 _impl->_publisher->delete_datawriter(_impl->_datawriter);
166
167 if (_impl->_publisher)
168 _impl->_participant->delete_publisher(_impl->_publisher);
169
170 if (_impl->_topic)
171 _impl->_participant->delete_topic(_impl->_topic);
172
173 if (_impl->_participant)
174 efd::DomainParticipantFactory::get_instance()->delete_participant(_impl->_participant);
175 }
176
178 _frame_id = other._frame_id;
179 _name = other._name;
180 _parent = other._parent;
181 _impl = other._impl;
182 }
183
185 _frame_id = other._frame_id;
186 _name = other._name;
187 _parent = other._parent;
188 _impl = other._impl;
189
190 return *this;
191 }
192
194 _frame_id = std::move(other._frame_id);
195 _name = std::move(other._name);
196 _parent = std::move(other._parent);
197 _impl = std::move(other._impl);
198 }
199
201 _frame_id = std::move(other._frame_id);
202 _name = std::move(other._name);
203 _parent = std::move(other._parent);
204 _impl = std::move(other._impl);
205
206 return *this;
207 }
208}
209}
const std::string & parent() const
CarlaSpeedometerSensor & operator=(const CarlaSpeedometerSensor &)
std::shared_ptr< CarlaSpeedometerSensorImpl > _impl
CarlaSpeedometerSensor(const char *ros_name="", const char *parent="")
This class represents the TopicDataType of the type Float32 defined by the user in the IDL file.
This class represents the structure Float32 defined by the user in the IDL file.
Definition Float32.h:71
eprosima::fastrtps::types::ReturnCode_t erc
This file contains definitions of common data structures used in traffic manager.
Definition Carla.cpp:133