Compare commits

..

1 Commits

Author SHA1 Message Date
discountchubbs 991a56d102 controls: add pidd lateral control 2026-11-23 10:50:05 -08:00
8 changed files with 346 additions and 2 deletions
@@ -11,6 +11,7 @@ from opendbc.car.interfaces import CarInterfaceBase
from openpilot.common.params import Params
from openpilot.common.swaglog import cloudlog
from openpilot.sunnypilot.selfdrive.controls.lib.nnlc.helpers import get_nn_model_path
from openpilot.sunnypilot.selfdrive.controls.lib.pidd import PIDD_VERSION
from openpilot.sunnypilot.selfdrive.controls.lib.speed_limit.helpers import set_speed_limit_assist_availability
from openpilot.sunnypilot.sunnylink.statsd import STATSLOGSP
@@ -55,10 +56,18 @@ def _initialize_intelligent_cruise_button_management(CP: structs.CarParams, CP_S
CP_SP.pcmCruiseSpeed = False
def _initialize_torque_lateral_control(CI: CarInterfaceBase, CP: structs.CarParams, enforce_torque: bool, nnlc_enabled: bool) -> None:
def _initialize_torque_lateral_control(CI: CarInterfaceBase, CP: structs.CarParams, enforce_torque: bool, nnlc_enabled: bool,
params: Params | None = None) -> None:
if params is None:
params = Params()
if nnlc_enabled or enforce_torque:
CI.configure_torque_tune(CP.carFingerprint, CP.lateralTuning)
if enforce_torque and not nnlc_enabled and CP.steerControlType == structs.CarParams.SteerControlType.torque \
and params.get("TorqueControlTune") == PIDD_VERSION:
CP.lateralTuning.init('pid')
def _cleanup_unsupported_params(CP: structs.CarParams, CP_SP: structs.CarParamsSP, params: Params | None = None) -> None:
if params is None:
@@ -92,7 +101,7 @@ def setup_interfaces(CI: CarInterfaceBase, params: Params | None = None) -> None
enforce_torque = _enforce_torque_lateral_control(CI.CP, params)
nnlc_enabled = _initialize_neural_network_lateral_control(CI.CP, CI.CP_SP, params)
_initialize_intelligent_cruise_button_management(CI.CP, CI.CP_SP, params)
_initialize_torque_lateral_control(CI, CI.CP, enforce_torque, nnlc_enabled)
_initialize_torque_lateral_control(CI, CI.CP, enforce_torque, nnlc_enabled, params)
_cleanup_unsupported_params(CI.CP, CI.CP_SP)
try:
@@ -17,6 +17,7 @@ from openpilot.sunnypilot.livedelay.helpers import get_lat_delay
from openpilot.sunnypilot.modeld_v2.modeld_base import ModelStateBase
from openpilot.sunnypilot.selfdrive.controls.lib.blinker_pause_lateral import BlinkerPauseLateral
from openpilot.sunnypilot.selfdrive.controls.lib.latcontrol_torque_v0 import LatControlTorque as LatControlTorqueV0
from openpilot.sunnypilot.selfdrive.controls.lib.pidd import LatControlPidd, pidd_selected
class ControlsExt(ModelStateBase):
@@ -30,11 +31,15 @@ class ControlsExt(ModelStateBase):
cloudlog.info("controlsd_ext is waiting for CarParamsSP")
self.CP_SP = messaging.log_from_bytes(params.get("CarParamsSP", block=True), custom.CarParamsSP)
cloudlog.info("controlsd_ext got CarParamsSP")
self.pidd: LatControlPidd | None = None
self.sm_services_ext = ['radarState', 'selfdriveStateSP']
self.pm_services_ext = ['carControlSP']
def initialize_lateral_control(self, lac, CI, dt):
if pidd_selected(self.CP):
self.pidd = LatControlPidd(self.CP, self.CP_SP, CI, dt)
return self.pidd
enforce_torque_control = self.params.get_bool("EnforceTorqueControl")
torque_versions = self.params.get("TorqueControlTune")
if not enforce_torque_control:
@@ -114,6 +119,13 @@ class ControlsExt(ModelStateBase):
pm.send('carControlSP', cc_sp_send)
def update_pidd_torque_params(self, sm: messaging.SubMaster) -> None:
torque_params = sm['lateralTorqueParameters']
if self.pidd is not None and sm.all_checks(['lateralTorqueParameters']) and torque_params.useParams:
self.pidd.update_torque_parameters(torque_params.latAccelFactorFiltered, torque_params.latAccelOffsetFiltered,
torque_params.frictionCoefficientFiltered)
def run_ext(self, sm: messaging.SubMaster, pm: messaging.PubMaster) -> None:
self.update_pidd_torque_params(sm)
CC_SP = self.state_control_ext(sm)
self.publish_ext(CC_SP, sm, pm)
@@ -1,4 +1,7 @@
{
"pidd": {
"version": "2.0"
},
"v1.0": {
"version": "1.0"
},
@@ -0,0 +1,180 @@
"""
Copyright (c) 2021-, Haibin Wen, sunnypilot, and a number of other contributors.
This file is part of sunnypilot and is licensed under the MIT License.
See the LICENSE.md file in the root directory for more details.
"""
import math
import numpy as np
from collections import deque
from opendbc.car.interfaces import get_torque_params
from opendbc.car.vehicle_model import VehicleModel
from openpilot.cereal import log
from openpilot.common.constants import ACCELERATION_DUE_TO_GRAVITY
from openpilot.common.filter_simple import FirstOrderFilter
from openpilot.common.pid import PIDController
from openpilot.selfdrive.controls.lib.latcontrol import LatControl
# gains are in lateral acceleration, scaled to torque per degree by each car's vehicle model and latAccelFactor
PIDD_VERSION = 2.0
INTERP_SPEEDS = [1.0, 1.5, 2.0, 3.0, 5.0, 7.5, 10.0, 15.0, 30.0]
KP_INTERP = [250.0, 120.0, 65.0, 30.0, 11.5, 5.5, 3.5, 2.0, 0.8]
TD_INTERP = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.15, 0.2]
HIGH_SPEED_BP = [25.0, 29.0]
TURN_RATE_BP = [5.0, 20.0]
TURN_LAT_ACCEL_BP = [0.18, 0.53]
DESIRED_ANGLE_BUFFER_SECONDS = 1.0
LIVE_LAT_ACCEL_FACTOR_RANGE = [1.0, 2.0]
LIVE_FRICTION_RANGE = [0.5, 2.0]
SWING_CAP_FRICTION = 1.3
def pidd_selected(CP):
return CP.steerControlType == 'torque' and CP.lateralTuning.which() == 'pid' and len(CP.lateralTuning.pid.kpBP) == 0 and \
CP.carFingerprint in get_torque_params()
def filter_rc(hz):
return 1 / (2 * math.pi * hz)
def lat_accel_per_deg(VM, v_ego):
return VM.calc_curvature(math.radians(1.0), v_ego, 0.0) * v_ego**2
class LatControlPidd(LatControl):
def __init__(self, CP, CP_SP, CI, dt):
super().__init__(CP, CP_SP, CI, dt)
torque_params = get_torque_params()[CP.carFingerprint]
self.VM = VehicleModel(CP)
self.pid = PIDController(0.0, 0.0, 0.0, pos_limit=self.steer_max, neg_limit=-self.steer_max)
self.offline_lat_accel_factor, self.offline_friction = float(torque_params['LAT_ACCEL_FACTOR']), float(torque_params['FRICTION'])
self.lat_accel_factor = self.friction = None
self.update_torque_parameters(self.offline_lat_accel_factor, 0.0, self.offline_friction)
self.error_filter = FirstOrderFilter(0.0, filter_rc(1.0), dt)
self.request_filter = FirstOrderFilter(0.0, filter_rc(3.0), dt, initialized=False)
self.error_rate_filter = FirstOrderFilter(0.0, 0.0354, dt)
self.wheel_angle_filter = FirstOrderFilter(0.0, filter_rc(10.0), dt, initialized=False)
self.wheel_rate_filter = FirstOrderFilter(0.0, 0.005, dt)
self.request_rate_ref = FirstOrderFilter(0.0, filter_rc(1.0), dt)
self.break_rate = FirstOrderFilter(0.0, filter_rc(0.5), dt)
self.break_rate2 = FirstOrderFilter(0.0, filter_rc(0.5), dt)
self.break_error = FirstOrderFilter(0.0, filter_rc(1.0), dt)
self.command_slow = FirstOrderFilter(0.0, filter_rc(0.5), dt, initialized=False)
self.last_error = 0.0
self.wheel_angle_held = None
self.swing = 0.0
buffer_len = int(DESIRED_ANGLE_BUFFER_SECONDS / dt)
self.desired_angle_buffer = deque([0.0] * buffer_len, maxlen=buffer_len)
self.delay_frames = int(np.clip(CP.steerActuatorDelay / dt + 1, 1, buffer_len))
self.rate_span_frames = max(1, int(0.1 / dt))
def update_torque_parameters(self, latAccelFactor, latAccelOffset, friction):
# a learned factor below the offline one would raise every gain
latAccelFactor = float(np.clip(latAccelFactor, *(r * self.offline_lat_accel_factor for r in LIVE_LAT_ACCEL_FACTOR_RANGE)))
friction = float(np.clip(friction, *(r * self.offline_friction for r in LIVE_FRICTION_RANGE)))
if (latAccelFactor, friction) == (self.lat_accel_factor, self.friction):
return
self.lat_accel_factor, self.friction = latAccelFactor, friction
self.kp = [kp * lat_accel_per_deg(self.VM, v) / latAccelFactor for kp, v in zip(KP_INTERP, INTERP_SPEEDS, strict=True)]
self.pid._k_p = (INTERP_SPEEDS, self.kp)
self.pid._k_i = (INTERP_SPEEDS, [kp / 6.0 for kp in self.kp])
self.pid._k_d = (INTERP_SPEEDS, [kp * td for kp, td in zip(self.kp, TD_INTERP, strict=True)])
def reset(self):
super().reset()
self.pid.reset()
self.swing = self.last_error = 0.0
self.command_slow.initialized = False
for f in (self.error_filter, self.error_rate_filter, self.wheel_rate_filter, self.request_rate_ref, self.break_error, self.break_rate, self.break_rate2):
f.x = 0.0
def update(self, active, CS, VM, params, steer_limited_by_safety, desired_curvature, calibrated_pose, curvature_limited, lat_delay):
pid_log = log.ControlsState.LateralPIDState.new_message()
pid_log.steeringAngleDeg = float(CS.steeringAngleDeg)
pid_log.steeringRateDeg = float(CS.steeringRateDeg)
high_speed = float(np.interp(CS.vEgo, HIGH_SPEED_BP, [0.0, 1.0]))
angle_steers_des_no_offset = self.request_filter.update(math.degrees(VM.get_steer_from_curvature(-desired_curvature, CS.vEgo, params.roll)))
angle_steers_des = angle_steers_des_no_offset + params.angleOffsetDeg
self.desired_angle_buffer.append(angle_steers_des)
expected_angle = self.desired_angle_buffer[-self.delay_frames]
request_rate = (self.desired_angle_buffer[-1] - self.desired_angle_buffer[-1 - self.rate_span_frames]) / (self.rate_span_frames * self.dt)
request_rate_ref = self.request_rate_ref.update(request_rate)
break_rate = self.break_rate2.update(self.break_rate.update(request_rate))
# a 1 Hz error filter lags enough for P to pump a 2 Hz wheel swing, so it opens up only while the request turns the wheel
error_filter_hz = float(np.interp(abs(request_rate_ref), TURN_RATE_BP, [1.0, 3.0]))
self.error_filter.update_alpha(filter_rc(float(np.interp(CS.vEgo, HIGH_SPEED_BP, [error_filter_hz, 1.0]))))
error = self.error_filter.update(expected_angle - CS.steeringAngleDeg)
# at highway speed, ignore the angle sensor toggling one count
held = self.wheel_angle_held if self.wheel_angle_held is not None else CS.steeringAngleDeg
wheel_backlash = 0.08 * high_speed
self.wheel_angle_held = held = CS.steeringAngleDeg - float(np.clip(CS.steeringAngleDeg - held, -wheel_backlash, wheel_backlash))
wheel_angle_last = self.wheel_angle_filter.x if self.wheel_angle_filter.initialized else held
wheel_rate = self.wheel_rate_filter.update((self.wheel_angle_filter.update(held) - wheel_angle_last) / self.dt)
wheel_swing = 0.0 if CS.steeringPressed else min(abs(wheel_rate - request_rate_ref), 60.0)
self.swing = max(wheel_swing, self.swing * (1.0 - self.dt))
pid_log.steeringAngleDesiredDeg = float(angle_steers_des)
pid_log.angleError = float(error)
if not active:
output_torque = 0.0
pid_log.active = False
self.reset()
else:
error_rate = self.error_rate_filter.update((error - self.last_error) / self.dt)
self.last_error = error
error_rate += high_speed * (-wheel_rate - error_rate)
ff = angle_steers_des_no_offset * lat_accel_per_deg(VM, CS.vEgo) / self.lat_accel_factor
ff += 0.4 * params.roll * ACCELERATION_DUE_TO_GRAVITY / self.lat_accel_factor
# break rack friction as soon as the request moves, before the error builds up
error_deadzone = error - float(np.clip(error, -0.3, 0.3))
turn_lat_accel = abs(desired_curvature) * max(CS.vEgo, 8.0) ** 2
friction_scale = float(np.interp(turn_lat_accel, TURN_LAT_ACCEL_BP, [0.6, 1.0]))
friction = self.friction * (friction_scale + high_speed * (0.1 - friction_scale))
friction_rate_gain = 0.75 - 0.25 * high_speed
ff += friction * float(np.tanh((error_deadzone + friction_rate_gain * request_rate) * 0.1887))
kp = float(np.interp(CS.vEgo, INTERP_SPEEDS, self.kp))
# damp the wheel toward the request's rate; the rack rings near 2 Hz without it
wheel_damp = 0.0 if CS.steeringPressed else (1.0 - high_speed) * kp * 0.05 * (wheel_rate - request_rate_ref)
ff -= wheel_damp
# half P on a straight, flat road; the cut goes through the feedforward so the integrator still holds a crown
kp_load = max(float(np.interp(turn_lat_accel, TURN_LAT_ACCEL_BP, [0.0, 1.0])),
float(np.interp(abs(params.roll) * ACCELERATION_DUE_TO_GRAVITY, [0.35, 0.7], [0.0, 1.0])))
kp_scale = 0.5 + 0.5 * kp_load
ff -= (1.0 - float(np.interp(CS.vEgo, [12.0, 18.0], [kp_scale, 1.0]))) * kp * error
ff += high_speed * 0.325 * self.friction * float(np.tanh((self.break_error.update(error) + 0.35 * break_rate) / 0.5))
freeze_integrator = steer_limited_by_safety or CS.steeringPressed or CS.vEgo < 5
error_rate_clean = error_rate - float(np.clip(error_rate, -0.3, 0.3))
output_torque = self.pid.update(error, error_rate=error_rate_clean, feedforward=ff, speed=CS.vEgo, freeze_integrator=freeze_integrator)
# a swing the request isn't asking for: pass only the command's slow part
command = output_torque + wheel_damp
self.command_slow.update(command)
if CS.steeringPressed:
self.command_slow.x = command
swing_gate = float(np.interp(self.swing, [16.0, 30.0], [0.0, 1.0])) * float(np.interp(CS.vEgo, [3.0, 5.0, 10.0, 14.0], [0.0, 1.0, 1.0, 0.0]))
swing_gate *= 1.0 - float(np.interp(abs(request_rate_ref), TURN_RATE_BP, [0.0, 1.0]))
swing_gate *= 1.0 - float(np.interp(turn_lat_accel, TURN_LAT_ACCEL_BP, [0.0, 1.0]))
swing_gate *= 1.0 - float(np.interp(abs(error), [3.0, 6.0], [0.0, 1.0]))
if not (CS.steeringPressed or CS.leftBlinker or CS.rightBlinker):
cap = SWING_CAP_FRICTION * self.friction
output_torque += swing_gate * float(np.clip(self.command_slow.x - command, -cap, cap))
output_torque = float(np.clip(output_torque, -self.steer_max, self.steer_max))
pid_log.active = True
pid_log.p = float(self.pid.p)
pid_log.i = float(self.pid.i)
pid_log.f = float(self.pid.f)
pid_log.output = float(output_torque)
pid_log.saturated = bool(self._check_saturation(self.steer_max - abs(output_torque) < 1e-3, CS, steer_limited_by_safety, curvature_limited))
return output_torque, angle_steers_des, pid_log
@@ -0,0 +1,127 @@
"""
Copyright (c) 2021-, Haibin Wen, sunnypilot, and a number of other contributors.
This file is part of sunnypilot and is licensed under the MIT License.
See the LICENSE.md file in the root directory for more details.
"""
import math
from types import SimpleNamespace
import numpy as np
import pytest
from opendbc.car.car_helpers import interfaces
from opendbc.car.honda.values import CAR as HONDA
from opendbc.car.hyundai.values import CAR as HYUNDAI
from opendbc.car.interfaces import get_torque_params
from opendbc.car.nissan.values import CAR as NISSAN
from opendbc.car.structs import car
from opendbc.car.toyota.values import CAR as TOYOTA
from opendbc.car.vehicle_model import VehicleModel
from openpilot.cereal import log
from openpilot.common.mock.generators import generate_deviceMotion
from openpilot.common.params import Params
from openpilot.common.realtime import DT_CTRL
from openpilot.common.test import OpenpilotTestCase
from openpilot.selfdrive.car.helpers import convert_to_capnp
from openpilot.selfdrive.locationd.helpers import Pose
from openpilot.selfdrive.locationd.torqued import TorqueEstimator
from openpilot.sunnypilot.selfdrive.car import interfaces as sunnypilot_interfaces
from openpilot.sunnypilot.selfdrive.controls.controlsd_ext import ControlsExt
from openpilot.sunnypilot.selfdrive.controls.lib.pidd import LatControlPidd, PIDD_VERSION
def setup_car(car_name):
CarInterface = interfaces[car_name]
CP = CarInterface.get_non_essential_params(car_name)
CP_SP = CarInterface.get_non_essential_params_sp(CP, car_name)
CI = CarInterface(CP, CP_SP)
sunnypilot_interfaces.setup_interfaces(CI)
return CI
def make_controller(CI):
return LatControlPidd(CI.CP.as_reader(), convert_to_capnp(CI.CP_SP).as_reader(), CI, DT_CTRL)
def drive(controller, CS, VM, angles, curvature=0.0):
params = log.VehicleParameters.new_message()
pose = Pose.from_device_motion(generate_deviceMotion().deviceMotion)
return [controller.update(True, CS, VM, params, False, curvature, pose, False, 0.2)[0] for CS.steeringAngleDeg in angles]
class TestPidd(OpenpilotTestCase):
def setup_method(self):
self.params = Params()
self.params.put_bool("EnforceTorqueControl", True, block=True)
self.params.put("TorqueControlTune", PIDD_VERSION, block=True)
def test_selected_on_any_torque_car(self):
for car_name in (HONDA.HONDA_CIVIC, TOYOTA.TOYOTA_RAV4, HYUNDAI.KIA_NIRO_EV):
assert setup_car(car_name).CP.lateralTuning.which() == 'pid'
def test_needs_enforce_torque(self):
self.params.remove("EnforceTorqueControl")
assert setup_car(HYUNDAI.KIA_NIRO_EV).CP.lateralTuning.which() == 'torque'
def test_not_on_angle_cars(self):
CI = setup_car(NISSAN.NISSAN_LEAF)
assert CI.CP.steerControlType == car.CarParams.SteerControlType.angle
assert not self.params.get_bool("EnforceTorqueControl")
def test_controlsd_builds_it(self):
CI = setup_car(HYUNDAI.KIA_NIRO_EV)
ext = SimpleNamespace(params=self.params, CP=CI.CP.as_reader(), CP_SP=convert_to_capnp(CI.CP_SP).as_reader())
assert isinstance(ControlsExt.initialize_lateral_control(ext, None, CI, DT_CTRL), LatControlPidd)
def test_selection_follows_car_params(self):
# params changing after card picked pidd must not leave controlsd with a pid that has no gains
CI = setup_car(HYUNDAI.KIA_NIRO_EV)
self.params.remove("EnforceTorqueControl")
self.params.put("TorqueControlTune", 1.0, block=True)
ext = SimpleNamespace(params=self.params, CP=CI.CP.as_reader(), CP_SP=convert_to_capnp(CI.CP_SP).as_reader())
assert isinstance(ControlsExt.initialize_lateral_control(ext, None, CI, DT_CTRL), LatControlPidd)
def test_damps_wheel_swing(self):
CI = setup_car(HONDA.HONDA_CIVIC)
VM = VehicleModel(CI.CP)
corr = {}
for pressed in (False, True):
controller = make_controller(CI)
CS = car.CarState.new_message(vEgo=8.0, steeringPressed=pressed)
angles = [2.0 * math.sin(2 * math.pi * 2.0 * i * DT_CTRL) for i in range(300)]
out = drive(controller, CS, VM, angles)[100:]
rate = [math.cos(2 * math.pi * 2.0 * i * DT_CTRL) for i in range(100, 300)]
corr[pressed] = float(np.corrcoef(out, rate)[0, 1])
assert corr[False] < corr[True] - 0.3
def test_output_within_steer_max(self):
CI = setup_car(HYUNDAI.KIA_NIRO_EV)
controller = make_controller(CI)
controller.pid.update = lambda *args, **kwargs: controller.steer_max
CS = car.CarState.new_message(vEgo=8.0)
out = drive(controller, CS, VehicleModel(CI.CP), [2.0 * math.sin(2 * math.pi * 2.5 * i * DT_CTRL) for i in range(500)])
assert max(abs(x) for x in out) <= controller.steer_max
def test_reset_clears_swing_gate(self):
CI = setup_car(HYUNDAI.KIA_NIRO_EV)
controller = make_controller(CI)
drive(controller, car.CarState.new_message(vEgo=8.0), VehicleModel(CI.CP), [30.0] * 200, curvature=-0.02)
controller.reset()
assert not controller.command_slow.initialized
def test_live_torque_params_rescale_gains(self):
controller = make_controller(setup_car(HYUNDAI.KIA_NIRO_EV))
kp = list(controller.kp)
controller.update_torque_parameters(2 * controller.lat_accel_factor, 0.0, controller.friction)
assert np.allclose(controller.kp, [k / 2 for k in kp])
assert np.allclose(controller.pid._k_p[1], controller.kp)
controller.update_torque_parameters(0.5 * controller.offline_lat_accel_factor, 0.0, 0.0)
assert np.allclose(controller.kp, kp)
def test_torqued_learns_under_pidd(self):
estimator = TorqueEstimator(setup_car(HYUNDAI.KIA_NIRO_EV).CP.as_reader())
offline = get_torque_params()[HYUNDAI.KIA_NIRO_EV]
assert estimator.offline_latAccelFactor == pytest.approx(offline['LAT_ACCEL_FACTOR'])
assert estimator.offline_friction == pytest.approx(offline['FRICTION'])
assert estimator.max_lataccel_factor > estimator.min_lataccel_factor
@@ -6,11 +6,13 @@ See the LICENSE.md file in the root directory for more details.
"""
import numpy as np
from opendbc.car.interfaces import get_torque_params
from opendbc.car.structs import car
from openpilot.common.params import Params
from openpilot.common.realtime import DT_MDL
from openpilot.sunnypilot import PARAMS_UPDATE_PERIOD
from openpilot.sunnypilot.selfdrive.controls.lib.pidd import pidd_selected
RELAXED_MIN_BUCKET_POINTS = np.array([1, 200, 300, 500, 500, 300, 200, 1])
@@ -37,6 +39,11 @@ class TorqueEstimatorExt:
def initialize_custom_params(self, decimated=False):
self.update_use_params()
if pidd_selected(self.CP):
torque_params = get_torque_params()[self.CP.carFingerprint]
self.offline_latAccelFactor = float(torque_params['LAT_ACCEL_FACTOR'])
self.offline_friction = float(torque_params['FRICTION'])
if self.enforce_torque_control_toggle:
if self._params.get_bool("LiveTorqueParamsRelaxedToggle"):
self.min_bucket_points = RELAXED_MIN_BUCKET_POINTS / (10 if decimated else 1)
@@ -484,6 +484,10 @@
"value": "",
"label": "Default"
},
{
"value": 2.0,
"label": "pidd"
},
{
"value": 1.0,
"label": "v1.0"
@@ -225,6 +225,8 @@ sections:
options:
- value: ''
label: Default
- value: 2.0
label: pidd
- value: 1.0
label: v1.0
- value: 0.0