Delay ordinary navigation turn slowdowns with reachable braking

This commit is contained in:
firestar5683
2026-11-17 20:14:00 -06:00
parent d73a87119f
commit 616c8e5bac
4 changed files with 54 additions and 15 deletions
@@ -21,7 +21,7 @@ from openpilot.selfdrive.controls.lib.longitudinal_mpc_lib.long_mpc import T_IDX
from openpilot.selfdrive.controls.lib.drive_helpers import CONTROL_N, get_accel_from_plan, should_stop
from openpilot.selfdrive.car.cruise import V_CRUISE_MAX, V_CRUISE_UNSET
from openpilot.common.swaglog import cloudlog
from openpilot.starpilot.navigation.intent import cruise_ceiling as navigation_ceiling
from openpilot.starpilot.navigation.intent import NAVIGATION_BRAKE_MAGNITUDE, cruise_ceiling as navigation_ceiling
from openpilot.starpilot.car.tesla.preap_adaptive import startup_owner as preap_adaptive_owner
from openpilot.starpilot.longitudinal.vehicle_policy import forecast_should_stop, planner_cost_owner, far_follow_owner
from openpilot.starpilot.longitudinal.cruise_ceiling import CruiseCeiling, CurveCeiling, select_cruise_ceiling, select_curve_ceiling
@@ -392,6 +392,12 @@ class LongitudinalPlanner:
abs(A_CRUISE_MIN)))
if curve.effective_mps is not None:
global_floor = min(global_floor, A_CRUISE_MIN)
if navigation_target is not None and not sm['controlsState'].forceDecel:
# A later turn approach must remain reachable with a soft cruise profile.
global_floor = min(global_floor, -NAVIGATION_BRAKE_MAGNITUDE)
if cruise_profile is not None:
cruise_profile = replace(cruise_profile, cruise_brake_magnitude=max(cruise_profile.cruise_brake_magnitude,
NAVIGATION_BRAKE_MAGNITUDE))
if selected_acceleration is not None and cruise_profile is not None:
# The selected curve owns acceleration; following/jerks retain their owner.
cruise_profile = replace(cruise_profile, acceleration_max=selected_acceleration)
+4 -2
View File
@@ -9,6 +9,7 @@ from openpilot.starpilot.navigation.wire import navigation_state
SERVICE = 'starpilotNavigation'
MAX_AGE_NS = 2_500_000_000
NAVIGATION_BRAKE_MAGNITUDE = 1.0
TARGET_MPH = {'uturn': 5., 'sharpLeft': 10., 'sharpRight': 10., 'left': 14., 'right': 14.}
@@ -64,8 +65,9 @@ def turn_speed(maneuver, min_steer_speed: float) -> float | None:
if target is None or not math.isfinite(maneuver.distanceMeters) or maneuver.distanceMeters < 0:
return None
target = max(target * CV.MPH_TO_MS, min_steer_speed)
approach_decel = .5 if kind == 'turn' and modifier in ('left', 'right') else .45
return math.sqrt(target * target + 2 * approach_decel * max(0., maneuver.distanceMeters - 8.))
ordinary = kind == 'turn' and modifier in ('left', 'right')
approach_decel, settling_distance = (.75, 20.) if ordinary else (.45, 8.)
return math.sqrt(target * target + 2 * approach_decel * max(0., maneuver.distanceMeters - settling_distance))
def cruise_ceiling(sm, CP, now_ns: int, cruise_mps: float) -> float | None:
@@ -51,7 +51,7 @@ def test_stale_previous_drive_and_disabled_nav_are_inert(sm, field, value):
def test_turn_ceiling_and_override(sm):
cp = NS(openpilotLongitudinalControl=True, passive=False, dashcamOnly=False, minSteerSpeed=0.)
expected = ((14 * CV.MPH_TO_MS) ** 2 + 2 * .5 * (30 - 8)) ** .5
expected = ((14 * CV.MPH_TO_MS) ** 2 + 2 * .75 * (30 - 20)) ** .5
assert cruise_ceiling(sm, cp, NOW, 20.) == pytest.approx(expected)
sm['carState'].gasPressed = True
assert cruise_ceiling(sm, cp, NOW, 20.) is None
@@ -161,20 +161,24 @@ def test_approaching_turn_does_not_start_or_replace_lane_change(sm):
@pytest.mark.parametrize('modifier', ['left', 'right'])
@pytest.mark.parametrize('cruise', [10., 15., 20., 25.])
def test_ordinary_turn_onset_is_ten_percent_shorter_with_same_final_target(modifier, cruise):
target = 14 * CV.MPH_TO_MS
old_onset = 8 + (cruise ** 2 - target ** 2) / (2 * .45)
new_onset = 8 + (cruise ** 2 - target ** 2) / (2 * .5)
@pytest.mark.parametrize('mph', [25., 35., 45., 50., 55., 65., 75.])
def test_ordinary_turn_onset_scales_with_speed_and_keeps_final_target(modifier, mph):
target, cruise = 14 * CV.MPH_TO_MS, mph * CV.MPH_TO_MS
old_onset = 8 + (cruise ** 2 - target ** 2)
new_onset = 20 + (cruise ** 2 - target ** 2) / 1.5
maneuver = NS(maneuverType='turn', maneuverModifier=modifier, distanceMeters=old_onset)
assert turn_speed(maneuver, 0.) > cruise
maneuver.distanceMeters = new_onset
assert turn_speed(maneuver, 0.) == pytest.approx(cruise)
assert new_onset - 8 == pytest.approx(.9 * (old_onset - 8))
maneuver.distanceMeters = 8.
assert turn_speed(maneuver, 0.) == pytest.approx(target)
maneuver.distanceMeters = 0.
assert turn_speed(maneuver, 0.) == pytest.approx(target)
assert new_onset < old_onset
if mph == 50.:
assert .19 < new_onset / 1609.344 < .21
elif mph >= 65.:
assert new_onset > .3 * 1609.344 # High-speed braking needs more than a fixed 0.2-mile cap.
for distance in (20., 8., 0.):
maneuver.distanceMeters = distance
assert turn_speed(maneuver, 0.) == pytest.approx(target)
assert turn_speed(maneuver, 10.) == pytest.approx(10.)
@pytest.mark.parametrize('kind,modifier,mph', [('turn', 'sharpLeft', 10.), ('turn', 'sharpRight', 10.),
@@ -182,3 +186,30 @@ def test_ordinary_turn_onset_is_ten_percent_shorter_with_same_final_target(modif
def test_tight_turn_approaches_keep_the_original_margin(kind, modifier, mph):
maneuver = NS(maneuverType=kind, maneuverModifier=modifier, distanceMeters=100.)
assert turn_speed(maneuver, 0.) == pytest.approx(((mph * CV.MPH_TO_MS) ** 2 + .9 * 92.) ** .5)
def test_native_navigation_approach_has_braking_capacity_without_changing_cruise_or_jerk():
from unittest.mock import patch
from opendbc.car.hyundai.interface import CarInterface
from opendbc.car.hyundai.values import CAR
from openpilot.selfdrive.controls.lib import longitudinal_planner as module
from openpilot.starpilot.longitudinal.tests.test_conditional_handoff import Frame, NOW as FRAME_NOW, DRIVE
cp = CarInterface.get_non_essential_params(CAR.HYUNDAI_IONIQ_6)
cp.openpilotLongitudinalControl = True
for response in (None, 'eco', 'standard', 'sport'):
for guided in (False, True):
planner = module.LongitudinalPlanner(cp, init_v=20.)
for tick in range(30):
frame = Frame()
now = FRAME_NOW + tick * 50_000_000
frame['carState'].vEgo = 20.
frame['carState'].gearShifter = 'drive'
frame.logMonoTime = dict.fromkeys(frame, now)
with patch.object(module, 'navigation_ceiling', return_value=10. if guided else None), \
patch.object(module, 'get_cruise_accel', wraps=module.get_cruise_accel) as cruise:
planner.update(frame, now_ns=now, drive_id=DRIVE, traffic_mode=False, global_braking_response=response)
if guided:
assert cruise.call_args.kwargs['base_brake_floor'] <= -1.
assert planner.a_cruise >= -max(1., {'sport': 2.4, 'standard': 1.2}.get(response, 0.))
else:
assert cruise.call_args.kwargs['base_brake_floor'] == -{None: .5, 'eco': .6, 'standard': 1.2, 'sport': 2.4}[response]
+1 -1
View File
@@ -1 +1 @@
{"protected_inventory_sha256": "f7fa5d53c496bc5d9f3902983ada1104d8808b22dfcbcf3b6b775bf5a3b924de", "schema_version": 1}
{"protected_inventory_sha256": "99d612f7a0bbfe0f72e3c8d0512b896e521b808b0a09151c021101d72d915eba", "schema_version": 1}