diff --git a/openpilot/selfdrive/controls/lib/accel_boost.py b/openpilot/selfdrive/controls/lib/accel_boost.py new file mode 100644 index 0000000000..4782dc842b --- /dev/null +++ b/openpilot/selfdrive/controls/lib/accel_boost.py @@ -0,0 +1,41 @@ +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 AccelBoost: + 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 + plan_ahead = pos.x[-1] / pos.t[-1] - max(sm['carState'].vEgo, 1.0) if len(pos.x) else PLAN_GATE[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/selfdrive/controls/lib/longitudinal_planner.py b/openpilot/selfdrive/controls/lib/longitudinal_planner.py index 6494b6de15..e339f16d54 100755 --- a/openpilot/selfdrive/controls/lib/longitudinal_planner.py +++ b/openpilot/selfdrive/controls/lib/longitudinal_planner.py @@ -3,11 +3,11 @@ import numpy as np import openpilot.cereal.messaging as messaging from opendbc.car.interfaces import ACCEL_MIN, ACCEL_MAX -from openpilot.cereal import log from openpilot.common.constants import CV 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.curve_speed import CurveSpeed from openpilot.selfdrive.controls.lib.longcontrol import LongCtrlState from openpilot.selfdrive.controls.lib.longitudinal_mpc_lib.long_mpc import LongitudinalMpc, LongitudinalPlanSource @@ -19,11 +19,6 @@ from openpilot.common.swaglog import cloudlog J_CRUISE_VALS = [1.6, 1.2, 0.8, 0.6] J_CRUISE_BP = [0., 10.0, 25., 40.] A_CRUISE_MIN = -0.5 -ACCEL_BOOST = { # m/s^2 - log.LongitudinalPersonality.relaxed: 0.0, - log.LongitudinalPersonality.standard: 0.05, - log.LongitudinalPersonality.aggressive: 0.15, -} CONTROL_N_T_IDX = ModelConstants.T_IDXS[:CONTROL_N] @@ -42,6 +37,7 @@ class LongitudinalPlanner: self.mpc = LongitudinalMpc(dt=dt) self.fcw = False self.dt = dt + self.accel_boost = AccelBoost() self.curve_speed = CurveSpeed(dt) self.v_desired_filter = FirstOrderFilter(init_v, 2.0, self.dt) @@ -73,6 +69,7 @@ class LongitudinalPlanner: self.v_desired_filter.x = v_ego self.output_a_target = np.clip(sm['carState'].aEgo, ACCEL_MIN, ACCEL_MAX) self.a_cruise = self.output_a_target + self.accel_boost.reset() self.curve_speed.reset() # Prevent divergence, smooth in current v_ego @@ -106,14 +103,13 @@ class LongitudinalPlanner: output_should_stop_mpc = should_stop(v_ego, output_a_target_mpc) curve_offset = self.curve_speed.update(sm['modelV2'], sm['vehicleParameters'].roll) output_a_target_e2e = sm['modelV2'].action.desiredAcceleration + curve_offset - speed_scale = np.interp(v_ego, [5.0, 10.0], [0.0, 1.0]) - accel_scale = np.interp(output_a_target_e2e, [-1.0, -0.5], [0.0, 1.0]) - output_a_target_e2e += ACCEL_BOOST[personality.raw] * speed_scale * accel_scale output_should_stop_e2e = sm['modelV2'].action.shouldStop self.a_cruise = get_cruise_accel(v_cruise, v_ego, self.a_cruise, self.output_a_target, self.dt) cruise_should_stop = should_stop(v_ego, self.a_cruise) + output_a_target_e2e = self.accel_boost.update(sm, output_a_target_e2e, curve_offset) + candidates = [(output_a_target_mpc, self.mpc.source, output_should_stop_mpc), (self.a_cruise, LongitudinalPlanSource.cruise, cruise_should_stop), (output_a_target_e2e, LongitudinalPlanSource.e2e, output_should_stop_e2e)]