Ubuntu20安装并使用Cartographer建图导航

本文提供了一篇关于在新安装的Ubuntu系统上安装Cartographer的详细教程,包括安装依赖、获取源码、解决依赖问题、安装abseil-cpp库、编译和使用Cartographer进行2D建图的过程。在建图过程中,创建并配置了.launch文件和.lua配置文件,并介绍了如何保存和转换地图数据。

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

根据官网安装结合我实际安装的过程进行补充,仅供参考。这里的安装适合于新安装的ubuntu,使用时间长的ubuntu可能环境之间有冲突

一、安装Cartographer

官网安装链接:https://google-cartographer-ros.readthedocs.io/en/latest/compilation.html

1、首先安装依赖
sudo apt-get update
sudo apt-get install -y python3-wstool python3-rosdep ninja-build stow
2、建立工作空间并下载Cartographer源码包
mkdir cartographer_ws
cd cartographer_ws
mkdir src
cd src
git clone https://github.com/googlecartographer/cartographer_ros.git
git clone https://github.com/googlecartographer/cartographer.git
git clone https://github.com/ceres-solver/ceres-solver.git

在src目录下你就会看到下面三个文件

3、安装cartographer的依赖
sudo rosdepc init
rosdepc update
rosdep install --from-paths src --ignore-src --rosdistro=${ROS_DISTRO} -y
在执行最后一步的时候出现了一个错误
ERROR: the following packages/stacks could not have their rosdep keys resolved
to system dependencies:
cartographer: [libabsl-dev] defined as "not available" for OS version [focal]
解决:最后一句可以看出没有libabsl-dev;所以找到cartographer下的package.xml文件将有关libabsl-dev的删掉
4、安装abseil-cpp(注意它和src是同一级目录)
src/cartographer/scripts/install_abseil.sh
5、编译
catkin_make_isolated --install --use-ninja

最后编译成功后

6、刷新环境的命令
source ~/cartographer_ws/install_isolated/setup.bash

至此,Cartographer安装成功

二、使用Cartographer进行建图

打开cartographer_ros/cartographer_ros/launch文件夹,新建一个launch文件复制下面内容

<!--
  Copyright 2016 The Cartographer Authors

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<launch>
  <param name="/use_sim_time" value="true" />

  <node name="cartographer_node" pkg="cartographer_ros"
      type="cartographer_node" args="
          -configuration_directory $(find cartographer_ros)/configuration_files
          -configuration_basename demo01.lua" 
      output="screen">    <!-- 注意这里demo01.lua,改为自己的lua文件 -->
    <remap from="scan" to="scan" />
    <remap from="odom" to="odom" />
  </node>

  <node name="cartographer_occupancy_grid_node" pkg="cartographer_ros"
      type="cartographer_occupancy_grid_node" args="-resolution 0.05" />

  <!-- <node name="rviz" pkg="rviz" type="rviz" required="true"
      args="-d $(find cartographer_ros)/configuration_files/demo_2d.rviz" /> -->
 

</launch>

打开configuration_files文件夹,新建.lua文件复制下面内容,并进行修改:

-- Copyright 2016 The Cartographer Authors
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

include "map_builder.lua"
include "trajectory_builder.lua"

  options = {
    map_builder = MAP_BUILDER,
    trajectory_builder = TRAJECTORY_BUILDER,
    map_frame = "map",
    -- tracking_frame更改为我们机器人的基坐标tf,一般是base_link,
    tracking_frame = "base_footprint",
  
    -- 以下四个参数与里程计有关,在下面进行单独说明
    odom_frame = "odom",
    published_frame = "base_footprint",
    provide_odom_frame = false,
    use_odometry = true,
  
    publish_frame_projected_to_2d = false,
    use_pose_extrapolator = true,
    use_nav_sat = false,
    use_landmarks = false,
    num_laser_scans = 1,
    num_multi_echo_laser_scans = 0,
    num_subdivisions_per_laser_scan = 1,
    num_point_clouds = 0,
    lookup_transform_timeout_sec = 0.2,
    submap_publish_period_sec = 0.3,
    pose_publish_period_sec = 5e-3,
    trajectory_publish_period_sec = 30e-3,
    rangefinder_sampling_ratio = 1.,
    odometry_sampling_ratio = 1.,
    fixed_frame_pose_sampling_ratio = 1.,
    imu_sampling_ratio = 1.,
    landmarks_sampling_ratio = 1.,
  }
  
  MAP_BUILDER.use_trajectory_builder_2d = true
  
  TRAJECTORY_BUILDER_2D.submaps.num_range_data = 35
  TRAJECTORY_BUILDER_2D.min_range = 0.3
  TRAJECTORY_BUILDER_2D.max_range = 8.
  TRAJECTORY_BUILDER_2D.missing_data_ray_length = 1.
  -- 决定是否使用imu数据
  TRAJECTORY_BUILDER_2D.use_imu_data = false
  
  TRAJECTORY_BUILDER_2D.use_online_correlative_scan_matching = true
  TRAJECTORY_BUILDER_2D.real_time_correlative_scan_matcher.linear_search_window = 0.1
  TRAJECTORY_BUILDER_2D.real_time_correlative_scan_matcher.translation_delta_cost_weight = 10.
  TRAJECTORY_BUILDER_2D.real_time_correlative_scan_matcher.rotation_delta_cost_weight = 1e-1
  
  POSE_GRAPH.optimization_problem.huber_scale = 1e2
  POSE_GRAPH.optimize_every_n_nodes = 35
  POSE_GRAPH.constraint_builder.min_score = 0.65
  
  return options

