Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d21c659

Browse files
author
Vehicle Researcher
committedJan 23, 2019
Squashed 'panda/' changes from 293fa33..9ee6285
9ee6285 optimize board build for size to avoid going over the limit. (#150) 20e8fa9 Start introducing Bounties a2046e9 make it smaller 1dfcf2b update panda price 37ee289 chrysler safety: fixed comments c2dfbad tesla safety: return -1 to block forward (#149) 74c0c1b update README be0061d Chrysler: safety now based on motor torque 039d183 Chrysler: fixed regression test 9193eeb Chrysler: safety limits updated 04f1d44 Chrysler safety: 3 sa max rate down for now cf3ecd6 Chrysler safety: re-using hyundai framework 49ed9bc Update CLICKS for longer bootup time of EONS and avoid unwanted fast charge mode git-subtree-dir: panda git-subtree-split: 9ee6285
1 parent c10a755 commit d21c659

11 files changed

+261
-145
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Welcome to panda
33

44
[panda](http://github.com/commaai/panda) is the nicest universal car interface ever.
55

6-
<a href="https://www.amazon.com/chffr-panda-OBD-II-Interface/dp/B07D6Y3GN2/"><img src="https://github.com/commaai/panda/blob/master/panda.png">
6+
<a href="https://comma.ai/shop/products/panda-obd-ii-dongle"><img src="https://github.com/commaai/panda/blob/master/panda.png">
77

88
<img src="https://github.com/commaai/panda/blob/master/buy.png"></a>
99

‎VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v1.1.7
1+
v1.1.8

‎board/bootstub.c

+3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ void fail() {
5151
// know where to sig check
5252
extern void *_app_start[];
5353

54+
// FIXME: sometimes your panda will fail flashing and will quickly blink a single Green LED
55+
// BOUNTY: $200 coupon on shop.comma.ai or $100 check.
56+
5457
int main() {
5558
__disable_irq();
5659
clock_init();

‎board/build.mk

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CFLAGS += -I inc -I ../ -nostdlib -fno-builtin -std=gnu11 -O2
1+
CFLAGS += -I inc -I ../ -nostdlib -fno-builtin -std=gnu11 -Os
22

33
CFLAGS += -Tstm32_flash.ld
44

@@ -51,6 +51,8 @@ obj/$(PROJ_NAME).bin: obj/$(STARTUP_FILE).o obj/main.$(PROJ_NAME).o
5151
$(CC) -Wl,--section-start,.isr_vector=0x8004000 $(CFLAGS) -o obj/$(PROJ_NAME).elf $^
5252
$(OBJCOPY) -v -O binary obj/$(PROJ_NAME).elf obj/code.bin
5353
SETLEN=1 ../crypto/sign.py obj/code.bin $@ $(CERT)
54+
@BINSIZE=$$(du -b "obj/$(PROJ_NAME).bin" | cut -f 1) ; if [ $$BINSIZE -ge 32768 ]; then echo "ERROR obj/$(PROJ_NAME).bin is too big!"; exit 1; fi;
55+
5456

5557
obj/bootstub.$(PROJ_NAME).bin: obj/$(STARTUP_FILE).o obj/bootstub.$(PROJ_NAME).o obj/sha.$(PROJ_NAME).o obj/rsa.$(PROJ_NAME).o
5658
$(CC) $(CFLAGS) -o obj/bootstub.$(PROJ_NAME).elf $^

‎board/main.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,8 @@ int main() {
589589
uint64_t marker = 0;
590590
#define CURRENT_THRESHOLD 0xF00
591591
#define CLICKS 8
592+
// Enough clicks to ensure that enumeration happened. Should be longer than bootup time of the device connected to EON
593+
#define CLICKS_BOOTUP 30
592594
#endif
593595

594596
for (cnt=0;;cnt++) {
@@ -615,8 +617,8 @@ int main() {
615617
}
616618
break;
617619
case USB_POWER_CDP:
618-
// been CLICKS clicks since we switched to CDP
619-
if ((cnt-marker) >= CLICKS) {
620+
// been CLICKS_BOOTUP clicks since we switched to CDP
621+
if ((cnt-marker) >= CLICKS_BOOTUP ) {
620622
// measure current draw, if positive and no enumeration, switch to DCP
621623
if (!is_enumerated && current < CURRENT_THRESHOLD) {
622624
puts("USBP: no enumeration with current draw, switching to DCP mode\n");

‎board/safety/safety_chrysler.h

+79-79
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
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;
1410
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
1514

1615
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;
1817
uint32_t addr;
1918
if (to_push->RIR & 4) {
2019
// Extended
@@ -26,40 +25,37 @@ static void chrysler_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
2625
addr = to_push->RIR >> 21;
2726
}
2827

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;
3232

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);
3735
}
3836

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) {
4241
controls_allowed = 1;
43-
} else {
42+
} else if (!cruise_engaged) {
4443
controls_allowed = 0;
4544
}
45+
chrysler_cruise_engaged_last = cruise_engaged;
4646
}
4747

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;
5352
}
5453
}
5554

56-
// if controls_allowed
57-
// allow steering up to limit
58-
// else
59-
// block all commands that produce actuation
6055
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) {
6359
return 0;
6460
}
6561

@@ -72,65 +68,69 @@ static int chrysler_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
7268
addr = to_send->RIR >> 21;
7369
}
7470

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
7773
if (addr == 0x292) {
7874
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;
9999
}
100100
}
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;
103112
}
104-
if (steer > (chrysler_desired_torque_last + max_rate)) {
113+
114+
if (violation) {
105115
return false;
106116
}
107-
108-
chrysler_desired_torque_last = steer;
109117
}
110118

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+
111124
// 1 allows the message through
112125
return true;
113126
}
114127

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-
}
127128

