17 #include "nav2_route/graph_loader.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::GraphFileLoader"),
28 default_plugin_id_(
"GeoJsonGraphFileLoader")
30 logger_ = node->get_logger();
34 graph_filepath_ = node->declare_or_get_parameter(
35 "graph_filepath", std::string(
""));
38 auto graph_file_loader_id = node->declare_or_get_parameter(
39 "graph_file_loader", default_plugin_id_);
40 if (graph_file_loader_id == default_plugin_id_) {
41 nav2::declare_parameter_if_not_declared(
42 node, default_plugin_id_ +
".plugin",
43 rclcpp::ParameterValue(
"nav2_route::GeoJsonGraphFileLoader"));
48 plugin_type_ = nav2::get_plugin_type_param(node, graph_file_loader_id);
49 graph_file_loader_ = plugin_loader_.createSharedInstance((plugin_type_));
51 logger_,
"Created GraphFileLoader %s of type %s",
52 graph_file_loader_id.c_str(), plugin_type_.c_str());
53 graph_file_loader_->configure(node);
54 }
catch (pluginlib::PluginlibException & ex) {
57 "Failed to create GraphFileLoader. Exception: %s", ex.what());
64 GraphToIDMap & graph_to_id_map,
65 const std::string & filepath)
67 if (filepath.empty()) {
69 logger_,
"The graph filepath was not provided.");
75 "Loading graph file from %s, by parser %s", filepath.c_str(), plugin_type_.c_str());
77 if (!graph_file_loader_->loadGraphFromFile(graph, graph_to_id_map, filepath)) {
84 "Failed to transform nodes graph file (%s) to %s!", filepath.c_str(), route_frame_.c_str());
93 GraphToIDMap & graph_to_id_map)
95 if (graph_filepath_.empty()) {
96 RCLCPP_INFO(logger_,
"No graph file provided to load yet.");
102 "Loading graph file from %s, by parser %s", graph_filepath_.c_str(), plugin_type_.c_str());
104 if (!graph_file_loader_->loadGraphFromFile(graph, graph_to_id_map, graph_filepath_)) {
111 "Failed to transform nodes graph file (%s) to %s!",
112 graph_filepath_.c_str(), route_frame_.c_str());
121 std::unordered_map<std::string, tf2::Transform> cached_transforms;
122 for (
auto & node : graph) {
123 std::string node_frame = node.coords.frame_id;
124 if (node_frame.empty() || node_frame == route_frame_) {
128 if (cached_transforms.find(node_frame) == cached_transforms.end()) {
129 tf2::Transform tf_transform;
130 bool got_transform = nav2_util::getTransform(
131 node_frame, route_frame_, tf2::durationFromSec(0.1), tf_, tf_transform);
133 if (!got_transform) {
137 cached_transforms.insert({node_frame, tf_transform});
140 tf2::Vector3 graph_coord(
145 tf2::Vector3 new_coord = cached_transforms[node_frame] * graph_coord;
147 node.coords.x =
static_cast<float>(new_coord.x());
148 node.coords.y =
static_cast<float>(new_coord.y());
149 node.coords.frame_id = route_frame_;
bool transformGraph(Graph &graph)
Transform the coordinates in the graph to the route frame.
GraphLoader(nav2::LifecycleNode::SharedPtr node, nav2::TransformBuffer::SharedPtr tf, const std::string frame)
A constructor for nav2_route::GraphLoader.
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.