Files
SLAM_Docker_Env/workspace/slam_in_autonomous_driving

SLAM in Autonomous Driving Book (SAD Book)

This book systematically introduces readers to inertial navigation, integrated navigation, LiDAR mapping, LiDAR localization, LiDAR-inertial odometry, and related knowledge. This repository contains the source code accompanying the book and is publicly available for use.

The English translation is almost done and is open-source, check it out here: https://github.com/gaoxiang12/slam-in-ad-en/blob/main/sad-en.pdf

Notes

  • The book began printing on July 10, 2023, and is expected to be available within two weeks. I will update the links to various platforms at that time.
  • August 9, 2023: The book is currently in its second printing, with some content corrections from the first edition (though without signatures). For details, see the code updates.
  • Official page from Electronic Industry Publishing House: https://item.jd.com/10080292102089.html
  • JD.com self-operated store: https://item.jd.com/13797963.html

Book Content Structure

  • Chapter 1: Overview
  • Chapter 2: Review of mathematical fundamentals: geometry, kinematics, KF filter theory, and matrix Lie groups
  • Chapter 3: Error-state Kalman filter, inertial navigation, satellite navigation, and integrated navigation
  • Chapter 4: Pre-integration, graph optimization, and pre-integration-based integrated navigation
  • Chapter 5: Point cloud basics, nearest neighbor structures, and point cloud linear fitting
  • Chapter 6: 2D LiDAR mapping: scan matching, likelihood fields, submaps, 2D loop closure detection, and pose graph
  • Chapter 7: 3D LiDAR mapping: ICP, ICP variants, NDT, NDT LO, Loam-like LO, and loosely-coupled LIO
  • Chapter 8: Tightly-coupled LIO, IESKF, and pre-integration tightly-coupled LIO
  • Chapter 9: Offline mapping: frontend, backend, batch loop closure detection, map optimization, and tile export
  • Chapter 10: Fusion localization: LiDAR localization, initialization search, tile map loading, and EKF fusion

Features of This Book

  • This book likely offers the simplest mathematical derivations and code implementations among similar materials.
  • In this book, you will reproduce many classic algorithms and data structures in LiDAR SLAM:
    • You will derive and implement an Error-State Kalman Filter (ESKF), feed it IMU and GNSS data, and observe how it estimates its state.
    • You will also implement a pre-integration system for the same purpose and compare their performance.
    • Next, you will implement common algorithms in 2D LiDAR SLAM: scan matching, likelihood fields, submaps, occupancy grids, and use loop closure detection to build larger maps—all by yourself.
    • In LiDAR SLAM, you will implement a K-d tree for approximate nearest neighbor searches, then use it for ICP and point-to-plane ICP, discussing potential improvements.
    • You will implement the classic NDT algorithm, test its registration performance, and use it to build a LiDAR odometer—much faster than most existing LOs.
    • You will also implement a point-to-plane ICP LiDAR odometer, which is similarly fast and works similarly to Loam but simpler.
    • You will integrate IMU into the LiDAR odometer, implementing both loosely-coupled and tightly-coupled LIO systems, including derivations for iterative Kalman filters and pre-integration graph optimization.
    • You will adapt the system for offline operation to allow thorough loop closure detection, ultimately creating an offline mapping system.
    • Finally, you will segment the resulting map for real-time localization.
  • Most implementations in this book are significantly simpler than those in comparable libraries, making it easier to understand their workings without dealing with complex interfaces.
  • The book employs convenient concurrent programming, often resulting in more efficient implementations than existing algorithms (partly due to historical reasons).
  • Each chapter includes dynamic demonstrations like these:

We hope you enjoy the minimalist style of this book and discover the joy of algorithms.

Datasets

  • Dataset download links:

  • Includes the following datasets (total 270GB; download selectively based on storage capacity):

    • UrbanLoco (ULHK, 3D LiDAR, urban road scenarios)
    • NCLT (3D LiDAR, RTK, campus scenarios)
    • WXB (3D LiDAR, campus scenarios)
    • 2dmapping (2D LiDAR, mall scenarios)
    • AVIA (DJI solid-state LiDAR)
    • UTBM (3D LiDAR, road scenarios)
  • Other built-in data:

    • Chapters 34 use text-formatted IMU and RTK data.
    • Chapter 7 uses some EPFL data for point cloud registration.
  • Store datasets in ./dataset/sad/ for default paths to work. Alternatively, manually specify paths or create symlinks if storage is limited.

