2019-06-12 21:35:47 +08:00
|
|
|
// include first, needed by safety policies
|
|
|
|
#include "safety_declarations.h"
|
2017-07-15 12:17:32 +08:00
|
|
|
// Include the actual safety policies.
|
2017-07-18 01:20:08 +08:00
|
|
|
#include "safety/safety_defaults.h"
|
|
|
|
#include "safety/safety_honda.h"
|
2017-08-25 13:31:34 +08:00
|
|
|
#include "safety/safety_toyota.h"
|
2018-04-12 05:31:45 +08:00
|
|
|
#include "safety/safety_toyota_ipas.h"
|
2018-10-05 01:22:10 +08:00
|
|
|
#include "safety/safety_tesla.h"
|
2019-02-06 10:37:02 +08:00
|
|
|
#include "safety/safety_gm_ascm.h"
|
2018-02-23 16:19:22 +08:00
|
|
|
#include "safety/safety_gm.h"
|
2018-05-03 13:45:23 +08:00
|
|
|
#include "safety/safety_ford.h"
|
2018-05-26 01:36:25 +08:00
|
|
|
#include "safety/safety_cadillac.h"
|
2018-08-18 12:31:00 +08:00
|
|
|
#include "safety/safety_hyundai.h"
|
2018-11-07 04:28:33 +08:00
|
|
|
#include "safety/safety_chrysler.h"
|
2019-01-29 13:42:06 +08:00
|
|
|
#include "safety/safety_subaru.h"
|
2019-09-10 06:58:41 +08:00
|
|
|
#include "safety/safety_mazda.h"
|
2019-10-10 04:54:22 +08:00
|
|
|
#include "safety/safety_volkswagen.h"
|
2017-08-12 07:17:43 +08:00
|
|
|
#include "safety/safety_elm327.h"
|
2017-07-15 12:17:32 +08:00
|
|
|
|
2019-10-05 04:30:00 +08:00
|
|
|
// from cereal.car.CarParams.SafetyModel
|
2019-11-21 03:56:26 +08:00
|
|
|
#define SAFETY_SILENT 0U
|
2019-10-05 04:30:00 +08:00
|
|
|
#define SAFETY_HONDA 1U
|
|
|
|
#define SAFETY_TOYOTA 2U
|
|
|
|
#define SAFETY_ELM327 3U
|
|
|
|
#define SAFETY_GM 4U
|
|
|
|
#define SAFETY_HONDA_BOSCH 5U
|
|
|
|
#define SAFETY_FORD 6U
|
|
|
|
#define SAFETY_CADILLAC 7U
|
|
|
|
#define SAFETY_HYUNDAI 8U
|
|
|
|
#define SAFETY_CHRYSLER 9U
|
|
|
|
#define SAFETY_TESLA 10U
|
|
|
|
#define SAFETY_SUBARU 11U
|
|
|
|
#define SAFETY_MAZDA 13U
|
2019-10-10 04:54:22 +08:00
|
|
|
#define SAFETY_VOLKSWAGEN 15U
|
2019-10-05 04:30:00 +08:00
|
|
|
#define SAFETY_TOYOTA_IPAS 16U
|
|
|
|
#define SAFETY_ALLOUTPUT 17U
|
|
|
|
#define SAFETY_GM_ASCM 18U
|
2019-11-21 03:56:26 +08:00
|
|
|
#define SAFETY_NOOUTPUT 19U
|
2019-10-05 04:30:00 +08:00
|
|
|
|
2019-11-21 03:56:26 +08:00
|
|
|
uint16_t current_safety_mode = SAFETY_SILENT;
|
2017-07-15 12:17:32 +08:00
|
|
|
const safety_hooks *current_hooks = &nooutput_hooks;
|
|
|
|
|
|
|
|
void safety_rx_hook(CAN_FIFOMailBox_TypeDef *to_push){
|
|
|
|
current_hooks->rx(to_push);
|
|
|
|
}
|
|
|
|
|
2017-07-21 14:36:06 +08:00
|
|
|
int safety_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
|
|
|
|
return current_hooks->tx(to_send);
|
2017-07-15 12:17:32 +08:00
|
|
|
}
|
|
|
|
|
2017-07-21 14:36:06 +08:00
|
|
|
int safety_tx_lin_hook(int lin_num, uint8_t *data, int len){
|
|
|
|
return current_hooks->tx_lin(lin_num, data, len);
|
2017-07-15 12:17:32 +08:00
|
|
|
}
|
|
|
|
|
2018-02-28 15:27:47 +08:00
|
|
|
int safety_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {
|
|
|
|
return current_hooks->fwd(bus_num, to_fwd);
|
|
|
|
}
|
|
|
|
|
2019-11-17 16:24:19 +08:00
|
|
|
bool addr_allowed(int addr, int bus, const AddrBus addr_list[], int len) {
|
|
|
|
bool allowed = false;
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
if ((addr == addr_list[i].addr) && (bus == addr_list[i].bus)) {
|
|
|
|
allowed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return allowed;
|
|
|
|
}
|
|
|
|
|
2017-07-15 12:17:32 +08:00
|
|
|
typedef struct {
|
|
|
|
uint16_t id;
|
|
|
|
const safety_hooks *hooks;
|
|
|
|
} safety_hook_config;
|
|
|
|
|
|
|
|
const safety_hook_config safety_hook_registry[] = {
|
2019-11-21 03:56:26 +08:00
|
|
|
{SAFETY_SILENT, &nooutput_hooks},
|
2017-07-18 01:48:16 +08:00
|
|
|
{SAFETY_HONDA, &honda_hooks},
|
2017-08-25 13:31:34 +08:00
|
|
|
{SAFETY_TOYOTA, &toyota_hooks},
|
2019-10-03 09:20:32 +08:00
|
|
|
{SAFETY_ELM327, &elm327_hooks},
|
2018-05-03 13:45:23 +08:00
|
|
|
{SAFETY_GM, &gm_hooks},
|
2019-10-03 09:20:32 +08:00
|
|
|
{SAFETY_HONDA_BOSCH, &honda_bosch_hooks},
|
2018-08-18 12:31:00 +08:00
|
|
|
{SAFETY_HYUNDAI, &hyundai_hooks},
|
2018-11-07 04:28:33 +08:00
|
|
|
{SAFETY_CHRYSLER, &chrysler_hooks},
|
2019-01-29 13:42:06 +08:00
|
|
|
{SAFETY_SUBARU, &subaru_hooks},
|
2019-09-10 06:58:41 +08:00
|
|
|
{SAFETY_MAZDA, &mazda_hooks},
|
2019-10-10 04:54:22 +08:00
|
|
|
{SAFETY_VOLKSWAGEN, &volkswagen_hooks},
|
2019-11-21 03:56:26 +08:00
|
|
|
{SAFETY_NOOUTPUT, &nooutput_hooks},
|
2019-11-16 04:20:15 +08:00
|
|
|
#ifdef ALLOW_DEBUG
|
|
|
|
{SAFETY_CADILLAC, &cadillac_hooks},
|
2018-04-12 05:31:45 +08:00
|
|
|
{SAFETY_TOYOTA_IPAS, &toyota_ipas_hooks},
|
2019-11-16 04:20:15 +08:00
|
|
|
{SAFETY_TESLA, &tesla_hooks},
|
2017-07-18 01:48:16 +08:00
|
|
|
{SAFETY_ALLOUTPUT, &alloutput_hooks},
|
2019-10-03 09:20:32 +08:00
|
|
|
{SAFETY_GM_ASCM, &gm_ascm_hooks},
|
2019-11-16 04:20:15 +08:00
|
|
|
{SAFETY_FORD, &ford_hooks},
|
|
|
|
#endif
|
2017-07-15 12:17:32 +08:00
|
|
|
};
|
|
|
|
|
2019-11-07 06:30:22 +08:00
|
|
|
int set_safety_hooks(uint16_t mode, int16_t param) {
|
2019-06-13 09:18:07 +08:00
|
|
|
int set_status = -1; // not set
|
2019-06-15 01:57:14 +08:00
|
|
|
int hook_config_count = sizeof(safety_hook_registry) / sizeof(safety_hook_config);
|
|
|
|
for (int i = 0; i < hook_config_count; i++) {
|
2017-07-18 01:27:34 +08:00
|
|
|
if (safety_hook_registry[i].id == mode) {
|
2017-07-15 12:17:32 +08:00
|
|
|
current_hooks = safety_hook_registry[i].hooks;
|
2019-10-05 04:30:00 +08:00
|
|
|
current_safety_mode = safety_hook_registry[i].id;
|
2019-06-13 09:18:07 +08:00
|
|
|
set_status = 0; // set
|
|
|
|
break;
|
2017-07-15 12:17:32 +08:00
|
|
|
}
|
|
|
|
}
|
2019-06-13 09:18:07 +08:00
|
|
|
if ((set_status == 0) && (current_hooks->init != NULL)) {
|
|
|
|
current_hooks->init(param);
|
|
|
|
}
|
|
|
|
return set_status;
|
2017-07-15 12:17:32 +08:00
|
|
|
}
|
2017-08-24 03:49:56 +08:00
|
|
|
|
2018-05-26 04:57:19 +08:00
|
|
|
// compute the time elapsed (in microseconds) from 2 counter samples
|
2019-06-11 17:07:02 +08:00
|
|
|
// case where ts < ts_last is ok: overflow is properly re-casted into uint32_t
|
2018-05-26 04:57:19 +08:00
|
|
|
uint32_t get_ts_elapsed(uint32_t ts, uint32_t ts_last) {
|
2019-06-11 17:07:02 +08:00
|
|
|
return ts - ts_last;
|
2018-05-26 04:57:19 +08:00
|
|
|
}
|
2018-05-26 08:52:37 +08:00
|
|
|
|
|
|
|
// convert a trimmed integer to signed 32 bit int
|
|
|
|
int to_signed(int d, int bits) {
|
2019-06-11 15:58:22 +08:00
|
|
|
int d_signed = d;
|
2019-06-18 04:48:14 +08:00
|
|
|
if (d >= (1 << MAX((bits - 1), 0))) {
|
|
|
|
d_signed = d - (1 << MAX(bits, 0));
|
2018-05-26 08:52:37 +08:00
|
|
|
}
|
2019-06-11 15:58:22 +08:00
|
|
|
return d_signed;
|
2018-05-26 08:52:37 +08:00
|
|
|
}
|
2018-06-01 09:49:12 +08:00
|
|
|
|
|
|
|
// given a new sample, update the smaple_t struct
|
|
|
|
void update_sample(struct sample_t *sample, int sample_new) {
|
2019-06-15 01:57:14 +08:00
|
|
|
int sample_size = sizeof(sample->values) / sizeof(sample->values[0]);
|
|
|
|
for (int i = sample_size - 1; i > 0; i--) {
|
2018-06-01 09:49:12 +08:00
|
|
|
sample->values[i] = sample->values[i-1];
|
|
|
|
}
|
|
|
|
sample->values[0] = sample_new;
|
|
|
|
|
|
|
|
// get the minimum and maximum measured samples
|
2019-06-11 16:10:45 +08:00
|
|
|
sample->min = sample->values[0];
|
|
|
|
sample->max = sample->values[0];
|
2019-06-15 01:57:14 +08:00
|
|
|
for (int i = 1; i < sample_size; i++) {
|
2019-06-11 16:07:41 +08:00
|
|
|
if (sample->values[i] < sample->min) {
|
|
|
|
sample->min = sample->values[i];
|
|
|
|
}
|
|
|
|
if (sample->values[i] > sample->max) {
|
|
|
|
sample->max = sample->values[i];
|
|
|
|
}
|
2018-06-01 09:49:12 +08:00
|
|
|
}
|
|
|
|
}
|
2018-06-13 15:56:29 +08:00
|
|
|
|
2019-06-18 09:48:38 +08:00
|
|
|
bool max_limit_check(int val, const int MAX_VAL, const int MIN_VAL) {
|
|
|
|
return (val > MAX_VAL) || (val < MIN_VAL);
|
2018-06-13 16:37:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// check that commanded value isn't too far from measured
|
2019-06-13 11:12:48 +08:00
|
|
|
bool dist_to_meas_check(int val, int val_last, struct sample_t *val_meas,
|
2018-06-13 16:37:36 +08:00
|
|
|
const int MAX_RATE_UP, const int MAX_RATE_DOWN, const int MAX_ERROR) {
|
|
|
|
|
|
|
|
// *** val rate limit check ***
|
2019-06-18 06:29:04 +08:00
|
|
|
int highest_allowed_rl = MAX(val_last, 0) + MAX_RATE_UP;
|
|
|
|
int lowest_allowed_rl = MIN(val_last, 0) - MAX_RATE_UP;
|
2018-06-13 16:37:36 +08:00
|
|
|
|
|
|
|
// if we've exceeded the meas val, we must start moving toward 0
|
2019-06-18 06:29:04 +08:00
|
|
|
int highest_allowed = MIN(highest_allowed_rl, MAX(val_last - MAX_RATE_DOWN, MAX(val_meas->max, 0) + MAX_ERROR));
|
|
|
|
int lowest_allowed = MAX(lowest_allowed_rl, MIN(val_last + MAX_RATE_DOWN, MIN(val_meas->min, 0) - MAX_ERROR));
|
2018-06-13 16:37:36 +08:00
|
|
|
|
|
|
|
// check for violation
|
2019-06-18 06:29:04 +08:00
|
|
|
return (val < lowest_allowed) || (val > highest_allowed);
|
2018-06-13 16:37:36 +08:00
|
|
|
}
|
|
|
|
|
2018-06-14 06:23:56 +08:00
|
|
|
// check that commanded value isn't fighting against driver
|
2019-06-13 11:12:48 +08:00
|
|
|
bool driver_limit_check(int val, int val_last, struct sample_t *val_driver,
|
2019-06-18 09:48:38 +08:00
|
|
|
const int MAX_VAL, const int MAX_RATE_UP, const int MAX_RATE_DOWN,
|
2018-06-14 06:23:56 +08:00
|
|
|
const int MAX_ALLOWANCE, const int DRIVER_FACTOR) {
|
|
|
|
|
2019-06-18 06:29:04 +08:00
|
|
|
int highest_allowed_rl = MAX(val_last, 0) + MAX_RATE_UP;
|
|
|
|
int lowest_allowed_rl = MIN(val_last, 0) - MAX_RATE_UP;
|
2018-06-14 06:23:56 +08:00
|
|
|
|
2019-06-18 09:48:38 +08:00
|
|
|
int driver_max_limit = MAX_VAL + (MAX_ALLOWANCE + val_driver->max) * DRIVER_FACTOR;
|
|
|
|
int driver_min_limit = -MAX_VAL + (-MAX_ALLOWANCE + val_driver->min) * DRIVER_FACTOR;
|
2018-06-14 06:23:56 +08:00
|
|
|
|
|
|
|
// if we've exceeded the applied torque, we must start moving toward 0
|
2019-06-18 06:29:04 +08:00
|
|
|
int highest_allowed = MIN(highest_allowed_rl, MAX(val_last - MAX_RATE_DOWN,
|
2019-06-18 04:48:14 +08:00
|
|
|
MAX(driver_max_limit, 0)));
|
2019-06-18 06:29:04 +08:00
|
|
|
int lowest_allowed = MAX(lowest_allowed_rl, MIN(val_last + MAX_RATE_DOWN,
|
2019-06-18 04:48:14 +08:00
|
|
|
MIN(driver_min_limit, 0)));
|
2018-06-14 06:23:56 +08:00
|
|
|
|
|
|
|
// check for violation
|
|
|
|
return (val < lowest_allowed) || (val > highest_allowed);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-13 15:56:29 +08:00
|
|
|
// real time check, mainly used for steer torque rate limiter
|
2019-06-13 11:12:48 +08:00
|
|
|
bool rt_rate_limit_check(int val, int val_last, const int MAX_RT_DELTA) {
|
2018-06-13 15:56:29 +08:00
|
|
|
|
2018-06-13 15:57:36 +08:00
|
|
|
// *** torque real time rate limit check ***
|
2019-06-18 04:48:14 +08:00
|
|
|
int highest_val = MAX(val_last, 0) + MAX_RT_DELTA;
|
|
|
|
int lowest_val = MIN(val_last, 0) - MAX_RT_DELTA;
|
2018-06-13 15:56:29 +08:00
|
|
|
|
2018-06-13 16:37:36 +08:00
|
|
|
// check for violation
|
2018-06-13 15:57:36 +08:00
|
|
|
return (val < lowest_val) || (val > highest_val);
|
2018-06-13 15:56:29 +08:00
|
|
|
}
|
2018-08-14 13:52:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
// interp function that holds extreme values
|
|
|
|
float interpolate(struct lookup_t xy, float x) {
|
2019-06-13 09:18:07 +08:00
|
|
|
|
2018-08-14 13:52:31 +08:00
|
|
|
int size = sizeof(xy.x) / sizeof(xy.x[0]);
|
2019-06-13 09:18:07 +08:00
|
|
|
float ret = xy.y[size - 1]; // default output is last point
|
|
|
|
|
2018-08-14 13:52:31 +08:00
|
|
|
// x is lower than the first point in the x array. Return the first point
|
|
|
|
if (x <= xy.x[0]) {
|
2019-06-13 09:18:07 +08:00
|
|
|
ret = xy.y[0];
|
2018-08-14 13:52:31 +08:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// find the index such that (xy.x[i] <= x < xy.x[i+1]) and linearly interp
|
2019-06-12 12:23:48 +08:00
|
|
|
for (int i=0; i < (size - 1); i++) {
|
2018-08-14 13:52:31 +08:00
|
|
|
if (x < xy.x[i+1]) {
|
|
|
|
float x0 = xy.x[i];
|
|
|
|
float y0 = xy.y[i];
|
|
|
|
float dx = xy.x[i+1] - x0;
|
|
|
|
float dy = xy.y[i+1] - y0;
|
|
|
|
// dx should not be zero as xy.x is supposed ot be monotonic
|
2019-06-11 16:07:41 +08:00
|
|
|
if (dx <= 0.) {
|
|
|
|
dx = 0.0001;
|
|
|
|
}
|
2019-06-13 09:18:07 +08:00
|
|
|
ret = (dy * (x - x0) / dx) + y0;
|
|
|
|
break;
|
2018-08-14 13:52:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-13 09:18:07 +08:00
|
|
|
return ret;
|
2018-08-14 13:52:31 +08:00
|
|
|
}
|