1. 程式人生 > >使用Google Cartographer演算法在Turtlebot等移動平臺上跑SLAM

使用Google Cartographer演算法在Turtlebot等移動平臺上跑SLAM

使用Google Cartographer演算法在Turtlebot等移動平臺上跑SLAM

本人系統為 Ubuntu 14.04.5,ROS 版本為 Indigo,使用到的平臺有 Turtlebot,Guardian,使用到的感測器有深度相機(Kinect,Asus Xtion Live Pro),鐳射雷達(Hokuyo URG-04LX,UTM-30LX)

Cartographer是Google的實時室內建圖專案,最初用在帶有感測器的谷歌揹包(backpack)上面,可以生成解析度為 5cm 的 2D 格網地圖。

Cartographer 獲得的每一幀 laser scan 資料,利用 scan match 在最佳估計位置處插入子圖(submap)中,且 scan matching 只跟當前 submap 有關。在生成一個 submap 後,會進行一次區域性的迴環(loop close),利用分支定位和預先計算的網格,所有 submap 完成後,會進行全域性的迴環[1]。

理論的東西我就搞不懂了……先看看怎麼用再說吧……

1. 系統要求及準備

參考官方說明文件[2],對於系統有如下要求:

System Requirements:
· 64-bit, modern CPU (e.g. 3rd generation i7)
· 16 GB RAM
· Ubuntu 14.04 (Trusty) and 16.04 (Xenial)
· gcc version 4.8.4 and 5.4.0
· ROS version Indigo and Kinetic

先安裝一些需要的庫和依賴包:

sudo apt-get update
sudo apt-get
install -y cmake g++ git google-mock libboost-all-dev libcairo2-dev libeigen3-dev libgflags-dev libgoogle-glog-dev liblua5.2-dev libprotobuf-dev libsuitesparse-dev libwebp-dev ninja-build protobuf-compiler python-sphinx

2. 安裝Cartographer

看了很多資料,比如:
谷歌開源SLAM庫cartographer在Turltlebot的應用
創客智造-ROS與SLAM入門教程-cartographer在Turltlebot的應用

目前多采用兩種方式安裝,一是用官方的方法(需要翻牆),二是用來自hitcm的安裝方法(不需要翻牆)。由於官方的原始碼一直在更新,我比較推薦用官方方法來安裝。hitcm的方法比較早就出現了,不過現在距離程式碼釋出都過了大半年了,不知道是不是因為程式碼更新的原因,反正我用hitcm的方法安裝了幾次是沒有成功過……用官方方法安裝也可以不用翻牆,下文中將會介紹。

用官方方法不翻牆安裝Cartographer:

# 安裝編譯工具
sudo apt-get install -y python-wstool python-rosdep ninja-build

# 建立工作空間
mkdir google_carto_ws
cd google_carto_ws
wstool init src

# 安裝原始碼包
wstool merge -t src https://raw.githubusercontent.com/googlecartographer/cartographer_turtlebot/master/cartographer_turtlebot.rosinstall
wstool update -t src    # 執行本步驟若出錯請看下文的說明

# 安裝原始碼包的依賴
rosdep init
rosdep update
rosdep install --from-paths src --ignore-src --rosdistro=${ROS_DISTRO} -y

# 編譯並安裝
catkin_make_isolated --install --use-ninja
source install_isolated/setup.bash

說明:
如果你掛了 VPN ,一般不會出錯。
如果你沒有翻牆,會出現無法下載 ceres-solver 的問題,我們可以修改其下載地址[3]:
1) 修改 google_carto_ws/src/.rosinstall 檔案
2) 修改 google_carto_ws/src/cartographter_ros/cartographter_ros.rosinstall 檔案
將兩個檔案中的 ceres-solver 路徑改為從 github 下載
https://github.com/ceres-solver/ceres-solver.git

下面是我的 .rosinstall 檔案的內容:

# THIS IS AN AUTOGENERATED FILE, LAST GENERATED USING wstool ON 2017-05-28
- git:
    local-name: cartographer
    uri: https://github.com/googlecartographer/cartographer.git
- git:
    local-name: cartographer_ros
    uri: https://github.com/googlecartographer/cartographer_ros.git
- git:
    local-name: ceres-solver
    uri: https://github.com/ceres-solver/ceres-solver.git
    version: 1.12.0rc4
- git:
    local-name: cartographer_turtlebot
    uri: https://github.com/googlecartographer/cartographer_turtlebot.git   
- git:
    local-name: cartographer_fetch
    uri: https://github.com/googlecartographer/cartographer_fetch.git  

下面是我的 cartographter_ros.rosinstall 檔案的內容:

- git: {local-name: cartographer, uri: 'https://github.com/googlecartographer/cartographer.git'}
- git: {local-name: cartographer_ros, uri: 'https://github.com/googlecartographer/cartographer_ros.git'}
- git: {local-name: ceres-solver, uri: 'https://ceres-solver.googlesource.com/ceres-solver.git', version: '1.12.0rc4'}
- git: {local-name: cartographer_turtlebot, uri: 'https://github.com/googlecartographer/cartographer_turtlebot.git'}
- git: {local-name: cartographer_fetch, uri: 'https://github.com/googlecartographer/cartographer_fetch.git'}

