17 #include "nav2_route/graph_saver.hpp"
18 #include "ament_index_cpp/get_package_share_directory.hpp"
24 rclcpp_lifecycle::LifecycleNode::SharedPtr node,
25 std::shared_ptr<tf2_ros::Buffer> 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 nav2_util::declare_parameter_if_not_declared(
35 node,
"graph_filepath", rclcpp::ParameterValue(std::string(
"")));
36 graph_filepath_ = node->get_parameter(
"graph_filepath").as_string();
39 const std::string default_plugin_type =
"nav2_route::GeoJsonGraphFileSaver";
40 nav2_util::declare_parameter_if_not_declared(
41 node,
"graph_file_saver", rclcpp::ParameterValue(default_plugin_id_));
42 auto graph_file_saver_id = node->get_parameter(
"graph_file_saver").as_string();
43 if (graph_file_saver_id == default_plugin_id_) {
44 nav2_util::declare_parameter_if_not_declared(
45 node, default_plugin_id_ +
".plugin", rclcpp::ParameterValue(default_plugin_type));
50 plugin_type_ = nav2_util::get_plugin_type_param(node, graph_file_saver_id);
51 graph_file_saver_ = plugin_loader_.createSharedInstance((plugin_type_));
53 logger_,
"Created GraphFileSaver %s of type %s",
54 graph_file_saver_id.c_str(), plugin_type_.c_str());
55 graph_file_saver_->configure(node);
56 }
catch (pluginlib::PluginlibException & ex) {
59 "Failed to create GraphFileSaver. Exception: %s", ex.what());
68 if (filepath.empty() && !graph_filepath_.empty()) {
70 logger_,
"The graph filepath was not provided. "
71 "Setting to %s", graph_filepath_.c_str());
72 filepath = graph_filepath_;
73 }
else if (filepath.empty() && graph_filepath_.empty()) {
77 "The graph filepath was not provided and no default was specified. "
78 "Failed to save the route graph.");
84 "Saving graph file from %s, by parser %s", filepath.c_str(), plugin_type_.c_str());
86 if (!graph_file_saver_->saveGraphToFile(graph, filepath)) {
93 "Failed to transform nodes graph file (%s) to %s!", filepath.c_str(), route_frame_.c_str());
102 std::unordered_map<std::string, tf2::Transform> cached_transforms;
103 for (
auto & node : graph) {
104 std::string node_frame = node.coords.frame_id;
105 if (node_frame.empty() || node_frame == route_frame_) {
111 if (cached_transforms.find(node_frame) == cached_transforms.end()) {
112 tf2::Transform tf_transform;
113 bool got_transform = nav2_util::getTransform(
114 node_frame, route_frame_, tf2::durationFromSec(0.1), tf_, tf_transform);
116 if (!got_transform) {
119 "Could not get transform from node frame %s to route frame %s",
120 node_frame.c_str(), route_frame_.c_str());
124 cached_transforms.insert({node_frame, tf_transform});
127 tf2::Vector3 graph_coord(
132 tf2::Vector3 new_coord = cached_transforms[node_frame] * graph_coord;
134 node.coords.x =
static_cast<float>(new_coord.x());
135 node.coords.y =
static_cast<float>(new_coord.y());
136 node.coords.frame_id = route_frame_;
GraphSaver(rclcpp_lifecycle::LifecycleNode::SharedPtr node, std::shared_ptr< tf2_ros::Buffer > 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.