2018-08-28 17:01:40 -06:00
|
|
|
const int HYUNDAI_MAX_STEER = 250;
|
2018-08-29 20:30:14 -06:00
|
|
|
const int HYUNDAI_MAX_RT_DELTA = 112; // max delta torque allowed for real time checks
|
2018-08-28 17:01:40 -06:00
|
|
|
const int32_t HYUNDAI_RT_INTERVAL = 250000; // 250ms between real time checks
|
2018-08-28 21:05:58 -06:00
|
|
|
const int HYUNDAI_MAX_RATE_UP = 3;
|
|
|
|
const int HYUNDAI_MAX_RATE_DOWN = 7;
|
2018-08-28 17:01:40 -06:00
|
|
|
const int HYUNDAI_DRIVER_TORQUE_ALLOWANCE = 50;
|
2018-08-28 21:05:58 -06:00
|
|
|
const int HYUNDAI_DRIVER_TORQUE_FACTOR = 2;
|
2018-08-17 22:31:00 -06:00
|
|
|
|
2018-08-28 17:01:40 -06:00
|
|
|
int hyundai_brake_prev = 0;
|
|
|
|
int hyundai_gas_prev = 0;
|
|
|
|
int hyundai_speed = 0;
|
|
|
|
int hyundai_camera_detected = 0;
|
|
|
|
int hyundai_rt_torque_last = 0;
|
|
|
|
int hyundai_desired_torque_last = 0;
|
|
|
|
int hyundai_cruise_engaged_last = 0;
|
|
|
|
uint32_t hyundai_ts_last = 0;
|
|
|
|
struct sample_t hyundai_torque_driver; // last few driver torques measured
|
2018-08-17 22:31:00 -06:00
|
|
|
|
|
|
|
static void hyundai_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
|
2018-08-28 17:01:40 -06:00
|
|
|
int bus_number = (to_push->RDTR >> 4) & 0xFF;
|
|
|
|
uint32_t addr;
|
|
|
|
if (to_push->RIR & 4) {
|
|
|
|
// Extended
|
|
|
|
// Not looked at, but have to be separated
|
|
|
|
// to avoid address collision
|
|
|
|
addr = to_push->RIR >> 3;
|
|
|
|
} else {
|
|
|
|
// Normal
|
|
|
|
addr = to_push->RIR >> 21;
|
|
|
|
}
|
2018-08-17 22:31:00 -06:00
|
|
|
|
2018-08-28 17:01:40 -06:00
|
|
|
if (addr == 897) {
|
2018-08-29 20:23:49 -06:00
|
|
|
int torque_driver_new = ((to_push->RDLR >> 11) & 0xfff) - 2048;
|
2018-08-28 17:01:40 -06:00
|
|
|
// update array of samples
|
|
|
|
update_sample(&hyundai_torque_driver, torque_driver_new);
|
2018-08-17 22:31:00 -06:00
|
|
|
}
|
|
|
|
|
2018-08-28 17:01:40 -06:00
|
|
|
// check if stock camera ECU is still online
|
|
|
|
if (bus_number == 0 && addr == 832) {
|
|
|
|
hyundai_camera_detected = 1;
|
|
|
|
controls_allowed = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// enter controls on rising edge of ACC, exit controls on ACC off
|
|
|
|
if ((to_push->RIR>>21) == 1057) {
|
|
|
|
// 2 bits: 13-14
|
|
|
|
int cruise_engaged = (to_push->RDLR >> 13) & 0x3;
|
|
|
|
if (cruise_engaged && !hyundai_cruise_engaged_last) {
|
|
|
|
controls_allowed = 1;
|
|
|
|
} else if (!cruise_engaged) {
|
|
|
|
controls_allowed = 0;
|
|
|
|
}
|
|
|
|
hyundai_cruise_engaged_last = cruise_engaged;
|
|
|
|
}
|
2018-08-17 22:31:00 -06:00
|
|
|
}
|
|
|
|
|
2018-08-28 17:01:40 -06:00
|
|
|
static int hyundai_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
|
|
|
|
|
|
|
|
// There can be only one! (camera)
|
|
|
|
if (hyundai_camera_detected) {
|
|
|
|
return 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 = hyundai_gas_prev || (hyundai_brake_prev && hyundai_speed);
|
|
|
|
int current_controls_allowed = controls_allowed && !pedal_pressed;
|
|
|
|
|
|
|
|
uint32_t addr;
|
|
|
|
if (to_send->RIR & 4) {
|
|
|
|
// Extended
|
|
|
|
addr = to_send->RIR >> 3;
|
|
|
|
} else {
|
|
|
|
// Normal
|
|
|
|
addr = to_send->RIR >> 21;
|
|
|
|
}
|
|
|
|
|
|
|
|
// LKA STEER: safety check
|
|
|
|
if (addr == 832) {
|
2018-08-29 20:23:49 -06:00
|
|
|
int desired_torque = ((to_send->RDLR >> 16) & 0x7ff) - 1024;
|
2018-08-28 17:01:40 -06:00
|
|
|
uint32_t ts = TIM2->CNT;
|
|
|
|
int violation = 0;
|
|
|
|
|
|
|
|
if (current_controls_allowed) {
|
|
|
|
|
|
|
|
// *** global torque limit check ***
|
|
|
|
violation |= max_limit_check(desired_torque, HYUNDAI_MAX_STEER, -HYUNDAI_MAX_STEER);
|
|
|
|
|
|
|
|
// *** torque rate limit check ***
|
|
|
|
violation |= driver_limit_check(desired_torque, hyundai_desired_torque_last, &hyundai_torque_driver,
|
|
|
|
HYUNDAI_MAX_STEER, HYUNDAI_MAX_RATE_UP, HYUNDAI_MAX_RATE_DOWN,
|
|
|
|
HYUNDAI_DRIVER_TORQUE_ALLOWANCE, HYUNDAI_DRIVER_TORQUE_FACTOR);
|
|
|
|
|
|
|
|
// used next time
|
|
|
|
hyundai_desired_torque_last = desired_torque;
|
2018-08-17 22:31:00 -06:00
|
|
|
|
2018-08-28 17:01:40 -06:00
|
|
|
// *** torque real time rate limit check ***
|
|
|
|
violation |= rt_rate_limit_check(desired_torque, hyundai_rt_torque_last, HYUNDAI_MAX_RT_DELTA);
|
|
|
|
|
|
|
|
// every RT_INTERVAL set the new limits
|
|
|
|
uint32_t ts_elapsed = get_ts_elapsed(ts, hyundai_ts_last);
|
|
|
|
if (ts_elapsed > HYUNDAI_RT_INTERVAL) {
|
|
|
|
hyundai_rt_torque_last = desired_torque;
|
|
|
|
hyundai_ts_last = ts;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// no torque if controls is not allowed
|
|
|
|
if (!current_controls_allowed && (desired_torque != 0)) {
|
|
|
|
violation = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// reset to 0 if either controls is not allowed or there's a violation
|
|
|
|
if (violation || !current_controls_allowed) {
|
|
|
|
hyundai_desired_torque_last = 0;
|
|
|
|
hyundai_rt_torque_last = 0;
|
|
|
|
hyundai_ts_last = ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (violation) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-17 22:31:00 -06:00
|
|
|
}
|
2018-08-28 17:01:40 -06:00
|
|
|
|
|
|
|
// 1 allows the message through
|
|
|
|
return true;
|
2018-08-17 22:31:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
const safety_hooks hyundai_hooks = {
|
2018-08-28 17:01:40 -06:00
|
|
|
.init = nooutput_init,
|
2018-08-17 22:31:00 -06:00
|
|
|
.rx = hyundai_rx_hook,
|
2018-08-28 17:01:40 -06:00
|
|
|
.tx = hyundai_tx_hook,
|
2018-08-17 22:31:00 -06:00
|
|
|
.tx_lin = nooutput_tx_lin_hook,
|
|
|
|
.ignition = default_ign_hook,
|
2018-08-28 17:01:40 -06:00
|
|
|
.fwd = nooutput_fwd_hook,
|
2018-08-17 22:31:00 -06:00
|
|
|
};
|