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