一、安装Cartographer
首先安装ubuntu对应的ROS内核版本(如:Noetic、Melodic、Lunar等版本,注意:要和ubuntu的版本对应)。

ros对应不同的ubuntu版本有不同的版本名字:
ubuntu16.04对应ros-kinetic;
ubuntu18.04对应ros-melodic;
ubuntu20.04对应ros-noetic。
如在Ubuntu20.04上,要安装ros应该用:
sudo apt-get install ros-noetic-desktop-full
再安装cartographer_ros
参考:
(3)常见的update错误解决方法:https://blog.csdn.net/Kenny_GuanHua/article/details/116845781
注意:我的cartographer_ros安装目录是google_ws
二、安装Turtlebot3
安装Turtlebot3
将Turtlebot3安装在cartographer_ros安装目录中,这是因为方便环境变量设置,否则,可能找不到cartographer算法模块。
turtlebot3_simulations:包含了更丰富的仿真文件(如一些rviz和gazebo的launch文件)
turtlebot3:功能包中继承了了TurtleBot3的机器人文件、SLAM和导航功能包、遥控功能包和bringup功能包等
turtlebot3包下载地址:https://github.com/ROBOTIS-GIT/turtlebot3
turtlebot3_simulations包下载地址:https://github.com/ROBOTIS-GIT/turtlebot3_simulations
turtlebot3 wiki简介:http://wiki.ros.org/turtlebot3
主要是两种型号:Burger 和 Waffle Pi:

参数如下:

cd ~/google_ws/src
git clone https://github.com/ROBOTIS-GIT/turtlebot3.git
git clone https://github.com/ROBOTIS-GIT/turtlebot3_simulations.git
cd ..
rosdep install --from-paths src --ignore-src --rosdistro=${ROS_DISTRO} -y
catkin_make_isolated
启动Turtlebot3仿真环境
首先启动一个Terminal窗口
(1)设置环境变量:source ~/google_ws/devel_isolated/setup.bash
(2)设置默认的 TURTLEBOT3_MODEL。turtlebot3有三个版本,burger、waffle和waffle_pi,其中waffle_pi版本中配备的传感器最全面。
(3)运行。turtlebot3_gazebo是turtlebot3的包名,turtlebot3_stage_4.launch是要运行的文件名称。
这一步的作用是先创建好一个需要仿真的模拟环境。
source ~/google_ws/devel_isolated/setup.bash
export TURTLEBOT3_MODEL=burger
roslaunch turtlebot3_gazebo turtlebot3_stage_4.launch

开始建图
再新建一个Terminal窗口,运行:
source ~/google_ws/devel_isolated/setup.bash
export TURTLEBOT3_MODEL=burger
roslaunch turtlebot3_slam turtlebot3_slam.launch slam_methods:=cartographer configuration_basename:=turtlebot3_lds_2d_gazebo.lua
这一步的功能是调用相应的建图算法对上一步的环境进行建图。(slam_methods:=cartographer指定SLAM建图算法是cartographer)

每一个terminal都要运行:source ~/google_ws/devel_isolated/setup.bash和export TURTLEBOT3_MODEL=burger,设置环境变量。
source ~/google_ws/devel_isolated/setup.bash
export TURTLEBOT3_MODEL=burger
turtlebot3_slam是turtlebot3的包名;slam_methods:=cartographer是设置slam的算法(支持gmapping, cartographer, hector, karto, frontier_exploration这几个slam算法);configuration_basename:=turtlebot3_lds_2d_gazebo.lua可以自行设置lua配置文件。
这一步中会自动调用rviz,对建图过程进行可视化。
手动控制建图
再新建一个Terminal窗口,运行如下命令打开键盘控制界面,可以通过键盘控制机器人进行移动,从而完成建图。
source ~/google_ws/devel_isolated/setup.bash
export TURTLEBOT3_MODEL=burger
roslaunch turtlebot3_teleop turtlebot3_teleop_key.launch
通过键盘进行控制小车的运行。
w和x:线速度;
a和d:角速度;
s:停止

