本文主要分享cartographer的安装,并基于上一篇博客中《机器人开发实践》的编译源码仿真机器人,实现仿真建图。在本系列博客下一篇将继续分享实际项目中RoboSense16线雷达基于cartographer的建图历程。
安装过程可参考该博客
cd ~/catkin_cartographer/
source install_isolated/setup.bash
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>
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”,
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仿真工具,且界面中加载了地图模型以及机器人模型,如下图:
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