CARLA
 
载入中...
搜索中...
未找到
CarlaMapSensorPublisher.cpp
浏览该文件的文档.
1#define _GLIBCXX_USE_CXX11_ABI 0
2// 定义了一个宏,用于设置C++标准库的ABI(应用程序二进制接口)版本为0,这可能与代码所依赖的库的编译设置相关。
3
5#include <string>
8#include <fastdds/dds/domain/DomainParticipant.hpp>
9#include <fastdds/dds/publisher/Publisher.hpp>
10#include <fastdds/dds/topic/Topic.hpp>
11#include <fastdds/dds/publisher/DataWriter.hpp>
12#include <fastdds/dds/topic/TypeSupport.hpp>
13#include <fastdds/dds/domain/qos/DomainParticipantQos.hpp>
14#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
15#include <fastdds/dds/publisher/qos/PublisherQos.hpp>
16#include <fastdds/dds/topic/qos/TopicQos.hpp>
17#include <fastrtps/attributes/ParticipantAttributes.h>
18#include <fastrtps/qos/QosPolicies.h>
19#include <fastdds/dds/publisher/qos/DataWriterQos.hpp>
20#include <fastdds/dds/publisher/DataWriterListener.hpp>
21// 引入了一系列必要的头文件,包括自定义的Carla相关头文件、标准库的<string>头文件以及Fast DDS相关的头文件,用于实现与Carla模拟器以及Fast DDS消息中间件的交互功能。
22
23namespace carla {
24namespace ros2 {
25 // 定义了carla::ros2命名空间,以下的所有类型和函数都在这个命名空间内。
26
27 efd = eprosima::fastdds::dds;
28 using erc = eprosima::fastrtps::types::ReturnCode_t;
29 // 为了方便使用,给eprosima::fastdds::dds和eprosima::fastrtps::types::ReturnCode_t分别定义了别名efd和erc。
30
32 efd::DomainParticipant* _participant { nullptr };
33 efd::Publisher* _publisher { nullptr };
34 efd::Topic* _topic { nullptr };
35 efd::DataWriter* _datawriter { nullptr };
36 efd::TypeSupport _type { new std_msgs::msg::StringPubSubType() };
37 CarlaListener _listener {};
39 };
40 // 定义了一个名为CarlaMapSensorPublisherImpl的结构体,用于存储与Carla地图传感器发布者相关的内部实现细节。
41 // 包含了Fast DDS的域参与者、发布者、主题、数据写入器等相关对象的指针,以及消息类型支持对象、监听器对象和一个用于存储要发布的字符串消息的对象。
42
44 if (_impl->_type == nullptr) {
45 std::cerr << "Invalid TypeSupport" << std::endl;
46 return false;
47 }
48 // 首先检查消息类型支持对象是否为空,如果为空则输出错误信息并返回false,表示初始化失败。
49
50 efd::DomainParticipantQos pqos = efd::PARTICIPANT_QOS_DEFAULT;
51 pqos.name(_name);
52 auto factory = efd::DomainParticipantFactory::get_instance();
53 _impl->_participant = factory->create_participant(0, pqos);
54 if (_impl->_participant == nullptr) {
55 std::cerr << "Failed to create DomainParticipant" << std::endl;
56 return false;
57 }
58 _impl->_type.register_type(_impl->_participant);
59 // 设置域参与者的默认质量服务属性(QoS),并设置其名称为传入的_name。然后通过域参与者工厂创建一个域参与者对象,
60 // 如果创建失败则输出错误信息并返回false。成功创建后,将消息类型注册到该域参与者上。
61
62 efd::PublisherQos pubqos = efd::PUBLISHER_QOS_DEFAULT;
63 _impl->_publisher = _impl->_participant->create_publisher(pubqos, nullptr);
64 if (_impl->_publisher == nullptr) {
65 std::cerr << "Failed to create Publisher" << std::endl;
66 return false;
67 }
68 // 设置发布者的默认QoS属性,然后通过已创建的域参与者对象创建一个发布者对象,
69 // 如果创建失败则输出错误信息并返回false。
70
71 efd::TopicQos tqos = efd::TOPIC_QOS_DEFAULT;
72 const std::string base { "rt/carla/" };
73 std::string topic_name = base;
74 if (!_parent.empty())
75 topic_name += _parent + "/";
76 topic_name += _name;
77 _impl->_topic = _impl->_participant->create_topic(topic_name, _impl->_type->getName(), tqos);
78 if (_impl->_topic == nullptr) {
79 std::cerr << "Failed to create Topic" << std::endl;
80 return false;
81 }
82 // 设置主题的默认QoS属性,根据传入的_parent和_name构建主题名称,然后通过域参与者对象创建一个主题对象,
83 // 如果创建失败则输出错误信息并返回false。
84
85 efd::DataWriterQos wqos = efd::DATAWRITER_QOS_DEFAULT;
86 wqos.endpoint().history_memory_policy = eprosima::fastrtps::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE;
87 efd::DataWriterListener* listener = (efd::DataWriterListener*)_impl->_listener._impl.get();
88 _impl->_datawriter = _impl->_publisher->create_datawriter(_impl->_topic, wqos, listener);
89 if (_impl->_datawriter == nullptr) {
90 std::cerr << "Failed to create DataWriter" << std::endl;
91 return false;
92 }
94 return true;
95 // 设置数据写入器的默认QoS属性,指定其历史内存策略。获取监听器对象的指针,然后通过发布者对象创建一个数据写入器对象,
96 // 如果创建失败则输出错误信息并返回false。最后设置_frame_id为_name,并返回true表示初始化成功。
97
98 }
99
101 eprosima::fastrtps::rtps::InstanceHandle_t instance_handle;
102 eprosima::fastrtps::types::ReturnCode_t rcode = _impl->_datawriter->write(&_impl->_string, instance_handle);
103 if (rcode == erc::ReturnCodeValue::RETCODE_OK) {
104 return true;
105 }
106 // 创建一个实例句柄对象,然后调用数据写入器的write方法尝试发布存储在_impl->_string中的消息,获取返回码rcode。
107 // 如果返回码为RETCODE_OK,表示发布成功,返回true。
108
109 if (rcode == erc::ReturnCodeValue::RETCODE_ERROR) {
110 std::cerr << "RETCODE_ERROR" << std::endl;
111 return false;
112 }
113 if (rcode == erc::ReturnCodeValue::RETCODE_UNSUPPORTED) {
114 std::cerr << "RETCODE_UNSUPPORTED" << std::endl;
115 return false;
116 }
117 if (rcode == erc::ReturnCodeValue::RETCODE_BAD_PARAMETER) {
118 std::cerr << "RETCODE_BAD_PARAMETER" << std::endl;
119 return false;
120 }
121 if (rcode == erc::ReturnCodeValue::RETCODE_PRECONDITION_NOT_MET) {
122 std::cerr << "RETCODE_PRECONDITION_NOT_MET" << std::endl;
123 return false;
124 }
125 if (rcode == erc::ReturnCodeValue::RETCODE_OUT_OF_RESOURCES) {
126 std::cerr << "RETCODE_OUT_OF_RESOURCES" << std::endl;
127 return false;
128 }
129 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ENABLED) {
130 std::cerr << "RETCODE_NOT_ENABLED" << std::endl;
131 return false;
132 }
133 if (rcode == erc::ReturnCodeValue::RETCODE_IMMUTABLE_POLICY) {
134 std::cerr << "RETCODE_IMMUTABLE_POLICY" << std::endl;
135 return false;
136 }
137 if (rcode == erc::ReturnCodeValue::RETCODE_INCONSISTENT_POLICY) {
138 std::cerr << "RETCODE_INCONSISTENT_POLICY" << std::endl;
139 return false;
140 }
141 if (rcode == erc::ReturnCodeValue::RETCODE_ALREADY_DELETED) {
142 std::cerr << "RETCODE_ALREADY_DELETED" << std::endl;
143 return false;
144 }
145 if (rcode == erc::ReturnCodeValue::RETCODE_TIMEOUT) {
146 std::cerr << "RETCODE_TIMEOUT" << std::endl;
147 return false;
148 }
149 if (rcode == erc::ReturnCodeValue::RETCODE_NO_DATA) {
150 std::cerr << "RETCODE_NO_DATA" << std::endl;
151 return false;
152 }
153 if (rcode == erc::ReturnCodeValue::RETCODE_ILLEGAL_OPERATION) {
154 std::cerr << "RETCODE_ILLEGAL_OPERATION" << std::endl;
155 return false;
156 }
157 if (rcode == erc::ReturnCodeValue::RETCODE_NOT_ALLOWED_BY_SECURITY) {
158 std::cerr << "RETCODE_NOT_ALLOWED_BY_SECURITY" << std::endl;
159 return false;
160 }
161 std::cerr << "UNKNOWN" << std::endl;
162 return false;
163 // 根据不同的返回码值,输出相应的错误信息并返回false,表示发布失败。如果返回码不属于上述已处理的情况,则输出"UNKNOWN"并返回false。
164
165 }
166
167 void CarlaMapSensorPublisher::SetData(const char* data) {
168 _impl->_string.data(data);
169 }
170 // 将传入的字符指针指向的数据设置为要发布的字符串消息的数据。
171
172 CarlaMapSensorPublisher::CarlaMapSensorPublisher(const char* ros_name, const char* parent) :
173 _impl(std::make_shared<CarlaMapSensorPublisherImpl>()) {
174 _name = ros_name;
175 _parent = parent;
176 }
177 // 构造函数,接受一个表示ROS名称的字符指针和一个表示父名称的字符指针作为参数。
178 // 在构造函数内部,创建一个CarlaMapSensorPublisherImpl结构体的共享指针,并初始化_name和_parent成员变量。
179
181 if (!_impl)
182 return;
183
184 if (_impl->_datawriter)
185 _impl->_publisher->delete_datawriter(_impl->_datawriter);
186
187 if (_impl->_publisher)
188 _impl->_participant->delete_publisher(_impl->_publisher);
189
190 if (_impl->_topic)
191 _impl->_participant->delete_topic(_impl->_topic);
192
193 if (_impl->_participant)
194 efd::DomainParticipantFactory::get_instance()->delete_participant(_impl->_participant);
195 }
196 // 析构函数,用于清理在构造函数和其他操作中创建的Fast DDS相关对象。
197 // 如果_impl指针为空则直接返回,否则依次删除数据写入器、发布者、主题和域参与者对象,以释放相关资源。
198
200 _frame_id = other._frame_id;
201 _name = other._name;
202 _parent = other._parent;
203 _impl = other._impl;
204 }
205 // 拷贝构造函数,用于创建一个与传入的CarlaMapSensorPublisher对象相同的新对象,
206 // 拷贝其_frame_id、_name、_parent和_impl成员变量的值。
207
209 _frame_id = other._frame_id;
210 _name = other._name;
211 _parent = other._parent;
212 _impl = other._impl;
213
214 return *this;
215 }
216 // 赋值运算符重载函数,用于将一个CarlaMapSensorPublisher对象的值赋给另一个对象,
217 // 赋值其_frame_id、_name、_parent和_impl成员变量的值,并返回被赋值的对象本身。
218
220 _frame_id = std::move(other._frame_id);
221 _name = std::move(other._name);
222 _parent = std::move(other._parent);
223 _impl = std::move(other._impl);
224 }
225 // 移动构造函数,用于通过移动语义创建一个新的CarlaMapSensorPublisher对象,
226 // 将传入对象的_frame_id、_name、_parent和_impl成员变量的值移动到新对象中,原对象的这些成员变量将变为未定义状态。
227
229 _frame_id = std::move(other._frame_id);
230 _name = std::move(other._name);
231 _parent = std::move(other._parent);
232 _impl = std::move(other._impl);
233
234 return *this;
235 }
236 // 移动赋值运算符重载函数,用于通过移动语义将一个CarlaMapSensorPublisher对象的值赋给另一个对象,
237 // 移动其_frame_id、_name、_parent和_impl成员变量的值,并返回被赋值的对象本身。
238
239}
240}
CarlaMapSensorPublisher & operator=(const CarlaMapSensorPublisher &)
CarlaMapSensorPublisher(const char *ros_name="", const char *parent="")
std::shared_ptr< CarlaMapSensorPublisherImpl > _impl
const std::string & parent() const
This class represents the TopicDataType of the type String defined by the user in the IDL file.
这个类表示用户在IDL文件中定义的结构体String。
eprosima::fastrtps::types::ReturnCode_t erc
@using erc
CARLA模拟器的主命名空间。
Definition Carla.cpp:139