Skip to content

Commit

Permalink
Support cruise control button tx/rx for ALT_BUTTONS.
Browse files Browse the repository at this point in the history
This is needed for the Kia Caravan '25.
  • Loading branch information
ccdunder committed Dec 17, 2024
1 parent 0401f64 commit a09c1cc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
17 changes: 13 additions & 4 deletions board/safety/safety_hyundai_canfd.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,16 @@ static bool hyundai_canfd_tx_hook(const CANPacket_t *to_send) {
}

// cruise buttons check
if (addr == 0x1cf) {
int button = GET_BYTE(to_send, 2) & 0x7U;
bool is_cancel = (button == HYUNDAI_BTN_CANCEL);
bool is_resume = (button == HYUNDAI_BTN_RESUME);
const int button_addr = hyundai_canfd_alt_buttons ? 0x1aa : 0x1cf;
if (addr == button_addr) {
int cruise_button = 0;
if (addr == 0x1cf) {
cruise_button = GET_BYTE(to_send, 2) & 0x7U;
} else {
cruise_button = (GET_BYTE(to_send, 4) >> 4) & 0x7U;
}
bool is_cancel = (cruise_button == HYUNDAI_BTN_CANCEL);
bool is_resume = (cruise_button == HYUNDAI_BTN_RESUME);

bool allowed = (is_cancel && cruise_engaged_prev) || (is_resume && controls_allowed);
if (!allowed) {
Expand Down Expand Up @@ -231,12 +237,14 @@ static safety_config hyundai_canfd_init(uint16_t param) {
static const CanMsg HYUNDAI_CANFD_HDA2_TX_MSGS[] = {
{0x50, 0, 16}, // LKAS
{0x1CF, 1, 8}, // CRUISE_BUTTON
{0x1AA, 1, 16}, // CRUISE_BUTTONS_ALT
{0x2A4, 0, 24}, // CAM_0x2A4
};

static const CanMsg HYUNDAI_CANFD_HDA2_ALT_STEERING_TX_MSGS[] = {
{0x110, 0, 32}, // LKAS_ALT
{0x1CF, 1, 8}, // CRUISE_BUTTON
{0x1AA, 1, 16}, // CRUISE_BUTTONS_ALT
// Needed for cruise control in case of ALT_BUTTONS.
{0x1A0, 1, 32}, // CRUISE_INFO
{0x362, 0, 32}, // CAM_0x362
Expand All @@ -262,6 +270,7 @@ static safety_config hyundai_canfd_init(uint16_t param) {
{0x12A, 0, 16}, // LFA
{0x1A0, 0, 32}, // CRUISE_INFO
{0x1CF, 2, 8}, // CRUISE_BUTTON
{0x1AA, 2, 16}, // CRUISE_BUTTONS_ALT
{0x1E0, 0, 16}, // LFAHDA_CLUSTER
};

Expand Down
26 changes: 10 additions & 16 deletions tests/safety/test_hyundai_canfd.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class TestHyundaiCanfdBase(HyundaiButtonBase, common.PandaCarSafetyTest, common.DriverTorqueSteeringSafetyTest, common.SteerRequestCutSafetyTest):
SAFETY_PARAM = 0
TX_MSGS = [[0x50, 0], [0x1CF, 1], [0x2A4, 0]]
TX_MSGS = [[0x50, 0], [0x1CF, 1], [0x1AA, 1], [0x2A4, 0]]
STANDSTILL_THRESHOLD = 12 # 0.375 kph
FWD_BLACKLISTED_ADDRS = {2: [0x50, 0x2a4]}
FWD_BUS_LOOKUP = {0: 2, 2: 0}
Expand Down Expand Up @@ -91,26 +91,20 @@ def setUpClass(cls):
super().setUpClass()
cls.SAFETY_PARAM |= Panda.FLAG_HYUNDAI_CANFD_ALT_BUTTONS

def _button_msg(self, buttons, main_button=0, bus=1):
def _button_msg(self, buttons, main_button=0, bus=None):
if bus is None:
bus = self.PT_BUS
values = {
"CRUISE_BUTTONS": buttons,
"ADAPTIVE_CRUISE_MAIN_BTN": main_button,
}
return self.packer.make_can_msg_panda("CRUISE_BUTTONS_ALT", self.PT_BUS, values)

def test_button_sends(self):
"""
No button send allowed with alt buttons.
"""
for enabled in (True, False):
for btn in range(8):
self.safety.set_controls_allowed(enabled)
self.assertFalse(self._tx(self._button_msg(btn)))
msg = self.packer.make_can_msg_panda("CRUISE_BUTTONS_ALT", bus, values)
return msg


class TestHyundaiCanfdHDA1Base(TestHyundaiCanfdBase):

TX_MSGS = [[0x12A, 0], [0x1A0, 1], [0x1CF, 0], [0x1E0, 0]]
TX_MSGS = [[0x12A, 0], [0x1A0, 1], [0x1CF, 2], [0x1AA, 2], [0x1E0, 0]]
RELAY_MALFUNCTION_ADDRS = {0: (0x12A,)} # LFA
FWD_BLACKLISTED_ADDRS = {2: [0x12A, 0x1E0]}
FWD_BUS_LOOKUP = {0: 2, 2: 0}
Expand Down Expand Up @@ -167,15 +161,15 @@ class TestHyundaiCanfdHDA2EVBase(TestHyundaiCanfdBase):


class TestHyundaiCanfdHDA2EV(TestHyundaiCanfdHDA2EVBase):
TX_MSGS = [[0x50, 0], [0x1CF, 1], [0x2A4, 0]]
TX_MSGS = [[0x50, 0], [0x1CF, 1], [0x1AA, 1], [0x2A4, 0]]
RELAY_MALFUNCTION_ADDRS = {0: (0x50,)}
FWD_BLACKLISTED_ADDRS = {2: [0x50, 0x2a4]}
STEER_MSG = "LKAS"
SAFETY_PARAM = Panda.FLAG_HYUNDAI_CANFD_HDA2 | Panda.FLAG_HYUNDAI_EV_GAS


class TestHyundaiCanfdHDA2EVAltSteering(TestHyundaiCanfdHDA2EVBase):
TX_MSGS = [[0x110, 0], [0x1CF, 1], [0x362, 0]]
TX_MSGS = [[0x110, 0], [0x1CF, 1], [0x1AA, 1], [0x362, 0]]
RELAY_MALFUNCTION_ADDRS = {0: (0x110,)}
FWD_BLACKLISTED_ADDRS = {2: [0x110, 0x362]}
STEER_MSG = "LKAS_ALT"
Expand All @@ -194,7 +188,7 @@ class TestHyundaiCanfdHDA2EVAltSteeringAltButtons(HyundaiCanfdAltButtonsMixin, T

class TestHyundaiCanfdHDA2LongEV(HyundaiLongitudinalBase, TestHyundaiCanfdHDA2EV):

TX_MSGS = [[0x50, 0], [0x1CF, 1], [0x2A4, 0], [0x51, 0], [0x730, 1], [0x12a, 1], [0x160, 1],
TX_MSGS = [[0x50, 0], [0x1CF, 1], [0x1AA, 1], [0x2A4, 0], [0x51, 0], [0x730, 1], [0x12a, 1], [0x160, 1],
[0x1e0, 1], [0x1a0, 1], [0x1ea, 1], [0x200, 1], [0x345, 1], [0x1da, 1]]

RELAY_MALFUNCTION_ADDRS = {0: (0x50,), 1: (0x1a0,)} # LKAS, SCC_CONTROL
Expand Down

0 comments on commit a09c1cc

Please sign in to comment.