diff --git a/openpilot/selfdrive/controls/plannerd.py b/openpilot/selfdrive/controls/plannerd.py index ea540be820..abc31ddfe2 100755 --- a/openpilot/selfdrive/controls/plannerd.py +++ b/openpilot/selfdrive/controls/plannerd.py @@ -334,9 +334,12 @@ def starpilot_main(): navigation_slowdown_percent = NavigationOwner().read()['turnSlowdownPercent'] except (OSError, ValueError): pass + downhill_available = CP.openpilotLongitudinalControl and not (CP.passive or CP.dashcamOnly or CP.notCar) longitudinal_planner = LongitudinalPlanner(CP, developer_tuning=read_tuning(params, CP), - navigation_slowdown_percent=navigation_slowdown_percent, - downhill_surplus_mps=read_surplus(params)) + navigation_slowdown_percent=navigation_slowdown_percent) + downhill_request_ns = -1_000_000_000 + if downhill_available and 'REPLAY' in os.environ: + longitudinal_planner.downhill_surplus_mps = read_surplus(params) nudge_preferences = NudgePreferences(params) # Saved-on features start only for a finalized, supported driving owner. Their # current-source and longitudinal authority checks still run every frame. @@ -400,7 +403,7 @@ def starpilot_main(): subscribed_services.extend(optional_slc) subscribed_services.append('starpilotNavigation') optional_conditional = ['starpilotNavigation'] + (['starpilotRadarState'] if conditional_host is not None else []) + optional_slc - if longitudinal_planner.downhill_surplus_mps > 0.: + if downhill_available: subscribed_services.append('deviceMotion') optional_conditional.append('deviceMotion') sm = messaging.SubMaster(subscribed_services, poll='modelV2', @@ -415,7 +418,11 @@ def starpilot_main(): # Due saved-setting verification can block on Params. Complete it # before acquiring the model and fast-source batch, not after that # batch has already begun aging toward the CEM freshness deadline. - slc_lifecycle.refresh(time.monotonic_ns(), pending_actions=pending_slc_actions, pending_cruise=pending_slc_cruise) + settings_now_ns = time.monotonic_ns() + slc_lifecycle.refresh(settings_now_ns, pending_actions=pending_slc_actions, pending_cruise=pending_slc_cruise) + if downhill_available and not 0 <= settings_now_ns - downhill_request_ns < 1_000_000_000: + downhill_request_ns = settings_now_ns + longitudinal_planner.downhill_surplus_mps = read_surplus(params) sm.update() if sm.updated['modelV2']: # Only explicit replay uses the recorded model timeline. In a live host, diff --git a/openpilot/starpilot/longitudinal/tests/test_downhill_coast.py b/openpilot/starpilot/longitudinal/tests/test_downhill_coast.py index c1f6d93b0f..dfa404ce8d 100644 --- a/openpilot/starpilot/longitudinal/tests/test_downhill_coast.py +++ b/openpilot/starpilot/longitudinal/tests/test_downhill_coast.py @@ -1,5 +1,6 @@ from pathlib import Path from types import SimpleNamespace +from unittest.mock import Mock import math import pytest @@ -49,6 +50,52 @@ def test_gravity_only_allowance_and_approach_to_limit(): assert error(SET_SPEED + 1, limit=SET_SPEED) is None +def test_live_caller_refreshes_toggle_and_slider_before_acquiring_input(tmp_path, monkeypatch): + from openpilot.common.params import Params + from openpilot.selfdrive.controls import plannerd + + class EndLoop(Exception): + pass + + params = Params(str(tmp_path)) + cp = CarInterface.get_non_essential_params(CAR.HONDA_CIVIC) + params.put('CarParams', cp.to_bytes(), block=True) + params.put(SURPLUS_KEY, 5., block=True) + planner = LongitudinalPlanner(cp) + clock = NOW + checks = iter(((0., {KEY: True}, 950_000_000), (0., {}, 50_000_000), (5., {SURPLUS_KEY: 10.}, 950_000_000), + (5., {}, 50_000_000), (10., {KEY: False}, 1_000_000_000), (0., {}, 50_000_000))) + + def acquire(): + nonlocal clock + try: + expected, changes, delay_ns = next(checks) + except StopIteration as e: + raise EndLoop from e + assert planner.downhill_surplus_mps == pytest.approx(expected * CV.MPH_TO_MS) + for key, value in changes.items(): + params.put(key, value, block=True) + # Advance to the next read, using a real 50 ms frame around the refresh boundary. + clock += delay_ns + + sm = Mock(updated={'modelV2': False}) + sm.update.side_effect = acquire + subscribe = Mock(return_value=sm) + monkeypatch.delenv('REPLAY', raising=False) + monkeypatch.setattr(plannerd, 'Params', lambda: params) + monkeypatch.setattr(plannerd, 'LongitudinalPlanner', lambda *a, **kw: planner) + monkeypatch.setattr(plannerd.time, 'monotonic_ns', lambda: clock) + monkeypatch.setattr(plannerd, 'config_realtime_process', Mock()) + monkeypatch.setattr(plannerd, 'set_core_affinity', Mock()) + monkeypatch.setattr(plannerd.messaging, 'SubMaster', subscribe) + monkeypatch.setattr(plannerd.messaging, 'PubMaster', Mock()) + monkeypatch.setattr(plannerd.messaging, 'sub_sock', Mock()) + with pytest.raises(EndLoop): + plannerd.starpilot_main() + assert 'deviceMotion' in subscribe.call_args.args[0] + assert all('deviceMotion' in subscribe.call_args.kwargs[key] for key in ('ignore_alive', 'ignore_valid', 'ignore_avg_freq')) + + class Frame(dict): def __init__(self, **kwargs): data, _ = messages(**kwargs) diff --git a/openpilot/starpilot/ui/feature_settings_owner.py b/openpilot/starpilot/ui/feature_settings_owner.py index c9c791e36e..ee897727af 100644 --- a/openpilot/starpilot/ui/feature_settings_owner.py +++ b/openpilot/starpilot/ui/feature_settings_owner.py @@ -1293,7 +1293,7 @@ class FeatureSettingsOwner: "Leads, stops, curve limits and driver overrides retain priority.")) downhill_allowed = allowed and self.authority("long") and self._value(PLANNER_SELECTION_KEY)[0] == "1" downhill = self._bool_row(DOWNHILL_KEY, "Coast Downhills", downhill_allowed, - "Lets gravity carry the car above set speed without targeting a higher speed. Applies next drive.") + "Lets gravity carry the car above set speed without targeting a higher speed. Changes apply while driving.") rows.append(downhill) if downhill.value == "On": rows.append(self._number_row(SURPLUS_KEY, "Maximum Speed Above Set Speed", downhill_allowed)) diff --git a/prebuilt.json b/prebuilt.json index fccaf27234..74fa971c18 100644 --- a/prebuilt.json +++ b/prebuilt.json @@ -1 +1 @@ -{"protected_inventory_sha256": "0738a5911d01ab90cf487f8ccb33eb92153836e2378da9fd29e88cc0fdb3cd87", "schema_version": 1} +{"protected_inventory_sha256": "3e4859b63d8b1a6300a7caa99d5329925152c9098f4157f4e93ea4e1d00ca019", "schema_version": 1}