ROS 2 Humble Hawksbill将于2022年5月发布

Posted zhangrelay

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ROS 2 Humble Hawksbill将于2022年5月发布相关的知识,希望对你有一定的参考价值。

ROS 2 Humble Hawksbill应该是第一款走向成熟的ROS2长期支持版本(LTS)。

ROS 2目前各模块功能还在不断完善和修订,ROS 2 Humble Hawksbill是ROS2的第八个发行版。

March 21, 2022Alpha + RMW freeze
April 4, 2022Freeze
April 18, 2022Branch
April 25, 2022Beta
May 16, 2022Release Candidate
May 19, 2022Distro Freeze
May 23, 2022 General Availability
Rolling Ridley

rclcpp

标准格式转换案例:

std_msgs::msg::String 与 std::string 转换。

template<>
struct rclcpp::TypeAdapter<
   std::string,
   std_msgs::msg::String
>
{
  using is_specialized = std::true_type;
  using custom_type = std::string;
  using ros_message_type = std_msgs::msg::String;

  static
  void
  convert_to_ros_message(
    const custom_type & source,
    ros_message_type & destination)
  {
    destination.data = source;
  }

  static
  void
  convert_to_custom(
    const ros_message_type & source,
    custom_type & destination)
  {
    destination = source.data;
  }
};

使用案例:

using MyAdaptedType = TypeAdapter<std::string, std_msgs::msg::String>;

// Publish a std::string
auto pub = node->create_publisher<MyAdaptedType>(...);
std::string custom_msg = "My std::string"
pub->publish(custom_msg);

// Pass a std::string to a subscription's callback
auto sub = node->create_subscription<MyAdaptedType>(
  "topic",
  10,
  [](const std::string & msg) {...});

在目前较新版本的ROS2(b83b18598b)仅pub或sub的实现方式cpp代码就有5种。以上面改进为例,pub代码如下(使用 `std::string` 代替 `std_msgs::msg::String`):

#include <chrono>
#include <functional>
#include <memory>
#include <string>

#include "rclcpp/type_adapter.hpp"
#include "rclcpp/rclcpp.hpp"

#include "std_msgs/msg/string.hpp"

using namespace std::chrono_literals;

template<>
struct rclcpp::TypeAdapter<std::string, std_msgs::msg::String>
{
  using is_specialized = std::true_type;
  using custom_type = std::string;
  using ros_message_type = std_msgs::msg::String;

  static
  void
  convert_to_ros_message(
    const custom_type & source,
    ros_message_type & destination)
  {
    destination.data = source;
  }

  static
  void
  convert_to_custom(
    const ros_message_type & source,
    custom_type & destination)
  {
    destination = source.data;
  }
};

class MinimalPublisher : public rclcpp::Node
{
  using MyAdaptedType = rclcpp::TypeAdapter<std::string, std_msgs::msg::String>;

public:
  MinimalPublisher()
  : Node("minimal_publisher"), count_(0)
  {
    publisher_ = this->create_publisher<MyAdaptedType>("topic", 10);
    timer_ = this->create_wall_timer(
      500ms, std::bind(&MinimalPublisher::timer_callback, this));
  }

private:
  void timer_callback()
  {
    std::string message = "Hello, world! " + std::to_string(count_++);
    RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.c_str());
    publisher_->publish(message);
  }
  rclcpp::TimerBase::SharedPtr timer_;
  rclcpp::Publisher<MyAdaptedType>::SharedPtr publisher_;
  size_t count_;
};

int main(int argc, char * argv[])
{
  rclcpp::init(argc, argv);
  rclcpp::spin(std::make_shared<MinimalPublisher>());
  rclcpp::shutdown();
  return 0;
}

sub代码如下:

#include <functional>
#include <memory>
#include <string>

#include "rclcpp/type_adapter.hpp"
#include "rclcpp/rclcpp.hpp"

#include "std_msgs/msg/string.hpp"

using std::placeholders::_1;


template<>
struct rclcpp::TypeAdapter<std::string, std_msgs::msg::String>
{
  using is_specialized = std::true_type;
  using custom_type = std::string;
  using ros_message_type = std_msgs::msg::String;

  static
  void
  convert_to_ros_message(
    const custom_type & source,
    ros_message_type & destination)
  {
    destination.data = source;
  }

  static
  void
  convert_to_custom(
    const ros_message_type & source,
    custom_type & destination)
  {
    destination = source.data;
  }
};

class MinimalSubscriber : public rclcpp::Node
{
  using MyAdaptedType = rclcpp::TypeAdapter<std::string, std_msgs::msg::String>;

public:
  MinimalSubscriber()
  : Node("minimal_subscriber")
  {
    subscription_ = this->create_subscription<MyAdaptedType>(
      "topic", 10, std::bind(&MinimalSubscriber::topic_callback, this, _1));
  }

private:
  void topic_callback(const std::string & msg) const
  {
    RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg.c_str());
  }
  rclcpp::Subscription<MyAdaptedType>::SharedPtr subscription_;
};

int main(int argc, char * argv[])
{
  rclcpp::init(argc, argv);
  rclcpp::spin(std::make_shared<MinimalSubscriber>());
  rclcpp::shutdown();
  return 0;
}

这样的细节变化非常多,在dashing到foxy中,更多的是功能完善,而非bug补丁,这从侧面体现了ROS2的版本离稳定成熟还需一段时间。

ros2cli

这是命令行接口,pub和sub其实都非常常用,但是

使用标志时,将在开始发布之前等待找到一个匹配的订阅。 这避免了 ros2cli 节点在发现匹配订阅之前开始发布的问题,这会导致一些第一条消息丢失。

ros2 topic pub -1 -w 1 /chatter std_msgs/msg/String "{data: 'foo'}"

更多细节查阅官方文档。


以上是关于ROS 2 Humble Hawksbill将于2022年5月发布的主要内容,如果未能解决你的问题,请参考以下文章

ROS 2 Humble Hawksbill 环境基础

ROS 2 Humble Hawksbill 命令基础

ROS 2 Humble Hawksbill 启动文件 launch

ROS 2 Humble Hawksbill 官方文档

ROS 2 Humble Hawksbill 正式版安装流程

ROS 2 Humble Hawksbill 正式发布啦