然后进行编译(第一次编译慢,后面非常快)

catkin_make_isolated --install --use-ninja
 刷新环境
source ~/cartographer_ws/install_isolated/setup.bash

启动仿真小车,并运行刚刚在cartographer中写的launch文件进行建图

 保存地图

1、首先进入cartographer工作空间下,然后刷新环境变量,然后执行下面语句,完成轨迹, 停止进一步的数据
    rosservice call /finish_trajectory 0
2、将当前地图信息保存为后缀名为.pbstream
    rosservice call /write_state "{filename: '<绝对路径存放地图信息>/<地图保存的名字>.pbstream'}"
3、将后缀名为.pbstream的地图信息,转化成ros地图信息
    rosrun cartographer_ros cartographer_pbstream_to_ros_map -map_filestem=<保存地图的绝对路径>/<地图命名> -pbstream_filename=<存放pbstream地图的绝对路径>/<filename>.pbstream -resolution=0.05

 

 至此地图保存完成。

三、使用Cartographer探索式建图

在启动建图的时候,直接启动move_base就行。

### Ubuntu 20.04 上使用 TurtleBot2 和 Cartographer 进行 SLAM 对于希望在Ubuntu 20.04上利用TurtleBot2和Cartographer实现SLAM的开发者而言,重要的是理解不同组件之间的兼容性和配置方法。由于Ubuntu 20.04支持ROS Noetic版本[^1],因此所有操作都将基于此环境。 #### 安装 ROS Noetic 设置工作空间 确保已经成功安装了ROS Noetic,了一个新的Catkin工作区用于后续开发: ```bash sudo apt update && sudo apt install ros-noetic-desktop-full source /opt/ros/noetic/setup.bash mkdir -p ~/catkin_ws/src cd ~/catkin_ws/ catkin_make source devel/setup.bash ``` #### 安装配置 TurtleBot2 软件包 为了使TurtleBot2能够在ROS环境中正常运行,需下载官方提供的软件包集合: ```bash git clone https://github.com/turtlebot/turtlebot.git src/turtlebot git clone https://github.com/turtlebot/turtlebot_apps.git src/turtlebot_apps git clone https://github.com/turtlebot/turtlebot_interactions.git src/turtlebot_interactions git clone https://github.com/turtlebot/turtlebot_simulator.git src/turtlebot_simulator ``` 之后编译整个项目以确保所有依赖项都已解决: ```bash rosdep install --from-paths src --ignore-src -r -y catkin_make ``` #### 配置 Cartographer 接下来获取Cartographer及其对应的ROS接口库,将其集成到现有的Catkin工作区内: ```bash cd ~/catkin_ws/src vcs import < turtlebot_cartographer.repos ``` 完成上述命令后再次执行`catkin_make`来构更新后的源码树结构。 #### 启动与测试 一切准备就绪后可以尝试启动完整的导航堆栈以及Cartographer节点来进行实时地绘制实验: ```bash roslaunch turtlebot_bringup minimal.launch roslaunch turtlebot_navigation amcl_demo.launch map_file:=<path_to_map> roslaunch turtlebot_cartographer demo_2d.launch ``` 通过RViz可视化工具观察机器人运动轨迹及所生成的地效果。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

没入&浅出

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

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

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

打赏作者

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

抵扣说明:

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

余额充值