可以看出來它下載了 cartographer, cartographer_ros , cartographer_turtlebot 和 ceres-solver 原始碼。這裡我還添加了 cartographer_fetch 包的路徑(未來實驗室要買一臺 Fetch ,到時候可以用上啦)

到這裡編譯成功,我們就可以愉快地使用啦!

3. 測試 Cartographer(跑虛擬實驗)

要跑虛擬實驗就得先下載官方提供的幾個 bag:
(更多bags的下載可以見參考資料[4])

# cartographer_paper_deutsches_museum.bag (2d資料,493M)
wget -P ~/Downloads https://storage.googleapis.com/cartographer-public-data/bags/backpack_2d/cartographer_paper_deutsches_museum.bag

# cartographer_paper_deutsches_museum.bag (3d資料,8G左右)
wget -P ~/Downloads https://storage.googleapis.com/cartographer-public-data/bags/backpack_3d/b3-2016-04-05-14-14-00.bag

# cartographer_turtlebot_demo.bag(跑Turtlebot的模擬,4.5G)
wget -P ~/Downloads https://storage.googleapis.com/cartographer-public-data/bags/turtlebot/cartographer_turtlebot_demo.bag

# cartographer_freight_simulation_demo.bag.(跑Fetch的模擬)
wget -P ~/Downloads https://storage.googleapis.com/cartographer-public-data/bags/fetch/cartographer_freight_simulation_demo.bag

資料量太大,可以直接進入上面程式碼中的網址用迅雷下載。
下載好 bags 後,可以開始測試了:

## 跑Backpack:
# 2D demo
roslaunch cartographer_ros demo_backpack_2d.launch bag_filename:=~/Downloads/cartographer_paper_deutsches_museum.bag
# 3D demo
roslaunch cartographer_ros demo_backpack_3d.launch bag_filename:=~/Downloads/b3-2016-04-05-14-14-00.bag

## 跑Turtlebot:
# Launch the 2D LIDAR demo.(2D雷達DEMO)
roslaunch cartographer_turtlebot demo_lidar_2d.launch bag_filename:=~/Downloads/cartographer_turtlebot_demo.bag

# Launch the 2D depth camera demo.(2D深度相機DEMO)
roslaunch cartographer_turtlebot demo_depth_camera_2d.launch bag_filename:=~/Downloads/cartographer_turtlebot_demo.bag

# Launch the 3D depth camera demo.(3D深度相機DEMO)
roslaunch cartographer_turtlebot demo_depth_camera_3d.launch bag_filename:=~/Downloads/cartographer_turtlebot_demo.ba

## 跑 Fetch 2D 例子
roslaunch cartographer_fetch demo.launch bag_filename:=~/Downloads/cartographer_freight_simulation_demo.bag

4. 在 Turtlebot 等機器人移動平臺上跑 SLAM

在 Turtlebot 上跑 SLAM 比較簡單,cartographer_turtlebot 包裡都有配置好的檔案了。不管是用深度相機還是鐳射雷達,到 launch 檔案裡找吧。
接上 Turtlebot ,執行以下命令即可:

# 用深度相機:
roslaunch cartographer_turtlebot turtlebot_depth_camera_2d.launch
roslaunch cartographer_turtlebot turtlebot_depth_camera_3d.launch
# 用 Hokuyo 鐳射雷達:
roslaunch cartographer_turtlebot turtlebot_urg_lidar_2d.launch

在 Guardian 上跑我還沒有配置出來,因為引數要自己設定,比較麻煩,而且自己對 Guardian 還不夠了解,所以還在研究中。不過該演算法直接用雷達就能跑,可以參考這兩篇部落格:
Only_uuu的部落格——Cartographer連線鐳射-hokuyo UTM-30LX
wen_hust的部落格——hokuyo鐳射雷達跑cartographer

5. 關於 Cartographer 的配置檔案

Cartographer 支援不同的平臺和不同的感測器,配置合理的話理論上都可以實現。關於配置檔案中各種引數的說明,可以見官方文件(參考資料[5])。不過我目前也處於摸索的階段,Guardian 的都還沒配出來呢,有新的進展我會及時更新部落格,歡迎共同探討。

參考資料
[1] http://blog.csdn.net/u012700322/article/details/52953768
[2] https://google-cartographer.readthedocs.io/en/latest/#
[3] http://blog.csdn.net/sean_xyz/article/details/53048605
[4] https://google-cartographer-ros.readthedocs.io/en/latest/
[5] http://google-cartographer-ros.readthedocs.io/en/latest/configuration.html

推薦閱讀:
google原始碼cartographer分析一
知乎-如何看待谷歌在 10 月 6 日開源的 SLAM 演算法 cartographer?