原文:
ROS2 Topic-Statistics-Tutorial编译出错
如何避免出现类似问题呢?
在最初的时候:
docs.ros.org/en/foxy/Tutorials/Topics/Topic-Statistics-Tutorial.html
此文档中:
wget -O member_function_with_topic_statistics.cpp https://raw.githubusercontent.com/ros2/examples/master/rclcpp/topics/minimal_subscriber/member_function_with_topic_statistics.cpp
遇到问题时必然的。
其实官方文档在版本管理上不足:
master主分支一般代表最新版本。
foxy和galactic,版本选择上推荐galactic
#include <chrono>
#include <memory>
#include "rclcpp/rclcpp.hpp"
#include "rclcpp/subscription_options.hpp"
#include "std_msgs/msg/string.hpp"
class MinimalSubscriberWithTopicStatistics : public rclcpp::Node
{
public:
MinimalSubscriberWithTopicStatistics()
: Node("minimal_subscriber_with_topic_statistics")
{
// manually enable topic statistics via options
auto options = rclcpp::SubscriptionOptions();
options.topic_stats_options.state = rclcpp::TopicStatisticsState::Enable;
// configure the collection window and publish period (default 1s)
options.topic_stats_options.publish_period = std::chrono::seconds(10);
// configure the topic name (default '/statistics')
// options.topic_stats_options.publish_topic = "/topic_statistics"
auto callback = [this](std_msgs::msg::String::SharedPtr msg) {
this->topic_callback(msg);
};
subscription_ = this->create_subscription<std_msgs::msg::String>(
"topic", 10, callback, options);
}
private:
void topic_callback(const std_msgs::msg::String::SharedPtr msg) const
{
RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg->data.c_str());
}
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_;
};
int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<MinimalSubscriberWithTopicStatistics>());
rclcpp::shutdown();
return 0;
}
github.com/ros2/examples/blob/galactic/rclcpp/topics/minimal_subscriber/member_function_with_topic_statistics.cpp
和
docs.ros.org/en/foxy/Tutorials/Topics/Topic-Statistics-Tutorial.html
代码是保持一致的。
关于上问最后提及的疑问(::SharedPtr 和 & ):
- github.com/ros2/examples/pull/281
- github.com/ros2/examples/pull/337
github.com/ros2/rclcpp/pull/1598
C++ 17 新增特性。C++ 11 和 C++ 14 必然不支持啊。