diff --git a/opendbc_repo/opendbc/car/hyundai/canfd_lfa_status.py b/opendbc_repo/opendbc/car/hyundai/canfd_lfa_status.py index 507016259c..8254ba71ec 100644 --- a/opendbc_repo/opendbc/car/hyundai/canfd_lfa_status.py +++ b/opendbc_repo/opendbc/car/hyundai/canfd_lfa_status.py @@ -67,6 +67,7 @@ class CANFDLfaStatus: self.display_fault_since_ns = 0 self.display_last_active_ns = 0 self.popup_last_applied_ns = 0 + self.ccnc_last_owned_ns = 0 def update(self, can_packets): boot_clock = getattr(time, 'CLOCK_BOOTTIME', None) @@ -185,6 +186,20 @@ class CANFDLfaStatus: return True return 0 < self.popup_last_applied_ns <= now_ns <= self.popup_last_applied_ns + DISPLAY_RELEASE_NS + def owns_ccnc_fault_status(self, now_ns, *, healthy=False, qualified_warning=False): + body = self.current(now_ns) + eps_fresh = 0 < self.eps_observed_ns <= now_ns and now_ns - self.eps_observed_ns <= MAX_AGE_NS + indication = ((body[4] >> 6) | ((body[5] & 1) << 2)) if body is not None else 7 + normal = body is not None and indication < 4 and not (body[6] & 0xF0 or body[7] >> 4 == 15) + if (not healthy or body is None or not self.camera_integrity or not eps_fresh or not self.eps_healthy or + not (normal or (qualified_warning and self._ownership_warning(body)))): + self.ccnc_last_owned_ns = 0 + return False + if qualified_warning: + self.ccnc_last_owned_ns = now_ns + return True + return 0 < self.ccnc_last_owned_ns <= now_ns <= self.ccnc_last_owned_ns + MAX_AGE_NS + def last_fault(self): body = self.body if body is not None and (body[6] & 0xC0 or body[7] & 0xF0 or ((body[4] >> 6) | ((body[5] & 1) << 2)) >= 4): diff --git a/opendbc_repo/opendbc/car/hyundai/carcontroller.py b/opendbc_repo/opendbc/car/hyundai/carcontroller.py index 1c9ee4ed55..d0b59bae82 100644 --- a/opendbc_repo/opendbc/car/hyundai/carcontroller.py +++ b/opendbc_repo/opendbc/car/hyundai/carcontroller.py @@ -419,6 +419,7 @@ class CarController(CarControllerBase): _projection = self.learning_projection_observer can_sends = [] stock_popup_owned = False + stock_ccnc_owned = False canfd_lfa_icon = (self.canfd_dash_icons.update(self.frame, CC.enabled, CC.latActive) if self.canfd_dash_icons is not None else None) @@ -548,6 +549,9 @@ class CarController(CarControllerBase): stock_owned_status = stock_source.owns_display_status(apply_steer_req, clock_boot_ns(), healthy=display_healthy) stock_popup_owned = stock_source.owns_disengage_popup(apply_steer_req, clock_boot_ns(), healthy=display_healthy, qualified_warning=stock_owned_status) + if ccnc_non_lka: + stock_ccnc_owned = stock_source.owns_ccnc_fault_status(clock_boot_ns(), healthy=display_healthy, + qualified_warning=stock_owned_status) damping_source = getattr(CS, "stock_lkas_damping", None) can_sends.extend(hyundaicanfd.create_steering_messages(self.packer, self.CP, self.CAN, steering_status_active, apply_steer_req, apply_torque, @@ -586,7 +590,7 @@ class CarController(CarControllerBase): can_sends.extend(hyundaicanfd.create_ccnc(self.packer, self.CAN, self.CP.openpilotLongitudinalControl, CC.enabled, hud_control, CC.leftBlinker, CC.rightBlinker, CS.ccnc_161.copy(), CS.ccnc_162.copy(), CS.ccnc_1b5 if support_current else None, - CS.is_metric, CS.out, CS.out.cruiseState.available, CC.latActive)) + CS.is_metric, CS.out, CS.out.cruiseState.available, CC.latActive, owned_lfa_status=stock_ccnc_owned)) self.last_ccnc_161_ts_ns = CS.ccnc_161_ts_ns self.last_ccnc_162_ts_ns = CS.ccnc_162_ts_ns else: diff --git a/opendbc_repo/opendbc/car/hyundai/hyundaicanfd.py b/opendbc_repo/opendbc/car/hyundai/hyundaicanfd.py index 8026865288..9a23b0d6e1 100644 --- a/opendbc_repo/opendbc/car/hyundai/hyundaicanfd.py +++ b/opendbc_repo/opendbc/car/hyundai/hyundaicanfd.py @@ -327,12 +327,13 @@ def create_ioniq6_blindspot_status(counter: int, status: BlindspotStatus, *, bus # Source-derived CCNC camera display transform; the repository MIT notice applies. def create_ccnc(packer, CAN, openpilot_longitudinal, enabled, hud, left_blinker, right_blinker, msg_161, msg_162, msg_1b5, - is_metric, out, main_cruise_enabled, lfa_icon): - if lfa_icon: + is_metric, out, main_cruise_enabled, lfa_icon, *, owned_lfa_status=False): + if lfa_icon or owned_lfa_status: # The host owns lateral; keep unrelated faults and obstruction warnings. for field in ("FAULT_LSS", "FAULT_HDA", "FAULT_DAS", "FAULT_LFA", "FAULT_DAW"): if msg_162.get(field) == 1: msg_162[field] = 0 + if lfa_icon: if msg_161.get("ALERTS_2") == 5: msg_161.update({"ALERTS_2": 0, "SOUNDS_2": 0}) if msg_161.get("ALERTS_3") == 17: diff --git a/opendbc_repo/opendbc/car/hyundai/tests/test_canfd_lfa_status.py b/opendbc_repo/opendbc/car/hyundai/tests/test_canfd_lfa_status.py index 9aa2f35ac9..1adaa9e7ef 100644 --- a/opendbc_repo/opendbc/car/hyundai/tests/test_canfd_lfa_status.py +++ b/opendbc_repo/opendbc/car/hyundai/tests/test_canfd_lfa_status.py @@ -862,3 +862,124 @@ def test_same_batch_ownership_requires_valid_ordered_healthy_predecessor(failure owned = source.owns_display_status(tick >= 2, clock[0], healthy=failure != 'host_fault') assert owned == (failure == 'none') assert not source.owns_display_status(False, clock[0] + 3_000_000_000, healthy=True) + + +@pytest.mark.parametrize('car', (CAR.HYUNDAI_KONA_2ND_GEN, CAR.HYUNDAI_KONA_HEV_2ND_GEN, + CAR.HYUNDAI_SANTA_CRUZ_2025, CAR.HYUNDAI_SONATA_2024, + CAR.HYUNDAI_SONATA_HEV_2024, CAR.HYUNDAI_TUCSON_2025, + CAR.HYUNDAI_TUCSON_HEV_2025, CAR.KIA_K5_2025)) +@pytest.mark.parametrize('alpha', (False, True)) +@pytest.mark.parametrize('failure', ('none', 'host_fault', 'eps_fault', 'camera_fault', 'higher_grade')) +def test_actual_ccnc_handover_keeps_owned_warning_quiet_and_real_faults_visible(car, alpha, failure, clock): + cp = params(car, alpha=alpha) + ci = CarInterface(cp) + ci.update([]) + packer = CANPacker(DBC[car][Bus.pt]) + bus = ci.CC.CAN + decoder = CANParser(DBC[car][Bus.pt], [('CCNC_0x161', 0), ('CCNC_0x162', 0)], bus.ECAN) + command = structs.CarControl.new_message() + fields = ('FAULT_LSS', 'FAULT_HDA', 'FAULT_DAS', 'FAULT_LFA', 'FAULT_DAW') + seen = 0 + for tick in range(100): + clock[0] = 40_000_000_000 + tick * 10_000_000 + command.enabled = command.latActive = 2 <= tick < 30 + faulted = failure != 'none' and tick >= 40 + stock = template(packer, bus.CAM, tick % 256, warning=15 if 14 <= tick < 75 else 0, + fault=int(faulted and failure == 'camera_fault')) + if faulted and failure == 'higher_grade': + data = bytearray(stock[1]) + data[4], data[5] = (data[4] & 0x3F) | 0x40, data[5] | 1 + data[:2] = hkg_can_fd_checksum(0x12A, None, data).to_bytes(2, 'little') + stock = stock[0], bytes(data), stock[2] + frames = [p for p in physical_frames(ci, packer, tick, stock) if not (p[0] == 0xEA and p[2] == bus.ECAN)] + frames.append(packer.make_can_msg('MDPS', bus.ECAN, {'COUNTER': tick % 256, + 'MDPS_LkaToiActvSta': int(4 <= tick < 32), 'MDPS_LkaFailSta': int(faulted and failure == 'eps_fault')})) + if tick % 5 == 0: + faults = dict.fromkeys(fields, int(14 <= tick < 80)) + frames.extend([packer.make_can_msg('CCNC_0x161', bus.CAM, {'COUNTER': tick // 5 % 256}), + packer.make_can_msg('CCNC_0x162', bus.CAM, {'COUNTER': tick // 5 % 256, 'FAULT_FSS': 2, **faults})]) + state = ci.update([(clock[0], frames)]) + assert state.canValid + if faulted and failure == 'host_fault': + ci.CS.out.accFaulted = True + _, packets = ci.apply(command.as_reader(), clock[0]) + display = [p for p in packets if p[0] in (0x161, 0x162)] + if not display: + continue + seen += 1 + decoder.update([(clock[0], display)]) + for field in fields: + assert decoder.vl['CCNC_0x162'][field] == int(faulted and 14 <= tick < 80), (tick, failure, field) + assert decoder.vl['CCNC_0x162']['FAULT_FSS'] == 2 + assert decoder.vl['CCNC_0x161']['LFA_ICON'] == (2 if command.latActive else 0) + assert decoder.vl['CCNC_0x161']['CENTERLINE'] == int(command.latActive) + assert seen == 19 + + +@pytest.mark.parametrize('failure', ('expired', 'backward', 'negative', 'host_fault', 'eps_fault', 'stale_eps', 'stale_camera', 'stale_camera_qualified', + 'camera_crc', 'camera_fault', 'higher_grade', 'unqualified_warning')) +def test_ccnc_normal_camera_handover_requires_current_health_and_expires(failure, clock): + packer = CANPacker(DBC[CAR.HYUNDAI_KONA_2ND_GEN][Bus.pt]) + source = CANFDLfaStatus(2) + for tick in range(16): + clock[0] = 50_000_000_000 + tick * 10_000_000 + camera = template(packer, 2, tick, warning=15 if tick >= 14 else 0) + eps = packer.make_can_msg('MDPS', 0, {'COUNTER': tick, 'MDPS_LkaToiActvSta': int(tick >= 4)}) + source.update([(clock[0], [eps, camera])]) + owned = source.owns_display_status(tick >= 2, clock[0], healthy=True) + assert source.owns_ccnc_fault_status(clock[0], healthy=True, qualified_warning=owned) == owned + assert owned + final_owned_ns = clock[0] + for tick in range(16, 23): + clock[0] += 10_000_000 + warning = 15 if failure == 'unqualified_warning' else 0 + camera = template(packer, 2, tick, warning=warning, fault=int(failure == 'camera_fault')) + if failure in ('camera_crc', 'higher_grade'): + data = bytearray(camera[1]) + if failure == 'camera_crc': + data[0] ^= 1 + else: + data[4], data[5] = (data[4] & 0x3F) | 0x40, data[5] | 1 + data[:2] = hkg_can_fd_checksum(0x12A, None, data).to_bytes(2, 'little') + camera = camera[0], bytes(data), camera[2] + eps = packer.make_can_msg('MDPS', 0, {'COUNTER': tick, 'MDPS_LkaFailSta': int(failure == 'eps_fault')}) + frames = [] if failure == 'stale_eps' else [eps] + if failure not in ('stale_camera', 'stale_camera_qualified'): + frames.append(camera) + source.update([(clock[0], frames)]) + now = final_owned_ns - 1 if failure == 'backward' else -1 if failure == 'negative' else clock[0] + held = source.owns_ccnc_fault_status(now, healthy=failure != 'host_fault', + qualified_warning=failure == 'stale_camera_qualified') + if failure == 'expired': + assert held == (clock[0] - final_owned_ns <= 50_000_000) + elif failure in ('stale_eps', 'stale_camera', 'stale_camera_qualified') and tick < 21: + continue + else: + assert not held + for tick in range(23, 27): + clock[0] += 10_000_000 + camera = template(packer, 2, tick) + eps = packer.make_can_msg('MDPS', 0, {'COUNTER': tick}) + source.update([(clock[0], [eps, camera])]) + assert not source.owns_ccnc_fault_status(clock[0], healthy=True, qualified_warning=False) + + +@pytest.mark.parametrize('fault', range(1, 8)) +@pytest.mark.parametrize('owned', (False, True)) +def test_ccnc_fault_handover_preserves_obstruction_and_unknown_codes(fault, owned): + from opendbc.car.hyundai.hyundaicanfd import create_ccnc + cp = params(CAR.HYUNDAI_KONA_2ND_GEN) + bus = CanBus(cp) + packer = CANPacker(DBC[cp.carFingerprint][Bus.pt]) + fields = ('FAULT_LSS', 'FAULT_HDA', 'FAULT_DAS', 'FAULT_LFA', 'FAULT_DAW') + original = dict.fromkeys(fields, fault) + original.update({'COUNTER': 173, 'FAULT_FCA': 1, 'FAULT_FSS': 2, 'FAULT_ESS': 1, 'FAULT_SCC': 3}) + messages = create_ccnc(packer, bus, False, False, None, False, False, {'COUNTER': 172}, original.copy(), + None, True, None, False, False, owned_lfa_status=owned) + decoder = CANParser(DBC[cp.carFingerprint][Bus.pt], [('CCNC_0x161', 0), ('CCNC_0x162', 0)], bus.ECAN) + decoder.update([(1_000_000_000, messages)]) + for field in fields: + assert decoder.vl['CCNC_0x162'][field] == (0 if owned and fault == 1 else fault) + for field in ('COUNTER', 'FAULT_FCA', 'FAULT_FSS', 'FAULT_ESS', 'FAULT_SCC'): + assert decoder.vl['CCNC_0x162'][field] == original[field] + assert decoder.vl['CCNC_0x161']['LFA_ICON'] == decoder.vl['CCNC_0x161']['CENTERLINE'] == 0 diff --git a/prebuilt.json b/prebuilt.json index bb3bbb0228..1018712951 100644 --- a/prebuilt.json +++ b/prebuilt.json @@ -1 +1 @@ -{"protected_inventory_sha256": "6548e067cdc2b559eef4a1eca1cc9e3c9fd05a378b133fb3d16b6a2fbde4bbae", "schema_version": 1} +{"protected_inventory_sha256": "4dd46e4c5f74278872b28191b0693ed6ca695bcb5610935f6b8f308d6225c853", "schema_version": 1}