receive_local_slam_results_handler.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2018 The Cartographer Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #include "async_grpc/rpc_handler.h"
21 #include "cartographer/cloud/proto/map_builder_service.pb.h"
24 
25 namespace cartographer {
26 namespace cloud {
27 namespace handlers {
28 namespace {
29 
30 std::unique_ptr<proto::ReceiveLocalSlamResultsResponse> GenerateResponse(
31  std::unique_ptr<MapBuilderContextInterface::LocalSlamResult>
32  local_slam_result) {
33  auto response = common::make_unique<proto::ReceiveLocalSlamResultsResponse>();
34  response->set_trajectory_id(local_slam_result->trajectory_id);
35  response->set_timestamp(common::ToUniversal(local_slam_result->time));
36  *response->mutable_local_pose() =
37  transform::ToProto(local_slam_result->local_pose);
38  if (local_slam_result->range_data) {
39  *response->mutable_range_data() =
40  sensor::ToProto(*local_slam_result->range_data);
41  }
42  if (local_slam_result->insertion_result) {
43  local_slam_result->insertion_result->node_id.ToProto(
44  response->mutable_insertion_result()->mutable_node_id());
45  }
46  return response;
47 }
48 
49 } // namespace
50 
51 void ReceiveLocalSlamResultsHandler::OnRequest(
52  const proto::ReceiveLocalSlamResultsRequest& request) {
53  auto writer = GetWriter();
54  MapBuilderContextInterface::LocalSlamSubscriptionId subscription_id =
55  GetUnsynchronizedContext<MapBuilderContextInterface>()
56  ->SubscribeLocalSlamResults(
57  request.trajectory_id(),
58  [writer](
59  std::unique_ptr<MapBuilderContextInterface::LocalSlamResult>
60  local_slam_result) {
61  if (local_slam_result) {
62  if (!writer.Write(
63  GenerateResponse(std::move(local_slam_result)))) {
64  // Client closed connection.
65  LOG(INFO) << "Client closed connection.";
66  return false;
67  }
68  } else {
69  // Callback with 'nullptr' signals that the trajectory
70  // finished.
71  writer.WritesDone();
72  }
73  return true;
74  });
75 
76  subscription_id_ =
77  common::make_unique<MapBuilderContextInterface::LocalSlamSubscriptionId>(
78  subscription_id);
79 }
80 
81 void ReceiveLocalSlamResultsHandler::OnFinish() {
82  if (subscription_id_) {
83  GetUnsynchronizedContext<MapBuilderContextInterface>()
84  ->UnsubscribeLocalSlamResults(*subscription_id_);
85  }
86 }
87 
88 } // namespace handlers
89 } // namespace cloud
90 } // namespace cartographer
proto::FixedFramePoseData ToProto(const FixedFramePoseData &pose_data)
proto::Rigid2d ToProto(const transform::Rigid2d &transform)
Definition: transform.cc:48
int64 ToUniversal(const Time time)
Definition: time.cc:36


cartographer
Author(s): The Cartographer Authors
autogenerated on Mon Feb 28 2022 22:00:58