深入解析 ROS 2 的通用发布者:GenericPublisher
类逐行解析
概述
在 ROS 2 中,GenericPublisher
类为用户提供了一种灵活的方式来发布序列化消息,特别是当消息类型在编译时未知时。这种动态处理使得 GenericPublisher
非常适合需要在运行时确定消息类型的应用场景。本文将逐行解析 GenericPublisher
类的实现,详细解释其构造、方法以及内部工作原理。
源码位置
// Copyright 2018, Bosch Software Innovations GmbH.
// Copyright 2021, Apex.AI Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef RCLCPP__GENERIC_PUBLISHER_HPP_
#define RCLCPP__GENERIC_PUBLISHER_HPP_
#include <memory>
#include <string>
#include "rcpputils/shared_library.hpp"
#include "rclcpp/callback_group.hpp"
#include "rclcpp/macros.hpp"
#include "rclcpp/node_interfaces/node_base_interface.hpp"
#include "rclcpp/node_interfaces/node_topics_interface.hpp"
#include "rclcpp/publisher_base.hpp"
#include "rclcpp/qos.hpp"
#include "rclcpp/serialized_message.hpp"
#include "rclcpp/typesupport_helpers.hpp"
#include "rclcpp/visibility_control.hpp"
#include "rmw/rmw.h"
namespace rclcpp
{
/// %Publisher for serialized messages whose type is not known at compile time.
/**
* Since the type is not known at compile time, this is not a template, and the dynamic library
* containing type support information has to be identified and loaded based on the type name.
*
* It does not support intra-process handling.
*/
class GenericPublisher : public rclcpp::PublisherBase
{
public:
// cppcheck-suppress unknownMacro
RCLCPP_SMART_PTR_DEFINITIONS(GenericPublisher)
/// Constructor.
/**
* In order to properly publish to a topic, this publisher needs to be added to
* the node_topic_interface of the node passed into this constructor.
*
* \sa rclcpp::Node::create_generic_publisher() or rclcpp::create_generic_publisher() for
* creating an instance of this class and adding it to the node_topic_interface.
*
* \param node_base Pointer to parent node's NodeBaseInterface
* \param ts_lib Type support library, needs to correspond to topic_type
* \param topic_name Topic name
* \param topic_type Topic type
* \param qos %QoS settings
* \param options %Publisher options.
* Not all publisher options are currently respected, the only relevant options for this
* publisher are `event_callbacks`, `use_default_callbacks`, and `%callback_group`.
*/
template<typename AllocatorT = std::allocator<void>>
GenericPublisher(
rclcpp::node_interfaces::NodeBaseInterface * node_base,
std::shared_ptr<rcpputils::SharedLibrary> ts_lib,
const std::string & topic_name,
const std::string & topic_type,
const rclcpp::QoS & qos,
const rclcpp::PublisherOptionsWithAllocator<AllocatorT> & options)
: rclcpp::PublisherBase(
node_base,
topic_name,
*rclcpp::get_message_typesupport_handle(topic_type, "rosidl_typesupport_cpp", *ts_lib),
options.template to_rcl_publisher_options<rclcpp::SerializedMessage>(qos),
// NOTE(methylDragon): Passing these args separately is necessary for event binding
options.event_callbacks,
options.use_default_callbacks),
ts_lib_(ts_lib)
{}
RCLCPP_PUBLIC
virtual ~GenericPublisher() = default;
/// Publish a rclcpp::SerializedMessage.
RCLCPP_PUBLIC
void publish(const rclcpp::SerializedMessage & message);
/**
* Publish a rclcpp::SerializedMessage via loaned message after de-serialization.
*
* \param message a serialized message
* \throws anything rclcpp::exceptions::throw_from_rcl_error can show
*/
RCLCPP_PUBLIC
void publish_as_loaned_msg(const rclcpp::SerializedMessage & message);
private:
// The type support library should stay loaded, so it is stored in the GenericPublisher
std::shared_ptr<rcpputils::SharedLibrary> ts_lib_;
void * borrow_loaned_message();
void deserialize_message(
const rmw_serialized_message_t & serialized_message,
void * deserialized_msg);
void publish_loaned_message(void * loaned_message);
};
} // namespace rclcpp
#endif // RCLCPP__GENERIC_PUBLISHER_HPP_
逐行解析
文件头部
// Copyright 2018, Bosch Software Innovations GmbH.
// Copyright 2021, Apex.AI Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
- 解释:这部分代码是版权声明和许可证信息,表明该代码最初由 Bosch Software Innovations GmbH 和 Apex.AI Inc. 开发,并受 Apache License 2.0 约束。
头文件保护与包含的头文件
#ifndef RCLCPP__GENERIC_PUBLISHER_HPP_
#define RCLCPP__GENERIC_PUBLISHER_HPP_
#include <memory>
#include <string>
#include "rcpputils/shared_library.hpp"
#include "rclcpp/callback_group.hpp"
#include "rclcpp/macros.hpp"
#include "rclcpp/node_interfaces/node_base_interface.hpp"
#include "rclcpp/node_interfaces/node_topics_interface.hpp"
#include "rclcpp/publisher_base.hpp"
#include "rclcpp/qos.hpp"
#include "rclcpp/serialized_message.hpp"
#include "rclcpp/typesupport_helpers.hpp"
#include "rclcpp/visibility_control.hpp"
#include "rmw/rmw.h"
- 解释:头文件保护机制确保该文件不会被多次包含。随后包含的头文件提供了实现
GenericPublisher
所需的基础设施,如rclcpp/publisher_base.hpp
定义了发布者的基础类,rclcpp/serialized_message.hpp
用于处理序列化消息。
类定义
namespace rclcpp
{
/// %Publisher for serialized messages whose type is not known at compile time.
/**
* Since the type is not known at compile time, this is not a template, and the dynamic library
* containing type support information has to be identified and loaded based on the type name.
*
* It does not support intra-process handling.
*/
class GenericPublisher : public rclcpp::PublisherBase
{
public:
RCLCPP_SMART_PTR_DEFINITIONS(GenericPublisher)
-
解释:
GenericPublisher
类继承自PublisherBase
,用于表示通用的发布者。由于消息类型在编译时未知,因此该类不是模板类,需要在运行时动态加载类型支持信息库。此外,该类不支持进程内通信。 -
智能指针定义:
RCLCPP_SMART_PTR_DEFINITIONS(GenericPublisher)
宏定义了GenericPublisher
类的智能指针类型(如SharedPtr
和ConstSharedPtr
),便于内存管理和资源共享。
构造函数
template<typename AllocatorT = std::allocator<void>>
GenericPublisher(
rclcpp::node_interfaces::NodeBaseInterface * node_base,
std::shared_ptr<rcpputils::SharedLibrary> ts_lib,
const std::string & topic_name,
const std::string & topic_type,
const rclcpp::QoS & qos,
const rclcpp::PublisherOptionsWithAllocator<AllocatorT> & options)
: rclcpp::PublisherBase(
node_base,
topic_name,
*rclcpp::get_message_typesupport_handle(topic_type, "rosidl_typesupport_cpp", *ts_lib),
options.template to_rcl_publisher_options<rclcpp::SerializedMessage>(qos),
options.event_callbacks,
options.use_default_callbacks),
ts_lib_(ts_lib)
{}
-
解释:
- 该构造函数用于初始化
GenericPublisher
对象。 node_base
是节点的基础接口指针,提供了与 ROS 2 节点相关的功能。ts_lib
是一个指向共享库的指针,动态加载消息类型的支持库。topic_name
和topic_type
分别表示发布的话题名称和话题类型。qos
是质量服务 (QoS) 设置,用于配置消息的传输质量。options
包含发布者的选项,如事件回调和回调组。
- 该构造函数用于初始化
-
父类构造函数:
- 该构造函数首先调用
PublisherBase
的构造函数,传入节点基础接口、话题名称、类型支持句柄、发布者选项等参数。 - 然后,将类型支持库 (
ts_lib
) 存储在成员变量ts_lib_
中,以确保在发布过程中该库始终处于加载状态。
- 该构造函数首先调用
析构函数
RCLCPP_PUBLIC
virtual ~GenericPublisher() = default;
- 解释:该析构函数声明为
virtual
以支持多态,使用默认析构函数即可。
publish
方法
/// Publish a rclcpp::SerializedMessage.
RCLCPP_PUBLIC
void publish(const rclcpp::SerializedMessage & message);
- 解释:
publish
方法用于发布一个序列化的消息。该方法公开定义 (RCLCPP_PUBLIC
) 以供外部调用。
publish_as_loaned_msg
方法
/**
* Publish a rclcpp::SerializedMessage via loaned message after de-serialization.
*
* \param message a serialized message
* \throws anything rclcpp::exceptions::throw_from_rcl_error can show
*/
RCLCPP_PUBLIC
void publish_as_loaned_msg(const rclcpp::SerializedMessage & message);
- 解释:
- 该方法用于通过借用消息(loaned message)的方式发布一个经过反序列化的
SerializedMessage
对象。 - 这个方法会首先将序列化的消息反序列化为特定类型的消息对象,然后以借用消息的方式发布。
- 在某些情况下,借用消息可以优化内存管理和提高发布效率。
- 如果在处理过程中发生错误,该方法会抛出
rclcpp::exceptions::throw_from_rcl_error
中描述的异常。
- 该方法用于通过借用消息(loaned message)的方式发布一个经过反序列化的
私有成员与辅助函数
private:
// The type support library should stay loaded, so it is stored in the GenericPublisher
std::shared_ptr<rcpputils::SharedLibrary> ts_lib_;
void * borrow_loaned_message();
void deserialize_message(
const rmw_serialized_message_t & serialized_message,
void * deserialized_msg);
void publish_loaned_message(void * loaned_message);
};
- 解释:
-
ts_lib_
:- 这是一个私有成员变量,用于存储类型支持库的共享指针 (
std::shared_ptr<rcpputils::SharedLibrary>
),确保该库在整个发布者的生命周期内保持加载状态。这对于动态加载消息类型的场景尤为重要。
- 这是一个私有成员变量,用于存储类型支持库的共享指针 (
-
borrow_loaned_message()
:- 这是一个私有方法,用于从中间件借用一个消息对象,该消息对象稍后将用于发布。借用消息通常可以避免不必要的内存分配和复制,从而提高性能。
-
deserialize_message()
:- 这个私有方法将一个
rmw_serialized_message_t
类型的序列化消息反序列化为特定类型的消息对象。它接收一个序列化的消息和一个指向反序列化后消息对象的指针作为参数。
- 这个私有方法将一个
-
publish_loaned_message()
:- 该私有方法用于发布借用的消息对象。与传统的消息发布方式不同,借用消息的发布可以减少内存拷贝,提高系统效率。
-
命名空间与头文件保护结束
} // namespace rclcpp
#endif // RCLCPP__GENERIC_PUBLISHER_HPP_
- 解释:
- 这部分代码关闭了
rclcpp
命名空间,并结束了头文件的防重复包含保护 (#ifndef ... #endif
)。
- 这部分代码关闭了
总结
GenericPublisher
类为 ROS 2 提供了一种灵活的方式来发布动态类型的序列化消息。通过动态加载消息类型支持库,GenericPublisher
能够在编译时类型未知的情况下发布消息。这对于需要在运行时确定消息类型的应用场景非常有用。
该类的构造函数负责初始化发布者,确保类型支持库始终保持加载状态。同时,publish
和 publish_as_loaned_msg
方法分别提供了普通发布和借用消息发布的功能,后者有助于优化内存管理和提高发布效率。私有方法和成员则进一步支持这些功能的实现。
通过对 GenericPublisher
的详细解析,开发者可以更好地理解其工作原理,并在需要动态处理消息类型的 ROS 2 应用中有效地使用这一工具。如果您在开发过程