1#define _GLIBCXX_USE_CXX11_ABI 0
12#include <fastdds/dds/domain/DomainParticipant.hpp>
13#include <fastdds/dds/publisher/Publisher.hpp>
14#include <fastdds/dds/topic/Topic.hpp>
15#include <fastdds/dds/publisher/DataWriter.hpp>
16#include <fastdds/dds/topic/TypeSupport.hpp>
18#include <fastdds/dds/domain/qos/DomainParticipantQos.hpp>
19#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
20#include <fastdds/dds/publisher/qos/PublisherQos.hpp>
21#include <fastdds/dds/topic/qos/TopicQos.hpp>
23#include <fastrtps/attributes/ParticipantAttributes.h>
24#include <fastrtps/qos/QosPolicies.h>
25#include <fastdds/dds/publisher/qos/DataWriterQos.hpp>
26#include <fastdds/dds/publisher/DataWriterListener.hpp>
38 namespace efd = eprosima::fastdds::dds;
43 using erc = eprosima::fastrtps::types::ReturnCode_t;
87 if (
_impl->_type ==
nullptr) {
88 std::cerr <<
"Invalid TypeSupport" << std::endl;
92 efd::DomainParticipantQos pqos = efd::PARTICIPANT_QOS_DEFAULT;
94 auto factory = efd::DomainParticipantFactory::get_instance();
95 _impl->_participant = factory->create_participant(0, pqos);
96 if (
_impl->_participant ==
nullptr) {
97 std::cerr <<
"Failed to create DomainParticipant" << std::endl;
102 _impl->_type.register_type(
_impl->_participant);
104 efd::PublisherQos pubqos = efd::PUBLISHER_QOS_DEFAULT;
105 _impl->_publisher =
_impl->_participant->create_publisher(pubqos,
nullptr);
106 if (
_impl->_publisher ==
nullptr) {
107 std::cerr <<
"Failed to create Publisher" << std::endl;
112 efd::TopicQos tqos = efd::TOPIC_QOS_DEFAULT;
113 const std::string topic_name {
"rt/clock" };
114 _impl->_topic =
_impl->_participant->create_topic(topic_name,
_impl->_type->getName(), tqos);
115 if (
_impl->_topic ==
nullptr) {
116 std::cerr <<
"Failed to create Topic" << std::endl;
121 efd::DataWriterQos wqos = efd::DATAWRITER_QOS_DEFAULT;
123 _impl->_datawriter =
_impl->_publisher->create_datawriter(
_impl->_topic, wqos, listener);
124 if (
_impl->_datawriter ==
nullptr) {
125 std::cerr <<
"Failed to create DataWriter" << std::endl;
143 eprosima::fastrtps::rtps::InstanceHandle_t instance_handle;
145 eprosima::fastrtps::types::ReturnCode_t rcode =
_impl->_datawriter->write(&
_impl->_clock, instance_handle);
147 if (rcode == erc::ReturnCodeValue::RETCODE_OK) {
151 if (rcode == erc::ReturnCodeValue::RETCODE_ERROR) {
152 std::cerr <<
"RETCODE_ERROR" << std::endl;
155 if (rcode == erc::ReturnCodeValue::RETCODE_UNSUPPORTED) {
156 std::cerr <<
"RETCODE_UNSUPPORTED" << std::endl;
160 if (rcode == erc::ReturnCodeValue::RETCODE_BAD_PARAMETER) {
161 std::cerr <<
"RETCODE_BAD_PARAMETER" << std::endl;
165 if (rcode == erc::ReturnCodeValue::RETCODE_PRECONDITION_NOT_MET) {
166 std::cerr <<
"RETCODE_PRECONDITION_NOT_MET" << std::endl;
170 if (rcode == erc::ReturnCodeValue::RETCODE_OUT_OF_RESOURCES) {
171 std::cerr <<
"RETCODE_OUT_OF_RESOURCES" << std::endl;
175 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ENABLED) {
176 std::cerr <<
"RETCODE_NOT_ENABLED" << std::endl;
180 if (rcode == erc::ReturnCodeValue::RETCODE_IMMUTABLE_POLICY) {
181 std::cerr <<
"RETCODE_IMMUTABLE_POLICY" << std::endl;
185 if (rcode == erc::ReturnCodeValue::RETCODE_INCONSISTENT_POLICY) {
186 std::cerr <<
"RETCODE_INCONSISTENT_POLICY" << std::endl;
190 if (rcode == erc::ReturnCodeValue::RETCODE_ALREADY_DELETED) {
191 std::cerr <<
"RETCODE_ALREADY_DELETED" << std::endl;
195 if (rcode == erc::ReturnCodeValue::RETCODE_TIMEOUT) {
196 std::cerr <<
"RETCODE_TIMEOUT" << std::endl;
200 if (rcode == erc::ReturnCodeValue::RETCODE_NO_DATA) {
201 std::cerr <<
"RETCODE_NO_DATA" << std::endl;
205 if (rcode == erc::ReturnCodeValue::RETCODE_ILLEGAL_OPERATION) {
206 std::cerr <<
"RETCODE_ILLEGAL_OPERATION" << std::endl;
210 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ALLOWED_BY_SECURITY) {
211 std::cerr <<
"RETCODE_NOT_ALLOWED_BY_SECURITY" << std::endl;
215 std::cerr <<
"UNKNOWN" << std::endl;
227 _impl->_clock.clock().sec(sec);
228 _impl->_clock.clock().nanosec(nanosec);
252 if (
_impl->_datawriter)
253 _impl->_publisher->delete_datawriter(
_impl->_datawriter);
255 if (
_impl->_publisher)
256 _impl->_participant->delete_publisher(
_impl->_publisher);
259 _impl->_participant->delete_topic(
_impl->_topic);
261 if (
_impl->_participant)
262 efd::DomainParticipantFactory::get_instance()->delete_participant(
_impl->_participant);
302 _name = std::move(other._name);
303 _parent = std::move(other._parent);
304 _impl = std::move(other._impl);
316 _name = std::move(other._name);
317 _parent = std::move(other._parent);
318 _impl = std::move(other._impl);
CarlaClockPublisher(const char *ros_name="", const char *parent="")
构造函数,初始化CarlaClockPublisher。
~CarlaClockPublisher()
析构函数,清理资源并释放内部实现。
CarlaClockPublisher & operator=(const CarlaClockPublisher &)
赋值运算符。
std::shared_ptr< CarlaClockPublisherImpl > _impl
指向内部实现结构体的智能指针。
void SetData(int32_t sec, uint32_t nanosec)
设置要发布的时钟数据。
const std::string & parent() const
此类表示用户在 IDL 文件中定义的类型 Clock 的 TopicDataType。 <>
This class represents the structure Clock defined by the user in the IDL file.
eprosima::fastrtps::types::ReturnCode_t erc
@using erc
CarlaClockPublisher的内部实现结构体,用于封装与Fast DDS相关的资源。
CarlaListener _listener
CarlaListener对象,用于监听DDS事件。
efd::Publisher * _publisher
指向Publisher的指针,用于发布数据。
efd::TypeSupport _type
TypeSupport对象,用于注册和识别消息类型。
rosgraph::msg::Clock _clock
存储Clock消息的变量。
efd::DomainParticipant * _participant
指向DomainParticipant的指针,用于管理DDS域中的参与者。
efd::Topic * _topic
指向Topic的指针,用于定义数据的主题。
efd::DataWriter * _datawriter
指向DataWriter的指针,用于写入数据到指定的Topic。