1. 配置Cartographer

1. 源码下载编译

# 安装依赖
sudo apt-get update
sudo apt-get install -y libceres-dev libprotobuf-dev protobuf-compiler python3-wstool python3-rosdep ninja-build stow liblua5.2-dev  python3-sphinx libgtest-dev libgoogle-glog-dev libgflags-dev libatlas-base-dev libeigen3-dev libsuitesparse-dev build-essential clang cmake g++ google-mock libboost-all-dev libcairo2-dev libcurl4-openssl-dev lsb-release stow libgmock-dev libmetis-dev# 创建工作空间
wstool init src# 下载Cartographer及ROS插件
wstool merge -t src https://raw.githubusercontent.com/cartographer-project/cartographer_ros/master/cartographer_ros.rosinstall
wstool update -t src# 安装依赖
# github源修改为清华大学镜像
sudo nano /etc/ros/rosdep/sources.list.d/20-default.list
rosdep update
# 注释libabsl-dev(ubuntu20不兼容)
nano src/cartographer/package.xml 
rosdep install --from-paths src --ignore-src --rosdistro=noetic -y
#或者跳过 rosdep install --from-paths src --ignore-src --rosdistro=noetic -y --skip-keys=libabsl-dev# 编译安装
src/cartographer/scripts/install_abseil.sh 
src/cartographer/scripts/install_proto3.sh
catkin_make_isolated --install --use-ninja
source devel_isolated/setup.bash
echo "source ~/catkin_ws/devel_isolated/setup.bash" >> ~/.bashrc

从零部署实现Cartographer算法建图_tf_echo


从零部署实现Cartographer算法建图_ros_02

从零部署实现Cartographer算法建图_算法_03


从零部署实现Cartographer算法建图_算法_04


从零部署实现Cartographer算法建图_cartographer_05


从零部署实现Cartographer算法建图_cartographer_06


从零部署实现Cartographer算法建图_tf_07

2. 启动建图

2.1 配置文件

touch ~/catkin_ws/src/cartographer_ros/cartographer_ros/configuration_files/turtlebot3_lds_2d.lua
nano ~/catkin_ws/src/cartographer_ros/cartographer_ros/configuration_files/turtlebot3_lds_2d.lua
cp ~/catkin_ws/src/cartographer_ros/cartographer_ros/configuration_files/turtlebot3_lds_2d.lua ~/catkin_ws/install_isolated/share/cartographer_ros/configuration_files/# 启用 2d或3d
nano ~/catkin_ws/src/cartographer/configuration_files/map_builder.lua
cp ~/catkin_ws/src/cartographer/configuration_files/map_builder.lua ~/catkin_ws/install_isolated/share/cartographer/configuration_files
-- 使用正确的相对路径包含文件
include "map_builder.lua"
include "trajectory_builder.lua"options = {map_builder = MAP_BUILDER,trajectory_builder = TRAJECTORY_BUILDER,map_frame = "map",tracking_frame = "base_link",published_frame = "odom",odom_frame = "odom",provide_odom_frame = false,publish_frame_projected_to_2d = false,use_odometry = 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.,
}-- Turtlebot3配置调整
TRAJECTORY_BUILDER_2D.min_range = 0.1
TRAJECTORY_BUILDER_2D.max_range = 3.5
TRAJECTORY_BUILDER_2D.missing_data_ray_length = 3.5
TRAJECTORY_BUILDER_2D.use_imu_data = false
TRAJECTORY_BUILDER_2D.use_online_correlative_scan_matching = truePOSE_GRAPH.optimization_problem.huber_scale = 1e2
POSE_GRAPH.optimize_every_n_nodes = 35return options

从零部署实现Cartographer算法建图_算法_08

2.2 cartographer启动launch

touch  ~/catkin_ws/src/cartographer_ros/cartographer_ros/launch/turtlebot3_cartographer.launch
nano ~/catkin_ws/src/cartographer_ros/cartographer_ros/launch/turtlebot3_cartographer.launch
cp ~/catkin_ws/src/cartographer_ros/cartographer_ros/launch/turtlebot3_cartographer.launch ~/catkin_ws/install_isolated/share/cartographer_ros/launch
<launch><param name="/use_sim_time" value="true" />  <!-- Gazebo使用仿真时间 --><env name="CARTOGRAPHER_CONFIGURATION_DIRECTORY" value="$(find cartographer_ros)/configuration_files;$(find cartographer)/configuration_files" /><node name="cartographer_node" pkg="cartographer_ros"type="cartographer_node" args="-configuration_directory $(find cartographer_ros)/configuration_files-configuration_basename turtlebot3_lds_2d.lua"output="screen"><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 pkg="tf" type="static_transform_publisher" name="base_link_to_base_scan"args="0.0 0.0 0.0 0.0 0.0 0.0 base_link base_scan 100" /><node pkg="tf" type="static_transform_publisher" name="map_to_odom"args="0.0 0.0 0.0 0.0 0.0 0.0 map odom 100" /><node pkg="tf" type="static_transform_publisher" name="base_footprint_to_base"args="0.0 0.0 0.0 0.0 0.0 0.0 base_footprint base_link 100" /></launch>

2.3 Turtlebot3启动launch

touch ~/catkin_ws/src/turtlebot3/turtlebot3_slam/launch/turtlebot3_carto_demo.launch
nano ~/catkin_ws/src/turtlebot3/turtlebot3_slam/launch/turtlebot3_carto_demo.launch
cp ~/catkin_ws/src/turtlebot3/turtlebot3_slam/launch/turtlebot3_carto_demo.launch ~/catkin_ws/install_isolated/share/turtlebot3_slam/launch
<launch><!-- 启动Gazebo仿真 --><include file="$(find turtlebot3_gazebo)/launch/turtlebot3_house.launch" /><!-- 启动Cartographer --><include file="$(find cartographer_ros)/launch/turtlebot3_cartographer.launch" /><!-- 启动 RViz --><node name="rviz" pkg="rviz" type="rviz" args="-d $(find turtlebot3_slam)/rviz/turtlebot3_cartographer.rviz"/>
</launch>

从零部署实现Cartographer算法建图_cartographer_09


从零部署实现Cartographer算法建图_cartographer_10

2.4 执行建图

# 终端1: 启动仿真和Cartographer
source ~/catkin_ws/devel_isolated/setup.bash
export TURTLEBOT3_MODEL=waffle_pi
roslaunch turtlebot3_slam turtlebot3_carto_demo.launch# 终端2: 控制机器人移动
rosrun turtlebot3_teleop turtlebot3_teleop_key

从零部署实现Cartographer算法建图_tf_11

从零部署实现Cartographer算法建图_算法_12


从零部署实现Cartographer算法建图_ros_13


从零部署实现Cartographer算法建图_cartographer_14


从零部署实现Cartographer算法建图_tf_echo_15

3. 保存地图

rosrun map_server map_saver -f ./maps/carto_map

从零部署实现Cartographer算法建图_tf_echo_16

从零部署实现Cartographer算法建图_cartographer_17

image: ./maps/carto_map.pgm
resolution: 0.050000
origin: [-4.767262, -5.126551, 0.000000]
negate: 0
occupied_thresh: 0.65
free_thresh: 0.196

4. 调试

rostopic echo /odom

从零部署实现Cartographer算法建图_算法_18

rostopic echo /map

从零部署实现Cartographer算法建图_cartographer_19

rosrun tf tf_echo map base_link

从零部署实现Cartographer算法建图_tf_20