mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-12-02 09:03:43 +08:00
Damp rapid highway steering feedforward on ninth-generation Accord
This commit is contained in:
@@ -213,7 +213,7 @@ def restore_snapshot(controller, vm, snapshot):
|
||||
setattr(obj, name, _decode(values, path, old))
|
||||
|
||||
|
||||
def frame_input(cs, vm, params, *, active, safety_limited, desired_curvature, curvature_limited, lat_delay):
|
||||
def frame_input(cs, vm, params, *, active, safety_limited, desired_curvature, curvature_limited, lat_delay, capture_turn_signals=False):
|
||||
values = [(name, _numeric(getattr(cs, name))) for name in _CS_FIELDS]
|
||||
values += [
|
||||
('gearShifter', float(cs.gearShifter.raw if hasattr(cs.gearShifter, 'raw') else int(cs.gearShifter))),
|
||||
@@ -226,6 +226,8 @@ def frame_input(cs, vm, params, *, active, safety_limited, desired_curvature, cu
|
||||
('lat_delay', _numeric(lat_delay)),
|
||||
]
|
||||
values += [('vm.' + name, _numeric(getattr(vm, name))) for name in _VM_FIELDS]
|
||||
if capture_turn_signals:
|
||||
values.extend((('leftBlinker', bool(cs.leftBlinker)), ('rightBlinker', bool(cs.rightBlinker))))
|
||||
if len(values) > MAX_FRAME_VALUES:
|
||||
raise ValueError('frame primitive budget')
|
||||
return tuple(values)
|
||||
@@ -436,6 +438,8 @@ def _car_state(values, *, first_segment_words=None):
|
||||
for name in _CS_FIELDS:
|
||||
setattr(cs, name, bool(values[name]) if name in ('steeringPressed', 'standstill', 'steerFaultTemporary', 'steerFaultPermanent') else values[name])
|
||||
cs.gearShifter = int(values['gearShifter'])
|
||||
cs.leftBlinker = bool(values.get('leftBlinker', False))
|
||||
cs.rightBlinker = bool(values.get('rightBlinker', False))
|
||||
return cs
|
||||
|
||||
|
||||
|
||||
@@ -671,6 +671,7 @@ class GlobalRuntime:
|
||||
desired_curvature=desired_curvature,
|
||||
curvature_limited=curvature_limited,
|
||||
lat_delay=lat_delay,
|
||||
capture_turn_signals=getattr(self.controller, "accord_center_friction", False),
|
||||
) + (
|
||||
('control_frame', float(self.frame_sequence)),
|
||||
('raw_model_curvature', float(raw_model_curvature)),
|
||||
|
||||
@@ -6,7 +6,7 @@ import numpy as np
|
||||
|
||||
from openpilot.starpilot.lateral.torque_extension import apply_turn_assist
|
||||
from openpilot.cereal import log
|
||||
from openpilot.common.constants import ACCELERATION_DUE_TO_GRAVITY
|
||||
from openpilot.common.constants import ACCELERATION_DUE_TO_GRAVITY, CV
|
||||
from openpilot.common.filter_simple import FirstOrderFilter
|
||||
from openpilot.common.pid import PIDController
|
||||
from opendbc.car import structs
|
||||
@@ -56,6 +56,24 @@ class HKGShapedTorquePolicy:
|
||||
self.previous_measurement = 0.0
|
||||
self.previous_setpoint = 0.0
|
||||
self.previous_pressed = False
|
||||
if parent.accord_center_friction:
|
||||
self.accord_feedforward = FirstOrderFilter(0.0, 0.08, self.dt, initialized=False)
|
||||
|
||||
def _accord_feedforward(self, future, setpoint, cs, curvature_limited):
|
||||
smoother = getattr(self, "accord_feedforward", None)
|
||||
if smoother is None:
|
||||
return future
|
||||
|
||||
speed_weight = np.interp(cs.vEgo, [50. * CV.MPH_TO_MS, 55. * CV.MPH_TO_MS], [0., 1.])
|
||||
curve_weight = np.interp(max(abs(future), abs(setpoint)), [0.25, 0.55], [1., 0.])
|
||||
if curvature_limited or cs.steeringPressed or cs.leftBlinker or cs.rightBlinker or speed_weight == 0 or curve_weight == 0:
|
||||
smoother.initialized = False
|
||||
return future
|
||||
|
||||
correction = float(np.clip((smoother.update(future) - future) * speed_weight * curve_weight, -0.08, 0.08))
|
||||
# Never carry the old curve's feedforward through a change in steering direction.
|
||||
shaped = future + correction
|
||||
return shaped if future * shaped > 0. else 0.
|
||||
|
||||
def feedforward_context(self, value, setpoint, jerk, measurement, speed):
|
||||
return self.feedforward(value, setpoint, jerk, speed)
|
||||
@@ -85,6 +103,8 @@ class HKGShapedTorquePolicy:
|
||||
self.measurement_filter.x = self.jerk_filter.x = 0.0
|
||||
self.previous_setpoint = future
|
||||
self.previous_pressed = cs.steeringPressed
|
||||
if parent.accord_center_friction:
|
||||
self.accord_feedforward.initialized = False
|
||||
pid_log.active = False
|
||||
return 0.0, 0.0, pid_log
|
||||
if self.previous_pressed and not cs.steeringPressed:
|
||||
@@ -107,7 +127,7 @@ class HKGShapedTorquePolicy:
|
||||
lsf = (np.interp(cs.vEgo, LOW_SPEED_X, LOW_SPEED_Y) / max(cs.vEgo, MIN_SPEED)) ** 2
|
||||
kp = np.interp(cs.vEgo, parent.pid._k_p[0], parent.pid._k_p[1])
|
||||
error = (setpoint - measurement) * (1 + lsf / max(kp, 1e-3))
|
||||
gravity_adjusted = future - roll
|
||||
gravity_adjusted = self._accord_feedforward(future, setpoint, cs, curvature_limited) - roll
|
||||
ff = gravity_adjusted - parent.torque_params.latAccelOffset * fade
|
||||
ff = self.feedforward_context(ff, setpoint, jerk, measurement, cs.vEgo)
|
||||
threshold, friction_scale = self.friction_context(setpoint, jerk, measurement, cs.vEgo)
|
||||
|
||||
@@ -47,3 +47,61 @@ def test_accord_highway_friction_preserves_other_configurations(mode, flags, lon
|
||||
cs = SimpleNamespace(vEgo=61. * CV.MPH_TO_MS, steeringPressed=False)
|
||||
baseline = get_friction(-0.12, 0.0, 0.30, controller.torque_params)
|
||||
assert controller.friction(-0.12, 0.0, 0.30, cs, -0.2) == pytest.approx(baseline)
|
||||
|
||||
|
||||
def test_accord_highway_feedforward_is_bounded_and_yields():
|
||||
cp = CarInterface.get_non_essential_params(CAR.HONDA_ACCORD_9G)
|
||||
cp.flags |= int(HondaFlags.GAS_INTERCEPTOR)
|
||||
cp.openpilotLongitudinalControl = True
|
||||
CarInterface.configure_torque_tune(cp.carFingerprint, cp.lateralTuning)
|
||||
controller = LatControlTorque(cp.as_reader(), CarInterface(cp), DT_CTRL, controller_mode=ControllerMode.STARPILOT)
|
||||
policy = controller.starpilot_extension.policy
|
||||
cs = SimpleNamespace(vEgo=59. * CV.MPH_TO_MS, steeringPressed=False, leftBlinker=False, rightBlinker=False)
|
||||
smooth = policy._accord_feedforward
|
||||
assert smooth(0., 0., cs, False) == 0.
|
||||
assert 0.12 - 1e-8 <= smooth(0.20, 0.20, cs, False) < 0.20
|
||||
assert smooth(-0.001, 0., cs, False) <= 0.
|
||||
for _ in range(100):
|
||||
on_curve = smooth(0.18, 0.18, cs, False)
|
||||
assert on_curve == pytest.approx(0.18, abs=1e-5)
|
||||
assert smooth(0.55, 0., cs, False) == 0.55
|
||||
assert smooth(0.04, 0., cs, False) == 0.04
|
||||
assert smooth(0.06, 0.55, cs, False) == 0.06
|
||||
assert smooth(0.01, 0., cs, False) == 0.01
|
||||
|
||||
for name in ('steeringPressed', 'leftBlinker', 'rightBlinker'):
|
||||
setattr(cs, name, True)
|
||||
assert smooth(0.18, 0., cs, False) == 0.18
|
||||
setattr(cs, name, False)
|
||||
assert smooth(-0.08, 0., cs, False) == -0.08
|
||||
assert smooth(0.10, 0., cs, True) == 0.10
|
||||
assert smooth(0.02, 0., cs, False) == 0.02
|
||||
cs.vEgo = 50. * CV.MPH_TO_MS
|
||||
assert smooth(-0.05, 0., cs, False) == -0.05
|
||||
|
||||
|
||||
def test_accord_feedforward_checkpoint_preserves_filter_and_signal_bypass():
|
||||
from opendbc.car import structs
|
||||
from opendbc.car.vehicle_model import VehicleModel
|
||||
from openpilot.starpilot.flm.controller_replay import _car_state, capture_snapshot, frame_input, restore_snapshot
|
||||
|
||||
cp = CarInterface.get_non_essential_params(CAR.HONDA_ACCORD_9G)
|
||||
cp.flags |= int(HondaFlags.GAS_INTERCEPTOR)
|
||||
cp.openpilotLongitudinalControl = True
|
||||
CarInterface.configure_torque_tune(cp.carFingerprint, cp.lateralTuning)
|
||||
ci = CarInterface(cp)
|
||||
source = LatControlTorque(cp.as_reader(), ci, DT_CTRL, controller_mode=ControllerMode.STARPILOT)
|
||||
target = LatControlTorque(cp.as_reader(), ci, DT_CTRL, controller_mode=ControllerMode.STARPILOT)
|
||||
vm = VehicleModel(cp)
|
||||
cs = structs.CarState.new_message(vEgo=59. * CV.MPH_TO_MS)
|
||||
source_policy = source.starpilot_extension.policy
|
||||
target_policy = target.starpilot_extension.policy
|
||||
source_policy._accord_feedforward(0.0, 0.0, cs, False)
|
||||
source_policy._accord_feedforward(0.19, 0.19, cs, False)
|
||||
restore_snapshot(target, vm, capture_snapshot(source, vm))
|
||||
assert target_policy._accord_feedforward(0.15, 0.15, cs, False) == source_policy._accord_feedforward(0.15, 0.15, cs, False)
|
||||
|
||||
cs.rightBlinker = True
|
||||
inputs = frame_input(cs, vm, SimpleNamespace(angleOffsetDeg=0., roll=0.), active=True, safety_limited=False,
|
||||
desired_curvature=0., curvature_limited=False, lat_delay=0.35, capture_turn_signals=True)
|
||||
assert target_policy._accord_feedforward(0.21, 0.21, _car_state(dict(inputs)), False) == 0.21
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
{"protected_inventory_sha256": "bff229eaa4911ce7bc4e994ed192a5823589f6d862885079e23fddbea78ce6f7", "schema_version": 1}
|
||||
{"protected_inventory_sha256": "5176bec51b1e2e005627ff429e7dca568724debc31a6fdc771d58551a2c3ada1", "schema_version": 1}
|
||||
|
||||
Reference in New Issue
Block a user