Cartographer_ros(1)
入口node_main.cc
node_main.cc主要实现参数文件读取和程序启动功能。
gflags
首先,使用谷歌的gflags命令行参数解析工具,实现将命令行输入参数读取到程序内部的功能。
DEFINE_bool(collect_metrics, false,
"Activates the collection of runtime metrics. If activated, the "
"metrics can be accessed via a ROS service.");
DEFINE_string(configuration_directory, "",
"First directory in which configuration files are searched, "
"second is always the Cartographer installation to allow "
"including files from there.");
DEFINE_string(configuration_basename, "",
"Basename, i.e. not containing any directory prefix, of the "
"configuration file.");
DEFINE_string(load_state_filename, "",
"If non-empty, filename of a .pbstream file to load, containing "
"a saved SLAM state.");
fgrep -l -f ./test ccc
-l
是不带参数的标记,-f
是带了./test
参数的标记
定义参数通过DEFINE_type
宏实现, 该宏的三个参数含义分别为命令行参数名, 参数默认值, 以及参数的帮助信息。
例如定义collect_metrics
DEFINE_bool(collect_metrics, false,
"Activates the collection of runtime metrics. If activated, the "
"metrics can be accessed via a ROS service.");
使用以FLAGS_name
的方式调用
Node node(node_options, std::move(map_builder), &tf_buffer,
FLAGS_collect_metrics);
glog
glog是谷歌开发的一个应用级别的日志系统库。glog里提供的CHECK系列的宏, 检测某个表达式是否为真。
Cartographer中大量使用CHECK函数。
main函数
glog库的初始化
google::InitGoogleLogging(argv[0]);
使用gflags进行参数的初始化. 其中第三个参数为remove_flag
如果为true, gflags会移除parse过的参数, 否则gflags就会保留这些参数, 但可能会对参数顺序进行调整.
google::ParseCommandLineFlags(&argc, &argv, true);
run函数
// 开启监听tf的独立线程
tf2_ros::TransformListener tf(tf_buffer);
NodeOptions node_options;
TrajectoryOptions trajectory_options;
// c++11: std::tie()函数可以将变量连接到一个给定的tuple上,生成一个元素类型全是引用的tuple
// 根据Lua配置文件中的内容, 为node_options, trajectory_options 赋值
std::tie(node_options, trajectory_options) =
LoadOptions(FLAGS_configuration_directory, FLAGS_configuration_basename);
// MapBuilder类是完整的SLAM算法类
// 包含前端(TrajectoryBuilders,scan to submap) 与 后端(用于查找回环的PoseGraph)
auto map_builder =
cartographer::mapping::CreateMapBuilder(node_options.map_builder_options);
map_builder
是完整的SLAM算法类,包含前端(trajectorybuilders, scan to submap matching)与后端(posegraph)
// Node类的初始化, 将ROS的topic传入SLAM, 也就是MapBuilder
Node node(node_options, std::move(map_builder), &tf_buffer,
FLAGS_collect_metrics);