CARLA
 
载入中...
搜索中...
未找到
CarlaCollisionPublisher.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
30 namespace efd = eprosima::fastdds::dds;
31 using erc = eprosima::fastrtps::types::ReturnCode_t;
32
34 efd::DomainParticipant* _participant { nullptr };
35 efd::Publisher* _publisher { nullptr };
36 efd::Topic* _topic { nullptr };
37 efd::DataWriter* _datawriter { nullptr };
41 };
42
44 if (_impl->_type == nullptr) {
45 std::cerr << "Invalid TypeSupport" << std::endl;
46 return false;
47 }
48
49 efd::DomainParticipantQos pqos = efd::PARTICIPANT_QOS_DEFAULT;
50 pqos.name(_name);
51 auto factory = efd::DomainParticipantFactory::get_instance();
52 _impl->_participant = factory->create_participant(0, pqos);
53 if (_impl->_participant == nullptr) {
54 std::cerr << "Failed to create DomainParticipant" << std::endl;
55 return false;
56 }
57 _impl->_type.register_type(_impl->_participant);
58
59 efd::PublisherQos pubqos = efd::PUBLISHER_QOS_DEFAULT;
60 _impl->_publisher = _impl->_participant->create_publisher(pubqos, nullptr);
61 if (_impl->_publisher == nullptr) {
62 std::cerr << "Failed to create Publisher" << std::endl;
63 return false;
64 }
65
66 efd::TopicQos tqos = efd::TOPIC_QOS_DEFAULT;
67 const std::string base { "rt/carla/" };
68 std::string topic_name = base;
69 if (!_parent.empty())
70 topic_name += _parent + "/";
71 topic_name += _name;
72 _impl->_topic = _impl->_participant->create_topic(topic_name, _impl->_type->getName(), tqos);
73 if (_impl->_topic == nullptr) {
74 std::cerr << "Failed to create Topic" << std::endl;
75 return false;
76 }
77
78 efd::DataWriterQos wqos = efd::DATAWRITER_QOS_DEFAULT;
79 wqos.endpoint().history_memory_policy = eprosima::fastrtps::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE;
80 efd::DataWriterListener* listener = (efd::DataWriterListener*)_impl->_listener._impl.get();
81 _impl->_datawriter = _impl->_publisher->create_datawriter(_impl->_topic, wqos, listener);
82 if (_impl->_datawriter == nullptr) {
83 std::cerr << "Failed to create DataWriter" << std::endl;
84 return false;
85 }
86
88 return true;
89 }
90
92 eprosima::fastrtps::rtps::InstanceHandle_t instance_handle;
93 eprosima::fastrtps::types::ReturnCode_t rcode = _impl->_datawriter->write(&_impl->_event, instance_handle);
94 if (rcode == erc::ReturnCodeValue::RETCODE_OK) {
95 return true;
96 }
97 if (rcode == erc::ReturnCodeValue::RETCODE_ERROR) {
98 std::cerr << "RETCODE_ERROR" << std::endl;
99 return false;
100 }
101 if (rcode == erc::ReturnCodeValue::RETCODE_UNSUPPORTED) {
102 std::cerr << "RETCODE_UNSUPPORTED" << std::endl;
103 return false;
104 }
105 if (rcode == erc::ReturnCodeValue::RETCODE_BAD_PARAMETER) {
106 std::cerr << "RETCODE_BAD_PARAMETER" << std::endl;
107 return false;
108 }
109 if (rcode == erc::ReturnCodeValue::RETCODE_PRECONDITION_NOT_MET) {
110 std::cerr << "RETCODE_PRECONDITION_NOT_MET" << std::endl;
111 return false;
112 }
113 if (rcode == erc::ReturnCodeValue::RETCODE_OUT_OF_RESOURCES) {
114 std::cerr << "RETCODE_OUT_OF_RESOURCES" << std::endl;
115 return false;
116 }
117 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ENABLED) {
118 std::cerr << "RETCODE_NOT_ENABLED" << std::endl;
119 return false;
120 }
121 if (rcode == erc::ReturnCodeValue::RETCODE_IMMUTABLE_POLICY) {
122 std::cerr << "RETCODE_IMMUTABLE_POLICY" << std::endl;
123 return false;
124 }
125 if (rcode == erc::ReturnCodeValue::RETCODE_INCONSISTENT_POLICY) {
126 std::cerr << "RETCODE_INCONSISTENT_POLICY" << std::endl;
127 return false;
128 }
129 if (rcode == erc::ReturnCodeValue::RETCODE_ALREADY_DELETED) {
130 std::cerr << "RETCODE_ALREADY_DELETED" << std::endl;
131 return false;
132 }
133 if (rcode == erc::ReturnCodeValue::RETCODE_TIMEOUT) {
134 std::cerr << "RETCODE_TIMEOUT" << std::endl;
135 return false;
136 }
137 if (rcode == erc::ReturnCodeValue::RETCODE_NO_DATA) {
138 std::cerr << "RETCODE_NO_DATA" << std::endl;
139 return false;
140 }
141 if (rcode == erc::ReturnCodeValue::RETCODE_ILLEGAL_OPERATION) {
142 std::cerr << "RETCODE_ILLEGAL_OPERATION" << std::endl;
143 return false;
144 }
145 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ALLOWED_BY_SECURITY) {
146 std::cerr << "RETCODE_NOT_ALLOWED_BY_SECURITY" << std::endl;
147 return false;
148 }
149 std::cerr << "UNKNOWN" << std::endl;
150 return false;
151 }
152
153void CarlaCollisionPublisher::SetData(int32_t seconds, uint32_t nanoseconds, uint32_t actor_id, float x, float y, float z) {
154 std::vector<float> vector_data ;
155 SetData(seconds, nanoseconds, actor_id, {x, y, z});
156 }
157
158 void CarlaCollisionPublisher::SetData(int32_t seconds, uint32_t nanoseconds, uint32_t actor_id, std::vector<float>&& data) {
160 time.sec(seconds);
161 time.nanosec(nanoseconds);
162
164 header.stamp(std::move(time));
165 header.frame_id(_frame_id);
166
168 impulse.x(data[0]);
169 impulse.y(data[1]);
170 impulse.z(data[2]);
171 _impl->_event.header(std::move(header));
172 _impl->_event.other_actor_id(actor_id);
173 _impl->_event.normal_impulse(impulse);
174 }
175
176 CarlaCollisionPublisher::CarlaCollisionPublisher(const char* ros_name, const char* parent) :
177 _impl(std::make_shared<CarlaCollisionPublisherImpl>()) {
178 _name = ros_name;
179 _parent = parent;
180 }
181
183 if (!_impl)
184 return;
185
186 if (_impl->_datawriter)
187 _impl->_publisher->delete_datawriter(_impl->_datawriter);
188
189 if (_impl->_publisher)
190 _impl->_participant->delete_publisher(_impl->_publisher);
191
192 if (_impl->_topic)
193 _impl->_participant->delete_topic(_impl->_topic);
194
195 if (_impl->_participant)
196 efd::DomainParticipantFactory::get_instance()->delete_participant(_impl->_participant);
197 }
198
200 _frame_id = other._frame_id;
201 _name = other._name;
202 _parent = other._parent;
203 _impl = other._impl;
204 }
205
207 _frame_id = other._frame_id;
208 _name = other._name;
209 _parent = other._parent;
210 _impl = other._impl;
211
212 return *this;
213 }
214
216 _frame_id = std::move(other._frame_id);
217 _name = std::move(other._name);
218 _parent = std::move(other._parent);
219 _impl = std::move(other._impl);
220 }
221
223 _frame_id = std::move(other._frame_id);
224 _name = std::move(other._name);
225 _parent = std::move(other._parent);
226 _impl = std::move(other._impl);
227
228 return *this;
229 }
230}
231}
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
std::shared_ptr< CarlaCollisionPublisherImpl > _impl
void SetData(int32_t seconds, uint32_t nanoseconds, uint32_t actor_id, float x, float y, float z)
CarlaCollisionPublisher(const char *ros_name="", const char *parent="")
CarlaCollisionPublisher & operator=(const CarlaCollisionPublisher &)
const std::string & parent() const
This class represents the TopicDataType of the type CarlaCollisionEvent defined by the user in the ID...
This class represents the structure CarlaCollisionEvent defined by the user in the IDL file.
This class represents the structure Vector3 defined by the user in the IDL file.
Definition Vector3.h:72
eProsima_user_DllExport void x(double _x)
This function sets a value in member x
Definition Vector3.cpp:143
eProsima_user_DllExport void z(double _z)
This function sets a value in member z
Definition Vector3.cpp:199
eProsima_user_DllExport void y(double _y)
This function sets a value in member y
Definition Vector3.cpp:171
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
carla_msgs::msg::CarlaCollisionEvent _event