"""Continuous two-track intent, source expiry, saturation and adapter parity.""" import importlib from pathlib import Path import pytest from k1link.fleet.differential_drive import ( DriveLimits, SideSpeeds, TwoSideDrive, arcade_intent, tank_intent, ) @pytest.fixture def module(monkeypatch): monkeypatch.syspath_prepend(str(Path(__file__).parents[1] / "simulation/ai-polygon")) return importlib.import_module("motion_control") @pytest.fixture def limits(): return DriveLimits(0.685, 0.3, 0.2, 0.4, 0.1) def test_slow_perception_keeps_continuous_physics_command(module): drive = module.DriveEnvelope() rows = [drive.step(0.15, 0, 1 / 60)[0] for _ in range(180)] assert rows[0] == pytest.approx(0.2 / 60) assert all(0 <= b - a <= 0.2 / 60 + 1e-9 for a, b in zip(rows, rows[1:], strict=False)) assert rows[44:] == pytest.approx([0.15] * 136) def test_zero_or_deadman_is_immediate_but_normal_reduction_is_smooth(module): drive = module.DriveEnvelope() for _ in range(60): drive.step(0.15, 0, 1 / 60) assert drive.step(0.03, 0, 1 / 60)[0] == pytest.approx(0.15 - 0.4 / 60) assert drive.step(0.15, 0.2, 1 / 60, stop=True) == (0, 0) assert drive.wheels == (0, 0) assert drive.step(0.15, 0, 1 / 60)[0] == pytest.approx(0.2 / 60) assert drive.step(0, 0, 1 / 60) == (0, 0) def test_w_a_release_a_and_change_to_d_keep_moving_without_side_jumps(limits): drive = TwoSideDrive(limits) previous = drive.applied finals = [] for steering in (0, 1, 0, -1, 0): requested = arcade_intent(1, steering, limits, 0.35) for _ in range(180): value = drive.step(requested, 1 / 60) assert min(value.left_mps, value.right_mps) > 0 assert max(value.left_mps, value.right_mps) <= 0.3 for a, b in zip(vars(previous).values(), vars(value).values(), strict=True): assert abs(b - a) <= 0.4 / 60 + 1e-9 previous = value finals.append(value.twist(limits.track_centres_m)) assert finals[0][1] == finals[2][1] == finals[4][1] == 0 assert finals[1][1] > 0 and finals[3][1] < 0 def test_tank_reversal_reaches_zero_without_stopping_other_side(limits): drive = TwoSideDrive(limits) for _ in range(120): drive.step(tank_intent(1, 1, limits), 1 / 60) values = [drive.step(tank_intent(-1, 1, limits), 1 / 60) for _ in range(180)] assert all(value.right_mps == pytest.approx(0.3) for value in values) first_reverse = next(i for i, value in enumerate(values) if value.left_mps < 0) assert values[first_reverse - 1].left_mps == 0 assert values[-1].left_mps == pytest.approx(-0.3) def test_twist_tank_and_arcade_share_direction_units_and_saturation(module, limits): requested = SideSpeeds.from_twist(0.3, 0.35, 0.685) bounded = requested.bounded(0.3) assert bounded.right_mps == 0.3 and 0 < bounded.left_mps < bounded.right_mps assert bounded.left_mps / bounded.right_mps == pytest.approx( requested.left_mps / requested.right_mps ) assert arcade_intent(1, 1, limits, 0.35) == bounded drive = module.DriveEnvelope() for _ in range(180): v, w = drive.step(0.3, 0.35, 1 / 60) assert (v, w) == pytest.approx(bounded.twist(0.685)) assert drive.wheels == pytest.approx((bounded.left_mps, bounded.right_mps)) assert SideSpeeds.from_twist(-0.2, -0.3, 0.685).twist(0.685) == pytest.approx((-0.2, -0.3)) def test_clock_gaps_and_invalid_values_cannot_create_unbounded_intent(limits): drive = TwoSideDrive(limits) assert drive.step(SideSpeeds(0.3, 0.3), 100).left_mps == pytest.approx(0.02) prior = drive.applied assert drive.step(SideSpeeds(0.3, 0.3), 0) == prior for dt in (-1, float("nan"), float("inf")): with pytest.raises(ValueError): drive.step(SideSpeeds(0.3, 0.3), dt) assert drive.applied == prior for value in (float("nan"), float("inf")): with pytest.raises(ValueError): SideSpeeds(value, 0) with pytest.raises(ValueError): tank_intent(1.1, 0, limits) with pytest.raises(ValueError): DriveLimits(0, 0.3, 0.2, 0.4, 0.1) def test_profile_mass_and_geometry_are_consumed_consistently(module): profile = importlib.import_module("rover_profile") chassis, wheel = profile.mass_distribution() assert chassis + len(profile.PROFILE["wheel_names"]) * wheel == 100 assert profile.mass_distribution({**profile.PROFILE, "mass_kg": 110})[0] == chassis + 10 with pytest.raises(ValueError): profile.mass_distribution({**profile.PROFILE, "mass_kg": 3}) robot = profile.ROBOT_DOCUMENT assert ( module.DriveEnvelope().limits.track_centres_m == robot["visual"]["motion"]["track_width_m"] ) assert robot["physics"]["track_width_m"] + robot["physics"]["wheel_width_m"] == pytest.approx( robot["physics"]["width_m"] ) assert ( robot["physics"]["zero_command_mode"] != robot["mechanics"]["vesc_adapter"]["zero_command"] ) def test_chassis_feedback_accounts_for_pitch_without_setting_body_motion(module): control = module.ChassisSpeedFeedback( {"proportional_gain": 0.75, "max_correction_mps": 0.15}, 0.37, 0.685, 0.3 ) # A forward-pitching body can outrun its belt while the contact moves slowly. nominal = control.step(0.15, 0.1, 0.15, 0) descending = control.step(0.15, 0.1, 0.19, 0.2) assert nominal == pytest.approx((0.15, 0.1)) assert 0 < descending[0] < 0.15 and descending[1] == pytest.approx(0.1) assert control.step(-0.15, -0.1, -0.19, -0.2) == pytest.approx((-descending[0], -0.1)) # Safety zero cannot acquire translation from feedback. assert control.step(0, 0, 0.2, 0.4) == (0, 0) # A low-speed arc must not turn into an unrequested inner-side reversal. v, w = control.step(0.07, 0.2, 0.2, 0.2) assert v - w * 0.685 / 2 >= -1e-12 and v + w * 0.685 / 2 >= 0 v, w = control.step(0.1, 0.2 / 0.685, 0, -0.2) assert v - w * 0.685 / 2 == pytest.approx(0) # Keep the intended held inner belt. assert control.step(0.01, 0, 0.4, 0.5) == (0, 0) # No autonomous reversal. v, w = control.step(0.3, 0.35, 0, -0.5) assert max(abs(v - w * 0.685 / 2), abs(v + w * 0.685 / 2)) <= 0.3 with pytest.raises(ValueError): control.step(0.15, 0, float("nan"), 0) def test_pivot_corrects_body_drift_without_reversing_sides_or_overriding_stop(module): control = module.ChassisSpeedFeedback( {"proportional_gain": 0.75, "max_correction_mps": 0.15}, 0.37, 0.685, 0.3 ) assert control.step(0, 0.35, 0, 0) == pytest.approx((0, 0.35)) assert control.step(0, 0.35, 0.04, 0) == pytest.approx((-0.03, 0.35)) assert control.step(0, -0.35, -0.04, 0) == pytest.approx((0.03, -0.35)) for yaw in (-0.35, -0.01, 0.01, 0.35): for measured in (-2, -0.04, 0.04, 2): v, w = control.step(0, yaw, measured, 0.4) left, right = v - w * 0.685 / 2, v + w * 0.685 / 2 assert left * yaw <= 1e-12 and right * yaw >= -1e-12 assert w == pytest.approx(yaw) assert max(abs(left), abs(right)) <= 0.3 shared = module.RoverMotionControl(importlib.import_module("rover_profile").ROBOT_DOCUMENT, 0.3) shared.step(0, 0.35, 1 / 60, 0.04, 0) assert shared.step(0, 0.35, 1 / 60, 0.2, 0.4, stop=True) == (0, 0) def test_speed_reserve_preserves_arc_reverse_pivot_and_zero(module): reserve = module.reserve_chassis_speed v, w = reserve(0.15, 0.2, 0.15, 0.03) assert v == pytest.approx(0.12) and w / v == pytest.approx(0.2 / 0.15) assert reserve(-0.15, -0.2, 0.15, 0.03) == pytest.approx((-v, -w)) assert reserve(0.05, 0.1, 0.15, 0.03) == (0.05, 0.1) assert reserve(0, 0.2, 0.15, 0.03) == (0, 0.2) assert reserve(0, 0, 0.15, 0.03) == (0, 0) with pytest.raises(ValueError): reserve(0.15, 0, 0.03, 0.03) def test_shared_runtime_command_path_matches_explicit_components(module): robot = importlib.import_module("rover_profile").ROBOT_DOCUMENT shared = module.RoverMotionControl(robot, 0.3) envelope = module.DriveEnvelope() settings = robot["control"]["chassis_speed_feedback"] feedback = module.ChassisSpeedFeedback( settings, robot["tracked_contact"]["chassis_com_height_m"], 0.685, 0.3 ) # Explicit composition, with changing feedback, arcs, reversals and watchdog stops. for v, w, stop in [ (0.3, 0, False), (0.3, 0.2, False), (0, 0.35, False), (-0.1, -0.2, False), (0.2, 0.2, True), (0, 0, False), ]: for i in range(90): observed, pitch = (i % 7) * 0.025, (i % 5 - 2) * 0.03 expected = module.reserve_chassis_speed( v, w, 0.3, 0.3 * settings["body_speed_reserve_fraction"] ) expected = envelope.step(*expected, 1 / 60, stop=stop) expected = feedback.step(*expected, observed, pitch) assert shared.step(v, w, 1 / 60, observed, pitch, stop=stop) == pytest.approx( expected, abs=1e-14 ) def test_explicit_control_profile_is_used_by_command_adapter(module): import copy robot = copy.deepcopy(importlib.import_module("rover_profile").ROBOT_DOCUMENT) robot["control"]["acceleration_mps2"] = 0.1 robot["control"]["chassis_speed_feedback"]["enabled"] = False control = module.RoverMotionControl(robot, 0.2) assert control.step(0.2, 0, 1 / 60, 0, 0) == pytest.approx((0.1 / 60, 0))