|
| 1 | +import os |
| 2 | +import pytest |
| 3 | +import rclpy |
| 4 | +from ament_index_python.packages import get_package_share_directory |
| 5 | +from launch import LaunchDescription |
| 6 | +from launch.actions import IncludeLaunchDescription, ExecuteProcess, DeclareLaunchArgument |
| 7 | +from launch.substitutions import LaunchConfiguration |
| 8 | +from launch.launch_description_sources import PythonLaunchDescriptionSource |
| 9 | +from launch_ros.actions import Node |
| 10 | +import launch_pytest |
| 11 | +from launch_pytest.tools import process as process_tools |
| 12 | +from artefacts_toolkit.rosbag import rosbag, image_topics |
| 13 | +from artefacts_toolkit.chart import make_chart |
| 14 | +from artefacts_toolkit.config import get_artefacts_param |
| 15 | +import sys |
| 16 | +import os |
| 17 | +sys.path.append(os.path.dirname(__file__)) |
| 18 | +from simulation_state_util import SimulationStateUtil |
| 19 | + |
| 20 | + |
| 21 | +ARTEFACTS_PARAMS_FILE = os.environ.get( |
| 22 | + "ARTEFACTS_SCENARIO_PARAMS_FILE", "scenario_params.yaml" |
| 23 | +) |
| 24 | + |
| 25 | +def deep_merge_dicts(source, override): |
| 26 | + """Recursively merge two dictionaries, with values from `override` taking precedence over `source`""" |
| 27 | + for key, value in override.items(): |
| 28 | + if isinstance(value, dict) and key in source: |
| 29 | + source[key] = deep_merge_dicts(source[key], value) |
| 30 | + else: |
| 31 | + source[key] = value |
| 32 | + return source |
| 33 | + |
| 34 | +def merge_ros_params_files(source, override, destination): |
| 35 | + """Merge two ROS2 yaml parameter files into one, overriding the values in the first one with the values in `override`""" |
| 36 | + import yaml |
| 37 | + |
| 38 | + with open(source, "r") as f: |
| 39 | + source_params = yaml.safe_load(f) |
| 40 | + |
| 41 | + with open(override, "r") as f: |
| 42 | + override_params = yaml.safe_load(f) |
| 43 | + |
| 44 | + merged_params = deep_merge_dicts(source_params, override_params) |
| 45 | + with open(destination, "w") as f: |
| 46 | + yaml.dump(merged_params, f) |
| 47 | + |
| 48 | +@pytest.fixture() |
| 49 | +def reach_goal_proc(): |
| 50 | + reach_goal = Node( |
| 51 | + package="sam_bot_nav2_gz", |
| 52 | + executable="reach_goal.py", |
| 53 | + output="screen", |
| 54 | + cached_output=True, |
| 55 | + ) |
| 56 | + return reach_goal |
| 57 | + |
| 58 | + |
| 59 | +@launch_pytest.fixture |
| 60 | +def launch_description(reach_goal_proc): |
| 61 | + """Launch description fixture for pytest-based testing""" |
| 62 | + try: |
| 63 | + world = get_artefacts_param("launch", "world", default="empty.sdf") |
| 64 | + except FileNotFoundError: |
| 65 | + world = "empty.sdf" |
| 66 | + |
| 67 | + run_headless = LaunchConfiguration("run_headless") |
| 68 | + source_params_file = "src/sam_bot_nav2_gz/config/nav2_params.yaml" |
| 69 | + new_params_file = "all_params.yaml" |
| 70 | + try: |
| 71 | + merge_ros_params_files(source_params_file, ARTEFACTS_PARAMS_FILE, new_params_file) |
| 72 | + except FileNotFoundError: |
| 73 | + pass |
| 74 | + |
| 75 | + launch_navigation_stack = IncludeLaunchDescription( |
| 76 | + PythonLaunchDescriptionSource( |
| 77 | + [ |
| 78 | + os.path.join( |
| 79 | + get_package_share_directory("sam_bot_nav2_gz"), |
| 80 | + "launch", |
| 81 | + "complete_navigation.launch.py" |
| 82 | + ), |
| 83 | + ] |
| 84 | + ), |
| 85 | + launch_arguments=[ |
| 86 | + ("run_headless", run_headless), |
| 87 | + ("world_file", world), |
| 88 | + ("params_file", new_params_file), |
| 89 | + ], |
| 90 | + ) |
| 91 | + |
| 92 | + |
| 93 | + topics = ["/odom"] |
| 94 | + metrics = ["/distance_from_start_gt", "/distance_from_start_est", "/odometry_error"] |
| 95 | + camera_topics = ["/sky_cam"] |
| 96 | + sim_topics = ["/world/dynamic_pose/info"] |
| 97 | + bag_recorder, rosbag_filepath = rosbag.get_bag_recorder( |
| 98 | + topics + sim_topics + metrics + camera_topics, use_sim_time=True |
| 99 | + ) |
| 100 | + |
| 101 | + # Gazebo ros bridge |
| 102 | + gz_bridge = Node( |
| 103 | + package="ros_gz_bridge", |
| 104 | + executable="parameter_bridge", |
| 105 | + parameters=[{ |
| 106 | + "config_file": os.path.join( |
| 107 | + "src", |
| 108 | + "sam_bot_nav2_gz", |
| 109 | + "test", |
| 110 | + "bridge.yaml" |
| 111 | + )}], |
| 112 | + output="screen", |
| 113 | + ) |
| 114 | + |
| 115 | + test_odometry_node = ExecuteProcess( |
| 116 | + cmd=[ |
| 117 | + "python3", |
| 118 | + os.path.join( |
| 119 | + "src", |
| 120 | + "sam_bot_nav2_gz", |
| 121 | + "test", |
| 122 | + "test_odometry_node.py" |
| 123 | + ), |
| 124 | + ] |
| 125 | + ) |
| 126 | + |
| 127 | + return LaunchDescription( |
| 128 | + [ |
| 129 | + DeclareLaunchArgument( |
| 130 | + name="run_headless", |
| 131 | + default_value="False", |
| 132 | + description="Start GZ in headless mode and don't start RViz (overrides use_rviz)", |
| 133 | + ), |
| 134 | + launch_navigation_stack, |
| 135 | + reach_goal_proc, |
| 136 | + test_odometry_node, |
| 137 | + gz_bridge, |
| 138 | + #bag_recorder, |
| 139 | + ] |
| 140 | + ) |
| 141 | + |
| 142 | + |
| 143 | +@pytest.fixture(scope="function") |
| 144 | +def simulation_state_util(): |
| 145 | + """Fixture that provides access to simulation state during tests""" |
| 146 | + if not rclpy.ok(): |
| 147 | + rclpy.init() |
| 148 | + util = SimulationStateUtil() |
| 149 | + |
| 150 | + # Wait for initial state |
| 151 | + util.wait_for_initial_state(timeout=15.0) |
| 152 | + |
| 153 | + yield util |
| 154 | + |
| 155 | + # Cleanup |
| 156 | + util.shutdown_util() |
| 157 | + |
| 158 | + |
| 159 | +@pytest.mark.launch(fixture=launch_description) |
| 160 | +def test_nav2_started(reach_goal_proc, launch_context): |
| 161 | + """Test that Nav2 starts successfully""" |
| 162 | + def validate_nav2_output(output): |
| 163 | + return output and 'Nav2 active!' in output |
| 164 | + |
| 165 | + # Get the reach_goal process from the launch context |
| 166 | + process_tools.wait_for_output_sync( |
| 167 | + launch_context, reach_goal_proc, validate_nav2_output, timeout=30) |
| 168 | + |
| 169 | + |
| 170 | +@pytest.mark.launch(fixture=launch_description) |
| 171 | +def test_reached_goal(reach_goal_proc,launch_context, simulation_state_util): |
| 172 | + """Check that the navigation goal is reached""" |
| 173 | + def validate_goal_output(output): |
| 174 | + return output and 'Goal succeeded!' in output |
| 175 | + |
| 176 | + # Get the reach_goal process from the launch context |
| 177 | + process_tools.wait_for_output_sync( |
| 178 | + launch_context, reach_goal_proc, validate_goal_output, timeout=40) |
| 179 | + |
| 180 | + # Additional assertion using simulation state |
| 181 | + goal_coordinates = (2.0, 3.0) # Example goal coordinates - adjust as needed |
| 182 | + distance = simulation_state_util.get("robot").distance_to(goal_coordinates) |
| 183 | + assert distance < 1.0, f"Robot is not close enough to goal at {goal_coordinates}. Distance: {distance}" |
| 184 | + |
| 185 | + |
0 commit comments