diff --git a/openpilot/selfdrive/controls/lib/longitudinal_planner.py b/openpilot/selfdrive/controls/lib/longitudinal_planner.py index b948f4d37f..fd4fe7010a 100755 --- a/openpilot/selfdrive/controls/lib/longitudinal_planner.py +++ b/openpilot/selfdrive/controls/lib/longitudinal_planner.py @@ -6,6 +6,7 @@ from dataclasses import replace import numpy as np import openpilot.cereal.messaging as messaging +from openpilot.cereal import log from opendbc.car.interfaces import ACCEL_MIN, ACCEL_MAX from openpilot.starpilot.longitudinal.lead_takeoff_preferences import MAX_ACCEL as TAKEOFF_ACCEL_MAX from openpilot.common.constants import CV @@ -13,6 +14,7 @@ from openpilot.common.filter_simple import FirstOrderFilter from openpilot.common.realtime import DT_MDL from openpilot.selfdrive.modeld.constants import ModelConstants from openpilot.selfdrive.controls.lib.accel_boost import AccelBoost +from openpilot.selfdrive.controls.lib.personality_accel_boost import PersonalityAccelBoost from openpilot.starpilot.longitudinal.encouragement import Encouragement from openpilot.selfdrive.controls.lib.longcontrol import LongCtrlState from openpilot.selfdrive.controls.lib.longitudinal_mpc_lib.long_mpc import (LongitudinalMpc, LongitudinalPlanSource, get_T_FOLLOW, get_jerk_factor, @@ -146,6 +148,7 @@ class LongitudinalPlanner: self.clock_ns = clock_ns self.allow_throttle = True self.accel_boost = AccelBoost() + self.personality_accel_boost = PersonalityAccelBoost(dt) self.encouragement = Encouragement() self.plan_drive_id = 0 self.throttle_gate = ModelThrottleGate() @@ -552,14 +555,26 @@ class LongitudinalPlanner: self.a_cruise = button_target cruise_should_stop = should_stop(v_ego, self.a_cruise, self.stop_speed) + e2e_active = sm['selfdriveState'].experimentalMode + personality = sm['selfdriveState'].personality + relaxed = personality == log.LongitudinalPersonality.relaxed + boost_blocked = ( + not e2e_active or reset_state or self.fcw or not self.allow_throttle or self.force_stop_plan.forcing or + output_should_stop_mpc or output_should_stop_e2e or cruise_should_stop or self.mpc.solution_status != 0 or + curve.effective_mps is not None or traffic_mode is not False or sm['controlsState'].forceDecel or + not all(getattr(sm, health).get(service, False) + for health in ('alive', 'valid', 'freq_ok') for service in ('modelV2', 'carState', 'radarState')) + ) encouraged_model = self.encouragement.update( sm, self.CP, now_ns=sample_now_ns, drive_id=drive_id, dt=self.dt, target_speed=v_cruise, model_accel=output_a_target_e2e, mpc_accel=output_a_target_mpc, cruise_accel=self.a_cruise, lateral_accel=(v_ego ** 2 * steer_angle_without_offset * CV.DEG_TO_RAD / (self.CP.steerRatio * self.CP.wheelbase) if self.CP.steerRatio > 0 and self.CP.wheelbase > 0 else math.inf), - blocked=bool(experimental_speed_nudge is not True or reset_state or self.fcw or not self.allow_throttle or self.force_stop_plan.forcing or - output_should_stop_mpc or cruise_should_stop or self.mpc.solution_status != 0 or - curve.effective_mps is not None or traffic_mode is not False)) + blocked=bool(boost_blocked or not relaxed or experimental_speed_nudge is not True)) + if boost_blocked or relaxed: + self.personality_accel_boost.reset() + else: + encouraged_model = self.personality_accel_boost.update(sm, output_a_target_e2e, 0.0) manual_model = self.accel_boost.update(sm, output_a_target_e2e, output_a_target_mpc, self.a_cruise) output_a_target_e2e = max(manual_model, encouraged_model) @@ -611,7 +626,7 @@ class LongitudinalPlanner: takeoff_frame = project_takeoff( sm, self.CP, now_ns=sample_now_ns, drive_id=takeoff_drive_id, stop_plan=self.force_stop_plan, acceleration_max=takeoff_max, mpc_accel=output_a_target_mpc, follow_seconds=float(self.mpc.params[0, 4]), - blocked=bool(experimental_speed_nudge is not True or reset_state or self.fcw or not self.allow_throttle or traffic_mode is not False or + blocked=bool(reset_state or self.fcw or not self.allow_throttle or traffic_mode is not False or self.mpc.solution_status != 0)) output_a_target, self.output_should_stop = self.lead_takeoff.step( True, takeoff_frame, output_a_target, self.output_should_stop) diff --git a/openpilot/selfdrive/controls/lib/personality_accel_boost.py b/openpilot/selfdrive/controls/lib/personality_accel_boost.py new file mode 100644 index 0000000000..c1b410d6a0 --- /dev/null +++ b/openpilot/selfdrive/controls/lib/personality_accel_boost.py @@ -0,0 +1,45 @@ +import numpy as np +from openpilot.cereal import log +from openpilot.common.filter_simple import FirstOrderFilter +from openpilot.common.realtime import DT_MDL + +PLAN_GATE = [-0.5, 1.0] # m/s the plan runs ahead of holding the current speed: no boost at 0.5 behind, full boost at 1 ahead + + +def get_accel_boost(personality=log.LongitudinalPersonality.standard): + # extra accel on top of the model's in m/s^2, and the extra fraction of the model's accel when speeding up + if personality == log.LongitudinalPersonality.relaxed: + return 0.0, 0.0 + elif personality == log.LongitudinalPersonality.standard: + return 0.15, 0.1 + elif personality == log.LongitudinalPersonality.aggressive: + return 0.3, 0.2 + else: + raise NotImplementedError("Longitudinal personality not supported") + + +class PersonalityAccelBoost: + def __init__(self, dt=DT_MDL): + self.plan_ahead_filter = FirstOrderFilter(0.0, 0.3, dt, initialized=False) # smooths 1-3 frame blips in the plan + self.bias_filter = FirstOrderFilter(0.0, 1.0, dt) # the bias drops at once when the plan slows and comes back over ~1 s + + def reset(self): + self.plan_ahead_filter.initialized = False + self.bias_filter.x = 0.0 + + def update(self, sm, output_a_target_e2e, curve_offset): + bias, gain = get_accel_boost(sm['selfdriveState'].personality) + pos = sm['modelV2'].position + # m/s the plan's average speed over the next 10 s (distance / time) runs ahead of the current speed + if (not len(pos.x) or len(pos.x) != len(pos.t) or not np.isfinite(pos.x[-1]) or + not np.isfinite(pos.t[-1]) or pos.t[-1] <= 0.0 or not np.isfinite(sm['carState'].vEgo)): + self.reset() + return output_a_target_e2e + plan_ahead = pos.x[-1] / pos.t[-1] - max(sm['carState'].vEgo, 1.0) + self.plan_ahead_filter.update(plan_ahead) + gate = np.interp(self.plan_ahead_filter.x, PLAN_GATE, [0.0, 1.0]) if curve_offset == 0.0 else 0.0 # clear road ahead, none while slowing for a curve + self.bias_filter.x = min(self.bias_filter.x, bias * gate) # drop at once when a slowdown shows up in the plan + self.bias_filter.update(bias * gate) # come back slowly once it clears + + gas_boost = gate * gain * max(output_a_target_e2e, 0.0) # boost gas only while the road ahead is clear, braking is left to the model + return output_a_target_e2e + gas_boost + self.bias_filter.x diff --git a/openpilot/starpilot/galaxy/setting_descriptions.py b/openpilot/starpilot/galaxy/setting_descriptions.py index 935dbfa11f..1e504f9432 100644 --- a/openpilot/starpilot/galaxy/setting_descriptions.py +++ b/openpilot/starpilot/galaxy/setting_descriptions.py @@ -70,7 +70,7 @@ DESCRIPTIONS = { 'RedneckCruise': 'Adjusts conventional factory cruise with Resume and Set after the next startup.', 'LowSpeedCruiseOnSet': ('Uses the vehicle’s minimum engagement speed as the floor when pressing SET with StarPilot cruise. ' + 'SET uses your current speed when you are above that floor.'), - 'ExperimentalSpeedNudge': ('Gently recovers toward set speed when Experimental mode hesitates on a clear road. ' + + 'ExperimentalSpeedNudge': ('Helps End to End return to set speed on clear roads in the Relaxed personality. ' + 'Leads, stops, curve limits and driver overrides retain priority.'), 'FasterLeadTakeoff': ('Improves launches from a stop and responds sooner when a lead starts moving. ' + 'Selected acceleration profiles, stop detection and vehicle limits still apply.'), diff --git a/openpilot/starpilot/galaxy/tests/test_lead_approach_settings.py b/openpilot/starpilot/galaxy/tests/test_lead_approach_settings.py index 114bc8028e..06a5cf8b3c 100644 --- a/openpilot/starpilot/galaxy/tests/test_lead_approach_settings.py +++ b/openpilot/starpilot/galaxy/tests/test_lead_approach_settings.py @@ -17,7 +17,7 @@ from openpilot.starpilot.ui.feature_settings_owner import FeatureSettingsOwner @pytest.fixture(params=[ ("LeadApproachBuffer", "Approaching lead buffer", "Off"), ("FasterLeadTakeoff", "Faster Takeoffs", "Off"), - ("ExperimentalSpeedNudge", "Experimental Speed Nudge", "On"), + ("ExperimentalSpeedNudge", "Speed Nudge in Relaxed", "On"), ("LowSpeedCruiseOnSet", "Low-Speed Cruise Start", "Off"), ("DisengageOnAccelerator", "Disengage on Accelerator Pedal", "Off"), ]) diff --git a/openpilot/starpilot/ui/feature_settings_owner.py b/openpilot/starpilot/ui/feature_settings_owner.py index 6aa2bf6924..ce28630d93 100644 --- a/openpilot/starpilot/ui/feature_settings_owner.py +++ b/openpilot/starpilot/ui/feature_settings_owner.py @@ -1255,8 +1255,8 @@ class FeatureSettingsOwner: rows.append(self._bool_row(LEAD_TAKEOFF_KEY, "Faster Takeoffs", self.authority("preferences"), reason="Improves launches from a stop and responds sooner when a lead starts moving. " + "Selected acceleration profiles, stop detection and vehicle limits still apply.")) - rows.append(self._bool_row(NUDGE_KEY, "Experimental Speed Nudge", self.authority("preferences"), - reason="Gently recovers toward set speed when Experimental mode hesitates on a clear road. " + + rows.append(self._bool_row(NUDGE_KEY, "Speed Nudge in Relaxed", self.authority("preferences"), + reason="Helps End to End return to set speed on clear roads in the Relaxed personality. " + "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, diff --git a/openpilot/starpilot/ui/tests/test_speed_nudge_settings.py b/openpilot/starpilot/ui/tests/test_speed_nudge_settings.py index a0f670736a..3e34cca0b4 100644 --- a/openpilot/starpilot/ui/tests/test_speed_nudge_settings.py +++ b/openpilot/starpilot/ui/tests/test_speed_nudge_settings.py @@ -15,7 +15,7 @@ def test_nudge_is_default_on_persistent_and_editable_without_a_vehicle(): owner = FeatureSettingsOwner(params, lambda group: group == 'preferences', vehicle_fingerprint=lambda: None) state = owner.snapshot('profiles', parked=False, system_long=False, lateral_context=False, metric=False) row = next(row for row in state.rows if row.key == 'ExperimentalSpeedNudge') - assert row.label == 'Experimental Speed Nudge' + assert row.label == 'Speed Nudge in Relaxed' assert row.value == row.default_value == 'On' assert row.available and row.vehicle_fingerprint is None assert owner.apply(FeatureSettingsRequest(row.key, row.source, 'Off'))