Cartographer_ros(1)

Cartographer_ros的node_main.cc主要负责参数读取和程序启动。使用gflags解析命令行参数,如collect_metrics、configuration_directory等。glog用于日志记录,如使用CHECK宏进行条件检查。程序通过LoadOptions从Lua配置文件获取节点和轨迹选项,并创建MapBuilder进行SLAM算法。Node类初始化时,将ROS话题传入SLAM算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Cartographer_ros(1)

入口node_main.cc

node_main.cc主要实现参数文件读取和程序启动功能。

gflags

首先,使用谷歌的gflags命令行参数解析工具,实现将命令行输入参数读取到程序内部的功能。

DEFINE_bool(collect_metrics, false,
            "Activates the collection of runtime metrics. If activated, the "
            "metrics can be accessed via a ROS service.");
DEFINE_string(configuration_directory, "",
              "First directory in which configuration files are searched, "
              "second is always the Cartographer installation to allow "
              "including files from there.");
DEFINE_string(configuration_basename, "",
              "Basename, i.e. not containing any directory prefix, of the "
              "configuration file.");
DEFINE_string(load_state_filename, "",
              "If non-empty, filename of a .pbstream file to load, containing "
              "a saved SLAM state.");
fgrep -l -f ./test ccc

-l是不带参数的标记,-f是带了./test参数的标记

定义参数通过DEFINE_type宏实现, 该宏的三个参数含义分别为命令行参数名, 参数默认值, 以及参数的帮助信息。

例如定义collect_metrics

DEFINE_bool(collect_metrics,  false,
            "Activates the collection of runtime metrics. If activated, the "
            "metrics can be accessed via a ROS service.");

使用以FLAGS_name的方式调用

  Node node(node_options, std::move(map_builder), &tf_buffer,
            FLAGS_collect_metrics);

glog

glog是谷歌开发的一个应用级别的日志系统库。glog里提供的CHECK系列的宏, 检测某个表达式是否为真。

Cartographer中大量使用CHECK函数。

main函数

glog库的初始化

 google::InitGoogleLogging(argv[0]);

使用gflags进行参数的初始化. 其中第三个参数为remove_flag

如果为true, gflags会移除parse过的参数, 否则gflags就会保留这些参数, 但可能会对参数顺序进行调整.

google::ParseCommandLineFlags(&argc, &argv, true);

run函数

 // 开启监听tf的独立线程
  tf2_ros::TransformListener tf(tf_buffer);

  NodeOptions node_options;
  TrajectoryOptions trajectory_options;

  // c++11: std::tie()函数可以将变量连接到一个给定的tuple上,生成一个元素类型全是引用的tuple

  // 根据Lua配置文件中的内容, 为node_options, trajectory_options 赋值
  std::tie(node_options, trajectory_options) =
      LoadOptions(FLAGS_configuration_directory, FLAGS_configuration_basename);

  // MapBuilder类是完整的SLAM算法类
  // 包含前端(TrajectoryBuilders,scan to submap) 与 后端(用于查找回环的PoseGraph) 
  auto map_builder =
      cartographer::mapping::CreateMapBuilder(node_options.map_builder_options);

map_builder 是完整的SLAM算法类,包含前端(trajectorybuilders, scan to submap matching)与后端(posegraph)

  // Node类的初始化, 将ROS的topic传入SLAM, 也就是MapBuilder
  Node node(node_options, std::move(map_builder), &tf_buffer,
            FLAGS_collect_metrics);
### ROS2 Galactic 中 cartographer_ros 的使用指南 #### 安装与配置 在 ROS2 Galactic 下安装 `cartographer_ros` 可通过源码编译完成。以下是具体操作方法: 1. 创建一个新的工作空间并克隆仓库: ```bash mkdir -p ~/ros2_galactic_ws/src cd ~/ros2_galctic_ws/src git clone https://github.com/ros2/cartographer_ros.git -b galactic-devel ``` 2. 编译依赖项并构建项目: ```bash vcs import < cartographer_ros/cartographer_ros.repos --recursive colcon build --symlink-install --packages-up-to cartographer_ros source install/setup.bash ``` 上述命令会下载必要的依赖包,并针对银河版 (Galactic) 进行适配[^1]。 3. 验证安装成功与否可以通过运行示例文件来测试: ```bash ros2 launch cartographer_ros demo_backpack_2d.launch.py ``` 此外,也可以启动 Rviz 查看效果: ```bash ros2 run rviz2 rviz2 -d ~/ros2_galactic_ws/src/cartographer_ros/cartographer_ros/configuration_files/demo_2d.rviz ``` 以上步骤展示了如何基于官方文档设置环境以及验证功能正常运作的情况[^4]。 #### 动态地图与导航集成 对于希望直接利用由 `cartographer_ros` 所产生的 SLAM 地图来进行路径规划的应用场景而言,无需额外转换至 PGM 文件再加载到 map_server 。而是可以直接订阅 `/map` 主题消息作为输入给全局或者局部成本映射器使用[^3]。 例如,在 AMCL 或者其他粒子滤波定位算法中,只需修改参数文件中的静态地图来源即可切换为实时更新的地图数据流: ```yaml global_costmap: static_layer: map_topic: /map ``` 这样做的好处在于能够即时反映环境中变化的部分,从而提高机器人应对复杂情况的能力。 #### 示例代码展示 下面给出一段简单的 Python 脚本例子说明怎样获取来自 Cartographer 发布的地图信息: ```python import rclpy from nav_msgs.msg import OccupancyGrid def callback(msg): print(f"Received new map with resolution {msg.info.resolution}") def main(args=None): rclpy.init(args=args) node = rclpy.create_node('map_subscriber') subscription = node.create_subscription( OccupancyGrid, '/map', callback, 10) while rclpy.ok(): rclpy.spin_once(node) if __name__ == '__main__': main() ``` 此脚本创建了一个节点用来监听 `/map` 主题上的新消息到达事件,并打印其分辨率属性值[^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值