diff --git a/nav2_bt_navigator/src/navigators/navigate_through_poses.cpp b/nav2_bt_navigator/src/navigators/navigate_through_poses.cpp index 1144ab3d8b0..f3b76f764f3 100644 --- a/nav2_bt_navigator/src/navigators/navigate_through_poses.cpp +++ b/nav2_bt_navigator/src/navigators/navigate_through_poses.cpp @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include #include #include #include @@ -60,13 +61,13 @@ NavigateThroughPosesNavigator::getDefaultBTFilepath( nav2::LifecycleNode::WeakPtr parent_node) { auto node = parent_node.lock(); - std::string pkg_share_dir = - ament_index_cpp::get_package_share_directory("nav2_bt_navigator"); + std::filesystem::path pkg_share_dir; + ament_index_cpp::get_package_share_directory("nav2_bt_navigator", pkg_share_dir); auto default_bt_xml_filename = node->declare_or_get_parameter( "default_nav_through_poses_bt_xml", - pkg_share_dir + - "/behavior_trees/navigate_through_poses_w_replanning_and_recovery.xml"); + (pkg_share_dir / "behavior_trees" / + "navigate_through_poses_w_replanning_and_recovery.xml").string()); return default_bt_xml_filename; } diff --git a/nav2_bt_navigator/src/navigators/navigate_to_pose.cpp b/nav2_bt_navigator/src/navigators/navigate_to_pose.cpp index 866a1cd3ae8..13ccbf56a4c 100644 --- a/nav2_bt_navigator/src/navigators/navigate_to_pose.cpp +++ b/nav2_bt_navigator/src/navigators/navigate_to_pose.cpp @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include #include #include #include @@ -62,13 +63,13 @@ NavigateToPoseNavigator::getDefaultBTFilepath( nav2::LifecycleNode::WeakPtr parent_node) { auto node = parent_node.lock(); - std::string pkg_share_dir = - ament_index_cpp::get_package_share_directory("nav2_bt_navigator"); + std::filesystem::path pkg_share_dir; + ament_index_cpp::get_package_share_directory("nav2_bt_navigator", pkg_share_dir); auto default_bt_xml_filename = node->declare_or_get_parameter( "default_nav_to_pose_bt_xml", - pkg_share_dir + - "/behavior_trees/navigate_to_pose_w_replanning_and_recovery.xml"); + (pkg_share_dir / + "behavior_trees " / "navigate_to_pose_w_replanning_and_recovery.xml").string()); return default_bt_xml_filename; } diff --git a/nav2_core/include/nav2_core/behavior_tree_navigator.hpp b/nav2_core/include/nav2_core/behavior_tree_navigator.hpp index d3e9f24de6b..b534921fff8 100644 --- a/nav2_core/include/nav2_core/behavior_tree_navigator.hpp +++ b/nav2_core/include/nav2_core/behavior_tree_navigator.hpp @@ -15,11 +15,13 @@ #ifndef NAV2_CORE__BEHAVIOR_TREE_NAVIGATOR_HPP_ #define NAV2_CORE__BEHAVIOR_TREE_NAVIGATOR_HPP_ +#include #include #include #include #include +#include "ament_index_cpp/get_package_share_directory.hpp" #include "nav2_util/odometry_utils.hpp" #include "tf2_ros/buffer.hpp" #include "rclcpp/rclcpp.hpp" @@ -201,10 +203,13 @@ class BehaviorTreeNavigator : public NavigatorBase // get the default behavior tree for this navigator std::string default_bt_xml_filename = getDefaultBTFilepath(parent_node); + std::filesystem::path file_path; + ament_index_cpp::get_package_share_directory("nav2_bt_navigator", file_path); + file_path /= "behavior_trees"; + auto search_directories = node->declare_or_get_parameter( "bt_search_directories", - std::vector{ament_index_cpp::get_package_share_directory( - "nav2_bt_navigator") + "/behavior_trees"} + std::vector{file_path.string()} ); // Create the Behavior Tree Action Server for this navigator diff --git a/nav2_docking/opennav_docking/test/test_controller.cpp b/nav2_docking/opennav_docking/test/test_controller.cpp index 685715ab045..08f9886d925 100644 --- a/nav2_docking/opennav_docking/test/test_controller.cpp +++ b/nav2_docking/opennav_docking/test/test_controller.cpp @@ -22,7 +22,6 @@ #include "nav2_util/geometry_utils.hpp" #include "nav2_ros_common/node_utils.hpp" #include "tf2_ros/buffer.hpp" -#include "ament_index_cpp/get_package_share_directory.hpp" // Testing the controller at high level; the nav2_graceful_controller // Where the control law derives has over 98% test coverage diff --git a/nav2_docking/opennav_docking/test/test_dock_database.cpp b/nav2_docking/opennav_docking/test/test_dock_database.cpp index e2026f09df6..46d105d4db0 100644 --- a/nav2_docking/opennav_docking/test/test_dock_database.cpp +++ b/nav2_docking/opennav_docking/test/test_dock_database.cpp @@ -13,6 +13,7 @@ // limitations under the License. #include +#include #include "gtest/gtest.h" #include "rclcpp/rclcpp.hpp" #include "opennav_docking/dock_database.hpp" @@ -100,12 +101,14 @@ TEST(DatabaseTests, getDockInstancesBadConversionFile) "dockv1.plugin", rclcpp::ParameterValue("opennav_docking::SimpleChargingDock")); + std::filesystem::path file_path; + ament_index_cpp::get_package_share_directory("opennav_docking", file_path); + // Set a valid path with a malformed file node->declare_parameter( "dock_database", rclcpp::ParameterValue( - ament_index_cpp::get_package_share_directory("opennav_docking") + - "/dock_files/test_dock_bad_conversion_file.yaml")); + (file_path / "dock_files" / "test_dock_bad_conversion_file.yaml").string())); opennav_docking::DockDatabase db; db.initialize(node, nullptr); @@ -149,8 +152,10 @@ TEST(DatabaseTests, reloadDbService) node->create_client("test/reload_database"); auto request = std::make_shared(); - request->filepath = ament_index_cpp::get_package_share_directory("opennav_docking") + - "/dock_files/test_dock_file.yaml"; + std::filesystem::path filepath; + ament_index_cpp::get_package_share_directory("opennav_docking", filepath); + filepath = filepath / "dock_files" / "test_dock_file.yaml"; + request->filepath = filepath.string(); EXPECT_TRUE(client->wait_for_service(1s)); auto result = client->async_call(request); EXPECT_EQ( @@ -160,8 +165,9 @@ TEST(DatabaseTests, reloadDbService) // Try again with a bogus file auto request2 = std::make_shared(); - request2->filepath = ament_index_cpp::get_package_share_directory("opennav_docking") + - "/file_does_not_exist.yaml"; + ament_index_cpp::get_package_share_directory("opennav_docking", filepath); + filepath = filepath / "file_does_not_exist.yaml"; + request2->filepath = filepath.string(); EXPECT_TRUE(client->wait_for_service(1s)); auto result2 = client->async_call(request2); EXPECT_EQ( @@ -190,8 +196,10 @@ TEST(DatabaseTests, reloadDbMutexLocked) node->create_client("test/reload_database"); auto request = std::make_shared(); - request->filepath = ament_index_cpp::get_package_share_directory("opennav_docking") + - "/dock_files/test_dock_file.yaml"; + std::filesystem::path filepath; + ament_index_cpp::get_package_share_directory("opennav_docking", filepath); + filepath = filepath / "dock_files" / "test_dock_file.yaml"; + request->filepath = filepath.string(); EXPECT_TRUE(client->wait_for_service(1s)); auto result = client->async_call(request); EXPECT_EQ( diff --git a/nav2_docking/opennav_docking/test/test_navigator.cpp b/nav2_docking/opennav_docking/test/test_navigator.cpp index c5641c1c857..c33c502ca13 100644 --- a/nav2_docking/opennav_docking/test/test_navigator.cpp +++ b/nav2_docking/opennav_docking/test/test_navigator.cpp @@ -18,7 +18,6 @@ #include "rclcpp/rclcpp.hpp" #include "nav2_ros_common/simple_action_server.hpp" #include "opennav_docking/navigator.hpp" -#include "ament_index_cpp/get_package_share_directory.hpp" // Test navigator diff --git a/nav2_docking/opennav_docking/test/test_simple_charging_dock.cpp b/nav2_docking/opennav_docking/test/test_simple_charging_dock.cpp index 341c04b4d11..6521f9e0630 100644 --- a/nav2_docking/opennav_docking/test/test_simple_charging_dock.cpp +++ b/nav2_docking/opennav_docking/test/test_simple_charging_dock.cpp @@ -20,7 +20,6 @@ #include "sensor_msgs/msg/battery_state.hpp" #include "sensor_msgs/msg/joint_state.hpp" #include "opennav_docking/simple_charging_dock.hpp" -#include "ament_index_cpp/get_package_share_directory.hpp" #include "tf2_geometry_msgs/tf2_geometry_msgs.hpp" #include "tf2/utils.hpp" diff --git a/nav2_docking/opennav_docking/test/test_simple_non_charging_dock.cpp b/nav2_docking/opennav_docking/test/test_simple_non_charging_dock.cpp index 225a5e4642b..f8f8ec1b3bf 100644 --- a/nav2_docking/opennav_docking/test/test_simple_non_charging_dock.cpp +++ b/nav2_docking/opennav_docking/test/test_simple_non_charging_dock.cpp @@ -20,7 +20,6 @@ #include "sensor_msgs/msg/battery_state.hpp" #include "sensor_msgs/msg/joint_state.hpp" #include "opennav_docking/simple_non_charging_dock.hpp" -#include "ament_index_cpp/get_package_share_directory.hpp" #include "tf2_geometry_msgs/tf2_geometry_msgs.hpp" #include "tf2/utils.hpp" diff --git a/nav2_docking/opennav_docking/test/test_utils.cpp b/nav2_docking/opennav_docking/test/test_utils.cpp index e3607643a9b..a2d881d8347 100644 --- a/nav2_docking/opennav_docking/test/test_utils.cpp +++ b/nav2_docking/opennav_docking/test/test_utils.cpp @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include #include "gtest/gtest.h" #include "rclcpp/rclcpp.hpp" #include "opennav_docking/utils.hpp" @@ -91,9 +92,10 @@ TEST(UtilsTests, parseDockFile) { auto node = std::make_shared("test4"); DockMap db; - std::string filepath = ament_index_cpp::get_package_share_directory("opennav_docking") + - "/dock_files/test_dock_file.yaml"; - EXPECT_TRUE(utils::parseDockFile(filepath, node, db)); + std::filesystem::path filepath; + ament_index_cpp::get_package_share_directory("opennav_docking", filepath); + filepath = filepath / "dock_files" / "test_dock_file.yaml"; + EXPECT_TRUE(utils::parseDockFile(filepath.string(), node, db)); EXPECT_EQ(db.size(), 2u); EXPECT_EQ(db["dock1"].frame, std::string("mapA")); EXPECT_EQ(db["dock2"].frame, std::string("map")); @@ -115,24 +117,26 @@ TEST(UtilsTests, parseDockFile2) DockMap db; // Test with a file that has no docks - std::string filepath = ament_index_cpp::get_package_share_directory("opennav_docking") + - "/dock_files/test_no_docks_file.yaml"; - EXPECT_FALSE(utils::parseDockFile(filepath, node, db)); + std::filesystem::path filepath; + + ament_index_cpp::get_package_share_directory("opennav_docking", filepath); + filepath = filepath / "dock_files" / "test_no_docks_file.yaml"; + EXPECT_FALSE(utils::parseDockFile(filepath.string(), node, db)); // Test with a file that has no type - filepath = ament_index_cpp::get_package_share_directory("opennav_docking") + - "/dock_files/test_dock_no_type_file.yaml"; - EXPECT_FALSE(utils::parseDockFile(filepath, node, db)); + ament_index_cpp::get_package_share_directory("opennav_docking", filepath); + filepath = filepath / "dock_files" / "test_dock_no_type_file.yaml"; + + EXPECT_FALSE(utils::parseDockFile(filepath.string(), node, db)); // Test with a file that has no pose - filepath = ament_index_cpp::get_package_share_directory("opennav_docking") + - "/dock_files/test_dock_no_pose_file.yaml"; - EXPECT_FALSE(utils::parseDockFile(filepath, node, db)); + ament_index_cpp::get_package_share_directory("opennav_docking", filepath); + filepath = filepath / "dock_files" / "test_dock_no_pose_file.yaml"; // Test with a file that has wring pose array size - filepath = ament_index_cpp::get_package_share_directory("opennav_docking") + - "/dock_files/test_dock_bad_pose_file.yaml"; - EXPECT_FALSE(utils::parseDockFile(filepath, node, db)); + ament_index_cpp::get_package_share_directory("opennav_docking", filepath); + filepath = filepath / "dock_files" / "test_dock_bad_pose_file.yaml"; + EXPECT_FALSE(utils::parseDockFile(filepath.string(), node, db)); } TEST(UtilsTests, testgetDockPoseStamped) diff --git a/nav2_route/src/graph_loader.cpp b/nav2_route/src/graph_loader.cpp index 1c71012e866..e5a26ab0b72 100644 --- a/nav2_route/src/graph_loader.cpp +++ b/nav2_route/src/graph_loader.cpp @@ -15,7 +15,6 @@ #include #include "nav2_route/graph_loader.hpp" -#include "ament_index_cpp/get_package_share_directory.hpp" namespace nav2_route { diff --git a/nav2_route/src/graph_saver.cpp b/nav2_route/src/graph_saver.cpp index 664d51fd8ae..204f4077958 100644 --- a/nav2_route/src/graph_saver.cpp +++ b/nav2_route/src/graph_saver.cpp @@ -15,7 +15,6 @@ #include #include "nav2_route/graph_saver.hpp" -#include "ament_index_cpp/get_package_share_directory.hpp" namespace nav2_route { diff --git a/nav2_route/test/test_geojson_graph_file_loader.cpp b/nav2_route/test/test_geojson_graph_file_loader.cpp index 1205d71c6a7..292fa608a16 100644 --- a/nav2_route/test/test_geojson_graph_file_loader.cpp +++ b/nav2_route/test/test_geojson_graph_file_loader.cpp @@ -13,6 +13,7 @@ // limitations under the License. #include +#include #include #include #include @@ -348,13 +349,14 @@ TEST(GeoJsonGraphFileLoader, simple_graph) TEST(GeoJsonGraphFileLoader, sample_graph) { - auto file_path = ament_index_cpp::get_package_share_directory("nav2_route") + - "/graphs/sample_graph.geojson"; + std::filesystem::path file_path; + ament_index_cpp::get_package_share_directory("nav2_route", file_path); + file_path = file_path / "graphs" / "sample_graph.geojson"; Graph graph; GraphToIDMap graph_to_id_map; GeoJsonGraphFileLoader graph_file_loader; - bool result = graph_file_loader.loadGraphFromFile(graph, graph_to_id_map, file_path); + bool result = graph_file_loader.loadGraphFromFile(graph, graph_to_id_map, file_path.string()); EXPECT_TRUE(result); Metadata region; @@ -379,10 +381,11 @@ TEST(GeoJsonGraphFileLoader, sample_graph) TEST(GeoJsonGraphFileLoader, invalid_file) { - auto file_path = ament_index_cpp::get_package_share_directory("nav2_route") + - "/test/test_graphs/invalid.json"; + std::filesystem::path file_path; + ament_index_cpp::get_package_share_directory("nav2_route", file_path); + file_path = file_path / "test" / "test_graphs" / "invalid.json"; GeoJsonGraphFileLoader graph_file_loader; Graph graph; GraphToIDMap graph_to_id_map; - EXPECT_FALSE(graph_file_loader.loadGraphFromFile(graph, graph_to_id_map, file_path)); + EXPECT_FALSE(graph_file_loader.loadGraphFromFile(graph, graph_to_id_map, file_path.string())); } diff --git a/nav2_route/test/test_geojson_graph_file_saver.cpp b/nav2_route/test/test_geojson_graph_file_saver.cpp index 921a0439b2d..d999b8c31a4 100644 --- a/nav2_route/test/test_geojson_graph_file_saver.cpp +++ b/nav2_route/test/test_geojson_graph_file_saver.cpp @@ -13,6 +13,7 @@ // limitations under the License. #include +#include #include #include #include @@ -319,19 +320,20 @@ TEST(GeoJsonGraphFileSaver, simple_graph) TEST(GeoJsonGraphFileSaver, sample_graph) { - auto file_path = ament_index_cpp::get_package_share_directory("nav2_route") + - "/graphs/sample_graph.geojson"; + std::filesystem::path file_path; + ament_index_cpp::get_package_share_directory("nav2_route", file_path); + file_path = file_path / "graphs" / "sample_graph.geojson"; Graph graph; GraphToIDMap graph_to_id_map; GeoJsonGraphFileLoader graph_file_loader; - graph_file_loader.loadGraphFromFile(graph, graph_to_id_map, file_path); + graph_file_loader.loadGraphFromFile(graph, graph_to_id_map, file_path.string()); GeoJsonGraphFileSaver graph_file_saver; - bool result = graph_file_saver.saveGraphToFile(graph, file_path); + bool result = graph_file_saver.saveGraphToFile(graph, file_path.string()); EXPECT_TRUE(result); Graph graph2; GraphToIDMap graph_to_id_map2; - graph_file_loader.loadGraphFromFile(graph2, graph_to_id_map2, file_path); + graph_file_loader.loadGraphFromFile(graph2, graph_to_id_map2, file_path.string()); Metadata region; region = graph2[0].metadata.getValue("region", region); diff --git a/nav2_route/test/test_graph_loader.cpp b/nav2_route/test/test_graph_loader.cpp index 7255ae33904..716234d370f 100644 --- a/nav2_route/test/test_graph_loader.cpp +++ b/nav2_route/test/test_graph_loader.cpp @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. Reserved. +#include #include #include @@ -38,10 +39,11 @@ TEST(GraphLoader, test_invalid_plugin) auto tf = std::make_shared(node->get_clock()); std::string frame = "map"; + std::filesystem::path file_path; + ament_index_cpp::get_package_share_directory("nav2_route", file_path); + file_path = file_path / "graphs" / "aws_graph.geojson"; nav2::declare_parameter_if_not_declared( - node, "graph_filepath", rclcpp::ParameterValue( - ament_index_cpp::get_package_share_directory("nav2_route") + - "/graphs/aws_graph.geojson")); + node, "graph_filepath", rclcpp::ParameterValue(file_path.string())); // Set dummy parameter std::string default_plugin = "nav2_route::Dummy"; @@ -57,10 +59,11 @@ TEST(GraphLoader, test_api) auto tf = std::make_shared(node->get_clock()); std::string frame = "map"; + std::filesystem::path file_path; + ament_index_cpp::get_package_share_directory("nav2_route", file_path); + file_path = file_path / "graphs" / "aws_graph.geojson"; nav2::declare_parameter_if_not_declared( - node, "graph_filepath", rclcpp::ParameterValue( - ament_index_cpp::get_package_share_directory("nav2_route") + - "/graphs/aws_graph.geojson")); + node, "graph_filepath", rclcpp::ParameterValue(file_path.string())); GraphLoader graph_loader(node, tf, frame); @@ -82,22 +85,21 @@ TEST(GraphLoader, test_transformation_api) auto tf_broadcaster = std::make_shared(node); std::string frame = "map"; + std::filesystem::path file_path; + ament_index_cpp::get_package_share_directory("nav2_route", file_path); + file_path = file_path / "graphs" / "aws_graph.geojson"; nav2::declare_parameter_if_not_declared( - node, "graph_filepath", rclcpp::ParameterValue( - ament_index_cpp::get_package_share_directory("nav2_route") + - "/graphs/aws_graph.geojson")); + node, "graph_filepath", rclcpp::ParameterValue(file_path.string())); GraphLoader graph_loader(node, tf, frame); // Test with a file that now works Graph graph; GraphToIDMap graph_to_id_map; - std::string filepath; - filepath = - ament_index_cpp::get_package_share_directory("nav2_route") + - "/graphs/aws_graph.geojson"; - EXPECT_TRUE(graph_loader.loadGraphFromFile(graph, graph_to_id_map, filepath)); + ament_index_cpp::get_package_share_directory("nav2_route", file_path); + file_path = file_path / "graphs" / "aws_graph.geojson"; + EXPECT_TRUE(graph_loader.loadGraphFromFile(graph, graph_to_id_map, file_path.string())); // Test with another frame, should transform geometry_msgs::msg::TransformStamped transform; @@ -135,10 +137,12 @@ TEST(GraphLoader, test_transformation_api2) std::string frame = "map"; + std::filesystem::path file_path; + ament_index_cpp::get_package_share_directory("nav2_route", file_path); + file_path = file_path / "test" / "test_graphs" / "no_frame.json"; + nav2::declare_parameter_if_not_declared( - node, "graph_filepath", rclcpp::ParameterValue( - ament_index_cpp::get_package_share_directory("nav2_route") + - "/test/test_graphs/no_frame.json")); + node, "graph_filepath", rclcpp::ParameterValue(file_path.string())); GraphLoader graph_loader(node, tf, frame); @@ -146,7 +150,7 @@ TEST(GraphLoader, test_transformation_api2) Graph graph; GraphToIDMap graph_to_id_map; EXPECT_FALSE(graph_loader.loadGraphFromParameter(graph, graph_to_id_map)); - std::string filepath = ament_index_cpp::get_package_share_directory("nav2_route") + - "/test/test_graphs/no_frame.json"; - EXPECT_FALSE(graph_loader.loadGraphFromFile(graph, graph_to_id_map, filepath)); + ament_index_cpp::get_package_share_directory("nav2_route", file_path); + file_path = file_path / "test" / "test_graphs" / "no_frame.json"; + EXPECT_FALSE(graph_loader.loadGraphFromFile(graph, graph_to_id_map, file_path.string())); } diff --git a/nav2_route/test/test_graph_saver.cpp b/nav2_route/test/test_graph_saver.cpp index 49c3747ebac..9ba7456adae 100644 --- a/nav2_route/test/test_graph_saver.cpp +++ b/nav2_route/test/test_graph_saver.cpp @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. Reserved. +#include #include #include #include @@ -40,10 +41,12 @@ TEST(GraphSaver, test_invalid_plugin) auto tf = std::make_shared(node->get_clock()); std::string frame = "map"; + std::filesystem::path file_path; + ament_index_cpp::get_package_share_directory("nav2_route", file_path); + file_path = file_path / "graphs" / "aws_graph.geojson"; + nav2::declare_parameter_if_not_declared( - node, "graph_filepath", rclcpp::ParameterValue( - ament_index_cpp::get_package_share_directory("nav2_route") + - "/graphs/aws_graph.geojson")); + node, "graph_filepath", rclcpp::ParameterValue(file_path.string())); // Set dummy parameter std::string default_plugin = "nav2_route::Dummy"; @@ -59,10 +62,12 @@ TEST(GraphSaver, test_empty_filename) auto tf = std::make_shared(node->get_clock()); std::string frame = "map"; + std::filesystem::path filepath; + ament_index_cpp::get_package_share_directory("nav2_route", filepath); + filepath = filepath / "graphs" / "aws_graph.geojson"; + nav2::declare_parameter_if_not_declared( - node, "graph_filepath", rclcpp::ParameterValue( - ament_index_cpp::get_package_share_directory("nav2_route") + - "/graphs/aws_graph.geojson")); + node, "graph_filepath", rclcpp::ParameterValue(filepath.string())); GraphLoader graph_loader(node, tf, frame); GraphSaver graph_saver(node, tf, frame); @@ -80,10 +85,12 @@ TEST(GraphSaver, test_api) auto tf = std::make_shared(node->get_clock()); std::string frame = "map"; + std::filesystem::path filepath; + ament_index_cpp::get_package_share_directory("nav2_route", filepath); + filepath = filepath / "graphs" / "aws_graph.geojson"; + nav2::declare_parameter_if_not_declared( - node, "graph_filepath", rclcpp::ParameterValue( - ament_index_cpp::get_package_share_directory("nav2_route") + - "/graphs/aws_graph.geojson")); + node, "graph_filepath", rclcpp::ParameterValue(filepath.string())); GraphLoader graph_loader(node, tf, frame); GraphSaver graph_saver(node, tf, frame); @@ -119,21 +126,21 @@ TEST(GraphSaver, test_transformation_api) std::string frame = "map"; + std::filesystem::path file_path; + ament_index_cpp::get_package_share_directory("nav2_route", file_path); + file_path = file_path / "graphs" / "aws_graph.geojson"; + nav2::declare_parameter_if_not_declared( - node, "graph_filepath", rclcpp::ParameterValue( - ament_index_cpp::get_package_share_directory("nav2_route") + - "/graphs/aws_graph.geojson")); + node, "graph_filepath", rclcpp::ParameterValue(file_path.string())); GraphLoader graph_loader(node, tf, frame); // Test with a file that now works Graph graph; GraphToIDMap graph_to_id_map; - std::string filepath; - filepath = - ament_index_cpp::get_package_share_directory("nav2_route") + - "/graphs/aws_graph.geojson"; - graph_loader.loadGraphFromFile(graph, graph_to_id_map, filepath); + ament_index_cpp::get_package_share_directory("nav2_route", file_path); + file_path = file_path / "graphs" / "aws_graph.geojson"; + graph_loader.loadGraphFromFile(graph, graph_to_id_map, file_path.string()); // Test with another frame, should transform geometry_msgs::msg::TransformStamped transform; @@ -148,11 +155,11 @@ TEST(GraphSaver, test_transformation_api) executor.spin_all(std::chrono::milliseconds(50)); GraphSaver graph_saver(node, tf, frame); - std::string file_path = "test.geojson"; + file_path = "test.geojson"; graph[0].coords.frame_id = "map_test"; EXPECT_EQ(graph[0].coords.frame_id, "map_test"); double or_coord = graph[0].coords.x; - EXPECT_TRUE(graph_saver.saveGraphToFile(graph, file_path)); + EXPECT_TRUE(graph_saver.saveGraphToFile(graph, file_path.string())); EXPECT_EQ(graph[0].coords.frame_id, "map"); EXPECT_NE(graph[0].coords.x, or_coord); std::filesystem::remove(file_path); @@ -161,7 +168,7 @@ TEST(GraphSaver, test_transformation_api) graph[0].coords.frame_id = "map_test2"; EXPECT_EQ(graph[0].coords.frame_id, "map_test2"); or_coord = graph[0].coords.x; - EXPECT_FALSE(graph_saver.saveGraphToFile(graph, file_path)); + EXPECT_FALSE(graph_saver.saveGraphToFile(graph, file_path.string())); EXPECT_EQ(graph[0].coords.frame_id, "map_test2"); EXPECT_EQ(graph[0].coords.x, or_coord); } diff --git a/nav2_route/test/test_route_server.cpp b/nav2_route/test/test_route_server.cpp index 5fb73b4ce0f..bb35f6324ef 100644 --- a/nav2_route/test/test_route_server.cpp +++ b/nav2_route/test/test_route_server.cpp @@ -13,6 +13,7 @@ // limitations under the License. Reserved. #include +#include #include #include #include @@ -163,12 +164,13 @@ TEST(RouteServerTest, test_lifecycle) TEST(RouteServerTest, test_set_srv) { - std::string pkg_share_dir = ament_index_cpp::get_package_share_directory("nav2_route"); - std::string real_filepath = pkg_share_dir + "/graphs/aws_graph.geojson"; + std::filesystem::path pkg_share_dir; + ament_index_cpp::get_package_share_directory("nav2_route", pkg_share_dir); + std::filesystem::path real_filepath = pkg_share_dir / "graphs" / "aws_graph.geojson"; rclcpp::NodeOptions options; auto server = std::make_shared(options); - server->declare_parameter("graph_filepath", rclcpp::ParameterValue(real_filepath)); + server->declare_parameter("graph_filepath", rclcpp::ParameterValue(real_filepath.string())); auto node_thread = std::make_unique(server); auto node2 = std::make_shared("my_node2"); @@ -182,13 +184,15 @@ TEST(RouteServerTest, test_set_srv) EXPECT_FALSE(resp->success); auto req2 = std::make_shared(); - req2->graph_filepath = real_filepath; + req2->graph_filepath = real_filepath.string(); auto resp2 = srv_client.invoke(req2, std::chrono::nanoseconds(1000000000)); EXPECT_TRUE(resp2->success); auto req3 = std::make_shared(); - req3->graph_filepath = ament_index_cpp::get_package_share_directory("nav2_route") + - "/test/test_graphs/invalid.json"; + std::filesystem::path invalid_filepath; + ament_index_cpp::get_package_share_directory("nav2_route", invalid_filepath); + invalid_filepath = invalid_filepath / "test" / "test_graphs" / "invalid.json"; + req3->graph_filepath = invalid_filepath.string(); auto resp3 = srv_client.invoke(req3, std::chrono::nanoseconds(1000000000)); EXPECT_FALSE(resp3->success); @@ -278,12 +282,14 @@ TEST(RouteServerTest, test_request_valid) TEST(RouteServerTest, test_complete_action_api) { - std::string pkg_share_dir = ament_index_cpp::get_package_share_directory("nav2_route"); - std::string real_file = pkg_share_dir + "/graphs/aws_graph.geojson"; + std::filesystem::path pkg_share_dir; + + ament_index_cpp::get_package_share_directory("nav2_route", pkg_share_dir); + std::filesystem::path real_file = pkg_share_dir / "graphs" / "aws_graph.geojson"; rclcpp::NodeOptions options; auto server = std::make_shared(options); - server->declare_parameter("graph_filepath", rclcpp::ParameterValue(real_file)); + server->declare_parameter("graph_filepath", rclcpp::ParameterValue(real_file.string())); auto node_thread = std::make_unique(server); server->startup(); @@ -353,12 +359,13 @@ TEST(RouteServerTest, test_complete_action_api) TEST(RouteServerTest, test_error_codes) { - std::string pkg_share_dir = ament_index_cpp::get_package_share_directory("nav2_route"); - std::string real_file = pkg_share_dir + "/test/test_graphs/error_codes.geojson"; + std::filesystem::path pkg_share_dir; + ament_index_cpp::get_package_share_directory("nav2_route", pkg_share_dir); + std::filesystem::path real_file = pkg_share_dir / "test" / "test_graphs" / "error_codes.geojson"; rclcpp::NodeOptions options; auto server = std::make_shared(options); - server->declare_parameter("graph_filepath", rclcpp::ParameterValue(real_file)); + server->declare_parameter("graph_filepath", rclcpp::ParameterValue(real_file.string())); auto node_thread = std::make_unique(server); server->startup(); diff --git a/nav2_rviz_plugins/src/nav2_panel.cpp b/nav2_rviz_plugins/src/nav2_panel.cpp index dd006e9fe88..b97c567471e 100644 --- a/nav2_rviz_plugins/src/nav2_panel.cpp +++ b/nav2_rviz_plugins/src/nav2_panel.cpp @@ -32,7 +32,6 @@ #include "rclcpp/rclcpp.hpp" #include "rviz_common/display_context.hpp" #include "rviz_common/load_resource.hpp" -#include "ament_index_cpp/get_package_share_directory.hpp" #include "yaml-cpp/yaml.h" #include "geometry_msgs/msg/pose.hpp" diff --git a/nav2_smac_planner/src/smac_planner_lattice.cpp b/nav2_smac_planner/src/smac_planner_lattice.cpp index 413cb1c66f2..ff397cde14b 100644 --- a/nav2_smac_planner/src/smac_planner_lattice.cpp +++ b/nav2_smac_planner/src/smac_planner_lattice.cpp @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. Reserved. +#include #include #include #include @@ -83,10 +84,12 @@ void SmacPlannerLattice::configure( node->get_parameter(name + ".smooth_path", smooth_path); // Default to a well rounded model: 16 bin, 0.4m turning radius, ackermann model + std::filesystem::path filepath; + ament_index_cpp::get_package_share_directory("nav2_smac_planner", filepath); + filepath = filepath / "sample_primitives" / "5cm_resolution" / "0.5m_turning_radius" / + "ackermann" / "output.json"; nav2::declare_parameter_if_not_declared( - node, name + ".lattice_filepath", rclcpp::ParameterValue( - ament_index_cpp::get_package_share_directory("nav2_smac_planner") + - "/sample_primitives/5cm_resolution/0.5m_turning_radius/ackermann/output.json")); + node, name + ".lattice_filepath", rclcpp::ParameterValue(filepath.string())); node->get_parameter(name + ".lattice_filepath", _search_info.lattice_filepath); nav2::declare_parameter_if_not_declared( node, name + ".cache_obstacle_heuristic", rclcpp::ParameterValue(false)); diff --git a/nav2_smac_planner/test/test_a_star.cpp b/nav2_smac_planner/test/test_a_star.cpp index fe032738ba2..a9242204888 100644 --- a/nav2_smac_planner/test/test_a_star.cpp +++ b/nav2_smac_planner/test/test_a_star.cpp @@ -13,6 +13,7 @@ // limitations under the License. Reserved. #include +#include #include #include #include @@ -304,10 +305,11 @@ TEST(AStarTest, test_a_star_lattice) info.reverse_penalty = 2.0; info.retrospective_penalty = 0.1; info.analytic_expansion_ratio = 3.5; - info.lattice_filepath = - ament_index_cpp::get_package_share_directory("nav2_smac_planner") + - "/sample_primitives/5cm_resolution/0.5m_turning_radius/ackermann" + - "/output.json"; + std::filesystem::path filepath; + ament_index_cpp::get_package_share_directory("nav2_smac_planner", filepath); + filepath = filepath / "sample_primitives" / "5cm_resolution" / "0.5m_turning_radius" / + "ackermann" / "output.json"; + info.lattice_filepath = filepath.string(); info.minimum_turning_radius = 8; // in grid coordinates 0.4/0.05 info.analytic_expansion_max_length = 20.0; // in grid coordinates unsigned int size_theta = 16; diff --git a/nav2_smac_planner/test/test_nodelattice.cpp b/nav2_smac_planner/test/test_nodelattice.cpp index 93efec2fac1..1dd96e99462 100644 --- a/nav2_smac_planner/test/test_nodelattice.cpp +++ b/nav2_smac_planner/test/test_nodelattice.cpp @@ -13,6 +13,7 @@ // See the License for the specific language governing permissions and // limitations under the License. Reserved. +#include #include #include #include @@ -28,12 +29,13 @@ using json = nlohmann::json; TEST(NodeLatticeTest, parser_test) { - std::string pkg_share_dir = ament_index_cpp::get_package_share_directory("nav2_smac_planner"); - std::string filePath = - pkg_share_dir + - "/sample_primitives/5cm_resolution/0.5m_turning_radius/ackermann" + - "/output.json"; - std::ifstream myJsonFile(filePath); + std::filesystem::path pkg_share_dir; + ament_index_cpp::get_package_share_directory("nav2_smac_planner", pkg_share_dir); + std::filesystem::path filePath = + pkg_share_dir / + "sample_primitives" / "5cm_resolution" / "0.5m_turning_radius" / "ackermann" / + "output.json"; + std::ifstream myJsonFile(filePath.string()); ASSERT_TRUE(myJsonFile.is_open()); @@ -86,11 +88,12 @@ TEST(NodeLatticeTest, parser_test) TEST(NodeLatticeTest, test_node_lattice_neighbors_and_parsing) { - std::string pkg_share_dir = ament_index_cpp::get_package_share_directory("nav2_smac_planner"); - std::string filePath = - pkg_share_dir + - "/sample_primitives/5cm_resolution/0.5m_turning_radius/ackermann" + - "/output.json"; + std::filesystem::path pkg_share_dir; + ament_index_cpp::get_package_share_directory("nav2_smac_planner", pkg_share_dir); + std::filesystem::path filePath = + pkg_share_dir / + "sample_primitives" / "5cm_resolution" / "0.5m_turning_radius" / "ackermann" / + "output.json"; nav2_smac_planner::SearchInfo info; info.minimum_turning_radius = 1.1; @@ -100,7 +103,7 @@ TEST(NodeLatticeTest, test_node_lattice_neighbors_and_parsing) info.cost_penalty = 1; info.retrospective_penalty = 0.0; info.analytic_expansion_ratio = 1; - info.lattice_filepath = filePath; + info.lattice_filepath = filePath.string(); info.cache_obstacle_heuristic = true; info.allow_reverse_expansion = true; @@ -132,11 +135,12 @@ TEST(NodeLatticeTest, test_node_lattice_neighbors_and_parsing) TEST(NodeLatticeTest, test_node_lattice_conversions) { - std::string pkg_share_dir = ament_index_cpp::get_package_share_directory("nav2_smac_planner"); - std::string filePath = - pkg_share_dir + - "/sample_primitives/5cm_resolution/0.5m_turning_radius/ackermann" + - "/output.json"; + std::filesystem::path pkg_share_dir; + ament_index_cpp::get_package_share_directory("nav2_smac_planner", pkg_share_dir); + std::filesystem::path filePath = + pkg_share_dir / + "sample_primitives" / "5cm_resolution" / "0.5m_turning_radius" / "ackermann" / + "output.json"; nav2_smac_planner::SearchInfo info; info.minimum_turning_radius = 1.1; @@ -146,7 +150,7 @@ TEST(NodeLatticeTest, test_node_lattice_conversions) info.cost_penalty = 1; info.retrospective_penalty = 0.0; info.analytic_expansion_ratio = 1; - info.lattice_filepath = filePath; + info.lattice_filepath = filePath.string(); info.cache_obstacle_heuristic = true; unsigned int x = 100; @@ -171,11 +175,12 @@ TEST(NodeLatticeTest, test_node_lattice_conversions) TEST(NodeLatticeTest, test_node_lattice) { auto node = std::make_shared("test"); - std::string pkg_share_dir = ament_index_cpp::get_package_share_directory("nav2_smac_planner"); - std::string filePath = - pkg_share_dir + - "/sample_primitives/5cm_resolution/0.5m_turning_radius/ackermann" + - "/output.json"; + std::filesystem::path pkg_share_dir; + ament_index_cpp::get_package_share_directory("nav2_smac_planner", pkg_share_dir); + std::filesystem::path filePath = + pkg_share_dir / + "sample_primitives" / "5cm_resolution" / "0.5m_turning_radius" / "ackermann" / + "output.json"; nav2_smac_planner::SearchInfo info; info.minimum_turning_radius = 1.1; @@ -185,7 +190,7 @@ TEST(NodeLatticeTest, test_node_lattice) info.cost_penalty = 1; info.retrospective_penalty = 0.1; info.analytic_expansion_ratio = 1; - info.lattice_filepath = filePath; + info.lattice_filepath = filePath.string(); info.cache_obstacle_heuristic = true; info.allow_reverse_expansion = true; @@ -255,11 +260,12 @@ TEST(NodeLatticeTest, test_node_lattice) TEST(NodeLatticeTest, test_get_neighbors) { auto lnode = std::make_shared("test"); - std::string pkg_share_dir = ament_index_cpp::get_package_share_directory("nav2_smac_planner"); - std::string filePath = - pkg_share_dir + - "/sample_primitives/5cm_resolution/0.5m_turning_radius/ackermann" + - "/output.json"; + std::filesystem::path pkg_share_dir; + ament_index_cpp::get_package_share_directory("nav2_smac_planner", pkg_share_dir); + std::filesystem::path filePath = + pkg_share_dir / + "sample_primitives" / "5cm_resolution" / "0.5m_turning_radius" / "ackermann" / + "output.json"; nav2_smac_planner::SearchInfo info; info.minimum_turning_radius = 1.1; @@ -269,7 +275,7 @@ TEST(NodeLatticeTest, test_get_neighbors) info.cost_penalty = 1; info.analytic_expansion_ratio = 1; info.retrospective_penalty = 0.0; - info.lattice_filepath = filePath; + info.lattice_filepath = filePath.string(); info.cache_obstacle_heuristic = true; info.allow_reverse_expansion = true; @@ -313,11 +319,12 @@ TEST(NodeLatticeTest, test_get_neighbors) TEST(NodeLatticeTest, test_node_lattice_custom_footprint) { auto lnode = std::make_shared("test"); - std::string pkg_share_dir = ament_index_cpp::get_package_share_directory("nav2_smac_planner"); - std::string filePath = - pkg_share_dir + - "/sample_primitives/5cm_resolution/0.5m_turning_radius/ackermann" + - "/output.json"; + std::filesystem::path pkg_share_dir; + ament_index_cpp::get_package_share_directory("nav2_smac_planner", pkg_share_dir); + std::filesystem::path filePath = + pkg_share_dir / + "sample_primitives" / "5cm_resolution" / "0.5m_turning_radius" / "ackermann" / + "output.json"; nav2_smac_planner::SearchInfo info; info.minimum_turning_radius = 0.5; @@ -327,7 +334,7 @@ TEST(NodeLatticeTest, test_node_lattice_custom_footprint) info.cost_penalty = 1; info.retrospective_penalty = 0.1; info.analytic_expansion_ratio = 1; - info.lattice_filepath = filePath; + info.lattice_filepath = filePath.string(); info.cache_obstacle_heuristic = true; info.allow_reverse_expansion = true; diff --git a/nav2_smac_planner/test/test_smac_lattice.cpp b/nav2_smac_planner/test/test_smac_lattice.cpp index e8d0a38945f..2d985de73bf 100644 --- a/nav2_smac_planner/test/test_smac_lattice.cpp +++ b/nav2_smac_planner/test/test_smac_lattice.cpp @@ -13,6 +13,7 @@ // limitations under the License. Reserved. #include +#include #include #include #include @@ -216,12 +217,15 @@ TEST(SmacTest, test_smac_lattice_reconfigure) // of heading, test output includes number of heading 15 parameters.clear(); + std::filesystem::path pkg_dir; + ament_index_cpp::get_package_share_directory("nav2_smac_planner", pkg_dir); + std::filesystem::path filepath = pkg_dir / "sample_primitives" / "test" / "output.json"; + parameters.push_back(rclcpp::Parameter("test.coarse_search_resolution", 4)); parameters.push_back( rclcpp::Parameter( "test.lattice_filepath", - ament_index_cpp::get_package_share_directory("nav2_smac_planner") + - "/sample_primitives/test/output.json")); + filepath.string())); EXPECT_NO_THROW(planner->callDynamicParams(parameters)); EXPECT_EQ(planner->getCoarseSearchResolution(), 1); diff --git a/nav2_smac_planner/test/test_smoother.cpp b/nav2_smac_planner/test/test_smoother.cpp index b34246beae9..94bada76dc5 100644 --- a/nav2_smac_planner/test/test_smoother.cpp +++ b/nav2_smac_planner/test/test_smoother.cpp @@ -27,7 +27,6 @@ #include "nav2_smac_planner/a_star.hpp" #include "nav2_smac_planner/collision_checker.hpp" #include "nav2_smac_planner/smoother.hpp" -#include "ament_index_cpp/get_package_share_directory.hpp" using namespace nav2_smac_planner; // NOLINT diff --git a/nav2_smoother/test/test_savitzky_golay_smoother.cpp b/nav2_smoother/test/test_savitzky_golay_smoother.cpp index 2bea24ae26e..eb122d6a09b 100644 --- a/nav2_smoother/test/test_savitzky_golay_smoother.cpp +++ b/nav2_smoother/test/test_savitzky_golay_smoother.cpp @@ -28,7 +28,6 @@ #include "nav2_msgs/msg/costmap.hpp" #include "nav2_ros_common/lifecycle_node.hpp" #include "nav2_smoother/savitzky_golay_smoother.hpp" -#include "ament_index_cpp/get_package_share_directory.hpp" using namespace nav2_smoother; // NOLINT using namespace std::chrono_literals; // NOLINT diff --git a/nav2_system_tests/src/behavior_tree/test_behavior_tree_node.cpp b/nav2_system_tests/src/behavior_tree/test_behavior_tree_node.cpp index 8441d50cc57..54c826f7871 100644 --- a/nav2_system_tests/src/behavior_tree/test_behavior_tree_node.cpp +++ b/nav2_system_tests/src/behavior_tree/test_behavior_tree_node.cpp @@ -254,9 +254,9 @@ std::shared_ptr BehaviorTreeTestFixture::bt_handler = nullp TEST_F(BehaviorTreeTestFixture, TestBTXMLFiles) { // Get the BT root directory - const auto root_dir = std::filesystem::path( - ament_index_cpp::get_package_share_directory("nav2_bt_navigator") - ) / "behavior_trees"; + std::filesystem::path pkg_share_dir; + ament_index_cpp::get_package_share_directory("nav2_bt_navigator", pkg_share_dir); + const auto root_dir = pkg_share_dir / "behavior_trees"; ASSERT_TRUE(std::filesystem::exists(root_dir)); ASSERT_TRUE(std::filesystem::is_directory(root_dir)); @@ -670,9 +670,9 @@ TEST_F(BehaviorTreeTestFixture, TestSkipFilesWithMissingID) { TEST_F(BehaviorTreeTestFixture, TestAllSuccess) { // Load behavior tree from file - const auto root_dir = std::filesystem::path( - ament_index_cpp::get_package_share_directory("nav2_bt_navigator") - ) / "behavior_trees"; + std::filesystem::path pkg_share_dir; + ament_index_cpp::get_package_share_directory("nav2_bt_navigator", pkg_share_dir); + const auto root_dir = pkg_share_dir / "behavior_trees"; auto bt_file = root_dir / "navigate_to_pose_w_replanning_and_recovery.xml"; std::vector search_directories = {root_dir.string()}; @@ -721,9 +721,9 @@ TEST_F(BehaviorTreeTestFixture, TestAllSuccess) TEST_F(BehaviorTreeTestFixture, TestAllFailure) { // Load behavior tree from file - const auto root_dir = std::filesystem::path( - ament_index_cpp::get_package_share_directory("nav2_bt_navigator") - ) / "behavior_trees"; + std::filesystem::path pkg_share_dir; + ament_index_cpp::get_package_share_directory("nav2_bt_navigator", pkg_share_dir); + const auto root_dir = pkg_share_dir / "behavior_trees"; auto bt_file = root_dir / "navigate_to_pose_w_replanning_and_recovery.xml"; std::vector search_directories = {root_dir.string()}; @@ -781,9 +781,9 @@ TEST_F(BehaviorTreeTestFixture, TestAllFailure) TEST_F(BehaviorTreeTestFixture, TestNavigateSubtreeRecoveries) { // Load behavior tree from file - const auto root_dir = std::filesystem::path( - ament_index_cpp::get_package_share_directory("nav2_bt_navigator") - ) / "behavior_trees"; + std::filesystem::path pkg_share_dir; + ament_index_cpp::get_package_share_directory("nav2_bt_navigator", pkg_share_dir); + const auto root_dir = pkg_share_dir / "behavior_trees"; auto bt_file = root_dir / "navigate_to_pose_w_replanning_and_recovery.xml"; std::vector search_directories = {root_dir.string()}; @@ -844,9 +844,9 @@ TEST_F(BehaviorTreeTestFixture, TestNavigateSubtreeRecoveries) TEST_F(BehaviorTreeTestFixture, TestNavigateRecoverySimple) { // Load behavior tree from file - const auto root_dir = std::filesystem::path( - ament_index_cpp::get_package_share_directory("nav2_bt_navigator") - ) / "behavior_trees"; + std::filesystem::path pkg_share_dir; + ament_index_cpp::get_package_share_directory("nav2_bt_navigator", pkg_share_dir); + const auto root_dir = pkg_share_dir / "behavior_trees"; auto bt_file = root_dir / "navigate_to_pose_w_replanning_and_recovery.xml"; std::vector search_directories = {root_dir.string()}; @@ -946,9 +946,9 @@ TEST_F(BehaviorTreeTestFixture, TestNavigateRecoverySimple) TEST_F(BehaviorTreeTestFixture, TestNavigateRecoveryComplex) { // Load behavior tree from file - const auto root_dir = std::filesystem::path( - ament_index_cpp::get_package_share_directory("nav2_bt_navigator") - ) / "behavior_trees"; + std::filesystem::path pkg_share_dir; + ament_index_cpp::get_package_share_directory("nav2_bt_navigator", pkg_share_dir); + const auto root_dir = pkg_share_dir / "behavior_trees"; auto bt_file = root_dir / "navigate_to_pose_w_replanning_and_recovery.xml"; std::vector search_directories = {root_dir.string()}; @@ -1018,9 +1018,9 @@ TEST_F(BehaviorTreeTestFixture, TestNavigateRecoveryComplex) TEST_F(BehaviorTreeTestFixture, TestRecoverySubtreeGoalUpdated) { // Load behavior tree from file - const auto root_dir = std::filesystem::path( - ament_index_cpp::get_package_share_directory("nav2_bt_navigator") - ) / "behavior_trees"; + std::filesystem::path pkg_share_dir; + ament_index_cpp::get_package_share_directory("nav2_bt_navigator", pkg_share_dir); + const auto root_dir = pkg_share_dir / "behavior_trees"; auto bt_file = root_dir / "navigate_to_pose_w_replanning_and_recovery.xml"; std::vector search_directories = {root_dir.string()};