Compilation

  • Recommended environment: Ubuntu 20.04. Older versions require GCC adjustments (C++17 support). For newer Ubuntu, install the corresponding ROS version.
  • Prerequisites:
  • Build commands:
    mkdir build
    cd build
    cmake ..
    make -j8
    
  • Executables are located in the bin directory.

Ubuntu 18.04 Adaptation

For Ubuntu 18.04, install GCC-9 and compatible TBB, or use Docker.

Install GCC-9:

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo update-alternatives --remove-all gcc
sudo update-alternatives --remove-all g++

# Priority values (1 and 10); auto-mode defaults to higher priority.
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 1
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 10

sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 1
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 10

Check version:

g++ -v

Build:

mkdir build
cd build
cmake .. -DBUILD_WITH_UBUNTU1804=ON
make -j8

Docker setup:

docker build -t sad:v1 .
./docker/docker_run.sh

Inside the container:

cd ./thirdparty/g2o
mkdir build
cd build
cmake ..
make -j8
cd /sad
mkdir build
cd build
cmake ..
make -j8

FAQs

  1. GUI crashes on certain 2023+ laptop models (GL hardware compatibility): https://github.com/gaoxiang12/slam_in_autonomous_driving/issues/67
  2. Chapter 5 test_nn GMock errors during compilation: https://github.com/gaoxiang12/slam_in_autonomous_driving/issues/18
  3. Compiler version issues: https://github.com/gaoxiang12/slam_in_autonomous_driving/issues/4
  4. g2o compilation (config.h not found): https://github.com/gaoxiang12/slam_in_autonomous_driving/issues/95

SLAM in Autonomous Driving book (SAD book)

本书向读者系统介绍了惯性导航、组合导航、激光建图、激光定位、激光惯导里程计等知识。本仓库是书籍对应的源代码仓库,可以公开使用。

注意

  • 本书已于2023.7.10开始印刷,预计在两周内上架。届时我会更新各平台的链接信息。
  • 2023.8.9 本书目前是第二次印刷,在第一次上修正了一部分内容(但没有签名了),详情见代码的推送。
  • 电子工业出版社官方:https://item.jd.com/10080292102089.html
  • 京东自营: https://item.jd.com/13797963.html

本书的内容编排

  • 第1章概述
  • 第2章数学基础知识回顾几何学、运动学、KF滤波器理论矩阵李群
  • 第3章误差状态卡尔曼滤波器惯性导航、卫星导航、组合导航
  • 第4章预积分图优化基于预积分的组合导航
  • 第5章点云基础处理各种最近邻结构点云线性拟合
  • 第6章2D激光建图scan matching, 似然场子地图2D回环检测pose graph
  • 第7章3D激光建图ICP变种ICPNDTNDT LO, Loam-like LOLIO松耦合
  • 第8章紧耦合LIOIESKF预积分紧耦合LIO
  • 第9章离线建图前端后端批量回环检测地图优化切片导出
  • 第10章融合定位激光定位初始化搜索切片地图加载EKF融合

本书的特点

  • 本书大概是您能找到的类似材料中,数学推导和代码实现最为简单的书籍。
  • 在这本书里您会复现许多激光SLAM中的经典算法和数据结构。
    • 您需要自己推导、实现一个误差状态卡尔曼滤波器(ESKF)把IMU和GNSS的数据喂给它看它如何推算自己的状态。
    • 您还会用预积分系统实现一样的功能,然后对比它们的运行方式。
    • 接下来您会实现一遍2D激光SLAM中的常见算法扫描匹配、似然场、子地图占据栅格再用回环检测来构建一个更大的地图。这些都需要您自己来完成。
    • 在激光SLAM中您也会自己实现一遍Kd树处理近似最近邻然后用这个Kd树来实现ICP点面ICP讨论它们有什么可以改进的地方。
    • 然后您会实现经典的NDT算法测试它的配准性能然后用它来搭建一个激光里程计。它比大部分现有LO快得多。
    • 您也会实现一个点面ICP的激光里程计它也非常快。它工作的方式类似于Loam但更简单。
    • 您会想要把IMU系统也放到激光里程计中。我们会实现松耦合和紧耦合的LIO系统。同样地您需要推导一遍迭代卡尔曼滤波器和预积分图优化。
    • 您需要把上面的系统改成离线运行的,让回环检测运行地充分一些。最后将它做成一个离线的建图系统。
    • 最后,您可以对上述地图进行切分,然后用来做实时定位。
  • 本书的大部分实现都要比类似的算法库简单的多。您可以快速地理解它们的工作方式,不需要面对复杂的接口。
  • 本书会使用非常方便的并发编程。您会发现,本书的实现往往比现有算法更高效。当然这有一部分是历史原因造成的。
  • 本书每章都会配有动态演示,像这样:

