ROS2 官方教程翻译(一) 关于组件(About Composition)

原文链接:https://docs.ros.org/en/foxy/Concepts/About-Composition.html

目录

ROS1-Nodes 与 Nodelets

在ROS1中,我们可以把代码写成一个Node或者一个nodelet.ROS1 的node会被编译成可执行文件executables.ROS1的nodelets会被编译成库之后在运行时被一个容器加载.

ROS2 统一的API

在ROS2里,我们建议使用一种和nodelet类似的方式编程,我们把这种方式命名为——组件(component). 这种方法可以使一些通用的概念更加容易地被加入到代码中,例如“生命周期” (life cycle). 在ROS2中使用了统一的API,这就避免了使用不同API造成的缺陷.

注:

现在还是可以自己给节点写main函数的,但是并不推荐这么做.

通过使这个过程变成“运行时决定”,码农们有了以下两种选择:

  • 让不同的节点跑在不同的进程中,这样可以使 运行/错误 分离,同时对单个节点的debug也更容易。
  • 在一个进程里跑多个节点,这样开销更小,通信效率更高。

另外,ros2 launch可以通过特殊的加载动作自动地实现这些操作。

编写一个组件

因为组件只会被编译成库,所以不需要main()函数,看这里的例子:https://github.com/ros2/demos/blob/master/composition/src/talker_component.cpp

组件通常都是rclcpp::Node的子类,因为组件不能控制线程,所以其构造函数里不能进行占用时间太长的或者是阻塞的任务。然而它可以使用计时器(tmier)来获取周期性的节律(就是可以用timer按照一定时间回调,跟spin差不多),除此以外它还可以创建publisher,subscriber,server,client等等。

这样做(把这样的class做成一个component)的一个重要原因是,这个class用rclcpp_components里的一个宏把自己注册成一个组件。

就是写好一个Node以后,在后面加上:

#include "rclcpp_components/register_node_macro.hpp"

// Register the component with class_loader.
// This acts as a sort of entry point, allowing the component to be discoverable when its library
// is being loaded into a running process.
RCLCPP_COMPONENTS_REGISTER_NODE(composition::Talker)

这样当这个Node的库被加载时,就能发现这个组件了。

另外,组件的CMakeLists里必须加上

add_library(talker_component SHARED
   src/talker_component.cpp)
rclcpp_components_register_nodes(talker_component "composition::Talker")
# To register multiple components in the same shared library, use multiple calls
# rclcpp_components_register_nodes(talker_component "composition::Talker2")

注:

为了让component_container能够找到想要的component,这个container必须在一个source过相关工作空间的shell里打开。

使用组件

composition包里有许多不同的组件使用方法,三个最常用的:

1.打开一个container,然后load_node.ROS服务就会加载传进来的包和库名字对应的组件,然后开始跑这个组件。不使用代码来激活ROS服务也行,可以通过命令行来。

2.写一个包含多个节点的程序,这时程序里需要包含组件的头文件。

3.写一个launch文件,用ros2 launch来创建container,并加载多个组件。

示例程序

// Copyright 2016 Open Source Robotics Foundation, 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.

#include "composition/talker_component.hpp"

#include <chrono>
#include <iostream>
#include <memory>
#include <utility>

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"

using namespace std::chrono_literals;

namespace composition
{

// Create a Talker "component" that subclasses the generic rclcpp::Node base class.
// Components get built into shared libraries and as such do not write their own main functions.
// The process using the component's shared library will instantiate the class as a ROS node.
Talker::Talker(const rclcpp::NodeOptions & options)
: Node("talker", options), count_(0)
{
  // Create a publisher of "std_mgs/String" messages on the "chatter" topic.
  pub_ = create_publisher<std_msgs::msg::String>("chatter", 10);

  // Use a timer to schedule periodic message publishing.
  timer_ = create_wall_timer(1s, std::bind(&Talker::on_timer, this));
}

void Talker::on_timer()
{
  auto msg = std::make_unique<std_msgs::msg::String>();
  msg->data = "Hello World: " + std::to_string(++count_);
  RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", msg->data.c_str());
  std::flush(std::cout);

  // Put the message into a queue to be processed by the middleware.
  // This call is non-blocking.
  pub_->publish(std::move(msg));
}

}  // namespace composition

#include "rclcpp_components/register_node_macro.hpp"

// Register the component with class_loader.
// This acts as a sort of entry point, allowing the component to be discoverable when its library
// is being loaded into a running process.
RCLCPP_COMPONENTS_REGISTER_NODE(composition::Talker)

 

  • 6
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值