(tutorial_indoorlaserslam_zh)= # 室内激光SLAM **注意:本教程适合于装配树莓派4或3b+的Kerloud飞车** 本教程阐述了使用激光扫描仪为Kerloud飞车实现室内SLAM的过程。 ```{caution} 📌 提示:例程代码仅适于项目研究,不可直接用于产品。 ``` ## 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传感器可提供高度测量结果。估计过程的示意图展示如下: ```{figure} ../img/tutorials/laserslam_diag.png :height: 450 px :width: 750 px :scale: 75 % :align: center ``` ## 3. 如何运行 ### 3.1 远程可视化的网络设置 远程可视化为用户提供了一种可从远程电脑访问机载计算机的友好体验。首先,我们需要将机载树莓派与本地Wi-Fi网络进行一次连接,这样下次就可以自动设置。 登录路由器,在管理员界面可以获取机体IP地址。以TP-Link路由器为例,用户可以访问192.168.0.1或者来查看接入本地网络的全部计算机。 机载电脑的默认名称为Ubuntu,其IP地址显示如下: ```{figure} ../img/tutorials/tplink_admin.png :height: 400 px :width: 750 px :scale: 100 % :align: center ``` 获取IP地址后,我们可以在远程电脑和飞车机载电脑上设置主机IP: sudo vim /etc/hosts # set a host name for the flying rover # e.g. 192.168.0.104 master_ip 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} 您也可以使用手机作为热点来进行网络设置。 如果您不需要连接飞车中的ROS master,则可以注释掉~/.bashrc中对应的ROS环境设置。 ``` 我们可以在远程电脑上使用以下命令来确认网络连接情况: # 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输出如下: ```{figure} ../img/tutorials/laserslam_rviz_simtest.png :height: 400 px :width: 750 px :scale: 100 % :align: center ``` 说明如下:蓝色线标识表示laser坐标系的轨迹,laser坐标系、odom坐标系的关系揭示了激光扫描仪的运动情况。可以通过命令查看各坐标系的TF树: rosrun tf view_frames 下图所示的仿真所用坐标系TF树可用于参考: ```{figure} ../img/tutorials/laserslam_tftree_bag.png :height: 350 px :width: 750 px :scale: 100 % :align: center ``` ### 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演示效果如下: ```{figure} ../img/tutorials/indoor_manual_rover_slam.gif :height: 450 px :width: 750 px :scale: 100 % :align: center ``` #### 3.4.2 多旋翼模式定位 飞车在多旋翼模式下的激光导航操作基本和车模式一致,除了底层EKF的传感设置,需要将激光定位数据和IMU数据融合。 飞车在激光SLAM定位下的半自动室内飞行演示如下: ```{raw} html ``` ## 参考文献 * Map Comparison of Lidar-based 2D SLAM Algorithms Using Precise Ground Truth, * Google cartographer documentation,