Skip to content

Commit cf3ecd6

Browse files
committed
Chrysler safety: re-using hyundai framework
1 parent 49ed9bc commit cf3ecd6

File tree

4 files changed

+257
-136
lines changed

4 files changed

+257
-136
lines changed

board/safety/safety_chrysler.h

+85-78
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
// board enforces
2-
// in-state
3-
// ACC is active (green)
4-
// out-state
5-
// brake pressed
6-
// stock LKAS ECU is online
7-
// ACC is not active (white or disabled)
8-
9-
// chrysler_: namespacing
10-
int chrysler_speed = 0;
11-
12-
// silence everything if stock ECUs are still online
13-
int chrysler_lkas_detected = 0;
1+
const int CHRYSLER_MAX_STEER = 230;
2+
const int CHRYSLER_MAX_RT_DELTA = 112; // max delta torque allowed for real time checks
3+
const int32_t CHRYSLER_RT_INTERVAL = 250000; // 250ms between real time checks
4+
const int CHRYSLER_MAX_RATE_UP = 3;
5+
const int CHRYSLER_MAX_RATE_DOWN = 7;
6+
const int CHRYSLER_DRIVER_TORQUE_ALLOWANCE = 0; // TODO
7+
const int CHRYSLER_DRIVER_TORQUE_FACTOR = 0; // TODO
8+
9+
int chrysler_camera_detected = 0;
10+
int chrysler_rt_torque_last = 0;
1411
int chrysler_desired_torque_last = 0;
12+
int chrysler_cruise_engaged_last = 0;
13+
uint32_t chrysler_ts_last = 0;
14+
struct sample_t chrysler_torque_driver; // last few driver torques measured
1515

1616
static void chrysler_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
17-
int bus_number = (to_push->RDTR >> 4) & 0xFF;
17+
int bus = (to_push->RDTR >> 4) & 0xFF;
1818
uint32_t addr;
1919
if (to_push->RIR & 4) {
2020
// Extended
@@ -26,40 +26,35 @@ static void chrysler_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
2626
addr = to_push->RIR >> 21;
2727
}
2828

