本文主要分享cartographer的安装,并基于上一篇博客中《机器人开发实践》的编译源码仿真机器人,实现仿真建图。在本系列博客下一篇将继续分享实际项目中RoboSense16线雷达基于cartographer的建图历程。
一. cartographer的安装
安装过程可参考该博客
二. cartographer的仿真建图
- 进入cartographer安装的工作空间,激活环境。
cd ~/catkin_cartographer/
source install_isolated/setup.bash
- 创建文件cartographer_demo_rplidar.launch
cd ~/catkin_cartographer/src/cartographer_ros/cartographer_ros/launch
cp demo_revo_lds.launch cartographer_demo_rplidar.launch
gedit cartographer_demo_rplidar.launch
并修改为以下代码:
<launch>
<param name="/use_sim_time" value="true" />
<node name="cartographer_node" pkg="cartographer_ros"
type="cartographer_node" args="
-configuration_directory $(find cartographer_ros)/configuration_files
-configuration_basename rplidar.lua"
output="screen">
<remap from="scan" to="scan" />
</node>
<node name="rviz" pkg="rviz" type="rviz" required="true"
args="-d $(find cartographer_ros)/configuration_files/demo_2d.rviz" />
</launch>
- 创建文件rplidar.lua
cd ~/catkin_cartographer/src/cartographer_ros/cartographer_ros/configuration_files
cp revo_lds.lua rplidar.lua
gedit rplidar.lua
修改字段如下:
tracking_frame = “laser_link”,
published_frame = “laser_link”,
- 配置文件修改后,需要重新编译cartographer
cd ~/catkin_cartographer
catkin_make_isolated --install --use-ninja
- 打开新的终端,启动古月仿真平台(工作空间和上一篇介绍的一致)
cd ~/catkin_guyue
source devel/setup.bash
roslaunch mrobot_gazebo mrobot_laser_nav_gazebo.launch
执行后会启动gazebo仿真工具,且界面中加载了地图模型以及机器人模型,如下图:
- 打开新的终端,启动Cartographer
cd ~/catkin_cartographer
source install_isolated/setup.bash
roslaunch cartographer_ros cartographer_demo_rplidar.launch
执行后会启动rviz可视化工具,如下图所示:
7. 打开新的终端,启动键盘控制,控制机器人移动
cd ~/catkin_guyue
source devel/setup.bash
roslaunch mrobot_teleop mrobot_teleop.launch
可以看到在rviz工具中,随着机器人的移动,进行了同步构图。控制机器人围绕环境走一圈后,可以在rviz中看到建好的地图。
- 打开新的终端,保存地图
(1)停止地图构建
rosservice call /finish_trajectory 0
(2)生成.pbstream文件,文件名及其路径可修改,以下代码是我个人目录
rosservice call /write_state /home/wz/Documents/map/guyue_map.pbstream
(3)使用catro提供文件,将.pbstream文件转为.yaml和.pgm文件
cd ~/catkin_cartographer
source install_isolated/setup.bash
rosrun cartographer_ros cartographer_pbstream_to_ros_map -map_filestem=/home/wz/Documents/map/guyue_map -pbstream_filename=/home/wz/Documents/map/guyue_map.pbstream -resolution=0.05
执行完成后,将在相应目录下生成三个文件
可以打开pgm查看图形缩略图。
(4)启动地图服务加载地图
rosrun map_server map_server guyue_map.yaml
(5)在rviz中显示
rosrun rviz rviz
(6)在rviz中增加Map订阅/map话题,可以看到地图被成功加载显示