@@ -11,11 +11,18 @@ const int CADILLAC_DRIVER_TORQUE_FACTOR = 4;
11
11
int cadillac_ign = 0 ;
12
12
int cadillac_cruise_engaged_last = 0 ;
13
13
int cadillac_rt_torque_last = 0 ;
14
- int cadillac_desired_torque_last = 0 ;
14
+ int cadillac_desired_torque_last [ 4 ] = { 0 }; // 4 torque messages
15
15
uint32_t cadillac_ts_last = 0 ;
16
16
17
17
struct sample_t cadillac_torque_driver ; // last 3 driver torques measured
18
18
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
+
19
26
static void cadillac_rx_hook (CAN_FIFOMailBox_TypeDef * to_push ) {
20
27
int bus_number = (to_push -> RDTR >> 4 ) & 0xFF ;
21
28
uint32_t addr = to_push -> RIR >> 21 ;
@@ -53,6 +60,7 @@ static int cadillac_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
53
60
int desired_torque = ((to_send -> RDLR & 0x3f ) << 8 ) + ((to_send -> RDLR & 0xff00 ) >> 8 );
54
61
int violation = 0 ;
55
62
uint32_t ts = TIM2 -> CNT ;
63
+ int idx = cadillac_get_torque_idx (addr );
56
64
desired_torque = to_signed (desired_torque , 14 );
57
65
58
66
if (controls_allowed ) {
@@ -63,8 +71,8 @@ static int cadillac_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
63
71
}
64
72
65
73
// *** 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 ;
68
76
69
77
int driver_torque_max_limit = CADILLAC_STEER_MAX +
70
78
(CADILLAC_DRIVER_TORQUE_ALLOWANCE + cadillac_torque_driver .max ) *
@@ -75,10 +83,10 @@ static int cadillac_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
75
83
76
84
// if we've exceeded the applied torque, we must start moving toward 0
77
85
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 ,
79
87
max (driver_torque_max_limit , 0 )));
80
88
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 ,
82
90
min (driver_torque_min_limit , 0 )));
83
91
84
92
// check for violation
@@ -87,7 +95,7 @@ static int cadillac_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
87
95
}
88
96
89
97
//// used next time
90
- cadillac_desired_torque_last = desired_torque ;
98
+ cadillac_desired_torque_last [ idx ] = desired_torque ;
91
99
92
100
// *** torque real time rate limit check ***
93
101
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) {
114
122
115
123
// reset to 0 if either controls is not allowed or there's a violation
116
124
if (violation || !controls_allowed ) {
117
- cadillac_desired_torque_last = 0 ;
125
+ cadillac_desired_torque_last [ idx ] = 0 ;
118
126
cadillac_rt_torque_last = 0 ;
119
127
cadillac_ts_last = ts ;
120
128
}
0 commit comments