map_builder_stub.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2017 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 
27 #include "cartographer/cloud/proto/map_builder_service.pb.h"
29 #include "glog/logging.h"
30 
31 namespace cartographer {
32 namespace cloud {
33 namespace {
34 
36 constexpr int kConnectionTimeoutInSecond = 10;
37 
38 } // namespace
39 
40 MapBuilderStub::MapBuilderStub(const std::string& server_address)
41  : client_channel_(::grpc::CreateChannel(
42  server_address, ::grpc::InsecureChannelCredentials())),
43  pose_graph_stub_(make_unique<PoseGraphStub>(client_channel_)) {
44  LOG(INFO) << "Connecting to SLAM process at " << server_address;
45  std::chrono::system_clock::time_point deadline(
46  std::chrono::system_clock::now() +
47  std::chrono::seconds(kConnectionTimeoutInSecond));
48  if (!client_channel_->WaitForConnected(deadline)) {
49  LOG(FATAL) << "Failed to connect to " << server_address;
50  }
51 }
52 
54  const std::set<SensorId>& expected_sensor_ids,
55  const mapping::proto::TrajectoryBuilderOptions& trajectory_options,
56  LocalSlamResultCallback local_slam_result_callback) {
57  proto::AddTrajectoryRequest request;
58  *request.mutable_trajectory_builder_options() = trajectory_options;
59  for (const auto& sensor_id : expected_sensor_ids) {
60  *request.add_expected_sensor_ids() = cloud::ToProto(sensor_id);
61  }
62  async_grpc::Client<handlers::AddTrajectorySignature> client(
63  client_channel_, async_grpc::CreateLimitedBackoffStrategy(
64  common::FromMilliseconds(100), 2.f, 5));
65  CHECK(client.Write(request));
66 
67  // Construct trajectory builder stub.
69  std::piecewise_construct,
70  std::forward_as_tuple(client.response().trajectory_id()),
71  std::forward_as_tuple(make_unique<TrajectoryBuilderStub>(
72  client_channel_, client.response().trajectory_id(),
73  local_slam_result_callback)));
74  return client.response().trajectory_id();
75 }
76 
78  const mapping::proto::TrajectoryBuilderOptionsWithSensorIds&
79  options_with_sensor_ids_proto) {
80  LOG(FATAL) << "Not implemented";
81 }
82 
84  int trajectory_id) const {
85  return trajectory_builder_stubs_.at(trajectory_id).get();
86 }
87 
88 void MapBuilderStub::FinishTrajectory(int trajectory_id) {
89  proto::FinishTrajectoryRequest request;
90  request.set_trajectory_id(trajectory_id);
91  async_grpc::Client<handlers::FinishTrajectorySignature> client(
93  CHECK(client.Write(request));
94  trajectory_builder_stubs_.erase(trajectory_id);
95 }
96 
98  const mapping::SubmapId& submap_id,
99  mapping::proto::SubmapQuery::Response* submap_query_response) {
100  proto::GetSubmapRequest request;
101  submap_id.ToProto(request.mutable_submap_id());
102  async_grpc::Client<handlers::GetSubmapSignature> client(client_channel_);
103  CHECK(client.Write(request));
104  submap_query_response->CopyFrom(client.response().submap_query_response());
105  return client.response().error_msg();
106 }
107 
109  google::protobuf::Empty request;
110  async_grpc::Client<handlers::WriteStateSignature> client(client_channel_);
111  CHECK(client.Write(request));
112  proto::WriteStateResponse response;
113  while (client.StreamRead(&response)) {
114  switch (response.state_chunk_case()) {
115  case proto::WriteStateResponse::kHeader:
116  writer->WriteProto(response.header());
117  break;
118  case proto::WriteStateResponse::kSerializedData:
119  writer->WriteProto(response.serialized_data());
120  break;
121  default:
122  LOG(FATAL) << "Unhandled message type";
123  }
124  }
125 }
126 
128  const bool load_frozen_state) {
129  if (!load_frozen_state) {
130  LOG(FATAL) << "Not implemented";
131  }
132  async_grpc::Client<handlers::LoadStateSignature> client(client_channel_);
133 
134  io::ProtoStreamDeserializer deserializer(reader);
135  // Request with a PoseGraph proto is sent first.
136  {
137  proto::LoadStateRequest request;
138  *request.mutable_pose_graph() = deserializer.pose_graph();
139  CHECK(client.Write(request));
140  }
141  // Request with an AllTrajectoryBuilderOptions should be second.
142  {
143  proto::LoadStateRequest request;
144  *request.mutable_all_trajectory_builder_options() =
145  deserializer.all_trajectory_builder_options();
146  CHECK(client.Write(request));
147  }
148  // Multiple requests with SerializedData are sent after.
149  proto::LoadStateRequest request;
150  while (
151  deserializer.ReadNextSerializedData(request.mutable_serialized_data())) {
152  CHECK(client.Write(request));
153  }
154 
155  CHECK(reader->eof());
156  CHECK(client.StreamWritesDone());
157  CHECK(client.StreamFinish().ok());
158 }
159 
161  return trajectory_builder_stubs_.size();
162 }
163 
165  return pose_graph_stub_.get();
166 }
167 
168 const std::vector<mapping::proto::TrajectoryBuilderOptionsWithSensorIds>&
170  LOG(FATAL) << "Not implemented";
171 }
172 
173 } // namespace cloud
174 } // namespace cartographer
common::Duration FromMilliseconds(const int64 milliseconds)
Definition: time.cc:43
std::map< int, std::unique_ptr< mapping::TrajectoryBuilderInterface > > trajectory_builder_stubs_
_Unique_if< T >::_Single_object make_unique(Args &&... args)
Definition: make_unique.h:46
TrajectoryBuilderInterface::LocalSlamResultCallback LocalSlamResultCallback
std::shared_ptr<::grpc::Channel > client_channel_
const std::vector< mapping::proto::TrajectoryBuilderOptionsWithSensorIds > & GetAllTrajectoryBuilderOptions() const override
void ToProto(proto::SubmapId *proto) const
Definition: id.h:101
MapBuilderStub(const std::string &server_address)
bool ReadNextSerializedData(mapping::proto::SerializedData *data)
mapping::TrajectoryBuilderInterface * GetTrajectoryBuilder(int trajectory_id) const override
int AddTrajectoryForDeserialization(const mapping::proto::TrajectoryBuilderOptionsWithSensorIds &options_with_sensor_ids_proto) override
void SerializeState(io::ProtoStreamWriterInterface *writer) override
std::unique_ptr< mapping::PoseGraphInterface > pose_graph_stub_
int AddTrajectoryBuilder(const std::set< SensorId > &expected_sensor_ids, const mapping::proto::TrajectoryBuilderOptions &trajectory_options, LocalSlamResultCallback local_slam_result_callback) override
const mapping::proto::AllTrajectoryBuilderOptions & all_trajectory_builder_options()
void LoadState(io::ProtoStreamReaderInterface *reader, bool load_frozen_state) override
std::string SubmapToProto(const mapping::SubmapId &submap_id, mapping::proto::SubmapQuery::Response *response) override
virtual void WriteProto(const google::protobuf::Message &proto)=0
proto::SensorId ToProto(const mapping::TrajectoryBuilderInterface::SensorId &sensor_id)
std::shared_ptr<::grpc::Channel > client_channel_
void FinishTrajectory(int trajectory_id) override
int num_trajectory_builders() const override
mapping::PoseGraphInterface * pose_graph() override


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