|
1 | 1 | """Exercise the ROS command gate and MKS frames without a hardware CAN interface.""" |
2 | 2 |
|
| 3 | +import importlib.util |
3 | 4 | import math |
4 | 5 | from pathlib import Path |
5 | 6 | import time |
| 7 | +from unittest.mock import MagicMock |
6 | 8 |
|
7 | 9 | import pytest |
8 | 10 | import rclpy |
| 11 | +from rclpy._rclpy_pybind11 import RCLError |
9 | 12 | from rclpy.parameter import Parameter |
10 | 13 | from sensor_msgs.msg import JointState |
11 | 14 | from std_msgs.msg import Bool, String |
12 | 15 |
|
13 | | -from waybionic_teleop import mks_can |
| 16 | +from waybionic_teleop import joy_udp_receiver, mks_can, sim_arm_drives_node, xbox_teleop_node |
14 | 17 | from waybionic_teleop.sim_arm_drives_node import REPLY_TIMEOUT_S, SimArmDrives |
15 | 18 |
|
16 | 19 | URDF = (Path(__file__).resolve().parents[2] / 'waybionic_description' / 'urdf' |
17 | 20 | / 'waybionic_arm.urdf').read_text(encoding='utf-8') |
| 21 | +CAMERA_PATH = (Path(__file__).resolve().parents[2] / 'waybionic_bringup' |
| 22 | + / 'scripts' / 'camera_follower.py') |
| 23 | +_camera_spec = importlib.util.spec_from_file_location('waybionic_camera_shutdown', CAMERA_PATH) |
| 24 | +camera_module = importlib.util.module_from_spec(_camera_spec) |
| 25 | +_camera_spec.loader.exec_module(camera_module) |
| 26 | + |
| 27 | +POSE = {'joint_1': 0.8, 'joint_2': -0.8, 'joint_3': 0.8, |
| 28 | + 'joint_4': 0.8, 'joint_5': 0.25, 'tool_grip': 0.8} |
| 29 | +RATES = {'joint_1': 0.2, 'joint_2': -0.2, 'joint_3': 0.2, |
| 30 | + 'joint_4': 0.2, 'joint_5': 0.05, 'tool_grip': 0.2} |
| 31 | + |
| 32 | +SHUTDOWN_NODES = [ |
| 33 | + (xbox_teleop_node, 'XboxTeleop'), |
| 34 | + (sim_arm_drives_node, 'SimArmDrives'), |
| 35 | + (joy_udp_receiver, 'JoyUdpReceiver'), |
| 36 | + (camera_module, 'CameraFollower'), |
| 37 | +] |
18 | 38 |
|
19 | 39 |
|
20 | 40 | @pytest.fixture |
@@ -44,25 +64,37 @@ def ready(node): |
44 | 64 | assert node.description_valid and node.authorized |
45 | 65 |
|
46 | 66 |
|
47 | | -def command(node, **changes): |
| 67 | +def command(node, velocities=None, **changes): |
48 | 68 | targets = dict.fromkeys(node.map.joints, 0.0) |
49 | 69 | targets.update(changes) |
| 70 | + if velocities is None: |
| 71 | + velocities = {name: math.copysign(0.2, value) if value else 0.0 |
| 72 | + for name, value in targets.items()} |
50 | 73 | return JointState(name=list(targets), position=list(targets.values()), |
51 | | - velocity=[0.0] * len(targets)) |
| 74 | + velocity=[velocities.get(name, 0.0) for name in targets]) |
| 75 | + |
| 76 | + |
| 77 | +def sent_frame(node, index): |
| 78 | + drive = node.map.drives[index] |
| 79 | + can_id, hex_data = node.last_command[index].split('#') |
| 80 | + assert int(can_id, 16) == drive.can_id |
| 81 | + return mks_can.parse(drive.can_id, bytes.fromhex(hex_data)) |
52 | 82 |
|
53 | 83 |
|
54 | 84 | def assert_stopped(node): |
55 | | - assert not node.authorized and all(servo.target is None for servo in node.bus.drives.values()) |
56 | | - for drive, frame in zip(node.map.drives, node.last_command): |
57 | | - code, args = mks_can.parse(drive.can_id, bytes.fromhex(frame.split('#')[1])) |
58 | | - assert code == mks_can.ABSOLUTE_AXIS and int.from_bytes(args[:2], 'big') == 0 |
| 85 | + assert not node.authorized |
| 86 | + assert all(servo.enabled and servo.target is None and servo.rpm == 0.0 |
| 87 | + for servo in node.bus.drives.values()) |
| 88 | + assert all(sent_frame(node, index) == (mks_can.ABSOLUTE_AXIS, bytes(6)) |
| 89 | + for index in range(len(node.map.drives))) |
59 | 90 |
|
60 | 91 |
|
61 | 92 | def test_a_valid_setpoint_reaches_the_simulated_bus_and_feedback(node): |
62 | 93 | ready(node) |
63 | 94 | requested = command(node, joint_1=0.5, joint_2=0.1) |
64 | 95 | node.on_command(requested) |
65 | 96 | advance(node) |
| 97 | + advance(node) |
66 | 98 | assert node.bus.drives[1].target is not None |
67 | 99 | assert node.bus.drives[1].axis > 0 |
68 | 100 | assert node.bus.drives[2].axis > 0 |
@@ -145,6 +177,17 @@ def test_int24_overflow_rejects_the_entire_coordinated_move(node): |
145 | 177 | advance(node) |
146 | 178 | assert node.rejected == 1 |
147 | 179 | assert_stopped(node) |
| 180 | + node.map.drives[0].gear_ratio = 1.0 |
| 181 | + valid = command(node, joint_1=0.5) |
| 182 | + node.on_enabled(Bool(data=True)) |
| 183 | + node.on_command(valid) |
| 184 | + advance(node) |
| 185 | + assert_stopped(node) |
| 186 | + node.on_enabled(Bool(data=False)) |
| 187 | + node.on_enabled(Bool(data=True)) |
| 188 | + node.on_command(valid) |
| 189 | + advance(node) |
| 190 | + assert node.authorized and node.bus.drives[1].target is not None |
148 | 191 |
|
149 | 192 |
|
150 | 193 | def test_encoder_outside_a_provisional_limit_may_move_only_toward_it(node): |
@@ -177,3 +220,111 @@ def test_stale_encoder_feedback_stops_before_sending_another_position(node): |
177 | 220 | node.on_command(command(node, joint_1=1.0)) |
178 | 221 | advance(node) |
179 | 222 | assert node.authorized and node.bus.drives[1].target is not None |
| 223 | + |
| 224 | + |
| 225 | +def test_zero_speed_f5_stops_six_drives_and_negative_targets_rearm(node): |
| 226 | + ready(node) |
| 227 | + node.max_rpm = 5 |
| 228 | + node.on_command(command(node, velocities=RATES, **POSE)) |
| 229 | + advance(node) |
| 230 | + assert all(drive.target is not None for drive in node.bus.drives.values()) |
| 231 | + advance(node, 0.1) |
| 232 | + assert all(drive.rpm != 0.0 for drive in node.bus.drives.values()) |
| 233 | + |
| 234 | + measured = node.map.to_positions(node.counts) |
| 235 | + node.on_command(command(node, velocities=dict.fromkeys(POSE, 0.0), **measured)) |
| 236 | + advance(node) |
| 237 | + assert node.authorized |
| 238 | + assert all(drive.enabled and drive.target is None and drive.rpm == 0.0 |
| 239 | + for drive in node.bus.drives.values()) |
| 240 | + assert all(sent_frame(node, index) == (mks_can.ABSOLUTE_AXIS, bytes(6)) |
| 241 | + for index in range(len(node.map.drives))) |
| 242 | + held = [drive.axis for drive in node.bus.drives.values()] |
| 243 | + advance(node, 0.6) |
| 244 | + assert [drive.axis for drive in node.bus.drives.values()] == held |
| 245 | + assert_stopped(node) |
| 246 | + |
| 247 | + node.on_enabled(Bool(data=False)) |
| 248 | + node.on_enabled(Bool(data=True)) |
| 249 | + reverse = dict.fromkeys(POSE, -0.4) |
| 250 | + reverse.update(joint_5=0.1, tool_grip=0.2) |
| 251 | + rates = dict.fromkeys(POSE, -0.2) |
| 252 | + rates.update(joint_5=0.05, tool_grip=0.2) |
| 253 | + node.on_command(command(node, velocities=rates, **reverse)) |
| 254 | + advance(node) |
| 255 | + assert node.authorized |
| 256 | + assert [drive.target for drive in node.bus.drives.values()] == node.map.to_counts(reverse) |
| 257 | + assert all(drive.speed > 0 for drive in node.bus.drives.values()) |
| 258 | + assert int.from_bytes(sent_frame(node, 0)[1][3:], 'big', signed=True) < 0 |
| 259 | + |
| 260 | + |
| 261 | +def test_six_drive_host_pause_drops_pending_targets_until_fresh_start(node): |
| 262 | + ready(node) |
| 263 | + node.max_rpm = 5 |
| 264 | + node.on_command(command(node, velocities=RATES, **POSE)) |
| 265 | + advance(node) |
| 266 | + assert all(drive.target is not None for drive in node.bus.drives.values()) |
| 267 | + next_pose = {joint: value * 1.1 for joint, value in POSE.items()} |
| 268 | + node.on_command(command(node, velocities=RATES, **next_pose)) |
| 269 | + advance(node, 2.0) |
| 270 | + assert all(drive.heartbeat_stops == 1 for drive in node.bus.drives.values()) |
| 271 | + assert_stopped(node) |
| 272 | + assert node.commanded != next_pose |
| 273 | + |
| 274 | + node.on_enabled(Bool(data=True)) |
| 275 | + node.on_command(command(node, velocities=RATES, **next_pose)) |
| 276 | + advance(node) |
| 277 | + assert_stopped(node) |
| 278 | + node.on_enabled(Bool(data=False)) |
| 279 | + node.on_enabled(Bool(data=True)) |
| 280 | + node.on_command(command(node, velocities=RATES, **next_pose)) |
| 281 | + advance(node) |
| 282 | + assert [drive.target for drive in node.bus.drives.values()] == node.map.to_counts(next_pose) |
| 283 | + advance(node, 2.0) |
| 284 | + assert all(drive.heartbeat_stops == 2 for drive in node.bus.drives.values()) |
| 285 | + assert_stopped(node) |
| 286 | + |
| 287 | + |
| 288 | +@pytest.mark.parametrize('module, class_name', SHUTDOWN_NODES) |
| 289 | +def test_shutdown_only_ignores_an_invalid_shutdown_context(module, class_name, monkeypatch): |
| 290 | + factory = MagicMock() |
| 291 | + spin = MagicMock(side_effect=RCLError('the given context is not valid')) |
| 292 | + monkeypatch.setattr(module, class_name, factory) |
| 293 | + monkeypatch.setattr(module.rclpy, 'init', MagicMock()) |
| 294 | + monkeypatch.setattr(module.rclpy, 'try_shutdown', MagicMock()) |
| 295 | + monkeypatch.setattr(module.rclpy, 'spin', spin) |
| 296 | + monkeypatch.setattr(module.rclpy, 'ok', lambda: False) |
| 297 | + module.main() |
| 298 | + |
| 299 | + spin.side_effect = RCLError('unrelated ROS failure') |
| 300 | + with pytest.raises(RCLError, match='unrelated ROS failure'): |
| 301 | + module.main() |
| 302 | + monkeypatch.setattr(module.rclpy, 'ok', lambda: True) |
| 303 | + spin.side_effect = RCLError('the given context is not valid') |
| 304 | + with pytest.raises(RCLError, match='context is not valid'): |
| 305 | + module.main() |
| 306 | + assert spin.call_count == 3 |
| 307 | + assert factory.return_value.destroy_node.call_count == 3 |
| 308 | + |
| 309 | + |
| 310 | +@pytest.mark.parametrize('module, class_name', SHUTDOWN_NODES) |
| 311 | +def test_binding_error_only_ignored_after_context_shutdown(module, class_name, monkeypatch): |
| 312 | + factory = MagicMock() |
| 313 | + error = RuntimeError("Unable to convert call argument '0' to Python object") |
| 314 | + spin = MagicMock(side_effect=error) |
| 315 | + monkeypatch.setattr(module, class_name, factory) |
| 316 | + monkeypatch.setattr(module.rclpy, 'init', MagicMock()) |
| 317 | + monkeypatch.setattr(module.rclpy, 'try_shutdown', MagicMock()) |
| 318 | + monkeypatch.setattr(module.rclpy, 'spin', spin) |
| 319 | + monkeypatch.setattr(module.rclpy, 'ok', lambda: False) |
| 320 | + module.main() |
| 321 | + |
| 322 | + spin.side_effect = RuntimeError('unrelated subscription failure') |
| 323 | + with pytest.raises(RuntimeError, match='unrelated subscription failure'): |
| 324 | + module.main() |
| 325 | + monkeypatch.setattr(module.rclpy, 'ok', lambda: True) |
| 326 | + spin.side_effect = error |
| 327 | + with pytest.raises(RuntimeError, match='Unable to convert call argument'): |
| 328 | + module.main() |
| 329 | + assert spin.call_count == 3 |
| 330 | + assert factory.return_value.destroy_node.call_count == 3 |
0 commit comments