Skip to content

Commit 38a9ea9

Browse files
authored
added gm safety for steering (#123)
* added gm safety for steering * safety tests done for safety_gm
1 parent bf5db45 commit 38a9ea9

File tree

6 files changed

+378
-20
lines changed

6 files changed

+378
-20
lines changed

board/safety/safety_cadillac.h

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const int CADILLAC_STEER_MAX = 150; // 1s
1+
const int CADILLAC_MAX_STEER = 150; // 1s
22
// real time torque limit to prevent controls spamming
33
// the real time limit is 1500/sec
44
const int CADILLAC_MAX_RT_DELTA = 75; // max delta torque allowed for real time checks
@@ -14,8 +14,7 @@ int cadillac_rt_torque_last = 0;
1414
int cadillac_desired_torque_last[4] = {0}; // 4 torque messages
1515
uint32_t cadillac_ts_last = 0;
1616
int cadillac_supercruise_on = 0;
17-
18-
struct sample_t cadillac_torque_driver; // last 3 driver torques measured
17+
struct sample_t cadillac_torque_driver; // last few driver torques measured
1918

2019
int cadillac_get_torque_idx(uint32_t addr) {
2120
if (addr==0x151) return 0;
@@ -60,7 +59,7 @@ static void cadillac_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
6059
static int cadillac_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
6160
uint32_t addr = to_send->RIR >> 21;
6261

63-
// block steering cmd above 150
62+
// steer cmd checks
6463
if (addr == 0x151 || addr == 0x152 || addr == 0x153 || addr == 0x154) {
6564
int desired_torque = ((to_send->RDLR & 0x3f) << 8) + ((to_send->RDLR & 0xff00) >> 8);
6665
int violation = 0;
@@ -71,12 +70,12 @@ static int cadillac_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
7170
if (controls_allowed) {
7271

7372
// *** global torque limit check ***
74-
violation |= max_limit_check(desired_torque, CADILLAC_STEER_MAX);
73+
violation |= max_limit_check(desired_torque, CADILLAC_MAX_STEER);
7574

7675
// *** torque rate limit check ***
7776
int desired_torque_last = cadillac_desired_torque_last[idx];
7877
violation |= driver_limit_check(desired_torque, desired_torque_last, &cadillac_torque_driver,
79-
CADILLAC_STEER_MAX, CADILLAC_MAX_RATE_UP, CADILLAC_MAX_RATE_DOWN,
78+
CADILLAC_MAX_STEER, CADILLAC_MAX_RATE_UP, CADILLAC_MAX_RATE_DOWN,
8079
CADILLAC_DRIVER_TORQUE_ALLOWANCE, CADILLAC_DRIVER_TORQUE_FACTOR);
8180

8281
// used next time
@@ -87,7 +86,7 @@ static int cadillac_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
8786

8887
// every RT_INTERVAL set the new limits
8988
uint32_t ts_elapsed = get_ts_elapsed(ts, cadillac_ts_last);
90-
if (ts_elapsed > RT_INTERVAL) {
89+
if (ts_elapsed > CADILLAC_RT_INTERVAL) {
9190
cadillac_rt_torque_last = desired_torque;
9291
cadillac_ts_last = ts;
9392
}
@@ -98,8 +97,8 @@ static int cadillac_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
9897
violation = 1;
9998
}
10099

101-
// reset to 0 if either controls is not allowed or there's a violation
102-
if (violation || !controls_allowed) {
100+
// reset to 0 if either controls is not allowed or there's a violation
101+
if (violation || !controls_allowed) {
103102
cadillac_desired_torque_last[idx] = 0;
104103
cadillac_rt_torque_last = 0;
105104
cadillac_ts_last = ts;

board/safety/safety_gm.h

+61-9
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,24 @@
88
// brake rising edge
99
// brake > 0mph
1010

11-
// gm_: poor man's namespacing
11+
const int GM_MAX_STEER = 255;
12+
const int GM_MAX_RT_DELTA = 128; // max delta torque allowed for real time checks
13+
const int32_t GM_RT_INTERVAL = 250000; // 250ms between real time checks
14+
const int GM_MAX_RATE_UP = 7;
15+
const int GM_MAX_RATE_DOWN = 17;
16+
const int GM_DRIVER_TORQUE_ALLOWANCE = 50;
17+
const int GM_DRIVER_TORQUE_FACTOR = 4;
18+
1219
int gm_brake_prev = 0;
1320
int gm_gas_prev = 0;
1421
int gm_speed = 0;
15-
1622
// silence everything if stock ECUs are still online
1723
int gm_ascm_detected = 0;
18-
1924
int gm_ignition_started = 0;
25+
int gm_rt_torque_last = 0;
26+
int gm_desired_torque_last = 0;
27+
uint32_t gm_ts_last = 0;
28+
struct sample_t gm_torque_driver; // last few driver torques measured
2029

2130
static void gm_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
2231
int bus_number = (to_push->RDTR >> 4) & 0xFF;
@@ -31,6 +40,13 @@ static void gm_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
3140
addr = to_push->RIR >> 21;
3241
}
3342

43+
if (addr == 388) {
44+
int torque_driver_new = (((to_push->RDHR >> 16) & 0x7) << 8) | ((to_push->RDHR >> 24) & 0xFF);
45+
torque_driver_new = to_signed(torque_driver_new, 11);
46+
// update array of samples
47+
update_sample(&gm_torque_driver, torque_driver_new);
48+
}
49+
3450
if (addr == 0x1f1 && bus_number == 0) {
3551
//Bit 5 should be ignition "on"
3652
//Backup plan is Bit 2 (accessory power)
@@ -136,13 +152,49 @@ static int gm_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
136152
// LKA STEER: safety check
137153
if (addr == 384) {
138154
int rdlr = to_send->RDLR;
139-
int steer = ((rdlr & 0x7) << 8) + ((rdlr & 0xFF00) >> 8);
140-
steer = to_signed(steer, 11);
141-
int max_steer = 255;
155+
int desired_torque = ((rdlr & 0x7) << 8) + ((rdlr & 0xFF00) >> 8);
156+
uint32_t ts = TIM2->CNT;
157+
int violation = 0;
158+
desired_torque = to_signed(desired_torque, 11);
159+
142160
if (current_controls_allowed) {
143-
if ((steer > max_steer) || (steer < -max_steer)) return 0;
144-
} else {
145-
if (steer != 0) return 0;
161+
162+
// *** global torque limit check ***
163+
violation |= max_limit_check(desired_torque, GM_MAX_STEER);
164+
165+
// *** torque rate limit check ***
166+
violation |= driver_limit_check(desired_torque, gm_desired_torque_last, &gm_torque_driver,
167+
GM_MAX_STEER, GM_MAX_RATE_UP, GM_MAX_RATE_DOWN,
168+
GM_DRIVER_TORQUE_ALLOWANCE, GM_DRIVER_TORQUE_FACTOR);
169+
170+
// used next time
171+
gm_desired_torque_last = desired_torque;
172+
173+
// *** torque real time rate limit check ***
174+
violation |= rt_rate_limit_check(desired_torque, gm_rt_torque_last, GM_MAX_RT_DELTA);
175+
176+
// every RT_INTERVAL set the new limits
177+
uint32_t ts_elapsed = get_ts_elapsed(ts, gm_ts_last);
178+
if (ts_elapsed > GM_RT_INTERVAL) {
179+
gm_rt_torque_last = desired_torque;
180+
gm_ts_last = ts;
181+
}
182+
}
183+
184+
// no torque if controls is not allowed
185+
if (!current_controls_allowed && (desired_torque != 0)) {
186+
violation = 1;
187+
}
188+
189+
// reset to 0 if either controls is not allowed or there's a violation
190+
if (violation || !current_controls_allowed) {
191+
gm_desired_torque_last = 0;
192+
gm_rt_torque_last = 0;
193+
gm_ts_last = ts;
194+
}
195+
196+
if (violation) {
197+
return false;
146198
}
147199
}
148200

tests/safety/libpandasafety_py.py

+8
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
void set_timer(int t);
4141
void set_torque_meas(int min, int max);
4242
void set_cadillac_torque_driver(int min, int max);
43+
void set_gm_torque_driver(int min, int max);
4344
void set_rt_torque_last(int t);
4445
void set_desired_torque_last(int t);
4546
int get_torque_meas_min(void);
@@ -62,6 +63,13 @@
6263
void set_cadillac_desired_torque_last(int t);
6364
void set_cadillac_rt_torque_last(int t);
6465
66+
void init_tests_gm(void);
67+
void gm_init(int16_t param);
68+
void gm_rx_hook(CAN_FIFOMailBox_TypeDef *to_push);
69+
int gm_tx_hook(CAN_FIFOMailBox_TypeDef *to_send);
70+
void set_gm_desired_torque_last(int t);
71+
void set_gm_rt_torque_last(int t);
72+
6573
void toyota_ipas_rx_hook(CAN_FIFOMailBox_TypeDef *to_push);
6674
int toyota_ipas_tx_hook(CAN_FIFOMailBox_TypeDef *to_send);
6775

tests/safety/test.c

+24
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ typedef struct
2424

2525
struct sample_t torque_meas;
2626
struct sample_t cadillac_torque_driver;
27+
struct sample_t gm_torque_driver;
2728

2829
TIM_TypeDef timer;
2930
TIM_TypeDef *TIM2 = &timer;
@@ -69,6 +70,11 @@ void set_cadillac_torque_driver(int min, int max){
6970
cadillac_torque_driver.max = max;
7071
}
7172

73+
void set_gm_torque_driver(int min, int max){
74+
gm_torque_driver.min = min;
75+
gm_torque_driver.max = max;
76+
}
77+
7278
int get_torque_meas_min(void){
7379
return torque_meas.min;
7480
}
@@ -85,6 +91,10 @@ void set_cadillac_rt_torque_last(int t){
8591
cadillac_rt_torque_last = t;
8692
}
8793

94+
void set_gm_rt_torque_last(int t){
95+
gm_rt_torque_last = t;
96+
}
97+
8898
void set_desired_torque_last(int t){
8999
desired_torque_last = t;
90100
}
@@ -93,6 +103,11 @@ void set_cadillac_desired_torque_last(int t){
93103
for (int i = 0; i < 4; i++) cadillac_desired_torque_last[i] = t;
94104
}
95105

106+
void set_gm_desired_torque_last(int t){
107+
gm_desired_torque_last = t;
108+
}
109+
110+
96111
int get_ego_speed(void){
97112
return ego_speed;
98113
}
@@ -131,6 +146,15 @@ void init_tests_cadillac(void){
131146
set_timer(0);
132147
}
133148

149+
void init_tests_gm(void){
150+
gm_torque_driver.min = 0;
151+
gm_torque_driver.max = 0;
152+
gm_desired_torque_last = 0;
153+
gm_rt_torque_last = 0;
154+
gm_ts_last = 0;
155+
set_timer(0);
156+
}
157+
134158
void init_tests_honda(void){
135159
ego_speed = 0;
136160
gas_interceptor_detected = 0;

0 commit comments

Comments
 (0)