42 #include "glog/logging.h" 54 const proto::MapBuilderServerOptions& map_builder_server_options,
55 std::unique_ptr<mapping::MapBuilderInterface> map_builder)
57 async_grpc::Server::Builder server_builder;
58 server_builder.SetServerAddress(map_builder_server_options.server_address());
59 server_builder.SetNumGrpcThreads(
60 map_builder_server_options.num_grpc_threads());
61 server_builder.SetNumEventThreads(
62 map_builder_server_options.num_event_threads());
63 if (!map_builder_server_options.uplink_server_address().empty()) {
65 map_builder_server_options.uplink_server_address(),
66 map_builder_server_options.upload_batch_size(),
67 map_builder_server_options.enable_ssl_encryption());
69 server_builder.RegisterHandler<handlers::AddTrajectoryHandler>();
70 server_builder.RegisterHandler<handlers::AddOdometryDataHandler>();
71 server_builder.RegisterHandler<handlers::AddImuDataHandler>();
72 server_builder.RegisterHandler<handlers::AddRangefinderDataHandler>();
73 server_builder.RegisterHandler<handlers::AddFixedFramePoseDataHandler>();
74 server_builder.RegisterHandler<handlers::AddLandmarkDataHandler>();
75 server_builder.RegisterHandler<handlers::AddSensorDataBatchHandler>();
76 server_builder.RegisterHandler<handlers::FinishTrajectoryHandler>();
78 .RegisterHandler<handlers::ReceiveGlobalSlamOptimizationsHandler>();
79 server_builder.RegisterHandler<handlers::ReceiveLocalSlamResultsHandler>();
80 server_builder.RegisterHandler<handlers::GetSubmapHandler>();
81 server_builder.RegisterHandler<handlers::GetTrajectoryNodePosesHandler>();
82 server_builder.RegisterHandler<handlers::GetLandmarkPosesHandler>();
83 server_builder.RegisterHandler<handlers::GetAllSubmapPosesHandler>();
84 server_builder.RegisterHandler<handlers::GetLocalToGlobalTransformHandler>();
85 server_builder.RegisterHandler<handlers::GetConstraintsHandler>();
86 server_builder.RegisterHandler<handlers::IsTrajectoryFinishedHandler>();
87 server_builder.RegisterHandler<handlers::IsTrajectoryFrozenHandler>();
88 server_builder.RegisterHandler<handlers::LoadStateHandler>();
89 server_builder.RegisterHandler<handlers::RunFinalOptimizationHandler>();
90 server_builder.RegisterHandler<handlers::WriteStateHandler>();
91 server_builder.RegisterHandler<handlers::SetLandmarkPoseHandler>();
93 if (map_builder_server_options.map_builder_options()
94 .use_trajectory_builder_2d()) {
95 grpc_server_->SetExecutionContext(
97 }
else if (map_builder_server_options.map_builder_options()
98 .use_trajectory_builder_3d()) {
99 grpc_server_->SetExecutionContext(
103 <<
"Set either use_trajectory_builder_2d or use_trajectory_builder_3d";
105 map_builder_->pose_graph()->SetGlobalSlamOptimizationCallback(
107 std::placeholders::_1, std::placeholders::_2));
143 LOG(INFO) <<
"Starting SLAM thread.";
146 std::unique_ptr<MapBuilderContextInterface::Data> sensor_data =
150 ->AddSensorDataToTrajectory(*sensor_data);
166 std::unique_ptr<const mapping::TrajectoryBuilderInterface::InsertionResult>
168 auto shared_range_data =
169 std::make_shared<sensor::RangeData>(std::move(range_data));
173 if (insertion_result &&
176 auto sensor_data = common::make_unique<proto::SensorData>();
179 ->local_trajectory_uploader()
180 ->GetLocalSlamResultSensorId(trajectory_id);
183 *insertion_result, sensor_data.get());
185 if (insertion_result->insertion_submaps.front()->finished()) {
189 ->local_trajectory_uploader()
194 for (
auto& entry : local_slam_subscriptions_[trajectory_id]) {
195 auto copy_of_insertion_result =
204 common::make_unique<MapBuilderContextInterface::LocalSlamResult>(
206 trajectory_id,
time, local_pose, shared_range_data,
207 std::move(copy_of_insertion_result)}))) {
208 LOG(INFO) <<
"Removing subscription with index: " << entry.first;
209 CHECK_EQ(local_slam_subscriptions_[trajectory_id].erase(entry.first), 1u);
215 const std::map<int, mapping::SubmapId>& last_optimized_submap_ids,
216 const std::map<int, mapping::NodeId>& last_optimized_node_ids) {
218 for (
auto& entry : global_slam_subscriptions_) {
219 if (!entry.second(last_optimized_submap_ids, last_optimized_node_ids)) {
220 LOG(INFO) <<
"Removing subscription with index: " << entry.first;
221 CHECK_EQ(global_slam_subscriptions_.erase(entry.first), 1u);
241 CHECK_EQ(local_slam_subscriptions_[subscription_id.
trajectory_id].erase(
254 int subscription_index) {
256 CHECK_EQ(global_slam_subscriptions_.erase(subscription_index), 1u);
261 for (
auto& entry : local_slam_subscriptions_[trajectory_id]) {
276 "cloud_internal_map_builder_server_incoming_data_queue_length",
277 "Incoming SLAM Data Queue length");
278 kIncomingDataQueueMetric = queue_length->Add({});
int current_subscription_index_
common::Duration FromMilliseconds(const int64 milliseconds)
void OnGlobalSlamOptimizations(const std::map< int, mapping::SubmapId > &last_optimized_submap_ids, const std::map< int, mapping::NodeId > &last_optimized_node_ids)
void WaitUntilIdle() final
std::unique_ptr< mapping::MapBuilderInterface > map_builder_
virtual Family< Gauge > * NewGaugeFamily(const std::string &name, const std::string &description)=0
_Unique_if< T >::_Single_object make_unique(Args &&... args)
MapBuilderContextInterface::LocalSlamSubscriptionId SubscribeLocalSlamResults(int trajectory_id, MapBuilderContextInterface::LocalSlamSubscriptionCallback callback)
std::function< bool(std::unique_ptr< LocalSlamResult >)> LocalSlamSubscriptionCallback
void NotifyFinishTrajectory(int trajectory_id)
std::function< bool(const std::map< int, mapping::SubmapId > &, const std::map< int, mapping::NodeId > &)> GlobalSlamOptimizationCallback
void WaitForShutdown() final
std::unique_ptr< MapBuilderInterface > map_builder_
std::unique_ptr< async_grpc::Server > grpc_server_
virtual void EnqueueSensorData(int trajectory_id, std::unique_ptr< sensor::Data > data)=0
UniversalTimeScaleClock::time_point Time
void OnLocalSlamResult(int trajectory_id, common::Time time, transform::Rigid3d local_pose, sensor::RangeData range_data, std::unique_ptr< const mapping::TrajectoryBuilderInterface::InsertionResult > insertion_result)
void ProcessSensorDataQueue()
std::unique_ptr< LocalTrajectoryUploaderInterface > CreateLocalTrajectoryUploader(const std::string &uplink_server_address, int batch_size, bool enable_ssl_encryption)
MapBuilderServer(const proto::MapBuilderServerOptions &map_builder_server_options, std::unique_ptr< mapping::MapBuilderInterface > map_builder)
UniversalTimeScaleClock::duration Duration
std::unique_ptr< LocalTrajectoryUploaderInterface > local_trajectory_uploader_
int SubscribeGlobalSlamOptimizations(MapBuilderContextInterface::GlobalSlamOptimizationCallback callback)
std::unique_ptr< std::thread > slam_thread_
const int subscription_index
void UnsubscribeLocalSlamResults(const MapBuilderContextInterface::LocalSlamSubscriptionId &subscription_id)
common::BlockingQueue< std::unique_ptr< MapBuilderContextInterface::Data > > incoming_data_queue_
static void RegisterMetrics(metrics::FamilyFactory *family_factory)
int starting_submap_index_
void CreateSensorDataForLocalSlamResult(const std::string &sensor_id, int trajectory_id, common::Time time, int starting_submap_index, const mapping::TrajectoryBuilderInterface::InsertionResult &insertion_result, proto::SensorData *proto)
Mutex::Locker MutexLocker
virtual LocalTrajectoryUploaderInterface * local_trajectory_uploader()=0
void UnsubscribeGlobalSlamOptimizations(int subscription_index)
common::Mutex subscriptions_lock_