node_grpc_main.cc
Go to the documentation of this file.
00001 /*
00002  * Copyright 2017 The Cartographer Authors
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "absl/memory/memory.h"
00018 #include "cartographer/cloud/client/map_builder_stub.h"
00019 #include "cartographer_ros/node.h"
00020 #include "cartographer_ros/node_options.h"
00021 #include "cartographer_ros/ros_log_sink.h"
00022 #include "gflags/gflags.h"
00023 #include "tf2_ros/transform_listener.h"
00024 
00025 DEFINE_bool(collect_metrics, false,
00026             "Activates the collection of runtime metrics. If activated, the "
00027             "metrics can be accessed via a ROS service.");
00028 DEFINE_string(configuration_directory, "",
00029               "First directory in which configuration files are searched, "
00030               "second is always the Cartographer installation to allow "
00031               "including files from there.");
00032 DEFINE_string(configuration_basename, "",
00033               "Basename, i.e. not containing any directory prefix, of the "
00034               "configuration file.");
00035 DEFINE_string(server_address, "localhost:50051",
00036               "gRPC server address to stream the sensor data to.");
00037 DEFINE_bool(
00038     start_trajectory_with_default_topics, true,
00039     "Enable to immediately start the first trajectory with default topics.");
00040 DEFINE_string(
00041     save_map_filename, "",
00042     "If non-empty, serialize state and write it to disk before shutting down.");
00043 DEFINE_string(load_state_filename, "",
00044               "If non-empty, filename of a .pbstream file "
00045               "to load, containing a saved SLAM state. "
00046               "Unless --upload_load_state_file is set, the filepath refers "
00047               "to the gRPC server's file system.");
00048 DEFINE_bool(load_frozen_state, true,
00049             "Load the saved state as frozen (non-optimized) trajectories.");
00050 DEFINE_bool(upload_load_state_file, false,
00051             "Upload the .pbstream file from a local path to the (remote) gRPC "
00052             "server instead of loading it from the server file system.");
00053 DEFINE_string(client_id, "",
00054               "Cartographer client ID to use when connecting to the server.");
00055 
00056 namespace cartographer_ros {
00057 namespace {
00058 
00059 void Run() {
00060   constexpr double kTfBufferCacheTimeInSeconds = 10.;
00061   tf2_ros::Buffer tf_buffer{::ros::Duration(kTfBufferCacheTimeInSeconds)};
00062   tf2_ros::TransformListener tf(tf_buffer);
00063   NodeOptions node_options;
00064   TrajectoryOptions trajectory_options;
00065   std::tie(node_options, trajectory_options) =
00066       LoadOptions(FLAGS_configuration_directory, FLAGS_configuration_basename);
00067 
00068   auto map_builder = absl::make_unique<::cartographer::cloud::MapBuilderStub>(
00069       FLAGS_server_address, FLAGS_client_id);
00070 
00071   if (!FLAGS_load_state_filename.empty() && !FLAGS_upload_load_state_file) {
00072     map_builder->LoadStateFromFile(FLAGS_load_state_filename,
00073                                    FLAGS_load_frozen_state);
00074   }
00075 
00076   Node node(node_options, std::move(map_builder), &tf_buffer,
00077             FLAGS_collect_metrics);
00078 
00079   if (!FLAGS_load_state_filename.empty() && FLAGS_upload_load_state_file) {
00080     node.LoadState(FLAGS_load_state_filename, FLAGS_load_frozen_state);
00081   }
00082 
00083   if (FLAGS_start_trajectory_with_default_topics) {
00084     node.StartTrajectoryWithDefaultTopics(trajectory_options);
00085   }
00086 
00087   ::ros::spin();
00088 
00089   node.FinishAllTrajectories();
00090   node.RunFinalOptimization();
00091 
00092   if (!FLAGS_save_map_filename.empty()) {
00093     node.SerializeState(FLAGS_save_map_filename,
00094                         false /* include_unfinished_submaps */);
00095   }
00096 }
00097 
00098 }  // namespace
00099 }  // namespace cartographer_ros
00100 
00101 int main(int argc, char** argv) {
00102   google::InitGoogleLogging(argv[0]);
00103   google::ParseCommandLineFlags(&argc, &argv, true);
00104 
00105   CHECK(!FLAGS_configuration_directory.empty())
00106       << "-configuration_directory is missing.";
00107   CHECK(!FLAGS_configuration_basename.empty())
00108       << "-configuration_basename is missing.";
00109   CHECK(!FLAGS_client_id.empty()) << "-client_id is missing.";
00110 
00111   ::ros::init(argc, argv, "cartographer_grpc_node");
00112   ::ros::start();
00113 
00114   cartographer_ros::ScopedRosLogSink ros_log_sink;
00115   cartographer_ros::Run();
00116   ::ros::shutdown();
00117 }


cartographer_ros
Author(s): The Cartographer Authors
autogenerated on Wed Jul 10 2019 04:10:28