(一) 安装和常用操作

运行博物馆数据集

launch修改如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<launch>
<param name="/use_sim_time" value="false" />

<param name="robot_description"
textfile="$(find cartographer_ros)/urdf/backpack_2d.urdf" />

<node name="robot_state_publisher" pkg="robot_state_publisher"
type="robot_state_publisher" />

<node name="cartographer_node" pkg="cartographer_ros"
type="cartographer_node" args="
-configuration_directory $(find cartographer_ros)/configuration_files
-configuration_basename backpack_2d.lua"
output="screen">
<remap from="echoes" to="horizontal_laser_2d" />
</node>

<node name="cartographer_occupancy_grid_node" pkg="cartographer_ros"
type="cartographer_occupancy_grid_node" args="-resolution 0.05" />
</launch>

一个终端先运行launch,再到另一个终端运行rosparam set use_sim_time true,再播放cartographer_paper_deutsches_museum.bag,否则会有时间戳问题

如果只运行bag文件是没有tf信息的,其实是在urdf里面:
德意志博物馆的tf树.png

使用rosbag最好不要加倍播放,很可能出现tf的时间戳问题,可能是lookup时间导致

注意rviz需要有这三项

我的rviz插件的名称和官方不同,保证话题一样就行:

使用雷达online建图

我先拿思岚A2雷达测试,运行雷达的命令是:roslaunch rplidar_ros rplidar.launch,参见ROS中使用激光雷达(RPLIDAR)

复制/catkin_ws/src/cartographer_ros/cartographer_ros/configuration_files/revo_lds_.lua文件为no_odom_and_imu.lua,在它基础上修改22和23行:

1
2
tracking_frame = "base_footprint",   --原来是laser
published_frame = "base_footprint", --原来是laser

复制catkin_ws/src/cartographer_ros/cartographer_ros/launch/demo_revo_lds.launchhome_map_no_odom.launch,在它基础上修改23行: -configuration_basename no_odom_and_imu.lua",最终是这样:
1
2
3
4
5
6
7
8
9
10
<launch>
<node name="cartographer_node" pkg="cartographer_ros"
type="cartographer_node" args="
-configuration_directory $(find cartographer_ros)/configuration_files
-configuration_basename no_odom_and_imu.lua"
output="screen">
</node>
<node name="cartographer_occupancy_grid_node" pkg="cartographer_ros"
type="cartographer_occupancy_grid_node" args="-resolution 0.05" />
</launch>

现在跟普通的launch文件不一样了,我们需要的是/home/user/catkin_ws/install_isolated/share/cartographer_ros中的launch和lua,此时还没有,可以直接复制过去或者重新编译:

1
2
cd ~/catkin_ws
catkin_make_isolated --install --use-ninja

添加source install_isolated/setup.zsh到 bash.rc,否则执行 roslaunch 会找不到文件。

现在运行roslaunch cartographer_ros home_map_no_odom.launch,效果如下:
小强机器人+cartographer
启动后,不移动时,正常的建图日志

1
2
3
4
5
6
7
8
9
10
11
12
Extrapolator is still initializing.
[pose_graph_2d.cc:148] Inserted submap (0, 0).

[pose_graph_2d.cc:538] Remaining work items in queue: 0
[constraint_builder_2d.cc:284] 0 computations resulted in 0 additional constraints.
[constraint_builder_2d.cc:286] Score histogram: Count: 0

[motion_filter.cc:42] Motion filter reduced the number of nodes to 4.2%.
[pose_graph_2d.cc:538] Remaining work items in queue: 0
[constraint_builder_2d.cc:284] 0 computations resulted in 0 additional constraints.
[constraint_builder_2d.cc:286] Score histogram: Count: 0
scan rate: 13.27 Hz 7.54e-02 s +/- 2.37e-03 s (pulsed at 100.01% real time)

节点关系

cartographer_node是各个节点或话题的枢纽。它订阅话题scan,有时会有imu(惯导数据),然后完成定位与子图的构建,并发布话题 submap_listsubmap_list这个话题被/cartographer_occupancy_grid_node订阅,用于构建子图占用的栅格地图。

保存地图

首先不再接受进一步数据

1
rosservice call /finish_trajectory 0

rviz的建图画面会冻结,service的response如下:
1
2
3
status:
code: 0
message: "Finished trajectory 0."

日志如下:
1
2
3
4
5
Shutdown the subscriber of [scan]
[ map_builder_bridge.cc:152] Finishing trajectory with ID '0'...
[pose_graph_2d.cc:538] Remaining work items in queue: 0
[constraint_builder_2d.cc:284] 0 computations resulted in 0 additional constraints.
[constraint_builder_2d.cc:286] Score histogram: Count: 0

这是结束轨迹,进行最终优化

然后序列化保存其当前状态:

1
rosservice call /write_state "{filename: '${MAPS}/home.pbstream'}"

pbstream文件是个二进制文件,包含已经保存的SLAM状态,可以在启动cartographer_node时加载

再将pbstream 转换为pgm和yaml

1
rosrun cartographer_ros cartographer_pbstream_to_ros_map  -map_filestem=${MAPS}/home  -pbstream_filename=${MAPS}/home.pbstream -resolution=0.05

然后出现:
1
2
[pbstream_to_ros_map_main.cc:50] Loading submap slices from serialized data.
[pbstream_to_ros_map_main.cc:70] Generating combined map image from submap slices.

生成pgm和yaml文件

其实可以不结束轨迹直接保存地图

报错

正式使用时,我在机器人工控机装好了 cartographer,在本地通过SSH启动,结果出现了报错:submapslist话题的md5值不同

2020-03-25_104035.png

建出来的图是这样的:
建图出错,未形成面

这是因为本机的rviz缺了一个插件,在编译carto时会装到rviz上,这就是一个棘手的问题,如果要在本机上开启rviz,就要在本机也把cartographer这一套编译通过,也就是需要装两次。如果本机安装不成功,还可以使用vncserver在远程机上启动rviz
cmake中的部分
rviz_plugin_description

插件效果
rviz加载submap插件失败

参考:
安装过程