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
|
|
|
|
2017-12-05 21:46:25 +08:00
|
|
|
#include "cartographer_grpc/map_builder_server.h"
|
|
|
|
|
2017-12-15 22:26:58 +08:00
|
|
|
#include "cartographer_grpc/handlers/add_fixed_frame_pose_data_handler.h"
|
2017-12-13 05:36:44 +08:00
|
|
|
#include "cartographer_grpc/handlers/add_imu_data_handler.h"
|
|
|
|
#include "cartographer_grpc/handlers/add_odometry_data_handler.h"
|
|
|
|
#include "cartographer_grpc/handlers/add_rangefinder_data_handler.h"
|
2017-12-05 23:36:54 +08:00
|
|
|
#include "cartographer_grpc/handlers/add_trajectory_handler.h"
|
|
|
|
#include "cartographer_grpc/handlers/finish_trajectory_handler.h"
|
2017-12-19 22:11:29 +08:00
|
|
|
#include "cartographer_grpc/handlers/receive_local_slam_results_handler.h"
|
2017-12-05 21:46:25 +08:00
|
|
|
#include "cartographer_grpc/proto/map_builder_service.grpc.pb.h"
|
|
|
|
#include "glog/logging.h"
|
|
|
|
|
|
|
|
namespace cartographer_grpc {
|
2017-12-18 17:02:57 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
const cartographer::common::Duration kPopTimeout =
|
|
|
|
cartographer::common::FromMilliseconds(100);
|
|
|
|
|
|
|
|
} // namespace
|
2017-12-05 21:46:25 +08:00
|
|
|
|
2017-12-13 05:36:44 +08:00
|
|
|
MapBuilderServer::MapBuilderContext::MapBuilderContext(
|
2017-12-18 23:32:34 +08:00
|
|
|
MapBuilderServer* map_builder_server)
|
|
|
|
: map_builder_server_(map_builder_server) {}
|
2017-12-13 05:36:44 +08:00
|
|
|
|
2017-12-19 21:36:25 +08:00
|
|
|
cartographer::mapping::MapBuilderInterface&
|
2017-12-13 05:36:44 +08:00
|
|
|
MapBuilderServer::MapBuilderContext::map_builder() {
|
2017-12-19 03:47:00 +08:00
|
|
|
return *map_builder_server_->map_builder_;
|
2017-12-13 05:36:44 +08:00
|
|
|
}
|
|
|
|
|
2017-12-18 17:02:57 +08:00
|
|
|
cartographer::common::BlockingQueue<
|
|
|
|
std::unique_ptr<MapBuilderServer::SensorData>>&
|
2017-12-13 05:36:44 +08:00
|
|
|
MapBuilderServer::MapBuilderContext::sensor_data_queue() {
|
2017-12-18 23:32:34 +08:00
|
|
|
return map_builder_server_->sensor_data_queue_;
|
|
|
|
}
|
|
|
|
|
|
|
|
cartographer::mapping::TrajectoryBuilderInterface::LocalSlamResultCallback
|
|
|
|
MapBuilderServer::MapBuilderContext::
|
|
|
|
GetLocalSlamResultCallbackForSubscriptions() {
|
|
|
|
MapBuilderServer* map_builder_server = map_builder_server_;
|
|
|
|
return [map_builder_server](
|
|
|
|
int trajectory_id, cartographer::common::Time time,
|
|
|
|
cartographer::transform::Rigid3d local_pose,
|
|
|
|
cartographer::sensor::RangeData range_data,
|
|
|
|
std::unique_ptr<const cartographer::mapping::NodeId> node_id) {
|
|
|
|
map_builder_server->OnLocalSlamResult(trajectory_id, time, local_pose,
|
|
|
|
std::move(range_data),
|
|
|
|
std::move(node_id));
|
|
|
|
};
|
2017-12-13 05:36:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapBuilderServer::MapBuilderContext::AddSensorDataToTrajectory(
|
|
|
|
const SensorData& sensor_data) {
|
|
|
|
sensor_data.sensor_data->AddToTrajectoryBuilder(
|
2017-12-19 03:47:00 +08:00
|
|
|
map_builder_server_->map_builder_->GetTrajectoryBuilder(
|
2017-12-18 23:32:34 +08:00
|
|
|
sensor_data.trajectory_id));
|
2017-12-13 05:36:44 +08:00
|
|
|
}
|
|
|
|
|
2017-12-19 22:11:29 +08:00
|
|
|
MapBuilderServer::SubscriptionId
|
|
|
|
MapBuilderServer::MapBuilderContext::SubscribeLocalSlamResults(
|
|
|
|
int trajectory_id, LocalSlamSubscriptionCallback callback) {
|
|
|
|
return map_builder_server_->SubscribeLocalSlamResults(trajectory_id,
|
|
|
|
callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapBuilderServer::MapBuilderContext::UnsubscribeLocalSlamResults(
|
|
|
|
const SubscriptionId& subscription_id) {
|
|
|
|
map_builder_server_->UnsubscribeLocalSlamResults(subscription_id);
|
|
|
|
}
|
|
|
|
|
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,
|
2017-12-19 21:36:25 +08:00
|
|
|
std::unique_ptr<cartographer::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());
|
2017-12-05 23:36:54 +08:00
|
|
|
server_builder.RegisterHandler<handlers::AddTrajectoryHandler,
|
|
|
|
proto::MapBuilderService>("AddTrajectory");
|
2017-12-13 05:36:44 +08:00
|
|
|
server_builder.RegisterHandler<handlers::AddOdometryDataHandler,
|
|
|
|
proto::MapBuilderService>("AddOdometryData");
|
|
|
|
server_builder
|
|
|
|
.RegisterHandler<handlers::AddImuDataHandler, proto::MapBuilderService>(
|
|
|
|
"AddImuData");
|
|
|
|
server_builder.RegisterHandler<handlers::AddRangefinderDataHandler,
|
|
|
|
proto::MapBuilderService>(
|
|
|
|
"AddRangefinderData");
|
2017-12-15 22:26:58 +08:00
|
|
|
server_builder.RegisterHandler<handlers::AddFixedFramePoseDataHandler,
|
|
|
|
proto::MapBuilderService>(
|
|
|
|
"AddFixedFramePoseData");
|
2017-12-05 23:36:54 +08:00
|
|
|
server_builder.RegisterHandler<handlers::FinishTrajectoryHandler,
|
|
|
|
proto::MapBuilderService>("FinishTrajectory");
|
2017-12-19 22:11:29 +08:00
|
|
|
server_builder.RegisterHandler<handlers::ReceiveLocalSlamResultsHandler,
|
|
|
|
proto::MapBuilderService>(
|
|
|
|
"ReceiveLocalSlamResults");
|
2017-12-05 21:46:25 +08:00
|
|
|
grpc_server_ = server_builder.Build();
|
|
|
|
grpc_server_->SetExecutionContext(
|
2017-12-18 23:32:34 +08:00
|
|
|
cartographer::common::make_unique<MapBuilderContext>(this));
|
2017-12-05 21:46:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapBuilderServer::Start() {
|
|
|
|
shutting_down_ = false;
|
|
|
|
StartSlamThread();
|
|
|
|
grpc_server_->Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapBuilderServer::WaitForShutdown() {
|
|
|
|
grpc_server_->WaitForShutdown();
|
|
|
|
if (slam_thread_) {
|
|
|
|
slam_thread_->join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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_) {
|
2017-12-18 17:02:57 +08:00
|
|
|
std::unique_ptr<SensorData> sensor_data =
|
|
|
|
sensor_data_queue_.PopWithTimeout(kPopTimeout);
|
|
|
|
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.
|
|
|
|
slam_thread_ = cartographer::common::make_unique<std::thread>(
|
|
|
|
[this]() { this->ProcessSensorDataQueue(); });
|
|
|
|
}
|
|
|
|
|
2017-12-18 23:32:34 +08:00
|
|
|
void MapBuilderServer::OnLocalSlamResult(
|
|
|
|
int trajectory_id, cartographer::common::Time time,
|
|
|
|
cartographer::transform::Rigid3d local_pose,
|
|
|
|
cartographer::sensor::RangeData range_data,
|
|
|
|
std::unique_ptr<const cartographer::mapping::NodeId> node_id) {
|
|
|
|
auto shared_range_data =
|
|
|
|
std::make_shared<cartographer::sensor::RangeData>(std::move(range_data));
|
|
|
|
cartographer::common::MutexLocker locker(&local_slam_subscriptions_lock_);
|
|
|
|
for (auto& entry : local_slam_subscriptions_[trajectory_id]) {
|
|
|
|
auto copy_of_node_id =
|
|
|
|
node_id ? cartographer::common::make_unique<
|
|
|
|
const cartographer::mapping::NodeId>(*node_id)
|
|
|
|
: nullptr;
|
|
|
|
LocalSlamSubscriptionCallback callback = entry.second;
|
2017-12-20 16:46:54 +08:00
|
|
|
callback(cartographer::common::make_unique<LocalSlamResult>(
|
|
|
|
LocalSlamResult{trajectory_id, time, local_pose, shared_range_data,
|
|
|
|
std::move(copy_of_node_id)}));
|
2017-12-18 23:32:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MapBuilderServer::SubscriptionId MapBuilderServer::SubscribeLocalSlamResults(
|
|
|
|
int trajectory_id, LocalSlamSubscriptionCallback callback) {
|
|
|
|
cartographer::common::MutexLocker locker(&local_slam_subscriptions_lock_);
|
|
|
|
local_slam_subscriptions_[trajectory_id].emplace(current_subscription_index_,
|
|
|
|
callback);
|
|
|
|
return SubscriptionId{trajectory_id, current_subscription_index_++};
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapBuilderServer::UnsubscribeLocalSlamResults(
|
|
|
|
const SubscriptionId& subscription_id) {
|
|
|
|
cartographer::common::MutexLocker locker(&local_slam_subscriptions_lock_);
|
|
|
|
CHECK_EQ(local_slam_subscriptions_[subscription_id.trajectory_id].erase(
|
|
|
|
subscription_id.subscription_index),
|
|
|
|
1u);
|
|
|
|
}
|
|
|
|
|
2017-12-05 21:46:25 +08:00
|
|
|
} // namespace cartographer_grpc
|