You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
172 lines
5.4 KiB
172 lines
5.4 KiB
7 years ago
|
struct sample_t torque_meas; // last 3 motor torques produced by the eps
|
||
7 years ago
|
|
||
|
// global torque limit
|
||
7 years ago
|
const int MAX_TORQUE = 1500; // max torque cmd allowed ever
|
||
7 years ago
|
|
||
|
// rate based torque limit + stay within actually applied
|
||
|
// packet is sent at 100hz, so this limit is 1000/sec
|
||
7 years ago
|
const int MAX_RATE_UP = 10; // ramp up slow
|
||
|
const int MAX_RATE_DOWN = 25; // ramp down fast
|
||
|
const int MAX_TORQUE_ERROR = 350; // max torque cmd in excess of torque motor
|
||
7 years ago
|
|
||
|
// real time torque limit to prevent controls spamming
|
||
|
// the real time limit is 1500/sec
|
||
7 years ago
|
const int MAX_RT_DELTA = 375; // max delta torque allowed for real time checks
|
||
|
const int RT_INTERVAL = 250000; // 250ms between real time checks
|
||
7 years ago
|
|
||
|
// longitudinal limits
|
||
7 years ago
|
const int MAX_ACCEL = 1500; // 1.5 m/s2
|
||
|
const int MIN_ACCEL = -3000; // 3.0 m/s2
|
||
7 years ago
|
|
||
|
// global actuation limit state
|
||
|
int actuation_limits = 1; // by default steer limits are imposed
|
||
7 years ago
|
int dbc_eps_torque_factor = 100; // conversion factor for STEER_TORQUE_EPS in %: see dbc file
|
||
7 years ago
|
|
||
|
// state of torque limits
|
||
7 years ago
|
int desired_torque_last = 0; // last desired steer torque
|
||
|
int rt_torque_last = 0; // last desired torque for real time check
|
||
7 years ago
|
uint32_t ts_last = 0;
|
||
7 years ago
|
int cruise_engaged_last = 0; // cruise state
|
||
|
|
||
7 years ago
|
|
||
|
static void toyota_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
|
||
|
// get eps motor torque (0.66 factor in dbc)
|
||
|
if ((to_push->RIR>>21) == 0x260) {
|
||
7 years ago
|
int torque_meas_new = (((to_push->RDHR) & 0xFF00) | ((to_push->RDHR >> 16) & 0xFF));
|
||
|
torque_meas_new = to_signed(torque_meas_new, 16);
|
||
|
|
||
|
// scale by dbc_factor
|
||
|
torque_meas_new = (torque_meas_new * dbc_eps_torque_factor) / 100;
|
||
7 years ago
|
|
||
|
// increase torque_meas by 1 to be conservative on rounding
|
||
7 years ago
|
torque_meas_new += (torque_meas_new > 0 ? 1 : -1);
|
||
7 years ago
|
|
||
7 years ago
|
// update array of sample
|
||
|
update_sample(&torque_meas, torque_meas_new);
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
// enter controls on rising edge of ACC, exit controls on ACC off
|
||
7 years ago
|
if ((to_push->RIR>>21) == 0x1D2) {
|
||
|
// 4 bits: 55-52
|
||
7 years ago
|
int cruise_engaged = to_push->RDHR & 0xF00000;
|
||
|
if (cruise_engaged && !cruise_engaged_last) {
|
||
7 years ago
|
controls_allowed = 1;
|
||
7 years ago
|
} else if (!cruise_engaged) {
|
||
7 years ago
|
controls_allowed = 0;
|
||
|
}
|
||
7 years ago
|
cruise_engaged_last = cruise_engaged;
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
|
static int toyota_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
|
||
|
|
||
|
// Check if msg is sent on BUS 0
|
||
|
if (((to_send->RDTR >> 4) & 0xF) == 0) {
|
||
|
|
||
7 years ago
|
// no IPAS in non IPAS mode
|
||
|
if (((to_send->RIR>>21) == 0x266) || ((to_send->RIR>>21) == 0x167)) return false;
|
||
|
|
||
7 years ago
|
// ACCEL: safety check on byte 1-2
|
||
|
if ((to_send->RIR>>21) == 0x343) {
|
||
7 years ago
|
int desired_accel = ((to_send->RDLR & 0xFF) << 8) | ((to_send->RDLR >> 8) & 0xFF);
|
||
|
desired_accel = to_signed(desired_accel, 16);
|
||
7 years ago
|
if (controls_allowed && actuation_limits) {
|
||
7 years ago
|
int violation = max_limit_check(desired_accel, MAX_ACCEL, MIN_ACCEL);
|
||
|
if (violation) return 0;
|
||
7 years ago
|
} else if (!controls_allowed && (desired_accel != 0)) {
|
||
|
return 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// STEER: safety check on bytes 2-3
|
||
|
if ((to_send->RIR>>21) == 0x2E4) {
|
||
7 years ago
|
int desired_torque = (to_send->RDLR & 0xFF00) | ((to_send->RDLR >> 16) & 0xFF);
|
||
|
desired_torque = to_signed(desired_torque, 16);
|
||
|
int violation = 0;
|
||
7 years ago
|
|
||
|
uint32_t ts = TIM2->CNT;
|
||
|
|
||
|
// only check if controls are allowed and actuation_limits are imposed
|
||
|
if (controls_allowed && actuation_limits) {
|
||
|
|
||
|
// *** global torque limit check ***
|
||
7 years ago
|
violation |= max_limit_check(desired_torque, MAX_TORQUE, -MAX_TORQUE);
|
||
7 years ago
|
|
||
|
// *** torque rate limit check ***
|
||
7 years ago
|
violation |= dist_to_meas_check(desired_torque, desired_torque_last, &torque_meas, MAX_RATE_UP, MAX_RATE_DOWN, MAX_TORQUE_ERROR);
|
||
7 years ago
|
|
||
|
// used next time
|
||
|
desired_torque_last = desired_torque;
|
||
|
|
||
|
// *** torque real time rate limit check ***
|
||
7 years ago
|
violation |= rt_rate_limit_check(desired_torque, rt_torque_last, MAX_RT_DELTA);
|
||
7 years ago
|
|
||
|
// every RT_INTERVAL set the new limits
|
||
7 years ago
|
uint32_t ts_elapsed = get_ts_elapsed(ts, ts_last);
|
||
7 years ago
|
if (ts_elapsed > RT_INTERVAL) {
|
||
|
rt_torque_last = desired_torque;
|
||
|
ts_last = ts;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// no torque if controls is not allowed
|
||
|
if (!controls_allowed && (desired_torque != 0)) {
|
||
|
violation = 1;
|
||
|
}
|
||
|
|
||
|
// reset to 0 if either controls is not allowed or there's a violation
|
||
|
if (violation || !controls_allowed) {
|
||
|
desired_torque_last = 0;
|
||
|
rt_torque_last = 0;
|
||
|
ts_last = ts;
|
||
|
}
|
||
|
|
||
|
if (violation) {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 1 allows the message through
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
static int toyota_tx_lin_hook(int lin_num, uint8_t *data, int len) {
|
||
|
// TODO: add safety if using LIN
|
||
|
return true;
|
||
|
}
|
||
|
|
||
7 years ago
|
static void toyota_init(int16_t param) {
|
||
7 years ago
|
controls_allowed = 0;
|
||
|
actuation_limits = 1;
|
||
7 years ago
|
dbc_eps_torque_factor = param;
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
static int toyota_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
7 years ago
|
const safety_hooks toyota_hooks = {
|
||
|
.init = toyota_init,
|
||
|
.rx = toyota_rx_hook,
|
||
|
.tx = toyota_tx_hook,
|
||
|
.tx_lin = toyota_tx_lin_hook,
|
||
7 years ago
|
.ignition = default_ign_hook,
|
||
7 years ago
|
.fwd = toyota_fwd_hook,
|
||
7 years ago
|
};
|
||
|
|
||
7 years ago
|
static void toyota_nolimits_init(int16_t param) {
|
||
7 years ago
|
controls_allowed = 0;
|
||
|
actuation_limits = 0;
|
||
7 years ago
|
dbc_eps_torque_factor = param;
|
||
7 years ago
|
}
|
||
|
|
||
|
const safety_hooks toyota_nolimits_hooks = {
|
||
|
.init = toyota_nolimits_init,
|
||
|
.rx = toyota_rx_hook,
|
||
|
.tx = toyota_tx_hook,
|
||
|
.tx_lin = toyota_tx_lin_hook,
|
||
7 years ago
|
.ignition = default_ign_hook,
|
||
7 years ago
|
.fwd = toyota_fwd_hook,
|
||
7 years ago
|
};
|