Skip to content

Commit bea5187

Browse files
committed
Cadillac: added max steer safety
1 parent dbc11a1 commit bea5187

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

board/safety.h

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ int safety_tx_hook(CAN_FIFOMailBox_TypeDef *to_send);
33
int safety_tx_lin_hook(int lin_num, uint8_t *data, int len);
44
int safety_ignition_hook();
55
uint32_t get_ts_elapsed(uint32_t ts, uint32_t ts_last);
6+
int to_signed(int d, int bits);
67

78
typedef void (*safety_hook_init)(int16_t param);
89
typedef void (*rx_hook)(CAN_FIFOMailBox_TypeDef *to_push);
@@ -109,3 +110,11 @@ int safety_set_mode(uint16_t mode, int16_t param) {
109110
uint32_t get_ts_elapsed(uint32_t ts, uint32_t ts_last) {
110111
return ts > ts_last ? ts - ts_last : (0xFFFFFFFF - ts_last) + 1 + ts;
111112
}
113+
114+
// convert a trimmed integer to signed 32 bit int
115+
int to_signed(int d, int bits) {
116+
if (d >= (1 << (bits - 1))) {
117+
d -= (1 << bits);
118+
}
119+
return d;
120+
}

board/safety/safety_cadillac.h

+32-3
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,49 @@
1+
const int STEER_MAX = 150; // 1s
12
const int CADILLAC_IGNITION_TIMEOUT = 1000000; // 1s
3+
24
int cadillac_can_seen = 0;
5+
int cadillac_cruise_engaged_last = 0;
36
uint32_t cadillac_ts_last = 0;
47

58
static void cadillac_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
69
int bus_number = (to_push->RDTR >> 4) & 0xFF;
710
uint32_t addr = to_push->RIR >> 21;
811

9-
if (addr == 0x135 && bus_number == 0) {
12+
if ((addr == 0x135) && (bus_number == 0)) {
1013
cadillac_can_seen = 1;
1114
cadillac_ts_last = TIM2->CNT; // reset timer when gear msg is received
1215
}
16+
17+
// enter controls on rising edge of ACC, exit controls on ACC off
18+
if ((addr == 0x370) && (bus_number == 0)) {
19+
int cruise_engaged = to_push->RDLR & 0x800000; // bit 23
20+
if (cruise_engaged && !cadillac_cruise_engaged_last) {
21+
controls_allowed = 1;
22+
} else if (!cruise_engaged) {
23+
controls_allowed = 0;
24+
}
25+
cadillac_cruise_engaged_last = cruise_engaged;
26+
}
27+
}
28+
29+
static int cadillac_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
30+
uint32_t addr = to_send->RIR >> 21;
31+
32+
// block steering cmd above 150
33+
if (addr == 0x151 || addr == 0x152 || addr == 0x153 || addr == 0x154) {
34+
int lkas_cmd = ((to_send->RDLR & 0x3f) << 8) + ((to_send->RDLR & 0xff00) >> 8);
35+
lkas_cmd = to_signed(lkas_cmd, 14);
36+
// block message is controls are allowed and lkas command exceeds max, or
37+
// if controls aren't allowed and lkas cmd isn't 0
38+
if (controls_allowed && ((lkas_cmd > STEER_MAX) || (lkas_cmd < -STEER_MAX))) {
39+
return 0;
40+
} else if (!controls_allowed && lkas_cmd) return 0;
41+
}
42+
return true;
1343
}
1444

1545
static void cadillac_init(int16_t param) {
1646
cadillac_can_seen = 0;
17-
controls_allowed = 1;
1847
}
1948

2049
static int cadillac_ign_hook() {
@@ -31,7 +60,7 @@ static int cadillac_ign_hook() {
3160
const safety_hooks cadillac_hooks = {
3261
.init = cadillac_init,
3362
.rx = cadillac_rx_hook,
34-
.tx = alloutput_tx_hook,
63+
.tx = cadillac_tx_hook,
3564
.tx_lin = alloutput_tx_lin_hook,
3665
.ignition = cadillac_ign_hook,
3766
.fwd = alloutput_fwd_hook,

board/safety/safety_toyota_ipas.h

-7
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,6 @@ uint32_t ts_angle_last = 0;
3535

3636
int controls_allowed_last = 0;
3737

38-
int to_signed(int d, int bits) {
39-
if (d >= (1 << (bits - 1))) {
40-
d -= (1 << bits);
41-
}
42-
return d;
43-
}
44-
4538
// interp function that holds extreme values
4639
float interpolate(struct lookup_t xy, float x) {
4740
int size = sizeof(xy.x) / sizeof(xy.x[0]);

0 commit comments

Comments
 (0)