描述
Run()函数中的记载配置参数的代码解析
node_main.cc中的Run()函数中有这样一段核心代码
NodeOptions node_options;
TrajectoryOptions trajectory_options;
std::tie(node_options, trajectory_options) =
LoadOptions(FLAGS_configuration_directory, FLAGS_configuration_basename);
auto map_builder =
cartographer::mapping::CreateMapBuilder(node_options.map_builder_options);
Node node(node_options, std::move(map_builder), &tf_buffer,
FLAGS_collect_metrics);
if (!FLAGS_load_state_filename.empty()) {
node.LoadState(FLAGS_load_state_filename, FLAGS_load_frozen_state);
}
if (FLAGS_start_trajectory_with_default_topics) {
node.StartTrajectoryWithDefaultTopics(trajectory_options);
}
这段代码中的以下语句是什么含义呢,加载配置文件
std::tie(node_options, trajectory_options) =
LoadOptions(FLAGS_configuration_directory, FLAGS_configuration_basename);
加载配置文件
node_main.cc中的Run()函数中
NodeOptions node_options;
TrajectoryOptions trajectory_options;
std::tie(node_options, trajectory_options) =
LoadOptions(FLAGS_configuration_directory, FLAGS_configuration_basename);
LoadOptions()函数声明在cartographer_ros/node_option.h
中,在node_option.cc
的源文件里写着实现内容
std::tuple<NodeOptions, TrajectoryOptions> LoadOptions(
const std::string& configuration_directory,
const std::string& configuration_basename) {
auto file_resolver =
absl::make_unique<cartographer::common::ConfigurationFileResolver>(
std::vector<std::string>{configuration_directory});
const std::string code =
file_resolver->GetFileContentOrDie(configuration_basename);
cartographer::common::LuaParameterDictionary lua_parameter_dictionary(
code, std::move(file_resolver));
return std::make_tuple(CreateNodeOptions(&lua_parameter_dictionary),
CreateTrajectoryOptions(&lua_parameter_dictionary));
}
函数中的
configuration_directory
是字符串$(find cartographer_ros)/configuration_files
configuration_basename
是字符串backpack_2d.lua
显然,他们是我们建图时的参数文件,该函数返回的NodeOptions
和TrajectoryOptions
其实就是两个struct,分别声明在node_options.h
和trajectory_options.h
中
- 读取NodeOptions
定义在cartographer_ros/node_options.cc
中
CreateNodeOptions(&lua_parameter_dictionary)
读取的参数包括
map_builder = MAP_BUILDER,
map_frame = "map",
lookup_transform_timeout_sec = 0.2,
submap_publish_period_sec = 0.3,
pose_publish_period_sec = 5e-3,
trajectory_publish_period_sec = 30e-3,
use_pose_extrapolator = true
还有两个参数是backpack_2d.lua文件中没有的,publish_to_tf
和publish_tracked_pose
- 读取TrajectoryOptions
定义在cartographer_ros/tracjectory_options.cc
中
CreateTrajectoryOptions(&lua_parameter_dictionary)
读取的参数包括
trajectory_builder = TRAJECTORY_BUILDER,
tracking_frame = "base_link",
published_frame = "base_link",
odom_frame = "odom",
provide_odom_frame = true,
use_odometry = false,
use_nav_sat = false,
use_landmarks = false,
publish_frame_projected_to_2d = false,
num_laser_scans = 0,
num_multi_echo_laser_scans = 1,
num_subdivisions_per_laser_scan = 10,
num_point_clouds = 0,
rangefinder_sampling_ratio = 1.,
odometry_sampling_ratio = 1.,
fixed_frame_pose_sampling_ratio = 1.,
imu_sampling_ratio = 1.,
landmarks_sampling_ratio = 1.,
两者加一起一共25项参数,backpack_2d.lua也只有25项参数,也就是说, CreateNodeOptions和CreateTrajectoryOptions刚好读取了参数文件中的全部配置项