2018-02-01 02:22:08 +08:00
|
|
|
/*
|
|
|
|
* Copyright 2018 The Cartographer Authors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2018-03-02 21:16:49 +08:00
|
|
|
#include "cartographer_grpc/internal/map_builder_context.h"
|
|
|
|
|
|
|
|
#include "cartographer_grpc/internal/map_builder_server.h"
|
2018-02-01 02:22:08 +08:00
|
|
|
|
2018-03-03 06:21:28 +08:00
|
|
|
namespace cartographer {
|
|
|
|
namespace cloud {
|
2018-02-01 02:22:08 +08:00
|
|
|
|
|
|
|
MapBuilderContext::MapBuilderContext(MapBuilderServer* map_builder_server)
|
|
|
|
: map_builder_server_(map_builder_server) {}
|
|
|
|
|
2018-03-03 06:21:28 +08:00
|
|
|
mapping::MapBuilderInterface& MapBuilderContext::map_builder() {
|
2018-02-01 02:22:08 +08:00
|
|
|
return *map_builder_server_->map_builder_;
|
|
|
|
}
|
|
|
|
|
2018-03-03 06:21:28 +08:00
|
|
|
common::BlockingQueue<std::unique_ptr<MapBuilderContextInterface::Data>>&
|
2018-02-01 02:22:08 +08:00
|
|
|
MapBuilderContext::sensor_data_queue() {
|
|
|
|
return map_builder_server_->incoming_data_queue_;
|
|
|
|
}
|
|
|
|
|
2018-03-03 06:21:28 +08:00
|
|
|
mapping::TrajectoryBuilderInterface::LocalSlamResultCallback
|
2018-02-01 02:22:08 +08:00
|
|
|
MapBuilderContext::GetLocalSlamResultCallbackForSubscriptions() {
|
|
|
|
MapBuilderServer* map_builder_server = map_builder_server_;
|
|
|
|
return [map_builder_server](
|
2018-03-03 06:21:28 +08:00
|
|
|
int trajectory_id, common::Time time,
|
|
|
|
transform::Rigid3d local_pose, sensor::RangeData range_data,
|
|
|
|
std::unique_ptr<
|
|
|
|
const mapping::TrajectoryBuilderInterface::InsertionResult>
|
2018-02-01 02:22:08 +08:00
|
|
|
insertion_result) {
|
|
|
|
map_builder_server->OnLocalSlamResult(trajectory_id, time, local_pose,
|
|
|
|
std::move(range_data),
|
|
|
|
std::move(insertion_result));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapBuilderContext::AddSensorDataToTrajectory(const Data& sensor_data) {
|
|
|
|
sensor_data.data->AddToTrajectoryBuilder(
|
|
|
|
map_builder_server_->map_builder_->GetTrajectoryBuilder(
|
|
|
|
sensor_data.trajectory_id));
|
|
|
|
}
|
|
|
|
|
|
|
|
MapBuilderContextInterface::SubscriptionId
|
|
|
|
MapBuilderContext::SubscribeLocalSlamResults(
|
|
|
|
int trajectory_id, LocalSlamSubscriptionCallback callback) {
|
|
|
|
return map_builder_server_->SubscribeLocalSlamResults(trajectory_id,
|
|
|
|
callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapBuilderContext::UnsubscribeLocalSlamResults(
|
|
|
|
const SubscriptionId& subscription_id) {
|
|
|
|
map_builder_server_->UnsubscribeLocalSlamResults(subscription_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapBuilderContext::NotifyFinishTrajectory(int trajectory_id) {
|
|
|
|
map_builder_server_->NotifyFinishTrajectory(trajectory_id);
|
|
|
|
}
|
|
|
|
|
2018-03-03 06:21:28 +08:00
|
|
|
std::shared_ptr<mapping::Submap2D> MapBuilderContext::UpdateSubmap2D(
|
|
|
|
const mapping::proto::Submap& proto) {
|
2018-02-01 02:22:08 +08:00
|
|
|
CHECK(proto.has_submap_2d());
|
2018-03-03 06:21:28 +08:00
|
|
|
mapping::SubmapId submap_id{proto.submap_id().trajectory_id(),
|
|
|
|
proto.submap_id().submap_index()};
|
|
|
|
std::shared_ptr<mapping::Submap2D> submap_2d_ptr;
|
2018-02-01 02:22:08 +08:00
|
|
|
auto submap_it = unfinished_submaps_.find(submap_id);
|
|
|
|
if (submap_it == unfinished_submaps_.end()) {
|
|
|
|
// Seeing a submap for the first time it should never be finished.
|
|
|
|
CHECK(!proto.submap_2d().finished());
|
2018-03-03 06:21:28 +08:00
|
|
|
submap_2d_ptr = std::make_shared<mapping::Submap2D>(proto.submap_2d());
|
2018-02-01 02:22:08 +08:00
|
|
|
unfinished_submaps_.Insert(submap_id, submap_2d_ptr);
|
|
|
|
} else {
|
2018-03-03 06:21:28 +08:00
|
|
|
submap_2d_ptr =
|
|
|
|
std::dynamic_pointer_cast<mapping::Submap2D>(submap_it->data);
|
2018-02-01 02:22:08 +08:00
|
|
|
CHECK(submap_2d_ptr);
|
|
|
|
submap_2d_ptr->UpdateFromProto(proto);
|
|
|
|
|
|
|
|
// If the submap was just finished by the recent update, remove it from the
|
|
|
|
// list of unfinished submaps.
|
|
|
|
if (submap_2d_ptr->finished()) {
|
|
|
|
unfinished_submaps_.Trim(submap_id);
|
|
|
|
} else {
|
|
|
|
// If the submap is unfinished set the 'num_range_data' to 0 since we
|
|
|
|
// haven't changed the HybridGrid.
|
|
|
|
submap_2d_ptr->SetNumRangeData(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return submap_2d_ptr;
|
|
|
|
}
|
|
|
|
|
2018-03-03 06:21:28 +08:00
|
|
|
std::shared_ptr<mapping::Submap3D> MapBuilderContext::UpdateSubmap3D(
|
|
|
|
const mapping::proto::Submap& proto) {
|
2018-02-01 02:22:08 +08:00
|
|
|
CHECK(proto.has_submap_3d());
|
2018-03-03 06:21:28 +08:00
|
|
|
mapping::SubmapId submap_id{proto.submap_id().trajectory_id(),
|
|
|
|
proto.submap_id().submap_index()};
|
|
|
|
std::shared_ptr<mapping::Submap3D> submap_3d_ptr;
|
2018-02-01 02:22:08 +08:00
|
|
|
auto submap_it = unfinished_submaps_.find(submap_id);
|
|
|
|
if (submap_it == unfinished_submaps_.end()) {
|
|
|
|
// Seeing a submap for the first time it should never be finished.
|
|
|
|
CHECK(!proto.submap_3d().finished());
|
2018-03-03 06:21:28 +08:00
|
|
|
submap_3d_ptr = std::make_shared<mapping::Submap3D>(proto.submap_3d());
|
2018-02-01 02:22:08 +08:00
|
|
|
unfinished_submaps_.Insert(submap_id, submap_3d_ptr);
|
|
|
|
submap_it = unfinished_submaps_.find(submap_id);
|
|
|
|
} else {
|
2018-03-03 06:21:28 +08:00
|
|
|
submap_3d_ptr =
|
|
|
|
std::dynamic_pointer_cast<mapping::Submap3D>(submap_it->data);
|
2018-02-01 02:22:08 +08:00
|
|
|
CHECK(submap_3d_ptr);
|
|
|
|
|
|
|
|
// Update submap with information in incoming request.
|
|
|
|
submap_3d_ptr->UpdateFromProto(proto);
|
|
|
|
|
|
|
|
// If the submap was just finished by the recent update, remove it from the
|
|
|
|
// list of unfinished submaps.
|
|
|
|
if (submap_3d_ptr->finished()) {
|
|
|
|
unfinished_submaps_.Trim(submap_id);
|
|
|
|
} else {
|
|
|
|
// If the submap is unfinished set the 'num_range_data' to 0 since we
|
|
|
|
// haven't changed the HybridGrid.
|
|
|
|
submap_3d_ptr->SetNumRangeData(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return submap_3d_ptr;
|
|
|
|
}
|
|
|
|
|
2018-03-03 06:21:28 +08:00
|
|
|
std::unique_ptr<mapping::LocalSlamResultData>
|
2018-02-01 02:22:08 +08:00
|
|
|
MapBuilderContext::ProcessLocalSlamResultData(
|
2018-03-03 06:21:28 +08:00
|
|
|
const std::string& sensor_id, common::Time time,
|
|
|
|
const mapping::proto::LocalSlamResultData& proto) {
|
2018-02-01 02:22:08 +08:00
|
|
|
CHECK_GE(proto.submaps().size(), 1);
|
|
|
|
CHECK(proto.submaps(0).has_submap_2d() || proto.submaps(0).has_submap_3d());
|
|
|
|
if (proto.submaps(0).has_submap_2d()) {
|
2018-03-03 06:21:28 +08:00
|
|
|
std::vector<std::shared_ptr<const mapping::Submap2D>> submaps;
|
2018-02-01 02:22:08 +08:00
|
|
|
for (const auto& submap_proto : proto.submaps()) {
|
|
|
|
submaps.push_back(UpdateSubmap2D(submap_proto));
|
|
|
|
}
|
2018-03-03 06:21:28 +08:00
|
|
|
return common::make_unique<mapping::LocalSlamResult2D>(
|
2018-02-01 02:22:08 +08:00
|
|
|
sensor_id, time,
|
2018-03-03 06:21:28 +08:00
|
|
|
std::make_shared<const mapping::TrajectoryNode::Data>(
|
|
|
|
mapping::FromProto(proto.node_data())),
|
2018-02-01 02:22:08 +08:00
|
|
|
submaps);
|
|
|
|
} else {
|
2018-03-03 06:21:28 +08:00
|
|
|
std::vector<std::shared_ptr<const mapping::Submap3D>> submaps;
|
2018-02-01 02:22:08 +08:00
|
|
|
for (const auto& submap_proto : proto.submaps()) {
|
|
|
|
submaps.push_back(UpdateSubmap3D(submap_proto));
|
|
|
|
}
|
2018-03-03 06:21:28 +08:00
|
|
|
return common::make_unique<mapping::LocalSlamResult3D>(
|
2018-02-01 02:22:08 +08:00
|
|
|
sensor_id, time,
|
2018-03-03 06:21:28 +08:00
|
|
|
std::make_shared<const mapping::TrajectoryNode::Data>(
|
|
|
|
mapping::FromProto(proto.node_data())),
|
2018-02-01 02:22:08 +08:00
|
|
|
std::move(submaps));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-01 20:05:08 +08:00
|
|
|
LocalTrajectoryUploaderInterface*
|
|
|
|
MapBuilderContext::local_trajectory_uploader() {
|
2018-02-01 02:22:08 +08:00
|
|
|
return map_builder_server_->local_trajectory_uploader_.get();
|
|
|
|
}
|
|
|
|
|
2018-03-03 06:21:28 +08:00
|
|
|
void MapBuilderContext::EnqueueSensorData(int trajectory_id,
|
|
|
|
std::unique_ptr<sensor::Data> data) {
|
2018-02-01 02:22:08 +08:00
|
|
|
map_builder_server_->incoming_data_queue_.Push(
|
2018-03-03 06:21:28 +08:00
|
|
|
common::make_unique<Data>(Data{trajectory_id, std::move(data)}));
|
2018-02-01 02:22:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapBuilderContext::EnqueueLocalSlamResultData(
|
|
|
|
int trajectory_id, const std::string& sensor_id,
|
2018-03-03 06:21:28 +08:00
|
|
|
std::unique_ptr<mapping::LocalSlamResultData> local_slam_result_data) {
|
|
|
|
map_builder_server_->incoming_data_queue_.Push(common::make_unique<Data>(
|
|
|
|
Data{trajectory_id, std::move(local_slam_result_data)}));
|
2018-02-01 02:22:08 +08:00
|
|
|
}
|
|
|
|
|
2018-03-03 06:21:28 +08:00
|
|
|
} // namespace cloud
|
|
|
|
} // namespace cartographer
|