1#define _GLIBCXX_USE_CXX11_ABI 0
26#include <fastdds/dds/domain/DomainParticipant.hpp>
30#include <fastdds/dds/publisher/Publisher.hpp>
34#include <fastdds/dds/topic/Topic.hpp>
38#include <fastdds/dds/publisher/DataWriter.hpp>
42#include <fastdds/dds/topic/TypeSupport.hpp>
46#include <fastdds/dds/domain/qos/DomainParticipantQos.hpp>
50#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
54#include <fastdds/dds/publisher/qos/PublisherQos.hpp>
58#include <fastdds/dds/topic/qos/TopicQos.hpp>
62#include <fastrtps/attributes/ParticipantAttributes.h>
66#include <fastrtps/qos/QosPolicies.h>
70#include <fastdds/dds/publisher/qos/DataWriterQos.hpp>
74#include <fastdds/dds/publisher/DataWriterListener.hpp>
90 namespace efd = eprosima::fastdds::dds;
91 using erc = eprosima::fastrtps::types::ReturnCode_t;
139 if (
_impl->_type ==
nullptr) {
140 std::cerr <<
"Invalid TypeSupport" << std::endl;
146 efd::DomainParticipantQos pqos = efd::PARTICIPANT_QOS_DEFAULT;
148 auto factory = efd::DomainParticipantFactory::get_instance();
149 _impl->_participant = factory->create_participant(0, pqos);
150 if (
_impl->_participant ==
nullptr) {
151 std::cerr <<
"Failed to create DomainParticipant" << std::endl;
157 _impl->_type.register_type(
_impl->_participant);
161 efd::PublisherQos pubqos = efd::PUBLISHER_QOS_DEFAULT;
162 _impl->_publisher =
_impl->_participant->create_publisher(pubqos,
nullptr);
163 if (
_impl->_publisher ==
nullptr) {
164 std::cerr <<
"Failed to create Publisher" << std::endl;
170 efd::TopicQos tqos = efd::TOPIC_QOS_DEFAULT;
171 const std::string base {
"rt/carla/" };
172 std::string topic_name = base;
176 _impl->_topic =
_impl->_participant->create_topic(topic_name,
_impl->_type->getName(), tqos);
177 if (
_impl->_topic ==
nullptr) {
178 std::cerr <<
"Failed to create Topic" << std::endl;
184 efd::DataWriterQos wqos = efd::DATAWRITER_QOS_DEFAULT;
185 wqos.endpoint().history_memory_policy = eprosima::fastrtps::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE;
187 _impl->_datawriter =
_impl->_publisher->create_datawriter(
_impl->_topic, wqos, listener);
188 if (
_impl->_datawriter ==
nullptr) {
189 std::cerr <<
"Failed to create DataWriter" << std::endl;
208 eprosima::fastrtps::rtps::InstanceHandle_t instance_handle;
213 eprosima::fastrtps::types::ReturnCode_t rcode =
_impl->_datawriter->write(&
_impl->_float, instance_handle);
215 if (rcode == erc::ReturnCodeValue::RETCODE_OK) {
219 if (rcode == erc::ReturnCodeValue::RETCODE_ERROR) {
221 std::cerr <<
"RETCODE_ERROR" << std::endl;
224 if (rcode == erc::ReturnCodeValue::RETCODE_UNSUPPORTED) {
226 std::cerr <<
"RETCODE_UNSUPPORTED" << std::endl;
229 if (rcode == erc::ReturnCodeValue::RETCODE_BAD_PARAMETER) {
231 std::cerr <<
"RETCODE_BAD_PARAMETER" << std::endl;
234 if (rcode == erc::ReturnCodeValue::RETCODE_PRECONDITION_NOT_MET) {
236 std::cerr <<
"RETCODE_PRECONDITION_NOT_MET" << std::endl;
239 if (rcode == erc::ReturnCodeValue::RETCODE_OUT_OF_RESOURCES) {
241 std::cerr <<
"RETCODE_OUT_OF_RESOURCES" << std::endl;
244 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ENABLED) {
246 std::cerr <<
"RETCODE_NOT_ENABLED" << std::endl;
249 if (rcode == erc::ReturnCodeValue::RETCODE_IMMUTABLE_POLICY) {
251 std::cerr <<
"RETCODE_IMMUTABLE_POLICY" << std::endl;
254 if (rcode == erc::ReturnCodeValue::RETCODE_INCONSISTENT_POLICY) {
256 std::cerr <<
"RETCODE_INCONSISTENT_POLICY" << std::endl;
259 if (rcode == erc::ReturnCodeValue::RETCODE_ALREADY_DELETED) {
261 std::cerr <<
"RETCODE_ALREADY_DELETED" << std::endl;
264 if (rcode == erc::ReturnCodeValue::RETCODE_TIMEOUT) {
266 std::cerr <<
"RETCODE_TIMEOUT" << std::endl;
269 if (rcode == erc::ReturnCodeValue::RETCODE_NO_DATA) {
271 std::cerr <<
"RETCODE_NO_DATA" << std::endl;
274 if (rcode == erc::ReturnCodeValue::RETCODE_ILLEGAL_OPERATION) {
276 std::cerr <<
"RETCODE_ILLEGAL_OPERATION" << std::endl;
279 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ALLOWED_BY_SECURITY) {
281 std::cerr <<
"RETCODE_NOT_ALLOWED_BY_SECURITY" << std::endl;
285 std::cerr <<
"UNKNOWN" << std::endl;
296 _impl->_float.data(data);
320 if (
_impl->_datawriter)
321 _impl->_publisher->delete_datawriter(
_impl->_datawriter);
323 if (
_impl->_publisher)
324 _impl->_participant->delete_publisher(
_impl->_publisher);
327 _impl->_participant->delete_topic(
_impl->_topic);
329 if (
_impl->_participant)
330 efd::DomainParticipantFactory::get_instance()->delete_participant(
_impl->_participant);
370 _name = std::move(other._name);
371 _parent = std::move(other._parent);
372 _impl = std::move(other._impl);
384 _name = std::move(other._name);
385 _parent = std::move(other._parent);
386 _impl = std::move(other._impl);
const std::string & parent() const
CarlaSpeedometerSensor & operator=(const CarlaSpeedometerSensor &)
拷贝赋值运算符
~CarlaSpeedometerSensor()
CarlaSpeedometerSensor类的析构函数
bool Publish()
发布速度计传感器的数据
std::shared_ptr< CarlaSpeedometerSensorImpl > _impl
void SetData(float data)
设置速度传感器的数据
bool Init()
初始化Carla车速传感器。
CarlaSpeedometerSensor(const char *ros_name="", const char *parent="")
CarlaSpeedometerSensor类的构造函数
此类表示由用户在 IDL 文件中定义的 Float32 类型的主题数据类型(TopicDataType)。它继承自 eprosima::fastdds::dds::TopicDataType,意味着需要...
这个类表示用户在IDL文件中定义的Float32结构。
eprosima::fastrtps::types::ReturnCode_t erc
@using erc
efd::DataWriter * _datawriter
FastDDS的DataWriter指针,用于写入数据到指定的Topic。
efd::TypeSupport _type
TypeSupport对象,用于注册和识别发布的消息类型。
efd::Topic * _topic
FastDDS的Topic指针,用于定义数据的主题。
CarlaListener _listener
CarlaListener对象,用于处理相关的回调事件。
efd::DomainParticipant * _participant
FastDDS的DomainParticipant指针,用于创建和管理FastDDS的通信实体。
efd::Publisher * _publisher
FastDDS的Publisher指针,用于发布数据。
std_msgs::msg::Float32 _float
用于发布的车速数据。