diff --git a/selfdrive/car/__init__.py b/selfdrive/car/__init__.py index ad21c694cc..8f2c8bd451 100644 --- a/selfdrive/car/__init__.py +++ b/selfdrive/car/__init__.py @@ -247,12 +247,21 @@ class CanSignalRateCalculator: CarInfos = Union[CarInfo, List[CarInfo]] +@dataclass +class CarSpecs: + mass: float + wheelbase: float + steerRatio: float + + @dataclass(order=True) class PlatformConfig: platform_str: str car_info: CarInfos dbc_dict: DbcDict + specs: Optional[CarSpecs] = None + def __hash__(self) -> int: return hash(self.platform_str) @@ -268,8 +277,8 @@ class Platforms(str, ReprEnum): @classmethod def create_dbc_map(cls) -> Dict[str, DbcDict]: - return {p.config.platform_str: p.config.dbc_dict for p in cls} + return {p: p.config.dbc_dict for p in cls} @classmethod def create_carinfo_map(cls) -> Dict[str, CarInfos]: - return {p.config.platform_str: p.config.car_info for p in cls} + return {p: p.config.car_info for p in cls} diff --git a/selfdrive/car/interfaces.py b/selfdrive/car/interfaces.py index 9767752edb..2b0b148ccf 100644 --- a/selfdrive/car/interfaces.py +++ b/selfdrive/car/interfaces.py @@ -4,7 +4,7 @@ import numpy as np import tomllib from abc import abstractmethod, ABC from enum import StrEnum -from typing import Any, Dict, Optional, Tuple, List, Callable, NamedTuple +from typing import Any, Dict, Optional, Tuple, List, Callable, NamedTuple, cast from cereal import car from openpilot.common.basedir import BASEDIR @@ -12,7 +12,7 @@ from openpilot.common.conversions import Conversions as CV from openpilot.common.simple_kalman import KF1D, get_kalman_gain from openpilot.common.numpy_fast import clip from openpilot.common.realtime import DT_CTRL -from openpilot.selfdrive.car import apply_hysteresis, gen_empty_fingerprint, scale_rot_inertia, scale_tire_stiffness, STD_CARGO_KG +from openpilot.selfdrive.car import PlatformConfig, apply_hysteresis, gen_empty_fingerprint, scale_rot_inertia, scale_tire_stiffness, STD_CARGO_KG from openpilot.selfdrive.controls.lib.drive_helpers import V_CRUISE_MAX, get_friction from openpilot.selfdrive.controls.lib.events import Events from openpilot.selfdrive.controls.lib.vehicle_model import VehicleModel @@ -109,6 +109,14 @@ class CarInterfaceBase(ABC): @classmethod def get_params(cls, candidate: str, fingerprint: Dict[int, Dict[int, int]], car_fw: List[car.CarParams.CarFw], experimental_long: bool, docs: bool): ret = CarInterfaceBase.get_std_params(candidate) + + if hasattr(candidate, "config"): + platform_config = cast(PlatformConfig, candidate.config) + if platform_config.specs is not None: + ret.mass = platform_config.specs.mass + ret.wheelbase = platform_config.specs.wheelbase + ret.steerRatio = platform_config.specs.steerRatio + ret = cls._get_params(ret, candidate, fingerprint, car_fw, experimental_long, docs) # Vehicle mass is published curb weight plus assumed payload such as a human driver; notCars have no assumed payload diff --git a/selfdrive/car/subaru/interface.py b/selfdrive/car/subaru/interface.py index 1296aead5e..edf07ac2ef 100644 --- a/selfdrive/car/subaru/interface.py +++ b/selfdrive/car/subaru/interface.py @@ -40,11 +40,10 @@ class CarInterface(CarInterfaceBase): else: CarInterfaceBase.configure_torque_tune(candidate, ret.lateralTuning) + + ret.centerToFront = ret.wheelbase * 0.5 + if candidate in (CAR.ASCENT, CAR.ASCENT_2023): - ret.mass = 2031. - ret.wheelbase = 2.89 - ret.centerToFront = ret.wheelbase * 0.5 - ret.steerRatio = 13.5 ret.steerActuatorDelay = 0.3 # end-to-end angle controller ret.lateralTuning.init('pid') ret.lateralTuning.pid.kf = 0.00003 @@ -52,10 +51,6 @@ class CarInterface(CarInterfaceBase): ret.lateralTuning.pid.kpV, ret.lateralTuning.pid.kiV = [[0.0025, 0.1], [0.00025, 0.01]] elif candidate == CAR.IMPREZA: - ret.mass = 1568. - ret.wheelbase = 2.67 - ret.centerToFront = ret.wheelbase * 0.5 - ret.steerRatio = 15 ret.steerActuatorDelay = 0.4 # end-to-end angle controller ret.lateralTuning.init('pid') ret.lateralTuning.pid.kf = 0.00005 @@ -63,58 +58,31 @@ class CarInterface(CarInterfaceBase): ret.lateralTuning.pid.kpV, ret.lateralTuning.pid.kiV = [[0.2, 0.3], [0.02, 0.03]] elif candidate == CAR.IMPREZA_2020: - ret.mass = 1480. - ret.wheelbase = 2.67 - ret.centerToFront = ret.wheelbase * 0.5 - ret.steerRatio = 17 # learned, 14 stock ret.lateralTuning.init('pid') ret.lateralTuning.pid.kf = 0.00005 ret.lateralTuning.pid.kiBP, ret.lateralTuning.pid.kpBP = [[0., 14., 23.], [0., 14., 23.]] ret.lateralTuning.pid.kpV, ret.lateralTuning.pid.kiV = [[0.045, 0.042, 0.20], [0.04, 0.035, 0.045]] elif candidate == CAR.CROSSTREK_HYBRID: - ret.mass = 1668. - ret.wheelbase = 2.67 - ret.centerToFront = ret.wheelbase * 0.5 - ret.steerRatio = 17 ret.steerActuatorDelay = 0.1 elif candidate in (CAR.FORESTER, CAR.FORESTER_2022, CAR.FORESTER_HYBRID): - ret.mass = 1568. - ret.wheelbase = 2.67 - ret.centerToFront = ret.wheelbase * 0.5 - ret.steerRatio = 17 # learned, 14 stock ret.lateralTuning.init('pid') ret.lateralTuning.pid.kf = 0.000038 ret.lateralTuning.pid.kiBP, ret.lateralTuning.pid.kpBP = [[0., 14., 23.], [0., 14., 23.]] ret.lateralTuning.pid.kpV, ret.lateralTuning.pid.kiV = [[0.01, 0.065, 0.2], [0.001, 0.015, 0.025]] elif candidate in (CAR.OUTBACK, CAR.LEGACY, CAR.OUTBACK_2023): - ret.mass = 1568. - ret.wheelbase = 2.67 - ret.centerToFront = ret.wheelbase * 0.5 - ret.steerRatio = 17 ret.steerActuatorDelay = 0.1 elif candidate in (CAR.FORESTER_PREGLOBAL, CAR.OUTBACK_PREGLOBAL_2018): ret.safetyConfigs[0].safetyParam = Panda.FLAG_SUBARU_PREGLOBAL_REVERSED_DRIVER_TORQUE # Outback 2018-2019 and Forester have reversed driver torque signal - ret.mass = 1568 - ret.wheelbase = 2.67 - ret.centerToFront = ret.wheelbase * 0.5 - ret.steerRatio = 20 # learned, 14 stock elif candidate == CAR.LEGACY_PREGLOBAL: - ret.mass = 1568 - ret.wheelbase = 2.67 - ret.centerToFront = ret.wheelbase * 0.5 - ret.steerRatio = 12.5 # 14.5 stock ret.steerActuatorDelay = 0.15 elif candidate == CAR.OUTBACK_PREGLOBAL: - ret.mass = 1568 - ret.wheelbase = 2.67 - ret.centerToFront = ret.wheelbase * 0.5 - ret.steerRatio = 20 # learned, 14 stock + pass else: raise ValueError(f"unknown car: {candidate}") diff --git a/selfdrive/car/subaru/values.py b/selfdrive/car/subaru/values.py index 7de083f4a2..b871a919e3 100644 --- a/selfdrive/car/subaru/values.py +++ b/selfdrive/car/subaru/values.py @@ -4,7 +4,7 @@ from typing import List from cereal import car from panda.python import uds -from openpilot.selfdrive.car import DbcDict, PlatformConfig, Platforms, dbc_dict +from openpilot.selfdrive.car import CarSpecs, DbcDict, PlatformConfig, Platforms, dbc_dict from openpilot.selfdrive.car.docs_definitions import CarFootnote, CarHarness, CarInfo, CarParts, Tool, Column from openpilot.selfdrive.car.fw_query_definitions import FwQueryConfig, Request, StdQueries, p16 @@ -100,14 +100,17 @@ class CAR(Platforms): ASCENT = SubaruPlatformConfig( "SUBARU ASCENT LIMITED 2019", SubaruCarInfo("Subaru Ascent 2019-21", "All"), + specs=CarSpecs(mass=2031, wheelbase=2.89, steerRatio=13.5), ) OUTBACK = SubaruPlatformConfig( "SUBARU OUTBACK 6TH GEN", SubaruCarInfo("Subaru Outback 2020-22", "All", car_parts=CarParts.common([CarHarness.subaru_b])), + specs=CarSpecs(mass=1568, wheelbase=2.67, steerRatio=17), ) LEGACY = SubaruPlatformConfig( "SUBARU LEGACY 7TH GEN", SubaruCarInfo("Subaru Legacy 2020-22", "All", car_parts=CarParts.common([CarHarness.subaru_b])), + specs=OUTBACK.specs, ) IMPREZA = SubaruPlatformConfig( "SUBARU IMPREZA LIMITED 2019", @@ -116,6 +119,7 @@ class CAR(Platforms): SubaruCarInfo("Subaru Crosstrek 2018-19", video_link="https://youtu.be/Agww7oE1k-s?t=26"), SubaruCarInfo("Subaru XV 2018-19", video_link="https://youtu.be/Agww7oE1k-s?t=26"), ], + specs=CarSpecs(mass=1568, wheelbase=2.67, steerRatio=15), ) IMPREZA_2020 = SubaruPlatformConfig( "SUBARU IMPREZA SPORT 2020", @@ -124,55 +128,66 @@ class CAR(Platforms): SubaruCarInfo("Subaru Crosstrek 2020-23"), SubaruCarInfo("Subaru XV 2020-21"), ], + specs=CarSpecs(mass=1480, wheelbase=2.67, steerRatio=17), ) # TODO: is there an XV and Impreza too? CROSSTREK_HYBRID = SubaruPlatformConfig( "SUBARU CROSSTREK HYBRID 2020", SubaruCarInfo("Subaru Crosstrek Hybrid 2020", car_parts=CarParts.common([CarHarness.subaru_b])), dbc_dict('subaru_global_2020_hybrid_generated', None), + specs=CarSpecs(mass=1668, wheelbase=2.67, steerRatio=17), + ) + FORESTER = SubaruPlatformConfig( + "SUBARU FORESTER 2019", + SubaruCarInfo("Subaru Forester 2019-21", "All"), + specs=CarSpecs(mass=1668, wheelbase=2.67, steerRatio=17), ) FORESTER_HYBRID = SubaruPlatformConfig( "SUBARU FORESTER HYBRID 2020", SubaruCarInfo("Subaru Forester Hybrid 2020"), dbc_dict('subaru_global_2020_hybrid_generated', None), - ) - FORESTER = SubaruPlatformConfig( - "SUBARU FORESTER 2019", - SubaruCarInfo("Subaru Forester 2019-21", "All"), + specs=FORESTER.specs, ) # Pre-global FORESTER_PREGLOBAL = SubaruPlatformConfig( "SUBARU FORESTER 2017 - 2018", SubaruCarInfo("Subaru Forester 2017-18"), dbc_dict('subaru_forester_2017_generated', None), + specs=CarSpecs(mass=1568, wheelbase=2.67, steerRatio=20), ) LEGACY_PREGLOBAL = SubaruPlatformConfig( "SUBARU LEGACY 2015 - 2018", SubaruCarInfo("Subaru Legacy 2015-18"), dbc_dict('subaru_outback_2015_generated', None), + specs=CarSpecs(mass=1568, wheelbase=2.67, steerRatio=12.5), ) OUTBACK_PREGLOBAL = SubaruPlatformConfig( "SUBARU OUTBACK 2015 - 2017", SubaruCarInfo("Subaru Outback 2015-17"), dbc_dict('subaru_outback_2015_generated', None), + specs=FORESTER_PREGLOBAL.specs, ) OUTBACK_PREGLOBAL_2018 = SubaruPlatformConfig( "SUBARU OUTBACK 2018 - 2019", SubaruCarInfo("Subaru Outback 2018-19"), dbc_dict('subaru_outback_2019_generated', None), + specs=FORESTER_PREGLOBAL.specs, ) # Angle LKAS FORESTER_2022 = SubaruPlatformConfig( "SUBARU FORESTER 2022", SubaruCarInfo("Subaru Forester 2022-24", "All", car_parts=CarParts.common([CarHarness.subaru_c])), + specs=FORESTER.specs, ) OUTBACK_2023 = SubaruPlatformConfig( "SUBARU OUTBACK 7TH GEN", SubaruCarInfo("Subaru Outback 2023", "All", car_parts=CarParts.common([CarHarness.subaru_d])), + specs=OUTBACK.specs, ) ASCENT_2023 = SubaruPlatformConfig( "SUBARU ASCENT 2023", - SubaruCarInfo("Subaru Ascent 2023", "All", car_parts=CarParts.common([CarHarness.subaru_d])) + SubaruCarInfo("Subaru Ascent 2023", "All", car_parts=CarParts.common([CarHarness.subaru_d])), + specs=ASCENT.specs, ) diff --git a/selfdrive/car/tests/test_models.py b/selfdrive/car/tests/test_models.py index 3592fd0baa..ec7527713d 100755 --- a/selfdrive/car/tests/test_models.py +++ b/selfdrive/car/tests/test_models.py @@ -50,7 +50,7 @@ def get_test_cases() -> List[Tuple[str, Optional[CarTestRoute]]]: for i, c in enumerate(sorted(all_known_cars())): if i % NUM_JOBS == JOB_ID: - test_cases.extend(sorted((c.value, r) for r in routes_by_car.get(c, (None,)))) + test_cases.extend(sorted((c, r) for r in routes_by_car.get(c, (None,)))) else: segment_list = read_segment_list(os.path.join(BASEDIR, INTERNAL_SEG_LIST))