Skip to content

Commit 7a2f3c0

Browse files
authored
Hyundai: clean up CarInterface apply function (#24000)
* clean up Hyundai CarController * remove
1 parent 7028676 commit 7a2f3c0

File tree

2 files changed

+42
-43
lines changed

2 files changed

+42
-43
lines changed

selfdrive/car/hyundai/carcontroller.py

+41-38
Original file line numberDiff line numberDiff line change
@@ -11,83 +11,85 @@
1111
LongCtrlState = car.CarControl.Actuators.LongControlState
1212

1313

14-
def process_hud_alert(enabled, fingerprint, visual_alert, left_lane,
15-
right_lane, left_lane_depart, right_lane_depart):
16-
sys_warning = (visual_alert in (VisualAlert.steerRequired, VisualAlert.ldw))
14+
def process_hud_alert(enabled, fingerprint, hud_control):
15+
sys_warning = (hud_control.visualAlert in (VisualAlert.steerRequired, VisualAlert.ldw))
1716

1817
# initialize to no line visible
1918
sys_state = 1
20-
if left_lane and right_lane or sys_warning: # HUD alert only display when LKAS status is active
19+
if hud_control.leftLaneVisible and hud_control.rightLaneVisible or sys_warning: # HUD alert only display when LKAS status is active
2120
sys_state = 3 if enabled or sys_warning else 4
22-
elif left_lane:
21+
elif hud_control.leftLaneVisible:
2322
sys_state = 5
24-
elif right_lane:
23+
elif hud_control.rightLaneVisible:
2524
sys_state = 6
2625

2726
# initialize to no warnings
2827
left_lane_warning = 0
2928
right_lane_warning = 0
30-
if left_lane_depart:
29+
if hud_control.leftLaneDepart:
3130
left_lane_warning = 1 if fingerprint in (CAR.GENESIS_G90, CAR.GENESIS_G80) else 2
32-
if right_lane_depart:
31+
if hud_control.rightLaneDepart:
3332
right_lane_warning = 1 if fingerprint in (CAR.GENESIS_G90, CAR.GENESIS_G80) else 2
3433

3534
return sys_warning, sys_state, left_lane_warning, right_lane_warning
3635

3736

38-
class CarController():
37+
class CarController:
3938
def __init__(self, dbc_name, CP, VM):
4039
self.CP = CP
41-
self.p = CarControllerParams(CP)
40+
self.params = CarControllerParams(CP)
4241
self.packer = CANPacker(dbc_name)
42+
self.frame = 0
4343

4444
self.apply_steer_last = 0
4545
self.car_fingerprint = CP.carFingerprint
4646
self.steer_rate_limited = False
4747
self.last_resume_frame = 0
4848
self.accel = 0
4949

50-
def update(self, c, CS, frame, actuators, pcm_cancel_cmd, visual_alert, hud_speed,
51-
left_lane, right_lane, left_lane_depart, right_lane_depart):
50+
def update(self, CC, CS):
51+
actuators = CC.actuators
52+
hud_control = CC.hudControl
53+
pcm_cancel_cmd = CC.cruiseControl.cancel
54+
5255
# Steering Torque
53-
new_steer = int(round(actuators.steer * self.p.STEER_MAX))
54-
apply_steer = apply_std_steer_torque_limits(new_steer, self.apply_steer_last, CS.out.steeringTorque, self.p)
56+
new_steer = int(round(actuators.steer * self.params.STEER_MAX))
57+
apply_steer = apply_std_steer_torque_limits(new_steer, self.apply_steer_last, CS.out.steeringTorque, self.params)
5558
self.steer_rate_limited = new_steer != apply_steer
5659

57-
if not c.latActive:
60+
if not CC.latActive:
5861
apply_steer = 0
5962

6063
self.apply_steer_last = apply_steer
6164

62-
sys_warning, sys_state, left_lane_warning, right_lane_warning = \
63-
process_hud_alert(c.enabled, self.car_fingerprint, visual_alert,
64-
left_lane, right_lane, left_lane_depart, right_lane_depart)
65+
sys_warning, sys_state, left_lane_warning, right_lane_warning = process_hud_alert(CC.enabled, self.car_fingerprint,
66+
hud_control)
6567

6668
can_sends = []
6769

6870
# tester present - w/ no response (keeps radar disabled)
6971
if self.CP.openpilotLongitudinalControl:
70-
if (frame % 100) == 0:
72+
if self.frame % 100 == 0:
7173
can_sends.append([0x7D0, 0, b"\x02\x3E\x80\x00\x00\x00\x00\x00", 0])
7274

73-
can_sends.append(create_lkas11(self.packer, frame, self.car_fingerprint, apply_steer, c.latActive,
74-
CS.lkas11, sys_warning, sys_state, c.enabled,
75-
left_lane, right_lane,
75+
can_sends.append(create_lkas11(self.packer, self.frame, self.car_fingerprint, apply_steer, CC.latActive,
76+
CS.lkas11, sys_warning, sys_state, CC.enabled,
77+
hud_control.leftLaneVisible, hud_control.rightLaneVisible,
7678
left_lane_warning, right_lane_warning))
7779

7880
if not self.CP.openpilotLongitudinalControl:
7981
if pcm_cancel_cmd:
80-
can_sends.append(create_clu11(self.packer, frame, CS.clu11, Buttons.CANCEL))
82+
can_sends.append(create_clu11(self.packer, self.frame, CS.clu11, Buttons.CANCEL))
8183
elif CS.out.cruiseState.standstill:
8284
# send resume at a max freq of 10Hz
83-
if (frame - self.last_resume_frame) * DT_CTRL > 0.1:
85+
if (self.frame - self.last_resume_frame) * DT_CTRL > 0.1:
8486
# send 25 messages at a time to increases the likelihood of resume being accepted
85-
can_sends.extend([create_clu11(self.packer, frame, CS.clu11, Buttons.RES_ACCEL)] * 25)
86-
self.last_resume_frame = frame
87+
can_sends.extend([create_clu11(self.packer, self.frame, CS.clu11, Buttons.RES_ACCEL)] * 25)
88+
self.last_resume_frame = self.frame
8789

88-
if frame % 2 == 0 and self.CP.openpilotLongitudinalControl:
90+
if self.frame % 2 == 0 and self.CP.openpilotLongitudinalControl:
8991
lead_visible = False
90-
accel = actuators.accel if c.longActive else 0
92+
accel = actuators.accel if CC.longActive else 0
9193

9294
jerk = clip(2.0 * (accel - CS.out.aEgo), -12.7, 12.7)
9395

@@ -97,28 +99,29 @@ def update(self, c, CS, frame, actuators, pcm_cancel_cmd, visual_alert, hud_spee
9799
accel = clip(accel, CarControllerParams.ACCEL_MIN, CarControllerParams.ACCEL_MAX)
98100

99101
stopping = (actuators.longControlState == LongCtrlState.stopping)
100-
set_speed_in_units = hud_speed * (CV.MS_TO_MPH if CS.clu11["CF_Clu_SPEED_UNIT"] == 1 else CV.MS_TO_KPH)
101-
can_sends.extend(create_acc_commands(self.packer, c.enabled, accel, jerk, int(frame / 2), lead_visible,
102+
set_speed_in_units = hud_control.setSpeed * (CV.MS_TO_MPH if CS.clu11["CF_Clu_SPEED_UNIT"] == 1 else CV.MS_TO_KPH)
103+
can_sends.extend(create_acc_commands(self.packer, CC.enabled, accel, jerk, int(self.frame / 2), lead_visible,
102104
set_speed_in_units, stopping, CS.out.gasPressed))
103105
self.accel = accel
104106

105107
# 20 Hz LFA MFA message
106-
if frame % 5 == 0 and self.car_fingerprint in (CAR.SONATA, CAR.PALISADE, CAR.IONIQ, CAR.KIA_NIRO_EV, CAR.KIA_NIRO_HEV_2021,
107-
CAR.IONIQ_EV_2020, CAR.IONIQ_PHEV, CAR.KIA_CEED, CAR.KIA_SELTOS, CAR.KONA_EV,
108-
CAR.ELANTRA_2021, CAR.ELANTRA_HEV_2021, CAR.SONATA_HYBRID, CAR.KONA_HEV, CAR.SANTA_FE_2022,
109-
CAR.KIA_K5_2021, CAR.IONIQ_HEV_2022, CAR.SANTA_FE_HEV_2022, CAR.GENESIS_G70_2020, CAR.SANTA_FE_PHEV_2022):
110-
can_sends.append(create_lfahda_mfc(self.packer, c.enabled))
108+
if self.frame % 5 == 0 and self.car_fingerprint in (CAR.SONATA, CAR.PALISADE, CAR.IONIQ, CAR.KIA_NIRO_EV, CAR.KIA_NIRO_HEV_2021,
109+
CAR.IONIQ_EV_2020, CAR.IONIQ_PHEV, CAR.KIA_CEED, CAR.KIA_SELTOS, CAR.KONA_EV,
110+
CAR.ELANTRA_2021, CAR.ELANTRA_HEV_2021, CAR.SONATA_HYBRID, CAR.KONA_HEV, CAR.SANTA_FE_2022,
111+
CAR.KIA_K5_2021, CAR.IONIQ_HEV_2022, CAR.SANTA_FE_HEV_2022, CAR.GENESIS_G70_2020, CAR.SANTA_FE_PHEV_2022):
112+
can_sends.append(create_lfahda_mfc(self.packer, CC.enabled))
111113

112114
# 5 Hz ACC options
113-
if frame % 20 == 0 and self.CP.openpilotLongitudinalControl:
115+
if self.frame % 20 == 0 and self.CP.openpilotLongitudinalControl:
114116
can_sends.extend(create_acc_opt(self.packer))
115117

116118
# 2 Hz front radar options
117-
if frame % 50 == 0 and self.CP.openpilotLongitudinalControl:
119+
if self.frame % 50 == 0 and self.CP.openpilotLongitudinalControl:
118120
can_sends.append(create_frt_radar_opt(self.packer))
119121

120122
new_actuators = actuators.copy()
121-
new_actuators.steer = apply_steer / self.p.STEER_MAX
123+
new_actuators.steer = apply_steer / self.params.STEER_MAX
122124
new_actuators.accel = self.accel
123125

126+
self.frame += 1
124127
return new_actuators, can_sends

selfdrive/car/hyundai/interface.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,5 @@ def update(self, c, can_strings):
348348
return self.CS.out
349349

350350
def apply(self, c):
351-
hud_control = c.hudControl
352-
ret = self.CC.update(c, self.CS, self.frame, c.actuators, c.cruiseControl.cancel, hud_control.visualAlert,
353-
hud_control.setSpeed, hud_control.leftLaneVisible, hud_control.rightLaneVisible,
354-
hud_control.leftLaneDepart, hud_control.rightLaneDepart)
355-
self.frame += 1
351+
ret = self.CC.update(c, self.CS)
356352
return ret

0 commit comments

Comments
 (0)