上一篇是由于node_main.cc中调用了node类的构造函数,所以展开阅读了下node.h和node.cc文件,从而大致了解node类内各种函数的主要功能。本篇将回到node_main.cc文件,继续读完剩余的代码。从node类的构造函数往下阅读,并了解如何具体应用node类的成员函数。
1.LoadState函数
if (!FLAGS_load_state_filename.empty()) {
//加载数据包数据;
node.LoadState(FLAGS_load_state_filename, FLAGS_load_frozen_state);
}
在node_main.cc函数的前面中,若是load_state_filename设置为非空,则调用node类的LoadState函数加载数据包。如下是具体实现:
void Node::LoadState(const std::string& state_filename,
const bool load_frozen_state) {
absl::MutexLock lock(&mutex_);// 互斥锁
map_builder_bridge_.LoadState(state_filename, load_frozen_state);
}
可以看到调用了MapBuilderBridge类的LoadState函数,跟着深入到MapBuilderBridge类,在map_builder_bridge.h中可以查看到其声明为:
void LoadState(const std::string& state_filename, bool load_frozen_state);
参数一为加载数据包名称,参数二为是否加载已保存的非优化路径状态。
接下来转到map_builder_bridge.cc文件中查看具体实现:
void MapBuilderBridge::LoadState(const std::string& state_filename,
bool load_frozen_state) {
// Check if suffix of the state file is ".pbstream".
const std::string suffix = ".pbstream";
CHECK_EQ(state_filename.substr(
std::max<int>(state_filename.size() - suffix.size(), 0)),
suffix)
<< "The file containing the state to be loaded must be a "
".pbstream file.";
LOG(INFO) << "Loading saved state '" << state_filename << "'...";
cartographer::io::ProtoStreamReader stream(state_filename);
map_builder_->LoadState(&stream, load_frozen_state);
}
从这里我们可以发现,MapBuilderBridge类的LoadState函数调用了 MapBuilder的成员函数 LoadState 来加载一个pbstream 文件,所以具体细节问题留到后续更新的cartographer底层代码阅读时讲解。map_builder_是接口MapBuilderInterface 的实例化对象,而根据是 2d 还是 3d 情况,其具体实现会略有不同,至于如何具体化为2D或者3D对象,在后面我也会慢慢细说。
2.StartTrajectoryWithDefaultTopics函数
if (FLAGS_start_trajectory_with_default_topics) {
//以默认话题开始规划路径;
node.StartTrajectoryWithDefaultTopics(trajectory_options);
}
同理,在node_main.cc函数的前面也是定义了start_trajectory_with_default_topics,默认情况下为true,cartographer的ROS上层也是从这里开始以默认话题进行轨迹的更新,这里稍带提一句,默认的话题名称设置可以到node_constants

本文深入解析了Cartographer的node_main.cc文件,详细介绍了Node类的LoadState、StartTrajectoryWithDefaultTopics和FinishAllTrajectories函数。LoadState通过MapBuilderBridge加载pbstream文件;StartTrajectoryWithDefaultTopics启动轨迹并根据配置信息添加传感器;FinishAllTrajectories则完成所有活动轨迹,关闭相关订阅。MapBuilderBridge作为桥梁,连接上层与底层算法,起到了关键作用。
最低0.47元/天 解锁文章
1033

被折叠的 条评论
为什么被折叠?



