From 697be9a77bc3bee7e94bab7e2d75651fdb197185 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Sch=C3=BCtte?= Date: Tue, 5 Dec 2017 14:46:25 +0100 Subject: [PATCH] Add MappingServer skeleton. (#730) --- cartographer_grpc/framework/server.cc | 8 +++ cartographer_grpc/framework/server.h | 5 ++ cartographer_grpc/map_builder_server.cc | 70 +++++++++++++++++++ cartographer_grpc/map_builder_server.h | 67 ++++++++++++++++++ .../map_builder_server_options.cc | 50 +++++++++++++ .../map_builder_server_options.h | 36 ++++++++++ .../proto/map_builder_server_options.proto | 25 +++++++ ...ervice.proto => map_builder_service.proto} | 6 +- 8 files changed, 264 insertions(+), 3 deletions(-) create mode 100644 cartographer_grpc/map_builder_server.cc create mode 100644 cartographer_grpc/map_builder_server.h create mode 100644 cartographer_grpc/map_builder_server_options.cc create mode 100644 cartographer_grpc/map_builder_server_options.h create mode 100644 cartographer_grpc/proto/map_builder_server_options.proto rename cartographer_grpc/proto/{cartographer_service.proto => map_builder_service.proto} (95%) diff --git a/cartographer_grpc/framework/server.cc b/cartographer_grpc/framework/server.cc index ef0669e..00e13c0 100644 --- a/cartographer_grpc/framework/server.cc +++ b/cartographer_grpc/framework/server.cc @@ -87,6 +87,14 @@ void Server::Start() { } } +void Server::WaitForShutdown() { + if (!server_) { + return; + } + + server_->Wait(); +} + void Server::Shutdown() { LOG(INFO) << "Shutting down server."; diff --git a/cartographer_grpc/framework/server.h b/cartographer_grpc/framework/server.h index 98b027c..d08ab98 100644 --- a/cartographer_grpc/framework/server.h +++ b/cartographer_grpc/framework/server.h @@ -85,6 +85,11 @@ class Server { // Starts a server starts serving the registered services. void Start(); + // Waits for the server to shut down. Note: The server must be either shutting + // down or some other thread must call 'Shutdown()' for this function to ever + // return. + void WaitForShutdown(); + // Shuts down the server and all of its services. void Shutdown(); diff --git a/cartographer_grpc/map_builder_server.cc b/cartographer_grpc/map_builder_server.cc new file mode 100644 index 0000000..d2aa330 --- /dev/null +++ b/cartographer_grpc/map_builder_server.cc @@ -0,0 +1,70 @@ +/* + * 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. + */ +#include "cartographer_grpc/map_builder_server.h" + +#include "cartographer_grpc/proto/map_builder_service.grpc.pb.h" +#include "glog/logging.h" + +namespace cartographer_grpc { + +MapBuilderServer::MapBuilderServer( + const proto::MapBuilderServerOptions& map_builder_server_options) + : map_builder_(map_builder_server_options.map_builder_options()) { + framework::Server::Builder server_builder; + server_builder.SetServerAddress(map_builder_server_options.server_address()); + server_builder.SetNumberOfThreads( + map_builder_server_options.num_grpc_threads()); + grpc_server_ = server_builder.Build(); + grpc_server_->SetExecutionContext( + cartographer::common::make_unique(&map_builder_)); +} + +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() { + while (!shutting_down_) { + // TODO(cschuet): Implement this. + } +} + +void MapBuilderServer::StartSlamThread() { + CHECK(!slam_thread_); + + // Start the SLAM processing thread. + slam_thread_ = cartographer::common::make_unique( + [this]() { this->ProcessSensorDataQueue(); }); +} + +} // namespace cartographer_grpc diff --git a/cartographer_grpc/map_builder_server.h b/cartographer_grpc/map_builder_server.h new file mode 100644 index 0000000..36ca158 --- /dev/null +++ b/cartographer_grpc/map_builder_server.h @@ -0,0 +1,67 @@ +/* + * 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. + */ + +#ifndef CARTOGRAPHER_GRPC_MAP_BUILDER_SERVER_H +#define CARTOGRAPHER_GRPC_MAP_BUILDER_SERVER_H + +#include "cartographer/common/blocking_queue.h" +#include "cartographer/mapping/map_builder.h" +#include "cartographer/sensor/data.h" +#include "cartographer_grpc/framework/execution_context.h" +#include "cartographer_grpc/framework/server.h" +#include "cartographer_grpc/proto/map_builder_server_options.pb.h" + +namespace cartographer_grpc { + +class MapBuilderServer { + public: + class MapBuilderContext : public framework::ExecutionContext { + public: + MapBuilderContext(cartographer::mapping::MapBuilder* map_builder) + : map_builder_(map_builder) {} + cartographer::mapping::MapBuilder& map_builder() { return *map_builder_; } + + private: + cartographer::mapping::MapBuilder* map_builder_; + }; + + MapBuilderServer( + const proto::MapBuilderServerOptions& map_builder_server_options); + + // Starts the gRPC server and the SLAM thread. + void Start(); + + // Waits for the 'MapBuilderServer' to shut down. Note: The server must be + // either shutting down or some other thread must call 'Shutdown()' for this + // function to ever return. + void WaitForShutdown(); + + // Shuts down the gRPC server and the SLAM thread. + void Shutdown(); + + private: + void ProcessSensorDataQueue(); + void StartSlamThread(); + + bool shutting_down_ = false; + std::unique_ptr slam_thread_; + std::unique_ptr grpc_server_; + cartographer::mapping::MapBuilder map_builder_; +}; + +} // namespace cartographer_grpc + +#endif // CARTOGRAPHER_GRPC_MAP_BUILDER_SERVER_H diff --git a/cartographer_grpc/map_builder_server_options.cc b/cartographer_grpc/map_builder_server_options.cc new file mode 100644 index 0000000..853704d --- /dev/null +++ b/cartographer_grpc/map_builder_server_options.cc @@ -0,0 +1,50 @@ +/* + * 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. + */ + +#include "cartographer_grpc/map_builder_server_options.h" + +#include "cartographer/common/configuration_file_resolver.h" +#include "cartographer/common/make_unique.h" +#include "cartographer/mapping/map_builder.h" + +namespace cartographer_grpc { + +proto::MapBuilderServerOptions CreateMapBuilderServerOptions( + ::cartographer::common::LuaParameterDictionary* lua_parameter_dictionary) { + proto::MapBuilderServerOptions map_builder_server_options; + map_builder_server_options.set_server_address( + lua_parameter_dictionary->GetString("server_address")); + map_builder_server_options.set_num_grpc_threads( + lua_parameter_dictionary->GetInt("num_grpc_threads")); + *map_builder_server_options.mutable_map_builder_options() = + cartographer::mapping::CreateMapBuilderOptions(lua_parameter_dictionary); + return map_builder_server_options; +} + +proto::MapBuilderServerOptions LoadMapBuilderServerOptions( + const std::string& configuration_directory, + const std::string& configuration_basename) { + auto file_resolver = cartographer::common::make_unique< + cartographer::common::ConfigurationFileResolver>( + std::vector{configuration_directory}); + const std::string code = + file_resolver->GetFileContentOrDie(configuration_basename); + cartographer::common::LuaParameterDictionary lua_parameter_dictionary( + code, std::move(file_resolver)); + return CreateMapBuilderServerOptions(&lua_parameter_dictionary); +} + +} // namespace cartographer_grpc diff --git a/cartographer_grpc/map_builder_server_options.h b/cartographer_grpc/map_builder_server_options.h new file mode 100644 index 0000000..e796038 --- /dev/null +++ b/cartographer_grpc/map_builder_server_options.h @@ -0,0 +1,36 @@ +/* + * 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. + */ + +#ifndef CARTOGRAPHER_GRPC_MAP_BUILDER_SERVER_OPTIONS_H +#define CARTOGRAPHER_GRPC_MAP_BUILDER_SERVER_OPTIONS_H + +#include + +#include "cartographer/common/lua_parameter_dictionary.h" +#include "cartographer_grpc/proto/map_builder_server_options.pb.h" + +namespace cartographer_grpc { + +proto::MapBuilderServerOptions CreateMapBuilderServerOptions( + ::cartographer::common::LuaParameterDictionary* lua_parameter_dictionary); + +proto::MapBuilderServerOptions LoadMapBuilderServerOptions( + const std::string& configuration_directory, + const std::string& configuration_basename); + +} // namespace cartographer_grpc + +#endif // CARTOGRAPHER_GRPC_MAP_BUILDER_SERVER_OPTIONS_H diff --git a/cartographer_grpc/proto/map_builder_server_options.proto b/cartographer_grpc/proto/map_builder_server_options.proto new file mode 100644 index 0000000..72748b4 --- /dev/null +++ b/cartographer_grpc/proto/map_builder_server_options.proto @@ -0,0 +1,25 @@ +// 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. + +syntax = "proto3"; + +import "cartographer/mapping/proto/map_builder_options.proto"; + +package cartographer_grpc.proto; + +message MapBuilderServerOptions { + string server_address = 1; + int32 num_grpc_threads = 2; + cartographer.mapping.proto.MapBuilderOptions map_builder_options = 3; +} diff --git a/cartographer_grpc/proto/cartographer_service.proto b/cartographer_grpc/proto/map_builder_service.proto similarity index 95% rename from cartographer_grpc/proto/cartographer_service.proto rename to cartographer_grpc/proto/map_builder_service.proto index 6ec9351..162dce5 100644 --- a/cartographer_grpc/proto/cartographer_service.proto +++ b/cartographer_grpc/proto/map_builder_service.proto @@ -21,8 +21,9 @@ import "google/protobuf/empty.proto"; package cartographer_grpc.proto; message AddTrajectoryRequest { + repeated string expected_sensor_ids = 1; cartographer.mapping.proto.TrajectoryBuilderOptions - trajectory_builder_options = 1; + trajectory_builder_options = 2; } message SensorMetadata { @@ -53,7 +54,7 @@ message FinishTrajectoryRequest { int32 trajectory_id = 1; } -service Cartographer { +service MapBuilderService { // Starts a new trajectory and returns its index. rpc AddTrajectory(AddTrajectoryRequest) returns (AddTrajectoryResponse); @@ -75,4 +76,3 @@ service Cartographer { // i.e. no further sensor data is expected. rpc FinishTrajectory(FinishTrajectoryRequest) returns (google.protobuf.Empty); } -