diff --git a/opendbc_repo/opendbc/car/hyundai/carcontroller.py b/opendbc_repo/opendbc/car/hyundai/carcontroller.py index 04510c4933..85c88d2889 100644 --- a/opendbc_repo/opendbc/car/hyundai/carcontroller.py +++ b/opendbc_repo/opendbc/car/hyundai/carcontroller.py @@ -178,7 +178,6 @@ class CarController(CarControllerBase): int(CP.safetyConfigs[-1].safetyParam) in (0x0811, 0x0891)) self.canfd_torque_aol_status = (qualified_canfd_stock_aol(CP) or qualified_canfd_long_aol(CP)) self.ccnc_fault_ownership_required = preserves_stock_lfa(CP) - self.kona_last_lfa_counter = None self.canfd_dash_icons = CanFDDashIcons() if ioniq6_status or self.canfd_torque_aol_status else None self.classic_dash_icons = CanFDDashIcons() if classic_aol_dash_enabled(CP) else None self.classic_lfa_icon = None @@ -590,29 +589,19 @@ class CarController(CarControllerBase): qualified_warning=stock_owned_status) if self.CP.carFingerprint == CAR.KIA_SPORTAGE_5TH_GEN: sportage_legacy_display = stock_source.sportage_legacy_display(self.CP, clock_boot_ns(), healthy=display_healthy) - if ccnc_non_lka: + if ccnc_non_lka and not kona_handoff: stock_ccnc_owned = stock_source.owns_ccnc_fault_status(clock_boot_ns(), display_sample_ns=CS.ccnc_162_ts_ns, healthy=display_healthy, qualified_warning=stock_owned_status) damping_source = getattr(CS, "stock_lkas_damping", None) - lfa_counter = None - if kona_handoff: - if (CC.enabled or CC.latActive) and stock_lfa is not None: - if stock_lfa[2] != self.kona_last_lfa_counter: - lfa_counter = stock_lfa[2] - self.kona_last_lfa_counter = lfa_counter - else: - self.kona_last_lfa_counter = None - if not kona_handoff or lfa_counter is not None: - can_sends.extend(hyundaicanfd.create_steering_messages(self.packer, self.CP, self.CAN, - steering_status_active, apply_steer_req, apply_torque, - stock_lfa=stock_lfa, stock_lfa_faults=stock_faults, - stock_lfa_owned_status=stock_owned_status, - stock_lkas_damping=(damping_source.current_damping(clock_boot_ns()) - if damping_source is not None else None), - lfa_counter=lfa_counter, - legacy_lane_display=sportage_legacy_display)) - if _projection is not None: - _projection.emit('hyundai.CANFD_TORQUE', apply_torque, apply_steer_req, can_sends[-1]) + can_sends.extend(hyundaicanfd.create_steering_messages(self.packer, self.CP, self.CAN, + steering_status_active, apply_steer_req, apply_torque, + stock_lfa=stock_lfa, stock_lfa_faults=stock_faults, + stock_lfa_owned_status=stock_owned_status and not kona_handoff, + stock_lkas_damping=(damping_source.current_damping(clock_boot_ns()) + if damping_source is not None else None), + legacy_lane_display=sportage_legacy_display)) + if _projection is not None: + _projection.emit('hyundai.CANFD_TORQUE', apply_torque, apply_steer_req, can_sends[-1]) # prevent LFA from activating on LKA steering cars by sending "no lane lines detected" to ADAS ECU if ( @@ -643,7 +632,7 @@ class CarController(CarControllerBase): 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, show_cruise, show_ccnc_lateral, - owned_lfa_status=(stock_ccnc_owned if self.ccnc_fault_ownership_required else None), + owned_lfa_status=stock_ccnc_owned if kona_handoff or self.ccnc_fault_ownership_required else None, owned_cruise_status=kona_handoff)) self.last_ccnc_161_ts_ns = CS.ccnc_161_ts_ns self.last_ccnc_162_ts_ns = CS.ccnc_162_ts_ns diff --git a/opendbc_repo/opendbc/car/hyundai/hyundaicanfd.py b/opendbc_repo/opendbc/car/hyundai/hyundaicanfd.py index 69ad1c6cdf..5ba0da3345 100644 --- a/opendbc_repo/opendbc/car/hyundai/hyundaicanfd.py +++ b/opendbc_repo/opendbc/car/hyundai/hyundaicanfd.py @@ -45,7 +45,7 @@ LFA_FAULT_MASK = bytes.fromhex("00000000c001c0f00000000000000000") def create_steering_messages(packer, CP, CAN, enabled, lat_active, apply_torque, *, stock_lfa=None, stock_lfa_faults=None, - stock_lkas_damping=None, stock_lfa_owned_status=False, lfa_counter=None, + stock_lkas_damping=None, stock_lfa_owned_status=False, legacy_lane_display=False): values = { "LKA_OptUsmSta": 2, @@ -74,8 +74,6 @@ def create_steering_messages(packer, CP, CAN, enabled, lat_active, apply_torque, lkas_values = {**values, "Damping_Gain": damping} ret.append(packer.make_can_msg(lkas_msg, CAN.ACAN, lkas_values)) else: - if lfa_counter is not None: - values["COUNTER"] = lfa_counter message = packer.make_can_msg("LFA", CAN.ECAN, values) source = stock_lfa if stock_lfa is not None else stock_lfa_faults if source is not None: diff --git a/opendbc_repo/opendbc/car/hyundai/tests/test_kona_ccnc_long_aol.py b/opendbc_repo/opendbc/car/hyundai/tests/test_kona_ccnc_long_aol.py index 01a946dd62..0afa9932b7 100644 --- a/opendbc_repo/opendbc/car/hyundai/tests/test_kona_ccnc_long_aol.py +++ b/opendbc_repo/opendbc/car/hyundai/tests/test_kona_ccnc_long_aol.py @@ -203,7 +203,7 @@ def test_wheel_choices_keep_aol_with_either_longitudinal_owner(car, alternate, a assert 'Toggle AOL' in feature.choices('MainCruiseButtonControl') -def test_idle_handoff_omits_only_kona_steering_packet(): +def test_kona_steering_has_its_own_continuous_counter(): reference_cp = kona_params(CARS[0]) handoff_cp = kona_params(CARS[0]) for cp, word in ((reference_cp, 0x0C0C), (handoff_cp, 0x1C0C)): @@ -212,11 +212,11 @@ def test_idle_handoff_omits_only_kona_steering_packet(): reference, candidate = CarInterface(reference_cp), CarInterface(handoff_cp) source = Mock() source.owns_display_status.return_value = False + source.last_fault.return_value = None candidate.CS.stock_lfa_status = source states = ((False, False, 10), (True, True, 11), (False, True, 11), (True, False, 12), (True, True, None), (False, False, 13), (True, True, 14)) - expected_counters = (None, 11, None, 12, None, None, 14) - for (enabled, lateral, camera_counter), expected in zip(states, expected_counters, strict=True): + for expected, (enabled, lateral, camera_counter) in enumerate(states): if camera_counter is None: source.current.return_value = None else: @@ -226,7 +226,7 @@ def test_idle_handoff_omits_only_kona_steering_packet(): _, after = candidate.apply(ctrl, 1_000_000_000) assert [frame for frame in after if frame[0] != 0x12A] == [frame for frame in before if frame[0] != 0x12A] assert any(frame[0] == 0x12A for frame in before) - assert [data[2] for addr, data, _ in after if addr == 0x12A] == ([] if expected is None else [expected]) + assert [data[2] for addr, data, _ in after if addr == 0x12A] == [expected] @pytest.mark.parametrize('panda_type,faults,expected', [('cuatro', [], 0x1C0C), ('redPanda', [], 0x0C0C), ('cuatro', ['fault'], 0x0C0C)]) def test_kona_factory_handoff_is_limited_to_healthy_internal_c4(panda_type, faults, expected): diff --git a/opendbc_repo/opendbc/safety/declarations.h b/opendbc_repo/opendbc/safety/declarations.h index 86b295edc5..96fd494174 100644 --- a/opendbc_repo/opendbc/safety/declarations.h +++ b/opendbc_repo/opendbc/safety/declarations.h @@ -233,7 +233,6 @@ typedef bool (*tx_hook)(const CANPacket_t *msg); // returns true if the message typedef bool (*fwd_hook)(int bus_num, int addr); // returns true if the message should be blocked from forwarding typedef bool (*fwd_packet_hook)(const CANPacket_t *msg); -typedef bool (*stage_tx_hook)(const CANPacket_t *msg); typedef bool (*fwd_output_hook)(const CANPacket_t *msg, CANPacket_t *output, bool *replaced); typedef struct { @@ -243,7 +242,6 @@ typedef struct { tx_hook tx; fwd_hook fwd; fwd_packet_hook fwd_packet; - stage_tx_hook stage_tx; fwd_output_hook fwd_output; get_checksum_t get_checksum; compute_checksum_t compute_checksum; @@ -376,10 +374,7 @@ extern safety_config current_safety_config; int safety_fwd_hook(int bus_num, int addr); int safety_fwd_packet_hook(const CANPacket_t *msg); -bool safety_uses_camera_slot(const CANPacket_t *msg); -uint32_t safety_camera_release_delay(const CANPacket_t *msg, uint32_t now); -bool safety_camera_release_current(const CANPacket_t *msg, uint32_t now); -bool safety_stage_tx_hook(const CANPacket_t *msg); +bool safety_uses_forward_transform(const CANPacket_t *msg); int safety_fwd_output_hook(const CANPacket_t *msg, CANPacket_t *output, bool *replaced); int set_safety_hooks(uint16_t mode, uint16_t param); diff --git a/opendbc_repo/opendbc/safety/modes/hyundai_canfd.h b/opendbc_repo/opendbc/safety/modes/hyundai_canfd.h index f89625d650..df212063d2 100644 --- a/opendbc_repo/opendbc/safety/modes/hyundai_canfd.h +++ b/opendbc_repo/opendbc/safety/modes/hyundai_canfd.h @@ -527,7 +527,55 @@ static void hyundai_canfd_ioniq6_optional_rx_hook(const CANPacket_t *msg) { } } +#if defined(ALLOW_DEBUG) && !defined(STM32F4) +static bool hyundai_kona_ccnc_feedback = false; +static bool hyundai_kona_camera_seen = false; +static uint8_t hyundai_kona_camera_counter = 0U; +static uint32_t hyundai_kona_camera_ts = 0U; +static bool hyundai_kona_host_seen = false; +static uint8_t hyundai_kona_host_counter = 0U; +static uint32_t hyundai_kona_host_ts = 0U; + +static bool hyundai_kona_lfa_valid(const CANPacket_t *msg) { + return !msg->extended && (GET_LEN(msg) == 16U) && + (hyundai_canfd_get_checksum(msg) == (hyundai_common_canfd_compute_checksum(msg) ^ 0x041DU)); +} + +static void hyundai_kona_camera_rx(const CANPacket_t *msg) { + if (hyundai_kona_ccnc_feedback && (msg->addr == 0x12AU) && (msg->bus == 2U) && + msg->fd && hyundai_kona_lfa_valid(msg)) { + const uint32_t now = microsecond_timer_get(); + const uint8_t delta = (uint8_t)(msg->data[2] - hyundai_kona_camera_counter); + if (!hyundai_kona_camera_seen || (safety_get_ts_elapsed(now, hyundai_kona_camera_ts) > 50000U) || + ((delta > 0U) && (delta <= 127U))) { + hyundai_kona_camera_seen = true; + hyundai_kona_camera_counter = msg->data[2]; + hyundai_kona_camera_ts = now; + } + } +} + +static bool hyundai_kona_mdps_output(const CANPacket_t *msg, CANPacket_t *output, bool *replaced) { + if (hyundai_kona_ccnc_feedback && (msg->addr == 0xEAU) && (msg->bus == 0U) && + msg->fd && !msg->extended && (GET_LEN(msg) == 24U) && + ((msg->data[3] & 7U) == 0U) && ((msg->data[6] & 0xFCU) == 0U) && + ((msg->data[6] & 3U) <= 1U) && (hyundai_canfd_get_checksum(msg) == hyundai_common_canfd_compute_checksum(msg))) { + // The physical EPS frame is still used unchanged by the vehicle and RX safety. + *output = *msg; + output->data[6] = 0x04U; + const uint16_t checksum = hyundai_common_canfd_compute_checksum(output); + output->data[0] = (uint8_t)checksum; + output->data[1] = (uint8_t)(checksum >> 8U); + *replaced = true; + } + return false; +} +#endif + static void hyundai_canfd_optional_rx_hook(const CANPacket_t *msg) { +#if defined(ALLOW_DEBUG) && !defined(STM32F4) + hyundai_kona_camera_rx(msg); +#endif hyundai_daw_rx(msg); if (hyundai_canfd_ioniq6_long && (msg->bus == 1U)) { hyundai_canfd_ioniq6_optional_rx_hook(msg); @@ -1231,169 +1279,6 @@ static bool hyundai_canfd_tx_hook_valid(const CANPacket_t *msg) { return tx; } -#if defined(ALLOW_DEBUG) && !defined(STM32F4) -static bool hyundai_kona_idle_handoff = false; -static bool hyundai_kona_camera_seen = false; -static bool hyundai_kona_output_seen = false; -static bool hyundai_kona_host_output = false; -static uint32_t hyundai_kona_camera_ts = 0U; -static uint32_t hyundai_kona_output_ts = 0U; -static uint8_t hyundai_kona_camera_counter = 0U; -static uint8_t hyundai_kona_output_counter = 0U; -static bool hyundai_kona_pending = false; -static bool hyundai_kona_emitting = false; -static bool hyundai_kona_slot_owned = false; -static uint32_t hyundai_kona_pending_ts = 0U; -static CANPacket_t hyundai_kona_pending_msg; - -static bool hyundai_kona_host_steering(void) { - return controls_allowed || ((aol_get_permission_mask() & 1U) != 0U); -} - -static bool hyundai_kona_lfa_valid(const CANPacket_t *msg) { - return !msg->extended && (GET_LEN(msg) == 16U) && - (hyundai_canfd_get_checksum(msg) == (hyundai_common_canfd_compute_checksum(msg) ^ 0x041DU)); -} - -static bool hyundai_kona_output_ready(uint32_t now, bool host, uint8_t counter) { - const uint32_t age = safety_get_ts_elapsed(now, hyundai_kona_output_ts); - const uint8_t delta = (uint8_t)(counter - hyundai_kona_output_counter); - return !hyundai_kona_output_seen || - (((hyundai_kona_host_output == host) || (age >= 10000U)) && - ((age > 50000U) || ((delta > 0U) && (delta <= 127U)))); -} - -static void hyundai_kona_track_output(uint32_t now, bool host, uint8_t counter) { - hyundai_kona_output_seen = true; - hyundai_kona_host_output = host; - hyundai_kona_output_ts = now; - hyundai_kona_output_counter = counter; -} - -static bool hyundai_kona_camera_without_slot(const CANPacket_t *msg) { - return hyundai_kona_idle_handoff && (msg->bus == 2U) && (msg->addr == 0x12AU); -} - -static bool hyundai_kona_camera_release_current(const CANPacket_t *msg, uint32_t now) { - return (msg->bus == 2U) && hyundai_kona_output_seen && hyundai_kona_host_output && - hyundai_kona_camera_seen && !relay_malfunction && !safety_rx_checks_invalid && aol_rx_healthy() && - !hyundai_kona_host_steering() && msg->fd && hyundai_kona_lfa_valid(msg) && - ((uint8_t)(msg->data[2] - hyundai_kona_output_counter) == 1U) && - ((uint8_t)(msg->data[2] - hyundai_kona_camera_counter) == 1U) && - (safety_get_ts_elapsed(now, hyundai_kona_camera_ts) <= 20000U); -} - -static bool hyundai_kona_stage_tx(const CANPacket_t *msg) { - bool staged = false; - if (hyundai_kona_idle_handoff && (msg->addr == 0x12AU) && (msg->bus == 0U)) { - const uint32_t now = microsecond_timer_get(); - const uint8_t source_delta = (uint8_t)(hyundai_kona_camera_counter - msg->data[2]); - staged = !safety_rx_checks_invalid && aol_rx_healthy() && hyundai_kona_host_steering() && - hyundai_kona_lfa_valid(msg) && hyundai_kona_camera_seen && - (safety_get_ts_elapsed(now, hyundai_kona_camera_ts) <= 20000U) && (source_delta <= 1U); - if (staged) { - hyundai_kona_pending_msg = *msg; - hyundai_kona_pending_ts = now; - hyundai_kona_pending = true; - } else { - hyundai_kona_pending = false; - } - } - return staged; -} - -static bool hyundai_kona_camera_output(const CANPacket_t *msg, CANPacket_t *output, bool *replaced) { - bool blocked = false; - if (hyundai_kona_idle_handoff && (msg->bus == 2U) && (msg->addr == 0x12AU)) { - const uint32_t now = microsecond_timer_get(); - const uint8_t counter = msg->data[2]; - const uint32_t camera_age = safety_get_ts_elapsed(now, hyundai_kona_camera_ts); - const uint8_t delta = (uint8_t)(counter - hyundai_kona_camera_counter); - const bool advancing = (delta > 0U) && (delta <= 127U); - const bool consecutive = hyundai_kona_camera_seen && (delta == 1U) && (camera_age <= 20000U); - blocked = !msg->fd || !hyundai_kona_lfa_valid(msg) || safety_rx_checks_invalid || !aol_rx_healthy() || - (hyundai_kona_camera_seen && (camera_age <= 50000U) && !advancing); - if (!blocked) { - hyundai_kona_camera_counter = counter; - hyundai_kona_camera_seen = true; - hyundai_kona_camera_ts = now; - - const bool authority = hyundai_kona_host_steering(); - if (!authority) { - hyundai_kona_slot_owned = false; - } - if (authority && consecutive && hyundai_kona_pending && - (safety_get_ts_elapsed(now, hyundai_kona_pending_ts) <= 20000U)) { - CANPacket_t candidate = hyundai_kona_pending_msg; - const uint8_t source_delta = (uint8_t)(counter - candidate.data[2]); - const uint8_t camera_grade = (msg->data[4] >> 6U) | ((msg->data[5] & 1U) << 2U); - const bool camera_warning = (camera_grade >= 4U) || ((msg->data[7] & 0xF0U) != 0U); - if (camera_warning) { - candidate.data[4] = (candidate.data[4] & 0x3FU) | (msg->data[4] & 0xC0U); - candidate.data[5] = (candidate.data[5] & 0xFEU) | (msg->data[5] & 0x01U); - candidate.data[7] = (candidate.data[7] & 0x0FU) | (msg->data[7] & 0xF0U); - } - const unsigned int raw_torque = (((unsigned int)candidate.data[6] & 0xFU) << 7U) | (candidate.data[5] >> 1U); - const int torque = (int)raw_torque - 1024; - if ((source_delta <= 2U) && - !safety_max_limit_check(torque, HYUNDAI_CANFD_STEER_MAX_TORQUE, -HYUNDAI_CANFD_STEER_MAX_TORQUE)) { - if (GET_BIT(&candidate, 52U)) { - const int max_torque = SAFETY_MAX(desired_torque_last, 0) + HYUNDAI_CANFD_STEER_MAX_RATE_UP; - const int min_torque = SAFETY_MIN(desired_torque_last, 0) - HYUNDAI_CANFD_STEER_MAX_RATE_UP; - const unsigned int raw = (unsigned int)(SAFETY_CLAMP(torque, min_torque, max_torque) + 1024); - candidate.data[5] = (candidate.data[5] & 0x01U) | (uint8_t)((raw & 0x7FU) << 1U); - candidate.data[6] = (candidate.data[6] & 0xF0U) | (uint8_t)(raw >> 7U); - } - candidate.fd = msg->fd; - candidate.data[2] = counter; - const uint16_t checksum = hyundai_common_canfd_compute_checksum(&candidate) ^ 0x041DU; - candidate.data[0] = (uint8_t)checksum; - candidate.data[1] = (uint8_t)(checksum >> 8U); - hyundai_kona_emitting = true; - *replaced = safety_tx_hook(&candidate); - hyundai_kona_emitting = false; - if (*replaced) { - *output = candidate; - hyundai_kona_slot_owned = true; - } - } - } - hyundai_kona_pending = false; - if (!*replaced && authority && consecutive && hyundai_kona_slot_owned) { - // Keep the camera's status and counter without repeating old host torque. - CANPacket_t neutral = *msg; - neutral.data[5] &= 0x01U; - neutral.data[6] = (neutral.data[6] & 0xC0U) | 0x08U; - const uint16_t checksum = hyundai_common_canfd_compute_checksum(&neutral) ^ 0x041DU; - neutral.data[0] = (uint8_t)checksum; - neutral.data[1] = (uint8_t)(checksum >> 8U); - neutral.bus = 0U; - hyundai_kona_emitting = true; - *replaced = safety_tx_hook(&neutral); - hyundai_kona_emitting = false; - if (*replaced) { - *output = neutral; - } - } - if (!*replaced) { - const bool camera_neutral = ((msg->data[6] & 0x3FU) == 0x08U) && ((msg->data[5] & 0xFEU) == 0U); - blocked = (hyundai_kona_slot_owned && !camera_neutral) || - !hyundai_kona_output_ready(now, false, counter); - if (!blocked) { - if (hyundai_kona_host_output) { - desired_torque_last = 0; - } - hyundai_kona_track_output(now, false, counter); - } - } - } else { - hyundai_kona_pending = false; - } - } - return blocked; -} -#endif - static bool hyundai_canfd_tx_hook_checked(const CANPacket_t *msg) { bool tx = false; const bool ordinary_angle_frame = hyundai_ordinary_angle_stock && ((msg->addr == 0x110U) || (msg->addr == 0x362U)); @@ -1464,30 +1349,33 @@ static bool hyundai_canfd_tx_hook_checked(const CANPacket_t *msg) { if (hyundai_angle_aol_enabled && independent_command && !tx) { hyundai_ordinary_angle_release(); } -#if defined(ALLOW_DEBUG) && !defined(STM32F4) - if (hyundai_kona_idle_handoff && (msg->addr == 0x12AU) && tx && !relay_malfunction) { - hyundai_kona_track_output(microsecond_timer_get(), true, msg->data[2]); - } -#endif return tx; } static bool hyundai_canfd_tx_hook(const CANPacket_t *msg) { #if defined(ALLOW_DEBUG) && !defined(STM32F4) - bool allowed = true; - if (hyundai_kona_idle_handoff && (msg->addr == 0x12AU)) { + if (hyundai_kona_ccnc_feedback && (msg->addr == 0x12AU)) { const uint32_t now = microsecond_timer_get(); - allowed = hyundai_kona_emitting && !safety_rx_checks_invalid && aol_rx_healthy() && hyundai_kona_host_steering() && - msg->fd && hyundai_kona_lfa_valid(msg) && hyundai_kona_camera_seen && - (safety_get_ts_elapsed(now, hyundai_kona_camera_ts) <= 50000U) && - (msg->data[2] == hyundai_kona_camera_counter) && hyundai_kona_output_ready(now, true, msg->data[2]); + const uint8_t delta = (uint8_t)(msg->data[2] - hyundai_kona_host_counter); + const bool advancing = !hyundai_kona_host_seen || + (safety_get_ts_elapsed(now, hyundai_kona_host_ts) > 50000U) || + ((delta > 0U) && (delta <= 127U)); + const bool active = ((msg->data[5] & 0xFEU) != 0U) || ((msg->data[6] & 0x3FU) != 8U); + const bool camera_current = hyundai_kona_camera_seen && + (safety_get_ts_elapsed(now, hyundai_kona_camera_ts) <= 50000U); + const bool allowed = !relay_malfunction && advancing && hyundai_kona_lfa_valid(msg) && + (!active || (!safety_rx_checks_invalid && aol_rx_healthy() && + lateral_controls_allowed() && camera_current)) && + hyundai_canfd_tx_hook_checked(msg); + if (allowed) { + hyundai_kona_host_seen = true; + hyundai_kona_host_counter = msg->data[2]; + hyundai_kona_host_ts = now; + } + return allowed; } #endif - return -#if defined(ALLOW_DEBUG) && !defined(STM32F4) - allowed && -#endif - hyundai_canfd_tx_hook_checked(msg); + return hyundai_canfd_tx_hook_checked(msg); } static void hyundai_canfd_reset_ioniq6_state(void) { @@ -2442,14 +2330,11 @@ static safety_config hyundai_canfd_init(uint16_t raw_param) { hyundai_canfd_reset_ioniq6_state(); hyundai_ordinary_angle_reset(); #if defined(ALLOW_DEBUG) && !defined(STM32F4) && !defined(PANDA_BODY) - hyundai_kona_idle_handoff = (raw_param == 0x1C0CU) && (alternative_experience == 32); + hyundai_kona_ccnc_feedback = (raw_param == 0x1C0CU) && (alternative_experience == 32); hyundai_kona_camera_seen = false; - hyundai_kona_output_seen = false; - hyundai_kona_pending = false; - hyundai_kona_emitting = false; - hyundai_kona_slot_owned = false; - const bool kona_profile_valid = (raw_param != 0x1C0CU) || hyundai_kona_idle_handoff; - const uint16_t param = hyundai_kona_idle_handoff ? 0x0C0CU : raw_param; + hyundai_kona_host_seen = false; + const bool kona_profile_valid = (raw_param != 0x1C0CU) || hyundai_kona_ccnc_feedback; + const uint16_t param = hyundai_kona_ccnc_feedback ? 0x0C0CU : raw_param; const bool valid = !GET_FLAG(param, 32768U) || (param == 0x8015U) || (param == 0x8095U) || (param == 0x8815U) || (param == 0x8895U) || hyundai_canfd_gv60_long_param(param); @@ -2470,20 +2355,6 @@ static safety_config hyundai_canfd_init(uint16_t raw_param) { const bool daw_profile = (param == 0x0011U) || (param == 0x0091U) || (param == 0x0811U) || (param == 0x0891U) || (param == 0x8015U) || (param == 0x8095U) || (param == 0x8815U) || (param == 0x8895U); ret = hyundai_daw_configure(ret, true, daw_profile); -#if defined(ALLOW_DEBUG) && !defined(STM32F4) - if (hyundai_kona_idle_handoff) { - static CanMsg kona_tx[64]; - if (hyundai_canfd_angle_aol_capacity(&ret)) { - for (int i = 0; i < ret.tx_msgs_len; i++) { - kona_tx[i] = ret.tx_msgs[i]; - if (kona_tx[i].addr == 0x12A) { - kona_tx[i].disable_static_blocking = true; - } - } - ret.tx_msgs = kona_tx; - } - } -#endif } return ret; } @@ -2495,9 +2366,7 @@ const safety_hooks hyundai_canfd_hooks = { .tx = hyundai_canfd_tx_hook, .fwd = hyundai_canfd_fwd_hook, #if defined(ALLOW_DEBUG) && !defined(STM32F4) - .fwd_packet = hyundai_kona_camera_without_slot, - .stage_tx = hyundai_kona_stage_tx, - .fwd_output = hyundai_kona_camera_output, + .fwd_output = hyundai_kona_mdps_output, #endif .get_counter = hyundai_canfd_get_counter, .get_checksum = hyundai_canfd_get_checksum, diff --git a/opendbc_repo/opendbc/safety/safety.h b/opendbc_repo/opendbc/safety/safety.h index 49f684a029..5fce8aad6d 100644 --- a/opendbc_repo/opendbc/safety/safety.h +++ b/opendbc_repo/opendbc/safety/safety.h @@ -368,40 +368,16 @@ int safety_fwd_packet_hook(const CANPacket_t *msg) { } #if !defined(STM32F4) && (defined(ALLOW_DEBUG) || !defined(STM32H7)) -bool safety_uses_camera_slot(const CANPacket_t *msg) { +bool safety_uses_forward_transform(const CANPacket_t *msg) { #ifdef ALLOW_DEBUG - return (current_safety_mode == SAFETY_HYUNDAI_CANFD) && hyundai_kona_idle_handoff && (msg->addr == 0x12AU); + return (current_safety_mode == SAFETY_HYUNDAI_CANFD) && hyundai_kona_ccnc_feedback && + (msg->addr == 0xEAU) && (msg->bus == 0U); #else (void)msg; return false; #endif } -bool safety_camera_release_current(const CANPacket_t *msg, uint32_t now) { -#ifdef ALLOW_DEBUG - return safety_uses_camera_slot(msg) && hyundai_kona_camera_release_current(msg, now); -#else - (void)msg; - (void)now; - return false; -#endif -} - -uint32_t safety_camera_release_delay(const CANPacket_t *msg, uint32_t now) { -#ifdef ALLOW_DEBUG - const uint32_t age = safety_get_ts_elapsed(now, hyundai_kona_output_ts); - return ((age >= 9000U) && (age < 10000U) && safety_camera_release_current(msg, now)) ? (10000U - age) : 0U; -#else - (void)msg; - (void)now; - return 0U; -#endif -} - -bool safety_stage_tx_hook(const CANPacket_t *msg) { - return !relay_malfunction && (current_hooks->stage_tx != NULL) && current_hooks->stage_tx(msg); -} - int safety_fwd_output_hook(const CANPacket_t *msg, CANPacket_t *output, bool *replaced) { int destination; if (current_hooks->fwd_output != NULL) { diff --git a/opendbc_repo/opendbc/safety/tests/misra/main.c b/opendbc_repo/opendbc/safety/tests/misra/main.c index 802ed8d54a..7606574764 100644 --- a/opendbc_repo/opendbc/safety/tests/misra/main.c +++ b/opendbc_repo/opendbc/safety/tests/misra/main.c @@ -11,10 +11,7 @@ SAFETY_UNUSED(safety_rx_hook); SAFETY_UNUSED(safety_tx_hook); SAFETY_UNUSED(safety_fwd_hook); SAFETY_UNUSED(safety_fwd_packet_hook); -SAFETY_UNUSED(safety_uses_camera_slot); -SAFETY_UNUSED(safety_camera_release_current); -SAFETY_UNUSED(safety_camera_release_delay); -SAFETY_UNUSED(safety_stage_tx_hook); +SAFETY_UNUSED(safety_uses_forward_transform); SAFETY_UNUSED(safety_fwd_output_hook); SAFETY_UNUSED(safety_tick); SAFETY_UNUSED(set_safety_hooks); diff --git a/opendbc_repo/opendbc/safety/tests/test_hyundai_kona_ccnc_handoff.py b/opendbc_repo/opendbc/safety/tests/test_hyundai_kona_ccnc_handoff.py index f201f7c33f..51a85a1a77 100644 --- a/opendbc_repo/opendbc/safety/tests/test_hyundai_kona_ccnc_handoff.py +++ b/opendbc_repo/opendbc/safety/tests/test_hyundai_kona_ccnc_handoff.py @@ -1,10 +1,11 @@ import unittest from opendbc.car import structs +from opendbc.car.hyundai.hyundaicanfd import hkg_can_fd_checksum from opendbc.safety.tests.libsafety import libsafety_py -class TestHyundaiKonaCcncHandoff(unittest.TestCase): +class TestHyundaiKonaCcncFeedback(unittest.TestCase): def setUp(self): self.safety = libsafety_py.libsafety self.addCleanup(self.safety.set_alternative_experience, 0) @@ -15,136 +16,101 @@ class TestHyundaiKonaCcncHandoff(unittest.TestCase): self.safety.set_alternative_experience(alternative) self.assertEqual(self.safety.set_safety_hooks(structs.CarParams.SafetyModel.hyundaiCanfd, word), 0) - def packet(self, bus, body): - packet = libsafety_py.make_CANPacket(0x12A, bus, bytes.fromhex(body)) - packet[0].fd = bus == 2 + @staticmethod + def packet(address, bus, body): + body = bytearray(body) + body[:2] = hkg_can_fd_checksum(address, None, body).to_bytes(2, 'little') + packet = libsafety_py.make_CANPacket(address, bus, body) + packet[0].fd = 1 return packet - def test_handoff_is_debug_only_and_requires_exact_alternative_experience(self): - debug = self.safety.set_safety_hooks(structs.CarParams.SafetyModel.allOutput, 0) == 0 - camera = self.packet(2, 'a6770a1880001800000a000000900000') - for experience in ((0,) if debug else (0, 32)): - self.mode(0x1C0C, experience) - self.assertFalse(self.safety.safety_uses_camera_slot(camera)) - self.assertFalse(self.safety.safety_camera_release_current(camera, 0)) - self.assertEqual(self.safety.safety_camera_release_delay(camera, 0), 0) - self.assertEqual(self.safety.safety_fwd_packet_hook(camera), -1) - self.assertFalse(self.safety.safety_tx_hook(self.packet(0, 'a6770a1880001800000a000000900000'))) - self.assertEqual(self.safety.safety_fwd_hook(2, 0x123), -1) - - def test_release_library_camera_slot_helpers_are_inactive(self): - release = libsafety_py.ffi.dlopen(libsafety_py._build_libsafety(release=True)) - release.set_alternative_experience(32) - self.assertEqual(release.set_safety_hooks(structs.CarParams.SafetyModel.hyundaiCanfd, 0x1C0C), 0) - camera = self.packet(2, 'a6770a1880001800000a000000900000') - self.assertFalse(release.safety_uses_camera_slot(camera)) - self.assertFalse(release.safety_camera_release_current(camera, 10_000)) - self.assertEqual(release.safety_camera_release_delay(camera, 10_000), 0) - self.assertEqual(release.safety_fwd_packet_hook(camera), -1) - - def test_output_hook_preserves_default_forwarding(self): - self.assertEqual(self.safety.set_safety_hooks(structs.CarParams.SafetyModel.hyundai, 0), 0) - for source, destination in ((0, 2), (1, -1), (2, 0)): - packet = libsafety_py.make_CANPacket(0x123, source, bytes(8)) - self.assertEqual(self.safety.safety_fwd_packet_hook(packet), destination) - self.assertEqual(self.forward(packet)[:2], (destination, False)) - def forward(self, packet): output = libsafety_py.new_CANPacket() replaced = libsafety_py.ffi.new('bool *') bus = self.safety.safety_fwd_output_hook(packet, output, replaced) return bus, bool(replaced[0]), output - def test_handoff_requires_current_camera_and_one_output_owner(self): + def debug_mode(self): if self.safety.set_safety_hooks(structs.CarParams.SafetyModel.allOutput, 0) != 0: - self.skipTest('Kona factory handoff is DEBUG only') - camera = self.packet(2, 'a6770a1880001800000a000000900000') - self.mode(0x0C0C) - self.assertEqual(self.forward(camera)[0], -1) - + self.skipTest('Kona feedback profile is DEBUG only') self.mode(0x1C0C) + + def test_feedback_is_exact_profile_and_debug_only(self): + feedback = self.packet(0xEA, 0, bytes(24)) + for word, alternative, forward in ((0x1C0C, 0, -1), (0x0C0C, 32, 2), (0x0008, 0, 2)): + with self.subTest(word=word, alternative=alternative): + self.mode(word, alternative) + self.assertFalse(self.safety.safety_uses_forward_transform(feedback)) + self.assertEqual(self.forward(feedback)[:2], (forward, False)) + release = libsafety_py.ffi.dlopen(libsafety_py._build_libsafety(release=True)) + release.set_alternative_experience(32) + self.assertEqual(release.set_safety_hooks(structs.CarParams.SafetyModel.hyundaiCanfd, 0x1C0C), 0) + self.assertFalse(release.safety_uses_forward_transform(feedback)) + self.assertEqual(release.safety_fwd_packet_hook(feedback), -1) + + def test_eps_feedback_preserves_faults_and_never_changes_vehicle_side_input(self): + self.debug_mode() + for warning_byte, toi_byte, modify in ((0, 0, True), (0, 1, True), (0, 3, False), + (1, 1, False), (0, 5, False), (0, 0x10, False), (0, 0x40, False)): + with self.subTest(warning_byte=warning_byte, toi_byte=toi_byte): + body = bytearray(24) + body[2:4] = bytes((0xAA, warning_byte)) + body[6:9] = bytes((toi_byte, 0x12, 0xA3)) + packet = self.packet(0xEA, 0, body) + original = bytes(packet[0].data[0:24]) + self.assertTrue(self.safety.safety_uses_forward_transform(packet)) + self.assertFalse(self.safety.safety_tx_hook(self.packet(0xEA, 2, original))) + bus, replaced, output = self.forward(packet) + self.assertEqual((bus, replaced), (2, modify)) + self.assertEqual(bytes(packet[0].data[0:24]), original) + if modify: + body[6] = 4 + expected = self.packet(0xEA, 0, body) + self.assertEqual(bytes(output[0].data[0:24]), bytes(expected[0].data[0:24])) + packet[0].data[0] ^= 1 + self.assertFalse(self.forward(packet)[1]) + self.safety.set_relay_malfunction(True) + self.assertEqual(self.forward(packet)[:2], (-1, False)) + + def test_host_steering_is_independent_of_camera_counter_and_respects_authority(self): + self.debug_mode() self.safety.set_timer(1_000_000) - self.assertEqual(self.forward(camera)[0], -1) for status in self.safety.rx_status: status.msg_seen = True status.valid_checksum = True status.valid_quality_flag = True status.last_timestamp = 1_000_000 - camera[0].fd = 0 - self.assertEqual(self.forward(camera)[0], -1) - camera[0].fd = 1 - self.assertEqual(self.forward(camera)[:2], (0, False)) - self.assertEqual(self.forward(camera)[0], -1) - corrupt = self.packet(2, 'a6770a1881001800000a000000900000') - self.assertEqual(self.forward(corrupt)[0], -1) - self.assertFalse(self.safety.safety_stage_tx_hook(self.packet(0, 'a6770a1880001800000a000000900000'))) - warning = self.packet(2, '784d1400000108f0000a000000640000') - self.assertEqual(self.forward(warning)[:2], (0, False)) + def lfa(bus, counter, torque, request): + body = bytearray.fromhex('0000000280000800000a000000900000') + raw_torque = 1024 + torque + body[2] = counter + body[5] = (raw_torque & 127) << 1 + body[6] = (raw_torque >> 7) | (0x10 if request else 0) + return self.packet(0x12A, bus, body) + self.safety.set_controls_allowed(True) self.safety.set_aol_test_heartbeat(True) self.safety.aol_set_host_request(3) - host = self.packet(0, 'a009150280001800000a000000900000') - host[0].data[2] = 20 - from opendbc.car.hyundai.hyundaicanfd import hkg_can_fd_checksum - crc = hkg_can_fd_checksum(0x12A, None, bytearray(host[0].data[0:16])) - host[0].data[0], host[0].data[1] = crc & 255, crc >> 8 - self.safety.set_timer(1_005_000) - self.assertFalse(self.safety.safety_tx_hook(host)) - host[0].data[0] ^= 1 - self.assertFalse(self.safety.safety_stage_tx_hook(host)) - host[0].data[0] ^= 1 - self.assertTrue(self.safety.safety_stage_tx_hook(host)) - self.safety.set_timer(1_010_000) - match = self.packet(2, '5ce51500000108f0000a000000640000') - bus, replaced, output = self.forward(match) - self.assertEqual((bus, replaced, output[0].data[2]), (0, True, 21)) - self.assertTrue(output[0].fd) - self.assertEqual(output[0].data[7] & 0xF0, 0xF0) - + self.assertFalse(self.safety.safety_tx_hook(lfa(0, 254, 5, True))) + camera = lfa(2, 81, 0, False) + self.assertEqual(self.forward(camera)[:2], (-1, False)) + self.safety.safety_rx_hook(camera) + self.assertTrue(self.safety.safety_tx_hook(lfa(0, 254, 5, True))) + for stamp, count, torque in ((1_010_000, 255, 10), (1_020_000, 0, 15)): + self.safety.set_timer(stamp) + self.assertTrue(self.safety.safety_tx_hook(lfa(0, count, torque, True))) + self.assertFalse(self.safety.safety_tx_hook(lfa(0, count, torque, True))) + corrupt = lfa(0, 1, 20, True) + corrupt[0].data[0] ^= 1 + self.assertFalse(self.safety.safety_tx_hook(corrupt)) + self.assertFalse(self.safety.safety_tx_hook(lfa(0, 1, 500, True))) + self.safety.set_timer(1_050_001) + self.assertFalse(self.safety.safety_tx_hook(lfa(0, 1, 5, True))) + self.safety.safety_rx_hook(lfa(2, 150, 0, False)) self.safety.set_controls_allowed(False) self.safety.aol_set_host_request(0) - following = self.packet(2, '110d1600000108f0000a000000640000') - self.safety.set_timer(1_019_999) - self.assertEqual(self.safety.safety_camera_release_delay(following, 1_019_999), 1) - self.safety.set_timer(1_020_000) - self.assertEqual(self.forward(following)[:2], (0, False)) - self.assertEqual(self.forward(following)[0], -1) + self.assertFalse(self.safety.safety_tx_hook(lfa(0, 1, 5, True))) + self.assertTrue(self.safety.safety_tx_hook(lfa(0, 1, 0, False))) self.safety.set_relay_malfunction(True) - self.assertEqual(self.forward(following)[0], -1) - self.assertFalse(self.safety.safety_stage_tx_hook(host)) - - def test_empty_owned_slot_uses_neutral_camera_status(self): - if self.safety.set_safety_hooks(structs.CarParams.SafetyModel.allOutput, 0) != 0: - self.skipTest('Kona factory handoff is DEBUG only') - self.mode(0x1C0C) - self.safety.set_timer(1_000_000) - for status in self.safety.rx_status: - status.msg_seen = True - status.valid_checksum = True - status.valid_quality_flag = True - status.last_timestamp = 1_000_000 - self.assertEqual(self.forward(self.packet(2, '784d1400000108f0000a000000640000'))[:2], (0, False)) - self.safety.set_controls_allowed(True) - self.safety.set_aol_test_heartbeat(True) - self.safety.aol_set_host_request(3) - host = self.packet(0, 'a009150280001800000a000000900000') - host[0].data[2] = 20 - from opendbc.car.hyundai.hyundaicanfd import hkg_can_fd_checksum - crc = hkg_can_fd_checksum(0x12A, None, bytearray(host[0].data[0:16])) - host[0].data[0], host[0].data[1] = crc & 255, crc >> 8 - self.safety.set_timer(1_005_000) - self.assertTrue(self.safety.safety_stage_tx_hook(host)) - self.safety.set_timer(1_010_000) - self.assertEqual(self.forward(self.packet(2, '5ce51500000108f0000a000000640000'))[:2], (0, True)) - - self.safety.set_timer(1_019_900) - camera = self.packet(2, '110d1600000108f0000a000000640000') - bus, replaced, output = self.forward(camera) - self.assertEqual((bus, replaced), (0, True)) - self.assertTrue(output[0].fd) - self.assertEqual(output[0].data[2], 22) - self.assertEqual(output[0].data[5] & 0xFE, 0) - self.assertEqual(output[0].data[6] & 0x3F, 8) - self.assertEqual(output[0].data[7], camera[0].data[7]) - self.assertEqual(self.forward(camera)[0], -1) + self.assertFalse(self.safety.safety_tx_hook(lfa(0, 2, 0, False))) diff --git a/openpilot/selfdrive/controls/lib/longitudinal_planner.py b/openpilot/selfdrive/controls/lib/longitudinal_planner.py index d63b02f5a9..b948f4d37f 100755 --- a/openpilot/selfdrive/controls/lib/longitudinal_planner.py +++ b/openpilot/selfdrive/controls/lib/longitudinal_planner.py @@ -36,7 +36,7 @@ from openpilot.starpilot.longitudinal.ioniq6_start import eligible as ioniq6_lon from openpilot.starpilot.longitudinal.lane_change_gap import LaneChangeGap, project as project_lane_gap from openpilot.starpilot.longitudinal.experimental_release import ConditionalHandoff, ExperimentalRelease, project as project_release from openpilot.starpilot.longitudinal.force_stop import StopPlan -from openpilot.starpilot.longitudinal.force_stop_braking import distance_target, eligible as distance_braking_eligible +from openpilot.starpilot.longitudinal.force_stop_braking import distance_target, eligible as distance_braking_eligible, kona_eligible from openpilot.starpilot.longitudinal.follow_jerk import FollowJerk from openpilot.starpilot.longitudinal.highlander_cruise import owner_for as highlander_cruise_owner from openpilot.starpilot.longitudinal.lead_takeoff import LeadTakeoff, project as project_takeoff @@ -151,6 +151,7 @@ class LongitudinalPlanner: self.throttle_gate = ModelThrottleGate() self.ioniq6_throttle_gate_enabled = ioniq6_long_eligible(CP) self.force_stop_distance_braking = distance_braking_eligible(CP) + self.kona_distance_handoff = self.force_stop_distance_braking and kona_eligible(CP) self.lane_change_gap = LaneChangeGap() self.experimental_release = ExperimentalRelease() self.lead_approach = LeadApproach(dt, CP.longitudinalActuatorDelay) @@ -564,8 +565,10 @@ class LongitudinalPlanner: candidates = [(output_a_target_mpc, self.mpc.source, output_should_stop_mpc), (self.a_cruise, LongitudinalPlanSource.cruise, cruise_should_stop)] - if self.force_stop_distance_braking and self.force_stop_plan.forcing and self.force_stop_plan.obstacle_m is not None: - stop_target = distance_target(v_ego, sm['carState'].aEgo, self.force_stop_plan.obstacle_m, action_t) + if (self.force_stop_distance_braking and self.force_stop_plan.forcing and self.force_stop_plan.obstacle_m is not None and + (not self.kona_distance_handoff or self.force_stop_plan.should_stop)): + stop_target = distance_target(v_ego, sm['carState'].aEgo, self.force_stop_plan.obstacle_m, action_t, + existing_brake=self.kona_distance_handoff) candidates.append((stop_target, LongitudinalPlanSource.cruise, False)) if sm['selfdriveState'].experimentalMode: candidates.append((output_a_target_e2e, LongitudinalPlanSource.e2e, output_should_stop_e2e)) diff --git a/openpilot/starpilot/longitudinal/force_stop.py b/openpilot/starpilot/longitudinal/force_stop.py index fca71e9c29..3bd95472ff 100644 --- a/openpilot/starpilot/longitudinal/force_stop.py +++ b/openpilot/starpilot/longitudinal/force_stop.py @@ -38,6 +38,7 @@ class StopTune: reanchor_speed_tolerance: float | None = None low_speed_hold_mps: float | None = None distance_control: bool = False + distance_handoff: bool = False def vehicle_tune(fingerprint: str) -> StopTune: @@ -402,6 +403,8 @@ class ForceStop: obstacle = max(0.0,min(self.distance * .93 + offset + tune.distance_bias_m, (self.distance + offset + tune.distance_bias_m) * .93)) should_stop = self.stop_requested if tune.distance_control else bool(self.forcing and ceiling is not None and ceiling <= .5) + if tune.distance_handoff and should_stop and not frame.standstill: + obstacle = max(0.0, self.distance * .93 + offset + tune.distance_bias_m) return StopPlan(now, ceiling, obstacle, self.forcing, should_stop, max(0.0,self.distance) if self.forcing else 0.0, approach, tune.jerk_scale if self.forcing or approach > 0 else 1.0, bool(held and self.forcing and not overridden), diff --git a/openpilot/starpilot/longitudinal/force_stop_braking.py b/openpilot/starpilot/longitudinal/force_stop_braking.py index e27983b255..689e4b02f7 100644 --- a/openpilot/starpilot/longitudinal/force_stop_braking.py +++ b/openpilot/starpilot/longitudinal/force_stop_braking.py @@ -3,6 +3,7 @@ import math from openpilot.starpilot.longitudinal.ioniq6_start import eligible as ioniq6_eligible +from opendbc.car.hyundai.canfd_stock_aol import KONA_IDLE_AOL_WORD, kona_idle_handoff_profile from opendbc.car.hyundai.ioniq5pe_long_profile import qualified as ioniq5pe_qualified MAX_AGE_NS = 150_000_000 @@ -10,21 +11,30 @@ STOP_RESERVE_M = .5 MIN_ACCEL = -3.5 +def kona_eligible(cp): + return (kona_idle_handoff_profile(cp) and cp.alternativeExperience == 32 and + cp.safetyConfigs[0].safetyParam == KONA_IDLE_AOL_WORD and + math.isfinite(cp.longitudinalActuatorDelay) and 0.0 < cp.longitudinalActuatorDelay <= 2.0) + + def eligible(cp): try: if cp.passive or cp.dashcamOnly or cp.notCar: return False - return ioniq6_eligible(cp) or ioniq5pe_qualified(cp) + return ioniq6_eligible(cp) or ioniq5pe_qualified(cp) or kona_eligible(cp) except (AttributeError, IndexError, TypeError, ValueError, RuntimeError): return False -def distance_target(speed, acceleration, distance, delay): +def distance_target(speed, acceleration, distance, delay, *, existing_brake=False): if (not all(math.isfinite(x) for x in (speed, acceleration, distance, delay)) or speed < 0 or distance < 0 or not 0 <= delay <= 2): return MIN_ACCEL - remaining = distance - speed * delay - .5 * max(acceleration, 0.) * delay**2 - STOP_RESERVE_M - future_speed = speed + max(acceleration, 0.) * delay + realized_accel = max(acceleration, 0.) + if existing_brake and acceleration < 0. and delay > 0.: + realized_accel = max(acceleration, -speed / delay) + remaining = distance - speed * delay - .5 * realized_accel * delay**2 - STOP_RESERVE_M + future_speed = speed + realized_accel * delay return max(MIN_ACCEL, -future_speed**2 / (2 * max(remaining, .01))) diff --git a/openpilot/starpilot/longitudinal/force_stop_runtime.py b/openpilot/starpilot/longitudinal/force_stop_runtime.py index 0c4d9a9539..fca857b25a 100644 --- a/openpilot/starpilot/longitudinal/force_stop_runtime.py +++ b/openpilot/starpilot/longitudinal/force_stop_runtime.py @@ -15,7 +15,7 @@ from openpilot.starpilot.longitudinal.force_stop import ForceStop, StopFrame, St from openpilot.starpilot.model_geometry import road_curvature, standstill_clear_horizon, standstill_release_speed from openpilot.starpilot.saved_source import read_saved from openpilot.starpilot.longitudinal.stop_resume import StopResume -from openpilot.starpilot.longitudinal.force_stop_braking import eligible as distance_braking_eligible +from openpilot.starpilot.longitudinal.force_stop_braking import eligible as distance_braking_eligible, kona_eligible from openpilot.starpilot.speed_limits.vision.stop_observation import CONTROL_STOP_MAX_AGE_NS, StopConfirmation @@ -211,7 +211,10 @@ class ForceStopRuntime: moving_preview=bool(perception_available and not model.action.shouldStop and len(model.velocity.x) == len(model.position.x) and math.isfinite(model.velocity.x[-1]))) - tune = replace(vehicle_tune(str(cp.carFingerprint)), distance_control=distance_braking_eligible(cp)) + distance_enabled = distance_braking_eligible(cp) + kona_handoff = distance_enabled and kona_eligible(cp) + tune = replace(vehicle_tune(str(cp.carFingerprint)), distance_control=distance_enabled and not kona_handoff, + distance_handoff=kona_handoff) self.plan = self.policy.step(frame, tune, self.offset) if takeoff_enabled: self.plan = replace(self.plan, takeoff_light_observed=takeoff_observed, takeoff_light=takeoff_light) diff --git a/openpilot/starpilot/longitudinal/tests/test_force_stop_braking.py b/openpilot/starpilot/longitudinal/tests/test_force_stop_braking.py index e897d2dc6e..68ee9dd4d4 100644 --- a/openpilot/starpilot/longitudinal/tests/test_force_stop_braking.py +++ b/openpilot/starpilot/longitudinal/tests/test_force_stop_braking.py @@ -190,3 +190,38 @@ class Ioniq5PEDistanceBrakingTests(TestCase): normal.long_control_state = LongCtrlState.pid normal.last_output_accel = -1.5 self.assertLess(normal.update(True, state, -.8, True, (-3.5, 2.), context=context), -1.5) + + +class KonaDistanceBrakingTests(TestCase): + def test_exact_profile_keeps_the_stop_line_and_uses_delivered_braking(self): + from dataclasses import replace + + from opendbc.car.hyundai.tests.test_kona_ccnc_long_aol import CARS, kona_params + from openpilot.starpilot.longitudinal.force_stop import ForceStop, vehicle_tune + from openpilot.starpilot.longitudinal.force_stop_braking import eligible, kona_eligible + from openpilot.starpilot.longitudinal.tests.test_force_stop import frame + + cp = kona_params(CARS[0]) + cp.alternativeExperience = 32 + cp.safetyConfigs[0].safetyParam = 0x1C0C + self.assertTrue(eligible(cp) and kona_eligible(cp)) + cp.safetyConfigs[0].safetyParam = 0x0C0C + self.assertFalse(eligible(cp)) + current, new = ForceStop(), ForceStop() + tune = vehicle_tune(str(cp.carFingerprint)) + for force in (current, new): + force.forcing = True + force.distance = force.distance_cap = 6.2 + sample = frame(20, speed_mps=3.58, horizon_m=23.1, light=True) + legacy = current.step(sample, tune) + candidate = new.step(sample, replace(tune, distance_handoff=True)) + self.assertEqual(legacy.should_stop, candidate.should_stop) + self.assertEqual(legacy.tracked_distance_m, candidate.tracked_distance_m) + self.assertEqual(legacy.speed_ceiling_mps, candidate.speed_ceiling_mps) + self.assertTrue(candidate.should_stop) + self.assertIsNotNone(candidate.obstacle_m) + delivered, speed = -1.3, 3.58 + command = distance_target(speed, delivered, candidate.obstacle_m, .55, existing_brake=True) + self.assertAlmostEqual(command, -1.2, delta=.15) + self.assertLess(distance_target(speed, 0., candidate.obstacle_m, .55, existing_brake=True), -1.8) + self.assertEqual(distance_target(speed, 0., .5, .55, existing_brake=True), -3.5) diff --git a/panda/board/drivers/can_common.h b/panda/board/drivers/can_common.h index 296adce23e..dd56b7678f 100644 --- a/panda/board/drivers/can_common.h +++ b/panda/board/drivers/can_common.h @@ -187,23 +187,11 @@ bool can_check_checksum(CANPacket_t *packet) { void can_send(CANPacket_t *to_push, uint8_t bus_number, bool skip_tx_hook) { bool tx_allowed = skip_tx_hook; - #if defined(STM32H7) && defined(ALLOW_DEBUG) - bool staged = false; - #endif if (!tx_allowed) { #ifdef STM32H7 mpu_safety_open(); #endif - #if defined(STM32H7) && defined(ALLOW_DEBUG) - if (safety_uses_camera_slot(to_push)) { - ENTER_CRITICAL(); - staged = safety_stage_tx_hook(to_push); - EXIT_CRITICAL(); - } - tx_allowed = !staged && (safety_tx_hook(to_push) != 0); - #else tx_allowed = safety_tx_hook(to_push) != 0; - #endif #ifdef STM32H7 mpu_safety_close(); #endif @@ -215,9 +203,6 @@ void can_send(CANPacket_t *to_push, uint8_t bus_number, bool skip_tx_hook) { process_can(CAN_NUM_FROM_BUS_NUM(bus_number)); } } else { - #if defined(STM32H7) && defined(ALLOW_DEBUG) - if (!staged) { - #endif safety_tx_blocked += 1U; to_push->returned = 0U; to_push->rejected = 1U; @@ -225,9 +210,6 @@ void can_send(CANPacket_t *to_push, uint8_t bus_number, bool skip_tx_hook) { // data changed can_set_checksum(to_push); rx_buffer_overflow += can_push(&can_rx_q, to_push) ? 0U : 1U; - #if defined(STM32H7) && defined(ALLOW_DEBUG) - } - #endif } } diff --git a/panda/board/drivers/fdcan.h b/panda/board/drivers/fdcan.h index a8d61f1f2b..c01958c468 100644 --- a/panda/board/drivers/fdcan.h +++ b/panda/board/drivers/fdcan.h @@ -154,61 +154,6 @@ void process_can(uint8_t can_number) { } } -#if defined(ALLOW_DEBUG) && !defined(PANDA_BODY) -static bool kona_release_pending = false; -static CANPacket_t kona_release_frame; -static uint32_t kona_release_due = 0U; -static uint8_t kona_release_source = 0U; - -static void kona_release_cancel(void) { - if (kona_release_pending) { - kona_release_pending = false; - register_clear_bits(&MICROSECOND_TIMER->DIER, TIM_DIER_CC1IE); - MICROSECOND_TIMER->SR = ~TIM_SR_CC1IF; - NVIC_ClearPendingIRQ(TIM2_IRQn); - } -} - -static void kona_release_irq(void) { - ENTER_CRITICAL(); - const uint32_t now = microsecond_timer_get(); - const uint32_t late = get_ts_elapsed(now, kona_release_due); - if (kona_release_pending && (late < 0x80000000U)) { - kona_release_cancel(); - mpu_safety_open(); - if ((late <= 1000U) && safety_camera_release_current(&kona_release_frame, now)) { - CANPacket_t output = kona_release_frame; - bool replaced = false; - const int destination = safety_fwd_output_hook(&kona_release_frame, &output, &replaced); - mpu_safety_close(); - if ((destination >= 0) && !replaced) { - can_set_checksum(&output); - can_send(&output, (uint8_t)destination, true); - can_health[kona_release_source].total_fwd_cnt += 1U; - } - } else { - mpu_safety_close(); - } - } - MICROSECOND_TIMER->SR = ~TIM_SR_CC1IF; - EXIT_CRITICAL(); -} - -static void kona_release_schedule(const CANPacket_t *msg, uint8_t source, uint32_t due) { - kona_release_cancel(); - kona_release_frame = *msg; - kona_release_source = source; - kona_release_due = due; - kona_release_pending = true; - MICROSECOND_TIMER->SR = ~TIM_SR_CC1IF; - MICROSECOND_TIMER->CCR1 = due; - register_set_bits(&MICROSECOND_TIMER->DIER, TIM_DIER_CC1IE); - if (get_ts_elapsed(microsecond_timer_get(), due) < 0x80000000U) { - NVIC_SetPendingIRQ(TIM2_IRQn); - } -} -#endif - // FDFDCANx_IT0 IRQ Handler (RX and errors) // blink blue when we are receiving CAN messages static void can_rx(uint8_t can_number) { @@ -265,22 +210,8 @@ static void can_rx(uint8_t can_number) { #endif #ifdef ALLOW_DEBUG int bus_fwd_num; - if (safety_uses_camera_slot(&to_push)) { - ENTER_CRITICAL(); - #if defined(ALLOW_DEBUG) && !defined(PANDA_BODY) - const uint32_t now = microsecond_timer_get(); - const uint32_t release_delay = safety_camera_release_delay(&to_push, now); - kona_release_cancel(); - if (release_delay != 0U) { - kona_release_schedule(&to_push, can_number, now + release_delay); - bus_fwd_num = -1; - } else { - bus_fwd_num = safety_fwd_output_hook(&to_push, &to_send, &replaced); - } - #else + if (safety_uses_forward_transform(&to_push)) { bus_fwd_num = safety_fwd_output_hook(&to_push, &to_send, &replaced); - #endif - EXIT_CRITICAL(); } else { bus_fwd_num = safety_fwd_packet_hook(&to_push); } @@ -364,13 +295,6 @@ bool can_init(uint8_t can_number) { REGISTER_INTERRUPT(FDCAN2_IT1_IRQn, FDCAN2_IT1_IRQ_Handler, CAN_INTERRUPT_RATE, FAULT_INTERRUPT_RATE_CAN_2) REGISTER_INTERRUPT(FDCAN3_IT0_IRQn, FDCAN3_IT0_IRQ_Handler, CAN_INTERRUPT_RATE, FAULT_INTERRUPT_RATE_CAN_3) REGISTER_INTERRUPT(FDCAN3_IT1_IRQn, FDCAN3_IT1_IRQ_Handler, CAN_INTERRUPT_RATE, FAULT_INTERRUPT_RATE_CAN_3) - #if defined(ALLOW_DEBUG) && !defined(PANDA_BODY) - ENTER_CRITICAL(); - REGISTER_INTERRUPT(TIM2_IRQn, kona_release_irq, 100U, FAULT_INTERRUPT_RATE_CAN_3) - kona_release_cancel(); - nvic_enable(TIM2_IRQn); - EXIT_CRITICAL(); - #endif if (can_number != 0xffU) { FDCAN_GlobalTypeDef *FDCANx = CANIF_FROM_CAN_NUM(can_number); diff --git a/panda/board/obj/body_h7.bin.signed b/panda/board/obj/body_h7.bin.signed index ac458d0110..225458db2a 100644 Binary files a/panda/board/obj/body_h7.bin.signed and b/panda/board/obj/body_h7.bin.signed differ diff --git a/panda/board/obj/bootstub.body_h7.bin b/panda/board/obj/bootstub.body_h7.bin index b06ac531e4..35f3be0db4 100644 Binary files a/panda/board/obj/bootstub.body_h7.bin and b/panda/board/obj/bootstub.body_h7.bin differ diff --git a/panda/board/obj/bootstub.panda.bin b/panda/board/obj/bootstub.panda.bin index acec1613d8..1bc7802ce2 100644 Binary files a/panda/board/obj/bootstub.panda.bin and b/panda/board/obj/bootstub.panda.bin differ diff --git a/panda/board/obj/bootstub.panda_can_ignition_only.bin b/panda/board/obj/bootstub.panda_can_ignition_only.bin index acec1613d8..1bc7802ce2 100644 Binary files a/panda/board/obj/bootstub.panda_can_ignition_only.bin and b/panda/board/obj/bootstub.panda_can_ignition_only.bin differ diff --git a/panda/board/obj/bootstub.panda_h7.bin b/panda/board/obj/bootstub.panda_h7.bin index e23b3ee821..8ba72c744e 100644 Binary files a/panda/board/obj/bootstub.panda_h7.bin and b/panda/board/obj/bootstub.panda_h7.bin differ diff --git a/panda/board/obj/bootstub.panda_h7_can_ignition_only.bin b/panda/board/obj/bootstub.panda_h7_can_ignition_only.bin index e23b3ee821..8ba72c744e 100644 Binary files a/panda/board/obj/bootstub.panda_h7_can_ignition_only.bin and b/panda/board/obj/bootstub.panda_h7_can_ignition_only.bin differ diff --git a/panda/board/obj/bootstub.panda_h7_hkg_remote.bin b/panda/board/obj/bootstub.panda_h7_hkg_remote.bin index e23b3ee821..8ba72c744e 100644 Binary files a/panda/board/obj/bootstub.panda_h7_hkg_remote.bin and b/panda/board/obj/bootstub.panda_h7_hkg_remote.bin differ diff --git a/panda/board/obj/bootstub.panda_h7_hkg_remote_can_ignition_only.bin b/panda/board/obj/bootstub.panda_h7_hkg_remote_can_ignition_only.bin index e23b3ee821..8ba72c744e 100644 Binary files a/panda/board/obj/bootstub.panda_h7_hkg_remote_can_ignition_only.bin and b/panda/board/obj/bootstub.panda_h7_hkg_remote_can_ignition_only.bin differ diff --git a/panda/board/obj/bootstub.panda_h7_remote.bin b/panda/board/obj/bootstub.panda_h7_remote.bin index e23b3ee821..8ba72c744e 100644 Binary files a/panda/board/obj/bootstub.panda_h7_remote.bin and b/panda/board/obj/bootstub.panda_h7_remote.bin differ diff --git a/panda/board/obj/bootstub.panda_h7_remote_can_ignition_only.bin b/panda/board/obj/bootstub.panda_h7_remote_can_ignition_only.bin index e23b3ee821..8ba72c744e 100644 Binary files a/panda/board/obj/bootstub.panda_h7_remote_can_ignition_only.bin and b/panda/board/obj/bootstub.panda_h7_remote_can_ignition_only.bin differ diff --git a/panda/board/obj/bootstub.panda_h7_tesla_wake.bin b/panda/board/obj/bootstub.panda_h7_tesla_wake.bin index e23b3ee821..8ba72c744e 100644 Binary files a/panda/board/obj/bootstub.panda_h7_tesla_wake.bin and b/panda/board/obj/bootstub.panda_h7_tesla_wake.bin differ diff --git a/panda/board/obj/bootstub.panda_h7_tesla_wake_can_ignition_only.bin b/panda/board/obj/bootstub.panda_h7_tesla_wake_can_ignition_only.bin index e23b3ee821..8ba72c744e 100644 Binary files a/panda/board/obj/bootstub.panda_h7_tesla_wake_can_ignition_only.bin and b/panda/board/obj/bootstub.panda_h7_tesla_wake_can_ignition_only.bin differ diff --git a/panda/board/obj/bootstub.panda_hkg_remote.bin b/panda/board/obj/bootstub.panda_hkg_remote.bin index acec1613d8..1bc7802ce2 100644 Binary files a/panda/board/obj/bootstub.panda_hkg_remote.bin and b/panda/board/obj/bootstub.panda_hkg_remote.bin differ diff --git a/panda/board/obj/bootstub.panda_hkg_remote_can_ignition_only.bin b/panda/board/obj/bootstub.panda_hkg_remote_can_ignition_only.bin index acec1613d8..1bc7802ce2 100644 Binary files a/panda/board/obj/bootstub.panda_hkg_remote_can_ignition_only.bin and b/panda/board/obj/bootstub.panda_hkg_remote_can_ignition_only.bin differ diff --git a/panda/board/obj/bootstub.panda_remote.bin b/panda/board/obj/bootstub.panda_remote.bin index acec1613d8..1bc7802ce2 100644 Binary files a/panda/board/obj/bootstub.panda_remote.bin and b/panda/board/obj/bootstub.panda_remote.bin differ diff --git a/panda/board/obj/bootstub.panda_remote_can_ignition_only.bin b/panda/board/obj/bootstub.panda_remote_can_ignition_only.bin index acec1613d8..1bc7802ce2 100644 Binary files a/panda/board/obj/bootstub.panda_remote_can_ignition_only.bin and b/panda/board/obj/bootstub.panda_remote_can_ignition_only.bin differ diff --git a/panda/board/obj/bootstub.panda_tesla_wake.bin b/panda/board/obj/bootstub.panda_tesla_wake.bin index acec1613d8..1bc7802ce2 100644 Binary files a/panda/board/obj/bootstub.panda_tesla_wake.bin and b/panda/board/obj/bootstub.panda_tesla_wake.bin differ diff --git a/panda/board/obj/bootstub.panda_tesla_wake_can_ignition_only.bin b/panda/board/obj/bootstub.panda_tesla_wake_can_ignition_only.bin index acec1613d8..1bc7802ce2 100644 Binary files a/panda/board/obj/bootstub.panda_tesla_wake_can_ignition_only.bin and b/panda/board/obj/bootstub.panda_tesla_wake_can_ignition_only.bin differ diff --git a/panda/board/obj/gitversion.h b/panda/board/obj/gitversion.h index 94684d7f31..76f886e38e 100644 --- a/panda/board/obj/gitversion.h +++ b/panda/board/obj/gitversion.h @@ -1,4 +1,4 @@ #pragma once extern const uint8_t gitversion[29]; -const uint8_t gitversion[29] = "DEV-968161b3-Pf38f504f-DEBUG"; +const uint8_t gitversion[29] = "DEV-93e8d9be-Pf38f504f-DEBUG"; diff --git a/panda/board/obj/panda.bin.signed b/panda/board/obj/panda.bin.signed index 9556631b52..03e4e55e75 100644 Binary files a/panda/board/obj/panda.bin.signed and b/panda/board/obj/panda.bin.signed differ diff --git a/panda/board/obj/panda_can_ignition_only.bin.signed b/panda/board/obj/panda_can_ignition_only.bin.signed index ff21e41a94..4a76edd0b9 100644 Binary files a/panda/board/obj/panda_can_ignition_only.bin.signed and b/panda/board/obj/panda_can_ignition_only.bin.signed differ diff --git a/panda/board/obj/panda_h7.bin.signed b/panda/board/obj/panda_h7.bin.signed index 8d17d99d7d..e801a554af 100644 Binary files a/panda/board/obj/panda_h7.bin.signed and b/panda/board/obj/panda_h7.bin.signed differ diff --git a/panda/board/obj/panda_h7_can_ignition_only.bin.signed b/panda/board/obj/panda_h7_can_ignition_only.bin.signed index aaec2fb3a1..168200bd0a 100644 Binary files a/panda/board/obj/panda_h7_can_ignition_only.bin.signed and b/panda/board/obj/panda_h7_can_ignition_only.bin.signed differ diff --git a/panda/board/obj/panda_h7_hkg_remote.bin.signed b/panda/board/obj/panda_h7_hkg_remote.bin.signed index 2818c1f16d..ae809da868 100644 Binary files a/panda/board/obj/panda_h7_hkg_remote.bin.signed and b/panda/board/obj/panda_h7_hkg_remote.bin.signed differ diff --git a/panda/board/obj/panda_h7_hkg_remote_can_ignition_only.bin.signed b/panda/board/obj/panda_h7_hkg_remote_can_ignition_only.bin.signed index e1a4d0c5af..662bff94e8 100644 Binary files a/panda/board/obj/panda_h7_hkg_remote_can_ignition_only.bin.signed and b/panda/board/obj/panda_h7_hkg_remote_can_ignition_only.bin.signed differ diff --git a/panda/board/obj/panda_h7_remote.bin.signed b/panda/board/obj/panda_h7_remote.bin.signed index e825437ac9..b8188a4f32 100644 Binary files a/panda/board/obj/panda_h7_remote.bin.signed and b/panda/board/obj/panda_h7_remote.bin.signed differ diff --git a/panda/board/obj/panda_h7_remote_can_ignition_only.bin.signed b/panda/board/obj/panda_h7_remote_can_ignition_only.bin.signed index bbc8ad230d..09d0af2cf2 100644 Binary files a/panda/board/obj/panda_h7_remote_can_ignition_only.bin.signed and b/panda/board/obj/panda_h7_remote_can_ignition_only.bin.signed differ diff --git a/panda/board/obj/panda_h7_tesla_wake.bin.signed b/panda/board/obj/panda_h7_tesla_wake.bin.signed index 8e75382b6e..b133bacbe7 100644 Binary files a/panda/board/obj/panda_h7_tesla_wake.bin.signed and b/panda/board/obj/panda_h7_tesla_wake.bin.signed differ diff --git a/panda/board/obj/panda_h7_tesla_wake_can_ignition_only.bin.signed b/panda/board/obj/panda_h7_tesla_wake_can_ignition_only.bin.signed index f18ab471b9..7fffec4c22 100644 Binary files a/panda/board/obj/panda_h7_tesla_wake_can_ignition_only.bin.signed and b/panda/board/obj/panda_h7_tesla_wake_can_ignition_only.bin.signed differ diff --git a/panda/board/obj/panda_hkg_remote.bin.signed b/panda/board/obj/panda_hkg_remote.bin.signed index 56c0d7f7e4..9e15dbd852 100644 Binary files a/panda/board/obj/panda_hkg_remote.bin.signed and b/panda/board/obj/panda_hkg_remote.bin.signed differ diff --git a/panda/board/obj/panda_hkg_remote_can_ignition_only.bin.signed b/panda/board/obj/panda_hkg_remote_can_ignition_only.bin.signed index e09a4f07b1..2c6f86e133 100644 Binary files a/panda/board/obj/panda_hkg_remote_can_ignition_only.bin.signed and b/panda/board/obj/panda_hkg_remote_can_ignition_only.bin.signed differ diff --git a/panda/board/obj/panda_remote.bin.signed b/panda/board/obj/panda_remote.bin.signed index ae204e2e76..0b62080029 100644 Binary files a/panda/board/obj/panda_remote.bin.signed and b/panda/board/obj/panda_remote.bin.signed differ diff --git a/panda/board/obj/panda_remote_can_ignition_only.bin.signed b/panda/board/obj/panda_remote_can_ignition_only.bin.signed index 1c00d47219..839a67f381 100644 Binary files a/panda/board/obj/panda_remote_can_ignition_only.bin.signed and b/panda/board/obj/panda_remote_can_ignition_only.bin.signed differ diff --git a/panda/board/obj/panda_tesla_wake.bin.signed b/panda/board/obj/panda_tesla_wake.bin.signed index 43c87c3673..528dc4bf5d 100644 Binary files a/panda/board/obj/panda_tesla_wake.bin.signed and b/panda/board/obj/panda_tesla_wake.bin.signed differ diff --git a/panda/board/obj/panda_tesla_wake_can_ignition_only.bin.signed b/panda/board/obj/panda_tesla_wake_can_ignition_only.bin.signed index 4b4ad62832..1ec87cafb7 100644 Binary files a/panda/board/obj/panda_tesla_wake_can_ignition_only.bin.signed and b/panda/board/obj/panda_tesla_wake_can_ignition_only.bin.signed differ diff --git a/panda/board/obj/version b/panda/board/obj/version index 60dd48347f..79d1464ab8 100644 --- a/panda/board/obj/version +++ b/panda/board/obj/version @@ -1 +1 @@ -DEV-19d9f323-Pf38f504f-DEBUG \ No newline at end of file +DEV-93e8d9be-Pf38f504f-DEBUG \ No newline at end of file diff --git a/panda/tests/libpanda/libpanda.so b/panda/tests/libpanda/libpanda.so index 4cf97962a4..01c5d29446 100755 Binary files a/panda/tests/libpanda/libpanda.so and b/panda/tests/libpanda/libpanda.so differ diff --git a/prebuilt.json b/prebuilt.json index 296fef6f78..6abca8ec7a 100644 --- a/prebuilt.json +++ b/prebuilt.json @@ -1 +1 @@ -{"protected_inventory_sha256": "91597d85f715a8447ecc79f9fddca1590791c3e25278a9816c850bc2362a83db", "schema_version": 1} +{"protected_inventory_sha256": "fcfca2f5a163409f010d2f0ae2111487ec19f8856cb8036b8f75265758b3b9e1", "schema_version": 1}