From 61754d7732107c636e46dc9409358d1d37d0fd10 Mon Sep 17 00:00:00 2001 From: Jason Wen Date: Tue, 24 Sep 2024 16:48:30 -0400 Subject: [PATCH 1/3] HKG: Car Port for Hyundai Palisade and Kia Telluride 2023-24 (non-HDA2) --- board/safety.h | 2 -- board/safety/safety_hyundai.h | 60 +++++++++++++++++++++++++---------- board/safety_declarations.h | 2 -- python/__init__.py | 1 + 4 files changed, 44 insertions(+), 21 deletions(-) diff --git a/board/safety.h b/board/safety.h index 913109957c..00693c10d5 100644 --- a/board/safety.h +++ b/board/safety.h @@ -269,7 +269,6 @@ void gen_crc_lookup_table_8(uint8_t poly, uint8_t crc_lut[]) { } } -#ifdef CANFD void gen_crc_lookup_table_16(uint16_t poly, uint16_t crc_lut[]) { for (uint16_t i = 0; i < 256U; i++) { uint16_t crc = i << 8U; @@ -283,7 +282,6 @@ void gen_crc_lookup_table_16(uint16_t poly, uint16_t crc_lut[]) { crc_lut[i] = crc; } } -#endif // 1Hz safety function called by main. Now just a check for lagging safety messages void safety_tick(const safety_config *cfg) { diff --git a/board/safety/safety_hyundai.h b/board/safety/safety_hyundai.h index 3e9d23216c..4d6f258efe 100644 --- a/board/safety/safety_hyundai.h +++ b/board/safety/safety_hyundai.h @@ -3,13 +3,13 @@ #include "safety_declarations.h" #include "safety_hyundai_common.h" -#define HYUNDAI_LIMITS(steer, rate_up, rate_down) { \ +#define HYUNDAI_LIMITS(steer, rate_up, rate_down, drv_trq_allowance) { \ .max_steer = (steer), \ .max_rate_up = (rate_up), \ .max_rate_down = (rate_down), \ .max_rt_delta = 112, \ .max_rt_interval = 250000, \ - .driver_torque_allowance = 50, \ + .driver_torque_allowance = (drv_trq_allowance), \ .driver_torque_factor = 2, \ .type = TorqueDriverLimited, \ /* the EPS faults when the steering angle is above a certain threshold for too long. to prevent this, */ \ @@ -38,10 +38,11 @@ static const CanMsg HYUNDAI_TX_MSGS[] = { {.msg = {{0x386, 0, 8, .check_checksum = !(legacy), .max_counter = (legacy) ? 0U : 15U, .frequency = 100U}, { 0 }, { 0 }}}, \ {.msg = {{0x394, 0, 8, .check_checksum = !(legacy), .max_counter = (legacy) ? 0U : 7U, .frequency = 100U}, { 0 }, { 0 }}}, \ -#define HYUNDAI_SCC12_ADDR_CHECK(scc_bus) \ - {.msg = {{0x421, (scc_bus), 8, .check_checksum = true, .max_counter = 15U, .frequency = 50U}, { 0 }, { 0 }}}, \ +#define HYUNDAI_SCC12_ADDR_CHECK(scc_bus, can_canfd_hybrid) \ + {.msg = {{0x421, (scc_bus), 8, .check_checksum = !(can_canfd_hybrid), .max_counter = 15U, .frequency = 50U}, { 0 }, { 0 }}}, \ static bool hyundai_legacy = false; +static bool hyundai_can_canfd_hybrid = false; static uint8_t hyundai_get_counter(const CANPacket_t *to_push) { int addr = GET_ADDR(to_push); @@ -54,7 +55,8 @@ static uint8_t hyundai_get_counter(const CANPacket_t *to_push) { } else if (addr == 0x394) { cnt = (GET_BYTE(to_push, 1) >> 5) & 0x7U; } else if (addr == 0x421) { - cnt = GET_BYTE(to_push, 7) & 0xFU; + uint8_t byte_421 = hyundai_can_canfd_hybrid ? (GET_BYTE(to_push, 1) >> 4) : GET_BYTE(to_push, 7); + cnt = byte_421 & 0xFU; } else if (addr == 0x4F1) { cnt = (GET_BYTE(to_push, 3) >> 4) & 0xFU; } else { @@ -73,7 +75,7 @@ static uint32_t hyundai_get_checksum(const CANPacket_t *to_push) { } else if (addr == 0x394) { chksum = GET_BYTE(to_push, 6) & 0xFU; } else if (addr == 0x421) { - chksum = GET_BYTE(to_push, 7) >> 4; + chksum = hyundai_can_canfd_hybrid ? GET_BYTE(to_push, 0) : GET_BYTE(to_push, 7) >> 4; } else { } return chksum; @@ -82,7 +84,7 @@ static uint32_t hyundai_get_checksum(const CANPacket_t *to_push) { static uint32_t hyundai_compute_checksum(const CANPacket_t *to_push) { int addr = GET_ADDR(to_push); - uint8_t chksum = 0; + uint16_t chksum = 0; if (addr == 0x386) { // count the bits for (int i = 0; i < 8; i++) { @@ -97,6 +99,8 @@ static uint32_t hyundai_compute_checksum(const CANPacket_t *to_push) { } } chksum = (chksum ^ 9U) & 15U; + } else if ((addr == 0x421) && hyundai_can_canfd_hybrid) { + chksum = hyundai_common_canfd_compute_checksum(to_push); } else { // sum of nibbles for (int i = 0; i < 8; i++) { @@ -109,7 +113,7 @@ static uint32_t hyundai_compute_checksum(const CANPacket_t *to_push) { } chksum += (b % 16U) + (b / 16U); } - chksum = (16U - (chksum % 16U)) % 16U; + chksum = (16U - (chksum % 16U)) % 16U; } return chksum; @@ -121,8 +125,8 @@ static void hyundai_rx_hook(const CANPacket_t *to_push) { // SCC12 is on bus 2 for camera-based SCC cars, bus 0 on all others if ((addr == 0x421) && (((bus == 0) && !hyundai_camera_scc) || ((bus == 2) && hyundai_camera_scc))) { - // 2 bits: 13-14 - int cruise_engaged = (GET_BYTES(to_push, 0, 4) >> 13) & 0x3U; + uint8_t cruise_byte = hyundai_can_canfd_hybrid ? (GET_BYTE(to_push, 3) >> 4) : (GET_BYTES(to_push, 0, 4) >> 13); + bool cruise_engaged = (cruise_byte & 0x3U) != 0U; hyundai_common_cruise_state_check(cruise_engaged); } @@ -173,8 +177,9 @@ static void hyundai_rx_hook(const CANPacket_t *to_push) { } static bool hyundai_tx_hook(const CANPacket_t *to_send) { - const SteeringLimits HYUNDAI_STEERING_LIMITS = HYUNDAI_LIMITS(384, 3, 7); - const SteeringLimits HYUNDAI_STEERING_LIMITS_ALT = HYUNDAI_LIMITS(270, 2, 3); + const SteeringLimits HYUNDAI_STEERING_LIMITS = HYUNDAI_LIMITS(384, 3, 7, 50); + const SteeringLimits HYUNDAI_STEERING_LIMITS_ALT = HYUNDAI_LIMITS(270, 2, 3, 50); + const SteeringLimits HYUNDAI_STEERING_LIMITS_CAN_CANFD_HYBRID = HYUNDAI_LIMITS(384, 2, 3, 250); bool tx = true; int addr = GET_ADDR(to_send); @@ -215,7 +220,7 @@ static bool hyundai_tx_hook(const CANPacket_t *to_send) { int desired_torque = ((GET_BYTES(to_send, 0, 4) >> 16) & 0x7ffU) - 1024U; bool steer_req = GET_BIT(to_send, 27U); - const SteeringLimits limits = hyundai_alt_limits ? HYUNDAI_STEERING_LIMITS_ALT : HYUNDAI_STEERING_LIMITS; + const SteeringLimits limits = hyundai_can_canfd_hybrid ? HYUNDAI_STEERING_LIMITS_CAN_CANFD_HYBRID : (hyundai_alt_limits ? HYUNDAI_STEERING_LIMITS_ALT : HYUNDAI_STEERING_LIMITS); if (steer_torque_cmd_checks(desired_torque, steer_req, limits)) { tx = false; } @@ -258,6 +263,8 @@ static int hyundai_fwd_hook(int bus_num, int addr) { } static safety_config hyundai_init(uint16_t param) { + const int HYUNDAI_PARAM_CAN_CANFD_HYBRID = 256; + static const CanMsg HYUNDAI_LONG_TX_MSGS[] = { {0x340, 0, 8}, // LKAS11 Bus 0 {0x4F1, 0, 4}, // CLU11 Bus 0 @@ -278,10 +285,21 @@ static safety_config hyundai_init(uint16_t param) { {0x485, 0, 4}, // LFAHDA_MFC Bus 0 }; + static const CanMsg HYUNDAI_CAN_CANFD_HYBRID_TX_MSGS[] = { + {0x340, 0, 8}, + {0x4F1, 0, 4}, + {0x485, 0, 8}, + }; + hyundai_common_init(param); hyundai_legacy = false; + hyundai_can_canfd_hybrid = GET_FLAG(param, HYUNDAI_PARAM_CAN_CANFD_HYBRID); - if (hyundai_camera_scc) { + if (hyundai_can_canfd_hybrid) { + gen_crc_lookup_table_16(0x1021, hyundai_canfd_crc_lut); + } + + if (hyundai_camera_scc || hyundai_can_canfd_hybrid) { hyundai_longitudinal = false; } @@ -297,14 +315,21 @@ static safety_config hyundai_init(uint16_t param) { } else if (hyundai_camera_scc) { static RxCheck hyundai_cam_scc_rx_checks[] = { HYUNDAI_COMMON_RX_CHECKS(false) - HYUNDAI_SCC12_ADDR_CHECK(2) + HYUNDAI_SCC12_ADDR_CHECK(2, false) }; ret = BUILD_SAFETY_CFG(hyundai_cam_scc_rx_checks, HYUNDAI_CAMERA_SCC_TX_MSGS); + } else if (hyundai_can_canfd_hybrid) { + static RxCheck hyundai_can_canfd_hybrid_rx_checks[] = { + HYUNDAI_COMMON_RX_CHECKS(false) + HYUNDAI_SCC12_ADDR_CHECK(0, true) + }; + + ret = BUILD_SAFETY_CFG(hyundai_can_canfd_hybrid_rx_checks, HYUNDAI_CAN_CANFD_HYBRID_TX_MSGS); } else { static RxCheck hyundai_rx_checks[] = { HYUNDAI_COMMON_RX_CHECKS(false) - HYUNDAI_SCC12_ADDR_CHECK(0) + HYUNDAI_SCC12_ADDR_CHECK(0, false) }; ret = BUILD_SAFETY_CFG(hyundai_rx_checks, HYUNDAI_TX_MSGS); @@ -316,13 +341,14 @@ static safety_config hyundai_legacy_init(uint16_t param) { // older hyundai models have less checks due to missing counters and checksums static RxCheck hyundai_legacy_rx_checks[] = { HYUNDAI_COMMON_RX_CHECKS(true) - HYUNDAI_SCC12_ADDR_CHECK(0) + HYUNDAI_SCC12_ADDR_CHECK(0, false) }; hyundai_common_init(param); hyundai_legacy = true; hyundai_longitudinal = false; hyundai_camera_scc = false; + hyundai_can_canfd_hybrid = false; return BUILD_SAFETY_CFG(hyundai_legacy_rx_checks, HYUNDAI_TX_MSGS); } diff --git a/board/safety_declarations.h b/board/safety_declarations.h index b0e9588e95..21e52a1eed 100644 --- a/board/safety_declarations.h +++ b/board/safety_declarations.h @@ -176,9 +176,7 @@ void update_sample(struct sample_t *sample, int sample_new); bool get_longitudinal_allowed(void); int ROUND(float val); void gen_crc_lookup_table_8(uint8_t poly, uint8_t crc_lut[]); -#ifdef CANFD void gen_crc_lookup_table_16(uint16_t poly, uint16_t crc_lut[]); -#endif void generic_rx_checks(bool stock_ecu_detected); bool steer_torque_cmd_checks(int desired_torque, int steer_req, const SteeringLimits limits); bool steer_angle_cmd_checks(int desired_angle, bool steer_control_enabled, const SteeringLimits limits); diff --git a/python/__init__.py b/python/__init__.py index 0e531dfc75..bca41513a6 100644 --- a/python/__init__.py +++ b/python/__init__.py @@ -201,6 +201,7 @@ class Panda: FLAG_HYUNDAI_CANFD_ALT_BUTTONS = 32 FLAG_HYUNDAI_ALT_LIMITS = 64 FLAG_HYUNDAI_CANFD_HDA2_ALT_STEERING = 128 + FLAG_HYUNDAI_CAN_CANFD_HYBRID = 256 FLAG_TESLA_POWERTRAIN = 1 FLAG_TESLA_LONG_CONTROL = 2 From e82d95fe6ed9f8b185f862cfd65b389073532927 Mon Sep 17 00:00:00 2001 From: Jason Wen Date: Fri, 27 Sep 2024 16:20:14 -0400 Subject: [PATCH 2/3] HKG: Car Port for Hyundai Palisade 2023 and Kia Telluride 2023 (non-HDA2) --- board/safety.h | 2 ++ board/safety/safety_hyundai.h | 29 ++++++++++------------------ board/safety/safety_hyundai_common.h | 5 +++++ board/safety_declarations.h | 2 ++ 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/board/safety.h b/board/safety.h index 00693c10d5..913109957c 100644 --- a/board/safety.h +++ b/board/safety.h @@ -269,6 +269,7 @@ void gen_crc_lookup_table_8(uint8_t poly, uint8_t crc_lut[]) { } } +#ifdef CANFD void gen_crc_lookup_table_16(uint16_t poly, uint16_t crc_lut[]) { for (uint16_t i = 0; i < 256U; i++) { uint16_t crc = i << 8U; @@ -282,6 +283,7 @@ void gen_crc_lookup_table_16(uint16_t poly, uint16_t crc_lut[]) { crc_lut[i] = crc; } } +#endif // 1Hz safety function called by main. Now just a check for lagging safety messages void safety_tick(const safety_config *cfg) { diff --git a/board/safety/safety_hyundai.h b/board/safety/safety_hyundai.h index 4d6f258efe..2ae4d6c96f 100644 --- a/board/safety/safety_hyundai.h +++ b/board/safety/safety_hyundai.h @@ -26,10 +26,13 @@ const LongitudinalLimits HYUNDAI_LONG_LIMITS = { .min_accel = -350, // 1/100 m/s2 }; +#define HYUNDAI_COMMON_TX_MSGS(can_canfd_hybrid) \ + {0x340, 0, 8}, \ + {0x4F1, 0, 4}, \ + {0x485, 0, (can_canfd_hybrid) ? 8 : 4}, \ + static const CanMsg HYUNDAI_TX_MSGS[] = { - {0x340, 0, 8}, // LKAS11 Bus 0 - {0x4F1, 0, 4}, // CLU11 Bus 0 - {0x485, 0, 4}, // LFAHDA_MFC Bus 0 + HYUNDAI_COMMON_TX_MSGS(false) }; #define HYUNDAI_COMMON_RX_CHECKS(legacy) \ @@ -42,7 +45,6 @@ static const CanMsg HYUNDAI_TX_MSGS[] = { {.msg = {{0x421, (scc_bus), 8, .check_checksum = !(can_canfd_hybrid), .max_counter = 15U, .frequency = 50U}, { 0 }, { 0 }}}, \ static bool hyundai_legacy = false; -static bool hyundai_can_canfd_hybrid = false; static uint8_t hyundai_get_counter(const CANPacket_t *to_push) { int addr = GET_ADDR(to_push); @@ -84,7 +86,7 @@ static uint32_t hyundai_get_checksum(const CANPacket_t *to_push) { static uint32_t hyundai_compute_checksum(const CANPacket_t *to_push) { int addr = GET_ADDR(to_push); - uint16_t chksum = 0; + uint8_t chksum = 0; if (addr == 0x386) { // count the bits for (int i = 0; i < 8; i++) { @@ -99,8 +101,6 @@ static uint32_t hyundai_compute_checksum(const CANPacket_t *to_push) { } } chksum = (chksum ^ 9U) & 15U; - } else if ((addr == 0x421) && hyundai_can_canfd_hybrid) { - chksum = hyundai_common_canfd_compute_checksum(to_push); } else { // sum of nibbles for (int i = 0; i < 8; i++) { @@ -220,7 +220,8 @@ static bool hyundai_tx_hook(const CANPacket_t *to_send) { int desired_torque = ((GET_BYTES(to_send, 0, 4) >> 16) & 0x7ffU) - 1024U; bool steer_req = GET_BIT(to_send, 27U); - const SteeringLimits limits = hyundai_can_canfd_hybrid ? HYUNDAI_STEERING_LIMITS_CAN_CANFD_HYBRID : (hyundai_alt_limits ? HYUNDAI_STEERING_LIMITS_ALT : HYUNDAI_STEERING_LIMITS); + const SteeringLimits limits = hyundai_can_canfd_hybrid ? HYUNDAI_STEERING_LIMITS_CAN_CANFD_HYBRID : \ + (hyundai_alt_limits ? HYUNDAI_STEERING_LIMITS_ALT : HYUNDAI_STEERING_LIMITS); if (steer_torque_cmd_checks(desired_torque, steer_req, limits)) { tx = false; } @@ -263,8 +264,6 @@ static int hyundai_fwd_hook(int bus_num, int addr) { } static safety_config hyundai_init(uint16_t param) { - const int HYUNDAI_PARAM_CAN_CANFD_HYBRID = 256; - static const CanMsg HYUNDAI_LONG_TX_MSGS[] = { {0x340, 0, 8}, // LKAS11 Bus 0 {0x4F1, 0, 4}, // CLU11 Bus 0 @@ -286,18 +285,11 @@ static safety_config hyundai_init(uint16_t param) { }; static const CanMsg HYUNDAI_CAN_CANFD_HYBRID_TX_MSGS[] = { - {0x340, 0, 8}, - {0x4F1, 0, 4}, - {0x485, 0, 8}, + HYUNDAI_COMMON_TX_MSGS(true) }; hyundai_common_init(param); hyundai_legacy = false; - hyundai_can_canfd_hybrid = GET_FLAG(param, HYUNDAI_PARAM_CAN_CANFD_HYBRID); - - if (hyundai_can_canfd_hybrid) { - gen_crc_lookup_table_16(0x1021, hyundai_canfd_crc_lut); - } if (hyundai_camera_scc || hyundai_can_canfd_hybrid) { hyundai_longitudinal = false; @@ -348,7 +340,6 @@ static safety_config hyundai_legacy_init(uint16_t param) { hyundai_legacy = true; hyundai_longitudinal = false; hyundai_camera_scc = false; - hyundai_can_canfd_hybrid = false; return BUILD_SAFETY_CFG(hyundai_legacy_rx_checks, HYUNDAI_TX_MSGS); } diff --git a/board/safety/safety_hyundai_common.h b/board/safety/safety_hyundai_common.h index d83b396401..e6ad070f43 100644 --- a/board/safety/safety_hyundai_common.h +++ b/board/safety/safety_hyundai_common.h @@ -36,6 +36,9 @@ bool hyundai_canfd_hda2 = false; extern bool hyundai_alt_limits; bool hyundai_alt_limits = false; +extern bool hyundai_can_canfd_hybrid; +bool hyundai_can_canfd_hybrid = false; + static uint8_t hyundai_last_button_interaction; // button messages since the user pressed an enable button void hyundai_common_init(uint16_t param) { @@ -44,12 +47,14 @@ void hyundai_common_init(uint16_t param) { const int HYUNDAI_PARAM_CAMERA_SCC = 8; const int HYUNDAI_PARAM_CANFD_HDA2 = 16; const int HYUNDAI_PARAM_ALT_LIMITS = 64; // TODO: shift this down with the rest of the common flags + const int HYUNDAI_PARAM_CAN_CANFD_HYBRID = 256; hyundai_ev_gas_signal = GET_FLAG(param, HYUNDAI_PARAM_EV_GAS); hyundai_hybrid_gas_signal = !hyundai_ev_gas_signal && GET_FLAG(param, HYUNDAI_PARAM_HYBRID_GAS); hyundai_camera_scc = GET_FLAG(param, HYUNDAI_PARAM_CAMERA_SCC); hyundai_canfd_hda2 = GET_FLAG(param, HYUNDAI_PARAM_CANFD_HDA2); hyundai_alt_limits = GET_FLAG(param, HYUNDAI_PARAM_ALT_LIMITS); + hyundai_can_canfd_hybrid = GET_FLAG(param, HYUNDAI_PARAM_CAN_CANFD_HYBRID); hyundai_last_button_interaction = HYUNDAI_PREV_BUTTON_SAMPLES; diff --git a/board/safety_declarations.h b/board/safety_declarations.h index 21e52a1eed..b0e9588e95 100644 --- a/board/safety_declarations.h +++ b/board/safety_declarations.h @@ -176,7 +176,9 @@ void update_sample(struct sample_t *sample, int sample_new); bool get_longitudinal_allowed(void); int ROUND(float val); void gen_crc_lookup_table_8(uint8_t poly, uint8_t crc_lut[]); +#ifdef CANFD void gen_crc_lookup_table_16(uint16_t poly, uint16_t crc_lut[]); +#endif void generic_rx_checks(bool stock_ecu_detected); bool steer_torque_cmd_checks(int desired_torque, int steer_req, const SteeringLimits limits); bool steer_angle_cmd_checks(int desired_angle, bool steer_control_enabled, const SteeringLimits limits); From 5725b14ca503c53031ee49059cf12c0ba1b3c2f6 Mon Sep 17 00:00:00 2001 From: Jason Wen Date: Fri, 27 Sep 2024 16:25:03 -0400 Subject: [PATCH 3/3] point to opendbc feature branch to pass unit test for now --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 5d3c1ffc1d..61d3edfc39 100644 --- a/Dockerfile +++ b/Dockerfile @@ -31,9 +31,9 @@ RUN pip3 install --break-system-packages --no-cache-dir $PYTHONPATH/panda/[dev] # TODO: this should be a "pip install" or not even in this repo at all RUN git config --global --add safe.directory $PYTHONPATH/panda -ENV OPENDBC_REF="5ed7a834a4e0e24c3968dd1e98ceb4b9d5f9791a" +ENV OPENDBC_REF="d68fdc4eb47100a41f770273bf6d32d2b3c19a51" RUN cd /tmp/ && \ - git clone --depth 1 https://github.com/commaai/opendbc opendbc_repo && \ + git clone --depth 1 https://github.com/sunnypilot/opendbc opendbc_repo && \ cd opendbc_repo && git fetch origin $OPENDBC_REF && git checkout FETCH_HEAD && rm -rf .git/ && \ pip3 install --break-system-packages --no-cache-dir Cython numpy && \ scons -j8 --minimal opendbc/ && \