diff --git a/openpilot/selfdrive/controls/controlsd.py b/openpilot/selfdrive/controls/controlsd.py index b66fd74715..8c083fdc56 100755 --- a/openpilot/selfdrive/controls/controlsd.py +++ b/openpilot/selfdrive/controls/controlsd.py @@ -633,6 +633,13 @@ class Controls: new_desired_curvature = limit_curvature_to_plan(model_v2, new_desired_curvature, CS.vEgo, min_reach=4.0) else: self.subaru_twitch_guard_armed = update_twitch_guard(False, CS.vEgo, CS.standstill) + kona_tau = getattr(self, "kona_smoothing", 0.) + from openpilot.starpilot.car.subaru.smoothing import seconds as subaru_smooth_seconds, control_maximum + + lat_delay = self.sm["lateralDelay"].lateralDelay + LAT_SMOOTH_SECONDS + subaru_smooth_seconds(CS.vEgo, control_maximum(self)) + kona_tau + if getattr(self, "volkswagen_smoothing", None) is not None: + lat_delay += self.volkswagen_smoothing.seconds(int(self.sm.logMonoTime["modelV2"]), time.monotonic_ns()) + if self.highway_smoothing_supported: preferences = getattr(self, "live_steering_preferences", None) if preferences is not None: @@ -656,12 +663,13 @@ class Controls: or self.sm.valid['lateralManeuverPlan'] ), predicted=predicted, + steering_limited=self.steer_limited_by_safety, + actuator_delay_s=lat_delay, ) lane_base_curvature = new_desired_curvature self.lane_centering_applied = 0.0 self.lane_centering_inactive_reason = 'inactive' self.last_lane_centering_result = None - kona_tau = getattr(self, "kona_smoothing", 0.) lane_delay = None if lane_centering is None and self.lane_centering_host is not None: now_ns = int(self.sm.logMonoTime['carState']) if os.getenv('REPLAY') == '1' else time.monotonic_ns() @@ -723,12 +731,6 @@ class Controls: self.desired_curvature = guarded curvature_limited = True self.lane_centering_applied = self.desired_curvature - baseline_curvature - from openpilot.starpilot.car.subaru.smoothing import seconds as subaru_smooth_seconds, control_maximum - - lat_delay = self.sm["lateralDelay"].lateralDelay + LAT_SMOOTH_SECONDS + subaru_smooth_seconds(CS.vEgo, control_maximum(self)) + kona_tau - if getattr(self, "volkswagen_smoothing", None) is not None: - lat_delay += self.volkswagen_smoothing.seconds(int(self.sm.logMonoTime["modelV2"]), time.monotonic_ns()) - actuators.curvature = self.desired_curvature if cycle_timing is not None: cycle_timing.mark() diff --git a/openpilot/selfdrive/controls/lib/highway_correction_gain.py b/openpilot/selfdrive/controls/lib/highway_correction_gain.py index 134bfb7f8f..1c04deb46a 100644 --- a/openpilot/selfdrive/controls/lib/highway_correction_gain.py +++ b/openpilot/selfdrive/controls/lib/highway_correction_gain.py @@ -25,6 +25,7 @@ TIGHT_CURVE_OFF = 2.0 # m/s^2 ENVELOPE_HOLD = 2.0 # s ENVELOPE_RELEASE_RATE = 0.3 # m/s^2 per second BYPASS_FADE_RATE = 2.0 # per second +LIMIT_RECOVERY_RAMP = 0.4 # s MIN_SETTING = 1.0 MAX_SETTING = 10.0 KEY = "HighwaySmoothingLevel" @@ -83,9 +84,11 @@ class HighwayCorrectionGain: self.prediction_envelope = _Envelope() self.bypass_weight = 0.0 self.weight = 0.0 + self.time_clear_of_limit: float | None = None + self.limit_recovery = 1.0 def update(self, curvature: float, v_ego: float, lat_active: bool, gain: float, bypass: bool, - predicted: tuple[float, ...] | None = None) -> float: + predicted: tuple[float, ...] | None = None, *, steering_limited: bool = False, actuator_delay_s: float = 0.0) -> float: if not lat_active: self.reset(curvature) return curvature @@ -117,8 +120,19 @@ class HighwayCorrectionGain: step = BYPASS_FADE_RATE * DT_CTRL self.bypass_weight += min(max((0.0 if bypass else 1.0) - self.bypass_weight, -step), step) + # Leave the original request intact while limited and let the delayed commands clear before blending back in. + if steering_limited: + self.time_clear_of_limit = self.limit_recovery = 0.0 + elif self.time_clear_of_limit is not None: + if math.isfinite(actuator_delay_s) and actuator_delay_s >= 0.0: + target = max((self.time_clear_of_limit - actuator_delay_s) / LIMIT_RECOVERY_RAMP, 0.0) + self.limit_recovery = min(target, self.limit_recovery + DT_CTRL / LIMIT_RECOVERY_RAMP, 1.0) + self.time_clear_of_limit = None if self.limit_recovery == 1.0 else self.time_clear_of_limit + DT_CTRL + else: + self.time_clear_of_limit = self.limit_recovery = 0.0 + gain = min(max(gain, MIN_SETTING / MAX_SETTING), 1.0) - self.weight = self.bypass_weight * _smoothstep(v_ego, SPEED_OFF, SPEED_ON) * gate + self.weight = self.bypass_weight * _smoothstep(v_ego, SPEED_OFF, SPEED_ON) * gate * self.limit_recovery k = 1.0 - self.weight * (1.0 - gain) if k >= 1.0: return curvature diff --git a/openpilot/selfdrive/controls/tests/test_highway_correction_gain.py b/openpilot/selfdrive/controls/tests/test_highway_correction_gain.py index 0fc872f171..821172ccab 100644 --- a/openpilot/selfdrive/controls/tests/test_highway_correction_gain.py +++ b/openpilot/selfdrive/controls/tests/test_highway_correction_gain.py @@ -119,6 +119,28 @@ def test_bypass_fades_without_steps(): assert np.max(np.abs(np.diff(out))) < 1.5 * max_raw_step +def test_limit_pauses_smoothing_until_the_actuator_delay_clears(): + hcg = HighwayCorrectionGain() + _run(hcg, _weave(seconds=5.0), gain=0.5) + assert hcg.weight == 1.0 + raw = 0.04 / _V_HWY ** 2 + delay = 0.3 + assert hcg.update(raw, _V_HWY, True, 0.5, False, steering_limited=True, actuator_delay_s=delay) == raw + for _ in range(round(delay / DT_CTRL)): + assert hcg.update(raw, _V_HWY, True, 0.5, False, actuator_delay_s=delay) == raw + weights = [] + for _ in range(41): + hcg.update(raw, _V_HWY, True, 0.5, False, actuator_delay_s=delay) + weights.append(hcg.weight) + assert 0.4 < weights[20] < 0.6 + assert weights[-1] == 1.0 + assert np.max(np.diff(weights)) < 0.026 + + assert hcg.update(raw, _V_HWY, True, 0.5, False, steering_limited=True, actuator_delay_s=delay) == raw + for _ in range(100): + assert hcg.update(raw, _V_HWY, True, 0.5, False, actuator_delay_s=math.nan) == raw + + def test_active_from_40_mph(): v = 40.0 * 0.44704 raw = _weave(v_ego=v) diff --git a/prebuilt.json b/prebuilt.json index c09c8c103c..bf53b03b4b 100644 --- a/prebuilt.json +++ b/prebuilt.json @@ -1 +1 @@ -{"protected_inventory_sha256": "00c942587cfe52c69855bdb413865a7beb5025c51c45fc9397d3cd33c9b0b06f", "schema_version": 1} +{"protected_inventory_sha256": "6de35a0f2b44802d4de951ecb99bad2ac114f8b0ca8c70d632343cd38aded6ef", "schema_version": 1}