首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >ROS 2 Humble Hawksbill将于2022年5月发布

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

作者头像
zhangrelay
发布2021-12-02 14:04:45
发布2021-12-02 14:04:45
7370
举报

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

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

March 21, 2022

Alpha + RMW freeze

April 4, 2022

Freeze

April 18, 2022

Branch

April 25, 2022

Beta

May 16, 2022

Release Candidate

May 19, 2022

Distro Freeze

May 23, 2022

General Availability

Rolling Ridley

rclcpp

标准格式转换案例:

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

代码语言:javascript
复制
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;
  }
};

使用案例:

代码语言:javascript
复制
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`):

代码语言:javascript
复制
#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代码如下:

代码语言:javascript
复制
#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 节点在发现匹配订阅之前开始发布的问题,这会导致一些第一条消息丢失。

代码语言:javascript
复制
ros2 topic pub -1 -w 1 /chatter std_msgs/msg/String "{data: 'foo'}"

更多细节查阅官方文档。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/08/18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • rclcpp
  • ros2cli
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档