Skip to content

Commit b0541a8

Browse files
author
Commaremote
committed
Cadillac: monitoring the 4 torque messages independently
1 parent cd1dba9 commit b0541a8

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

board/safety/safety_cadillac.h

+15-7
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,18 @@ const int CADILLAC_DRIVER_TORQUE_FACTOR = 4;
1111
int cadillac_ign = 0;
1212
int cadillac_cruise_engaged_last = 0;
1313
int cadillac_rt_torque_last = 0;
14-
int cadillac_desired_torque_last = 0;
14+
int cadillac_desired_torque_last[4] = {0}; // 4 torque messages
1515
uint32_t cadillac_ts_last = 0;
1616

1717
struct sample_t cadillac_torque_driver; // last 3 driver torques measured
1818

19+
int cadillac_get_torque_idx(uint32_t addr) {
20+
if (addr==0x151) return 0;
21+
else if (addr==0x152) return 1;
22+
else if (addr==0x153) return 2;
23+
else return 3;
24+
}
25+
1926
static void cadillac_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
2027
int bus_number = (to_push->RDTR >> 4) & 0xFF;
2128
uint32_t addr = to_push->RIR >> 21;
@@ -53,6 +60,7 @@ static int cadillac_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
5360
int desired_torque = ((to_send->RDLR & 0x3f) << 8) + ((to_send->RDLR & 0xff00) >> 8);
5461
int violation = 0;
5562
uint32_t ts = TIM2->CNT;
63+
int idx = cadillac_get_torque_idx(addr);
5664
desired_torque = to_signed(desired_torque, 14);
5765

5866
if (controls_allowed) {
@@ -63,8 +71,8 @@ static int cadillac_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
6371
}
6472

6573
// *** torque rate limit check ***
66-
int highest_allowed_torque = max(cadillac_desired_torque_last, 0) + CADILLAC_MAX_RATE_UP;
67-
int lowest_allowed_torque = min(cadillac_desired_torque_last, 0) - CADILLAC_MAX_RATE_UP;
74+
int highest_allowed_torque = max(cadillac_desired_torque_last[idx], 0) + CADILLAC_MAX_RATE_UP;
75+
int lowest_allowed_torque = min(cadillac_desired_torque_last[idx], 0) - CADILLAC_MAX_RATE_UP;
6876

6977
int driver_torque_max_limit = CADILLAC_STEER_MAX +
7078
(CADILLAC_DRIVER_TORQUE_ALLOWANCE + cadillac_torque_driver.max) *
@@ -75,10 +83,10 @@ static int cadillac_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
7583

7684
// if we've exceeded the applied torque, we must start moving toward 0
7785
highest_allowed_torque = min(highest_allowed_torque,
78-
max(cadillac_desired_torque_last - CADILLAC_MAX_RATE_DOWN,
86+
max(cadillac_desired_torque_last[idx] - CADILLAC_MAX_RATE_DOWN,
7987
max(driver_torque_max_limit, 0)));
8088
lowest_allowed_torque = max(lowest_allowed_torque,
81-
min(cadillac_desired_torque_last + CADILLAC_MAX_RATE_DOWN,
89+
min(cadillac_desired_torque_last[idx] + CADILLAC_MAX_RATE_DOWN,
8290
min(driver_torque_min_limit, 0)));
8391

8492
// check for violation
@@ -87,7 +95,7 @@ static int cadillac_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
8795
}
8896

8997
//// used next time
90-
cadillac_desired_torque_last = desired_torque;
98+
cadillac_desired_torque_last[idx] = desired_torque;
9199

92100
// *** torque real time rate limit check ***
93101
int highest_rt_torque = max(cadillac_rt_torque_last, 0) + CADILLAC_MAX_RT_DELTA;
@@ -114,7 +122,7 @@ static int cadillac_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
114122

115123
// reset to 0 if either controls is not allowed or there's a violation
116124
if (violation || !controls_allowed) {
117-
cadillac_desired_torque_last = 0;
125+
cadillac_desired_torque_last[idx] = 0;
118126
cadillac_rt_torque_last = 0;
119127
cadillac_ts_last = ts;
120128
}

tests/safety/test.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ void set_desired_torque_last(int t){
9090
}
9191

9292
void set_cadillac_desired_torque_last(int t){
93-
cadillac_desired_torque_last = t;
93+
for (int i = 0; i < 4; i++) cadillac_desired_torque_last[i] = t;
9494
}
9595

9696
int get_ego_speed(void){
@@ -117,7 +117,7 @@ void init_tests_toyota(void){
117117
void init_tests_cadillac(void){
118118
cadillac_torque_driver.min = 0;
119119
cadillac_torque_driver.max = 0;
120-
cadillac_desired_torque_last = 0;
120+
for (int i = 0; i < 4; i++) cadillac_desired_torque_last[i] = 0;
121121
cadillac_rt_torque_last = 0;
122122
cadillac_ts_last = 0;
123123
set_timer(0);

0 commit comments

Comments
 (0)