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>
30 namespace efd = eprosima::fastdds::dds;
32 using erc = eprosima::fastrtps::types::ReturnCode_t;
53 if (
_impl->_type ==
nullptr) {
54 std::cerr <<
"Invalid TypeSupport" << std::endl;
58 efd::DomainParticipantQos pqos = efd::PARTICIPANT_QOS_DEFAULT;
61 auto factory = efd::DomainParticipantFactory::get_instance();
63 _impl->_participant = factory->create_participant(0, pqos);
64 if (
_impl->_participant ==
nullptr) {
65 std::cerr <<
"Failed to create DomainParticipant" << std::endl;
68 _impl->_type.register_type(
_impl->_participant);
70 efd::PublisherQos pubqos = efd::PUBLISHER_QOS_DEFAULT;
72 _impl->_publisher =
_impl->_participant->create_publisher(pubqos,
nullptr);
74 if (
_impl->_publisher ==
nullptr) {
75 std::cerr <<
"Failed to create Publisher" << std::endl;
79 efd::TopicQos tqos = efd::TOPIC_QOS_DEFAULT;
81 const std::string base {
"rt/carla/" };
83 std::string topic_name = base;
87 _impl->_topic =
_impl->_participant->create_topic(topic_name,
_impl->_type->getName(), tqos);
88 if (
_impl->_topic ==
nullptr) {
89 std::cerr <<
"Failed to create Topic" << std::endl;
93 efd::DataWriterQos wqos = efd::DATAWRITER_QOS_DEFAULT;
95 wqos.endpoint().history_memory_policy = eprosima::fastrtps::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE;
98 _impl->_datawriter =
_impl->_publisher->create_datawriter(
_impl->_topic, wqos, listener);
100 if (
_impl->_datawriter ==
nullptr) {
101 std::cerr <<
"Failed to create DataWriter" << std::endl;
110 eprosima::fastrtps::rtps::InstanceHandle_t instance_handle;
112 eprosima::fastrtps::types::ReturnCode_t rcode =
_impl->_datawriter->write(&
_impl->_imu, instance_handle);
114 if (rcode == erc::ReturnCodeValue::RETCODE_OK) {
118 if (rcode == erc::ReturnCodeValue::RETCODE_ERROR) {
119 std::cerr <<
"RETCODE_ERROR" << std::endl;
123 if (rcode == erc::ReturnCodeValue::RETCODE_UNSUPPORTED) {
124 std::cerr <<
"RETCODE_UNSUPPORTED" << std::endl;
128 if (rcode == erc::ReturnCodeValue::RETCODE_BAD_PARAMETER) {
129 std::cerr <<
"RETCODE_BAD_PARAMETER" << std::endl;
132 if (rcode == erc::ReturnCodeValue::RETCODE_PRECONDITION_NOT_MET) {
133 std::cerr <<
"RETCODE_PRECONDITION_NOT_MET" << std::endl;
136 if (rcode == erc::ReturnCodeValue::RETCODE_OUT_OF_RESOURCES) {
137 std::cerr <<
"RETCODE_OUT_OF_RESOURCES" << std::endl;
140 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ENABLED) {
141 std::cerr <<
"RETCODE_NOT_ENABLED" << std::endl;
144 if (rcode == erc::ReturnCodeValue::RETCODE_IMMUTABLE_POLICY) {
145 std::cerr <<
"RETCODE_IMMUTABLE_POLICY" << std::endl;
148 if (rcode == erc::ReturnCodeValue::RETCODE_INCONSISTENT_POLICY) {
149 std::cerr <<
"RETCODE_INCONSISTENT_POLICY" << std::endl;
152 if (rcode == erc::ReturnCodeValue::RETCODE_ALREADY_DELETED) {
153 std::cerr <<
"RETCODE_ALREADY_DELETED" << std::endl;
156 if (rcode == erc::ReturnCodeValue::RETCODE_TIMEOUT) {
157 std::cerr <<
"RETCODE_TIMEOUT" << std::endl;
160 if (rcode == erc::ReturnCodeValue::RETCODE_NO_DATA) {
161 std::cerr <<
"RETCODE_NO_DATA" << std::endl;
164 if (rcode == erc::ReturnCodeValue::RETCODE_ILLEGAL_OPERATION) {
165 std::cerr <<
"RETCODE_ILLEGAL_OPERATION" << std::endl;
168 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ALLOWED_BY_SECURITY) {
169 std::cerr <<
"RETCODE_NOT_ALLOWED_BY_SECURITY" << std::endl;
172 std::cerr <<
"UNKNOWN" << std::endl;
182 const float ax = *pAccelerometer++;
183 const float ay = *pAccelerometer++;
184 const float az = *pAccelerometer++;
185 linear_acceleration.
x(ax);
186 linear_acceleration.
y(ay);
187 linear_acceleration.
z(az);
189 const float gx = *pGyroscope++;
190 const float gy = *pGyroscope++;
191 const float gz = *pGyroscope++;
200 std_msgs::msg::Header header;
201 header.stamp(std::move(time));
206 const float rx = 0.0f;
207 const float ry = (M_PIf32 / 2.0f) - compass;
208 const float rz = 0.0f;
210 const float cr = cosf(rz * 0.5f);
211 const float sr = sinf(rz * 0.5f);
212 const float cp = cosf(rx * 0.5f);
213 const float sp = sinf(rx * 0.5f);
214 const float cy = cosf(ry * 0.5f);
215 const float sy = sinf(ry * 0.5f);
217 orientation.
w(
cr * cp * cy + sr * sp * sy);
218 orientation.
x(sr * cp * cy -
cr * sp * sy);
219 orientation.
y(
cr * sp * cy + sr * cp * sy);
220 orientation.
z(
cr * cp * sy - sr * sp * cy);
222 _impl->_imu.header(std::move(header));
223 _impl->_imu.orientation(orientation);
224 _impl->_imu.angular_velocity(gyroscope);
225 _impl->_imu.linear_acceleration(linear_acceleration);
238 if (
_impl->_datawriter)
239 _impl->_publisher->delete_datawriter(
_impl->_datawriter);
241 if (
_impl->_publisher)
242 _impl->_participant->delete_publisher(
_impl->_publisher);
245 _impl->_participant->delete_topic(
_impl->_topic);
247 if (
_impl->_participant)
248 efd::DomainParticipantFactory::get_instance()->delete_participant(
_impl->_participant);
269 _name = std::move(other._name);
270 _parent = std::move(other._parent);
271 _impl = std::move(other._impl);
276 _name = std::move(other._name);
277 _parent = std::move(other._parent);
278 _impl = std::move(other._impl);
eProsima_user_DllExport void nanosec(uint32_t _nanosec)
此函数设置成员nanosec的值。
eProsima_user_DllExport void sec(int32_t _sec)
此函数设置成员sec的值。
CarlaIMUPublisher(const char *ros_name="", const char *parent="")
std::shared_ptr< CarlaIMUPublisherImpl > _impl
void SetData(int32_t seconds, uint32_t nanoseconds, float *accelerometer, float *gyroscope, float compass)
CarlaIMUPublisher & operator=(const CarlaIMUPublisher &)
const std::string & parent() const
This class represents the structure Quaternion defined by the user in the IDL file.
eProsima_user_DllExport void x(double _x)
This function sets a value in member x
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
此类表示用户在 IDL 文件中定义的 Vector3 结构。 <>
eProsima_user_DllExport void x(double _x)
设置成员 x 的值
eProsima_user_DllExport void z(double _z)
设置成员 z 的值
eProsima_user_DllExport void y(double _y)
设置成员 y 的值
This class represents the TopicDataType of the type Imu defined by the user in the IDL file.
This class represents the structure Imu defined by the user in the IDL file.
eprosima::fastrtps::types::ReturnCode_t erc
@using erc
efd::Publisher * _publisher
efd::DomainParticipant * _participant
sensor_msgs::msg::Imu _imu
efd::DataWriter * _datawriter