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 = 261 ;
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 = 3 ;
6
+ const int CHRYSLER_MAX_TORQUE_ERROR = 80 ; // max torque cmd in excess of torque motor
7
+
8
+ int chrysler_camera_detected = 0 ;
9
+ int chrysler_rt_torque_last = 0 ;
14
10
int chrysler_desired_torque_last = 0 ;
11
+ int chrysler_cruise_engaged_last = 0 ;
12
+ uint32_t chrysler_ts_last = 0 ;
13
+ struct sample_t chrysler_torque_meas ; // last few torques measured
15
14
16
15
static void chrysler_rx_hook (CAN_FIFOMailBox_TypeDef * to_push ) {
17
- int bus_number = (to_push -> RDTR >> 4 ) & 0xFF ;
16
+ int bus = (to_push -> RDTR >> 4 ) & 0xFF ;
18
17
uint32_t addr ;
19
18
if (to_push -> RIR & 4 ) {
20
19
// Extended
@@ -26,40 +25,37 @@ static void chrysler_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
26
25
addr = to_push -> RIR >> 21 ;
27
26
}
28
27
29
- if (addr == 0x144 && bus_number == 0 ) {
30
- chrysler_speed = ((to_push -> RDLR & 0xFF000000 ) >> 16 ) | (to_push -> RDHR & 0xFF );
31
- }
28
+ // Measured eps torque
29
+ if (addr == 544 ) {
30
+ int rdhr = to_push -> RDHR ;
31
+ int torque_meas_new = ((rdhr & 0x7 ) << 8 ) + ((rdhr & 0xFF00 ) >> 8 ) - 1024 ;
32
32
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 ;
33
+ // update array of samples
34
+ update_sample (& chrysler_torque_meas , torque_meas_new );
37
35
}
38
36
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 ) {
37
+ // enter controls on rising edge of ACC, exit controls on ACC off
38
+ if (addr == 0x1f4 ) {
39
+ int cruise_engaged = ((to_push -> RDLR & 0x380000 ) >> 19 ) == 7 ;
40
+ if (cruise_engaged && !chrysler_cruise_engaged_last ) {
42
41
controls_allowed = 1 ;
43
- } else {
42
+ } else if (! cruise_engaged ) {
44
43
controls_allowed = 0 ;
45
44
}
45
+ chrysler_cruise_engaged_last = cruise_engaged ;
46
46
}
47
47
48
- // exit controls on brake press by human
49
- if (addr == 0x140 ) {
50
- if (to_push -> RDLR & 0x4 ) {
51
- controls_allowed = 0 ;
52
- }
48
+ // check if stock camera ECU is still online
49
+ if (bus == 0 && addr == 0x292 ) {
50
+ chrysler_camera_detected = 1 ;
51
+ controls_allowed = 0 ;
53
52
}
54
53
}
55
54
56
- // if controls_allowed
57
- // allow steering up to limit
58
- // else
59
- // block all commands that produce actuation
60
55
static int chrysler_tx_hook (CAN_FIFOMailBox_TypeDef * to_send ) {
61
- // There can be only one! (LKAS)
62
- if (chrysler_lkas_detected ) {
56
+
57
+ // There can be only one! (camera)
58
+ if (chrysler_camera_detected ) {
63
59
return 0 ;
64
60
}
65
61
@@ -72,65 +68,69 @@ static int chrysler_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
72
68
addr = to_send -> RIR >> 21 ;
73
69
}
74
70
75
- // LKA STEER: Too large of values cause the steering actuator ECU to silently
76
- // fault and no longer actuate the wheel until the car is rebooted.
71
+
72
+ // LKA STEER
77
73
if (addr == 0x292 ) {
78
74
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;
75
+ int desired_torque = ((rdlr & 0x7 ) << 8 ) + ((rdlr & 0xFF00 ) >> 8 ) - 1024 ;
76
+ uint32_t ts = TIM2 -> CNT ;
77
+ int violation = 0 ;
78
+
79
+ if (controls_allowed ) {
80
+
81
+ // *** global torque limit check ***
82
+ violation |= max_limit_check (desired_torque , CHRYSLER_MAX_STEER , - CHRYSLER_MAX_STEER );
83
+
84
+ // *** torque rate limit check ***
85
+ violation |= dist_to_meas_check (desired_torque , chrysler_desired_torque_last ,
86
+ & chrysler_torque_meas , CHRYSLER_MAX_RATE_UP , CHRYSLER_MAX_RATE_DOWN , CHRYSLER_MAX_TORQUE_ERROR );
87
+
88
+ // used next time
89
+ chrysler_desired_torque_last = desired_torque ;
90
+
91
+ // *** torque real time rate limit check ***
92
+ violation |= rt_rate_limit_check (desired_torque , chrysler_rt_torque_last , CHRYSLER_MAX_RT_DELTA );
93
+
94
+ // every RT_INTERVAL set the new limits
95
+ uint32_t ts_elapsed = get_ts_elapsed (ts , chrysler_ts_last );
96
+ if (ts_elapsed > CHRYSLER_RT_INTERVAL ) {
97
+ chrysler_rt_torque_last = desired_torque ;
98
+ chrysler_ts_last = ts ;
99
99
}
100
100
}
101
- if (steer < (chrysler_desired_torque_last - max_rate )) {
102
- return false;
101
+
102
+ // no torque if controls is not allowed
103
+ if (!controls_allowed && (desired_torque != 0 )) {
104
+ violation = 1 ;
105
+ }
106
+
107
+ // reset to 0 if either controls is not allowed or there's a violation
108
+ if (violation || !controls_allowed ) {
109
+ chrysler_desired_torque_last = 0 ;
110
+ chrysler_rt_torque_last = 0 ;
111
+ chrysler_ts_last = ts ;
103
112
}
104
- if (steer > (chrysler_desired_torque_last + max_rate )) {
113
+
114
+ if (violation ) {
105
115
return false;
106
116
}
107
-
108
- chrysler_desired_torque_last = steer ;
109
117
}
110
118
119
+ // FORCE CANCEL: safety check only relevant when spamming the cancel button.
120
+ // ensuring that only the cancel button press is sent when controls are off.
121
+ // This avoids unintended engagements while still allowing resume spam
122
+ // TODO: fix bug preventing the button msg to be fwd'd on bus 2
123
+
111
124
// 1 allows the message through
112
125
return true;
113
126
}
114
127
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
- }
127
128
128
129
const safety_hooks chrysler_hooks = {
129
- .init = chrysler_init ,
130
+ .init = nooutput_init ,
130
131
.rx = chrysler_rx_hook ,
131
132
.tx = chrysler_tx_hook ,
132
- .tx_lin = chrysler_tx_lin_hook ,
133
+ .tx_lin = nooutput_tx_lin_hook ,
133
134
.ignition = default_ign_hook ,
134
- .fwd = chrysler_fwd_hook ,
135
+ .fwd = nooutput_fwd_hook ,
135
136
};
136
-
0 commit comments