MapBuilder的成员变量sensor::Collator sensor_collator_;
再次阅读MapBuilder::AddTrajectoryBuilder方法。首先构造了mapping::GlobalTrajectoryBuilder单例,接着作为参数构造了CollatedTrajectoryBuilder单例。
trajectory_builders_.push_back( common::make_unique<CollatedTrajectoryBuilder>( &sensor_collator_, trajectory_id, expected_sensor_ids, common::make_unique<mapping::GlobalTrajectoryBuilder<mapping_2d::LocalTrajectoryBuilder,mapping_2d::proto::LocalTrajectoryBuilderOptions,mapping_2d::PoseGraph>> (trajectory_options.trajectory_builder_2d_options(),trajectory_id, pose_graph_2d_.get(),local_slam_result_callback) ) );
这里sensor_collator_作为参数传入,参与CollatedTrajectoryBuilder构造。
查看构造函数
CollatedTrajectoryBuilder::CollatedTrajectoryBuilder(sensor::Collator* const sensor_collator, const int trajectory_id, const std::unordered_set<std::string>& expected_sensor_ids, std::unique_ptr<TrajectoryBuilderInterface> wrapped_trajectory_builder)
: sensor_collator_(sensor_collator)
, trajectory_id_(trajectory_id)
, wrapped_trajectory_builder_(std::move(wrapped_trajectory_builder))
, last_logging_time_(std::chrono::steady_clock::now())
{
sensor_collator_->AddTrajectory(trajectory_id, expected_sensor_ids,
[this](const std::string& sensor_id, std::unique_ptr<sensor::Data> data)
{
HandleCollatedSensorData(sensor_id, std::move(data));
}
);
}
这里是回调函数。
[this](const std::string& sensor_id, std::unique_ptr<sensor::Data> data) { HandleCollatedSensorData(sensor_id, std::move(data)); }
查看sensor::Collator的AddTrajectory方法:
void Collator::AddTrajectory( const int trajectory_id, const std::unordered_set<std::string>& expected_sensor_ids, const Callback& callback) { for (const auto& sensor_id : expected_sensor_ids) { const auto queue_key = QueueKey{trajectory_id, sensor_id}; queue_.AddQueue(queue_key, [callback, sensor_id](std::unique_ptr<Data> data) { callback(sensor_id, std::move(data)); }); queue_keys_[trajectory_id].push_back(queue_key); } }
for (const auto& sensor_id : expected_sensor_ids)用到了C++11的auto新特性。