mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-12-02 09:03:43 +08:00
Enable optional cooperative steering for Tesla Model Y
This commit is contained in:
@@ -30,7 +30,8 @@ class CarController(CarControllerBase):
|
||||
return
|
||||
self.apply_angle_last = 0
|
||||
self.coop_steer = CooperativeSteeringController()
|
||||
self.coop_enabled = CP.carFingerprint == CAR.TESLA_MODEL_3 and bool(CP.safetyConfigs[0].safetyParam & TeslaSafetyFlags.COOP_STEERING)
|
||||
self.coop_enabled = (CP.carFingerprint in (CAR.TESLA_MODEL_3, CAR.TESLA_MODEL_Y) and
|
||||
bool(CP.safetyConfigs[0].safetyParam & TeslaSafetyFlags.COOP_STEERING))
|
||||
self.steering_limit_info = {}
|
||||
self.packer = CANPacker(dbc_names[Bus.party])
|
||||
self.tesla_can = TeslaCAN(CP, self.packer)
|
||||
|
||||
@@ -32,7 +32,8 @@ class CarState(CarStateBase):
|
||||
self.hands_on_level = 0
|
||||
self.das_control = {}
|
||||
return
|
||||
self.cooperative_steering = CP.carFingerprint == CAR.TESLA_MODEL_3 and bool(CP.safetyConfigs[0].safetyParam & TeslaSafetyFlags.COOP_STEERING)
|
||||
self.cooperative_steering = (CP.carFingerprint in (CAR.TESLA_MODEL_3, CAR.TESLA_MODEL_Y) and
|
||||
bool(CP.safetyConfigs[0].safetyParam & TeslaSafetyFlags.COOP_STEERING))
|
||||
self.can_define = CANDefine(DBC[CP.carFingerprint][Bus.party])
|
||||
self.shifter_values = self.can_define.dv["DI_systemStatus"]["DI_gear"]
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ def modern_aol_supported(cp):
|
||||
cp.flags & ~int(TeslaFlags.MISSING_DAS_SETTINGS | TeslaFlags.HAS_VEHICLE_BUS | TeslaFlags.AOL_SCREEN_BUTTON)):
|
||||
return False
|
||||
word = int(cp.safetyConfigs[0].safetyParam)
|
||||
return (word in BASE_WORDS | AOL_WORDS and (not word & 256 or cp.carFingerprint == CAR.TESLA_MODEL_3) and
|
||||
return (word in BASE_WORDS | AOL_WORDS and
|
||||
bool(word & 1) == bool(cp.openpilotLongitudinalControl) and
|
||||
bool(word & 512) == bool(cp.flags & TeslaFlags.AOL_SCREEN_BUTTON) and
|
||||
(not word & 512 or bool(cp.flags & TeslaFlags.HAS_VEHICLE_BUS)))
|
||||
|
||||
@@ -62,10 +62,11 @@ def test_inactive_lateral_resets_override(vehicle_model):
|
||||
@pytest.mark.parametrize(("candidate", "enabled", "expected"), (
|
||||
(CAR.TESLA_MODEL_3, False, False),
|
||||
(CAR.TESLA_MODEL_3, True, True),
|
||||
(CAR.TESLA_MODEL_Y, True, False),
|
||||
(CAR.TESLA_MODEL_Y, False, False),
|
||||
(CAR.TESLA_MODEL_Y, True, True),
|
||||
(CAR.TESLA_MODEL_S_PREAP, True, False),
|
||||
))
|
||||
def test_safety_flag_is_model_3_only(candidate, enabled, expected):
|
||||
def test_safety_flag_is_modern_model_3_y_only(candidate, enabled, expected):
|
||||
params = CarInterface.get_params(candidate, gen_empty_fingerprint(), [], False, False, True)
|
||||
from openpilot.starpilot.car.tesla.coop_preferences import prepare
|
||||
prepare(params, enabled)
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
"""Model Y's real parser/controller stays within the existing native Tesla limits."""
|
||||
import os
|
||||
|
||||
import pytest
|
||||
|
||||
from opendbc.can import CANPacker
|
||||
from opendbc.car import structs
|
||||
from opendbc.car.tesla.carcontroller import CarController
|
||||
from opendbc.car.tesla.interface import CarInterface
|
||||
from opendbc.car.tesla.screen_button import apply_modern_aol, modern_aol_qualified
|
||||
from opendbc.car.tesla.tests.test_screen_button import screen_params
|
||||
from opendbc.car.tesla.values import CAR, DBC
|
||||
from opendbc.safety.tests.libsafety import libsafety_py
|
||||
from openpilot.starpilot.car.tesla.coop_preferences import prepare
|
||||
|
||||
|
||||
@pytest.mark.parametrize('mode', ('stock', 'ordinary', 'screen'))
|
||||
@pytest.mark.parametrize('enabled', (False, True))
|
||||
@pytest.mark.parametrize('long', (False, True))
|
||||
@pytest.mark.parametrize('brake_disengage', (False, True))
|
||||
@pytest.mark.parametrize('speed', (0., 10., 30.))
|
||||
def test_model_y_controller_frames_and_driver_override(mode, enabled, long, brake_disengage, speed):
|
||||
cp = screen_params(CAR.TESLA_MODEL_Y, harness=mode == 'screen', long=long)
|
||||
apply_modern_aol(cp, mode != 'stock', mode == 'screen', brake_disengage)
|
||||
prepare(cp, enabled)
|
||||
assert bool(cp.safetyConfigs[0].safetyParam & 256) is enabled
|
||||
cp.alternativeExperience = 32 if mode != 'stock' else 0
|
||||
assert modern_aol_qualified(cp, marked_only=True) is (mode != 'stock')
|
||||
ci = CarInterface(cp)
|
||||
controller = CarController(DBC[CAR.TESLA_MODEL_Y], cp)
|
||||
assert controller.coop_enabled is enabled
|
||||
assert ci.CS.cooperative_steering is enabled
|
||||
packer = CANPacker('tesla_model3_party')
|
||||
native = libsafety_py.libsafety
|
||||
native.init_tests()
|
||||
native.set_alternative_experience(cp.alternativeExperience)
|
||||
try:
|
||||
result = native.set_safety_hooks(10, cp.safetyConfigs[0].safetyParam)
|
||||
release_long = long and os.environ.get('TESLA_SCREEN_NATIVE_RELEASE') == '1'
|
||||
if release_long and mode != 'stock':
|
||||
assert result == -1
|
||||
return
|
||||
assert result == 0
|
||||
native.set_aol_test_heartbeat(True)
|
||||
cc = structs.CarControl(latActive=True, enabled=True)
|
||||
offsets = []
|
||||
accepted_steering = 0
|
||||
for tick in range(220):
|
||||
now = 1_000_000_000 + tick * 10_000_000
|
||||
native.set_timer(now // 1000)
|
||||
torque = 1.5 if tick < 100 else -1.5 if tick < 210 else 5.01
|
||||
hands = 3 if tick >= 215 else 0
|
||||
messages = (
|
||||
('DAS_control', 2, {}), ('DAS_steeringControl', 2, {}),
|
||||
('SCCM_steeringAngleSensor', 2, {}), ('DAS_status', 2, {}), ('DAS_settings', 2, {}),
|
||||
('DI_speed', 0, {'DI_vehicleSpeed': speed * 3.6}),
|
||||
('ESP_B', 0, {'ESP_vehicleSpeed': speed * 3.6, 'ESP_wheelSpeedsQF': 1, 'ESP_vehicleStandstillSts': int(speed == 0)}),
|
||||
('EPAS3S_sysStatus', 0, {'EPAS3S_internalSAS': 0, 'EPAS3S_eacStatus': 1,
|
||||
'EPAS3S_handsOnLevel': hands, 'EPAS3S_torsionBarTorque': -torque}),
|
||||
('DI_systemStatus', 0, {'DI_gear': 4}), ('ESP_status', 0, {'ESP_driverBrakeApply': 1}),
|
||||
('UI_warning', 0, {'buckleStatus': 1}), ('DI_state', 0, {'DI_cruiseState': 2 if tick >= 12 else 0}),
|
||||
)
|
||||
frames = [packer.make_can_msg(name, bus, values) for name, bus, values in messages]
|
||||
for address, data, bus in frames:
|
||||
assert native.safety_rx_hook(libsafety_py.make_CANPacket(address, bus, data))
|
||||
native.safety_tick()
|
||||
cs = ci.update([(now, frames)])
|
||||
if tick < 14:
|
||||
continue
|
||||
assert cs.canValid and not cs.canTimeout
|
||||
if mode != 'stock':
|
||||
native.aol_set_host_request(1)
|
||||
output, sends = controller.update(cc.as_reader(), ci.CS, now)
|
||||
for address, data, bus in sends:
|
||||
accepted = native.safety_tx_hook(libsafety_py.make_CANPacket(address, bus, data))
|
||||
assert bool(accepted) is not (release_long and address == 0x2B9), (tick, address, output.steeringAngleDeg)
|
||||
accepted_steering += int(address == 0x488)
|
||||
if 30 <= tick < 100:
|
||||
offsets.append(controller.coop_steer.cooperative_offset_deg)
|
||||
if (tick >= 210 and enabled) or tick >= 215:
|
||||
assert cs.steeringDisengage
|
||||
assert not native.get_controls_allowed()
|
||||
assert not controller.steering_limit_info
|
||||
assert accepted_steering > 90
|
||||
assert any(abs(offset) > 0 for offset in offsets) is enabled
|
||||
finally:
|
||||
native.init_tests()
|
||||
native.set_alternative_experience(0)
|
||||
native.set_safety_hooks(0, 0)
|
||||
@@ -1,10 +1,10 @@
|
||||
"""Frozen Model 3 cooperative steering selection."""
|
||||
"""Frozen Model 3/Y cooperative steering selection."""
|
||||
from opendbc.car.tesla.values import CAR, TeslaSafetyFlags, TeslaFlags
|
||||
|
||||
|
||||
def supported(cp):
|
||||
try:
|
||||
if (cp.brand != 'tesla' or cp.carFingerprint != CAR.TESLA_MODEL_3 or cp.passive or cp.dashcamOnly or cp.notCar or
|
||||
if (cp.brand != 'tesla' or cp.carFingerprint not in (CAR.TESLA_MODEL_3, CAR.TESLA_MODEL_Y) or cp.passive or cp.dashcamOnly or cp.notCar or
|
||||
cp.alternativeExperience not in (0, 32) or len(cp.safetyConfigs) != 1 or int(cp.safetyConfigs[0].safetyModel.raw) != 10 or
|
||||
cp.flags & ~int(TeslaFlags.MISSING_DAS_SETTINGS | TeslaFlags.HAS_VEHICLE_BUS | TeslaFlags.AOL_SCREEN_BUTTON)):
|
||||
return False
|
||||
|
||||
@@ -21,7 +21,7 @@ def is_angle_steering_limited(CP, requested_angle: float, car_output, companion,
|
||||
safety_config.safetyParam & TeslaSafetyFlags.COOP_STEERING.value
|
||||
for safety_config in CP.safetyConfigs
|
||||
)
|
||||
if CP.carFingerprint != CAR.TESLA_MODEL_3 or not cooperative_enabled or not output_healthy:
|
||||
if CP.carFingerprint not in (CAR.TESLA_MODEL_3, CAR.TESLA_MODEL_Y) or not cooperative_enabled or not output_healthy:
|
||||
return legacy_limited
|
||||
|
||||
info = companion.steeringLimitInfo
|
||||
|
||||
@@ -15,8 +15,9 @@ from openpilot.starpilot.ui.feature_settings_state import FeatureSettingsRequest
|
||||
|
||||
@pytest.mark.parametrize('long', (False, True))
|
||||
@pytest.mark.parametrize('brake', (False, True))
|
||||
def test_screen_and_cooperative_selection_compose(long, brake):
|
||||
cp = screen_params(long=long)
|
||||
@pytest.mark.parametrize('candidate', (CAR.TESLA_MODEL_3, CAR.TESLA_MODEL_Y))
|
||||
def test_screen_and_cooperative_selection_compose(long, brake, candidate):
|
||||
cp = screen_params(candidate, long=long)
|
||||
prepare(cp, True)
|
||||
apply_screen_button(cp, True, brake)
|
||||
assert cp.safetyConfigs[0].safetyParam == 768 + int(long) + 1024 * brake
|
||||
@@ -30,7 +31,8 @@ def test_screen_and_cooperative_selection_compose(long, brake):
|
||||
|
||||
@pytest.mark.parametrize('enabled,safe,value,expected', ((True,None,b'1',True),(False,None,b'1',False),
|
||||
(True,b'1',b'1',False),(True,None,b'bad',False),(True,None,None,False)))
|
||||
def test_startup_snapshot(tmp_path, enabled, safe, value, expected):
|
||||
@pytest.mark.parametrize('candidate', (CAR.TESLA_MODEL_3, CAR.TESLA_MODEL_Y))
|
||||
def test_startup_snapshot(tmp_path, enabled, safe, value, expected, candidate):
|
||||
saved = Params(str(tmp_path))
|
||||
for key, raw in (('TeslaCoopSteering',value),('SafeMode',safe)):
|
||||
if raw is not None:
|
||||
@@ -40,14 +42,15 @@ def test_startup_snapshot(tmp_path, enabled, safe, value, expected):
|
||||
saved.put_bool(key,raw == b'1',block=True)
|
||||
snapshot = VehicleStartupPreferences.read(saved, enabled=enabled)
|
||||
assert snapshot.tesla_cooperative is expected
|
||||
cp=screen_params()
|
||||
cp=screen_params(candidate)
|
||||
snapshot.prepare(cp)
|
||||
assert bool(cp.safetyConfigs[0].safetyParam & 256) is expected
|
||||
|
||||
|
||||
def test_ui_setting_is_parked_bound_and_requires_startup(tmp_path):
|
||||
@pytest.mark.parametrize('candidate', (CAR.TESLA_MODEL_3, CAR.TESLA_MODEL_Y))
|
||||
def test_ui_setting_is_parked_bound_and_requires_startup(tmp_path, candidate):
|
||||
saved=Params(str(tmp_path))
|
||||
cp=screen_params()
|
||||
cp=screen_params(candidate)
|
||||
parked=True
|
||||
owner=FeatureSettingsOwner(saved,lambda group:parked and group=='parked_preferences',vehicle_fingerprint=lambda:cp.carFingerprint,vehicle_params=lambda:cp)
|
||||
row=owner.tesla_coop.rows(True)[0]
|
||||
@@ -57,12 +60,13 @@ def test_ui_setting_is_parked_bound_and_requires_startup(tmp_path):
|
||||
assert cp.safetyConfigs[0].safetyParam==0
|
||||
parked=False
|
||||
assert not owner.apply(replace(request,expected=b'1',value='Off'))
|
||||
cp.carFingerprint=CAR.TESLA_MODEL_Y
|
||||
cp.carFingerprint=CAR.TESLA_MODEL_X
|
||||
assert not owner.tesla_coop.rows(True)
|
||||
|
||||
|
||||
def test_offset_is_not_saturation_and_invalid_evidence_falls_back():
|
||||
cp=screen_params()
|
||||
@pytest.mark.parametrize('candidate', (CAR.TESLA_MODEL_3, CAR.TESLA_MODEL_Y))
|
||||
def test_offset_is_not_saturation_and_invalid_evidence_falls_back(candidate):
|
||||
cp=screen_params(candidate)
|
||||
prepare(cp,True)
|
||||
output=SimpleNamespace(actuatorsOutput=SimpleNamespace(steeringAngleDeg=4.))
|
||||
info=SimpleNamespace(valid=True,monoTime=100,modelLimitErrorDeg=0.,resumeLimitErrorDeg=0.,cooperativeLimitErrorDeg=0.,combinedLimitErrorDeg=0.,cooperativeOffsetDeg=4.)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
"""Saved screen gestures for a detected Tesla Vehicle CAN connection."""
|
||||
"""Saved cooperative steering selection for modern Tesla vehicles."""
|
||||
from openpilot.starpilot.saved_document import commit_exact
|
||||
from openpilot.starpilot.saved_source import read_saved
|
||||
from openpilot.starpilot.ui.feature_settings_state import FeatureRow
|
||||
@@ -6,7 +6,7 @@ from openpilot.starpilot.ui.feature_settings_state import FeatureRow
|
||||
|
||||
KEYS = frozenset(('TeslaCoopSteering',))
|
||||
ROWS = (('TeslaCoopSteering', 'Tesla Co-op Steering',
|
||||
'Model 3 only. Light steering input adjusts the assisted steering angle within its limits. Applies after the next startup.'),)
|
||||
'Model 3 and Model Y. Light steering input adjusts the assisted steering angle within its limits. Applies after the next startup.'),)
|
||||
|
||||
|
||||
class TeslaCoopFeature:
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
{"protected_inventory_sha256": "4dd46e4c5f74278872b28191b0693ed6ca695bcb5610935f6b8f308d6225c853", "schema_version": 1}
|
||||
{"protected_inventory_sha256": "15f515eb72499c62b4e5c2afd1f78241ca6dfbc55e06e9ea91b07510386afb8e", "schema_version": 1}
|
||||
|
||||
Reference in New Issue
Block a user