2017-04-17 16:28:28 -06:00
|
|
|
// board enforces
|
|
|
|
// in-state
|
|
|
|
// accel set/resume
|
|
|
|
// out-state
|
|
|
|
// cancel button
|
2017-09-14 21:23:20 -06:00
|
|
|
// accel rising edge
|
|
|
|
// brake rising edge
|
|
|
|
// brake > 0mph
|
2017-04-17 16:28:28 -06:00
|
|
|
|
2019-06-05 14:38:59 -06:00
|
|
|
const int HONDA_GAS_INTERCEPTOR_THRESHOLD = 328; // ratio between offset and gain from dbc file
|
2019-06-04 13:43:50 -06:00
|
|
|
int honda_brake_prev = 0;
|
2019-06-04 13:21:52 -06:00
|
|
|
int honda_gas_prev = 0;
|
2019-06-04 13:43:50 -06:00
|
|
|
int honda_ego_speed = 0;
|
|
|
|
bool honda_bosch_hardware = false;
|
2018-06-06 15:20:07 -06:00
|
|
|
bool honda_alt_brake_msg = false;
|
2017-07-12 12:25:10 -06:00
|
|
|
|
2017-07-17 11:20:08 -06:00
|
|
|
static void honda_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
|
2017-09-14 21:23:20 -06:00
|
|
|
|
2019-06-14 20:13:05 -06:00
|
|
|
int addr = GET_ADDR(to_push);
|
|
|
|
int len = GET_LEN(to_push);
|
2019-06-11 22:23:48 -06:00
|
|
|
|
2017-09-14 21:23:20 -06:00
|
|
|
// sample speed
|
2019-06-11 22:23:48 -06:00
|
|
|
if (addr == 0x158) {
|
2017-09-14 21:23:20 -06:00
|
|
|
// first 2 bytes
|
2019-06-04 13:43:50 -06:00
|
|
|
honda_ego_speed = to_push->RDLR & 0xFFFF;
|
2017-09-14 21:23:20 -06:00
|
|
|
}
|
|
|
|
|
2017-04-06 19:11:36 -06:00
|
|
|
// state machine to enter and exit controls
|
|
|
|
// 0x1A6 for the ILX, 0x296 for the Civic Touring
|
2019-06-11 22:23:48 -06:00
|
|
|
if ((addr == 0x1A6) || (addr == 0x296)) {
|
2017-04-06 19:11:36 -06:00
|
|
|
int buttons = (to_push->RDLR & 0xE0) >> 5;
|
2019-06-11 22:23:48 -06:00
|
|
|
if ((buttons == 4) || (buttons == 3)) {
|
2017-04-06 19:11:36 -06:00
|
|
|
controls_allowed = 1;
|
|
|
|
} else if (buttons == 2) {
|
|
|
|
controls_allowed = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-06 15:20:07 -06:00
|
|
|
// user brake signal on 0x17C reports applied brake from computer brake on accord
|
|
|
|
// and crv, which prevents the usual brake safety from working correctly. these
|
|
|
|
// cars have a signal on 0x1BE which only detects user's brake being applied so
|
|
|
|
// in these cases, this is used instead.
|
|
|
|
// most hondas: 0x17C bit 53
|
|
|
|
// accord, crv: 0x1BE bit 4
|
2019-06-11 22:23:48 -06:00
|
|
|
#define IS_USER_BRAKE_MSG(addr) (!honda_alt_brake_msg ? ((addr) == 0x17C) : ((addr) == 0x1BE))
|
|
|
|
#define USER_BRAKE_VALUE(to_push) (!honda_alt_brake_msg ? ((to_push)->RDHR & 0x200000) : ((to_push)->RDLR & 0x10))
|
2017-09-14 21:23:20 -06:00
|
|
|
// exit controls on rising edge of brake press or on brake press when
|
|
|
|
// speed > 0
|
2019-06-12 21:12:48 -06:00
|
|
|
bool is_user_brake_msg = IS_USER_BRAKE_MSG(addr); // needed to enforce type
|
|
|
|
if (is_user_brake_msg) {
|
2018-03-06 12:56:01 -07:00
|
|
|
int brake = USER_BRAKE_VALUE(to_push);
|
2019-06-04 13:43:50 -06:00
|
|
|
if (brake && (!(honda_brake_prev) || honda_ego_speed)) {
|
2017-04-06 19:11:36 -06:00
|
|
|
controls_allowed = 0;
|
|
|
|
}
|
2019-06-04 13:43:50 -06:00
|
|
|
honda_brake_prev = brake;
|
2017-04-06 19:11:36 -06:00
|
|
|
}
|
|
|
|
|
2018-03-06 15:37:06 -07:00
|
|
|
// exit controls on rising edge of gas press if interceptor (0x201 w/ len = 6)
|
|
|
|
// length check because bosch hardware also uses this id (0x201 w/ len = 8)
|
2019-06-14 20:13:05 -06:00
|
|
|
if ((addr == 0x201) && (len == 6)) {
|
2019-06-04 14:36:46 -06:00
|
|
|
gas_interceptor_detected = 1;
|
2017-09-14 21:23:20 -06:00
|
|
|
int gas_interceptor = ((to_push->RDLR & 0xFF) << 8) | ((to_push->RDLR & 0xFF00) >> 8);
|
2019-06-04 13:21:52 -06:00
|
|
|
if ((gas_interceptor > HONDA_GAS_INTERCEPTOR_THRESHOLD) &&
|
2019-06-05 15:00:07 -06:00
|
|
|
(gas_interceptor_prev <= HONDA_GAS_INTERCEPTOR_THRESHOLD) &&
|
|
|
|
long_controls_allowed) {
|
2017-04-06 19:11:36 -06:00
|
|
|
controls_allowed = 0;
|
|
|
|
}
|
2019-06-05 14:38:59 -06:00
|
|
|
gas_interceptor_prev = gas_interceptor;
|
2017-04-06 19:11:36 -06:00
|
|
|
}
|
|
|
|
|
2017-09-14 21:23:20 -06:00
|
|
|
// exit controls on rising edge of gas press if no interceptor
|
2019-06-04 14:36:46 -06:00
|
|
|
if (!gas_interceptor_detected) {
|
2019-06-11 22:23:48 -06:00
|
|
|
if (addr == 0x17C) {
|
2017-09-14 21:23:20 -06:00
|
|
|
int gas = to_push->RDLR & 0xFF;
|
2019-06-05 15:00:07 -06:00
|
|
|
if (gas && !(honda_gas_prev) && long_controls_allowed) {
|
2017-04-06 19:11:36 -06:00
|
|
|
controls_allowed = 0;
|
|
|
|
}
|
2019-06-04 13:21:52 -06:00
|
|
|
honda_gas_prev = gas;
|
2017-04-06 19:11:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 21:23:20 -06:00
|
|
|
// all commands: gas, brake and steering
|
|
|
|
// if controls_allowed and no pedals pressed
|
|
|
|
// allow all commands up to limit
|
|
|
|
// else
|
|
|
|
// block all commands that produce actuation
|
|
|
|
|
2017-07-21 00:36:06 -06:00
|
|
|
static int honda_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
|
2017-09-14 21:23:20 -06:00
|
|
|
|
2019-06-12 19:18:07 -06:00
|
|
|
int tx = 1;
|
2019-06-14 20:13:05 -06:00
|
|
|
int addr = GET_ADDR(to_send);
|
|
|
|
int bus = GET_BUS(to_send);
|
2019-06-11 22:23:48 -06:00
|
|
|
|
2017-09-14 21:23:20 -06:00
|
|
|
// disallow actuator commands if gas or brake (with vehicle moving) are pressed
|
|
|
|
// and the the latching controls_allowed flag is True
|
2019-06-05 14:38:59 -06:00
|
|
|
int pedal_pressed = honda_gas_prev || (gas_interceptor_prev > HONDA_GAS_INTERCEPTOR_THRESHOLD) ||
|
2019-06-04 13:43:50 -06:00
|
|
|
(honda_brake_prev && honda_ego_speed);
|
2019-06-12 21:12:48 -06:00
|
|
|
bool current_controls_allowed = controls_allowed && !(pedal_pressed);
|
2017-09-14 21:23:20 -06:00
|
|
|
|
2017-04-06 19:11:36 -06:00
|
|
|
// BRAKE: safety check
|
2019-06-11 22:23:48 -06:00
|
|
|
if (addr == 0x1FA) {
|
2019-06-05 15:00:07 -06:00
|
|
|
if (current_controls_allowed && long_controls_allowed) {
|
2019-06-12 19:18:07 -06:00
|
|
|
if ((to_send->RDLR & 0xFFFFFF3F) != to_send->RDLR) {
|
|
|
|
tx = 0;
|
|
|
|
}
|
2017-04-06 19:11:36 -06:00
|
|
|
} else {
|
2019-06-12 19:18:07 -06:00
|
|
|
if ((to_send->RDLR & 0xFFFF0000) != to_send->RDLR) {
|
|
|
|
tx = 0;
|
|
|
|
}
|
2017-04-06 19:11:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// STEER: safety check
|
2019-06-11 22:23:48 -06:00
|
|
|
if ((addr == 0xE4) || (addr == 0x194)) {
|
2017-09-14 21:23:20 -06:00
|
|
|
if (current_controls_allowed) {
|
2017-07-17 19:54:50 -06:00
|
|
|
// all messages are fine here
|
2017-04-06 19:11:36 -06:00
|
|
|
} else {
|
2019-06-12 19:18:07 -06:00
|
|
|
if ((to_send->RDLR & 0xFFFF0000) != to_send->RDLR) {
|
|
|
|
tx = 0;
|
|
|
|
}
|
2017-04-06 19:11:36 -06:00
|
|
|
}
|
2017-07-14 22:17:32 -06:00
|
|
|
}
|
2017-04-06 19:11:36 -06:00
|
|
|
|
|
|
|
// GAS: safety check
|
2019-06-11 22:23:48 -06:00
|
|
|
if (addr == 0x200) {
|
2019-06-05 15:00:07 -06:00
|
|
|
if (current_controls_allowed && long_controls_allowed) {
|
2017-07-17 19:54:50 -06:00
|
|
|
// all messages are fine here
|
2017-04-06 19:11:36 -06:00
|
|
|
} else {
|
2019-06-12 19:18:07 -06:00
|
|
|
if ((to_send->RDLR & 0xFFFF0000) != to_send->RDLR) {
|
|
|
|
tx = 0;
|
|
|
|
}
|
2017-04-06 19:11:36 -06:00
|
|
|
}
|
2017-07-14 22:17:32 -06:00
|
|
|
}
|
2018-08-28 17:01:40 -06:00
|
|
|
|
2018-06-06 15:20:07 -06:00
|
|
|
// FORCE CANCEL: safety check only relevant when spamming the cancel button in Bosch HW
|
|
|
|
// ensuring that only the cancel button press is sent (VAL 2) when controls are off.
|
|
|
|
// This avoids unintended engagements while still allowing resume spam
|
2019-06-11 22:23:48 -06:00
|
|
|
if ((addr == 0x296) && honda_bosch_hardware &&
|
2019-06-14 20:13:05 -06:00
|
|
|
!current_controls_allowed && (bus == 0)) {
|
2019-06-12 19:18:07 -06:00
|
|
|
if (((to_send->RDLR >> 5) & 0x7) != 2) {
|
|
|
|
tx = 0;
|
|
|
|
}
|
2018-06-06 15:20:07 -06:00
|
|
|
}
|
2017-04-06 19:11:36 -06:00
|
|
|
|
2017-04-28 10:56:01 -06:00
|
|
|
// 1 allows the message through
|
2019-06-12 19:18:07 -06:00
|
|
|
return tx;
|
2017-04-06 19:11:36 -06:00
|
|
|
}
|
|
|
|
|
2018-01-26 00:57:04 -07:00
|
|
|
static void honda_init(int16_t param) {
|
2017-08-22 14:44:11 -06:00
|
|
|
controls_allowed = 0;
|
2019-06-04 13:43:50 -06:00
|
|
|
honda_bosch_hardware = false;
|
2018-06-06 15:20:07 -06:00
|
|
|
honda_alt_brake_msg = false;
|
2017-07-14 22:17:32 -06:00
|
|
|
}
|
|
|
|
|
2018-02-28 00:27:47 -07:00
|
|
|
static void honda_bosch_init(int16_t param) {
|
|
|
|
controls_allowed = 0;
|
2019-06-04 13:43:50 -06:00
|
|
|
honda_bosch_hardware = true;
|
2018-06-06 15:20:07 -06:00
|
|
|
// Checking for alternate brake override from safety parameter
|
2019-06-11 22:23:48 -06:00
|
|
|
honda_alt_brake_msg = (param == 1) ? true : false;
|
2018-02-28 00:27:47 -07:00
|
|
|
}
|
|
|
|
|
2018-11-03 18:11:51 -06:00
|
|
|
static int honda_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {
|
|
|
|
// fwd from car to camera. also fwd certain msgs from camera to car
|
2018-11-09 18:58:55 -07:00
|
|
|
// 0xE4 is steering on all cars except CRV and RDX, 0x194 for CRV and RDX,
|
|
|
|
// 0x1FA is brake control, 0x30C is acc hud, 0x33D is lkas hud,
|
|
|
|
// 0x39f is radar hud
|
2019-06-12 19:18:07 -06:00
|
|
|
int bus_fwd = -1;
|
|
|
|
|
2018-11-03 18:11:51 -06:00
|
|
|
if (bus_num == 0) {
|
2019-06-12 19:18:07 -06:00
|
|
|
bus_fwd = 2;
|
2019-06-05 15:00:07 -06:00
|
|
|
} else if (bus_num == 2) {
|
|
|
|
// block stock lkas messages and stock acc messages (if OP is doing ACC)
|
2019-06-14 20:13:05 -06:00
|
|
|
int addr = GET_ADDR(to_fwd);
|
2019-06-11 22:23:48 -06:00
|
|
|
int is_lkas_msg = (addr == 0xE4) || (addr == 0x194) || (addr == 0x33D);
|
|
|
|
int is_acc_msg = (addr == 0x1FA) || (addr == 0x30C) || (addr == 0x39F);
|
2019-06-12 19:18:07 -06:00
|
|
|
int block_fwd = is_lkas_msg || (is_acc_msg && long_controls_allowed);
|
|
|
|
if (!block_fwd) {
|
|
|
|
bus_fwd = 0;
|
2019-06-05 15:00:07 -06:00
|
|
|
}
|
2018-11-03 18:11:51 -06:00
|
|
|
}
|
2019-06-12 19:18:07 -06:00
|
|
|
return bus_fwd;
|
2018-11-03 18:11:51 -06:00
|
|
|
}
|
|
|
|
|
2018-03-05 21:18:43 -07:00
|
|
|
static int honda_bosch_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {
|
2019-06-12 19:18:07 -06:00
|
|
|
int bus_fwd = -1;
|
2019-06-11 22:23:48 -06:00
|
|
|
|
|
|
|
if (bus_num == 2) {
|
2019-06-12 19:18:07 -06:00
|
|
|
bus_fwd = 1;
|
|
|
|
} else if (bus_num == 1) {
|
2019-06-14 20:13:05 -06:00
|
|
|
int addr = GET_ADDR(to_fwd);
|
2019-06-12 19:18:07 -06:00
|
|
|
int is_lkas_msg = (addr == 0xE4) || (addr == 0x33D);
|
|
|
|
if (!is_lkas_msg) {
|
|
|
|
bus_fwd = 2;
|
|
|
|
}
|
2018-03-05 21:18:43 -07:00
|
|
|
}
|
2019-06-12 19:18:07 -06:00
|
|
|
return bus_fwd;
|
2018-03-05 21:18:43 -07:00
|
|
|
}
|
|
|
|
|
2018-11-03 18:11:51 -06:00
|
|
|
const safety_hooks honda_hooks = {
|
|
|
|
.init = honda_init,
|
|
|
|
.rx = honda_rx_hook,
|
|
|
|
.tx = honda_tx_hook,
|
|
|
|
.tx_lin = nooutput_tx_lin_hook,
|
|
|
|
.ignition = default_ign_hook,
|
|
|
|
.fwd = honda_fwd_hook,
|
|
|
|
};
|
|
|
|
|
2018-02-28 00:27:47 -07:00
|
|
|
const safety_hooks honda_bosch_hooks = {
|
|
|
|
.init = honda_bosch_init,
|
|
|
|
.rx = honda_rx_hook,
|
|
|
|
.tx = honda_tx_hook,
|
2018-08-28 17:01:40 -06:00
|
|
|
.tx_lin = nooutput_tx_lin_hook,
|
2018-05-25 19:20:55 -06:00
|
|
|
.ignition = default_ign_hook,
|
2018-03-05 21:18:43 -07:00
|
|
|
.fwd = honda_bosch_fwd_hook,
|
2018-02-28 00:27:47 -07:00
|
|
|
};
|