Skip to content

Commit c249cbd

Browse files
committed
Add Subaru SNG support
1 parent 5264485 commit c249cbd

File tree

3 files changed

+107
-2
lines changed

3 files changed

+107
-2
lines changed

selfdrive/car/subaru/carcontroller.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@ def __init__(self, dbc_name, CP, VM):
99
self.apply_steer_last = 0
1010
self.es_distance_cnt = -1
1111
self.es_lkas_cnt = -1
12+
self.throttle_cnt = -1
1213
self.cruise_button_prev = 0
1314
self.steer_rate_limited = False
15+
self.sng_acc_resume = False
16+
self.sng_acc_resume_cnt = -1
17+
self.manual_hold = False
18+
self.prev_cruise_state = 0
19+
self.prev_close_distance = 0
1420

1521
self.p = CarControllerParams(CP)
1622
self.packer = CANPacker(DBC[CP.carFingerprint]['pt'])
@@ -40,6 +46,49 @@ def update(self, enabled, CS, frame, actuators, pcm_cancel_cmd, visual_alert, le
4046

4147
self.apply_steer_last = apply_steer
4248

49+
# *** stop and go ***
50+
51+
throttle_cmd = False
52+
53+
if CS.CP.carFingerprint in PREGLOBAL_CARS:
54+
if (enabled # ACC active
55+
and CS.car_follow == 1 # lead car
56+
and CS.out.standstill # must be standing still
57+
and CS.close_distance > 3 # acc resume trigger threshold
58+
and CS.close_distance < 4.5 # max operating distance to filter false positives
59+
and CS.close_distance > self.prev_close_distance): # distance with lead car is increasing
60+
self.sng_acc_resume = True
61+
elif CS.CP.carFingerprint not in PREGLOBAL_CARS:
62+
# Record manual hold set while in standstill and no car in front
63+
if CS.out.standstill and self.prev_cruise_state == 1 and CS.cruise_state == 3 and CS.car_follow == 0:
64+
self.manual_hold = True
65+
# Cancel manual hold when car starts moving
66+
if not CS.out.standstill:
67+
self.manual_hold = False
68+
if (enabled # ACC active
69+
and not self.manual_hold
70+
and CS.car_follow == 1 # lead car
71+
and CS.cruise_state == 3 # ACC HOLD (only with EPB)
72+
and CS.out.standstill # must be standing still
73+
and CS.close_distance > 150 # acc resume trigger threshold
74+
and CS.close_distance < 255 # ignore max value
75+
and CS.close_distance > self.prev_close_distance): # distance with lead car is increasing
76+
self.sng_acc_resume = True
77+
self.prev_cruise_state = CS.cruise_state
78+
79+
if self.sng_acc_resume:
80+
if self.sng_acc_resume_cnt < 5:
81+
throttle_cmd = True
82+
self.sng_acc_resume_cnt += 1
83+
else:
84+
self.sng_acc_resume = False
85+
self.sng_acc_resume_cnt = -1
86+
87+
# Cancel ACC if stopped, brake pressed and not stopped behind another car
88+
if enabled and CS.out.brakePressed and CS.car_follow == 0 and CS.out.standstill:
89+
pcm_cancel_cmd = True
90+
91+
self.prev_close_distance = CS.close_distance
4392

4493
# *** alerts and pcm cancel ***
4594

@@ -63,6 +112,10 @@ def update(self, enabled, CS, frame, actuators, pcm_cancel_cmd, visual_alert, le
63112
can_sends.append(subarucan.create_preglobal_es_distance(self.packer, cruise_button, CS.es_distance_msg))
64113
self.es_distance_cnt = CS.es_distance_msg["Counter"]
65114

115+
if self.throttle_cnt != CS.throttle_msg["Counter"]:
116+
can_sends.append(subarucan.create_preglobal_throttle(self.packer, CS.throttle_msg, throttle_cmd))
117+
self.throttle_cnt = CS.throttle_msg["Counter"]
118+
66119
else:
67120
if self.es_distance_cnt != CS.es_distance_msg["Counter"]:
68121
can_sends.append(subarucan.create_es_distance(self.packer, CS.es_distance_msg, pcm_cancel_cmd))
@@ -72,6 +125,10 @@ def update(self, enabled, CS, frame, actuators, pcm_cancel_cmd, visual_alert, le
72125
can_sends.append(subarucan.create_es_lkas(self.packer, CS.es_lkas_msg, enabled, visual_alert, left_line, right_line, left_lane_depart, right_lane_depart))
73126
self.es_lkas_cnt = CS.es_lkas_msg["Counter"]
74127

128+
if self.throttle_cnt != CS.throttle_msg["Counter"]:
129+
can_sends.append(subarucan.create_throttle(self.packer, CS.throttle_msg, throttle_cmd))
130+
self.throttle_cnt = CS.throttle_msg["Counter"]
131+
75132
new_actuators = actuators.copy()
76133
new_actuators.steer = self.apply_steer_last / self.p.STEER_MAX
77134

selfdrive/car/subaru/carstate.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ def update(self, cp, cp_cam):
7171
ret.steerWarning = cp.vl["Steering_Torque"]["Steer_Warning"] == 1
7272
ret.cruiseState.nonAdaptive = cp_cam.vl["ES_DashStatus"]["Conventional_Cruise"] == 1
7373
self.es_lkas_msg = copy.copy(cp_cam.vl["ES_LKAS_State"])
74+
self.cruise_state = cp_cam.vl["ES_DashStatus"]["Cruise_State"]
75+
self.car_follow = cp_cam.vl["ES_Distance"]["Car_Follow"]
76+
self.close_distance = cp_cam.vl["ES_Distance"]["Close_Distance"]
77+
self.throttle_msg = copy.copy(cp.vl["Throttle"])
7478
self.es_distance_msg = copy.copy(cp_cam.vl["ES_Distance"])
7579

7680
return ret
@@ -85,7 +89,6 @@ def get_can_parser(CP):
8589
("Steer_Error_1", "Steering_Torque", 0),
8690
("Cruise_On", "CruiseControl", 0),
8791
("Cruise_Activated", "CruiseControl", 0),
88-
("Brake_Pedal", "Brake_Pedal", 0),
8992
("Throttle_Pedal", "Throttle", 0),
9093
("LEFT_BLINKER", "Dashlights", 0),
9194
("RIGHT_BLINKER", "Dashlights", 0),
@@ -105,7 +108,6 @@ def get_can_parser(CP):
105108
# sig_address, frequency
106109
("Throttle", 100),
107110
("Dashlights", 10),
108-
("Brake_Pedal", 50),
109111
("Wheel_Speeds", 50),
110112
("Transmission", 100),
111113
("Steering_Torque", 50),
@@ -125,6 +127,16 @@ def get_can_parser(CP):
125127

126128
if CP.carFingerprint not in PREGLOBAL_CARS:
127129
signals += [
130+
("Counter", "Throttle", 0),
131+
("Signal1", "Throttle", 0),
132+
("Engine_RPM", "Throttle", 0),
133+
("Signal2", "Throttle", 0),
134+
("Throttle_Pedal", "Throttle", 0),
135+
("Throttle_Cruise", "Throttle", 0),
136+
("Throttle_Combo", "Throttle", 0),
137+
("Signal1", "Throttle", 0),
138+
("Off_Accel", "Throttle", 0),
139+
128140
("Steer_Warning", "Steering_Torque", 0),
129141
("Brake", "Brake_Status", 0),
130142
("UNITS", "Dashlights", 0),
@@ -138,11 +150,27 @@ def get_can_parser(CP):
138150
]
139151
else:
140152
signals += [
153+
("Throttle_Pedal", "Throttle", 0),
154+
("Counter", "Throttle", 0),
155+
("Signal1", "Throttle", 0),
156+
("Not_Full_Throttle", "Throttle", 0),
157+
("Signal2", "Throttle", 0),
158+
("Engine_RPM", "Throttle", 0),
159+
("Off_Throttle", "Throttle", 0),
160+
("Signal3", "Throttle", 0),
161+
("Throttle_Cruise", "Throttle", 0),
162+
("Throttle_Combo", "Throttle", 0),
163+
("Throttle_Body", "Throttle", 0),
164+
("Off_Throttle_2", "Throttle", 0),
165+
("Signal4", "Throttle", 0),
166+
141167
("UNITS", "Dash_State2", 0),
168+
("Brake_Pedal", "Brake_Pedal", 0),
142169
]
143170

144171
checks += [
145172
("Dash_State2", 1),
173+
("Brake_Pedal", 50),
146174
]
147175

148176
if CP.carFingerprint == CAR.FORESTER_PREGLOBAL:
@@ -194,6 +222,7 @@ def get_cam_can_parser(CP):
194222
signals = [
195223
("Cruise_Set_Speed", "ES_DashStatus", 0),
196224
("Conventional_Cruise", "ES_DashStatus", 0),
225+
("Cruise_State", "ES_DashStatus", 0),
197226

198227
("Counter", "ES_Distance", 0),
199228
("Signal1", "ES_Distance", 0),

selfdrive/car/subaru/subarucan.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ def create_es_lkas(packer, es_lkas_msg, enabled, visual_alert, left_line, right_
6161

6262
return packer.make_can_msg("ES_LKAS_State", 0, values)
6363

64+
def create_throttle(packer, throttle_msg, throttle_cmd):
65+
66+
values = copy.copy(throttle_msg)
67+
if throttle_cmd:
68+
values["Throttle_Pedal"] = 5
69+
70+
return packer.make_can_msg("Throttle", 2, values)
71+
72+
6473
# *** Subaru Pre-global ***
6574

6675
def subaru_preglobal_checksum(packer, values, addr):
@@ -88,3 +97,13 @@ def create_preglobal_es_distance(packer, cruise_button, es_distance_msg):
8897
values["Checksum"] = subaru_preglobal_checksum(packer, values, "ES_Distance")
8998

9099
return packer.make_can_msg("ES_Distance", 0, values)
100+
101+
def create_preglobal_throttle(packer, throttle_msg, throttle_cmd):
102+
103+
values = copy.copy(throttle_msg)
104+
if throttle_cmd:
105+
values["Throttle_Pedal"] = 5
106+
107+
values["Checksum"] = subaru_preglobal_checksum(packer, values, "Throttle")
108+
109+
return packer.make_can_msg("Throttle", 2, values)

0 commit comments

Comments
 (0)