@[TOC](gazebo的运用:使用gmapping 和 hector_slam功能包实现SLAM仿真)

一、 在gazebo中构建一个用于建图和导航的虚拟环境,可以使用Building Editor工具创建,也可以使用其他功能包中已有的虚拟环境;将前面作业完成的带传感器的移动机器人(小车)模型放置到虚拟环境中,使用gmapping 和 hector_slam功能包实现SLAM仿真建立完成的地图上,实现基于move_base和amcl功能包的机器人自主导航仿真。
gmapping和hector_hector包文件
链接:https://pan.baidu.com/s/1EXYq0NG0wW8jIzQS3HSMlw
提取码:80cn

配置gmapping节点

下载gmapping

sudo apt-get install ros-melodic-gmapping
启动gmapping演示

bash
roslaunch mbot_gazebo mbot_laser_nav_gazebo.launch

![在这里插入图片描述](https://img-blog.csdnimg.cn/2020040116261436.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwNjUxMDE3,size_16,color_FFFFFF,t_70)
新的终端:

bash
roslaunch mbot_navigation gmapping_demo.launch

![在这里插入图片描述](https://img-blog.csdnimg.cn/2020040116271779.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwNjUxMDE3,size_16,color_FFFFFF,t_70)
启动控制键盘

bash
roslaunch mbot_teleop mbot_teleop.launch

控制机器人绕环境旋转一周
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200401162808212.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwNjUxMDE3,size_16,color_FFFFFF,t_70)
保存pgm(首先下载server)

bash
sudo apt-get install ros-melodic-map-server

保存地图

bash
rosrun map_server map_saver -f cloister_gmapping

关闭启动项
在根目录可见:

![在这里插入图片描述](https://img-blog.csdnimg.cn/2020040116283634.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwNjUxMDE3,size_16,color_FFFFFF,t_70)

## 安装hector——slam

bash
sudo apt-get install ros-melodic-hector-slam

![在这里插入图片描述](https://img-blog.csdnimg.cn/20200401163003868.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwNjUxMDE3,size_16,color_FFFFFF,t_70)
启动hector_slam演示

bash
roslaunch mbot_gazebo mbot_laser_nav_gazebo.launch

![在这里插入图片描述](https://img-blog.csdnimg.cn/20200401163030579.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwNjUxMDE3,size_16,color_FFFFFF,t_70)
启动rviz演示

bash
roslaunch mbot_navigation hector_demo.launch

![在这里插入图片描述](https://img-blog.csdnimg.cn/20200401163114591.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwNjUxMDE3,size_16,color_FFFFFF,t_70)
启动键盘控制

bash
roslaunch mbot_teleop mbot_teleop.launch

![在这里插入图片描述](https://img-blog.csdnimg.cn/20200401163146411.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwNjUxMDE3,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200401163301810.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwNjUxMDE3,size_16,color_FFFFFF,t_70)


保存图片

bash
rosrun map_server map_saver -f cloister_hector

![在这里插入图片描述](https://img-blog.csdnimg.cn/20200401164110757.png)

## 基于move_base的导航框架

*

bash
sudo apt-get install ros-melodic-navigation

![在这里插入图片描述](https://img-blog.csdnimg.cn/20200401164220643.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwNjUxMDE3,size_16,color_FFFFFF,t_70)
配置move-base节点
配置amcl节点
导航实例

bash
roslaunch rbx1_bringup fake_turtlebot.launch
roslaunch rbx1_nav fake_move_base_map_with_obstacles.launch
rosrun rviz rviz -d rospack find rbx1_nav/nav_obstacles.rviz
rosrun rbx1_nav move_base_square.py

导航仿真

bash
roslaunch mbot_gazebo mbot_laser_nav_gazebo.launch

![在这里插入图片描述](https://img-blog.csdnimg.cn/20200401164250575.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwNjUxMDE3,size_16,color_FFFFFF,t_70)

bash
roslaunch mbot_navigation nav_cloister_demo.launch

![在这里插入图片描述](https://img-blog.csdnimg.cn/20200401164306736.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwNjUxMDE3,size_16,color_FFFFFF,t_70)
调整位置
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200401164342699.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwNjUxMDE3,size_16,color_FFFFFF,t_70)
通过2D nav goal让机器人动起来
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200401164500529.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwNjUxMDE3,size_16,color_FFFFFF,t_70)
躲避实时的障碍物(在模拟环境加入障碍物)

![在这里插入图片描述](https://img-blog.csdnimg.cn/20200401164513892.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwNjUxMDE3,size_16,color_FFFFFF,t_70)
导航SLAM仿真

bash
roslaunch mbot_gazebo mbot_laser_nav_gazebo.launch

![在这里插入图片描述](https://img-blog.csdnimg.cn/20200401164546133.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwNjUxMDE3,size_16,color_FFFFFF,t_70)

bash
roslaunch mbot_navigation exploring_slam_demo.launch

![在这里插入图片描述](https://img-blog.csdnimg.cn/2020040116465052.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwNjUxMDE3,size_16,color_FFFFFF,t_70)
自主探索SLAM仿真

bash
roslaunch mbot_gazebo mbot_laser_nav_gazebo.launch
roslaunch mbot_navigation exploring_slam_demo.launch
rosrun mbot_navigation exploring_slam.py
```

在这里插入图片描述

粉色的花瓣,美丽地缠绕在身上。依在风里。