diff --git a/.gitmodules b/.gitmodules index a2d892d07f..c3dcde1a39 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,6 +4,7 @@ [submodule "opendbc"] path = opendbc_repo url = https://github.com/sunnypilot/opendbc.git + branch = tn [submodule "msgq"] path = msgq_repo url = https://github.com/sunnypilot/msgq.git diff --git a/opendbc_repo b/opendbc_repo index 06743dfb39..ef206475a9 160000 --- a/opendbc_repo +++ b/opendbc_repo @@ -1 +1 @@ -Subproject commit 06743dfb39cff0f0cd5ddae244afef42891f4b93 +Subproject commit ef206475a9b1e15048d00363131850fee4c469e8 diff --git a/openpilot/common/params_keys.h b/openpilot/common/params_keys.h index 111099fce9..f8ee269816 100644 --- a/openpilot/common/params_keys.h +++ b/openpilot/common/params_keys.h @@ -187,6 +187,12 @@ inline static std::unordered_map keys = { {"StandstillTimer", {PERSISTENT | BACKUP, BOOL, "0"}}, {"TrueVEgoUI", {PERSISTENT | BACKUP, BOOL, "0"}}, + // toyota specific params + {"ToyotaAutoHold", {PERSISTENT | BACKUP, BOOL, "0"}}, + {"ToyotaEnhancedBsm", {PERSISTENT | BACKUP, BOOL, "0"}}, + {"ToyotaTSS2Long", {PERSISTENT | BACKUP, BOOL, "0"}}, + {"ToyotaDriveMode", {PERSISTENT | BACKUP, BOOL, "0"}}, + // MADS params {"Mads", {PERSISTENT | BACKUP, BOOL, "1"}}, {"MadsMainCruiseAllowed", {PERSISTENT | BACKUP, BOOL, "1"}}, diff --git a/openpilot/selfdrive/car/card.py b/openpilot/selfdrive/car/card.py index e253788e5e..44a4d2f8c0 100755 --- a/openpilot/selfdrive/car/card.py +++ b/openpilot/selfdrive/car/card.py @@ -11,13 +11,13 @@ from opendbc.car.structs import car from openpilot.common.params import Params from openpilot.common.realtime import config_realtime_process, Priority, Ratekeeper from openpilot.common.swaglog import cloudlog, ForwardingHandler - from opendbc.car import DT_CTRL, structs from opendbc.car.can_definitions import CanData, CanRecvCallable, CanSendCallable from opendbc.car.carlog import carlog from opendbc.car.fw_versions import ObdCallback from opendbc.car.car_helpers import get_car, interfaces from opendbc.car.interfaces import CarInterfaceBase, RadarInterfaceBase +from opendbc.safety import ALTERNATIVE_EXPERIENCE from openpilot.selfdrive.pandad import can_capnp_to_list, can_list_to_can_capnp from openpilot.selfdrive.car.cruise import VCruiseHelper from openpilot.selfdrive.car.helpers import convert_carControlSP, convert_to_capnp @@ -123,6 +123,9 @@ class Car: self.RI = RI self.CP.alternativeExperience = 0 + if self.params.get_bool("ToyotaAutoHold"): + self.CP.alternativeExperience |= ALTERNATIVE_EXPERIENCE.ALLOW_AEB + # mads set_alternative_experience(self.CP, self.CP_SP, self.params) set_car_specific_params(self.CP, self.CP_SP, self.params) diff --git a/openpilot/selfdrive/car/cruise.py b/openpilot/selfdrive/car/cruise.py index b554c1a978..8b03a32262 100644 --- a/openpilot/selfdrive/car/cruise.py +++ b/openpilot/selfdrive/car/cruise.py @@ -19,6 +19,7 @@ IMPERIAL_INCREMENT = round(CV.MPH_TO_KPH, 1) # round here to avoid rounding err ButtonEvent = car.CarState.ButtonEvent ButtonType = car.CarState.ButtonEvent.Type CRUISE_LONG_PRESS = 50 +TOYOTA_VIRTUAL_CRUISE_LONG_PRESS = 65 CRUISE_NEAREST_FUNC = { ButtonType.accelCruise: math.ceil, ButtonType.decelCruise: math.floor, @@ -43,6 +44,30 @@ class VCruiseHelper(VCruiseHelperSP): def v_cruise_initialized(self): return self.v_cruise_kph != V_CRUISE_UNSET + @property + def software_pcm_cruise_speed(self) -> bool: + return self.CP.brand == "toyota" and self.CP.pcmCruise and self.CP.openpilotLongitudinalControl and not self.CP_SP.pcmCruiseSpeed + + @property + def cruise_long_press_frames(self) -> int: + return TOYOTA_VIRTUAL_CRUISE_LONG_PRESS if self.software_pcm_cruise_speed else CRUISE_LONG_PRESS + + @property + def software_pcm_cruise_initialized(self) -> bool: + return 0 < self.v_cruise_kph < V_CRUISE_UNSET and 0 < self.v_cruise_cluster_kph < V_CRUISE_UNSET + + def _apply_software_pcm_cruise_delta(self, delta_kph: float, is_metric: bool) -> None: + """Move Toyota's planner/display targets together while respecting both targets' bounds.""" + cluster_min_kph = self.v_cruise_min if is_metric else self.v_cruise_min * CV.MPH_TO_KPH + min_delta = max(V_CRUISE_MIN - self.v_cruise_kph, cluster_min_kph - self.v_cruise_cluster_kph) + max_delta = min(V_CRUISE_MAX - self.v_cruise_kph, V_CRUISE_MAX - self.v_cruise_cluster_kph) + if delta_kph > 0: + applied_delta = min(delta_kph, max(0., max_delta)) + else: + applied_delta = max(delta_kph, min(0., min_delta)) + self.v_cruise_kph = round(self.v_cruise_kph + applied_delta, 1) + self.v_cruise_cluster_kph = round(self.v_cruise_cluster_kph + applied_delta, 1) + def update_v_cruise(self, CS, enabled, is_metric): self.v_cruise_kph_last = self.v_cruise_kph @@ -51,11 +76,21 @@ class VCruiseHelper(VCruiseHelperSP): _enabled = self.update_enabled_state(CS, enabled) if CS.cruiseState.available: - if not self.CP.pcmCruise or (not self.CP_SP.pcmCruiseSpeed and _enabled): + software_pcm_enabled = not self.CP_SP.pcmCruiseSpeed and _enabled + if self.software_pcm_cruise_speed: + software_pcm_enabled = software_pcm_enabled and self.software_pcm_cruise_initialized + + if not self.CP.pcmCruise or software_pcm_enabled: # if stock cruise is completely disabled, then we can use our own set speed logic self._update_v_cruise_non_pcm(CS, _enabled, is_metric) + v_cruise_kph_before_sla = self.v_cruise_kph self.update_speed_limit_assist_v_cruise_non_pcm() - self.v_cruise_cluster_kph = self.v_cruise_kph + if self.software_pcm_cruise_speed: + sla_delta_kph = self.v_cruise_kph - v_cruise_kph_before_sla + self.v_cruise_kph = v_cruise_kph_before_sla + self._apply_software_pcm_cruise_delta(sla_delta_kph, is_metric) + else: + self.v_cruise_cluster_kph = self.v_cruise_kph else: self.v_cruise_kph = CS.cruiseState.speed * CV.MS_TO_KPH self.v_cruise_cluster_kph = CS.cruiseState.speedCluster * CV.MS_TO_KPH @@ -85,13 +120,13 @@ class VCruiseHelper(VCruiseHelperSP): for b in CS.buttonEvents: if b.type.raw in self.button_timers and not b.pressed: - if self.button_timers[b.type.raw] > CRUISE_LONG_PRESS: + if self.button_timers[b.type.raw] > self.cruise_long_press_frames: return # end long press button_type = b.type.raw break else: for k, timer in self.button_timers.items(): - if timer and timer % CRUISE_LONG_PRESS == 0: + if timer and timer % self.cruise_long_press_frames == 0: button_type = k long_press = True break @@ -115,10 +150,26 @@ class VCruiseHelper(VCruiseHelperSP): return long_press, v_cruise_delta = VCruiseHelperSP.update_v_cruise_delta(self, long_press, v_cruise_delta) - if long_press and self.v_cruise_kph % v_cruise_delta != 0: # partial interval - self.v_cruise_kph = CRUISE_NEAREST_FUNC[button_type](self.v_cruise_kph / v_cruise_delta) * v_cruise_delta + # Toyota's canonical PCM set speed and displayed cluster set speed can differ. In + # software-owned PCM mode, round the value the driver sees and apply the same delta + # to both targets so the planner/cluster calibration offset remains intact. + v_cruise_reference = self.v_cruise_cluster_kph if self.software_pcm_cruise_speed else self.v_cruise_kph + if long_press and v_cruise_reference % v_cruise_delta != 0: # partial interval + v_cruise_reference_new = CRUISE_NEAREST_FUNC[button_type](v_cruise_reference / v_cruise_delta) * v_cruise_delta else: - self.v_cruise_kph += v_cruise_delta * CRUISE_INTERVAL_SIGN[button_type] + v_cruise_reference_new = v_cruise_reference + v_cruise_delta * CRUISE_INTERVAL_SIGN[button_type] + + if self.software_pcm_cruise_speed: + delta_kph = v_cruise_reference_new - v_cruise_reference + + # If SET is pressed while overriding, do not lower the target below the current speed. + if CS.gasPressed and button_type in (ButtonType.decelCruise, ButtonType.setCruise): + delta_kph = max(delta_kph, CS.vEgo * CV.MS_TO_KPH - self.v_cruise_kph) + + self._apply_software_pcm_cruise_delta(delta_kph, is_metric) + return + + self.v_cruise_kph += v_cruise_reference_new - v_cruise_reference # If set is pressed while overriding, clip cruise speed to minimum of vEgo if CS.gasPressed and button_type in (ButtonType.decelCruise, ButtonType.setCruise): @@ -127,6 +178,12 @@ class VCruiseHelper(VCruiseHelperSP): self.v_cruise_kph = np.clip(round(self.v_cruise_kph, 1), self.v_cruise_min, V_CRUISE_MAX) def update_button_timers(self, CS, enabled): + if self.software_pcm_cruise_speed and (not enabled or not CS.cruiseState.available or not self.software_pcm_cruise_initialized): + for k in self.button_timers: + self.button_timers[k] = 0 + self.button_change_states[k] = {"standstill": False, "enabled": False} + return + # increment timer for buttons still pressed for k in self.button_timers: if self.button_timers[k] > 0: diff --git a/openpilot/selfdrive/ui/sunnypilot/layouts/settings/cruise.py b/openpilot/selfdrive/ui/sunnypilot/layouts/settings/cruise.py index 671174ac7a..da9e5bdc15 100644 --- a/openpilot/selfdrive/ui/sunnypilot/layouts/settings/cruise.py +++ b/openpilot/selfdrive/ui/sunnypilot/layouts/settings/cruise.py @@ -143,7 +143,8 @@ class CruiseLayout(Widget): self.icbm_toggle.show_description(True) if has_long or has_icbm: - self.custom_acc_toggle.action_item.set_enabled(((has_long and not ui_state.CP.pcmCruise) or has_icbm) and ui_state.is_offroad()) + software_cruise_speed = has_long and (not ui_state.CP.pcmCruise or not ui_state.CP_SP.pcmCruiseSpeed) + self.custom_acc_toggle.action_item.set_enabled((software_cruise_speed or has_icbm) and ui_state.is_offroad()) self.dec_toggle.action_item.set_enabled(has_long) self.scc_v_toggle.action_item.set_enabled(True) self.scc_m_toggle.action_item.set_enabled(True) @@ -169,7 +170,7 @@ class CruiseLayout(Widget): show_custom_acc_desc = True else: if has_long or has_icbm: - if has_long and ui_state.CP.pcmCruise: + if has_long and ui_state.CP.pcmCruise and ui_state.CP_SP.pcmCruiseSpeed: new_custom_acc_desc = tr(ACC_PCMCRUISE_DISABLED_DESCRIPTION) show_custom_acc_desc = True else: diff --git a/openpilot/selfdrive/ui/sunnypilot/layouts/settings/vehicle/brands/toyota.py b/openpilot/selfdrive/ui/sunnypilot/layouts/settings/vehicle/brands/toyota.py index 5a696466b1..4f2eb87e52 100644 --- a/openpilot/selfdrive/ui/sunnypilot/layouts/settings/vehicle/brands/toyota.py +++ b/openpilot/selfdrive/ui/sunnypilot/layouts/settings/vehicle/brands/toyota.py @@ -23,7 +23,7 @@ DESCRIPTIONS = { 'stop_and_go_hack': tr_noop( 'sunnypilot will allow some Toyota/Lexus cars to auto resume during stop and go traffic. ' + 'This feature is only applicable to certain models that are able to use longitudinal control. This is an alpha feature. Use at your own risk.' - ) + ), } diff --git a/openpilot/sunnypilot/selfdrive/car/intelligent_cruise_button_management/controller.py b/openpilot/sunnypilot/selfdrive/car/intelligent_cruise_button_management/controller.py index ded2c0ab19..c5fced2e35 100644 --- a/openpilot/sunnypilot/selfdrive/car/intelligent_cruise_button_management/controller.py +++ b/openpilot/sunnypilot/selfdrive/car/intelligent_cruise_button_management/controller.py @@ -115,7 +115,7 @@ class IntelligentCruiseButtonManagement: self.is_ready = ready and not button_pressed def run(self, CS: car.CarState, CC: car.CarControl, LP_SP: custom.LongitudinalPlanSP, is_metric: bool) -> None: - if self.CP_SP.pcmCruiseSpeed: + if self.CP_SP.pcmCruiseSpeed or not self.CP_SP.intelligentCruiseButtonManagementAvailable: return self.is_metric = is_metric diff --git a/openpilot/sunnypilot/selfdrive/car/interfaces.py b/openpilot/sunnypilot/selfdrive/car/interfaces.py index 4988e863b1..5dacfb8ce1 100644 --- a/openpilot/sunnypilot/selfdrive/car/interfaces.py +++ b/openpilot/sunnypilot/selfdrive/car/interfaces.py @@ -136,6 +136,9 @@ def initialize_params(params) -> list[dict[str, Any]]: keys.extend([ "ToyotaEnforceStockLongitudinal", "ToyotaStopAndGoHack", + "ToyotaTSS2Long", + "ToyotaEnhancedBsm", + "ToyotaAutoHold", ]) return [{k: params.get(k, return_default=True)} for k in keys] diff --git a/openpilot/sunnypilot/selfdrive/car/tests/test_custom_cruise.py b/openpilot/sunnypilot/selfdrive/car/tests/test_custom_cruise.py index 56491e3518..735e140a46 100644 --- a/openpilot/sunnypilot/selfdrive/car/tests/test_custom_cruise.py +++ b/openpilot/sunnypilot/selfdrive/car/tests/test_custom_cruise.py @@ -1,14 +1,26 @@ +from opendbc.can.parser import CANParser +from opendbc.car import create_button_events from opendbc.car.structs import car +from opendbc.car.toyota.carstate import get_virtual_cruise_button, VIRTUAL_CRUISE_BUTTONS +from openpilot.cereal import custom from openpilot.common.constants import CV from openpilot.common.parameterized import parameterized, parameterized_class from openpilot.common.params import Params -from openpilot.selfdrive.car.cruise import V_CRUISE_INITIAL +from openpilot.common.test import OpenpilotTestCase +from openpilot.selfdrive.car.cruise import TOYOTA_VIRTUAL_CRUISE_LONG_PRESS, VCruiseHelper, V_CRUISE_INITIAL, V_CRUISE_UNSET from openpilot.selfdrive.car.tests.test_cruise_speed import TestVCruiseHelper +from openpilot.sunnypilot.selfdrive.car.interfaces import initialize_params ButtonEvent = car.CarState.ButtonEvent ButtonType = car.CarState.ButtonEvent.Type +class TestToyotaParamsHandoff(OpenpilotTestCase): + def test_tss2_long_tuning_param_is_forwarded_to_opendbc(self): + keys = {next(iter(entry)) for entry in initialize_params(Params())} + assert "ToyotaTSS2Long" in keys + + # TODO: test pcmCruise and pcmCruiseSpeed @parameterized_class(('pcm_cruise', 'pcm_cruise_speed'), [(False, True)]) class TestCustomAccIncrements(TestVCruiseHelper): @@ -114,8 +126,8 @@ class TestCustomAccIncrements(TestVCruiseHelper): def test_rounding_behavior(self): """Test rounding behavior for 5 and 10 increments""" test_cases = [ - (47, 5, 50), # 47 -> 50 (round up to next 5) - (45, 5, 50), # 45 -> 50 (already at 5, increment by 5) + (47, 5, 50), # 47 -> 50 (round up to next 5) + (45, 5, 50), # 45 -> 50 (already at 5, increment by 5) (43, 10, 50), # 43 -> 50 (round up to next 10) (40, 10, 50), # 40 -> 50 (already at 10, increment by 10) ] @@ -146,3 +158,302 @@ class TestCustomAccIncrements(TestVCruiseHelper): initial_speed = self.v_cruise_helper.v_cruise_kph self.press_button_long(ButtonType.accelCruise) assert self.v_cruise_helper.v_cruise_kph == initial_speed + 10 # Should fallback to 10 + + +class TestToyotaVirtualCruiseSpeed(OpenpilotTestCase): + def setup_method(self): + self.params = Params() + self.params.put_bool("CustomAccIncrementsEnabled", True, block=True) + self.params.put("CustomAccShortPressIncrement", 5, block=True) + self.params.put("CustomAccLongPressIncrement", 5, block=True) + + CP = car.CarParams(brand="toyota", pcmCruise=True, openpilotLongitudinalControl=True) + CP_SP = custom.CarParamsSP(pcmCruiseSpeed=False) + self.v_cruise_helper = VCruiseHelper(CP, CP_SP) + self.v_cruise_helper.read_custom_set_speed_params() + self.route_parser = CANParser("toyota_nodsu_pt_generated", [("CLUTCH", 16)], 0) + self.route_button = 0 + + @staticmethod + def car_state(canonical_kph, cluster_kph, *, available=True, standstill=False, gas_pressed=False, v_ego_kph=0.0, button_events=None): + CS = car.CarState( + gasPressed=gas_pressed, + vEgo=v_ego_kph * CV.KPH_TO_MS, + cruiseState={ + "available": available, + "speed": canonical_kph * CV.KPH_TO_MS, + "speedCluster": cluster_kph * CV.KPH_TO_MS, + "standstill": standstill, + }, + ) + CS.buttonEvents = button_events or [] + return CS + + def seed_enabled(self, canonical_kph, cluster_kph, *, is_metric=True): + CS = self.car_state(canonical_kph, cluster_kph) + self.v_cruise_helper.update_v_cruise(CS, enabled=False, is_metric=is_metric) + self.v_cruise_helper.update_v_cruise(CS, enabled=True, is_metric=is_metric) + self.v_cruise_helper.update_v_cruise(CS, enabled=True, is_metric=is_metric) + assert self.v_cruise_helper.v_cruise_kph == canonical_kph + assert self.v_cruise_helper.v_cruise_cluster_kph == cluster_kph + + def press(self, button_type, canonical_kph, cluster_kph, hold_frames=0, *, standstill=False, gas_pressed=False, v_ego_kph=0.0, is_metric=True): + pressed = [ButtonEvent(type=button_type, pressed=True)] + self.v_cruise_helper.update_v_cruise( + self.car_state(canonical_kph, cluster_kph, standstill=standstill, gas_pressed=gas_pressed, v_ego_kph=v_ego_kph, button_events=pressed), + enabled=True, + is_metric=is_metric, + ) + for _ in range(hold_frames): + self.v_cruise_helper.update_v_cruise( + self.car_state(canonical_kph, cluster_kph, standstill=standstill, gas_pressed=gas_pressed, v_ego_kph=v_ego_kph), + enabled=True, + is_metric=is_metric, + ) + released = [ButtonEvent(type=button_type, pressed=False)] + self.v_cruise_helper.update_v_cruise( + self.car_state(canonical_kph, cluster_kph, standstill=standstill, gas_pressed=gas_pressed, v_ego_kph=v_ego_kph, button_events=released), + enabled=True, + is_metric=is_metric, + ) + + def set_increments(self, short_increment, long_increment): + self.params.put("CustomAccShortPressIncrement", short_increment, block=True) + self.params.put("CustomAccLongPressIncrement", long_increment, block=True) + self.v_cruise_helper.read_custom_set_speed_params() + + def assert_kph_almost_equal(self, actual, expected): + self.assertAlmostEqual(actual, expected, delta=abs(expected) * 1e-6) + + def route_button_events(self, payload): + self.route_parser.update((1, [(0x361, bytes.fromhex(payload), 0)])) + current = get_virtual_cruise_button( + self.route_parser.vl["CLUTCH"]["CRUISE_RES"], + self.route_parser.vl["CLUTCH"]["CRUISE_SET"], + ) + events = create_button_events(current, self.route_button, VIRTUAL_CRUISE_BUTTONS) + self.route_button = current + return events + + def test_short_press_rounds_display_target_and_preserves_offset(self): + self.seed_enabled(27, 31) + self.press(ButtonType.accelCruise, 28, 32) + + assert self.v_cruise_helper.v_cruise_kph == 31 + assert self.v_cruise_helper.v_cruise_cluster_kph == 35 + + def test_decel_at_display_minimum_does_not_increase_target(self): + self.seed_enabled(26, 30) + self.press(ButtonType.decelCruise, 25, 29) + + assert self.v_cruise_helper.v_cruise_kph == 26 + assert self.v_cruise_helper.v_cruise_cluster_kph == 30 + + @parameterized.expand((52, TOYOTA_VIRTUAL_CRUISE_LONG_PRESS - 1)) + def test_route_length_short_press_is_not_a_long_press(self, hold_frames): + self.set_increments(short_increment=2, long_increment=5) + self.seed_enabled(27, 31) + self.press(ButtonType.accelCruise, 28, 32, hold_frames=hold_frames) + + assert self.v_cruise_helper.v_cruise_kph == 29 + assert self.v_cruise_helper.v_cruise_cluster_kph == 33 + + def test_toyota_long_press_uses_route_validated_cadence_and_suppresses_release(self): + self.set_increments(short_increment=2, long_increment=5) + self.seed_enabled(27, 31) + + pressed = [ButtonEvent(type=ButtonType.accelCruise, pressed=True)] + self.v_cruise_helper.update_v_cruise(self.car_state(31, 35, button_events=pressed), enabled=True, is_metric=True) + for _ in range(TOYOTA_VIRTUAL_CRUISE_LONG_PRESS): + self.v_cruise_helper.update_v_cruise(self.car_state(31, 35), enabled=True, is_metric=True) + + assert self.v_cruise_helper.v_cruise_kph == 31 + assert self.v_cruise_helper.v_cruise_cluster_kph == 35 + + released = [ButtonEvent(type=ButtonType.accelCruise, pressed=False)] + self.v_cruise_helper.update_v_cruise(self.car_state(31, 35, button_events=released), enabled=True, is_metric=True) + assert self.v_cruise_helper.v_cruise_kph == 31 + assert self.v_cruise_helper.v_cruise_cluster_kph == 35 + + def test_route_4_32_second_hold_repeats_six_times(self): + self.seed_enabled(26, 30) + self.press(ButtonType.accelCruise, 30, 34, hold_frames=432) + + assert self.v_cruise_helper.v_cruise_kph == 56 + assert self.v_cruise_helper.v_cruise_cluster_kph == 60 + + def test_maximum_boundary_caps_pair_and_preserves_offset(self): + self.seed_enabled(141, 145) + self.press(ButtonType.accelCruise, 142, 146) + + assert self.v_cruise_helper.v_cruise_kph == 141 + assert self.v_cruise_helper.v_cruise_cluster_kph == 145 + + self.press(ButtonType.accelCruise, 143, 147) + assert self.v_cruise_helper.v_cruise_kph == 141 + assert self.v_cruise_helper.v_cruise_cluster_kph == 145 + + @parameterized.expand( + ( + (25, 29, ButtonType.decelCruise), + (141, 147, ButtonType.accelCruise), + ) + ) + def test_out_of_range_raw_pair_is_not_moved_in_opposite_direction(self, canonical_kph, cluster_kph, button_type): + self.seed_enabled(canonical_kph, cluster_kph) + self.press(button_type, canonical_kph, cluster_kph) + + assert self.v_cruise_helper.v_cruise_kph == canonical_kph + assert self.v_cruise_helper.v_cruise_cluster_kph == cluster_kph + + def test_imperial_increment_preserves_canonical_cluster_pair(self): + self.seed_enabled(45, 50, is_metric=False) + self.press(ButtonType.accelCruise, 46, 51, is_metric=False) + + assert self.v_cruise_helper.v_cruise_kph == 51 + assert self.v_cruise_helper.v_cruise_cluster_kph == 56 + + def test_engagement_button_held_does_not_change_target(self): + initial = self.car_state(27, 31) + self.v_cruise_helper.update_v_cruise(initial, enabled=False, is_metric=True) + + pressed = [ButtonEvent(type=ButtonType.decelCruise, pressed=True)] + self.v_cruise_helper.update_v_cruise(self.car_state(27, 31, button_events=pressed), enabled=False, is_metric=True) + for _ in range(TOYOTA_VIRTUAL_CRUISE_LONG_PRESS + 10): + self.v_cruise_helper.update_v_cruise(self.car_state(28, 32), enabled=True, is_metric=True) + + released = [ButtonEvent(type=ButtonType.decelCruise, pressed=False)] + self.v_cruise_helper.update_v_cruise(self.car_state(28, 32, button_events=released), enabled=True, is_metric=True) + self.v_cruise_helper.update_v_cruise(self.car_state(28, 32), enabled=True, is_metric=True) + + assert self.v_cruise_helper.v_cruise_kph == 28 + assert self.v_cruise_helper.v_cruise_cluster_kph == 32 + + def test_delayed_pcm_target_seeds_before_software_ownership(self): + invalid = self.car_state(0, 0) + self.v_cruise_helper.update_v_cruise(invalid, enabled=False, is_metric=True) + + release = [ButtonEvent(type=ButtonType.decelCruise, pressed=False)] + for _ in range(4): + self.v_cruise_helper.update_v_cruise(self.car_state(0, 0, button_events=release), enabled=True, is_metric=True) + assert self.v_cruise_helper.v_cruise_kph == V_CRUISE_UNSET + assert self.v_cruise_helper.v_cruise_cluster_kph == V_CRUISE_UNSET + + self.v_cruise_helper.update_v_cruise(self.car_state(27, 31), enabled=True, is_metric=True) + self.assert_kph_almost_equal(self.v_cruise_helper.v_cruise_kph, 27) + self.assert_kph_almost_equal(self.v_cruise_helper.v_cruise_cluster_kph, 31) + + self.v_cruise_helper.update_v_cruise(self.car_state(28, 32), enabled=True, is_metric=True) + self.assert_kph_almost_equal(self.v_cruise_helper.v_cruise_kph, 27) + self.assert_kph_almost_equal(self.v_cruise_helper.v_cruise_cluster_kph, 31) + + def test_route_payload_short_press_drives_virtual_target(self): + self.seed_enabled(27, 31) + + pressed = self.route_button_events("a61a0000561a1a81") + self.v_cruise_helper.update_v_cruise(self.car_state(27, 31, button_events=pressed), enabled=True, is_metric=True) + for _ in range(52): + self.v_cruise_helper.update_v_cruise(self.car_state(28, 32), enabled=True, is_metric=True) + + released = self.route_button_events("861a0000561b1a81") + self.v_cruise_helper.update_v_cruise(self.car_state(28, 32, button_events=released), enabled=True, is_metric=True) + assert self.v_cruise_helper.v_cruise_kph == 31 + assert self.v_cruise_helper.v_cruise_cluster_kph == 35 + + def test_prius_route_payload_short_set_drives_virtual_target(self): + self.seed_enabled(31, 35) + + pressed = self.route_button_events("965f000056666585") + self.v_cruise_helper.update_v_cruise(self.car_state(31, 35, button_events=pressed), enabled=True, is_metric=True) + for _ in range(45): + self.v_cruise_helper.update_v_cruise(self.car_state(30, 34), enabled=True, is_metric=True) + + released = self.route_button_events("865f000056666585") + self.v_cruise_helper.update_v_cruise(self.car_state(30, 34, button_events=released), enabled=True, is_metric=True) + assert self.v_cruise_helper.v_cruise_kph == 26 + assert self.v_cruise_helper.v_cruise_cluster_kph == 30 + + def test_prius_route_payload_standstill_res_does_not_change_target(self): + self.seed_enabled(27, 31) + + pressed = self.route_button_events("a61b0000561c1c80") + self.v_cruise_helper.update_v_cruise( + self.car_state(27, 31, standstill=True, button_events=pressed), + enabled=True, + is_metric=True, + ) + for _ in range(TOYOTA_VIRTUAL_CRUISE_LONG_PRESS): + self.v_cruise_helper.update_v_cruise(self.car_state(27, 31, standstill=True), enabled=True, is_metric=True) + + released = self.route_button_events("865f000056666585") + self.v_cruise_helper.update_v_cruise( + self.car_state(27, 31, standstill=True, button_events=released), + enabled=True, + is_metric=True, + ) + assert self.v_cruise_helper.v_cruise_kph == 27 + assert self.v_cruise_helper.v_cruise_cluster_kph == 31 + + def test_route_payload_disengage_mid_hold_clears_pending_action(self): + self.seed_enabled(27, 31) + + pressed = self.route_button_events("a61a0000561a1a81") + self.v_cruise_helper.update_v_cruise(self.car_state(27, 31, button_events=pressed), enabled=True, is_metric=True) + for _ in range(30): + self.v_cruise_helper.update_v_cruise(self.car_state(28, 32), enabled=True, is_metric=True) + + self.v_cruise_helper.update_v_cruise(self.car_state(28, 32), enabled=False, is_metric=True) + released = self.route_button_events("861a0000561b1a81") + self.v_cruise_helper.update_v_cruise(self.car_state(28, 32, available=False, button_events=released), enabled=False, is_metric=True) + + self.v_cruise_helper.update_v_cruise(self.car_state(28, 32), enabled=False, is_metric=True) + self.v_cruise_helper.update_v_cruise(self.car_state(28, 32), enabled=True, is_metric=True) + self.v_cruise_helper.update_v_cruise(self.car_state(28, 32), enabled=True, is_metric=True) + self.assert_kph_almost_equal(self.v_cruise_helper.v_cruise_kph, 28) + self.assert_kph_almost_equal(self.v_cruise_helper.v_cruise_cluster_kph, 32) + + def test_standstill_resume_does_not_change_target(self): + self.seed_enabled(27, 31) + self.press(ButtonType.accelCruise, 27, 31, standstill=True) + + assert self.v_cruise_helper.v_cruise_kph == 27 + assert self.v_cruise_helper.v_cruise_cluster_kph == 31 + + def test_disengagement_discards_virtual_target_and_reseeds_raw_pair(self): + self.seed_enabled(27, 31) + self.press(ButtonType.accelCruise, 28, 32) + assert self.v_cruise_helper.v_cruise_kph == 31 + + raw = self.car_state(28, 32) + self.v_cruise_helper.update_v_cruise(raw, enabled=False, is_metric=True) + self.assert_kph_almost_equal(self.v_cruise_helper.v_cruise_kph, 28) + self.assert_kph_almost_equal(self.v_cruise_helper.v_cruise_cluster_kph, 32) + + self.v_cruise_helper.update_v_cruise(raw, enabled=True, is_metric=True) + self.v_cruise_helper.update_v_cruise(raw, enabled=True, is_metric=True) + self.assert_kph_almost_equal(self.v_cruise_helper.v_cruise_kph, 28) + self.assert_kph_almost_equal(self.v_cruise_helper.v_cruise_cluster_kph, 32) + + def test_unavailable_and_mads_handback_discard_virtual_target(self): + self.seed_enabled(27, 31) + self.press(ButtonType.accelCruise, 28, 32) + assert self.v_cruise_helper.v_cruise_kph == 31 + + self.v_cruise_helper.update_v_cruise(self.car_state(28, 32), enabled=False, is_metric=True) + self.assert_kph_almost_equal(self.v_cruise_helper.v_cruise_kph, 28) + self.assert_kph_almost_equal(self.v_cruise_helper.v_cruise_cluster_kph, 32) + + self.v_cruise_helper.update_v_cruise(self.car_state(0, 0, available=False), enabled=False, is_metric=True) + assert self.v_cruise_helper.v_cruise_kph == V_CRUISE_UNSET + assert self.v_cruise_helper.v_cruise_cluster_kph == V_CRUISE_UNSET + + self.v_cruise_helper.update_v_cruise(self.car_state(29, 33), enabled=False, is_metric=True) + self.assert_kph_almost_equal(self.v_cruise_helper.v_cruise_kph, 29) + self.assert_kph_almost_equal(self.v_cruise_helper.v_cruise_cluster_kph, 33) + + def test_set_during_gas_override_clips_target_to_ego_speed(self): + self.seed_enabled(27, 31) + self.press(ButtonType.decelCruise, 26, 30, gas_pressed=True, v_ego_kph=50) + + assert self.v_cruise_helper.v_cruise_kph == 50 + assert self.v_cruise_helper.v_cruise_cluster_kph == 54 diff --git a/openpilot/sunnypilot/sunnylink/settings_ui.json b/openpilot/sunnypilot/sunnylink/settings_ui.json index 63bf342fcb..b310ad67ff 100644 --- a/openpilot/sunnypilot/sunnylink/settings_ui.json +++ b/openpilot/sunnypilot/sunnylink/settings_ui.json @@ -2302,6 +2302,50 @@ "title": "Toyota / Lexus Settings", "description": "", "items": [ + { + "key": "ToyotaAutoHold", + "widget": "toggle", + "needs_onroad_cycle": true, + "title": "Toyota: Auto Brake Hold FOR TSS2 HYBRID CARS", + "enablement": [ + { + "type": "not_engaged" + } + ] + }, + { + "key": "ToyotaEnhancedBsm", + "widget": "toggle", + "needs_onroad_cycle": true, + "title": "Toyota: Prius TSS2 BSM and some tssp", + "enablement": [ + { + "type": "not_engaged" + } + ] + }, + { + "key": "ToyotaTSS2Long", + "widget": "toggle", + "needs_onroad_cycle": true, + "title": "Toyota: custom longitudinal for TSS2", + "enablement": [ + { + "type": "not_engaged" + } + ] + }, + { + "key": "ToyotaDriveMode", + "widget": "toggle", + "needs_onroad_cycle": true, + "title": "Enable drive mode btn link", + "enablement": [ + { + "type": "not_engaged" + } + ] + }, { "key": "ToyotaEnforceStockLongitudinal", "widget": "toggle", diff --git a/openpilot/sunnypilot/sunnylink/settings_ui_src/pages/vehicle.yaml b/openpilot/sunnypilot/sunnylink/settings_ui_src/pages/vehicle.yaml index 7dd23256b1..e9b73a1976 100644 --- a/openpilot/sunnypilot/sunnylink/settings_ui_src/pages/vehicle.yaml +++ b/openpilot/sunnypilot/sunnylink/settings_ui_src/pages/vehicle.yaml @@ -82,6 +82,30 @@ sections: title: Toyota / Lexus Settings description: '' items: + - key: ToyotaAutoHold + widget: toggle + needs_onroad_cycle: true + title: 'Toyota: Auto Brake Hold FOR TSS2 HYBRID CARS' + enablement: + - $ref: '#/macros/not_engaged' + - key: ToyotaEnhancedBsm + widget: toggle + needs_onroad_cycle: true + title: 'Toyota: Prius TSS2 BSM and some tssp' + enablement: + - $ref: '#/macros/not_engaged' + - key: ToyotaTSS2Long + widget: toggle + needs_onroad_cycle: true + title: 'Toyota: custom longitudinal for TSS2' + enablement: + - $ref: '#/macros/not_engaged' + - key: ToyotaDriveMode + widget: toggle + needs_onroad_cycle: true + title: Enable drive mode btn link + enablement: + - $ref: '#/macros/not_engaged' - key: ToyotaEnforceStockLongitudinal widget: toggle needs_onroad_cycle: true diff --git a/openpilot/sunnypilot/sunnylink/tests/test_capabilities.py b/openpilot/sunnypilot/sunnylink/tests/test_capabilities.py index 7e0272a7c9..4729a13f0d 100644 --- a/openpilot/sunnypilot/sunnylink/tests/test_capabilities.py +++ b/openpilot/sunnypilot/sunnylink/tests/test_capabilities.py @@ -10,9 +10,10 @@ change and must be intentional. KNOWN_PROTOCOL_VERSIONS pins the set we explicitly support — when the constant is bumped, this list must be edited in the same commit so the bump shows up in code review. """ + from __future__ import annotations - +from openpilot.common.test import OpenpilotTestCase from openpilot.sunnypilot.sunnylink.capabilities import ( CAPABILITY_DEFAULTS, CAPABILITY_FIELDS, @@ -20,13 +21,23 @@ from openpilot.sunnypilot.sunnylink.capabilities import ( PROTOCOL_VERSION, generate_capabilities, ) -from openpilot.common.test import OpenpilotTestCase KNOWN_PROTOCOL_VERSIONS = (1,) LATEST_KNOWN = max(KNOWN_PROTOCOL_VERSIONS) +class FakeParams: + def __init__(self, values=None): + self.values = values or {} + + def get(self, key, *args, **kwargs): + return self.values.get(key) + + def get_bool(self, key): + return bool(self.values.get(key, False)) + + def caps(): return generate_capabilities() @@ -52,14 +63,12 @@ class TestProtocolVersion(OpenpilotTestCase): def test_protocol_version_is_known(self): """Sentinel against accidental bumps. Edit KNOWN_PROTOCOL_VERSIONS if intentional.""" assert PROTOCOL_VERSION in KNOWN_PROTOCOL_VERSIONS, ( - f"PROTOCOL_VERSION={PROTOCOL_VERSION} is not in KNOWN_PROTOCOL_VERSIONS={KNOWN_PROTOCOL_VERSIONS}. " + - "If this bump is intentional, add it to KNOWN_PROTOCOL_VERSIONS." + f"PROTOCOL_VERSION={PROTOCOL_VERSION} is not in KNOWN_PROTOCOL_VERSIONS={KNOWN_PROTOCOL_VERSIONS}. " + + "If this bump is intentional, add it to KNOWN_PROTOCOL_VERSIONS." ) def test_protocol_version_matches_latest_known(self): - assert PROTOCOL_VERSION == LATEST_KNOWN, ( - "Test invariant: PROTOCOL_VERSION must equal max(KNOWN_PROTOCOL_VERSIONS)." - ) + assert PROTOCOL_VERSION == LATEST_KNOWN, "Test invariant: PROTOCOL_VERSION must equal max(KNOWN_PROTOCOL_VERSIONS)." class TestOpaquePerBrandFlags(OpenpilotTestCase): diff --git a/openpilot/sunnypilot/sunnylink/tests/test_settings_changes.py b/openpilot/sunnypilot/sunnylink/tests/test_settings_changes.py index d9b1267a10..8d34aa38e9 100644 --- a/openpilot/sunnypilot/sunnylink/tests/test_settings_changes.py +++ b/openpilot/sunnypilot/sunnylink/tests/test_settings_changes.py @@ -9,6 +9,7 @@ isolates one of the gating bugs that the design-overhaul branch fixes so a future regression is loud and obvious. These tests are intentionally narrow and additive — they do not replace the broader test_settings_schema.py. """ + from __future__ import annotations import json @@ -24,14 +25,13 @@ from openpilot.sunnypilot.sunnylink.tools.generate_settings_schema import ( _load_torque_versions, generate_schema, ) +from openpilot.sunnypilot.sunnylink.tools.validate_settings_ui import validate as validate_settings_ui from openpilot.common.test import OpenpilotTestCase -SCHEMA_VALIDATOR_PATH = os.path.join(os.path.dirname(DEFINITION_PATH), "settings_ui.schema.json") - - def _walk_items(schema: dict[str, Any]): """Yield every item dict from the schema.""" + def _yield(item: dict[str, Any]): yield item for sub in item.get("sub_items", []): @@ -149,22 +149,13 @@ class TestTestManeuversSection(OpenpilotTestCase): assert "is_sp_release" in vis_refs enablement = section.get("enablement") or [] enable_refs = json.dumps(enablement) - assert "ShowAdvancedControls" in enable_refs, \ - "test_maneuvers must gate ShowAdvancedControls via enablement" + assert "ShowAdvancedControls" in enable_refs, "test_maneuvers must gate ShowAdvancedControls via enablement" class TestValidator(OpenpilotTestCase): def test_validator_accepts_real_json(self): - """settings_ui.json validates against settings_ui.schema.json.""" - try: - import jsonschema - except ImportError: - self.skipTest("jsonschema not installed") - with open(DEFINITION_PATH) as f: - data = json.load(f) - with open(SCHEMA_VALIDATOR_PATH) as f: - validator = json.load(f) - jsonschema.validate(instance=data, schema=validator) + """settings_ui.json passes the repository's production schema validator.""" + self.assertTrue(validate_settings_ui(DEFINITION_PATH)) class TestTorqueOptionGeneration(OpenpilotTestCase): @@ -177,16 +168,17 @@ class TestTorqueOptionGeneration(OpenpilotTestCase): assert item.get("options") == expected def test_torque_versions_path_resolves(self): - assert os.path.exists(TORQUE_VERSIONS_PATH), ( - f"latcontrol_torque_versions.json not found at {TORQUE_VERSIONS_PATH}" - ) + assert os.path.exists(TORQUE_VERSIONS_PATH), f"latcontrol_torque_versions.json not found at {TORQUE_VERSIONS_PATH}" class TestReleaseBranchGates(OpenpilotTestCase): - @parameterized.expand([ - "EnableGithubRunner", - "QuickBootToggle", - ], names=["key"]) + @parameterized.expand( + [ + "EnableGithubRunner", + "QuickBootToggle", + ], + names=["key"], + ) def test_sp_dev_items_gate_on_is_sp_release(self, schema, key): """sunnypilot dev items must hide on sunnypilot release branches (is_sp_release gate).""" item = _find_item(schema, key) @@ -208,11 +200,14 @@ class TestSpuriousOffroadGatesDropped(OpenpilotTestCase): class TestNotEngagedReplacement(OpenpilotTestCase): - @parameterized.expand([ - "AlphaLongitudinalEnabled", - "ToyotaEnforceStockLongitudinal", - "ToyotaStopAndGoHack", - ], names=["key"]) + @parameterized.expand( + [ + "AlphaLongitudinalEnabled", + "ToyotaEnforceStockLongitudinal", + "ToyotaStopAndGoHack", + ], + names=["key"], + ) def test_offroad_only_replaced_with_not_engaged(self, schema, key): """These items should use not_engaged, not offroad_only.""" item = _find_item(schema, key) @@ -220,3 +215,5 @@ class TestNotEngagedReplacement(OpenpilotTestCase): rule_types = _flatten_rule_types(item.get("enablement")) assert "offroad_only" not in rule_types, f"{key} still uses offroad_only" assert "not_engaged" in rule_types, f"{key} missing not_engaged" + + diff --git a/openpilot/sunnypilot/sunnylink/tests/test_settings_schema.py b/openpilot/sunnypilot/sunnylink/tests/test_settings_schema.py index e60ac000f4..478fca8a5a 100644 --- a/openpilot/sunnypilot/sunnylink/tests/test_settings_schema.py +++ b/openpilot/sunnypilot/sunnylink/tests/test_settings_schema.py @@ -282,7 +282,7 @@ class TestKnownVehicleSettings(OpenpilotTestCase): keys = {i["key"] for i in _brand_items(schema["vehicle_settings"].get("hyundai"))} assert "HyundaiLongitudinalTuning" in keys - def test_toyota_has_enforce_stock_and_stop_go(self, schema): + def test_toyota_has_enforce_stock_stop_go(self, schema): keys = {i["key"] for i in _brand_items(schema["vehicle_settings"].get("toyota"))} assert "ToyotaEnforceStockLongitudinal" in keys assert "ToyotaStopAndGoHack" in keys