三、分析
(1)首先看看roslaunch命令行:
roslaunch turtlebot3_gazebo turtlebot3_stage_4.launch
这句话是说:调用turtlebot3_gazebo功能包中的turtlebot3_stage_4.launch文件来启动仿真环境。
我们来看看配置文件turtlebot3_stage_4.launch:
目录:google_ws/src/turtlebot3_simulations/turtlebot3_gazebo/launch/turtlebot3_stage_4.launch
<launch>
<arg name="model" default="$(env TURTLEBOT3_MODEL)" doc="model type [burger, waffle, waffle_pi]"/>
<arg name="x_pos" default="-0.7"/>
<arg name="y_pos" default="0.0"/>
<arg name="z_pos" default="0.0"/>
<!--首先启动一个empty_world.launch(空的world),这个world的参数有:world_name、pauseduse_sim_time等-->
<include file="$(find gazebo_ros)/launch/empty_world.launch">
<arg name="world_name" value="$(find turtlebot3_gazebo)/worlds/turtlebot3_stage_4.world"/>
<arg name="paused" value="false"/>
<arg name="use_sim_time" value="true"/>
<arg name="gui" value="true"/>
<arg name="headless" value="false"/>
<arg name="debug" value="false"/>
</include>
<param name="robot_description" command="$(find xacro)/xacro --inorder $(find turtlebot3_description)/urdf/turtlebot3_$(arg model).urdf.xacro" />
<!--最后启动一个节点spawn_urdf,属于gazebo_ros包,类型是spawn_model,传入上述的arg参数-->
<node name="spawn_urdf" pkg="gazebo_ros" type="spawn_model" args="-urdf -model turtlebot3_burger -x $(arg x_pos) -y $(arg y_pos) -z $(arg z_pos) -param robot_description" />
</launch>
(2)再来看看命令:
source ~/google_ws/devel_isolated/setup.bash
export TURTLEBOT3_MODEL=burger
roslaunch turtlebot3_slam turtlebot3_slam.launch slam_methods:=cartographer configuration_basename:=turtlebot3_lds_2d_gazebo.lua
这个命令是:调用turtlebot3_slam功能包中的turtlebot3_slam.launch文件启动建图功能,参数是:slam_methods和configuration_basename。
a.首先我们来看看配置文件turtlebot3_lds_2d_gazebo.lua。
目录:google_ws/src/turtlebot3/turtlebot3_slam/config/turtlebot3_lds_2d_gazebo.lua
#turtlebot3_lds_2d_gazebo.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 = "base_footprint", -- imu_link, If you are using gazebo, use 'base_footprint' (libgazebo_ros_imu's bug)
published_frame = "odom", -- 设置为tf树最顶端的坐标系名称,设置完成后cartographer会发布map->published_frame的坐标系
odom_frame = "odom", -- 里程计的坐标系名字
provide_odom_frame = false, -- 是否提供odom的tf, 如果为true,则tf树为map->odom->footprint,如果为false,则tf树为map->published_frame
publish_frame_projected_to_2d = false, -- 是否将坐标系投影到平面上,一般为false
use_odometry = true, --是否使用里程计,如果使用要求一定要有odom的tf
use_nav_sat = false, --是否使用gps
use_landmarks = false, --是否使用landmark
num_laser_scans = 1, --是否使用单线激光数据并设置订阅topic的数量
num_multi_echo_laser_scans = 0, --是否使用multi_echo_laser_scans并设置订阅topic的数量
num_subdivisions_per_laser_scan = 1, --1帧数据被分成几次处理,一般为1
num_point_clouds = 0, -- 是否使用16线点云数据
lookup_transform_timeout_sec = 0.2,-- 查找tf时的超时时间
submap_publish_period_sec = 0.3,-- 发布数据的时间间隔
pose_publish_period_sec = 5e-3,
trajectory_publish_period_sec = 30e-3,
-- 传感器数据的采样频率
rangefinder_sampling_ratio = 1., --设置为0.1则表示每10帧数据使用1帧数据
odometry_sampling_ratio = 1.,
fixed_frame_pose_sampling_ratio = 1.,
imu_sampling_ratio = 1.,
landmarks_sampling_ratio = 1.,
}
--修改之前5个lua文件参数可以使用这种方式
MAP_BUILDER.use_trajectory_builder_2d = true -- 进行2D建图
TRAJECTORY_BUILDER_2D.min_range = 0.1 --激光的最近有效距离
TRAJECTORY_BUILDER_2D.max_range = 3.5 --激光最远的有效距离
TRAJECTORY_BUILDER_2D.missing_data_ray_length = 3. --无效激光数据设置距离为该数值
TRAJECTORY_BUILDER_2D.use_imu_data = true --是否使用imu数据
TRAJECTORY_BUILDER_2D.use_online_correlative_scan_matching = true
TRAJECTORY_BUILDER_2D.motion_filter.max_angle_radians = math.rad(0.1)
POSE_GRAPH.constraint_builder.min_score = 0.65
POSE_GRAPH.constraint_builder.global_localization_min_score = 0.7
return options
b.我们再来看看启动文件
目录:google_ws/src/turtlebot3/turtlebot3_slam/launch/turtlebot3_slam.launch
<!-- turtlebot3_slam.launch -->
<launch>
<!-- Arguments -->
<!-- 自己可以设置模型的名称,如:burger, waffle, waffle_pi,一般通过export TURTLEBOT3_MODEL=burger来设置 -->
<arg name="model" default="$(env TURTLEBOT3_MODEL)" doc="model type [burger, waffle, waffle_pi]"/>
<!-- 自己可以设置slam算法 -->
<arg name="slam_methods" default="gmapping" doc="slam type [gmapping, cartographer, hector, karto, frontier_exploration]"/>
<!-- 配置文件的路径,默认是turtlebot3_lds_2d.lua,但是该参数在命令行中被指定为:turtlebot3_lds_2d_gazebo.lua -->
<arg name="configuration_basename" default="turtlebot3_lds_2d.lua"/>
<!-- true:表示默认启动rviz -->
<arg name="open_rviz" default="true"/>
<!-- TurtleBot3 -->
<!-- 启动TurtleBot3包中robot的外形配置文件 -->
<include file="$(find turtlebot3_bringup)/launch/turtlebot3_remote.launch">
<arg name="model" value="$(arg model)" />
</include>
<!-- SLAM: Gmapping, Cartographer, Hector, Karto, Frontier_exploration, RTAB-Map -->
<!-- 启动TurtleBot3的算法配置文件 -->
<include file="$(find turtlebot3_slam)/launch/turtlebot3_$(arg slam_methods).launch">
<arg name="model" value="$(arg model)"/>
<arg name="configuration_basename" value="$(arg configuration_basename)"/>
</include>
<!-- rviz -->
<!-- 启动vriz配置文件 -->
<group if="$(arg open_rviz)">
<node pkg="rviz" type="rviz" name="rviz" required="true"
args="-d $(find turtlebot3_slam)/rviz/turtlebot3_$(arg slam_methods).rviz"/>
</group>
</launch>
c.我们看看TurtleBot3包中robot的外形配置文件turtlebot3_remote.launch
google_ws/src/turtlebot3/turtlebot3_slam/turtlebot3_bringup/launch/turtlebot3_remote.launch
<launch>
<arg name="model" default="$(env TURTLEBOT3_MODEL)" doc="model type [burger, waffle, waffle_pi]"/>
<!-- 多机协同,默认为空 -->
<arg name="multi_robot_name" default=""/>
<!-- 使用include函数将description.launch.xml文件包含进来 -->
<include file="$(find turtlebot3_bringup)/launch/includes/description.launch.xml">
<arg name="model" value="$(arg model)" />
</include>
<node pkg="robot_state_publisher" type="robot_state_publisher" name="robot_state_publisher">
<param name="publish_frequency" type="double" value="50.0" />
<param name="tf_prefix" value="$(arg multi_robot_name)"/>
</node>
</launch>
d.我们看看description.launch.xml文件
目录:google_ws/src/turtlebot3/turtlebot3_slam/turtlebot3_bringup/launch/includes/description.launch.xml
<launch>
<arg name="model"/>
<!--使用xacro类型的文件来描述robot的外形,并用xacro命令进行解析,最后形成urdf类型文件,方便gazebo识别-->
<!--urdf描述语言不能进行逻辑判断等高级操作,而xacro类型文件支持高级语法,如变量定义、逻辑判断-->
<arg name="urdf_file" default="$(find xacro)/xacro --inorder '$(find turtlebot3_description)/urdf/turtlebot3_$(arg model).urdf.xacro'" />
<param name="robot_description" command="$(arg urdf_file)" />
</launch>
f.最后我们看看我们看看turtlebot3_$(arg model).urdf.xacro文件,由于model我们通过export TURTLEBOT3_MODEL=burger命令设置为burger,所以,该文件是:turtlebot3_burger.urdf.xacro
目录:google_ws/src/turtlebot3/turtlebot3_description/urdf/turtlebot3_burger.urdf.xacro
注意base_link和base_footprint的区别:
base_link是固定在机器人本体上的坐标系,通常选择机器人腰部。
base_footprint表示机器人base_link原点在地面上的投影,区别base_link之处是其“z”坐标不同。
一般为了模型不陷入地面,base_footprint的“z”坐标比base_link高。
<?xml version="1.0" ?>
<robot name="turtlebot3_burger" xmlns:xacro="http://ros.org/wiki/xacro">
<xacro:include filename="$(find turtlebot3_description)/urdf/common_properties.xacro"/>
<xacro:include filename="$(find turtlebot3_description)/urdf/turtlebot3_burger.gazebo.xacro"/>
<link name="base_footprint"/>
<!--设置joint(关节名为base_joint),将base_link连接到base_footprint上面-->
<joint name="base_joint" type="fixed">
<parent link="base_footprint"/>
<child link="base_link"/>
<!--相对于base_footprint坐标系,base_link在base_footprint上方0.01米处,没有旋转-->
<origin xyz="0.0 0.0 0.010" rpy="0 0 0"/>
</joint>
<link name="base_link">
<visual>
<origin xyz="-0.032 0 0.0" rpy="0 0 0"/>
<geometry>
<mesh filename="package://turtlebot3_description/meshes/bases/burger_base.stl" scale="0.001 0.001 0.001"/>
</geometry>
<material name="light_black"/>
</visual>
<collision>
<origin xyz="-0.032 0 0.070" rpy="0 0 0"/>
<geometry>
<box size="0.140 0.140 0.143"/>
</geometry>
</collision>
<inertial>
<origin xyz="0 0 0" rpy="0 0 0"/>
<mass value="8.2573504e-01"/>
<inertia ixx="2.2124416e-03" ixy="-1.2294101e-05" ixz="3.4938785e-05"
iyy="2.1193702e-03" iyz="-5.0120904e-06"
izz="2.0064271e-03" />
</inertial>
</link>
<joint name="wheel_left_joint" type="continuous">
<parent link="base_link"/>
<child link="wheel_left_link"/>
<origin xyz="0.0 0.08 0.023" rpy="-1.57 0 0"/>
<axis xyz="0 0 1"/>
</joint>
<link name="wheel_left_link">
<visual>
<origin xyz="0 0 0" rpy="1.57 0 0"/>
<geometry>
<mesh filename="package://turtlebot3_description/meshes/wheels/left_tire.stl" scale="0.001 0.001 0.001"/>
</geometry>
<material name="dark"/>
</visual>
<collision>
<origin xyz="0 0 0" rpy="0 0 0"/>
<geometry>
<cylinder length="0.018" radius="0.033"/>
</geometry>
</collision>
<inertial>
<origin xyz="0 0 0" />
<mass value="2.8498940e-02" />
<inertia ixx="1.1175580e-05" ixy="-4.2369783e-11" ixz="-5.9381719e-09"
iyy="1.1192413e-05" iyz="-1.4400107e-11"
izz="2.0712558e-05" />
</inertial>
</link>
<joint name="wheel_right_joint" type="continuous">
<parent link="base_link"/>
<child link="wheel_right_link"/>
<origin xyz="0.0 -0.080 0.023" rpy="-1.57 0 0"/>
<axis xyz="0 0 1"/>
</joint>
<link name="wheel_right_link">
<visual>
<origin xyz="0 0 0" rpy="1.57 0 0"/>
<geometry>
<mesh filename="package://turtlebot3_description/meshes/wheels/right_tire.stl" scale="0.001 0.001 0.001"/>
</geometry>
<material name="dark"/>
</visual>
<collision>
<origin xyz="0 0 0" rpy="0 0 0"/>
<geometry>
<cylinder length="0.018" radius="0.033"/>
</geometry>
</collision>
<inertial>
<origin xyz="0 0 0" />
<mass value="2.8498940e-02" />
<inertia ixx="1.1175580e-05" ixy="-4.2369783e-11" ixz="-5.9381719e-09"
iyy="1.1192413e-05" iyz="-1.4400107e-11"
izz="2.0712558e-05" />
</inertial>
</link>
<joint name="caster_back_joint" type="fixed">
<parent link="base_link"/>
<child link="caster_back_link"/>
<origin xyz="-0.081 0 -0.004" rpy="-1.57 0 0"/>
</joint>
<link name="caster_back_link">
<collision>
<origin xyz="0 0.001 0" rpy="0 0 0"/>
<geometry>
<box size="0.030 0.009 0.020"/>
</geometry>
</collision>
<inertial>
<origin xyz="0 0 0" />
<mass value="0.005" />
<inertia ixx="0.001" ixy="0.0" ixz="0.0"
iyy="0.001" iyz="0.0"
izz="0.001" />
</inertial>
</link>
<joint name="imu_joint" type="fixed">
<parent link="base_link"/>
<child link="imu_link"/>
<origin xyz="-0.032 0 0.068" rpy="0 0 0"/>
</joint>
<link name="imu_link"/>
<joint name="scan_joint" type="fixed">
<parent link="base_link"/>
<child link="base_scan"/>
<origin xyz="-0.032 0 0.172" rpy="0 0 0"/>
</joint>
<!--Laser link的配置-->
<link name="base_scan">
<visual>
<origin xyz="0 0 0.0" rpy="0 0 0"/>
<geometry>
<mesh filename="package://turtlebot3_description/meshes/sensors/lds.stl" scale="0.001 0.001 0.001"/>
</geometry>
<material name="dark"/>
</visual>
<collision>
<origin xyz="0.015 0 -0.0065" rpy="0 0 0"/>
<geometry>
<cylinder length="0.0315" radius="0.055"/>
</geometry>
</collision>
<inertial>
<mass value="0.114" />
<origin xyz="0 0 0" />
<inertia ixx="0.001" ixy="0.0" ixz="0.0"
iyy="0.001" iyz="0.0"
izz="0.001" />
</inertial>
</link>
</robot>