Skip to content

Commit

Permalink
Hyundai: share flags with CAN-FD platform (#1099)
Browse files Browse the repository at this point in the history
* Hyundai: share flags with CAN-FD platform

* update flags there
  • Loading branch information
adeebshihadeh authored Oct 13, 2022
1 parent 2f3e282 commit 622ce92
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 31 deletions.
23 changes: 6 additions & 17 deletions board/safety/safety_hyundai.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,9 @@ AddrCheckStruct hyundai_legacy_addr_checks[] = {
};
#define HYUNDAI_LEGACY_ADDR_CHECK_LEN (sizeof(hyundai_legacy_addr_checks) / sizeof(hyundai_legacy_addr_checks[0]))

const int HYUNDAI_PARAM_EV_GAS = 1;
const int HYUNDAI_PARAM_HYBRID_GAS = 2;
const int HYUNDAI_PARAM_LONGITUDINAL = 4;
const int HYUNDAI_PARAM_CAMERA_SCC = 8;

bool hyundai_legacy = false;
bool hyundai_ev_gas_signal = false;
bool hyundai_hybrid_gas_signal = false;
bool hyundai_camera_scc = false;

addr_checks hyundai_rx_checks = {hyundai_addr_checks, HYUNDAI_ADDR_CHECK_LEN};
Expand Down Expand Up @@ -331,18 +326,13 @@ static int hyundai_fwd_hook(int bus_num, CANPacket_t *to_fwd) {
}

static const addr_checks* hyundai_init(uint16_t param) {
hyundai_common_init(param);
hyundai_legacy = false;
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_last_button_interaction = HYUNDAI_PREV_BUTTON_SAMPLES;

#ifdef ALLOW_DEBUG
// TODO: add longitudinal support for camera-based SCC platform
hyundai_longitudinal = GET_FLAG(param, HYUNDAI_PARAM_LONGITUDINAL) && !hyundai_camera_scc;
#else
hyundai_longitudinal = false;
#endif
if (hyundai_camera_scc) {
hyundai_longitudinal = false;
}

if (hyundai_longitudinal) {
hyundai_rx_checks = (addr_checks){hyundai_long_addr_checks, HYUNDAI_LONG_ADDR_CHECK_LEN};
Expand All @@ -355,12 +345,11 @@ static const addr_checks* hyundai_init(uint16_t param) {
}

static const addr_checks* hyundai_legacy_init(uint16_t param) {
hyundai_common_init(param);
hyundai_legacy = true;
hyundai_longitudinal = false;
hyundai_camera_scc = false;
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_last_button_interaction = HYUNDAI_PREV_BUTTON_SAMPLES;

hyundai_rx_checks = (addr_checks){hyundai_legacy_addr_checks, HYUNDAI_LEGACY_ADDR_CHECK_LEN};
return &hyundai_rx_checks;
}
Expand Down
16 changes: 7 additions & 9 deletions board/safety/safety_hyundai_canfd.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ addr_checks hyundai_canfd_rx_checks = {hyundai_canfd_addr_checks, HYUNDAI_CANFD_
uint16_t hyundai_canfd_crc_lut[256];


const int HYUNDAI_PARAM_CANFD_HDA2 = 1;
const int HYUNDAI_PARAM_CANFD_ALT_BUTTONS = 2;
const int HYUNDAI_PARAM_CANFD_LONG = 4;
const int HYUNDAI_PARAM_CANFD_HDA2 = 8;
const int HYUNDAI_PARAM_CANFD_ALT_BUTTONS = 16;
bool hyundai_canfd_hda2 = false;
bool hyundai_canfd_alt_buttons = false;

Expand Down Expand Up @@ -287,16 +286,15 @@ static int hyundai_canfd_fwd_hook(int bus_num, CANPacket_t *to_fwd) {
}

static const addr_checks* hyundai_canfd_init(uint16_t param) {
hyundai_common_init(param);

gen_crc_lookup_table_16(0x1021, hyundai_canfd_crc_lut);
hyundai_last_button_interaction = HYUNDAI_PREV_BUTTON_SAMPLES;
hyundai_canfd_hda2 = GET_FLAG(param, HYUNDAI_PARAM_CANFD_HDA2);
hyundai_canfd_alt_buttons = GET_FLAG(param, HYUNDAI_PARAM_CANFD_ALT_BUTTONS);

#ifdef ALLOW_DEBUG
hyundai_longitudinal = GET_FLAG(param, HYUNDAI_PARAM_CANFD_LONG) && hyundai_canfd_hda2;
#else
hyundai_longitudinal = false;
#endif
if (!hyundai_canfd_hda2) {
hyundai_longitudinal = false;
}

return &hyundai_canfd_rx_checks;
}
Expand Down
18 changes: 18 additions & 0 deletions board/safety/safety_hyundai_common.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifndef SAFETY_HYUNDAI_COMMON_H
#define SAFETY_HYUNDAI_COMMON_H

const int HYUNDAI_PARAM_EV_GAS = 1;
const int HYUNDAI_PARAM_HYBRID_GAS = 2;
const int HYUNDAI_PARAM_LONGITUDINAL = 4;

const uint8_t HYUNDAI_PREV_BUTTON_SAMPLES = 8; // roughly 160 ms
const uint32_t HYUNDAI_STANDSTILL_THRSLD = 30; // ~1kph

Expand All @@ -12,9 +16,23 @@ enum {
};

// common state
bool hyundai_ev_gas_signal = false;
bool hyundai_hybrid_gas_signal = false;
bool hyundai_longitudinal = false;
uint8_t hyundai_last_button_interaction; // button messages since the user pressed an enable button

void hyundai_common_init(uint16_t param) {
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_last_button_interaction = HYUNDAI_PREV_BUTTON_SAMPLES;

#ifdef ALLOW_DEBUG
hyundai_longitudinal = GET_FLAG(param, HYUNDAI_PARAM_LONGITUDINAL);
#else
hyundai_longitudinal = false;
#endif
}

void hyundai_common_cruise_state_check(const int cruise_engaged) {
// some newer HKG models can re-enable after spamming cancel button,
Expand Down
6 changes: 2 additions & 4 deletions python/__init__.py