128129
const safety_hooks chrysler_hooks = {
129-
.init = chrysler_init,
130+
.init = nooutput_init,
130131
.rx = chrysler_rx_hook,
131132
.tx = chrysler_tx_hook,
132-
.tx_lin = chrysler_tx_lin_hook,
133+
.tx_lin = nooutput_tx_lin_hook,
133134
.ignition = default_ign_hook,
134-
.fwd = chrysler_fwd_hook,
135+
.fwd = nooutput_fwd_hook,
135136
};
136-

‎board/safety/safety_tesla.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ static int tesla_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd)
258258
// remove EPB_epasControl
259259
if (addr == 0x214)
260260
{
261-
return false;
261+
return -1;
262262
}
263263

264264
return 2; // Custom EPAS bus
@@ -269,12 +269,12 @@ static int tesla_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd)
269269
// remove GTW_epasControl in forwards
270270
if (addr == 0x101)
271271
{
272-
return false;
272+
return -1;
273273
}
274274

275275
return 0; // Chassis CAN
276276
}
277-
return false;
277+
return -1;
278278
}
279279

280280
const safety_hooks tesla_hooks = {

‎buy.png

-7.75 KB
Loading

‎tests/safety/libpandasafety_py.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,13 @@
4242
void set_cadillac_torque_driver(int min, int max);
4343
void set_gm_torque_driver(int min, int max);
4444
void set_hyundai_torque_driver(int min, int max);
45+
void set_chrysler_torque_meas(int min, int max);
4546
void set_toyota_rt_torque_last(int t);
4647
void set_toyota_desired_torque_last(int t);
4748
int get_toyota_torque_meas_min(void);
4849
int get_toyota_torque_meas_max(void);
50+
int get_chrysler_torque_meas_min(void);
51+
int get_chrysler_torque_meas_max(void);
4952
5053
void init_tests_honda(void);
5154
int get_ego_speed(void);
@@ -84,8 +87,9 @@
8487
void init_tests_chrysler(void);
8588
void chrysler_rx_hook(CAN_FIFOMailBox_TypeDef *to_push);
8689
int chrysler_tx_hook(CAN_FIFOMailBox_TypeDef *to_send);
87-
void chrysler_init(int16_t param);
8890
void set_chrysler_desired_torque_last(int t);
91+
void set_chrysler_rt_torque_last(int t);
92+
8993
9094
""")
9195

‎tests/safety/test.c

+27-5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ struct sample_t toyota_torque_meas;
2626
struct sample_t cadillac_torque_driver;
2727
struct sample_t gm_torque_driver;
2828
struct sample_t hyundai_torque_driver;
29+
struct sample_t chrysler_torque_driver;
2930

3031
TIM_TypeDef timer;
3132
TIM_TypeDef *TIM2 = &timer;
@@ -81,6 +82,19 @@ void set_hyundai_torque_driver(int min, int max){
8182
hyundai_torque_driver.max = max;
8283
}
8384

85+
void set_chrysler_torque_meas(int min, int max){
86+
chrysler_torque_meas.min = min;
87+
chrysler_torque_meas.max = max;
88+
}
89+
90+
int get_chrysler_torque_meas_min(void){
91+
return chrysler_torque_meas.min;
92+
}
93+
94+
int get_chrysler_torque_meas_max(void){
95+
return chrysler_torque_meas.max;
96+
}
97+
8498
int get_toyota_torque_meas_min(void){
8599
return toyota_torque_meas.min;
86100
}
@@ -105,6 +119,10 @@ void set_hyundai_rt_torque_last(int t){
105119
hyundai_rt_torque_last = t;
106120
}
107121

122+
void set_chrysler_rt_torque_last(int t){
123+
chrysler_rt_torque_last = t;
124+
}
125+
108126
void set_toyota_desired_torque_last(int t){
109127
toyota_desired_torque_last = t;
110128
}
@@ -181,18 +199,22 @@ void init_tests_hyundai(void){
181199
set_timer(0);
182200
}
183201

202+
void init_tests_chrysler(void){
203+
chrysler_torque_driver.min = 0;
204+
chrysler_torque_driver.max = 0;
205+
chrysler_desired_torque_last = 0;
206+
chrysler_rt_torque_last = 0;
207+
chrysler_ts_last = 0;
208+
set_timer(0);
209+
}
210+
184211
void init_tests_honda(void){
185212
ego_speed = 0;
186213
gas_interceptor_detected = 0;
187214
brake_prev = 0;
188215
gas_prev = 0;
189216
}
190217

191-
void init_tests_chrysler(void){
192-
chrysler_desired_torque_last = 0;
193-
set_timer(0);
194-
}
195-
196218
void set_gmlan_digital_output(int to_set){
197219
}
198220

0 commit comments

Comments
 (0)
Please sign in to comment.