本文主要是介绍ROS 2 Humble Hawksbill将于2022年5月发布,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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 |
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;staticvoidconvert_to_ros_message(const custom_type & source,ros_message_type & destination){destination.data = source;}staticvoidconvert_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;staticvoidconvert_to_ros_message(const custom_type & source,ros_message_type & destination){destination.data = source;}staticvoidconvert_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;staticvoidconvert_to_ros_message(const custom_type & source,ros_message_type & destination){destination.data = source;}staticvoidconvert_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月发布的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!