|
| 1 | +# Copyright 2025 ArduPilot.org. |
| 2 | +# |
| 3 | +# This program is free software: you can redistribute it and/or modify |
| 4 | +# it under the terms of the GNU General Public License as published by |
| 5 | +# the Free Software Foundation, either version 3 of the License, or |
| 6 | +# (at your option) any later version. |
| 7 | +# |
| 8 | +# This program is distributed in the hope that it will be useful, |
| 9 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +# GNU General Public License for more details. |
| 12 | +# |
| 13 | +# You should have received a copy of the GNU General Public License |
| 14 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 15 | + |
| 16 | +""" |
| 17 | +Bring up ArduPilot SITL and check the airspeed commands result in changed airspeed. |
| 18 | +
|
| 19 | +colcon test --executor sequential --parallel-workers 0 \ |
| 20 | + --packages-select ardupilot_dds_tests \ |
| 21 | + --event-handlers=console_cohesion+ --pytest-args -k test_plane_airspeed |
| 22 | +""" |
| 23 | + |
| 24 | + |
| 25 | +import launch_pytest |
| 26 | +import pytest |
| 27 | +import rclpy |
| 28 | +import rclpy.node |
| 29 | +import threading |
| 30 | + |
| 31 | +from launch import LaunchDescription |
| 32 | + |
| 33 | +from launch_pytest.tools import process as process_tools |
| 34 | + |
| 35 | +from rclpy.qos import QoSProfile |
| 36 | +from rclpy.qos import QoSReliabilityPolicy |
| 37 | +from rclpy.qos import QoSHistoryPolicy |
| 38 | + |
| 39 | +from ardupilot_msgs.msg import Airspeed |
| 40 | + |
| 41 | +TOPIC = "/ap/airspeed" |
| 42 | +AIRSPEED_RECV_TIMEOUT = 20.0 |
| 43 | + |
| 44 | + |
| 45 | +class AirspeedTester(rclpy.node.Node): |
| 46 | + """Subscribe to Airspeed messages.""" |
| 47 | + |
| 48 | + def __init__(self): |
| 49 | + """Initialise the node.""" |
| 50 | + super().__init__("airspeed_listener") |
| 51 | + self.msg_event_object = threading.Event() |
| 52 | + |
| 53 | + def start_subscriber(self): |
| 54 | + """Start the subscriber.""" |
| 55 | + qos_profile = QoSProfile( |
| 56 | + reliability=QoSReliabilityPolicy.BEST_EFFORT, |
| 57 | + history=QoSHistoryPolicy.KEEP_LAST, |
| 58 | + depth=1, |
| 59 | + ) |
| 60 | + |
| 61 | + self.subscription = self.create_subscription(Airspeed, TOPIC, self.airspeed_data_callback, qos_profile) |
| 62 | + |
| 63 | + # Add a spin thread. |
| 64 | + self.ros_spin_thread = threading.Thread(target=lambda node: rclpy.spin(node), args=(self,)) |
| 65 | + self.ros_spin_thread.start() |
| 66 | + |
| 67 | + def airspeed_data_callback(self, msg): |
| 68 | + """Process a airspeed message from the autopilot.""" |
| 69 | + self.msg_event_object.set() |
| 70 | + |
| 71 | + self.get_logger().info(f"From AP : airspeed {msg}") |
| 72 | + |
| 73 | + |
| 74 | +@launch_pytest.fixture |
| 75 | +def launch_sitl_copter_dds_serial(sitl_copter_dds_serial): |
| 76 | + """Fixture to create the launch description.""" |
| 77 | + sitl_ld, sitl_actions = sitl_copter_dds_serial |
| 78 | + |
| 79 | + ld = LaunchDescription( |
| 80 | + [ |
| 81 | + sitl_ld, |
| 82 | + launch_pytest.actions.ReadyToTest(), |
| 83 | + ] |
| 84 | + ) |
| 85 | + actions = sitl_actions |
| 86 | + yield ld, actions |
| 87 | + |
| 88 | + |
| 89 | +@launch_pytest.fixture |
| 90 | +def launch_sitl_copter_dds_udp(sitl_copter_dds_udp): |
| 91 | + """Fixture to create the launch description.""" |
| 92 | + sitl_ld, sitl_actions = sitl_copter_dds_udp |
| 93 | + |
| 94 | + ld = LaunchDescription( |
| 95 | + [ |
| 96 | + sitl_ld, |
| 97 | + launch_pytest.actions.ReadyToTest(), |
| 98 | + ] |
| 99 | + ) |
| 100 | + actions = sitl_actions |
| 101 | + yield ld, actions |
| 102 | + |
| 103 | + |
| 104 | +@launch_pytest.fixture |
| 105 | +def launch_sitl_plane_dds_serial(sitl_plane_dds_serial): |
| 106 | + """Fixture to create the launch description.""" |
| 107 | + sitl_ld, sitl_actions = sitl_plane_dds_serial |
| 108 | + |
| 109 | + ld = LaunchDescription( |
| 110 | + [ |
| 111 | + sitl_ld, |
| 112 | + launch_pytest.actions.ReadyToTest(), |
| 113 | + ] |
| 114 | + ) |
| 115 | + actions = sitl_actions |
| 116 | + yield ld, actions |
| 117 | + |
| 118 | + |
| 119 | +@launch_pytest.fixture |
| 120 | +def launch_sitl_plane_dds_udp(sitl_plane_dds_udp): |
| 121 | + """Fixture to create the launch description.""" |
| 122 | + sitl_ld, sitl_actions = sitl_plane_dds_udp |
| 123 | + |
| 124 | + ld = LaunchDescription( |
| 125 | + [ |
| 126 | + sitl_ld, |
| 127 | + launch_pytest.actions.ReadyToTest(), |
| 128 | + ] |
| 129 | + ) |
| 130 | + actions = sitl_actions |
| 131 | + yield ld, actions |
| 132 | + |
| 133 | + |
| 134 | +@pytest.mark.launch(fixture=launch_sitl_copter_dds_serial) |
| 135 | +def test_dds_serial_airspeed_msg_recv_copter(launch_context, launch_sitl_copter_dds_serial): |
| 136 | + """Test airspeed messages are published by AP_DDS.""" |
| 137 | + _, actions = launch_sitl_copter_dds_serial |
| 138 | + virtual_ports = actions["virtual_ports"].action |
| 139 | + micro_ros_agent = actions["micro_ros_agent"].action |
| 140 | + mavproxy = actions["mavproxy"].action |
| 141 | + sitl = actions["sitl"].action |
| 142 | + |
| 143 | + # Wait for process to start. |
| 144 | + process_tools.wait_for_start_sync(launch_context, virtual_ports, timeout=2) |
| 145 | + process_tools.wait_for_start_sync(launch_context, micro_ros_agent, timeout=2) |
| 146 | + process_tools.wait_for_start_sync(launch_context, mavproxy, timeout=2) |
| 147 | + process_tools.wait_for_start_sync(launch_context, sitl, timeout=2) |
| 148 | + |
| 149 | + rclpy.init() |
| 150 | + try: |
| 151 | + node = AirspeedTester() |
| 152 | + node.start_subscriber() |
| 153 | + msgs_received_flag = node.msg_event_object.wait(timeout=AIRSPEED_RECV_TIMEOUT) |
| 154 | + assert msgs_received_flag, f"Did not receive '{TOPIC}' msgs." |
| 155 | + finally: |
| 156 | + rclpy.shutdown() |
| 157 | + yield |
| 158 | + |
| 159 | + |
| 160 | +@pytest.mark.launch(fixture=launch_sitl_copter_dds_udp) |
| 161 | +def test_dds_udp_airspeed_msg_recv_copter(launch_context, launch_sitl_copter_dds_udp): |
| 162 | + """Test airspeed messages are published by AP_DDS.""" |
| 163 | + _, actions = launch_sitl_copter_dds_udp |
| 164 | + micro_ros_agent = actions["micro_ros_agent"].action |
| 165 | + mavproxy = actions["mavproxy"].action |
| 166 | + sitl = actions["sitl"].action |
| 167 | + |
| 168 | + # Wait for process to start. |
| 169 | + process_tools.wait_for_start_sync(launch_context, micro_ros_agent, timeout=2) |
| 170 | + process_tools.wait_for_start_sync(launch_context, mavproxy, timeout=2) |
| 171 | + process_tools.wait_for_start_sync(launch_context, sitl, timeout=2) |
| 172 | + |
| 173 | + rclpy.init() |
| 174 | + try: |
| 175 | + node = AirspeedTester() |
| 176 | + node.start_subscriber() |
| 177 | + msgs_received_flag = node.msg_event_object.wait(timeout=AIRSPEED_RECV_TIMEOUT) |
| 178 | + assert msgs_received_flag, f"Did not receive '{TOPIC}' msgs." |
| 179 | + |
| 180 | + finally: |
| 181 | + rclpy.shutdown() |
| 182 | + yield |
| 183 | + |
| 184 | + |
| 185 | +@pytest.mark.launch(fixture=launch_sitl_plane_dds_serial) |
| 186 | +def test_dds_serial_airspeed_msg_recv_plane(launch_context, launch_sitl_plane_dds_serial): |
| 187 | + """Test airspeed messages are published by AP_DDS.""" |
| 188 | + _, actions = launch_sitl_plane_dds_serial |
| 189 | + virtual_ports = actions["virtual_ports"].action |
| 190 | + micro_ros_agent = actions["micro_ros_agent"].action |
| 191 | + mavproxy = actions["mavproxy"].action |
| 192 | + sitl = actions["sitl"].action |
| 193 | + |
| 194 | + # Wait for process to start. |
| 195 | + process_tools.wait_for_start_sync(launch_context, virtual_ports, timeout=2) |
| 196 | + process_tools.wait_for_start_sync(launch_context, micro_ros_agent, timeout=2) |
| 197 | + process_tools.wait_for_start_sync(launch_context, mavproxy, timeout=2) |
| 198 | + process_tools.wait_for_start_sync(launch_context, sitl, timeout=2) |
| 199 | + |
| 200 | + rclpy.init() |
| 201 | + try: |
| 202 | + node = AirspeedTester() |
| 203 | + node.start_subscriber() |
| 204 | + msgs_received_flag = node.msg_event_object.wait(timeout=AIRSPEED_RECV_TIMEOUT) |
| 205 | + assert msgs_received_flag, f"Did not receive '{TOPIC}' msgs." |
| 206 | + finally: |
| 207 | + rclpy.shutdown() |
| 208 | + yield |
| 209 | + |
| 210 | + |
| 211 | +@pytest.mark.launch(fixture=launch_sitl_plane_dds_udp) |
| 212 | +def test_dds_udp_airspeed_msg_recv_plane(launch_context, launch_sitl_plane_dds_udp): |
| 213 | + """Test airspeed messages are published by AP_DDS.""" |
| 214 | + _, actions = launch_sitl_plane_dds_udp |
| 215 | + micro_ros_agent = actions["micro_ros_agent"].action |
| 216 | + mavproxy = actions["mavproxy"].action |
| 217 | + sitl = actions["sitl"].action |
| 218 | + |
| 219 | + # Wait for process to start. |
| 220 | + process_tools.wait_for_start_sync(launch_context, micro_ros_agent, timeout=2) |
| 221 | + process_tools.wait_for_start_sync(launch_context, mavproxy, timeout=2) |
| 222 | + process_tools.wait_for_start_sync(launch_context, sitl, timeout=2) |
| 223 | + |
| 224 | + rclpy.init() |
| 225 | + try: |
| 226 | + node = AirspeedTester() |
| 227 | + node.start_subscriber() |
| 228 | + msgs_received_flag = node.msg_event_object.wait(timeout=AIRSPEED_RECV_TIMEOUT) |
| 229 | + assert msgs_received_flag, f"Did not receive '{TOPIC}' msgs." |
| 230 | + |
| 231 | + finally: |
| 232 | + rclpy.shutdown() |
| 233 | + yield |
0 commit comments