#Install wstool and rosdep.
sudo apt-get update
sudo apt-get install -y python-wstool python-rosdep ninja-build
#Create a new workspace in ‘catkin_google_ws’.
mkdir catkin_google_ws
cd catkin_google_ws
wstool init src
#Merge the cartographer_ros.rosinstall file and fetch code for dependencies.
wstool merge -t src https://raw.githubusercontent.com/googlecartographer/cartographer_ros/master/cartographer_ros.rosinstall
wstool update -t src
wstool update -t src这一步可能会显示无法下载cere-solver
解决办法:更改src下的.rosinstall文件的cere-slover的国内路径,将https://ceres-solver.googlesource.com/ceres-solver.git改为
https://github.com/ceres-solver/ceres-solver.git,版本改为1.12.0
#Install deb dependencies.
#The command ‘sudo rosdep init’ will print an error if you have already
#executed it since installing ROS. This error can be ignored.
src/cartographer/scripts/install_proto3.sh
sudo rosdep init
rosdep update
rosdep install --from-paths src --ignore-src --rosdistro=${ROS_DISTRO} -y
#Build and install.
catkin_make_isolated --install --use-ninja
source install_isolated/setup.bash
(如果你用的是zsh不是bash上面的那句改为下面这样)
source install_isolated/setup.zsh
编译会有几分钟完全可以去喝杯茶
这里部分代码需要修改具体如下:
在/home/zhaoekexeu/catkin_google_ws/src/cartographer_ros/cartographer_ros/launch下修改 demo_revo_lds.launch
2.1 删除运行bag的node节点,如下
2.2 修改frame 这个很重要
在/home/zhaoekexeu/catkin_google_ws/src/cartographer_ros/cartographer_ros/configuration_files下修改 revo_lds.lua
主要修改:
map_frame = “map”,
tracking_frame = “horizontal_laser_link”,
published_frame = “horizontal_laser_link”,
odom_frame = “odom”,
这里需要查询一下自己的tf_tree
rosrun rqt_tf_tree rqt_tf_tree
显示如下图片(我这里是运行正确之后的frames ,你可以根据错误的信息进行更改上面四行代码)
修改后的代码
include "map_builder.lua"
include "trajectory_builder.lua"
options = {
map_builder = MAP_BUILDER,
trajectory_builder = TRAJECTORY_BUILDER,
map_frame = "map",
tracking_frame = "base_footprint",
published_frame = "base_footprint",
odom_frame = "odom",
provide_odom_frame = true,
publish_frame_projected_to_2d = false,
use_odometry = false,
use_nav_sat = false,
use_landmarks = false,
num_laser_scans = 1,
num_multi_echo_laser_scans = 0,
num_subdivisions_per_laser_scan = 1,
num_point_clouds = 0,
lookup_transform_timeout_sec = 0.2,
submap_publish_period_sec = 0.3,
pose_publish_period_sec = 5e-3,
trajectory_publish_period_sec = 30e-3,
rangefinder_sampling_ratio = 1.,
odometry_sampling_ratio = 1.,
fixed_frame_pose_sampling_ratio = 1.,
imu_sampling_ratio = 1.,
landmarks_sampling_ratio = 1.,
}
MAP_BUILDER.use_trajectory_builder_2d = true
TRAJECTORY_BUILDER_2D.submaps.num_range_data = 35
TRAJECTORY_BUILDER_2D.min_range = 0.3
TRAJECTORY_BUILDER_2D.max_range = 8.
TRAJECTORY_BUILDER_2D.missing_data_ray_length = 1.
TRAJECTORY_BUILDER_2D.use_imu_data = false
TRAJECTORY_BUILDER_2D.use_online_correlative_scan_matching = true
TRAJECTORY_BUILDER_2D.real_time_correlative_scan_matcher.linear_search_window = 0.1
TRAJECTORY_BUILDER_2D.real_time_correlative_scan_matcher.translation_delta_cost_weight = 10.
TRAJECTORY_BUILDER_2D.real_time_correlative_scan_matcher.rotation_delta_cost_weight = 1e-1
POSE_GRAPH.optimization_problem.huber_scale = 1e2
POSE_GRAPH.optimize_every_n_nodes = 35
POSE_GRAPH.constraint_builder.min_score = 0.65
return options
这样就修改OK了。
开始准备测试
(这里是按照我自己做的机器人进行描述的,可供参考)
roscore
ssh [email protected]
sudo chmod 666 /dev/ttyUSB0
sudo chmod 777 /dev/ttyACM0
roslaunch mbot_bringup mbot_with_laser.launch
cd catkin_google_ws
catkin_make_isolated --install --use-ninja //编译
source install_isolated/setup.zsh //刷新环境 在此之前也可以重新编译一下
roslaunch cartographer_ros demo_revo_lds.launch //执行demo_revo_lds.launch
roslaunch mbot_bringup mbot_with_laser.launch //这样就可以控制机器人进行建图了。
方式1
1.当使用cartographer在线建图时,cartographer不知道什么时候结束,所以要先调用服务来关闭传感器数据的接收。
rosservice call /finish_trajectory 0
2.将当前地图信息的快照保存为后缀名为.pbstream。
# Ask Cartographer to serialize its current state.
# (press tab to quickly expand the parameter syntax)
rosservice call /write_state "{filename: '<绝对路径存放地图信息>/<地图保存的名字>.pbstream', include_unfinished_submaps: 'true'}"
3.通过cartographer提供的ros包,将后缀名为.pbstream的地图信息,转化成ros地图信息。
rosrun cartographer_ros cartographer_pbstream_to_ros_map -map_filestem=<保存地图的绝对路径>/<地图命名> -pbstream_filename=<存放pbstream地图快照信息的绝对地址>/.pbstream -resolution=0.05
warning:上面的方法是网上各大博主的方法,我测试没有通过,会报下面的错误,暂时没有解决,如果您解决了,希望给我分享一下
方式2
这里需要修改map_server
如果想将GMapping中生成的地图保存下来,可以
rosrun map_server map_saver -f ~/robot_ws/map/gmapping
main函数中定义了一个MapGenerator类对象,该类初始化之后订阅map话题。在订阅的回调函数中,将订阅得到的map按照一定格式保存到当地的.pgm文件,同时将地图信息保存到对应的.yaml文件中。
需要注意的是,如果你是想保存Cartographer生成的地图,是需要对源代码做一些修改的。因为Cartographer的map格式跟GMapping中的格式不太一样。具体不一样如下:
我按照这个不同对map_server包做了一些修改,详情看github:https://github.com/HaoQChen/map_server 。如果觉得有用,记得star一下哦。
完成修改之后
可以使用如下格式的代码进行地图保存(根据自己的情况进行修改)
rosrun map_server map_saver map:=/ -f
局部参考:
https://blog.csdn.net/weixin_40712763/article/details/82292087
https://blog.csdn.net/m0_37672916/article/details/77198261
https://blog.csdn.net/u014493244/article/details/84101374
https://blog.csdn.net/SimileciWH/article/details/84846416
https://blog.csdn.net/u013834525/article/details/84622496