Skip to content

Commit 1966bdf

Browse files
author
Commaremote
committed
Safety: made real time rate limit check a shared function
1 parent e214477 commit 1966bdf

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

board/safety.h

+12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ int safety_ignition_hook();
1212
uint32_t get_ts_elapsed(uint32_t ts, uint32_t ts_last);
1313
int to_signed(int d, int bits);
1414
void update_sample(struct sample_t *sample, int sample_new);
15+
int rt_rate_limit_check(int val, int val_last, const int MAX_RT_DELTA);
1516

1617
typedef void (*safety_hook_init)(int16_t param);
1718
typedef void (*rx_hook)(CAN_FIFOMailBox_TypeDef *to_push);
@@ -141,3 +142,14 @@ void update_sample(struct sample_t *sample, int sample_new) {
141142
if (sample->values[i] > sample->max) sample->max = sample->values[i];
142143
}
143144
}
145+
146+
// real time check, mainly used for steer torque rate limiter
147+
int rt_rate_limit_check(int val, int val_last, const int MAX_RT_DELTA) {
148+
149+
// *** torque real time rate limit check ***
150+
int16_t highest_val = max(val_last, 0) + MAX_RT_DELTA;
151+
int16_t lowest_val = min(val_last, 0) - MAX_RT_DELTA;
152+
153+
// return 1 if violation
154+
return (val < lowest_val) || (val > highest_val);
155+
}

board/safety/safety_cadillac.h

+1-8
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,7 @@ static int cadillac_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
103103
cadillac_desired_torque_last[idx] = desired_torque;
104104

105105
// *** torque real time rate limit check ***
106-
int highest_rt_torque = max(cadillac_rt_torque_last, 0) + CADILLAC_MAX_RT_DELTA;
107-
int lowest_rt_torque = min(cadillac_rt_torque_last, 0) - CADILLAC_MAX_RT_DELTA;
108-
109-
110-
// check for violation
111-
if ((desired_torque < lowest_rt_torque) || (desired_torque > highest_rt_torque)) {
112-
violation = 1;
113-
}
106+
violation |= rt_rate_limit_check(desired_torque, cadillac_rt_torque_last, CADILLAC_MAX_RT_DELTA);
114107

115108
// every RT_INTERVAL set the new limits
116109
uint32_t ts_elapsed = get_ts_elapsed(ts, cadillac_ts_last);

board/safety/safety_toyota.h

+1-8
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,8 @@ static int toyota_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
108108
// used next time
109109
desired_torque_last = desired_torque;
110110

111-
112111
// *** torque real time rate limit check ***
113-
int16_t highest_rt_torque = max(rt_torque_last, 0) + MAX_RT_DELTA;
114-
int16_t lowest_rt_torque = min(rt_torque_last, 0) - MAX_RT_DELTA;
115-
116-
// check for violation
117-
if ((desired_torque < lowest_rt_torque) || (desired_torque > highest_rt_torque)) {
118-
violation = 1;
119-
}
112+
violation |= rt_rate_limit_check(desired_torque, rt_torque_last, MAX_RT_DELTA);
120113

121114
// every RT_INTERVAL set the new limits
122115
uint32_t ts_elapsed = get_ts_elapsed(ts, ts_last);

0 commit comments

Comments
 (0)