使用激光雷达(rplidar A2)和 google cartographer_ros算法包建图(以及转换为yaml文件)

一.下载必要的cartographer依赖以及SDK
(引用自参照网址:
https://community.bwbot.org/topic/136/google%E6%BF%80%E5%85%89%E9%9B%B7%E8%BE%BEslam%E7%AE%97%E6%B3%95cartographer%E7%9A%84%E5%AE%89%E8%A3%85%E5%8F%8Abag%E5%8C%85demo%E6%B5%8B%E8%AF%95/2)
1.安装依赖包
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.安装ceres_solver

cd  ~/Documents
git clone https://github.com/BlueWhaleRobot/ceres-solver.git
cd ceres-solver
mkdir build
cd build
cmake ..
make -j
sudo make install

3.安装prtobuf 3.0

cd  ~/Documents
git clone https://github.com/google/protobuf.git
cd protobuf
git checkout 3.6.x
mkdir build
cd build
cmake  \
  -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
  -DCMAKE_BUILD_TYPE=Release \
  -Dprotobuf_BUILD_TESTS=OFF \
  ../cmake
make -j 2
sudo make install

4.安装cartographer开发工具包

cd  ~/Documents
git clone https://github.com/BlueWhaleRobot/cartographer.git
cd cartographer
mkdir build
cd build
cmake ..
make -j
sudo make install

5.安装cartographer_ros(ROS的package 安装在ROS工作空间下即可)

cd ~/Documents/ros/src   #请修改路径到自己的ROS catkin工作空间
git clone https://github.com/BlueWhaleRobot/cartographer_ros.git
cd ..
catkin_make
注意
若以上编译过程出现类似报错:
CMake Error at /usr/local/share/cartographer/cmake/functions.cmake:101 (message):
  Compiling in Debug mode is not supported and can cause severely degraded
  performance.  You should change the build type to Release.  If you want to
  build in Debug mode anyway, call CMake with -DFORCE_DEBUG_BUILD=True
Call Stack (most recent call first):
  cartographer_ros/cartographer_ros/CMakeLists.txt:42 (google_initialize_cartographer_project)

那么是编译时选择编译模式的问题,对此,解决的办法是在编译时采用DEBUG模式,即将对应的编译语句改为如下:

$ catkin_make -DFORCE_DEBUG_BUILD=True
$ cmake -DFORCE_DEBUG_BUILD=True ..

问题即可解决

二.安装激光雷达的ROS包(我的激光雷达为 思岚A2)

$ cd (你的ROS工作空间下)/src
$ git clone https://github.com/Slamtec/rplidar_ros.git(如果这个包不好用,百度一下其他的包,这个是思岚的)
$ cd ..
$ catkin_make
$ roslaunch rplidar_ros view_rpliar.launch

不出意外的话,可以看到弹出Rviz里面显示了激光雷达的识别结果,表示安装成功了

三.建图走起
1.首先启动激光雷达,以及 rosbag
$ roslaunch    rplidar_ros rplidar.launch

用rosbag 记录雷达发出的话题 /scan  的数据,用来建图

$ rosbag record /scan

2. 启动机器人的运动控制程序,让机器人围绕你想要建图的区域跑一圈或以上,在rosbag中记录数据,每个机器的运动控制不一样(手柄 手机 键盘...),此处不加代码了

3.机器人跑完一圈后,本地的rosbag应该也有了一份完整的地图数据源,可以关闭以上的rplidar_ros和数据录制了.将录制数据保存在 /home 下,重命名为 '1.bag'

4.回放刚刚保存的数据,建图

$ roslaunch    cartographer_ros    demo_xiaoqiang_rplidar_2d.launch bag_filename:=/home/xiaoqiang/ 1.bag(你存放地图的目录)

此时,你应该看到Rviz里面的建图画面:
使用激光雷达(rplidar A2)和 google cartographer_ros算法包建图(以及转换为yaml文件)_第1张图片
下面,静静等待你的数据包回放完毕(终端会有提示),过程时间会根据你的电脑性能而有差异.
5.结束建图
将建图的结果(.pbstream)保存,文件可以在~/.ros/目录找到.操作如下:

#打开ros service调用工具
#在小车主机上新开一个命令终端
rosrun rqt_service_caller rqt_service_caller
#先调用/finish_trajectory服务结束建图
#接着调用/write_state服务,将cartographer_ros建图结果保存为pbstream文件,test.pbstream。

6.地图格式转换
将建好的地图保存为我们可以使用的 .yaml 文件
使用如下方法,其中-pbstream_filename后面为上面保存的pbstream文件的目录
-map_filestem为即将保存的ROS地图文件名和路径,此处不佳后缀
(如果提示参数不对,将 '=' 改为 ':=')
-resolution=0.05 为地图精度:一个栅格0.05m

rosrun cartographer_ros cartographer_pbstream_to_ros_map -map_filestem=/home/xiaoqiang/map -pbstream_filename=/home/xiaoqiang/test.pbstream -resolution=0.05

如果成功的话,将会得到两个ROS的地图数据文件

你可能感兴趣的:(ROS建图)