希望您能喜欢本书的极简风格,发现算法的乐趣所在。

数据集

  • 数据集下载链接:

  • 百度云链接: https://pan.baidu.com/s/1ELOcF1UTKdfiKBAaXnE8sQ?pwd=feky 提取码: feky

  • OneDrive链接https://1drv.ms/u/s!AgNFVSzSYXMahcEZejoUwCaHRcactQ?e=YsOYy2

  • 包含以下数据集。总量较大(270GB),请视自己硬盘容量下载。

    • UrbanLoco (ULHK3D激光道路场景)
    • NCLT (3D激光RTK校园场景)
    • WXB (3D激光园区场景)
    • 2dmapping (2D激光商场场景)
    • AVIA (大疆固态激光)
    • UTBM (3D激光道路场景)
  • 其他的内置数据

    • 第3,4章使用文本格式的IMURTK数据
    • 第7章使用了一部分EPFL的数据作为配准点云来源
  • 您应该将上述数据下载至./dataset/sad/目录下,这样许多默认参数可以正常工作。如果不那么做,您也可以手动指定这些文件路径。如果您硬盘容量不足,可以将其他硬盘的目录软链至此处。

编译

  • 本书推荐的编译环境是Ubuntu 20.04。更老的Ubuntu版本需要适配gcc编译器主要是C++17标准。更新的Ubuntu则需要您自己安装对应的ROS版本。
  • 在编译本书代码之前,请先安装以下库(如果您机器上没有安装的话)
    sudo apt install -y ros-noetic-pcl-ros ros-noetic-velodyne-msgs libopencv-dev libgoogle-glog-dev libeigen3-dev libsuitesparse-dev libpcl-dev libyaml-cpp-dev libbtbb-dev libgmock-dev
    
  • 之后使用通常的cmake, make方式就可以编译本书所有内容了。例如
mkdir build
cd build
cmake ..
make -j8
  • 编译后各章的可执行文件位于bin目录下

适配Ubuntu18.04

为了在Ubuntu18.04上编译运行需要安装gcc-9并且使用对应版本的TBB。或者在docker环境下使用。

安装gcc-9

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo update-alternatives --remove-all gcc
sudo update-alternatives --remove-all g++

#命令最后的1和10是优先级如果使用auto选择模式系统将默认使用优先级高的
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 1
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 10

sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 1
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 10

检查版本

g++ -v

编译程序

mkdir build
cd build
cmake .. -DBUILD_WITH_UBUNTU1804=ON
make -j8

在docker环境下使用

docker build -t sad:v1 .
./docker/docker_run.sh

进入docker容器后

cd ./thirdparty/g2o
mkdir build
cd build
cmake ..
make -j8
cd /sad
mkdir build
cd build
cmake ..
make -j8

常见问题

  1. 图形界面在2023年以后特定型号的笔记本端导致桌面卡死GL硬件兼容性https://github.com/gaoxiang12/slam_in_autonomous_driving/issues/67
  2. 第5章test_nn编译时gtest报gmock错误https://github.com/gaoxiang12/slam_in_autonomous_driving/issues/18
  3. 编译器版本问题:https://github.com/gaoxiang12/slam_in_autonomous_driving/issues/4
  4. g2o编译问题config.h找不到 https://github.com/gaoxiang12/slam_in_autonomous_driving/issues/95

TODO项

  • LioPreiteg在某些数据集上不收敛

NOTES

  • [已确认] ULHK的IMU似乎和别家的不一样已经去了gravity, iekf初期可能有问题
  • [已确认] NCLT的IMU在转包的时候转成了Lidar系于是Lidar与IMU之间没有旋转的外参本来Lidar是转了90度的现在Lidar是X左Y后Z下原车是X前Y右Z下。本书使用的NCLT数据均基于点云系, IMU的杆臂被忽略。
  • [已确认] NCLT的rtk fix并不是非常稳定平均误差在米级