2017-12-05 21:46:25 +08:00
|
|
|
/*
|
|
|
|
* Copyright 2017 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.
|
|
|
|
*/
|
2017-12-13 05:36:44 +08:00
|
|
|
|
2018-03-02 21:16:49 +08:00
|
|
|
#include "cartographer_grpc/internal/map_builder_server.h"
|
2017-12-05 21:46:25 +08:00
|
|
|
|
2018-03-02 18:38:35 +08:00
|
|
|
#include "cartographer_grpc/internal/handlers/add_fixed_frame_pose_data_handler.h"
|
|
|
|
#include "cartographer_grpc/internal/handlers/add_imu_data_handler.h"
|
|
|
|
#include "cartographer_grpc/internal/handlers/add_landmark_data_handler.h"
|
|
|
|
#include "cartographer_grpc/internal/handlers/add_local_slam_result_data_handler.h"
|
|
|
|
#include "cartographer_grpc/internal/handlers/add_odometry_data_handler.h"
|
|
|
|
#include "cartographer_grpc/internal/handlers/add_rangefinder_data_handler.h"
|
|
|
|
#include "cartographer_grpc/internal/handlers/add_trajectory_handler.h"
|
|
|
|
#include "cartographer_grpc/internal/handlers/finish_trajectory_handler.h"
|
|
|
|
#include "cartographer_grpc/internal/handlers/get_all_submap_poses.h"
|
|
|
|
#include "cartographer_grpc/internal/handlers/get_constraints_handler.h"
|
|
|
|
#include "cartographer_grpc/internal/handlers/get_landmark_poses_handler.h"
|
|
|
|
#include "cartographer_grpc/internal/handlers/get_local_to_global_transform_handler.h"
|
|
|
|
#include "cartographer_grpc/internal/handlers/get_submap_handler.h"
|
|
|
|
#include "cartographer_grpc/internal/handlers/get_trajectory_node_poses_handler.h"
|
|
|
|
#include "cartographer_grpc/internal/handlers/load_state_handler.h"
|
|
|
|
#include "cartographer_grpc/internal/handlers/receive_local_slam_results_handler.h"
|
|
|
|
#include "cartographer_grpc/internal/handlers/run_final_optimization_handler.h"
|
|
|
|
#include "cartographer_grpc/internal/handlers/write_state_handler.h"
|
2018-03-02 21:16:49 +08:00
|
|
|
#include "cartographer_grpc/internal/sensor/serialization.h"
|
2017-12-05 21:46:25 +08:00
|
|
|
#include "glog/logging.h"
|
|
|
|
|
2018-03-03 06:21:28 +08:00
|
|
|
namespace cartographer {
|
|
|
|
namespace cloud {
|
2017-12-18 17:02:57 +08:00
|
|
|
namespace {
|
|
|
|
|
2018-03-03 06:21:28 +08:00
|
|
|
const common::Duration kPopTimeout = common::FromMilliseconds(100);
|
2017-12-18 17:02:57 +08:00
|
|
|
|
|
|
|
} // namespace
|
2017-12-05 21:46:25 +08:00
|
|
|
|
|
|
|
MapBuilderServer::MapBuilderServer(
|
2017-12-19 03:47:00 +08:00
|
|
|
const proto::MapBuilderServerOptions& map_builder_server_options,
|
2018-03-03 06:21:28 +08:00
|
|
|
std::unique_ptr<mapping::MapBuilderInterface> map_builder)
|
2017-12-19 03:47:00 +08:00
|
|
|
: map_builder_(std::move(map_builder)) {
|
2017-12-05 21:46:25 +08:00
|
|
|
framework::Server::Builder server_builder;
|
|
|
|
server_builder.SetServerAddress(map_builder_server_options.server_address());
|
2017-12-15 19:21:44 +08:00
|
|
|
server_builder.SetNumGrpcThreads(
|
2017-12-05 21:46:25 +08:00
|
|
|
map_builder_server_options.num_grpc_threads());
|
2017-12-15 19:21:44 +08:00
|
|
|
server_builder.SetNumEventThreads(
|
|
|
|
map_builder_server_options.num_event_threads());
|
2018-01-13 06:16:29 +08:00
|
|
|
if (!map_builder_server_options.uplink_server_address().empty()) {
|
2018-03-02 18:38:35 +08:00
|
|
|
local_trajectory_uploader_ = CreateLocalTrajectoryUploader(
|
|
|
|
map_builder_server_options.uplink_server_address());
|
2018-01-13 06:16:29 +08:00
|
|
|
}
|
2018-01-31 19:26:56 +08:00
|
|
|
server_builder.RegisterHandler<handlers::AddTrajectoryHandler>();
|
|
|
|
server_builder.RegisterHandler<handlers::AddOdometryDataHandler>();
|
|
|
|
server_builder.RegisterHandler<handlers::AddImuDataHandler>();
|
|
|
|
server_builder.RegisterHandler<handlers::AddRangefinderDataHandler>();
|
|
|
|
server_builder.RegisterHandler<handlers::AddFixedFramePoseDataHandler>();
|
|
|
|
server_builder.RegisterHandler<handlers::AddLandmarkDataHandler>();
|
|
|
|
server_builder.RegisterHandler<handlers::AddLocalSlamResultDataHandler>();
|
|
|
|
server_builder.RegisterHandler<handlers::FinishTrajectoryHandler>();
|
|
|
|
server_builder.RegisterHandler<handlers::ReceiveLocalSlamResultsHandler>();
|
|
|
|
server_builder.RegisterHandler<handlers::GetSubmapHandler>();
|
|
|
|
server_builder.RegisterHandler<handlers::GetTrajectoryNodePosesHandler>();
|
2018-02-07 01:13:31 +08:00
|
|
|
server_builder.RegisterHandler<handlers::GetLandmarkPosesHandler>();
|
2018-01-31 19:26:56 +08:00
|
|
|
server_builder.RegisterHandler<handlers::GetAllSubmapPosesHandler>();
|
|
|
|
server_builder.RegisterHandler<handlers::GetLocalToGlobalTransformHandler>();
|
|
|
|
server_builder.RegisterHandler<handlers::GetConstraintsHandler>();
|
2018-02-28 03:38:57 +08:00
|
|
|
server_builder.RegisterHandler<handlers::LoadStateHandler>();
|
2018-01-31 19:26:56 +08:00
|
|
|
server_builder.RegisterHandler<handlers::RunFinalOptimizationHandler>();
|
2018-02-28 03:38:57 +08:00
|
|
|
server_builder.RegisterHandler<handlers::WriteStateHandler>();
|
2017-12-05 21:46:25 +08:00
|
|
|
grpc_server_ = server_builder.Build();
|
|
|
|
grpc_server_->SetExecutionContext(
|
2018-03-03 06:21:28 +08:00
|
|
|
common::make_unique<MapBuilderContext>(this));
|
2017-12-05 21:46:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapBuilderServer::Start() {
|
|
|
|
shutting_down_ = false;
|
2018-01-17 19:01:29 +08:00
|
|
|
if (local_trajectory_uploader_) {
|
|
|
|
local_trajectory_uploader_->Start();
|
|
|
|
}
|
2017-12-05 21:46:25 +08:00
|
|
|
StartSlamThread();
|
|
|
|
grpc_server_->Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapBuilderServer::WaitForShutdown() {
|
|
|
|
grpc_server_->WaitForShutdown();
|
|
|
|
if (slam_thread_) {
|
|
|
|
slam_thread_->join();
|
|
|
|
}
|
2018-01-17 19:01:29 +08:00
|
|
|
if (local_trajectory_uploader_) {
|
|
|
|
local_trajectory_uploader_->Shutdown();
|
|
|
|
}
|
2017-12-05 21:46:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapBuilderServer::Shutdown() {
|
|
|
|
shutting_down_ = true;
|
|
|
|
grpc_server_->Shutdown();
|
|
|
|
if (slam_thread_) {
|
|
|
|
slam_thread_->join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapBuilderServer::ProcessSensorDataQueue() {
|
2017-12-13 05:36:44 +08:00
|
|
|
LOG(INFO) << "Starting SLAM thread.";
|
2017-12-05 21:46:25 +08:00
|
|
|
while (!shutting_down_) {
|
2018-02-01 02:22:08 +08:00
|
|
|
std::unique_ptr<MapBuilderContextInterface::Data> sensor_data =
|
2018-01-12 20:00:25 +08:00
|
|
|
incoming_data_queue_.PopWithTimeout(kPopTimeout);
|
2017-12-18 17:02:57 +08:00
|
|
|
if (sensor_data) {
|
|
|
|
grpc_server_->GetContext<MapBuilderContext>()->AddSensorDataToTrajectory(
|
|
|
|
*sensor_data);
|
|
|
|
}
|
2017-12-05 21:46:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapBuilderServer::StartSlamThread() {
|
|
|
|
CHECK(!slam_thread_);
|
|
|
|
|
|
|
|
// Start the SLAM processing thread.
|
2018-03-03 06:21:28 +08:00
|
|
|
slam_thread_ = common::make_unique<std::thread>(
|
2017-12-05 21:46:25 +08:00
|
|
|
[this]() { this->ProcessSensorDataQueue(); });
|
|
|
|
}
|
|
|
|
|
2017-12-18 23:32:34 +08:00
|
|
|
void MapBuilderServer::OnLocalSlamResult(
|
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-01-11 00:26:04 +08:00
|
|
|
insertion_result) {
|
2017-12-18 23:32:34 +08:00
|
|
|
auto shared_range_data =
|
2018-03-03 06:21:28 +08:00
|
|
|
std::make_shared<sensor::RangeData>(std::move(range_data));
|
2018-01-29 21:02:33 +08:00
|
|
|
|
|
|
|
// If there is an uplink server and a submap insertion happened, enqueue this
|
|
|
|
// local SLAM result for uploading.
|
|
|
|
if (insertion_result &&
|
|
|
|
grpc_server_->GetUnsynchronizedContext<MapBuilderContext>()
|
|
|
|
->local_trajectory_uploader()) {
|
2018-03-03 06:21:28 +08:00
|
|
|
auto data_request =
|
|
|
|
common::make_unique<proto::AddLocalSlamResultDataRequest>();
|
2018-01-29 21:02:33 +08:00
|
|
|
auto sensor_id = grpc_server_->GetUnsynchronizedContext<MapBuilderContext>()
|
|
|
|
->local_trajectory_uploader()
|
|
|
|
->GetLocalSlamResultSensorId(trajectory_id);
|
2018-03-03 06:21:28 +08:00
|
|
|
CreateAddLocalSlamResultDataRequest(sensor_id.id, trajectory_id, time,
|
|
|
|
starting_submap_index_,
|
|
|
|
*insertion_result, data_request.get());
|
2018-01-29 21:02:33 +08:00
|
|
|
// TODO(cschuet): Make this more robust.
|
|
|
|
if (insertion_result->insertion_submaps.front()->finished()) {
|
|
|
|
++starting_submap_index_;
|
|
|
|
}
|
|
|
|
grpc_server_->GetUnsynchronizedContext<MapBuilderContext>()
|
|
|
|
->local_trajectory_uploader()
|
|
|
|
->EnqueueDataRequest(std::move(data_request));
|
|
|
|
}
|
|
|
|
|
2018-03-03 06:21:28 +08:00
|
|
|
common::MutexLocker locker(&local_slam_subscriptions_lock_);
|
2017-12-18 23:32:34 +08:00
|
|
|
for (auto& entry : local_slam_subscriptions_[trajectory_id]) {
|
2018-01-11 00:26:04 +08:00
|
|
|
auto copy_of_insertion_result =
|
|
|
|
insertion_result
|
2018-03-03 06:21:28 +08:00
|
|
|
? common::make_unique<
|
|
|
|
const mapping::TrajectoryBuilderInterface::InsertionResult>(
|
|
|
|
*insertion_result)
|
2018-01-11 00:26:04 +08:00
|
|
|
: nullptr;
|
2018-02-01 02:22:08 +08:00
|
|
|
MapBuilderContextInterface::LocalSlamSubscriptionCallback callback =
|
|
|
|
entry.second;
|
2018-03-03 06:21:28 +08:00
|
|
|
callback(common::make_unique<MapBuilderContextInterface::LocalSlamResult>(
|
2018-02-01 02:22:08 +08:00
|
|
|
MapBuilderContextInterface::LocalSlamResult{
|
|
|
|
trajectory_id, time, local_pose, shared_range_data,
|
|
|
|
std::move(copy_of_insertion_result)}));
|
2017-12-18 23:32:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-01 02:22:08 +08:00
|
|
|
MapBuilderContextInterface::SubscriptionId
|
|
|
|
MapBuilderServer::SubscribeLocalSlamResults(
|
|
|
|
int trajectory_id,
|
|
|
|
MapBuilderContextInterface::LocalSlamSubscriptionCallback callback) {
|
2018-03-03 06:21:28 +08:00
|
|
|
common::MutexLocker locker(&local_slam_subscriptions_lock_);
|
2017-12-18 23:32:34 +08:00
|
|
|
local_slam_subscriptions_[trajectory_id].emplace(current_subscription_index_,
|
|
|
|
callback);
|
2018-02-01 02:22:08 +08:00
|
|
|
return MapBuilderContextInterface::SubscriptionId{
|
|
|
|
trajectory_id, current_subscription_index_++};
|
2017-12-18 23:32:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapBuilderServer::UnsubscribeLocalSlamResults(
|
2018-02-01 02:22:08 +08:00
|
|
|
const MapBuilderContextInterface::SubscriptionId& subscription_id) {
|
2018-03-03 06:21:28 +08:00
|
|
|
common::MutexLocker locker(&local_slam_subscriptions_lock_);
|
2017-12-18 23:32:34 +08:00
|
|
|
CHECK_EQ(local_slam_subscriptions_[subscription_id.trajectory_id].erase(
|
|
|
|
subscription_id.subscription_index),
|
|
|
|
1u);
|
|
|
|
}
|
|
|
|
|
2017-12-20 18:42:01 +08:00
|
|
|
void MapBuilderServer::NotifyFinishTrajectory(int trajectory_id) {
|
2018-03-03 06:21:28 +08:00
|
|
|
common::MutexLocker locker(&local_slam_subscriptions_lock_);
|
2017-12-20 18:42:01 +08:00
|
|
|
for (auto& entry : local_slam_subscriptions_[trajectory_id]) {
|
2018-02-01 02:22:08 +08:00
|
|
|
MapBuilderContextInterface::LocalSlamSubscriptionCallback callback =
|
|
|
|
entry.second;
|
2017-12-20 18:42:01 +08:00
|
|
|
// 'nullptr' signals subscribers that the trajectory finished.
|
|
|
|
callback(nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-03 21:56:56 +08:00
|
|
|
void MapBuilderServer::WaitUntilIdle() {
|
2018-01-12 20:00:25 +08:00
|
|
|
incoming_data_queue_.WaitUntilEmpty();
|
2018-01-03 21:56:56 +08:00
|
|
|
map_builder_->pose_graph()->RunFinalOptimization();
|
|
|
|
}
|
|
|
|
|
2018-03-03 06:21:28 +08:00
|
|
|
} // namespace cloud
|
|
|
|
} // namespace cartographer
|