2018-01-11 17:19:37 +08:00
|
|
|
/*
|
|
|
|
* Copyright 2018 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.
|
|
|
|
*/
|
|
|
|
|
2018-03-02 21:16:49 +08:00
|
|
|
#include "cartographer_grpc/internal/local_trajectory_uploader.h"
|
2018-03-02 18:38:35 +08:00
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <thread>
|
|
|
|
|
2018-01-17 19:01:29 +08:00
|
|
|
#include "cartographer/common/make_unique.h"
|
2018-03-02 21:16:49 +08:00
|
|
|
#include "cartographer_grpc/internal/framework/client.h"
|
2018-03-02 18:38:35 +08:00
|
|
|
#include "cartographer_grpc/internal/handlers/add_fixed_frame_pose_data_handler.h"
|
|
|
|
#include "cartographer_grpc/internal/handlers/add_imu_data_handler.h"
|
|
|
|
#include "cartographer_grpc/internal/handlers/add_landmark_data_handler.h"
|
|
|
|
#include "cartographer_grpc/internal/handlers/add_local_slam_result_data_handler.h"
|
|
|
|
#include "cartographer_grpc/internal/handlers/add_odometry_data_handler.h"
|
|
|
|
#include "cartographer_grpc/internal/handlers/add_trajectory_handler.h"
|
|
|
|
#include "cartographer_grpc/internal/handlers/finish_trajectory_handler.h"
|
2018-03-02 21:16:49 +08:00
|
|
|
#include "cartographer_grpc/internal/sensor/serialization.h"
|
2018-01-17 19:01:29 +08:00
|
|
|
#include "cartographer_grpc/proto/map_builder_service.pb.h"
|
2018-01-13 06:16:29 +08:00
|
|
|
#include "glog/logging.h"
|
2018-03-02 18:38:35 +08:00
|
|
|
#include "grpc++/grpc++.h"
|
2018-01-13 06:16:29 +08:00
|
|
|
|
2018-03-03 06:21:28 +08:00
|
|
|
namespace cartographer {
|
|
|
|
namespace cloud {
|
2018-01-17 19:01:29 +08:00
|
|
|
namespace {
|
|
|
|
|
2018-03-03 06:21:28 +08:00
|
|
|
using common::make_unique;
|
2018-03-02 18:38:35 +08:00
|
|
|
|
2018-03-03 06:21:28 +08:00
|
|
|
const common::Duration kPopTimeout = common::FromMilliseconds(100);
|
2018-01-17 19:01:29 +08:00
|
|
|
|
2018-03-02 18:38:35 +08:00
|
|
|
class LocalTrajectoryUploader : public LocalTrajectoryUploaderInterface {
|
|
|
|
public:
|
|
|
|
LocalTrajectoryUploader(const std::string &uplink_server_address)
|
2018-03-03 06:21:28 +08:00
|
|
|
: client_channel_(::grpc::CreateChannel(
|
|
|
|
uplink_server_address, ::grpc::InsecureChannelCredentials())) {}
|
2018-03-02 18:38:35 +08:00
|
|
|
~LocalTrajectoryUploader();
|
|
|
|
|
|
|
|
// Starts the upload thread.
|
|
|
|
void Start() final;
|
|
|
|
|
|
|
|
// Shuts down the upload thread. This method blocks until the shutdown is
|
|
|
|
// complete.
|
|
|
|
void Shutdown() final;
|
|
|
|
|
|
|
|
void AddTrajectory(
|
|
|
|
int local_trajectory_id, const std::set<SensorId> &expected_sensor_ids,
|
2018-03-03 06:21:28 +08:00
|
|
|
const mapping::proto::TrajectoryBuilderOptions &trajectory_options) final;
|
2018-03-02 18:38:35 +08:00
|
|
|
void FinishTrajectory(int local_trajectory_id) final;
|
|
|
|
void EnqueueDataRequest(
|
|
|
|
std::unique_ptr<google::protobuf::Message> data_request) final;
|
2018-01-11 17:19:37 +08:00
|
|
|
|
2018-03-02 18:38:35 +08:00
|
|
|
SensorId GetLocalSlamResultSensorId(int local_trajectory_id) const final {
|
|
|
|
return SensorId{SensorId::SensorType::LOCAL_SLAM_RESULT,
|
|
|
|
"local_slam_result_" + std::to_string(local_trajectory_id)};
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void ProcessSendQueue();
|
|
|
|
void TranslateTrajectoryId(proto::SensorMetadata *sensor_metadata);
|
|
|
|
void ProcessFixedFramePoseDataMessage(
|
|
|
|
proto::AddFixedFramePoseDataRequest *data_request);
|
|
|
|
void ProcessImuDataMessage(proto::AddImuDataRequest *data_request);
|
|
|
|
void ProcessLocalSlamResultDataMessage(
|
|
|
|
proto::AddLocalSlamResultDataRequest *data_request);
|
|
|
|
void ProcessOdometryDataMessage(proto::AddOdometryDataRequest *data_request);
|
|
|
|
void ProcessLandmarkDataMessage(proto::AddLandmarkDataRequest *data_request);
|
|
|
|
|
2018-03-03 06:21:28 +08:00
|
|
|
std::shared_ptr<::grpc::Channel> client_channel_;
|
2018-03-02 18:38:35 +08:00
|
|
|
std::map<int, int> local_to_cloud_trajectory_id_map_;
|
2018-03-03 06:21:28 +08:00
|
|
|
common::BlockingQueue<std::unique_ptr<google::protobuf::Message>> send_queue_;
|
2018-03-02 18:38:35 +08:00
|
|
|
bool shutting_down_ = false;
|
|
|
|
std::unique_ptr<std::thread> upload_thread_;
|
|
|
|
std::unique_ptr<framework::Client<handlers::AddFixedFramePoseDataHandler>>
|
|
|
|
add_fixed_frame_pose_client_;
|
|
|
|
std::unique_ptr<framework::Client<handlers::AddImuDataHandler>>
|
|
|
|
add_imu_client_;
|
|
|
|
std::unique_ptr<framework::Client<handlers::AddLocalSlamResultDataHandler>>
|
|
|
|
add_local_slam_result_client_;
|
|
|
|
std::unique_ptr<framework::Client<handlers::AddOdometryDataHandler>>
|
|
|
|
add_odometry_client_;
|
|
|
|
std::unique_ptr<framework::Client<handlers::AddLandmarkDataHandler>>
|
|
|
|
add_landmark_client_;
|
|
|
|
};
|
2018-01-13 06:16:29 +08:00
|
|
|
|
2018-01-17 19:01:29 +08:00
|
|
|
LocalTrajectoryUploader::~LocalTrajectoryUploader() {
|
2018-02-02 00:19:15 +08:00
|
|
|
if (add_imu_client_) {
|
|
|
|
CHECK(add_imu_client_->WritesDone());
|
|
|
|
CHECK(add_imu_client_->Finish().ok());
|
|
|
|
}
|
|
|
|
if (add_odometry_client_) {
|
|
|
|
CHECK(add_odometry_client_->WritesDone());
|
|
|
|
CHECK(add_odometry_client_->Finish().ok());
|
2018-01-17 19:01:29 +08:00
|
|
|
}
|
2018-02-02 00:19:15 +08:00
|
|
|
if (add_fixed_frame_pose_client_) {
|
|
|
|
CHECK(add_fixed_frame_pose_client_->WritesDone());
|
|
|
|
CHECK(add_fixed_frame_pose_client_->Finish().ok());
|
2018-01-17 19:01:29 +08:00
|
|
|
}
|
2018-02-02 00:19:15 +08:00
|
|
|
if (add_local_slam_result_client_) {
|
|
|
|
CHECK(add_local_slam_result_client_->WritesDone());
|
|
|
|
CHECK(add_local_slam_result_client_->Finish().ok());
|
|
|
|
}
|
|
|
|
if (add_landmark_client_) {
|
|
|
|
CHECK(add_landmark_client_->WritesDone());
|
|
|
|
CHECK(add_landmark_client_->Finish().ok());
|
2018-01-17 19:01:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalTrajectoryUploader::Start() {
|
|
|
|
CHECK(!shutting_down_);
|
|
|
|
CHECK(!upload_thread_);
|
2018-03-02 18:38:35 +08:00
|
|
|
upload_thread_ =
|
|
|
|
make_unique<std::thread>([this]() { this->ProcessSendQueue(); });
|
2018-01-17 19:01:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocalTrajectoryUploader::Shutdown() {
|
|
|
|
CHECK(!shutting_down_);
|
|
|
|
CHECK(upload_thread_);
|
|
|
|
shutting_down_ = true;
|
|
|
|
upload_thread_->join();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalTrajectoryUploader::ProcessSendQueue() {
|
|
|
|
LOG(INFO) << "Starting uploader thread.";
|
|
|
|
while (!shutting_down_) {
|
|
|
|
auto data_message = send_queue_.PopWithTimeout(kPopTimeout);
|
|
|
|
if (data_message) {
|
2018-01-29 21:02:33 +08:00
|
|
|
if (auto *fixed_frame_pose_data =
|
|
|
|
dynamic_cast<proto::AddFixedFramePoseDataRequest *>(
|
2018-01-17 19:01:29 +08:00
|
|
|
data_message.get())) {
|
|
|
|
ProcessFixedFramePoseDataMessage(fixed_frame_pose_data);
|
2018-01-29 21:02:33 +08:00
|
|
|
} else if (auto *imu_data = dynamic_cast<proto::AddImuDataRequest *>(
|
|
|
|
data_message.get())) {
|
2018-01-17 19:01:29 +08:00
|
|
|
ProcessImuDataMessage(imu_data);
|
2018-01-29 21:02:33 +08:00
|
|
|
} else if (auto *odometry_data =
|
|
|
|
dynamic_cast<proto::AddOdometryDataRequest *>(
|
2018-01-17 19:01:29 +08:00
|
|
|
data_message.get())) {
|
|
|
|
ProcessOdometryDataMessage(odometry_data);
|
2018-01-29 21:02:33 +08:00
|
|
|
} else if (auto *local_slam_result_data =
|
|
|
|
dynamic_cast<proto::AddLocalSlamResultDataRequest *>(
|
|
|
|
data_message.get())) {
|
|
|
|
ProcessLocalSlamResultDataMessage(local_slam_result_data);
|
2018-02-02 00:19:15 +08:00
|
|
|
} else if (auto *landmark_data =
|
|
|
|
dynamic_cast<proto::AddLandmarkDataRequest *>(
|
|
|
|
data_message.get())) {
|
|
|
|
ProcessLandmarkDataMessage(landmark_data);
|
2018-01-17 19:01:29 +08:00
|
|
|
} else {
|
|
|
|
LOG(FATAL) << "Unknown message type: " << data_message->GetTypeName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 21:02:33 +08:00
|
|
|
void LocalTrajectoryUploader::TranslateTrajectoryId(
|
|
|
|
proto::SensorMetadata *sensor_metadata) {
|
|
|
|
int cloud_trajectory_id =
|
|
|
|
local_to_cloud_trajectory_id_map_.at(sensor_metadata->trajectory_id());
|
|
|
|
sensor_metadata->set_trajectory_id(cloud_trajectory_id);
|
|
|
|
}
|
|
|
|
|
2018-01-17 19:01:29 +08:00
|
|
|
void LocalTrajectoryUploader::ProcessFixedFramePoseDataMessage(
|
2018-01-29 21:02:33 +08:00
|
|
|
proto::AddFixedFramePoseDataRequest *data_request) {
|
2018-02-02 00:19:15 +08:00
|
|
|
if (!add_fixed_frame_pose_client_) {
|
2018-03-02 18:38:35 +08:00
|
|
|
add_fixed_frame_pose_client_ =
|
|
|
|
make_unique<framework::Client<handlers::AddFixedFramePoseDataHandler>>(
|
|
|
|
client_channel_);
|
2018-01-17 19:01:29 +08:00
|
|
|
}
|
2018-01-29 21:02:33 +08:00
|
|
|
TranslateTrajectoryId(data_request->mutable_sensor_metadata());
|
2018-02-02 00:19:15 +08:00
|
|
|
CHECK(add_fixed_frame_pose_client_->Write(*data_request));
|
2018-01-17 19:01:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocalTrajectoryUploader::ProcessImuDataMessage(
|
2018-01-29 21:02:33 +08:00
|
|
|
proto::AddImuDataRequest *data_request) {
|
2018-02-02 00:19:15 +08:00
|
|
|
if (!add_imu_client_) {
|
2018-03-02 18:38:35 +08:00
|
|
|
add_imu_client_ =
|
|
|
|
make_unique<framework::Client<handlers::AddImuDataHandler>>(
|
|
|
|
client_channel_);
|
2018-01-17 19:01:29 +08:00
|
|
|
}
|
2018-01-29 21:02:33 +08:00
|
|
|
TranslateTrajectoryId(data_request->mutable_sensor_metadata());
|
2018-02-02 00:19:15 +08:00
|
|
|
CHECK(add_imu_client_->Write(*data_request));
|
2018-01-17 19:01:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocalTrajectoryUploader::ProcessOdometryDataMessage(
|
2018-01-29 21:02:33 +08:00
|
|
|
proto::AddOdometryDataRequest *data_request) {
|
2018-02-02 00:19:15 +08:00
|
|
|
if (!add_odometry_client_) {
|
2018-03-02 18:38:35 +08:00
|
|
|
add_odometry_client_ =
|
|
|
|
make_unique<framework::Client<handlers::AddOdometryDataHandler>>(
|
|
|
|
client_channel_);
|
2018-02-02 00:19:15 +08:00
|
|
|
}
|
|
|
|
TranslateTrajectoryId(data_request->mutable_sensor_metadata());
|
|
|
|
CHECK(add_odometry_client_->Write(*data_request));
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalTrajectoryUploader::ProcessLandmarkDataMessage(
|
|
|
|
proto::AddLandmarkDataRequest *data_request) {
|
|
|
|
if (!add_landmark_client_) {
|
2018-03-02 18:38:35 +08:00
|
|
|
add_landmark_client_ =
|
|
|
|
make_unique<framework::Client<handlers::AddLandmarkDataHandler>>(
|
|
|
|
client_channel_);
|
2018-01-17 19:01:29 +08:00
|
|
|
}
|
2018-01-29 21:02:33 +08:00
|
|
|
TranslateTrajectoryId(data_request->mutable_sensor_metadata());
|
2018-02-02 00:19:15 +08:00
|
|
|
CHECK(add_landmark_client_->Write(*data_request));
|
2018-01-17 19:01:29 +08:00
|
|
|
}
|
|
|
|
|
2018-01-29 21:02:33 +08:00
|
|
|
void LocalTrajectoryUploader::ProcessLocalSlamResultDataMessage(
|
|
|
|
proto::AddLocalSlamResultDataRequest *data_request) {
|
2018-02-02 00:19:15 +08:00
|
|
|
if (!add_local_slam_result_client_) {
|
2018-03-02 18:38:35 +08:00
|
|
|
add_local_slam_result_client_ =
|
|
|
|
make_unique<framework::Client<handlers::AddLocalSlamResultDataHandler>>(
|
|
|
|
client_channel_);
|
2018-01-29 21:02:33 +08:00
|
|
|
}
|
|
|
|
TranslateTrajectoryId(data_request->mutable_sensor_metadata());
|
|
|
|
// A submap also holds a trajectory id that must be translated to uplink's
|
|
|
|
// trajectory id.
|
2018-03-03 06:21:28 +08:00
|
|
|
for (mapping::proto::Submap &mutable_submap :
|
2018-01-29 21:02:33 +08:00
|
|
|
*data_request->mutable_local_slam_result_data()->mutable_submaps()) {
|
|
|
|
mutable_submap.mutable_submap_id()->set_trajectory_id(
|
|
|
|
data_request->sensor_metadata().trajectory_id());
|
|
|
|
}
|
2018-02-02 00:19:15 +08:00
|
|
|
CHECK(add_local_slam_result_client_->Write(*data_request));
|
2018-01-29 21:02:33 +08:00
|
|
|
}
|
|
|
|
|
2018-01-15 22:31:33 +08:00
|
|
|
void LocalTrajectoryUploader::AddTrajectory(
|
2018-01-26 22:07:49 +08:00
|
|
|
int local_trajectory_id, const std::set<SensorId> &expected_sensor_ids,
|
2018-03-03 06:21:28 +08:00
|
|
|
const mapping::proto::TrajectoryBuilderOptions &trajectory_options) {
|
2018-01-13 06:16:29 +08:00
|
|
|
proto::AddTrajectoryRequest request;
|
|
|
|
*request.mutable_trajectory_builder_options() = trajectory_options;
|
2018-01-26 22:07:49 +08:00
|
|
|
for (const SensorId &sensor_id : expected_sensor_ids) {
|
2018-01-29 21:02:33 +08:00
|
|
|
// Range sensors are not forwarded, but combined into a LocalSlamResult.
|
|
|
|
if (sensor_id.type != SensorId::SensorType::RANGE) {
|
2018-03-03 06:21:28 +08:00
|
|
|
*request.add_expected_sensor_ids() = cloud::ToProto(sensor_id);
|
2018-01-29 21:02:33 +08:00
|
|
|
}
|
2018-01-13 06:16:29 +08:00
|
|
|
}
|
2018-01-29 21:02:33 +08:00
|
|
|
*request.add_expected_sensor_ids() =
|
2018-03-03 06:21:28 +08:00
|
|
|
cloud::ToProto(GetLocalSlamResultSensorId(local_trajectory_id));
|
2018-02-02 00:19:15 +08:00
|
|
|
framework::Client<handlers::AddTrajectoryHandler> client(client_channel_);
|
|
|
|
CHECK(client.Write(request));
|
2018-01-13 06:16:29 +08:00
|
|
|
CHECK_EQ(local_to_cloud_trajectory_id_map_.count(local_trajectory_id), 0);
|
|
|
|
local_to_cloud_trajectory_id_map_[local_trajectory_id] =
|
2018-02-02 00:19:15 +08:00
|
|
|
client.response().trajectory_id();
|
2018-01-13 06:16:29 +08:00
|
|
|
}
|
|
|
|
|
2018-01-15 22:31:33 +08:00
|
|
|
void LocalTrajectoryUploader::FinishTrajectory(int local_trajectory_id) {
|
2018-01-13 06:16:29 +08:00
|
|
|
CHECK_EQ(local_to_cloud_trajectory_id_map_.count(local_trajectory_id), 1);
|
|
|
|
int cloud_trajectory_id =
|
|
|
|
local_to_cloud_trajectory_id_map_[local_trajectory_id];
|
|
|
|
proto::FinishTrajectoryRequest request;
|
|
|
|
request.set_trajectory_id(cloud_trajectory_id);
|
2018-02-02 00:19:15 +08:00
|
|
|
framework::Client<handlers::FinishTrajectoryHandler> client(client_channel_);
|
|
|
|
CHECK(client.Write(request));
|
2018-01-13 06:16:29 +08:00
|
|
|
}
|
2018-01-11 17:19:37 +08:00
|
|
|
|
2018-01-15 22:31:33 +08:00
|
|
|
void LocalTrajectoryUploader::EnqueueDataRequest(
|
|
|
|
std::unique_ptr<google::protobuf::Message> data_request) {
|
|
|
|
send_queue_.Push(std::move(data_request));
|
|
|
|
}
|
|
|
|
|
2018-03-02 18:38:35 +08:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
std::unique_ptr<LocalTrajectoryUploaderInterface> CreateLocalTrajectoryUploader(
|
|
|
|
const std::string &uplink_server_address) {
|
|
|
|
return make_unique<LocalTrajectoryUploader>(uplink_server_address);
|
|
|
|
}
|
|
|
|
|
2018-03-03 06:21:28 +08:00
|
|
|
} // namespace cloud
|
|
|
|
} // namespace cartographer
|