當前位置:網站首頁>ROS 2 Humble Hawksbill將於2022年5月發布

ROS 2 Humble Hawksbill將於2022年5月發布

2021-08-20 11:58:53 zhangrelay

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 轉換。

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'}"

更多細節查閱官方文檔。


版權聲明
本文為[zhangrelay]所創,轉載請帶上原文鏈接,感謝
https://cht.chowdera.com/2021/08/20210820115852920v.html

隨機推薦