CARLA
 
载入中...
搜索中...
未找到
CarlaTransformPublisher.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 tf2_msgs::msg::TFMessagePubSubType() };
40
41 float last_translation[3] = {0.0f};
42 float last_rotation[3] = {0.0f};
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::PublisherQos pubqos = efd::PUBLISHER_QOS_DEFAULT;
64 _impl->_publisher = _impl->_participant->create_publisher(pubqos, nullptr);
65 if (_impl->_publisher == nullptr) {
66 std::cerr << "Failed to create Publisher" << std::endl;
67 return false;
68 }
69
70 efd::TopicQos tqos = efd::TOPIC_QOS_DEFAULT;
71 const std::string topic_name { "rt/tf" };
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 }
87 return true;
88 }
89
91 eprosima::fastrtps::rtps::InstanceHandle_t instance_handle;
92 eprosima::fastrtps::types::ReturnCode_t rcode = _impl->_datawriter->write(&_impl->_transform, instance_handle);
93 if (rcode == erc::ReturnCodeValue::RETCODE_OK) {
94 return true;
95 }
96 if (rcode == erc::ReturnCodeValue::RETCODE_ERROR) {
97 std::cerr << "RETCODE_ERROR" << std::endl;
98 return false;
99 }
100 if (rcode == erc::ReturnCodeValue::RETCODE_UNSUPPORTED) {
101 std::cerr << "RETCODE_UNSUPPORTED" << std::endl;
102 return false;
103 }
104 if (rcode == erc::ReturnCodeValue::RETCODE_BAD_PARAMETER) {
105 std::cerr << "RETCODE_BAD_PARAMETER" << std::endl;
106 return false;
107 }
108 if (rcode == erc::ReturnCodeValue::RETCODE_PRECONDITION_NOT_MET) {
109 std::cerr << "RETCODE_PRECONDITION_NOT_MET" << std::endl;
110 return false;
111 }
112 if (rcode == erc::ReturnCodeValue::RETCODE_OUT_OF_RESOURCES) {
113 std::cerr << "RETCODE_OUT_OF_RESOURCES" << std::endl;
114 return false;
115 }
116 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ENABLED) {
117 std::cerr << "RETCODE_NOT_ENABLED" << std::endl;
118 return false;
119 }
120 if (rcode == erc::ReturnCodeValue::RETCODE_IMMUTABLE_POLICY) {
121 std::cerr << "RETCODE_IMMUTABLE_POLICY" << std::endl;
122 return false;
123 }
124 if (rcode == erc::ReturnCodeValue::RETCODE_INCONSISTENT_POLICY) {
125 std::cerr << "RETCODE_INCONSISTENT_POLICY" << std::endl;
126 return false;
127 }
128 if (rcode == erc::ReturnCodeValue::RETCODE_ALREADY_DELETED) {
129 std::cerr << "RETCODE_ALREADY_DELETED" << std::endl;
130 return false;
131 }
132 if (rcode == erc::ReturnCodeValue::RETCODE_TIMEOUT) {
133 std::cerr << "RETCODE_TIMEOUT" << std::endl;
134 return false;
135 }
136 if (rcode == erc::ReturnCodeValue::RETCODE_NO_DATA) {
137 std::cerr << "RETCODE_NO_DATA" << std::endl;
138 return false;
139 }
140 if (rcode == erc::ReturnCodeValue::RETCODE_ILLEGAL_OPERATION) {
141 std::cerr << "RETCODE_ILLEGAL_OPERATION" << std::endl;
142 return false;
143 }
144 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ALLOWED_BY_SECURITY) {
145 std::cerr << "RETCODE_NOT_ALLOWED_BY_SECURITY" << std::endl;
146 return false;
147 }
148 std::cerr << "UNKNOWN" << std::endl;
149 return false;
150 }
151
152 void CarlaTransformPublisher::SetData(int32_t seconds, uint32_t nanoseconds, const float* translation, const float* rotation) {
153
154 int same_translation = std::memcmp(translation, _impl->last_translation, sizeof(float) * 3);
155 int same_rotation = std::memcmp(rotation, _impl->last_rotation, sizeof(float) * 3);
156 if (same_translation != 0 || same_rotation != 0) {
157 std::memcpy(_impl->last_translation, translation, sizeof(float) * 3);
158 std::memcpy(_impl->last_rotation, rotation, sizeof(float) * 3);
159
160 const float tx = *translation++;
161 const float ty = *translation++;
162 const float tz = *translation++;
163
164 const float rx = ((*rotation++) * -1.0f) * (M_PIf32 / 180.0f);
165 const float ry = ((*rotation++) * -1.0f) * (M_PIf32 / 180.0f);
166 const float rz = *rotation++ * (M_PIf32 / 180.0f);
167
168 const float cr = cosf(rz * 0.5f);
169 const float sr = sinf(rz * 0.5f);
170 const float cp = cosf(rx * 0.5f);
171 const float sp = sinf(rx * 0.5f);
172 const float cy = cosf(ry * 0.5f);
173 const float sy = sinf(ry * 0.5f);
174
175 _impl->vec_translation.x(tx);
176 _impl->vec_translation.y(-ty);
177 _impl->vec_translation.z(tz);
178
179 _impl->vec_rotation.w(cr * cp * cy + sr * sp * sy);
180 _impl->vec_rotation.x(sr * cp * cy - cr * sp * sy);
181 _impl->vec_rotation.y(cr * sp * cy + sr * cp * sy);
182 _impl->vec_rotation.z(cr * cp * sy - sr * sp * cy);
183 }
184
186 time.sec(seconds);
187 time.nanosec(nanoseconds);
188
190 header.stamp(std::move(time));
191 header.frame_id(_parent);
192
194 t.rotation(_impl->vec_rotation);
195 t.translation(_impl->vec_translation);
196
198 ts.header(std::move(header));
199 ts.transform(std::move(t));
201 _impl->_transform.transforms({ts});
202 }
203
204 CarlaTransformPublisher::CarlaTransformPublisher(const char* ros_name, const char* parent) :
205 _impl(std::make_shared<CarlaTransformPublisherImpl>()) {
206 _name = ros_name;
207 _parent = parent;
208 }
209
211 if (!_impl)
212 return;
213
214 if (_impl->_datawriter)
215 _impl->_publisher->delete_datawriter(_impl->_datawriter);
216
217 if (_impl->_publisher)
218 _impl->_participant->delete_publisher(_impl->_publisher);
219
220 if (_impl->_topic)
221 _impl->_participant->delete_topic(_impl->_topic);
222
223 if (_impl->_participant)
224 efd::DomainParticipantFactory::get_instance()->delete_participant(_impl->_participant);
225 }
226
228 _frame_id = other._frame_id;
229 _name = other._name;
230 _parent = other._parent;
231 _impl = other._impl;
232 }
233
235 _frame_id = other._frame_id;
236 _name = other._name;
237 _parent = other._parent;
238 _impl = other._impl;
239
240 return *this;
241 }
242
244 _frame_id = std::move(other._frame_id);
245 _name = std::move(other._name);
246 _parent = std::move(other._parent);
247 _impl = std::move(other._impl);
248 }
249
251 _frame_id = std::move(other._frame_id);
252 _name = std::move(other._name);
253 _parent = std::move(other._parent);
254 _impl = std::move(other._impl);
255
256 return *this;
257 }
258}
259}
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
const std::string & parent() const
std::shared_ptr< CarlaTransformPublisherImpl > _impl
void SetData(int32_t seconds, uint32_t nanoseconds, const float *translation, const float *rotation)
CarlaTransformPublisher & operator=(const CarlaTransformPublisher &)
CarlaTransformPublisher(const char *ros_name="", const char *parent="")
This class represents the structure Quaternion defined by the user in the IDL file.
Definition Quaternion.h:71
This class represents the structure TransformStamped defined by the user in the IDL file.
eProsima_user_DllExport void transform(const geometry_msgs::msg::Transform &_transform)
This function copies the value in member transform
eProsima_user_DllExport void child_frame_id(const std::string &_child_frame_id)
This function copies the value in member child_frame_id
eProsima_user_DllExport void header(const std_msgs::msg::Header &_header)
This function copies the value in member header
This class represents the structure Transform defined by the user in the IDL file.
eProsima_user_DllExport void translation(const geometry_msgs::msg::Vector3 &_translation)
This function copies the value in member translation
eProsima_user_DllExport void rotation(const geometry_msgs::msg::Quaternion &_rotation)
This function copies the value in member rotation
This class represents the structure Vector3 defined by the user in the IDL file.
Definition Vector3.h:72
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
This class represents the TopicDataType of the type TFMessage defined by the user in the IDL file.
This class represents the structure TFMessage defined by the user in the IDL file.
Definition TFMessage.h:74
eprosima::fastrtps::types::ReturnCode_t erc
This file contains definitions of common data structures used in traffic manager.
Definition Carla.cpp:133