From 9272ea86cbd4cf816181ce23ee3d9cef41bb3844 Mon Sep 17 00:00:00 2001 From: firestarsdog <229254897+firestarsdog@users.noreply.github.com> Date: Thu, 19 Nov 2026 13:18:41 -0500 Subject: [PATCH] Soften the brake onset after SLC overshoots --- .../controls/lib/longitudinal_planner.py | 15 +++- openpilot/starpilot/longitudinal/README.md | 9 ++- .../longitudinal/tests/test_global_braking.py | 2 +- .../tests/test_profile_runtime.py | 2 +- .../longitudinal/tests/test_slc_braking.py | 73 +++++++++++++++++++ .../tests/test_traffic_acceleration.py | 3 +- openpilot/starpilot/speed_limits/README.md | 3 +- .../tests/test_cluster_feedback.py | 2 +- 8 files changed, 98 insertions(+), 11 deletions(-) create mode 100644 openpilot/starpilot/longitudinal/tests/test_slc_braking.py diff --git a/openpilot/selfdrive/controls/lib/longitudinal_planner.py b/openpilot/selfdrive/controls/lib/longitudinal_planner.py index 3de0daf45e..eb39bb2ad4 100755 --- a/openpilot/selfdrive/controls/lib/longitudinal_planner.py +++ b/openpilot/selfdrive/controls/lib/longitudinal_planner.py @@ -50,6 +50,8 @@ A_CRUISE_MAX_BP = [0., 10.0, 25., 40.] J_CRUISE_VALS = [1.6, 1.2, 0.8, 0.6] A_CRUISE_MIN = -0.5 CRUISE_ACCEL_RESPONSE_TIME = 2.0 +SLC_BRAKE_ONSET_JERK = 0.15 # m/s^3: how fast braking may build after an SLC overshoot +SLC_BRAKE_ONSET_ACCEL = -0.3 # m/s^2: past this the brakes are engaged and normal jerk resumes CONTROL_N_T_IDX = ModelConstants.T_IDXS[:CONTROL_N] MIN_ALLOW_THROTTLE_SPEED = 2.5 NATIVE_ALLOW_THROTTLE_PROBABILITY = 0.4 @@ -72,7 +74,7 @@ def get_coast_accel(pitch): def get_cruise_accel(e2e, v_cruise, v_ego, a_cruise_prev, angle_steers, CP, dt, accel_coast, allow_throttle, profile: AppliedProfile | None = None, base_brake_floor: float = A_CRUISE_MIN, selected_acceleration_max: float | None = None, faster_takeoffs: bool = False, - *, a_target_prev: float | None = None): + *, a_target_prev: float | None = None, ease_slc_braking: bool = False): ceiling = TAKEOFF_ACCEL_MAX if faster_takeoffs else ACCEL_MAX max_accel = ceiling if e2e else get_max_accel(v_ego, faster_takeoffs) if selected_acceleration_max is not None and not e2e: @@ -97,10 +99,16 @@ def get_cruise_accel(e2e, v_cruise, v_ego, a_cruise_prev, angle_steers, CP, dt, speed_error = v_cruise - v_ego if not e2e and not faster_takeoffs and v_ego >= MIN_ALLOW_THROTTLE_SPEED and speed_error > 0.: speed_error /= CRUISE_ACCEL_RESPONSE_TIME + elif ease_slc_braking and speed_error < 0.: + speed_error /= CRUISE_ACCEL_RESPONSE_TIME # correct an SLC overshoot as gradually as an approach target_accel = np.clip(speed_error, cruise_min, max_accel) j_cruise = np.interp(v_ego, A_CRUISE_MAX_BP, J_CRUISE_VALS) decel_start = a_cruise_prev if a_target_prev is None else min(a_cruise_prev, a_target_prev) - target_accel = float(np.clip(target_accel, decel_start - j_cruise * dt, a_cruise_prev + j_cruise * dt)) + lower = decel_start - j_cruise * dt + if ease_slc_braking and decel_start > SLC_BRAKE_ONSET_ACCEL: + # Ease braking in from zero so friction brakes engage without a jolt; lifting off the throttle keeps normal jerk. + lower = max(lower, min(decel_start, 0.) - SLC_BRAKE_ONSET_JERK * dt) + target_accel = float(np.clip(target_accel, lower, a_cruise_prev + j_cruise * dt)) return target_accel @@ -413,7 +421,8 @@ class LongitudinalPlanner: self.a_cruise, steer_angle_without_offset, self.CP, self.dt, accel_coast, self.allow_throttle, cruise_profile, base_brake_floor=global_floor, selected_acceleration_max=selected_acceleration, faster_takeoffs=faster_takeoffs, - a_target_prev=self.output_a_target) + a_target_prev=self.output_a_target, + ease_slc_braking=v_cruise == ceiling.effective_mps and not relevant_lead) if self.button_cruise is not None: button_target = self.button_cruise.sample( sm, now_ns=sample_now_ns, drive_id=drive_id, target_speed=v_cruise, diff --git a/openpilot/starpilot/longitudinal/README.md b/openpilot/starpilot/longitudinal/README.md index e5cfbba1c6..46f211b0df 100644 --- a/openpilot/starpilot/longitudinal/README.md +++ b/openpilot/starpilot/longitudinal/README.md @@ -31,8 +31,11 @@ The planner records the decision in `last_cruise_ceiling_status`; it does not publish that diagnostic or alter the longitudinal plan schema. This optional input changes only the cruise candidate before the planner's -existing jerk limiting. SLC uses the ordinary cruise braking response and selected -profile for its capped target; a cap does not strengthen a softer cruise profile. Lead-relevance checks still prevent cruise comfort +existing jerk limiting. SLC uses the selected profile for its capped target; a cap does +not strengthen a softer cruise profile. While the SLC cap is the final cruise target and no +braking-relevant lead is present, an overshoot is corrected over `CRUISE_ACCEL_RESPONSE_TIME` +and braking builds at 0.15 m/s³ until -0.3 m/s², so friction brakes engage gently; braking +already requested elsewhere keeps the normal jerk. Lead-relevance checks still prevent cruise comfort tuning from weakening braking for a close, slow or braking lead; malformed lead evidence also vetoes that tuning. The native MPC, lead, e2e, stop and acceleration arbitration remain in place. An inactive planner may still compute a nonzero @@ -80,7 +83,7 @@ acceleration/braking limits. It cannot widen the native car acceleration range. Valid saved curves above native acceleration bounds are intersected with those bounds without dropping their other tuning or rewriting the saved document. Lead, e2e and cruise candidates still arbitrate normally. A lower explicit -cruise or curve ceiling retains at least native cruise braking even when a saved profile asks +cruise or curve ceiling retains at least the native cruise braking floor even when a saved profile asks for a softer approach; stronger configured braking may still apply. `forceDecel` keeps the native cruise braking response. Stock ACC and lateral-only control receive no profile effect. The acados model and cereal schemas are unchanged. diff --git a/openpilot/starpilot/longitudinal/tests/test_global_braking.py b/openpilot/starpilot/longitudinal/tests/test_global_braking.py index fe6416b3d5..b16c633b6f 100644 --- a/openpilot/starpilot/longitudinal/tests/test_global_braking.py +++ b/openpilot/starpilot/longitudinal/tests/test_global_braking.py @@ -54,7 +54,7 @@ class GlobalBrakingTests(unittest.TestCase): ordinary.mpc.params[:, [0, 1, 4, 5]].tolist()) def test_ceiling_hazards_and_category_override(self): - ceiling = CruiseCeiling(19.0, ACTIVE) + ceiling = CruiseCeiling(18.0, ACTIVE) # 2 m/s over: the halved SLC correction (-1.0) still reaches the eco floor (-0.6) eco, cruise = self.run_planner('eco', ceiling=ceiling) self.assertEqual(cruise['base_brake_floor'], -0.6) self.assertAlmostEqual(eco.a_cruise, -0.6) diff --git a/openpilot/starpilot/longitudinal/tests/test_profile_runtime.py b/openpilot/starpilot/longitudinal/tests/test_profile_runtime.py index b57fc0405c..be7425df5e 100644 --- a/openpilot/starpilot/longitudinal/tests/test_profile_runtime.py +++ b/openpilot/starpilot/longitudinal/tests/test_profile_runtime.py @@ -650,7 +650,7 @@ class ProfileRuntimeTests(unittest.TestCase): strong = LongitudinalPlanner(self.cp, init_v=V_EGO) soft_tuning = ProfileTuning('standard', 1.45, 1, 1, 1, 1, 1, None, 0.75) strong_tuning = ProfileTuning('standard', 1.45, 1, 1, 1, 1, 1, None, 1.8) - for _ in range(40): + for _ in range(60): # 3 s: SLC braking takes 2 s to ease in to -0.3, then reaches each profile floor sm, _ = messages() sm['carControl'].longActive = True baseline.update(sm, cruise_ceiling=ceiling) diff --git a/openpilot/starpilot/longitudinal/tests/test_slc_braking.py b/openpilot/starpilot/longitudinal/tests/test_slc_braking.py new file mode 100644 index 0000000000..84f1617a14 --- /dev/null +++ b/openpilot/starpilot/longitudinal/tests/test_slc_braking.py @@ -0,0 +1,73 @@ +"""SLC overshoot braking eases in without weakening lead, model or acceleration responses.""" +from unittest.mock import patch + +import pytest + +from opendbc.car.honda.interface import CarInterface +from opendbc.car.honda.values import CAR +from openpilot.selfdrive.controls.lib import longitudinal_planner as planner_module +from openpilot.selfdrive.controls.lib.longitudinal_planner import (A_CRUISE_MIN, SLC_BRAKE_ONSET_ACCEL, SLC_BRAKE_ONSET_JERK, + LongitudinalPlanner, get_cruise_accel) +from openpilot.starpilot.longitudinal.cruise_ceiling import CruiseCeiling +from openpilot.starpilot.longitudinal.tests.test_cruise_ceiling import ACTIVE, V_EGO, messages +from openpilot.starpilot.longitudinal.tests.test_cruise_taps import camry + +DT = .05 +CAP = CruiseCeiling(60. / 3.6, ACTIVE) # below the fixture's 100 km/h SET, with the car 3.3 m/s over it + + +def test_overshoot_braking_eases_in_then_uses_normal_jerk(): + cp, accel, trace = camry(), 1.2, [] + for _ in range(100): + previous, accel = accel, get_cruise_accel(False, 10., 12., accel, 0., cp, DT, 0., True, ease_slc_braking=True) + trace.append((previous, previous - accel)) + normal = get_cruise_accel(False, 10., 12., 1.2, 0., cp, DT, 0., True) + assert trace[0][1] == pytest.approx(1.2 - normal) # lifting off the throttle is not slowed + onset = [step for previous, step in trace if SLC_BRAKE_ONSET_ACCEL < previous <= 0.] + assert max(onset) <= SLC_BRAKE_ONSET_JERK * DT + 1e-9 + assert len(onset) * DT >= abs(SLC_BRAKE_ONSET_ACCEL) / SLC_BRAKE_ONSET_JERK - DT # about 2 s to reach -0.3 + assert accel == pytest.approx(A_CRUISE_MIN) + + +@pytest.mark.parametrize('error', (-.2, -.4)) +def test_overshoot_correction_is_halved(error): + assert get_cruise_accel(False, 28. + error, 28., error / 2, 0., camry(), DT, 0., True, + ease_slc_braking=True) == pytest.approx(error / 2) + + +def test_braking_requested_elsewhere_keeps_normal_jerk(): + args = (False, 20., 25., 0., 0., camry(), DT, 0., True) + assert get_cruise_accel(*args, ease_slc_braking=True) == pytest.approx(-SLC_BRAKE_ONSET_JERK * DT) + assert get_cruise_accel(*args, a_target_prev=-1., ease_slc_braking=True) == get_cruise_accel(*args, a_target_prev=-1.) + + +@pytest.mark.parametrize('faster', (False, True)) +def test_acceleration_is_unchanged(faster): + args = (False, 28.4, 28., .1, 0., camry(), DT, 0., True) + assert get_cruise_accel(*args, faster_takeoffs=faster, ease_slc_braking=True) == get_cruise_accel(*args, faster_takeoffs=faster) + + +def planner_easing(*, ceiling=CAP, lead=False, e2e=False): + planner = LongitudinalPlanner(CarInterface.get_non_essential_params(CAR.HONDA_CIVIC), init_v=V_EGO) + real, seen = planner_module.get_cruise_accel, [] + + def observe(*args, **options): + seen.append(options['ease_slc_braking']) + return real(*args, **options) + + with patch.object(planner_module, 'get_cruise_accel', side_effect=observe): + for _ in range(3): + planner.update(messages(lead=lead, e2e=e2e)[0], cruise_ceiling=ceiling) + return seen, planner + + +@pytest.mark.parametrize('ceiling,lead,eased', ((CAP, False, True), (CAP, True, False), (None, False, False), + (CruiseCeiling(40., ACTIVE), False, False))) +def test_planner_eases_only_toward_an_applied_slc_cap_without_a_braking_lead(ceiling, lead, eased): + assert planner_easing(ceiling=ceiling, lead=lead)[0] == [eased] * 3 + + +def test_model_braking_still_wins_while_slc_braking_is_eased(): + seen, planner = planner_easing(e2e=True) + assert seen == [True] * 3 + assert planner.output_a_target < planner.a_cruise diff --git a/openpilot/starpilot/longitudinal/tests/test_traffic_acceleration.py b/openpilot/starpilot/longitudinal/tests/test_traffic_acceleration.py index e497e0ccc3..e3cd8def2a 100644 --- a/openpilot/starpilot/longitudinal/tests/test_traffic_acceleration.py +++ b/openpilot/starpilot/longitudinal/tests/test_traffic_acceleration.py @@ -76,7 +76,8 @@ class TrafficAccelerationPlannerTests(unittest.TestCase): sm['carState'].vCruise = 15.0 * 3.6 baseline.update(sm, profile_tuning=target, traffic_mode=True) self.assertEqual(traffic.mpc.solution_status, 0) - self.assertLessEqual(traffic.a_cruise, baseline.a_cruise + 1e-8) + if force: # cap inactive, so cruise matches SET exactly; an applied cap eases its braking in + self.assertLessEqual(traffic.a_cruise, baseline.a_cruise + 1e-8) self.assertEqual(traffic.last_cruise_ceiling_status, 'force_decel' if force else 'applied') self.assertIsNotNone(traffic.last_profile) self.assertAlmostEqual(traffic.last_profile.cruise_brake_magnitude, 0.35) diff --git a/openpilot/starpilot/speed_limits/README.md b/openpilot/starpilot/speed_limits/README.md index 8493339fb6..3af26492c4 100644 --- a/openpilot/starpilot/speed_limits/README.md +++ b/openpilot/starpilot/speed_limits/README.md @@ -6,7 +6,8 @@ physical-speed coordinates once. Accepting a limit never rewrites SET. Native PC cruise owns its setpoint; non-PCM software cruise and the dedicated Subaru software cruise mode retain their existing owners. Automatic SLC requires system longitudinal control. Lead, curve, stop and actuator safety constraints retain their existing owners. -SLC works with either the StarPilot or upstream longitudinal planner. +SLC works with either the StarPilot or upstream longitudinal planner. The StarPilot planner +eases braking after an overshoot past the cap; the upstream planner keeps its native response. ## New limits and adoption diff --git a/openpilot/starpilot/speed_limits/tests/test_cluster_feedback.py b/openpilot/starpilot/speed_limits/tests/test_cluster_feedback.py index b6a88aee6b..fd8025cd38 100644 --- a/openpilot/starpilot/speed_limits/tests/test_cluster_feedback.py +++ b/openpilot/starpilot/speed_limits/tests/test_cluster_feedback.py @@ -77,7 +77,7 @@ def test_actual_native_planner_responds_before_the_dashboard_updates(drive): current.update(fixture.sm, cruise_ceiling=result.ceiling) legacy.update(fixture.sm, cruise_ceiling=old.ceiling) assert current.mpc.solution_status == legacy.mpc.solution_status == 0 - assert current.output_a_target < -.01 + assert current.output_a_target < -1e-3 # braking has begun; eased SLC braking starts small assert abs(legacy.output_a_target) < 1e-5 np.testing.assert_array_equal(current.mpc.params, legacy.mpc.params) np.testing.assert_array_equal(current.mpc.x_sol, legacy.mpc.x_sol)