Skip to content

Commit

Permalink
WIP: Relay malfunction (#384)
Browse files Browse the repository at this point in the history
* relay malfunction handling. WIP

* more cars to relay_malfunctions

* fixed safety tests

* minor change

* Fix linter

* all cars now have a relay_malfunction safety check

* added relay_malfunction safety test for fwd hooks

* added proper regression tests for relay malfunction to all cars

* temp patch to not fail regression in honda bosch

* also addr 0x194 is some nidec honda is steer control

* proper relay check for honda bosch too
  • Loading branch information
rbiasini authored Nov 15, 2019
1 parent 69d9d61 commit cfcce8f
Show file tree
Hide file tree
Showing 22 changed files with 300 additions and 261 deletions.
34 changes: 14 additions & 20 deletions board/safety/safety_chrysler.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const int CHRYSLER_MAX_RATE_UP = 3;
const int CHRYSLER_MAX_RATE_DOWN = 3;
const int CHRYSLER_MAX_TORQUE_ERROR = 80; // max torque cmd in excess of torque motor

bool chrysler_camera_detected = 0; // is giraffe switch 2 high?
int chrysler_rt_torque_last = 0;
int chrysler_desired_torque_last = 0;
int chrysler_cruise_engaged_last = 0;
Expand Down Expand Up @@ -36,10 +35,9 @@ static void chrysler_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
chrysler_cruise_engaged_last = cruise_engaged;
}

// check if stock camera ECU is still online
// check if stock camera ECU is on bus 0
if ((bus == 0) && (addr == 0x292)) {
chrysler_camera_detected = 1;
controls_allowed = 0;
relay_malfunction = true;
}
}

Expand All @@ -48,8 +46,7 @@ static int chrysler_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
int addr = GET_ADDR(to_send);
int tx = 1;

// If camera is on bus 0, then nothing can be sent
if (chrysler_camera_detected) {
if (relay_malfunction) {
tx = 0;
}

Expand Down Expand Up @@ -108,30 +105,27 @@ static int chrysler_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
return tx;
}

static void chrysler_init(int16_t param) {
UNUSED(param);
controls_allowed = 0;
chrysler_camera_detected = 0;
}

