17 #include "nav2_route/graph_loader.hpp"
18 #include "ament_index_cpp/get_package_share_directory.hpp"
24 nav2_util::LifecycleNode::SharedPtr node,
25 std::shared_ptr<tf2_ros::Buffer> tf,
26 const std::string frame)
27 : plugin_loader_(
"nav2_route",
"nav2_route::GraphFileLoader"),
28 default_plugin_id_(
"GeoJsonGraphFileLoader")
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 nav2_util::declare_parameter_if_not_declared(
40 node,
"graph_file_loader", rclcpp::ParameterValue(default_plugin_id_));
41 auto graph_file_loader_id = node->get_parameter(
"graph_file_loader").as_string();
42 if (graph_file_loader_id == default_plugin_id_) {
43 nav2_util::declare_parameter_if_not_declared(
44 node, default_plugin_id_ +
".plugin",
45 rclcpp::ParameterValue(
"nav2_route::GeoJsonGraphFileLoader"));
50 plugin_type_ = nav2_util::get_plugin_type_param(node, graph_file_loader_id);
51 graph_file_loader_ = plugin_loader_.createSharedInstance((plugin_type_));
53 logger_,
"Created GraphFileLoader %s of type %s",
54 graph_file_loader_id.c_str(), plugin_type_.c_str());
55 graph_file_loader_->configure(node);
56 }
catch (pluginlib::PluginlibException & ex) {
59 "Failed to create GraphFileLoader. Exception: %s", ex.what());
66 GraphToIDMap & graph_to_id_map,
67 const std::string & filepath)
69 if (filepath.empty()) {
71 logger_,
"The graph filepath was not provided.");
77 "Loading graph file from %s, by parser %s", filepath.c_str(), plugin_type_.c_str());
79 if (!graph_file_loader_->loadGraphFromFile(graph, graph_to_id_map, filepath)) {
86 "Failed to transform nodes graph file (%s) to %s!", filepath.c_str(), route_frame_.c_str());
95 GraphToIDMap & graph_to_id_map)
97 if (graph_filepath_.empty()) {
98 RCLCPP_INFO(logger_,
"No graph file provided to load yet.");
104 "Loading graph file from %s, by parser %s", graph_filepath_.c_str(), plugin_type_.c_str());
106 if (!graph_file_loader_->loadGraphFromFile(graph, graph_to_id_map, graph_filepath_)) {
113 "Failed to transform nodes graph file (%s) to %s!",
114 graph_filepath_.c_str(), route_frame_.c_str());
123 std::unordered_map<std::string, tf2::Transform> cached_transforms;
124 for (
auto & node : graph) {
125 std::string node_frame = node.coords.frame_id;
126 if (node_frame.empty() || node_frame == route_frame_) {
130 if (cached_transforms.find(node_frame) == cached_transforms.end()) {
131 tf2::Transform tf_transform;
132 bool got_transform = nav2_util::getTransform(
133 node_frame, route_frame_, tf2::durationFromSec(0.1), tf_, tf_transform);
135 if (!got_transform) {
139 cached_transforms.insert({node_frame, tf_transform});
142 tf2::Vector3 graph_coord(
147 tf2::Vector3 new_coord = cached_transforms[node_frame] * graph_coord;
149 node.coords.x =
static_cast<float>(new_coord.x());
150 node.coords.y =
static_cast<float>(new_coord.y());
151 node.coords.frame_id = route_frame_;
bool transformGraph(Graph &graph)
Transform the coordinates in the graph to the route frame.
bool loadGraphFromFile(Graph &graph, GraphToIDMap &idx_map, const std::string &filepath)
Loads a graph object with file information from a filepath.
bool loadGraphFromParameter(Graph &graph, GraphToIDMap &idx_map)
Loads a graph object with file information from ROS parameter, if provided.
GraphLoader(nav2_util::LifecycleNode::SharedPtr node, std::shared_ptr< tf2_ros::Buffer > tf, const std::string frame)
A constructor for nav2_route::GraphLoader.