29-
if (addr == 0x144 && bus_number == 0) {
30-
chrysler_speed = ((to_push->RDLR & 0xFF000000) >> 16) | (to_push->RDHR & 0xFF);
29+
// TODO
30+
if (addr == 544) {
31+
int torque_driver_new = 0;
32+
// update array of samples
33+
update_sample(&chrysler_torque_driver, torque_driver_new);
3134
}
3235

33-
// check if stock LKAS ECU is still online
34-
if (addr == 0x292 && bus_number == 0) {
35-
chrysler_lkas_detected = 1;
36-
controls_allowed = 0;
37-
}
38-
39-
// ["ACC_2"]['ACC_STATUS_2'] == 7 for active (green) Adaptive Cruise Control
40-
if (addr == 0x1f4 && bus_number == 0) {
41-
if (((to_push->RDLR & 0x380000) >> 19) == 7) {
36+
// enter controls on rising edge of ACC, exit controls on ACC off
37+
if (addr == 0x1f4) {
38+
int cruise_engaged = ((to_push->RDLR & 0x380000) >> 19) == 7;
39+
if (cruise_engaged && !chrysler_cruise_engaged_last) {
4240
controls_allowed = 1;
43-
} else {
41+
} else if (!cruise_engaged) {
4442
controls_allowed = 0;
4543
}
44+
chrysler_cruise_engaged_last = cruise_engaged;
4645
}
4746

48-
// exit controls on brake press by human
49-
if (addr == 0x140) {
50-
if (to_push->RDLR & 0x4) {
51-
controls_allowed = 0;
52-
}
47+
// check if stock camera ECU is still online
48+
if (bus == 0 && addr == 0x292) {
49+
chrysler_camera_detected = 1;
50+
controls_allowed = 0;
5351
}
5452
}
5553

56-
// if controls_allowed
57-
// allow steering up to limit
58-
// else
59-
// block all commands that produce actuation
6054
static int chrysler_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
61-
// There can be only one! (LKAS)
62-
if (chrysler_lkas_detected) {
55+
56+
// There can be only one! (camera)
57+
if (chrysler_camera_detected) {
6358
return 0;
6459
}
6560

@@ -72,65 +67,77 @@ static int chrysler_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
7267
addr = to_send->RIR >> 21;
7368
}
7469

70+
// LKA STEER: safety check
71+
72+
73+
7574
// LKA STEER: Too large of values cause the steering actuator ECU to silently
7675
// fault and no longer actuate the wheel until the car is rebooted.
7776
if (addr == 0x292) {
7877
int rdlr = to_send->RDLR;
79-
int straight = 1024;
80-
int steer = ((rdlr & 0x7) << 8) + ((rdlr & 0xFF00) >> 8) - straight;
81-
int max_steer = 230;
82-
int max_rate = 50; // ECU is fine with 100, but 3 is typical.
83-
if (steer > max_steer) {
84-
return false;
85-
}
86-
if (steer < -max_steer) {
87-
return false;
88-
}
89-
if (!controls_allowed && steer != 0) {
90-
// If controls not allowed, only allow steering to move closer to 0.
91-
if (chrysler_desired_torque_last == 0) {
92-
return false;
93-
}
94-
if ((chrysler_desired_torque_last > 0) && (steer >= chrysler_desired_torque_last)) {
95-
return false;
96-
}
97-
if ((chrysler_desired_torque_last < 0) && (steer <= chrysler_desired_torque_last)) {
98-
return false;
78+
int desired_torque = ((rdlr & 0x7) << 8) + ((rdlr & 0xFF00) >> 8) - 1024;
79+
uint32_t ts = TIM2->CNT;
80+
int violation = 0;
81+
82+
if (controls_allowed) {
83+
84+
// *** global torque limit check ***
85+
violation |= max_limit_check(desired_torque, CHRYSLER_MAX_STEER, -CHRYSLER_MAX_STEER);
86+
87+
// *** torque rate limit check ***
88+
violation |= driver_limit_check(desired_torque, chrysler_desired_torque_last, &chrysler_torque_driver,
89+
CHRYSLER_MAX_STEER, CHRYSLER_MAX_RATE_UP, CHRYSLER_MAX_RATE_DOWN,
90+
CHRYSLER_DRIVER_TORQUE_ALLOWANCE, CHRYSLER_DRIVER_TORQUE_FACTOR);
91+
92+
// used next time
93+
chrysler_desired_torque_last = desired_torque;
94+
95+
// *** torque real time rate limit check ***
96+
violation |= rt_rate_limit_check(desired_torque, chrysler_rt_torque_last, CHRYSLER_MAX_RT_DELTA);
97+
98+
// every RT_INTERVAL set the new limits
99+
uint32_t ts_elapsed = get_ts_elapsed(ts, chrysler_ts_last);
100+
if (ts_elapsed > CHRYSLER_RT_INTERVAL) {
101+
chrysler_rt_torque_last = desired_torque;
102+
chrysler_ts_last = ts;
99103
}
100104
}
101-
if (steer < (chrysler_desired_torque_last - max_rate)) {
102-
return false;
105+
106+
// no torque if controls is not allowed
107+
if (!controls_allowed && (desired_torque != 0)) {
108+
violation = 1;
109+
}
110+
111+
// reset to 0 if either controls is not allowed or there's a violation
112+
if (violation || !controls_allowed) {
113+
chrysler_desired_torque_last = 0;
114+
chrysler_rt_torque_last = 0;
115+
chrysler_ts_last = ts;
103116
}
104-
if (steer > (chrysler_desired_torque_last + max_rate)) {
117+
118+
if (violation) {
105119
return false;
106120
}
107-
108-
chrysler_desired_torque_last = steer;
109121
}
110122

123+
// FORCE CANCEL: safety check only relevant when spamming the cancel button.
124+
// ensuring that only the cancel button press is sent (VAL 4) when controls are off.
125+
// This avoids unintended engagements while still allowing resume spam
126+
// TODO: fix bug preventing the button msg to be fwd'd on bus 2
127+
//if (((to_send->RIR>>21) == 1265) && !controls_allowed && ((to_send->RDTR >> 4) & 0xFF) == 0) {
128+
// if ((to_send->RDLR & 0x7) != 4) return 0;
129+
//}
130+
111131
// 1 allows the message through
112132
return true;
113133
}
114134

115-
static int chrysler_tx_lin_hook(int lin_num, uint8_t *data, int len) {
116-
// LIN is not used.
117-
return false;
118-
}
119-
120-
static void chrysler_init(int16_t param) {
121-
controls_allowed = 0;
122-
}
123-
124-
static int chrysler_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {
125-
return -1;
126-
}
127135

128136
const safety_hooks chrysler_hooks = {
129-
.init = chrysler_init,
137+
.init = nooutput_init,
130138
.rx = chrysler_rx_hook,
131139
.tx = chrysler_tx_hook,
132-
.tx_lin = chrysler_tx_lin_hook,
140+
.tx_lin = nooutput_tx_lin_hook,
133141
.ignition = default_ign_hook,
134-
.fwd = chrysler_fwd_hook,
142+
.fwd = nooutput_fwd_hook,
135143
};
136-

tests/safety/libpandasafety_py.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
void set_cadillac_torque_driver(int min, int max);
4343
void set_gm_torque_driver(int min, int max);
4444
void set_hyundai_torque_driver(int min, int max);
45+
void set_chrysler_torque_driver(int min, int max);
4546
void set_toyota_rt_torque_last(int t);
4647
void set_toyota_desired_torque_last(int t);
4748
int get_toyota_torque_meas_min(void);
@@ -84,8 +85,9 @@
8485
void init_tests_chrysler(void);
8586
void chrysler_rx_hook(CAN_FIFOMailBox_TypeDef *to_push);
8687
int chrysler_tx_hook(CAN_FIFOMailBox_TypeDef *to_send);
87-
void chrysler_init(int16_t param);
8888
void set_chrysler_desired_torque_last(int t);
89+
void set_chrysler_rt_torque_last(int t);
90+
8991
9092
""")
9193

tests/safety/test.c

+19-5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ struct sample_t toyota_torque_meas;
2626
struct sample_t cadillac_torque_driver;
2727
struct sample_t gm_torque_driver;
2828
struct sample_t hyundai_torque_driver;
29+
struct sample_t chrysler_torque_driver;
2930

3031
TIM_TypeDef timer;
3132
TIM_TypeDef *TIM2 = &timer;
@@ -81,6 +82,11 @@ void set_hyundai_torque_driver(int min, int max){
8182
hyundai_torque_driver.max = max;
8283
}
8384

85+
void set_chrysler_torque_driver(int min, int max){
86+
chrysler_torque_driver.min = min;
87+
chrysler_torque_driver.max = max;
88+
}
89+
8490
int get_toyota_torque_meas_min(void){
8591
return toyota_torque_meas.min;
8692
}
@@ -105,6 +111,10 @@ void set_hyundai_rt_torque_last(int t){
105111
hyundai_rt_torque_last = t;
106112
}
107113

114+
void set_chrysler_rt_torque_last(int t){
115+
chrysler_rt_torque_last = t;
116+
}
117+
108118
void set_toyota_desired_torque_last(int t){
109119
toyota_desired_torque_last = t;
110120
}
@@ -181,18 +191,22 @@ void init_tests_hyundai(void){
181191
set_timer(0);
182192
}
183193

194+
void init_tests_chrysler(void){
195+
chrysler_torque_driver.min = 0;
196+
chrysler_torque_driver.max = 0;
197+
chrysler_desired_torque_last = 0;
198+
chrysler_rt_torque_last = 0;
199+
chrysler_ts_last = 0;
200+
set_timer(0);
201+
}
202+
184203
void init_tests_honda(void){
185204
ego_speed = 0;
186205
gas_interceptor_detected = 0;
187206
brake_prev = 0;
188207
gas_prev = 0;
189208
}
190209

191-
void init_tests_chrysler(void){
192-
chrysler_desired_torque_last = 0;
193-
set_timer(0);
194-
}
195-
196210
void set_gmlan_digital_output(int to_set){
197211
}
198212

0 commit comments

Comments
 (0)