static int chrysler_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {

int bus_fwd = -1;
int addr = GET_ADDR(to_fwd);
// forward CAN 0 -> 2 so stock LKAS camera sees messages
if ((bus_num == 0) && !chrysler_camera_detected) {
bus_fwd = 2;
}
// forward all messages from camera except LKAS_COMMAND and LKAS_HUD
if ((bus_num == 2) && !chrysler_camera_detected && (addr != 658) && (addr != 678)) {
bus_fwd = 0;

if (!relay_malfunction) {
// forward CAN 0 -> 2 so stock LKAS camera sees messages
if (bus_num == 0) {
bus_fwd = 2;
}
// forward all messages from camera except LKAS_COMMAND and LKAS_HUD
if ((bus_num == 2) && (addr != 658) && (addr != 678)) {
bus_fwd = 0;
}
}
return bus_fwd;
}


const safety_hooks chrysler_hooks = {
.init = chrysler_init,
.init = nooutput_init,
.rx = chrysler_rx_hook,
.tx = chrysler_tx_hook,
.tx_lin = nooutput_tx_lin_hook,
Expand Down
6 changes: 4 additions & 2 deletions board/safety/safety_defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ void default_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {

static void nooutput_init(int16_t param) {
UNUSED(param);
controls_allowed = 0;
controls_allowed = false;
relay_malfunction = false;
}

static int nooutput_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
Expand Down Expand Up @@ -39,7 +40,8 @@ const safety_hooks nooutput_hooks = {

static void alloutput_init(int16_t param) {
UNUSED(param);
controls_allowed = 1;
controls_allowed = true;
relay_malfunction = false;
}

static int alloutput_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
Expand Down
14 changes: 13 additions & 1 deletion board/safety/safety_ford.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ bool ford_moving = false;
static void ford_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {

int addr = GET_ADDR(to_push);
int bus = GET_BUS(to_push);

if (addr == 0x217) {
// wheel speeds are 14 bits every 16
Expand Down Expand Up @@ -53,6 +54,10 @@ static void ford_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
}
ford_gas_prev = gas;
}

if ((bus == 0) && (addr == 0x3CA)) {
relay_malfunction = true;
}
}

// all commands: just steering
Expand All @@ -64,11 +69,16 @@ static void ford_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
static int ford_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {

int tx = 1;
int addr = GET_ADDR(to_send);

// disallow actuator commands if gas or brake (with vehicle moving) are pressed
// and the the latching controls_allowed flag is True
int pedal_pressed = ford_gas_prev || (ford_brake_prev && ford_moving);
bool current_controls_allowed = controls_allowed && !(pedal_pressed);
int addr = GET_ADDR(to_send);

if (relay_malfunction) {
tx = 0;
}

// STEER: safety check
if (addr == 0x3CA) {
Expand All @@ -92,6 +102,8 @@ static int ford_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
return tx;
}

// TODO: keep camera on bus 2 and make a fwd_hook

const safety_hooks ford_hooks = {
.init = nooutput_init,
.rx = ford_rx_hook,
Expand Down
15 changes: 3 additions & 12 deletions board/safety/safety_gm.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ const int GM_MAX_BRAKE = 350;
int gm_brake_prev = 0;
int gm_gas_prev = 0;
bool gm_moving = false;
// silence everything if stock car control ECUs are still online
bool gm_ascm_detected = 0;
int gm_rt_torque_last = 0;
int gm_desired_torque_last = 0;
uint32_t gm_ts_last = 0;
Expand Down Expand Up @@ -51,8 +49,7 @@ static void gm_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
// 384 = ASCMLKASteeringCmd
// 715 = ASCMGasRegenCmd
if ((bus_number == 0) && ((addr == 384) || (addr == 715))) {
gm_ascm_detected = 1;
controls_allowed = 0;
relay_malfunction = true;
}

// ACC steering wheel buttons
Expand Down Expand Up @@ -114,8 +111,7 @@ static int gm_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {

int tx = 1;

// There can be only one! (ASCM)
if (gm_ascm_detected) {
if (relay_malfunction) {
tx = 0;
}

Expand Down Expand Up @@ -213,14 +209,9 @@ static int gm_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
return tx;
}

static void gm_init(int16_t param) {
UNUSED(param);
controls_allowed = 0;
}


const safety_hooks gm_hooks = {
.init = gm_init,
.init = nooutput_init,
.rx = gm_rx_hook,
.tx = gm_tx_hook,
.tx_lin = nooutput_tx_lin_hook,
Expand Down
68 changes: 44 additions & 24 deletions board/safety/safety_honda.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ static void honda_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
// Leave honda forward brake as is
}
}

// if steering controls messages are received on the destination bus, it's an indication
// that the relay might be malfucntioning
int bus_rdr_car = (board_has_relay()) ? 0 : 2; // radar bus, car side
if ((addr == 0xE4) || (addr == 0x194)) {
if ((honda_bosch_hardware && (bus == bus_rdr_car)) ||
(!honda_bosch_hardware && (bus == 0))) {
relay_malfunction = true;
}
}
}

// all commands: gas, brake and steering
Expand All @@ -112,6 +122,10 @@ static int honda_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
int addr = GET_ADDR(to_send);
int bus = GET_BUS(to_send);

if (relay_malfunction) {
tx = 0;
}

// disallow actuator commands if gas or brake (with vehicle moving) are pressed
// and the the latching controls_allowed flag is True
int pedal_pressed = honda_gas_prev || (gas_interceptor_prev > HONDA_GAS_INTERCEPTOR_THRESHOLD) ||
Expand Down Expand Up @@ -170,13 +184,15 @@ static int honda_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {

static void honda_init(int16_t param) {
UNUSED(param);
controls_allowed = 0;
controls_allowed = false;
relay_malfunction = false;
honda_bosch_hardware = false;
honda_alt_brake_msg = false;
}

static void honda_bosch_init(int16_t param) {
controls_allowed = 0;
controls_allowed = false;
relay_malfunction = false;
honda_bosch_hardware = true;
// Checking for alternate brake override from safety parameter
honda_alt_brake_msg = (param == 1) ? true : false;
Expand All @@ -189,20 +205,22 @@ static int honda_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {
// 0x39f is radar hud
int bus_fwd = -1;

if (bus_num == 0) {
bus_fwd = 2;
}
if (bus_num == 2) {
// block stock lkas messages and stock acc messages (if OP is doing ACC)
int addr = GET_ADDR(to_fwd);
bool is_lkas_msg = (addr == 0xE4) || (addr == 0x194) || (addr == 0x33D);
bool is_acc_hud_msg = (addr == 0x30C) || (addr == 0x39F);
bool is_brake_msg = addr == 0x1FA;
bool block_fwd = is_lkas_msg ||
(is_acc_hud_msg && long_controls_allowed) ||
(is_brake_msg && long_controls_allowed && !honda_fwd_brake);
if (!block_fwd) {
bus_fwd = 0;
if (!relay_malfunction) {
if (bus_num == 0) {
bus_fwd = 2;
}
if (bus_num == 2) {
// block stock lkas messages and stock acc messages (if OP is doing ACC)
int addr = GET_ADDR(to_fwd);
bool is_lkas_msg = (addr == 0xE4) || (addr == 0x194) || (addr == 0x33D);
bool is_acc_hud_msg = (addr == 0x30C) || (addr == 0x39F);
bool is_brake_msg = addr == 0x1FA;
bool block_fwd = is_lkas_msg ||
(is_acc_hud_msg && long_controls_allowed) ||
(is_brake_msg && long_controls_allowed && !honda_fwd_brake);
if (!block_fwd) {
bus_fwd = 0;
}
}
}
return bus_fwd;
Expand All @@ -213,14 +231,16 @@ static int honda_bosch_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {
int bus_rdr_cam = (board_has_relay()) ? 2 : 1; // radar bus, camera side
int bus_rdr_car = (board_has_relay()) ? 0 : 2; // radar bus, car side

if (bus_num == bus_rdr_car) {
bus_fwd = bus_rdr_cam;
}
if (bus_num == bus_rdr_cam) {
int addr = GET_ADDR(to_fwd);
int is_lkas_msg = (addr == 0xE4) || (addr == 0x33D);
if (!is_lkas_msg) {
bus_fwd = bus_rdr_car;
if (!relay_malfunction) {
if (bus_num == bus_rdr_car) {
bus_fwd = bus_rdr_cam;
}
if (bus_num == bus_rdr_cam) {
int addr = GET_ADDR(to_fwd);
int is_lkas_msg = (addr == 0xE4) || (addr == 0x33D);
if (!is_lkas_msg) {
bus_fwd = bus_rdr_car;
}
}
}
return bus_fwd;
Expand Down
40 changes: 9 additions & 31 deletions board/safety/safety_hyundai.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ const int HYUNDAI_MAX_RATE_DOWN = 7;
const int HYUNDAI_DRIVER_TORQUE_ALLOWANCE = 50;
const int HYUNDAI_DRIVER_TORQUE_FACTOR = 2;

bool hyundai_camera_detected = 0;
bool hyundai_giraffe_switch_2 = 0; // is giraffe switch 2 high?
int hyundai_camera_bus = 0;
int hyundai_rt_torque_last = 0;
int hyundai_desired_torque_last = 0;
int hyundai_cruise_engaged_last = 0;
Expand All @@ -25,15 +22,9 @@ static void hyundai_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
update_sample(&hyundai_torque_driver, torque_driver_new);
}

// check if stock camera ECU is still online
// check if stock camera ECU is on bus 0
if ((bus == 0) && (addr == 832)) {
hyundai_camera_detected = 1;
controls_allowed = 0;
}

// Find out which bus the camera is on
if (addr == 832) {
hyundai_camera_bus = bus;
relay_malfunction = true;
}

// enter controls on rising edge of ACC, exit controls on ACC off
Expand All @@ -48,20 +39,14 @@ static void hyundai_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
}
hyundai_cruise_engaged_last = cruise_engaged;
}

// 832 is lkas cmd. If it is on camera bus, then giraffe switch 2 is high
if ((addr == 832) && (bus == hyundai_camera_bus) && (hyundai_camera_bus != 0)) {
hyundai_giraffe_switch_2 = 1;
}
}

static int hyundai_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {

int tx = 1;
int addr = GET_ADDR(to_send);

// There can be only one! (camera)
if (hyundai_camera_detected) {
if (relay_malfunction) {
tx = 0;
}

Expand Down Expand Up @@ -128,29 +113,22 @@ static int hyundai_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
static int hyundai_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {

int bus_fwd = -1;
int addr = GET_ADDR(to_fwd);
// forward cam to ccan and viceversa, except lkas cmd
if (hyundai_giraffe_switch_2) {
if (!relay_malfunction) {
if (bus_num == 0) {
bus_fwd = hyundai_camera_bus;
bus_fwd = 2;
}
if (bus_num == hyundai_camera_bus) {
int addr = GET_ADDR(to_fwd);
if (addr != 832) {
bus_fwd = 0;
}
if ((bus_num == 2) && (addr != 832)) {
bus_fwd = 0;
}
}
return bus_fwd;
}

static void hyundai_init(int16_t param) {
UNUSED(param);
controls_allowed = 0;
hyundai_giraffe_switch_2 = 0;
}

const safety_hooks hyundai_hooks = {
.init = hyundai_init,
.init = nooutput_init,
.rx = hyundai_rx_hook,
.tx = hyundai_tx_hook,
.tx_lin = nooutput_tx_lin_hook,
Expand Down
Loading

0 comments on commit cfcce8f

Please sign in to comment.