start_trajectory_main.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2016 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 
17 #include <string>
18 #include <vector>
19 
27 #include "cartographer_ros_msgs/StartTrajectory.h"
28 #include "cartographer_ros_msgs/StatusCode.h"
29 #include "cartographer_ros_msgs/TrajectoryOptions.h"
30 #include "gflags/gflags.h"
31 #include "ros/ros.h"
32 
33 DEFINE_string(configuration_directory, "",
34  "First directory in which configuration files are searched, "
35  "second is always the Cartographer installation to allow "
36  "including files from there.");
37 DEFINE_string(configuration_basename, "",
38  "Basename, i.e. not containing any directory prefix, of the "
39  "configuration file.");
40 
41 DEFINE_string(initial_pose, "", "Starting pose of a new trajectory");
42 
43 namespace cartographer_ros {
44 namespace {
45 
46 TrajectoryOptions LoadOptions() {
47  auto file_resolver = cartographer::common::make_unique<
49  std::vector<std::string>{FLAGS_configuration_directory});
50  const std::string code =
51  file_resolver->GetFileContentOrDie(FLAGS_configuration_basename);
52  auto lua_parameter_dictionary =
54  code, std::move(file_resolver));
55  if (!FLAGS_initial_pose.empty()) {
56  auto initial_trajectory_pose_file_resolver =
59  std::vector<std::string>{FLAGS_configuration_directory});
60  auto initial_trajectory_pose =
62  "return " + FLAGS_initial_pose,
63  std::move(initial_trajectory_pose_file_resolver));
64  return CreateTrajectoryOptions(lua_parameter_dictionary.get(),
65  initial_trajectory_pose.get());
66  } else {
67  return CreateTrajectoryOptions(lua_parameter_dictionary.get());
68  }
69 }
70 
71 bool Run() {
72  ros::NodeHandle node_handle;
73  ros::ServiceClient client =
74  node_handle.serviceClient<cartographer_ros_msgs::StartTrajectory>(
76  cartographer_ros_msgs::StartTrajectory srv;
77  srv.request.options = ToRosMessage(LoadOptions());
78  srv.request.topics.laser_scan_topic = node_handle.resolveName(
79  kLaserScanTopic, true /* apply topic remapping */);
80  srv.request.topics.multi_echo_laser_scan_topic =
81  node_handle.resolveName(kMultiEchoLaserScanTopic, true);
82  srv.request.topics.point_cloud2_topic =
83  node_handle.resolveName(kPointCloud2Topic, true);
84  srv.request.topics.imu_topic = node_handle.resolveName(kImuTopic, true);
85  srv.request.topics.odometry_topic =
86  node_handle.resolveName(kOdometryTopic, true);
87 
88  if (!client.call(srv)) {
89  LOG(ERROR) << "Failed to call " << kStartTrajectoryServiceName << ".";
90  return false;
91  }
92  if (srv.response.status.code != cartographer_ros_msgs::StatusCode::OK) {
93  LOG(ERROR) << "Error starting trajectory - message: '"
94  << srv.response.status.message
95  << "' (status code: " << std::to_string(srv.response.status.code)
96  << ").";
97  return false;
98  }
99  LOG(INFO) << "Started trajectory " << srv.response.trajectory_id;
100  return true;
101 }
102 
103 } // namespace
104 } // namespace cartographer_ros
105 
106 int main(int argc, char** argv) {
107  google::InitGoogleLogging(argv[0]);
108  google::SetUsageMessage(
109  "\n\n"
110  "Convenience tool around the start_trajectory service. This takes a Lua "
111  "file that is accepted by the node as well and starts a new trajectory "
112  "using its settings.\n");
113  google::ParseCommandLineFlags(&argc, &argv, true);
114 
115  CHECK(!FLAGS_configuration_directory.empty())
116  << "-configuration_directory is missing.";
117  CHECK(!FLAGS_configuration_basename.empty())
118  << "-configuration_basename is missing.";
119 
120  ::ros::init(argc, argv, "cartographer_start_trajectory");
121  ::ros::start();
122 
124  int exit_code = cartographer_ros::Run() ? 0 : 1;
125  ::ros::shutdown();
126  return exit_code;
127 }
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
_Unique_if< T >::_Single_object make_unique(Args &&... args)
bool call(MReq &req, MRes &res)
constexpr char kImuTopic[]
constexpr char kLaserScanTopic[]
std::tuple< NodeOptions, TrajectoryOptions > LoadOptions(const std::string &configuration_directory, const std::string &configuration_basename)
Definition: node_options.cc:46
std::string resolveName(const std::string &name, bool remap=true) const
static std::unique_ptr< LuaParameterDictionary > NonReferenceCounted(const std::string &code, std::unique_ptr< FileResolver > file_resolver)
std::string GetFileContentOrDie(const std::string &basename) override
constexpr char kStartTrajectoryServiceName[]
constexpr char kOdometryTopic[]
TrajectoryOptions CreateTrajectoryOptions(::cartographer::common::LuaParameterDictionary *const lua_parameter_dictionary)
constexpr char kMultiEchoLaserScanTopic[]
void Run(const std::string &configuration_directory, const std::string &configuration_basename)
constexpr char kPointCloud2Topic[]
int main(int argc, char **argv)
DEFINE_string(configuration_directory, "", "First directory in which configuration files are searched, " "second is always the Cartographer installation to allow " "including files from there.")
cartographer_ros_msgs::TrajectoryOptions ToRosMessage(const TrajectoryOptions &options)


cartographer_ros
Author(s): The Cartographer Authors
autogenerated on Mon Feb 28 2022 22:06:05