# 室内激光SLAM

**注意：本教程适合于装配树莓派4或3b+的Kerloud飞车**

本教程阐述了使用激光扫描仪为Kerloud飞车实现室内SLAM的过程。

{% hint style="info" %}
📌 提示：例程代码仅适于项目研究，不可直接用于产品。
{% endhint %}

## 1. 硬件配置及环境要求

* Kerloud飞车Scorpion Laser
* 室内激光Pi定位单元(配备[思岚RpLidar A1](http://www.slamtec.com/en/Lidar/A1), 树莓派model 4或3b+和[北醒TF-Luna测距传感器](http://en.benewake.com/product/detail/5e1c1fd04d839408076b6255.html))

实测运行良好的环境为ROS Noetic和Ubuntu 20.04，以及ROS Kinetic和Ubuntu 16.04，全部必要组件在生产阶段已正确配置。

## 2. 工作原理

室内SLAM过程的实现，是通过在自动驾驶仪中将2D激光SLAM算法和EKF状态估计单元相结合。所采用的2D激光SLAM算法为谷歌Cartographer， 也是机器人界的标杆算法。Cartographer软件包可在水平二维平面中输出运动体的精准实时位置，同时TF-Luna传感器可提供高度测量结果。估计过程的示意图展示如下：

![](/files/-Mklveo081cXZTZn7Kzr)

## 3. 如何运行

### 3.1 远程可视化的网络设置

远程可视化为用户提供了一种可从远程电脑访问机载计算机的友好体验。首先，我们需要将机载树莓派与本地Wi-Fi网络进行一次连接，这样下次就可以自动设置。 登录路由器，在管理员界面可以获取机体IP地址。以TP-Link路由器为例，用户可以访问192.168.0.1或者<http://tplogin.cn/>来查看接入本地网络的全部计算机。 机载电脑的默认名称为Ubuntu，其IP地址显示如下：

![](/files/-Mklp7b6Ec5dOrD1CDZ9)

获取IP地址后，我们可以在远程电脑和飞车机载电脑上设置主机IP：

```
sudo vim /etc/hosts

# set a host name for the flying rover
# e.g. 192.168.0.104 master_ip
<IP address> master_ip
```

然后，我们可以设置远程电脑ROS环境变量，飞车机载电脑端出厂时已设置：

```
vim ~/.bashrc

export ROS_IP=`hostname -I | awk '{print $1}'`
export ROS_HOSTNAME=`hostname -I | awk '{print $1}'`
export ROS_MASTER_URI=http://master_ip:11311
```

{% hint style="info" %}
📌 您也可以使用手机作为热点来进行网络设置。 如果您不需要连接飞车中的ROS master，则可以注释掉\~/.bashrc中对应的ROS环境设置。
{% endhint %}

我们可以在远程电脑上使用以下命令来确认网络连接情况：

```
# PC side Terminal 1: setup ssh connection with the onboard Raspberry Pi
ssh ubuntu@master_ip
roscore

# PC side Terminal 2:
rostopic list
```

若网络设置正确，则可以在上面的Terminal 2中查看到/rosout和/rosout\_agg话题，否则会显示如下报错信息：

```
ERROR: Unable to communicate with master!
```

### 3.2 编译工作区

室内激光SLAM的ROS工作区位于\~/src/catkinws\_laserslam目录下，包含如下软件包：

* rplidar\_ros: RpLidar A1的驱动程序包，提供激光扫描数据；
* robot\_laserslam: laser slam包含Cartographer的配置及加载文件；
* pose\_converter: 将slam输出与测距结果融合，并将3D信息传至mavros。

工作区编译指令如下：

```
cd ~/src/catkinws_laserslam
catkin build -j 1 # set j=1 for raspberry Pi to avoid overload
```

为实现远程可视化，需要将工作区内容复制到远程电脑，并使用相同的编译指令。在后文的讲解中，我们假设远程电脑中文件路径相同。

### 3.3 基于数据集的SLAM仿真

为了便于室内slam软件包的调用，我们在目录 \~/src/catkinws\_laserslam/dataset/2D-laser-datasets下提供了几组激光扫描数据集，用户通过可以加载基于这些数据集的激光slam节点来熟悉软件工具的使用。

执行仿真所用的指令如下：

```
# PC side terminal 1: launch roscore after ssh connection
ssh ubuntu@master_ip
roscore

# PC side terminal 2: launch rosbag to play a dataset
ssh ubuntu@master_ip

cd ~/src/catkinws_laserslam \
&& cd dataset/2D-laser-datasets \
&& rosbag play floor1.bag --clock

# PC side terminal 3: launch rosbag to play a dataset
ssh ubuntu@master_ip

cd ~/src/catkinws_laserslam \
&& source devel/setup.bash \
&& roslaunch robot_laserslam databag_sim.launch

# PC side terminal 4: remote visualization
# users have to copy the workspace from the raspberry pi to the PC and build it as well
cd ~/src/catkinws_laserslam \
source devel/setup.bash \
&& roslaunch robot_laserslam visualization.launch
```

如果您正确执行以上步骤，将会弹出rviz窗口，并显示slam输出如下：

![](/files/-MkmC8gArleyI4_qravr)

说明如下：蓝色线标识表示laser坐标系的轨迹，laser坐标系、odom坐标系的关系揭示了激光扫描仪的运动情况。可以通过命令查看各坐标系的TF树：

```
rosrun tf view_frames
```

下图所示的仿真所用坐标系TF树可用于参考：

![](/files/-MkmC8gBLytKIktgs5FF)

### 3.4 室内实验

#### 3.4.1 车模式定位

在车模式下启用激光slam，建议按如下流程操作：

* 将飞车放置于四周有墙面且光线良好的室内环境中；
* 将飞车上电，并确保模式开关置于车模式；
* 使用路由器设置好本地网络，并确认机载树莓派电脑能自动接入；
* 将飞车接入QGroundcontrol，建议首次操作时严格校准陀螺仪及磁力计；
* 使用如下指令远程启动激光slam进程：

  ```
    ssh ubuntu@master_ip
    cd ~/src/catkinws_laserslam \
    && bash run_2d.sh
  ```

run\_2d.sh将依次启用室内激光slam的所有节点。

* 可以从本地电脑启动远程可视化进程：

  ```
    cd ~/src/catkinws_laserslam \
    && source devel/setup.bash \
    && roslaunch robot_laserslam visualization.launch
  ```

请注意：远程可视化操作会占用树莓派的部分算力，测试中建议关闭。

* 打开一个新的terminal用于远程树莓派，查验本地位置信息：

  ```
    rostopic echo /mavros/vision_pose/pose
    rostopic echo /mavros/local_position/pose
  ```

/mavros/vision\_pose/pose话题中的数据为pose\_converter软件包输出的融合后位置数据，而/mavros/local\_position/pose中的数据由自动驾驶仪中的位置信息。若两个话题数据正确，则可确认slam输出结果有效。

* 接下来用户可以在手动模式下，或使用基于我们SDK的应用程序来轻松实现车体控制。

车模式下的激光slam演示效果如下：

![](/files/-MkqJFe5iZAK95q4I7wq)

#### 3.4.2 多旋翼模式定位

飞车在多旋翼模式下的激光导航操作基本和车模式一致，除了底层EKF的传感设置，需要将激光定位数据和IMU数据融合。

飞车在激光SLAM定位下的半自动室内飞行演示如下：

[Video Link](https://www.bilibili.com/video/BV12u41127n7)

{% embed url="<https://www.bilibili.com/video/BV12u41127n7>" %}

## 参考文献

* Map Comparison of Lidar-based 2D SLAM Algorithms Using Precise Ground Truth, <https://ieeexplore.ieee.org/document/8581131>
* Google cartographer documentation, <https://google-cartographer-ros.readthedocs.io/en/latest/>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cloudkerneltech.gitbook.io/kerloud-flyingrover/user-guide-zh/tutorials_zh/indoor_laser_slam_zh.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
