2019-06-12 21:35:47 +08:00
|
|
|
// sample struct that keeps 3 samples in memory
|
|
|
|
struct sample_t {
|
|
|
|
int values[6];
|
|
|
|
int min;
|
|
|
|
int max;
|
|
|
|
} sample_t_default = {{0}, 0, 0};
|
|
|
|
|
|
|
|
// safety code requires floats
|
|
|
|
struct lookup_t {
|
|
|
|
float x[3];
|
|
|
|
float y[3];
|
|
|
|
};
|
|
|
|
|
2019-11-17 16:24:19 +08:00
|
|
|
typedef struct {
|
|
|
|
int addr;
|
|
|
|
int bus;
|
|
|
|
} AddrBus;
|
|
|
|
|
2019-06-12 21:35:47 +08:00
|
|
|
void safety_rx_hook(CAN_FIFOMailBox_TypeDef *to_push);
|
|
|
|
int safety_tx_hook(CAN_FIFOMailBox_TypeDef *to_send);
|
|
|
|
int safety_tx_lin_hook(int lin_num, uint8_t *data, int len);
|
|
|
|
uint32_t get_ts_elapsed(uint32_t ts, uint32_t ts_last);
|
|
|
|
int to_signed(int d, int bits);
|
|
|
|
void update_sample(struct sample_t *sample, int sample_new);
|
2019-06-13 11:12:48 +08:00
|
|
|
bool max_limit_check(int val, const int MAX, const int MIN);
|
|
|
|
bool dist_to_meas_check(int val, int val_last, struct sample_t *val_meas,
|
2019-06-12 21:35:47 +08:00
|
|
|
const int MAX_RATE_UP, const int MAX_RATE_DOWN, const int MAX_ERROR);
|
2019-06-13 11:12:48 +08:00
|
|
|
bool driver_limit_check(int val, int val_last, struct sample_t *val_driver,
|
2019-06-12 21:35:47 +08:00
|
|
|
const int MAX, const int MAX_RATE_UP, const int MAX_RATE_DOWN,
|
|
|
|
const int MAX_ALLOWANCE, const int DRIVER_FACTOR);
|
2019-06-13 11:12:48 +08:00
|
|
|
bool rt_rate_limit_check(int val, int val_last, const int MAX_RT_DELTA);
|
2019-06-12 21:35:47 +08:00
|
|
|
float interpolate(struct lookup_t xy, float x);
|
2019-12-18 16:17:25 +08:00
|
|
|
bool msg_allowed(int addr, int bus, const AddrBus addr_list[], int len);
|
2019-06-12 21:35:47 +08:00
|
|
|
|
|
|
|
typedef void (*safety_hook_init)(int16_t param);
|
|
|
|
typedef void (*rx_hook)(CAN_FIFOMailBox_TypeDef *to_push);
|
|
|
|
typedef int (*tx_hook)(CAN_FIFOMailBox_TypeDef *to_send);
|
|
|
|
typedef int (*tx_lin_hook)(int lin_num, uint8_t *data, int len);
|
|
|
|
typedef int (*fwd_hook)(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd);
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
safety_hook_init init;
|
|
|
|
rx_hook rx;
|
|
|
|
tx_hook tx;
|
|
|
|
tx_lin_hook tx_lin;
|
|
|
|
fwd_hook fwd;
|
|
|
|
} safety_hooks;
|
|
|
|
|
2019-11-15 16:32:45 +08:00
|
|
|
// This can be set by the safety hooks
|
|
|
|
bool controls_allowed = false;
|
|
|
|
bool relay_malfunction = false;
|
|
|
|
bool gas_interceptor_detected = false;
|
2019-06-12 21:35:47 +08:00
|
|
|
int gas_interceptor_prev = 0;
|
|
|
|
|
2019-11-27 13:19:54 +08:00
|
|
|
// time since safety mode has been changed
|
|
|
|
uint32_t safety_mode_cnt = 0U;
|
2019-11-27 16:19:41 +08:00
|
|
|
// allow 1s of transition timeout after relay changes state before assessing malfunctioning
|
|
|
|
const uint32_t RELAY_TRNS_TIMEOUT = 1U;
|
2019-11-27 13:19:54 +08:00
|
|
|
|
2019-07-16 04:15:22 +08:00
|
|
|
// avg between 2 tracks
|
|
|
|
#define GET_INTERCEPTOR(msg) (((GET_BYTE((msg), 0) << 8) + GET_BYTE((msg), 1) + ((GET_BYTE((msg), 2) << 8) + GET_BYTE((msg), 3)) / 2 ) / 2)
|