1 #define _GLIBCXX_USE_CXX11_ABI 0 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> 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> 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> 29 namespace efd = eprosima::fastdds::dds;
30 using erc = eprosima::fastrtps::types::ReturnCode_t;
43 if (_impl->_type ==
nullptr) {
44 std::cerr <<
"Invalid TypeSupport" << std::endl;
48 efd::DomainParticipantQos pqos = efd::PARTICIPANT_QOS_DEFAULT;
50 auto factory = efd::DomainParticipantFactory::get_instance();
51 _impl->_participant = factory->create_participant(0, pqos);
52 if (_impl->_participant ==
nullptr) {
53 std::cerr <<
"Failed to create DomainParticipant" << std::endl;
56 _impl->_type.register_type(_impl->_participant);
58 efd::PublisherQos pubqos = efd::PUBLISHER_QOS_DEFAULT;
59 _impl->_publisher = _impl->_participant->create_publisher(pubqos,
nullptr);
60 if (_impl->_publisher ==
nullptr) {
61 std::cerr <<
"Failed to create Publisher" << std::endl;
65 efd::TopicQos tqos = efd::TOPIC_QOS_DEFAULT;
66 const std::string base {
"rt/carla/" };
67 std::string topic_name = base;
69 topic_name += _parent +
"/";
71 _impl->_topic = _impl->_participant->create_topic(topic_name, _impl->_type->getName(), tqos);
72 if (_impl->_topic ==
nullptr) {
73 std::cerr <<
"Failed to create Topic" << std::endl;
77 efd::DataWriterQos wqos = efd::DATAWRITER_QOS_DEFAULT;
78 wqos.endpoint().history_memory_policy = eprosima::fastrtps::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE;
79 efd::DataWriterListener* listener = (efd::DataWriterListener*)_impl->_listener._impl.get();
80 _impl->_datawriter = _impl->_publisher->create_datawriter(_impl->_topic, wqos, listener);
81 if (_impl->_datawriter ==
nullptr) {
82 std::cerr <<
"Failed to create DataWriter" << std::endl;
91 eprosima::fastrtps::types::ReturnCode_t rcode = _impl->_datawriter->write(&_impl->_imu, instance_handle);
92 if (rcode == erc::ReturnCodeValue::RETCODE_OK) {
95 if (rcode == erc::ReturnCodeValue::RETCODE_ERROR) {
96 std::cerr <<
"RETCODE_ERROR" << std::endl;
99 if (rcode == erc::ReturnCodeValue::RETCODE_UNSUPPORTED) {
100 std::cerr <<
"RETCODE_UNSUPPORTED" << std::endl;
103 if (rcode == erc::ReturnCodeValue::RETCODE_BAD_PARAMETER) {
104 std::cerr <<
"RETCODE_BAD_PARAMETER" << std::endl;
107 if (rcode == erc::ReturnCodeValue::RETCODE_PRECONDITION_NOT_MET) {
108 std::cerr <<
"RETCODE_PRECONDITION_NOT_MET" << std::endl;
111 if (rcode == erc::ReturnCodeValue::RETCODE_OUT_OF_RESOURCES) {
112 std::cerr <<
"RETCODE_OUT_OF_RESOURCES" << std::endl;
115 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ENABLED) {
116 std::cerr <<
"RETCODE_NOT_ENABLED" << std::endl;
119 if (rcode == erc::ReturnCodeValue::RETCODE_IMMUTABLE_POLICY) {
120 std::cerr <<
"RETCODE_IMMUTABLE_POLICY" << std::endl;
123 if (rcode == erc::ReturnCodeValue::RETCODE_INCONSISTENT_POLICY) {
124 std::cerr <<
"RETCODE_INCONSISTENT_POLICY" << std::endl;
127 if (rcode == erc::ReturnCodeValue::RETCODE_ALREADY_DELETED) {
128 std::cerr <<
"RETCODE_ALREADY_DELETED" << std::endl;
131 if (rcode == erc::ReturnCodeValue::RETCODE_TIMEOUT) {
132 std::cerr <<
"RETCODE_TIMEOUT" << std::endl;
135 if (rcode == erc::ReturnCodeValue::RETCODE_NO_DATA) {
136 std::cerr <<
"RETCODE_NO_DATA" << std::endl;
139 if (rcode == erc::ReturnCodeValue::RETCODE_ILLEGAL_OPERATION) {
140 std::cerr <<
"RETCODE_ILLEGAL_OPERATION" << std::endl;
143 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ALLOWED_BY_SECURITY) {
144 std::cerr <<
"RETCODE_NOT_ALLOWED_BY_SECURITY" << std::endl;
147 std::cerr <<
"UNKNOWN" << std::endl;
154 const float ax = *pAccelerometer++;
155 const float ay = *pAccelerometer++;
156 const float az = *pAccelerometer++;
157 linear_acceleration.
x(ax);
158 linear_acceleration.
y(ay);
159 linear_acceleration.
z(az);
160 const float gx = *pGyroscope++;
161 const float gy = *pGyroscope++;
162 const float gz = *pGyroscope++;
172 header.
stamp(std::move(time));
177 const float rx = 0.0f;
178 const float ry = (M_PIf32 / 2.0f) - compass;
179 const float rz = 0.0f;
181 const float cr = cosf(rz * 0.5f);
182 const float sr = sinf(rz * 0.5f);
183 const float cp = cosf(rx * 0.5f);
184 const float sp = sinf(rx * 0.5f);
185 const float cy = cosf(ry * 0.5f);
186 const float sy = sinf(ry * 0.5f);
188 orientation.
w(cr * cp * cy + sr * sp * sy);
189 orientation.
x(sr * cp * cy - cr * sp * sy);
190 orientation.
y(cr * sp * cy + sr * cp * sy);
191 orientation.
z(cr * cp * sy - sr * sp * cy);
193 _impl->_imu.header(std::move(header));
194 _impl->_imu.orientation(orientation);
195 _impl->_imu.angular_velocity(gyroscope);
196 _impl->_imu.linear_acceleration(linear_acceleration);
209 if (
_impl->_datawriter)
210 _impl->_publisher->delete_datawriter(
_impl->_datawriter);
212 if (
_impl->_publisher)
213 _impl->_participant->delete_publisher(
_impl->_publisher);
216 _impl->_participant->delete_topic(
_impl->_topic);
218 if (
_impl->_participant)
219 efd::DomainParticipantFactory::get_instance()->delete_participant(
_impl->_participant);
240 _name = std::move(other._name);
241 _parent = std::move(other._parent);
242 _impl = std::move(other._impl);
247 _name = std::move(other._name);
248 _parent = std::move(other._parent);
249 _impl = std::move(other._impl);
void SetData(int32_t seconds, uint32_t nanoseconds, float *accelerometer, float *gyroscope, float compass)
eProsima_user_DllExport void y(double _y)
This function sets a value in member y.
CarlaIMUPublisher(const char *ros_name="", const char *parent="")
eprosima::fastrtps::rtps::InstanceHandle_t InstanceHandle_t
eprosima::fastrtps::types::ReturnCode_t erc
This file contains definitions of common data structures used in traffic manager. ...
eProsima_user_DllExport void y(double _y)
This function sets a value in member y.
eProsima_user_DllExport void z(double _z)
This function sets a value in member z.
eProsima_user_DllExport void w(double _w)
This function sets a value in member w.
This class represents the structure Vector3 defined by the user in the IDL file.
efd::DataWriter * _datawriter
std::shared_ptr< CarlaIMUPublisherImpl > _impl
efd::Publisher * _publisher
This class represents the structure Imu defined by the user in the IDL file.
eProsima_user_DllExport void sec(int32_t _sec)
This function sets a value in member sec.
eProsima_user_DllExport void nanosec(uint32_t _nanosec)
This function sets a value in member nanosec.
eProsima_user_DllExport void x(double _x)
This function sets a value in member x.
CarlaIMUPublisher & operator=(const CarlaIMUPublisher &)
const std::string & parent() const
eProsima_user_DllExport void z(double _z)
This function sets a value in member z.
This class represents the structure Time defined by the user in the IDL file.
eProsima_user_DllExport void x(double _x)
This function sets a value in member x.
sensor_msgs::msg::Imu _imu
This class represents the structure Quaternion defined by the user in the IDL file.
efd::DomainParticipant * _participant
This class represents the TopicDataType of the type Imu defined by the user in the IDL file...