从零Gazebo中实现Cartographer算法建图
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# 创建工作空间
mkdir -p ~/cartographer_ws/src
cd ~/cartographer_ws
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 install_isolated/setup.bash
echo "source ~/cartographer_ws/install_isolated/setup.bash" >> ~/.bashrc
2. 启动建图
2.1 配置文件
touch ~/cartographer_ws/src/cartographer_ros/cartographer_ros/configuration_files/turtlebot3_lds_2d.lua
nano ~/cartographer_ws/src/cartographer_ros/cartographer_ros/configuration_files/turtlebot3_lds_2d.lua
cp ~/cartographer_ws/src/cartographer_ros/cartographer_ros/configuration_files/turtlebot3_lds_2d.lua ~/cartographer_ws/install_isolated/share/cartographer_ros/configuration_files/# 启用
nano ~/cartographer_ws/src/cartographer/configuration_files/map_builder.lua
cp ~/cartographer_ws/src/cartographer/configuration_files/map_builder.lua ~/cartographer_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
2.2 cartographer启动launch
touch ~/cartographer_ws/src/cartographer_ros/cartographer_ros/launch/turtlebot3_cartographer.launch
nano ~/cartographer_ws/src/cartographer_ros/cartographer_ros/launch/turtlebot3_cartographer.launch
cp ~/cartographer_ws/src/cartographer_ros/cartographer_ros/launch/turtlebot3_cartographer.launch ~/cartographer_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 ~/cartographer_ws/src/turtlebot3/turtlebot3_slam/launch/turtlebot3_carto_demo.launch
nano ~/cartographer_ws/src/turtlebot3/turtlebot3_slam/launch/turtlebot3_carto_demo.launch
cp ~/cartographer_ws/src/turtlebot3/turtlebot3_slam/launch/turtlebot3_carto_demo.launch ~/cartographer_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>
2.4 执行建图
# 终端1: 启动仿真和Cartographer
source ~/catkin_ws/devel/setup.bash
source ~/cartographer_ws/devel_isolated/setup.bash
export TURTLEBOT3_MODEL=waffle_pi
roslaunch turtlebot3_slam turtlebot3_carto_demo.launch# 终端2: 控制机器人移动
rosrun turtlebot3_teleop turtlebot3_teleop_key
3. 保存地图
rosrun map_server map_saver -f ./map/carto_map
image: ./map/carto_map.pgm
resolution: 0.050000
origin: [-5.298746, -6.654918, 0.000000]
negate: 0
occupied_thresh: 0.65
free_thresh: 0.196