2017-12-13 22:53:47 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cartographer_grpc/mapping/trajectory_builder_stub.h"
|
|
|
|
|
2018-01-11 17:19:37 +08:00
|
|
|
#include "cartographer/mapping/local_slam_result_data.h"
|
2017-12-18 18:24:16 +08:00
|
|
|
#include "cartographer_grpc/proto/map_builder_service.pb.h"
|
2018-01-15 22:31:33 +08:00
|
|
|
#include "cartographer_grpc/sensor/serialization.h"
|
2017-12-13 22:53:47 +08:00
|
|
|
#include "glog/logging.h"
|
|
|
|
|
|
|
|
namespace cartographer_grpc {
|
|
|
|
namespace mapping {
|
|
|
|
|
|
|
|
TrajectoryBuilderStub::TrajectoryBuilderStub(
|
2017-12-20 19:42:27 +08:00
|
|
|
std::shared_ptr<grpc::Channel> client_channel, const int trajectory_id,
|
|
|
|
LocalSlamResultCallback local_slam_result_callback)
|
2017-12-18 18:24:16 +08:00
|
|
|
: client_channel_(client_channel), trajectory_id_(trajectory_id) {
|
|
|
|
stub_ = proto::MapBuilderService::NewStub(client_channel_);
|
|
|
|
CHECK(stub_) << "Failed to create stub.";
|
2017-12-20 19:42:27 +08:00
|
|
|
if (local_slam_result_callback) {
|
|
|
|
proto::ReceiveLocalSlamResultsRequest request;
|
|
|
|
request.set_trajectory_id(trajectory_id);
|
|
|
|
local_slam_result_reader_.client_reader = stub_->ReceiveLocalSlamResults(
|
|
|
|
&local_slam_result_reader_.client_context, request);
|
|
|
|
auto* client_reader_ptr = local_slam_result_reader_.client_reader.get();
|
|
|
|
local_slam_result_reader_.thread =
|
|
|
|
cartographer::common::make_unique<std::thread>(
|
|
|
|
[client_reader_ptr, local_slam_result_callback]() {
|
|
|
|
RunLocalSlamResultReader(client_reader_ptr,
|
|
|
|
local_slam_result_callback);
|
|
|
|
});
|
|
|
|
}
|
2017-12-18 18:24:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TrajectoryBuilderStub::~TrajectoryBuilderStub() {
|
2017-12-20 19:42:27 +08:00
|
|
|
if (local_slam_result_reader_.thread) {
|
|
|
|
local_slam_result_reader_.thread->join();
|
|
|
|
}
|
2017-12-18 18:24:16 +08:00
|
|
|
if (rangefinder_writer_.client_writer) {
|
2017-12-19 22:49:56 +08:00
|
|
|
CHECK(rangefinder_writer_.client_writer->WritesDone());
|
2017-12-18 18:24:16 +08:00
|
|
|
CHECK(rangefinder_writer_.client_writer->Finish().ok());
|
|
|
|
}
|
|
|
|
if (imu_writer_.client_writer) {
|
2017-12-19 22:49:56 +08:00
|
|
|
CHECK(imu_writer_.client_writer->WritesDone());
|
2017-12-18 18:24:16 +08:00
|
|
|
CHECK(imu_writer_.client_writer->Finish().ok());
|
|
|
|
}
|
|
|
|
if (odometry_writer_.client_writer) {
|
2017-12-19 22:49:56 +08:00
|
|
|
CHECK(odometry_writer_.client_writer->WritesDone());
|
2017-12-18 18:24:16 +08:00
|
|
|
CHECK(odometry_writer_.client_writer->Finish().ok());
|
|
|
|
}
|
|
|
|
if (fixed_frame_writer_.client_writer) {
|
2017-12-19 22:49:56 +08:00
|
|
|
CHECK(fixed_frame_writer_.client_writer->WritesDone());
|
2017-12-18 18:24:16 +08:00
|
|
|
CHECK(fixed_frame_writer_.client_writer->Finish().ok());
|
|
|
|
}
|
|
|
|
}
|
2017-12-13 22:53:47 +08:00
|
|
|
|
|
|
|
void TrajectoryBuilderStub::AddSensorData(
|
|
|
|
const std::string& sensor_id,
|
|
|
|
const cartographer::sensor::TimedPointCloudData& timed_point_cloud_data) {
|
2017-12-18 18:24:16 +08:00
|
|
|
if (!rangefinder_writer_.client_writer) {
|
|
|
|
rangefinder_writer_.client_writer = stub_->AddRangefinderData(
|
|
|
|
&rangefinder_writer_.client_context, &rangefinder_writer_.response);
|
|
|
|
CHECK(rangefinder_writer_.client_writer);
|
|
|
|
}
|
|
|
|
proto::AddRangefinderDataRequest request;
|
2018-01-16 19:20:15 +08:00
|
|
|
sensor::CreateAddRangeFinderDataRequest(
|
2018-01-15 22:31:33 +08:00
|
|
|
sensor_id, trajectory_id_,
|
|
|
|
cartographer::sensor::ToProto(timed_point_cloud_data), &request);
|
2017-12-18 18:24:16 +08:00
|
|
|
rangefinder_writer_.client_writer->Write(request);
|
2017-12-13 22:53:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TrajectoryBuilderStub::AddSensorData(
|
|
|
|
const std::string& sensor_id,
|
|
|
|
const cartographer::sensor::ImuData& imu_data) {
|
2017-12-18 18:24:16 +08:00
|
|
|
if (!imu_writer_.client_writer) {
|
|
|
|
imu_writer_.client_writer =
|
|
|
|
stub_->AddImuData(&imu_writer_.client_context, &imu_writer_.response);
|
|
|
|
CHECK(imu_writer_.client_writer);
|
|
|
|
}
|
|
|
|
proto::AddImuDataRequest request;
|
2018-01-16 19:20:15 +08:00
|
|
|
sensor::CreateAddImuDataRequest(sensor_id, trajectory_id_,
|
|
|
|
cartographer::sensor::ToProto(imu_data),
|
|
|
|
&request);
|
2017-12-18 18:24:16 +08:00
|
|
|
imu_writer_.client_writer->Write(request);
|
2017-12-13 22:53:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TrajectoryBuilderStub::AddSensorData(
|
|
|
|
const std::string& sensor_id,
|
|
|
|
const cartographer::sensor::OdometryData& odometry_data) {
|
2017-12-18 18:24:16 +08:00
|
|
|
if (!odometry_writer_.client_writer) {
|
|
|
|
odometry_writer_.client_writer = stub_->AddOdometryData(
|
|
|
|
&odometry_writer_.client_context, &odometry_writer_.response);
|
|
|
|
CHECK(odometry_writer_.client_writer);
|
|
|
|
}
|
|
|
|
proto::AddOdometryDataRequest request;
|
2018-01-16 19:20:15 +08:00
|
|
|
sensor::CreateAddOdometryDataRequest(
|
|
|
|
sensor_id, trajectory_id_, cartographer::sensor::ToProto(odometry_data),
|
|
|
|
&request);
|
2017-12-18 18:24:16 +08:00
|
|
|
odometry_writer_.client_writer->Write(request);
|
2017-12-13 22:53:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TrajectoryBuilderStub::AddSensorData(
|
|
|
|
const std::string& sensor_id,
|
|
|
|
const cartographer::sensor::FixedFramePoseData& fixed_frame_pose) {
|
2017-12-18 18:24:16 +08:00
|
|
|
if (!fixed_frame_writer_.client_writer) {
|
|
|
|
fixed_frame_writer_.client_writer = stub_->AddFixedFramePoseData(
|
|
|
|
&fixed_frame_writer_.client_context, &fixed_frame_writer_.response);
|
|
|
|
CHECK(fixed_frame_writer_.client_writer);
|
|
|
|
}
|
|
|
|
proto::AddFixedFramePoseDataRequest request;
|
2018-01-16 19:20:15 +08:00
|
|
|
sensor::CreateAddFixedFramePoseDataRequest(
|
2018-01-15 22:31:33 +08:00
|
|
|
sensor_id, trajectory_id_,
|
|
|
|
cartographer::sensor::ToProto(fixed_frame_pose), &request);
|
2017-12-18 18:24:16 +08:00
|
|
|
fixed_frame_writer_.client_writer->Write(request);
|
|
|
|
}
|
|
|
|
|
2018-01-19 16:41:24 +08:00
|
|
|
void TrajectoryBuilderStub::AddSensorData(
|
|
|
|
const std::string& sensor_id,
|
|
|
|
const cartographer::sensor::LandmarkData& landmark_data) {
|
2018-01-20 00:24:30 +08:00
|
|
|
if (!landmark_writer_.client_writer) {
|
|
|
|
landmark_writer_.client_writer = stub_->AddLandmarkData(
|
|
|
|
&landmark_writer_.client_context, &landmark_writer_.response);
|
|
|
|
CHECK(landmark_writer_.client_writer);
|
|
|
|
}
|
|
|
|
proto::AddLandmarkDataRequest request;
|
|
|
|
sensor::CreateAddLandmarkDataRequest(
|
|
|
|
sensor_id, trajectory_id_, cartographer::sensor::ToProto(landmark_data),
|
|
|
|
&request);
|
|
|
|
landmark_writer_.client_writer->Write(request);
|
2018-01-19 16:41:24 +08:00
|
|
|
}
|
|
|
|
|
2018-01-11 17:19:37 +08:00
|
|
|
void TrajectoryBuilderStub::AddLocalSlamResultData(
|
|
|
|
std::unique_ptr<cartographer::mapping::LocalSlamResultData>
|
|
|
|
local_slam_result_data) {
|
|
|
|
LOG(FATAL) << "Not implemented";
|
|
|
|
}
|
|
|
|
|
2017-12-20 19:42:27 +08:00
|
|
|
void TrajectoryBuilderStub::RunLocalSlamResultReader(
|
|
|
|
grpc::ClientReader<proto::ReceiveLocalSlamResultsResponse>* client_reader,
|
|
|
|
LocalSlamResultCallback local_slam_result_callback) {
|
|
|
|
proto::ReceiveLocalSlamResultsResponse response;
|
|
|
|
while (client_reader->Read(&response)) {
|
|
|
|
int trajectory_id = response.trajectory_id();
|
|
|
|
cartographer::common::Time time =
|
|
|
|
cartographer::common::FromUniversal(response.timestamp());
|
|
|
|
cartographer::transform::Rigid3d local_pose =
|
|
|
|
cartographer::transform::ToRigid3(response.local_pose());
|
|
|
|
cartographer::sensor::RangeData range_data =
|
|
|
|
cartographer::sensor::FromProto(response.range_data());
|
2018-01-11 00:26:04 +08:00
|
|
|
auto insertion_result =
|
|
|
|
response.has_insertion_result()
|
|
|
|
? cartographer::common::make_unique<InsertionResult>(
|
|
|
|
InsertionResult{cartographer::mapping::NodeId{
|
|
|
|
response.insertion_result().node_id().trajectory_id(),
|
|
|
|
response.insertion_result().node_id().node_index()}})
|
2017-12-20 19:42:27 +08:00
|
|
|
: nullptr;
|
|
|
|
local_slam_result_callback(trajectory_id, time, local_pose, range_data,
|
2018-01-11 00:26:04 +08:00
|
|
|
std::move(insertion_result));
|
2017-12-20 19:42:27 +08:00
|
|
|
}
|
|
|
|
client_reader->Finish();
|
|
|
|
}
|
|
|
|
|
2017-12-13 22:53:47 +08:00
|
|
|
} // namespace mapping
|
|
|
|
} // namespace cartographer_grpc
|