Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
graph_saver.cpp
1 // Copyright (c) 2024 John Chrosniak
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <memory>
16 
17 #include "nav2_route/graph_saver.hpp"
18 #include "nav2_ros_common/tf2_factories.hpp"
19 
20 namespace nav2_route
21 {
22 
24  nav2::LifecycleNode::SharedPtr node,
25  nav2::TransformBuffer::SharedPtr tf,
26  const std::string frame)
27 : plugin_loader_("nav2_route", "nav2_route::GraphFileSaver"),
28  default_plugin_id_("GeoJsonGraphFileSaver")
29 {
30  logger_ = node->get_logger();
31  tf_ = tf;
32  route_frame_ = frame;
33 
34  graph_filepath_ = node->declare_or_get_parameter(
35  "graph_filepath", std::string(""));
36 
37  // Default Graph Parser
38  const std::string default_plugin_type = "nav2_route::GeoJsonGraphFileSaver";
39  auto graph_file_saver_id = node->declare_or_get_parameter(
40  "graph_file_saver", default_plugin_id_);
41  if (graph_file_saver_id == default_plugin_id_) {
42  nav2::declare_parameter_if_not_declared(
43  node, default_plugin_id_ + ".plugin", rclcpp::ParameterValue(default_plugin_type));
44  }
45 
46  // Create graph file saver plugin
47  try {
48  plugin_type_ = nav2::get_plugin_type_param(node, graph_file_saver_id);
49  graph_file_saver_ = plugin_loader_.createSharedInstance((plugin_type_));
50  RCLCPP_INFO(
51  logger_, "Created GraphFileSaver %s of type %s",
52  graph_file_saver_id.c_str(), plugin_type_.c_str());
53  graph_file_saver_->configure(node);
54  } catch (pluginlib::PluginlibException & ex) {
55  RCLCPP_FATAL(
56  logger_,
57  "Failed to create GraphFileSaver. Exception: %s", ex.what());
58  throw ex;
59  }
60 }
61 
63  Graph & graph,
64  std::string filepath)
65 {
66  if (filepath.empty() && !graph_filepath_.empty()) {
67  RCLCPP_DEBUG(
68  logger_, "The graph filepath was not provided. "
69  "Setting to %s", graph_filepath_.c_str());
70  filepath = graph_filepath_;
71  } else if (filepath.empty() && graph_filepath_.empty()) {
72  // No graph to try to save
73  RCLCPP_WARN(
74  logger_,
75  "The graph filepath was not provided and no default was specified. "
76  "Failed to save the route graph.");
77  return false;
78  }
79 
80  RCLCPP_INFO(
81  logger_,
82  "Saving graph file from %s, by parser %s", filepath.c_str(), plugin_type_.c_str());
83 
84  if (!graph_file_saver_->saveGraphToFile(graph, filepath)) {
85  return false;
86  }
87 
88  if (!transformGraph(graph)) {
89  RCLCPP_WARN(
90  logger_,
91  "Failed to transform nodes graph file (%s) to %s!", filepath.c_str(), route_frame_.c_str());
92  return false;
93  }
94 
95  return true;
96 }
97 
98 bool GraphSaver::transformGraph(Graph & graph)
99 {
100  std::unordered_map<std::string, tf2::Transform> cached_transforms;
101  for (auto & node : graph) {
102  std::string node_frame = node.coords.frame_id;
103  if (node_frame.empty() || node_frame == route_frame_) {
104  // If there is no frame provided or the frame of the node is the same as the route graph
105  // then no transform is required
106  continue;
107  }
108  // Find the transform to convert node from node frame to route frame
109  if (cached_transforms.find(node_frame) == cached_transforms.end()) {
110  tf2::Transform tf_transform;
111  bool got_transform = nav2_util::getTransform(
112  node_frame, route_frame_, tf2::durationFromSec(0.1), tf_, tf_transform);
113 
114  if (!got_transform) {
115  RCLCPP_WARN(
116  logger_,
117  "Could not get transform from node frame %s to route frame %s",
118  node_frame.c_str(), route_frame_.c_str());
119  return false;
120  }
121 
122  cached_transforms.insert({node_frame, tf_transform});
123  }
124  // Apply the transform
125  tf2::Vector3 graph_coord(
126  node.coords.x,
127  node.coords.y,
128  0.0);
129 
130  tf2::Vector3 new_coord = cached_transforms[node_frame] * graph_coord;
131 
132  node.coords.x = static_cast<float>(new_coord.x());
133  node.coords.y = static_cast<float>(new_coord.y());
134  node.coords.frame_id = route_frame_;
135  }
136 
137  return true;
138 }
139 
140 } // namespace nav2_route
GraphSaver(nav2::LifecycleNode::SharedPtr node, nav2::TransformBuffer::SharedPtr tf, const std::string frame)
A constructor for nav2_route::GraphSaver.
Definition: graph_saver.cpp:23
bool saveGraphToFile(Graph &graph, std::string filepath="")
Saves a graph object to a file.
Definition: graph_saver.cpp:62
bool transformGraph(Graph &graph)
Transform the coordinates in the graph to the route frame.
Definition: graph_saver.cpp:98