[转]ROS2源码解析与实践-Node

更新时间:2023-07-05 19:19:03 阅读: 评论:0

[转]ROS2源码解析与实践-Node
转载说明: 原⽂链接
感谢原作者分享!如有侵权,请联系我删除,谢谢!
⽂章⽬录
1. Node定义
1.1 ROS1与ROS2的定义区别
ROS1和ROS2在功能上没有区别(提供publish,subscribe,client,rvice等⾓⾊功能)
⽣命周期:
ROS1中每个node的初始化都在⼀个main函数中,都对应⼀个进程。main函数中,通过初始化⼀个node得到⼀个node handle 传⼊⾃定义的node behavior构造函数来定义node⾏为
ROS2中每个node都继承⾃⼀个基类rclcpp::Node,采⽤⼦类继承的⽅式来暴露接⼝,这样每个node的⽣命周期都得到有效的控制。(这个lifecycle定义在更底层rcl层)
ROS2 Node提供的创建服务接⼝,node实例继承⾃基类,创建函数返回的都是SharedPtr:
rclcpp::Node->create_callback_group
rclcpp::Node->create_publisher
rclcpp::Node->create_subscription
rclcpp::Node->create_client
rclcpp::Node->create_rvice
rclcpp::Node->create_subnode
rclcpp::Node->create_wall_timer
1.2 Node.hpp代码
//std::enable_shared_from_this<T> 能让⼀个(类型是T,且已经被shared_ptr管理)安全地⽣成额外的std::shared_ptr。(定义于<memory>, 所以只要继承rc lcpp::Node就必须要添加memory)
class Node :public std::enable_shared_from_this<Node>
{
public:
RCLCPP_SMART_PTR_DEFINITIONS(Node)//"rclcpp/macros.hpp" ⽣成指针SharedPtr
/// Create a new node with the specified name.
/**
* \param[in] node_name Name of the node.
* \param[in] options Additional options to control creation of the node.
*/
RCLCPP_PUBLIC //"rclcpp/visibility_control.hpp"
explicit Node(//禁⽌隐式转换的构造函数。
const std::string & node_name,
const NodeOptions & options =NodeOptions());
/// Create a new node with the specified name.
/**
* \param[in] node_name Name of the node.
* \param[in] namespace_ Namespace of the node.
* \param[in] options Additional options to control creation of the node.
*/
RCLCPP_PUBLIC
explicit Node(
explicit Node(
const std::string & node_name,
const std::string & namespace_,
const NodeOptions & options =NodeOptions());
RCLCPP_PUBLIC
virtual~Node();
/// Get the name of the node.
/** \return The name of the node. */
RCLCPP_PUBLIC
const char*//Node::name⼀旦定义就不能改变
get_name()const;//声明const不可修改成员变量
/// Get the namespace of the node.
/**
* This namespace is the "node's" namespace, and therefore is not affected
* by any sub-namespace's that may affect entities created with this instance.
* U get_effective_namespace() to get the full namespace ud by entities.
*
* \sa get_sub_namespace()
* \sa get_effective_namespace()
* \return The namespace of the node.
*/
RCLCPP_PUBLIC
甜食英语const char*
get_namespace()const;
/
// Get the fully-qualified name of the node.
/**
* The fully-qualified name includes the local namespace and name of the node.  */
RCLCPP_PUBLIC
const char*
get_fully_qualified_name()const;
/// Get the logger of the node.
/** \return The logger of the node. */
RCLCPP_PUBLIC
rclcpp::Logger
get_logger()const;
/
// Create and return a callback group.
RCLCPP_PUBLIC
rclcpp::callback_group::CallbackGroup::SharedPtr
create_callback_group(rclcpp::callback_group::CallbackGroupType group_type);
/// Return the list of callback groups in the node.
RCLCPP_PUBLIC
const std::vector<rclcpp::callback_group::CallbackGroup::WeakPtr>&
get_callback_groups()const;
/// Create and return a Publisher.
/**
* The rclcpp::QoS has veral convenient constructors, including a
* conversion constructor for size_t, which mimics older API's that
* allows just a string and size_t to create a publisher.
*
* For example, all of the cas will work:
*
* cpp
* pub = node->create_publisher<MsgT>("chatter", 10);  // implicitly KeepLast
* pub = node->create_publisher<MsgT>("chatter", QoS(10));  // implicitly KeepLast    * pub = node->create_publisher<MsgT>("chatter", QoS(KeepLast(10)));
* pub = node->create_publisher<MsgT>("chatter", QoS(KeepAll()));
* pub = node->create_publisher<MsgT>("chatter", QoS(1).best_effort().volatile());  * {
*  rclcpp::QoS custom_qos(KeepLast(10), rmw_qos_profile_nsor_data);
*  pub = node->create_publisher<MsgT>("chatter", custom_qos);
* }
*
*
*
* The publisher options may optionally be pasd as the third argument for
* any of the above cas.
*
* \param[in] topic_name The topic for this publisher to publish on.
* \param[in] qos The Quality of Service ttings for the publisher.
* \param[in] options Additional options for the created Publisher.
* \return Shared pointer to the created publisher.
*/
template<
typename MessageT,
typename AllocatorT = std::allocator<void>,
typename PublisherT = rclcpp::Publisher<MessageT, AllocatorT>>
宝宝的英文std::shared_ptr<PublisherT>
create_publisher(
const std::string & topic_name,
const rclcpp::QoS & qos,
const PublisherOptionsWithAllocator<AllocatorT>& options =
PublisherOptionsWithAllocator<AllocatorT>()
);
//publisherT指针,定义topic_name, qos和相关options
跟读书有关的成语/// Create and return a Subscription.
/**
* \param[in] topic_name The topic to subscribe on.
* \param[in] callback The ur-defined callback function to receive a message
* \param[in] qos_history_depth The depth of the subscription's incoming message queue.
* \param[in] options Additional options for the creation of the Subscription.
* \param[in] msg_mem_strat The message memory strategy to u for allocating messages.
* \return Shared pointer to the created subscription.
*/
template<
typename MessageT,
typename CallbackT,
typename AllocatorT = std::allocator<void>,
typename CallbackMessageT =
typename rclcpp::subscription_traits::has_message_type<CallbackT>::type,
typename SubscriptionT = rclcpp::Subscription<CallbackMessageT, AllocatorT>,
typename MessageMemoryStrategyT = rclcpp::message_memory_strategy::MessageMemoryStrategy<      CallbackMessageT,
AllocatorT
>
>
std::shared_ptr<SubscriptionT>
create_subscription(
const std::string & topic_name,
const rclcpp::QoS & qos,
CallbackT && callback,
const SubscriptionOptionsWithAllocator<AllocatorT>& options =
SubscriptionOptionsWithAllocator<AllocatorT>(),
typename MessageMemoryStrategyT::SharedPtr msg_mem_strat =(
MessageMemoryStrategyT::create_default()
)
)
;
//sub消息指针,qos,相关triger callback, options
/// Create a timer.
/**
* \param[in] period Time interval between triggers of the callback.
* \param[in] callback Ur-defined callback function.
* \param[in] group Callback group to execute this timer's callback in.
中华口腔医学杂志
*/
template<typename DurationRepT =int64_t,typename DurationT = std::milli,typename CallbackT> typename rclcpp::WallTimer<CallbackT>::SharedPtr火星白羊
create_wall_timer(
std::chrono::duration<DurationRepT, DurationT> period,
std::chrono::duration<DurationRepT, DurationT> period,
CallbackT callback,
rclcpp::callback_group::CallbackGroup::SharedPtr group =nullptr);
//timer定时器,定时区间period,trigger callback
//rvice和client
/* Create and return a Client. */
template<typename ServiceT>
typename rclcpp::Client<ServiceT>::SharedPtr
create_client(
const std::string & rvice_name,
const rmw_qos_profile_t & qos_profile = rmw_qos_profile_rvices_default,    rclcpp::callback_group::CallbackGroup::SharedPtr group =nullptr);
/* Create and return a Service. */
template<typename ServiceT,typename CallbackT>
typename rclcpp::Service<ServiceT>::SharedPtr
create_rvice(
const std::string & rvice_name,
CallbackT && callback,
const rmw_qos_profile_t & qos_profile = rmw_qos_profile_rvices_default,    rclcpp::callback_group::CallbackGroup::SharedPtr group =nullptr);
//参数设置部分
RCLCPP_PUBLIC
const rclcpp::ParameterValue &
declare_parameter(
const std::string & name,
const rclcpp::ParameterValue & default_value = rclcpp::ParameterValue(), const rcl_interfaces::msg::ParameterDescriptor & parameter_descriptor =    rcl_interfaces::msg::ParameterDescriptor(),我们的生活
bool ignore_override =fal);
//泛型编程重载函数1
template<typename ParameterT>
auto
declare_parameter(
const std::string & name,
const ParameterT & default_value,
const rcl_interfaces::msg::ParameterDescriptor & parameter_descriptor =    rcl_interfaces::msg::Par
ameterDescriptor(),
bool ignore_override =fal);
//重载2
template<typename ParameterT>
std::vector<ParameterT>
declare_parameters(
const std::string & namespace_,
const std::map<std::string, ParameterT>& parameters,
bool ignore_overrides =fal);
//重载3
template<typename ParameterT>
北京跑腿公司
std::vector<ParameterT>
declare_parameters(
const std::string & namespace_,
const std::map<
std::string,
std::pair<ParameterT, rcl_interfaces::msg::ParameterDescriptor>
>& parameters,
bool ignore_overrides =fal);
//删除parameter
//删除parameter
RCLCPP_PUBLIC
void
undeclare_parameter(const std::string & name);
RCLCPP_PUBLIC
bool
has_parameter(const std::string & name)const;
RCLCPP_PUBLIC
rcl_interfaces::msg::SetParametersResult
t_parameter(const rclcpp::Parameter & parameter);
RCLCPP_PUBLIC
std::vector<rcl_interfaces::msg::SetParametersResult>
t_parameters(const std::vector<rclcpp::Parameter>& parameters);
RCLCPP_PUBLIC
rcl_interfaces::msg::SetParametersResult
t_parameters_atomically(const std::vector<rclcpp::Parameter>& parameters);
...//相关parameters的重载函数⾮常多。不列举了
//成员变量
protected:
/// Construct a sub-node, which will extend the namespace of all entities created with it. /**
* \sa create_sub_node()
*
* \param[in] other The node from which a new sub-node is created.消毒记录
* \param[in] sub_namespace The sub-namespace of the sub-node.
*/
RCLCPP_PUBLIC
Node(
const Node & other,
const std::string & sub_namespace);
//私有成员变量
private:
RCLCPP_DISABLE_COPY(Node)//删除拷贝构造函数
RCLCPP_PUBLIC
bool
group_in_node(callback_group::CallbackGroup::SharedPtr group);
rclcpp::node_interfaces::NodeBaInterface::SharedPtr node_ba_;
rclcpp::node_interfaces::NodeGraphInterface::SharedPtr node_graph_;
rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr node_logging_;
rclcpp::node_interfaces::NodeTimersInterface::SharedPtr node_timers_;
rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr node_topics_;
rclcpp::node_interfaces::NodeServicesInterface::SharedPtr node_rvices_;
rclcpp::node_interfaces::NodeClockInterface::SharedPtr node_clock_;
rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_parameters_;
rclcpp::node_interfaces::NodeTimeSourceInterface::SharedPtr node_time_source_;  rclcpp::node_interfaces::NodeWaitablesInterface::SharedPtr node_waitables_;
const rclcpp::NodeOptions node_options_;
const std::string sub_namespace_;
const std::string effective_namespace_;
};
2. 相关C++11的语法:
2.1 继承⾃std::enable_shared_from_this

本文发布于:2023-07-05 19:19:03,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1069190.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:函数   定义   创建   基类   分享   成员   重载
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图