关于“ROS2 Topic-Statistics-Tutorial编译出错”的思考

本文档介绍了在遵循ROS2的Topic Statistics教程时可能遇到的编译问题及其解决方案。问题源于官方文档在版本管理上的不足,导致代码示例与实际版本不一致。建议使用galactic版本,并注意C++17的新特性,因为C++11和C++14可能不支持。此外,提到了代码中SharedPtr和&的差异,并提供了相关拉取请求链接以获取更多信息。

原文:

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 必然不支持啊。


  

在Ubuntu 22.04系统上安装和编译ROS2的Fast - LIO2,可按以下步骤操作: ### 环境准备 首先要确保Ubuntu 22.04系统已安装ROS 2 Humble版本。若未安装,可按以下命令安装: ```bash sudo apt update sudo apt install software-properties-common sudo add-apt-repository universe sudo apt update sudo apt install curl sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(. /etc/os-release && echo $UBUNTU_CODENAME) main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null sudo apt update sudo apt install ros-humble-desktop ``` 安装完成后,设置环境变量: ```bash source /opt/ros/humble/setup.bash ``` ### 安装依赖 安装Fast - LIO2所需的依赖库: ```bash sudo apt install python3-colcon-common-extensions sudo apt install ros-humble-navigation2 ros-humble-nav2-bringup ``` ### 下载Fast - LIO2代码 创建工作空间并下载Fast - LIO2代码: ```bash mkdir -p ws_fast_lio2/src cd ws_fast_lio2/src git clone https://github.com/hku-mars/FAST_LIO.git ``` ### 安装雷达驱动(以Livox雷达为例) 若使用Livox雷达,需安装相应驱动: ```bash git clone https://github.com/Livox-SDK/livox_ros_driver2.git ws_fast_lio2/src/livox_ros_driver2 cd ws_fast_lio2/src/livox_ros_driver2 source /opt/ros/humble/setup.sh ./build.sh humble ``` ### 编译工作空间 回到工作空间根目录进行编译: ```bash cd ~/ws_fast_lio2 colcon build --symlink-install ``` ### 设置环境变量 编译完成后,设置工作空间的环境变量: ```bash source install/setup.bash ``` ### 运行Fast - LIO2 运行Fast - LIO2节点,根据实际情况修改配置文件和启动参数: ```bash ros2 launch fast_lio mapping_avia.launch.py ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangrelay

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值