1#define _GLIBCXX_USE_CXX11_ABI 0
9#include <fastdds/dds/domain/DomainParticipant.hpp>
10#include <fastdds/dds/subscriber/Subscriber.hpp>
11#include <fastdds/dds/topic/Topic.hpp>
12#include <fastdds/dds/subscriber/DataReader.hpp>
13#include <fastdds/dds/topic/TypeSupport.hpp>
14#include <fastdds/dds/subscriber/SampleInfo.hpp>
16#include <fastdds/dds/domain/qos/DomainParticipantQos.hpp>
17#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
18#include <fastdds/dds/subscriber/qos/SubscriberQos.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/subscriber/qos/DataReaderQos.hpp>
24#include <fastdds/dds/subscriber/DataReaderListener.hpp>
38 namespace efd = eprosima::fastdds::dds;
39 using erc = eprosima::fastrtps::types::ReturnCode_t;
101 if (
_impl->_type ==
nullptr) {
102 std::cerr <<
"Invalid TypeSupport" << std::endl;
108 efd::DomainParticipantQos pqos = efd::PARTICIPANT_QOS_DEFAULT;
110 auto factory = efd::DomainParticipantFactory::get_instance();
111 _impl->_participant = factory->create_participant(0, pqos);
112 if (
_impl->_participant ==
nullptr) {
113 std::cerr <<
"Failed to create DomainParticipant" << std::endl;
119 _impl->_type.register_type(
_impl->_participant);
123 efd::SubscriberQos subqos = efd::SUBSCRIBER_QOS_DEFAULT;
124 _impl->_subscriber =
_impl->_participant->create_subscriber(subqos,
nullptr);
125 if (
_impl->_subscriber ==
nullptr) {
126 std::cerr <<
"Failed to create Subscriber" << std::endl;
132 efd::TopicQos tqos = efd::TOPIC_QOS_DEFAULT;
133 const std::string base {
"rt/carla/" };
134 const std::string publisher_type {
"/vehicle_control_cmd"};
135 std::string topic_name = base;
139 topic_name += publisher_type;
140 _impl->_topic =
_impl->_participant->create_topic(topic_name,
_impl->_type->getName(), tqos);
141 if (
_impl->_topic ==
nullptr) {
142 std::cerr <<
"Failed to create Topic" << std::endl;
148 efd::DataReaderQos rqos = efd::DATAREADER_QOS_DEFAULT;
150 _impl->_datareader =
_impl->_subscriber->create_datareader(
_impl->_topic, rqos, listener);
151 if (
_impl->_datareader ==
nullptr) {
152 std::cerr <<
"Failed to create DataReader" << std::endl;
166 efd::SampleInfo info;
168 eprosima::fastrtps::types::ReturnCode_t rcode =
_impl->_datareader->take_next_sample(&
_impl->_event, &info);
170 if (rcode == erc::ReturnCodeValue::RETCODE_OK) {
174 if (rcode == erc::ReturnCodeValue::RETCODE_ERROR) {
176 std::cerr <<
"RETCODE_ERROR" << std::endl;
179 if (rcode == erc::ReturnCodeValue::RETCODE_UNSUPPORTED) {
181 std::cerr <<
"RETCODE_UNSUPPORTED" << std::endl;
184 if (rcode == erc::ReturnCodeValue::RETCODE_BAD_PARAMETER) {
186 std::cerr <<
"RETCODE_BAD_PARAMETER" << std::endl;
189 if (rcode == erc::ReturnCodeValue::RETCODE_PRECONDITION_NOT_MET) {
191 std::cerr <<
"RETCODE_PRECONDITION_NOT_MET" << std::endl;
194 if (rcode == erc::ReturnCodeValue::RETCODE_OUT_OF_RESOURCES) {
196 std::cerr <<
"RETCODE_OUT_OF_RESOURCES" << std::endl;
199 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ENABLED) {
201 std::cerr <<
"RETCODE_NOT_ENABLED" << std::endl;
204 if (rcode == erc::ReturnCodeValue::RETCODE_IMMUTABLE_POLICY) {
206 std::cerr <<
"RETCODE_IMMUTABLE_POLICY" << std::endl;
209 if (rcode == erc::ReturnCodeValue::RETCODE_INCONSISTENT_POLICY) {
211 std::cerr <<
"RETCODE_INCONSISTENT_POLICY" << std::endl;
214 if (rcode == erc::ReturnCodeValue::RETCODE_ALREADY_DELETED) {
216 std::cerr <<
"RETCODE_ALREADY_DELETED" << std::endl;
219 if (rcode == erc::ReturnCodeValue::RETCODE_TIMEOUT) {
221 std::cerr <<
"RETCODE_TIMEOUT" << std::endl;
224 if (rcode == erc::ReturnCodeValue::RETCODE_NO_DATA) {
226 std::cerr <<
"RETCODE_NO_DATA" << std::endl;
229 if (rcode == erc::ReturnCodeValue::RETCODE_ILLEGAL_OPERATION) {
231 std::cerr <<
"RETCODE_ILLEGAL_OPERATION" << std::endl;
234 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ALLOWED_BY_SECURITY) {
236 std::cerr <<
"RETCODE_NOT_ALLOWED_BY_SECURITY" << std::endl;
240 std::cerr <<
"UNKNOWN" << std::endl;
252 _impl->_control = control;
253 _impl->_new_message =
true;
261 _impl->_alive =
false;
272 _impl->_new_message =
false;
273 return _impl->_control;
283 return _impl->_alive;
293 return _impl->_new_message;
303 return _impl->_vehicle;
316 _impl->_listener.SetOwner(
this);
317 _impl->_vehicle = vehicle;
331 if (
_impl->_datareader)
332 _impl->_subscriber->delete_datareader(
_impl->_datareader);
334 if (
_impl->_subscriber)
335 _impl->_participant->delete_subscriber(
_impl->_subscriber);
338 _impl->_participant->delete_topic(
_impl->_topic);
340 if (
_impl->_participant)
341 efd::DomainParticipantFactory::get_instance()->delete_participant(
_impl->_participant);
356 _impl->_listener.SetOwner(
this);
372 _impl->_listener.SetOwner(
this);
386 _name = std::move(other._name);
387 _parent = std::move(other._parent);
388 _impl = std::move(other._impl);
389 _impl->_listener.SetOwner(
this);
402 _name = std::move(other._name);
403 _parent = std::move(other._parent);
404 _impl = std::move(other._impl);
405 _impl->_listener.SetOwner(
this);
CarlaEgoVehicleControlSubscriber类,用于订阅并处理来自ROS2的自动驾驶车辆控制消息
void * GetVehicle()
获取与用户关联的车辆实例的指针。
bool IsAlive()
检查用户是否仍然活跃。
CarlaEgoVehicleControlSubscriber & operator=(const CarlaEgoVehicleControlSubscriber &)
拷贝赋值运算符,用于深拷贝赋值。
VehicleControl GetMessage()
获取最新的车辆控制消息。
CarlaEgoVehicleControlSubscriber(void *vehicle, const char *ros_name="", const char *parent="")
构造函数,初始化用户并关联到指定的车辆。
~CarlaEgoVehicleControlSubscriber()
析构函数,清理资源并销毁用户。
std::shared_ptr< CarlaEgoVehicleControlSubscriberImpl > _impl
指向内部实现结构的智能指针,用于隐藏实现细节。
bool HasNewMessage()
检查是否有新的控制消息到达。
void ForwardMessage(VehicleControl control)
(内部使用)将控制消息转发给内部处理函数。
void DestroySubscriber()
销毁用户并释放相关资源。
Carla订阅者监听器类,用于处理与ROS 2订阅者相关的事件。
const std::string & parent() const
此类表示用户在 IDL 文件中定义的类型 CarlaEgoVehicleControl 的 TopicDataType。 <>
该类表示用户在 IDL 文件中定义的结构 CarlaEgoVehicleControl。 <>
eprosima::fastrtps::types::ReturnCode_t erc
@using erc
CarlaEgoVehicleControlSubscriber的内部实现结构体。
bool _new_message
标记是否有新的消息到达。
efd::Topic * _topic
DDS主题指针。
efd::TypeSupport _type
DDS类型支持,用于注册消息类型。
VehicleControl _control
转换为内部使用的VehicleControl结构体。
efd::DomainParticipant * _participant
DDS域参与者指针。
carla_msgs::msg::CarlaEgoVehicleControl _event
存储接收到的CARLA EgoVehicleControl消息。
efd::Subscriber * _subscriber
DDS订阅者指针。
efd::DataReader * _datareader
DDS数据读取器指针。
CarlaSubscriberListener _listener
订阅者监听器,用于处理接收到的消息。
void * _vehicle
与车辆控制相关的指针(具体类型未在代码中定义)。