Restore Kona factory steering while StarPilot is idle

This commit is contained in:
firestar5683
2026-11-23 15:20:16 -06:00
parent d24bfcd0f3
commit ff9f0f0b33
46 changed files with 178 additions and 19 deletions
@@ -117,7 +117,8 @@ CARNIVAL_LONG_AOL_WORD = 0x28B4
LONG_AOL_WORDS = frozenset((CARNIVAL_LONG_AOL_WORD, 0x080D, 0x0815, 0x0895, 0x2815, 0x2895)) | SPORTAGE_LONG_AOL_WORDS
CCNC_LONG_AOL_WORDS = frozenset((0x0C0C, 0x0C0E, 0x0C2C, 0x0C2E))
LONG_EV_CARS = frozenset((CAR.HYUNDAI_IONIQ_5, CAR.HYUNDAI_KONA_EV_2ND_GEN))
LONG_AOL_WORDS |= CCNC_LONG_AOL_WORDS
KONA_IDLE_AOL_WORD = 0x1C0C
LONG_AOL_WORDS |= CCNC_LONG_AOL_WORDS | {KONA_IDLE_AOL_WORD}
GENERIC_HDA2_CARS = frozenset((
CAR.GENESIS_G80_2ND_GEN_FL,
CAR.HYUNDAI_IONIQ_5_N,
@@ -149,6 +150,12 @@ INDEPENDENT_LONG_BUTTON_CARS = LONG_EV_CARS | GENERIC_HDA2_CARS | {
}
def kona_idle_handoff_profile(cp):
return (cp.carFingerprint == CAR.HYUNDAI_KONA_2ND_GEN and len(cp.safetyConfigs) == 1 and
cp.alphaLongitudinalAvailable and cp.openpilotLongitudinalControl and not cp.pcmCruise and
_base_word(cp, experiences=(0, 32)) == 0x408)
def qualified_long(cp, *, marked_only=False):
if cp.carFingerprint == CAR.KIA_EV6 and int(cp.flags) & _FLAG_CANFD_LKA_STEER_MSG:
from opendbc.car.hyundai.ev6_aol import qualified as ev6_qualified
@@ -189,6 +196,8 @@ def qualified_long(cp, *, marked_only=False):
if not raw & 0x2000:
return False
raw &= ~0x2000
if raw == KONA_IDLE_AOL_WORD:
return cp.alternativeExperience == 32 and kona_idle_handoff_profile(cp)
return (raw == (word | 0x804) and cp.alternativeExperience == 32) or (
not marked_only and raw == (word | 4) and cp.alternativeExperience == 0
)
@@ -31,6 +31,7 @@ from opendbc.car.hyundai.canfd_lfa_status import preserves_stock_lfa
from opendbc.car.hyundai.classic_scc_aol import qualified as qualified_classic_stock_aol
from opendbc.car.hyundai.classic_long_aol import AOL_EXPERIENCE, qualified as qualified_classic_long_aol
from opendbc.car.hyundai.canfd_stock_aol import qualified as qualified_canfd_stock_aol, qualified_long as qualified_canfd_long_aol
from opendbc.car.hyundai.canfd_stock_aol import KONA_IDLE_AOL_WORD
from opendbc.car.hyundai.g90_longitudinal import G90LongitudinalPolicy
from opendbc.car.hyundai.gv70_longitudinal import is_gv70, scc_request, suppress_stock_cancel
from opendbc.car.hyundai.g90_lead import G90LeadState, eligible as g90_lead_eligible
@@ -174,6 +175,7 @@ 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
@@ -569,14 +571,26 @@ class CarController(CarControllerBase):
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)
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)))
if _projection is not None:
_projection.emit('hyundai.CANFD_TORQUE', apply_torque, apply_steer_req, can_sends[-1])
kona_handoff = (self.CP.carFingerprint == CAR.HYUNDAI_KONA_2ND_GEN and
self.CP.safetyConfigs[-1].safetyParam == KONA_IDLE_AOL_WORD)
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))
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 (
@@ -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):
stock_lkas_damping=None, stock_lfa_owned_status=False, lfa_counter=None):
values = {
"LKA_OptUsmSta": 2,
"LKA_SysIndReq": 2 if enabled else 1,
@@ -73,6 +73,8 @@ 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:
@@ -37,9 +37,10 @@ def test_factory_ccnc_alpha_admits_only_matching_owner(car, alternate, offset):
assert qualified_long(cp) and independent_cruise_buttons_supported(cp)
policy = policy_for(cp)
assert policy.intent_supported and policy.runtime_supported and policy.explicit_latch
assert policy.safety_param_addition == 0x800 and policy.alternative_experience_addition == 32
expected = 0x1800 if car == CARS[0] and not alternate and offset == 0 else 0x800
assert policy.safety_param_addition == expected and policy.alternative_experience_addition == 32
assert cp.to_dict() == original
word = int(cp.safetyConfigs[-1].safetyParam) | 0x800
word = int(cp.safetyConfigs[-1].safetyParam) | expected
cp.safetyConfigs[-1].safetyParam = word
cp.alternativeExperience = 32
assert qualified_long(cp, marked_only=True)
@@ -108,7 +109,8 @@ def test_actual_card_marker_and_settings_preserve_startup_contract(car, alternat
try:
with structs.CarParams.from_bytes(saved.get('CarParams')) as published:
assert published.openpilotLongitudinalControl and not published.pcmCruise
assert published.safetyConfigs[-1].safetyParam == (base_word | (0x800 if enabled else 0))
addition = 0x1800 if car == CARS[0] and not alternate and offset == 0 else 0x800
assert published.safetyConfigs[-1].safetyParam == (base_word | (addition if enabled else 0))
assert published.alternativeExperience == (32 if enabled else 0)
assert qualified_long(published, marked_only=enabled)
assert CarInterface.startup_required(published) == required
@@ -199,3 +201,29 @@ def test_wheel_choices_keep_aol_with_either_longitudinal_owner(car, alternate, a
feature = WheelFeature(Owner())
assert 'Toggle AOL' in feature.choices('LKASButtonControl')
assert 'Toggle AOL' in feature.choices('MainCruiseButtonControl')
def test_idle_handoff_omits_only_kona_steering_packet():
reference_cp = kona_params(CARS[0])
handoff_cp = kona_params(CARS[0])
for cp, word in ((reference_cp, 0x0C0C), (handoff_cp, 0x1C0C)):
cp.safetyConfigs[0].safetyParam = word
cp.alternativeExperience = 32
reference, candidate = CarInterface(reference_cp), CarInterface(handoff_cp)
source = Mock()
source.owns_display_status.return_value = False
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):
if camera_counter is None:
source.current.return_value = None
else:
source.current.return_value = bytes([0, 0, camera_counter] + [0] * 13)
ctrl = structs.CarControl(enabled=enabled, latActive=lateral).as_reader()
_, before = reference.apply(ctrl, 1_000_000_000)
_, 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])
@@ -1228,7 +1228,78 @@ static bool hyundai_canfd_tx_hook_valid(const CANPacket_t *msg) {
return tx;
}
#ifdef ALLOW_DEBUG
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_host_steering(void) {
return controls_allowed || ((aol_get_permission_mask() & 1U) != 0U);
}
static bool hyundai_kona_lfa_valid(const CANPacket_t *msg) {
return msg->fd && !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_fwd(const CANPacket_t *msg) {
bool blocked = false;
if (hyundai_kona_idle_handoff && (msg->bus == 2U) && (msg->addr == 0x12AU)) {
const uint32_t now = microsecond_timer_get();
const bool valid = hyundai_kona_lfa_valid(msg);
if (valid) {
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;
}
}
blocked = !valid || safety_rx_checks_invalid || !aol_rx_healthy() || hyundai_kona_host_steering() ||
(msg->data[2] != hyundai_kona_camera_counter) ||
!hyundai_kona_output_ready(now, false, msg->data[2]);
if (!blocked) {
hyundai_kona_track_output(now, false, msg->data[2]);
}
}
return blocked;
}
#endif
static bool hyundai_canfd_tx_hook(const CANPacket_t *msg) {
#ifdef ALLOW_DEBUG
if (hyundai_kona_idle_handoff && (msg->addr == 0x12AU)) {
const uint32_t now = microsecond_timer_get();
if (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) > 50000U) ||
(msg->data[2] != hyundai_kona_camera_counter) || !hyundai_kona_output_ready(now, true, msg->data[2])) {
return false;
}
}
#endif
bool tx = false;
const bool ordinary_angle_frame = hyundai_ordinary_angle_stock && ((msg->addr == 0x110U) || (msg->addr == 0x362U));
bool ordinary_angle_valid = true;
@@ -1288,6 +1359,11 @@ static bool hyundai_canfd_tx_hook(const CANPacket_t *msg) {
if (hyundai_angle_aol_enabled && independent_command && !tx) {
hyundai_ordinary_angle_release();
}
#ifdef ALLOW_DEBUG
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;
}
@@ -2243,10 +2319,22 @@ static safety_config hyundai_canfd_init(uint16_t param) {
hyundai_canfd_reset_ioniq6_state();
hyundai_ordinary_angle_reset();
#ifdef ALLOW_DEBUG
hyundai_kona_idle_handoff = (param == 0x1C0CU) && (alternative_experience == 32);
hyundai_kona_camera_seen = false;
hyundai_kona_output_seen = false;
if ((param == 0x1C0CU) && !hyundai_kona_idle_handoff) {
return (safety_config){.disable_forwarding = true};
}
if (hyundai_kona_idle_handoff) {
param = 0x0C0CU;
}
const bool valid = !GET_FLAG(param, 32768U) || (param == 0x8015U) || (param == 0x8095U) ||
(param == 0x8815U) || (param == 0x8895U) ||
hyundai_canfd_gv60_long_param(param);
#else
if (param == 0x1C0CU) {
return (safety_config){.disable_forwarding = true};
}
const bool valid = !GET_FLAG(param, 32768U);
#endif
safety_config ret = {0};
@@ -2261,6 +2349,20 @@ static safety_config hyundai_canfd_init(uint16_t 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);
#ifdef ALLOW_DEBUG
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 == 0x12AU) {
kona_tx[i].disable_static_blocking = true;
}
}
ret.tx_msgs = kona_tx;
}
}
#endif
}
return ret;
}
@@ -2271,6 +2373,9 @@ const safety_hooks hyundai_canfd_hooks = {
.optional_rx = hyundai_canfd_optional_rx_hook,
.tx = hyundai_canfd_tx_hook,
.fwd = hyundai_canfd_fwd_hook,
#ifdef ALLOW_DEBUG
.fwd_packet = hyundai_kona_camera_fwd,
#endif
.get_counter = hyundai_canfd_get_counter,
.get_checksum = hyundai_canfd_get_checksum,
.compute_checksum = hyundai_common_canfd_compute_checksum,
@@ -25,7 +25,7 @@ static inline bool hyundai_canfd_torque_long_aol_param(uint16_t param) {
((base & 4U) != 0U) && ((fuel == 0U) || (fuel == 2U)) &&
((topology == 0U) || (topology == 8U) || (topology == 16U) ||
(topology == 144U) || (topology == 32U) || (topology == 40U));
return (param == 0x0C0CU) || (param == 0x0C0EU) || (param == 0x0C2CU) || (param == 0x0C2EU) ||
return (param == 0x1C0CU) || (param == 0x0C0CU) || (param == 0x0C0EU) || (param == 0x0C2CU) || (param == 0x0C2EU) ||
(param == 0x28B4U) || sportage || (param == 0x080DU) || (param == 0x0815U) || (param == 0x0895U) || (param == 0x2815U) || (param == 0x2895U);
}
#endif
Binary file not shown.
@@ -511,7 +511,7 @@ int main() {
constexpr uint16_t sportage_long_words[] = {0x0804U, 0x0806U, 0x080CU, 0x080EU, 0x0814U, 0x0816U,
0x0894U, 0x0896U, 0x0824U, 0x0826U, 0x082CU, 0x082EU};
for (uint32_t word = 0U; word <= 0xFFFFU; ++word) {
bool expected = word == 0x0C0CU || word == 0x0C0EU || word == 0x0C2CU || word == 0x0C2EU || word == 0x28B4U || word == 0x080DU || word == 0x2815U || word == 0x2895U || word == 0x0815U || word == 0x0895U || word == 0x8815U || word == 0x8895U || word == 0x5491U || word == 0x5C91U || word == 0x5C95U || word == 0x5495U;
bool expected = word == 0x1C0CU || word == 0x0C0CU || word == 0x0C0EU || word == 0x0C2CU || word == 0x0C2EU || word == 0x28B4U || word == 0x080DU || word == 0x2815U || word == 0x2895U || word == 0x0815U || word == 0x0895U || word == 0x8815U || word == 0x8895U || word == 0x5491U || word == 0x5C91U || word == 0x5C95U || word == 0x5495U;
for (uint16_t stock_word : stock_canfd_words) {
expected = expected || word == stock_word;
}
+3 -2
View File
@@ -25,7 +25,7 @@ from opendbc.car.hyundai.classic_scc_aol import (
from opendbc.car.hyundai.canfd_owner import config_index
from opendbc.car.hyundai.canfd_stock_aol import (
qualified as qualified_canfd_stock, qualified_long as qualified_canfd_long,
LONG_AOL_WORDS as CANFD_LONG_AOL_WORDS, STOCK_AOL_MARKER, STOCK_AOL_WORDS,
LONG_AOL_WORDS as CANFD_LONG_AOL_WORDS, STOCK_AOL_MARKER, STOCK_AOL_WORDS, kona_idle_handoff_profile,
)
from opendbc.car.hyundai.non_scc_aol import qualified as qualified_non_scc, aol_word, AOL_MARKER, AOL_EXPERIENCE, AOL_WORDS, SOFTWARE_CRUISE
from opendbc.car.hyundai.kona_aol import allow_lateral_onset as kona_lateral_onset
@@ -153,7 +153,8 @@ def _policy_for(CP) -> AolVehiclePolicy:
)
if qualified_canfd_long(CP):
return AolVehiclePolicy(intent_supported=True, settings_supported=True, runtime_supported=True,
normal_runtime_supported=True, explicit_latch=True, safety_param_addition=0x0800,
normal_runtime_supported=True, explicit_latch=True,
safety_param_addition=0x1800 if kona_idle_handoff_profile(CP) else 0x0800,
alternative_experience_addition=32)
if qualified_canfd_stock(CP):
return AolVehiclePolicy(
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+1 -1
View File
@@ -1 +1 @@
DEV-6ccf23dd-Pf38f504f-DEBUG
DEV-549f62bb-Pf38f504f-DEBUG
Binary file not shown.
+1 -1
View File
@@ -1 +1 @@
{"protected_inventory_sha256": "368a283e1d9307f768816fe7e1cc5c3c06861d9aac3841e3e67268c4b2a09b83", "schema_version": 1}
{"protected_inventory_sha256": "dd17fb0e6752bbb51904bb27251c34b9cdd4e0199ef30366f19f26ed44a7efbf", "schema_version": 1}