Default gateway Mk7 Golf steering to the Torque Controller

This commit is contained in:
firestar5683
2026-11-29 21:43:57 -06:00
parent eaf7a7cba2
commit f2b99c1eec
5 changed files with 95 additions and 12 deletions
@@ -118,7 +118,7 @@ def torque_choice_cp(cp):
def prepare_controller(cp, raw):
"""Apply an explicit saved type before the interface and published CP exist."""
"""Apply the selected type before the interface and published CP exist."""
selected = selection_from_bytes(cp, raw)
if selected.source != 'invalid' and selected.force_torque and cp.lateralTuning.which() == 'pid':
prepared = torque_choice_cp(cp)
@@ -204,7 +204,16 @@ def model_turn_assist_supported(CP) -> bool:
return turn_assist_supported(CP) and policy_for(CP) == 'ioniq6'
def _golf_torque_default(cp) -> bool:
return (cp.brand == 'volkswagen' and cp.carFingerprint == 'VOLKSWAGEN_GOLF_MK7' and
cp.networkLocation == structs.CarParams.NetworkLocation.gateway and
cp.transmissionType in (structs.CarParams.TransmissionType.manual, structs.CarParams.TransmissionType.automatic) and
len(cp.safetyConfigs) == 1 and cp.safetyConfigs[0].safetyModel == structs.CarParams.SafetyModel.volkswagen)
def default_selection(CP) -> ControllerSelection:
if _golf_torque_default(CP) and torque_choice_cp(CP) is not None:
return ControllerSelection(ControllerMode.STANDARD, 'generic', 'default', force_torque=True)
policy = policy_for(CP)
use_starpilot = policy is not None and CP.brand != 'ford'
mode = ControllerMode.STARPILOT if use_starpilot else ControllerMode.STANDARD
@@ -251,8 +260,8 @@ def replace_mode(raw: bytes | None, CP, mode: ControllerMode, *, force_torque: b
document = {'version': DOCUMENT_VERSION, 'vehicles': {}} if raw is None else parse_document(raw)
vehicles = dict(document['vehicles'])
vehicles[str(CP.carFingerprint)] = {'brand': str(CP.brand), 'mode': mode.value}
if force_torque:
vehicles[str(CP.carFingerprint)]['forceTorque'] = True
if force_torque or (_golf_torque_default(CP) and mode == ControllerMode.STANDARD):
vehicles[str(CP.carFingerprint)]['forceTorque'] = force_torque
encoded = json.dumps({'version': DOCUMENT_VERSION, 'vehicles': vehicles},
sort_keys=True, separators=(',', ':'), allow_nan=False).encode()
if len(encoded) > MAX_DOCUMENT_BYTES:
@@ -268,6 +277,10 @@ def selection_from_bytes(CP, raw: bytes | None) -> ControllerSelection:
choice = parse_document(raw)['vehicles'].get(str(CP.carFingerprint))
if choice is None or choice['brand'] != str(CP.brand):
return default
# Previous Stock Controller entries omitted forceTorque. Apply the new Golf
# default to those entries and honor explicit PID choices saved afterward.
if default.force_torque and choice['mode'] == ControllerMode.STANDARD and 'forceTorque' not in choice:
return default
force_torque = choice.get('forceTorque', False)
choice_cp = torque_choice_cp(CP) if force_torque else CP
policy = policy_for(choice_cp) if choice_cp is not None else None
@@ -163,3 +163,67 @@ def test_every_eligible_factory_pid_can_select_and_restore():
assert original.to_dict() == before, identity
supported.append(identity)
assert set(PID_CARS).issubset(supported)
@pytest.mark.parametrize('automatic', (False, True))
@pytest.mark.parametrize('alpha', (False, True))
def test_gateway_golf_upgrades_once_and_can_keep_stock_pid(tmp_path, automatic, alpha):
from opendbc.car import gen_empty_fingerprint
identity = 'VOLKSWAGEN_GOLF_MK7'
interface = interfaces[identity]
fingerprints = gen_empty_fingerprint()
fingerprints[1][0xFD] = 8
if automatic:
fingerprints[0][0xAD] = 8
def golf():
return interface.get_params(identity, fingerprints, [], alpha, False, False)
params = Params(str(tmp_path))
cp = golf()
factory_config = cp.to_dict()
legacy = {'version': 1, 'vehicles': {identity: {'brand': 'volkswagen', 'mode': 'standard'}}}
for saved in (None, json.dumps(legacy).encode()):
torque = prepare_controller(golf(), saved)
assert torque.lateralTuning.which() == 'torque'
before = torque.to_dict()
before['lateralTuning'] = factory_config['lateralTuning']
assert before == factory_config
choice = selection_from_bytes(torque, saved)
assert choice.force_torque and choice.mode == ControllerMode.STANDARD
ci = interface(torque)
control = create_lateral_controller(torque.as_reader(), ci, DT_CTRL, mode=choice.mode)
assert type(control).__name__ == 'LatControlTorque' and control.starpilot_extension is None
params.put(DOCUMENT_KEY, legacy, block=True)
owner = FeatureSettingsOwner(params, lambda _: True, vehicle_fingerprint=lambda: identity, vehicle_params=lambda: torque.as_reader())
def row():
state = owner.snapshot('torque/developer', parked=True, system_long=alpha, lateral_context=True, metric=False)
return next(r for r in state.rows if r.key == DOCUMENT_KEY)
current = row()
assert current.value == current.default_value == 'Torque Controller'
assert owner.apply(FeatureSettingsRequest(current.key, current.source, 'Stock Controller', vehicle_fingerprint=identity,
capability=current.capability, dependencies=current.dependencies))
current = row()
assert current.value == 'Stock Controller' and 'Torque Controller' in current.choices
assert parse_document(current.source)['vehicles'][identity]['forceTorque'] is False
for _ in range(2):
assert VehicleStartupPreferences.read(params, enabled=True).prepare(golf()).to_dict() == factory_config
assert owner.apply(FeatureSettingsRequest(current.key, current.source, 'StarPilot Controller', vehicle_fingerprint=identity,
capability=current.capability, dependencies=current.dependencies))
other_raw = row().source
assert parse_document(other_raw)['vehicles'][identity]['forceTorque'] is True
assert selection_from_bytes(golf(), other_raw).mode == ControllerMode.STARPILOT
assert VehicleStartupPreferences.read(params, enabled=False).prepare(golf()).to_dict() == factory_config
assert prepare_controller(golf(), b'{}').to_dict() == factory_config
fingerprints[1].clear()
assert prepare_controller(golf(), None).lateralTuning.which() == 'pid'
fingerprints[1][0xFD] = 8
fingerprints[0].pop(0xAD, None)
fingerprints[0][0x187] = 8
assert prepare_controller(golf(), None).lateralTuning.which() == 'pid'
+8 -4
View File
@@ -123,7 +123,8 @@ class ControllerFeature:
return {**SETUP_DEFAULTS, **learning, LANE_CHANGE_KEY: json.dumps(to_value(lane), sort_keys=True, separators=(',', ':')).encode(),
DOCUMENT_KEY: replace_mode(sources[DOCUMENT_KEY], CP, ControllerMode.STARPILOT,
force_torque=steering_controller(CP) == 'pid' or
selection_from_bytes(CP, sources[DOCUMENT_KEY]).force_torque)}
selection_from_bytes(CP, sources[DOCUMENT_KEY]).force_torque or
selection_from_bytes(CP, None).force_torque)}
def setup_row(self) -> FeatureRow | None:
CP = self.vehicle_params()
@@ -184,7 +185,9 @@ class ControllerFeature:
valid = readable and selected is not None and selected.source != 'invalid'
value = (TORQUE_CHOICE if selected.force_torque and selected.mode == ControllerMode.STANDARD else
CHOICES[0] if selected.mode == ControllerMode.STANDARD else CHOICES[1]) if valid else 'Invalid saved controller choice'
choices = (CHOICES[0], TORQUE_CHOICE, CHOICES[1]) if steering_controller(CP) == 'pid' or valid and selected.force_torque else CHOICES
default = selection_from_bytes(CP, None)
has_pid_choice = steering_controller(CP) == 'pid' or default.force_torque or valid and selected.force_torque
choices = (CHOICES[0], TORQUE_CHOICE, CHOICES[1]) if has_pid_choice else CHOICES
learning, learning_readable = read_saved(self.params, LEARNING_OFF_KEY, 1)
preference = read_preference(self.params, CP, legacy_snapshot=(learning, learning_readable))
learning_valid = learning_readable and preference.valid
@@ -194,7 +197,8 @@ class ControllerFeature:
return FeatureRow(DOCUMENT_KEY, 'Steering Controller', value, raw, choices if valid else (),
available=allowed, reason=reason, capability=capability,
dependencies=((LEARNING_OFF_KEY, learning), (LEARNING_PREFERENCES_KEY, preference.raw)),
default_value=CHOICES[0] if selection_from_bytes(CP, None).mode == ControllerMode.STANDARD else CHOICES[1], level=SettingLevel.ADVANCED)
default_value=TORQUE_CHOICE if default.force_torque else CHOICES[0] if default.mode == ControllerMode.STANDARD else CHOICES[1],
level=SettingLevel.ADVANCED)
def learning_row(self, CP=None) -> FeatureRow | None:
CP = self.vehicle_params() if CP is None else CP
@@ -260,7 +264,7 @@ class ControllerFeature:
if selected.source == 'invalid':
return False
force_torque = request.value == TORQUE_CHOICE or request.value == CHOICES[1] and (
steering_controller(CP) == 'pid' or selected.force_torque)
steering_controller(CP) == 'pid' or selected.force_torque or selection_from_bytes(CP, None).force_torque)
encoded = replace_mode(raw, CP, MODES[request.value], force_torque=force_torque)
except (UnicodeError, ValueError, TypeError, OverflowError):
return False
+6 -4
View File
@@ -31,6 +31,7 @@ def toyota_disable_long_supported(cp) -> bool:
@dataclass(frozen=True)
class VehicleStartupPreferences:
controller_selection: bytes | None = field(default=None, kw_only=True)
controller_enabled: bool = field(default=True, kw_only=True)
toyota_auto_hold: bool = False
toyota_enhanced_bsm: bool = field(default=False, kw_only=True)
toyota_sng: bool = field(default=False, kw_only=True)
@@ -74,7 +75,8 @@ class VehicleStartupPreferences:
from openpilot.starpilot.car.ford.bluepilot import startup_params as ford_bluepilot
from openpilot.starpilot.lateral.controller_selection import DOCUMENT_KEY, MAX_DOCUMENT_BYTES
controller_selection, readable_selection = read_saved(params, DOCUMENT_KEY, MAX_DOCUMENT_BYTES)
if not enabled or not readable_selection:
controller_enabled = enabled and readable_selection
if not controller_enabled:
controller_selection = None
smoothing = read_snapshot(params, enabled=enabled)
radar = read_radar_settings(params)
@@ -105,7 +107,7 @@ class VehicleStartupPreferences:
volt_one_pedal = bool(enabled and not disable_bolt and read_saved(params, "VoltOnePedalMode", 8) == (b"1", True) and
readable and safe in (None, b"0"))
except (OSError, TypeError, ValueError):
return cls(controller_selection=controller_selection, vw_stock_long=vw_stock_long,
return cls(controller_selection=controller_selection, controller_enabled=controller_enabled, vw_stock_long=vw_stock_long,
subaru_smoothing=smoothing, disable_bolt_long=disable_bolt, honda_bosch_a_radar=honda_radar, tesla_preap_stock=preap_stock,
tesla_preap_radar=radar, tesla_preap_pedal=pedal,
gm_camera_pedal=False, toyota_filter=False)
@@ -129,7 +131,7 @@ class VehicleStartupPreferences:
# (experimental, so not on release branches).
speed_sync = bool(enabled and readable and safe in (None, b"0") and
read_saved(params, "IsReleaseBranch", 8) in ((None, True), (b"0", True)))
return cls(controller_selection=controller_selection, vw_stock_long=vw_stock_long,
return cls(controller_selection=controller_selection, controller_enabled=controller_enabled, vw_stock_long=vw_stock_long,
subaru_smoothing=smoothing, subaru_avh_startup=bool(enabled and readable and safe in (None, b"0") and
read_saved(params, "IsReleaseBranch", 8) in ((None, True), (b"0", True)) and
read_saved(params, "SubaruAvhStartup", 8) == (b"1", True)),
@@ -263,7 +265,7 @@ class VehicleStartupPreferences:
from opendbc.bluepilot_lateral.hosts.starpilot.safety_param import init_ford_safety_param_starpilot
init_ford_safety_param_starpilot(cp, dict(self.ford_bluepilot))
from openpilot.starpilot.lateral.controller_selection import prepare_controller
return prepare_controller(cp, self.controller_selection)
return prepare_controller(cp, self.controller_selection) if self.controller_enabled else cp
def finalize(self, cp) -> None:
from opendbc.car.volkswagen.meb_modes import prepare_stock as prepare_meb_stock
+1 -1
View File
@@ -1 +1 @@
{"protected_inventory_sha256": "b329e403657098d046f42834e96c76df8b2233fd26922e5f0f1e7947b6a0290c", "schema_version": 1}
{"protected_inventory_sha256": "812061cbd9b41da665a2b9515aded964e83ff41eb3fa3ddd8cc18acc335e8feb", "schema_version": 1}