17 #include "nav2_route/graph_saver.hpp"
18 #include "nav2_ros_common/tf2_factories.hpp"
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")
30 logger_ = node->get_logger();
34 graph_filepath_ = node->declare_or_get_parameter(
35 "graph_filepath", std::string(
""));
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));
48 plugin_type_ = nav2::get_plugin_type_param(node, graph_file_saver_id);
49 graph_file_saver_ = plugin_loader_.createSharedInstance((plugin_type_));
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) {
57 "Failed to create GraphFileSaver. Exception: %s", ex.what());
66 if (filepath.empty() && !graph_filepath_.empty()) {
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()) {
75 "The graph filepath was not provided and no default was specified. "
76 "Failed to save the route graph.");
82 "Saving graph file from %s, by parser %s", filepath.c_str(), plugin_type_.c_str());
84 if (!graph_file_saver_->saveGraphToFile(graph, filepath)) {
91 "Failed to transform nodes graph file (%s) to %s!", filepath.c_str(), route_frame_.c_str());
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_) {
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);
114 if (!got_transform) {
117 "Could not get transform from node frame %s to route frame %s",
118 node_frame.c_str(), route_frame_.c_str());
122 cached_transforms.insert({node_frame, tf_transform});
125 tf2::Vector3 graph_coord(
130 tf2::Vector3 new_coord = cached_transforms[node_frame] * graph_coord;
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_;
GraphSaver(nav2::LifecycleNode::SharedPtr node, nav2::TransformBuffer::SharedPtr tf, const std::string frame)
A constructor for nav2_route::GraphSaver.
bool saveGraphToFile(Graph &graph, std::string filepath="")
Saves a graph object to a file.
bool transformGraph(Graph &graph)
Transform the coordinates in the graph to the route frame.