Skip to content

Commit e7df968

Browse files
authored
Merge pull request commaai#363 from ShaneSmiskol/0512
enable smooth follow. shorter distance above 40 mph, larger distance below.
2 parents 27599af + 3fdd3be commit e7df968

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

selfdrive/controls/lib/long_mpc.py

+12-16
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,12 @@ def set_cur_state(self, v, a):
6767
self.cur_state[0].a_ego = a
6868

6969
def get_acceleration(self): # calculate acceleration to generate more accurate following distances
70-
if len(self.car_data["lead_vels"]) > self.calc_rate() and sum(self.car_data["lead_vels"]) != 0:
71-
try:
72-
a = (self.car_data["lead_vels"][-1] - self.car_data["lead_vels"][0]) / float(len(self.car_data["lead_vels"]) / self.calc_rate())
73-
except ZeroDivisionError:
74-
a = 0.0
70+
a = 0.0
71+
if len(self.car_data["lead_vels"]) > self.calc_rate(2):
72+
num = (self.car_data["lead_vels"][-1] - self.car_data["lead_vels"][0])
73+
den = len(self.car_data["lead_vels"]) / self.calc_rate()
74+
if den > 0:
75+
a = num / float(den)
7576
return a
7677

7778
def save_car_data(self):
@@ -101,7 +102,7 @@ def get_traffic_level(self): # based on fluctuation of v_lead
101102

102103
def smooth_follow(self): # in m/s
103104
x_vel = [0.0, 4.8, 9.0, 11.3, 13.6, 17.1, 23.1, 29.5, 35.1, 39.8, 42.2] # velocities
104-
y_mod = [1.35, 1.36, 1.39, 1.43, 1.46, 1.48, 1.49, 1.53, 1.59, 1.68, 1.8] # distances
105+
y_mod = [1.402, 1.408, 1.426, 1.45, 1.468, 1.48, 1.486, 1.51, 1.546, 1.6, 1.672] # distances
105106

106107
if self.v_ego > 3.57632: # 8 mph
107108
TR = interp(self.v_ego, x_vel, y_mod)
@@ -116,7 +117,7 @@ def smooth_follow(self): # in m/s
116117
TR_mod = interp(self.v_lead + self.v_ego, x, y) # quicker acceleration/don't brake when lead is overtaking
117118

118119
x = [-1.49, -1.1, -0.67, 0.0, 0.67, 1.1, 1.49]
119-
y = [0.14, 0.08, 0.04, 0.0, -0.04, -0.08, -0.14]
120+
y = [0.056, 0.032, 0.016, 0.0, -0.016, -0.032, -0.056]
120121
TR_mod += interp(self.get_acceleration(), x, y) # when lead car has been braking over the past 3 seconds, slightly increase TR
121122

122123
TR += TR_mod
@@ -141,7 +142,7 @@ def get_TR(self):
141142

142143
if self.v_ego < 2.0 and read_distance_lines != 2:
143144
return 1.8
144-
elif self.car_state.leftBlinker or self.car_state.rightBlinker:
145+
elif (self.car_state.leftBlinker or self.car_state.rightBlinker) and self.v_ego > 8.9408: # don't get super close when signaling in a turn lane
145146
if self.last_cost != 1.0:
146147
self.libmpc.change_tr(MPC_COST_LONG.TTC, 1.0, MPC_COST_LONG.ACCELERATION, MPC_COST_LONG.JERK)
147148
self.last_cost = 1.0
@@ -152,18 +153,13 @@ def get_TR(self):
152153
self.last_cost = 1.0
153154
return 0.9 # 10m at 40km/hr
154155
elif read_distance_lines == 2:
155-
if self.last_cost != 0.1:
156-
self.libmpc.change_tr(MPC_COST_LONG.TTC, 0.1, MPC_COST_LONG.ACCELERATION, MPC_COST_LONG.JERK)
157-
self.last_cost = 0.1
158-
return 1.6
159-
'''self.save_car_data()
156+
self.save_car_data()
160157
TR = self.smooth_follow()
161-
#cost = self.get_cost(TR)
162-
cost = 0.1
158+
cost = self.get_cost(TR)
163159
if abs(cost - self.last_cost) > .15:
164160
self.libmpc.init(MPC_COST_LONG.TTC, cost, MPC_COST_LONG.ACCELERATION, MPC_COST_LONG.JERK)
165161
self.last_cost = cost
166-
return TR'''
162+
return TR
167163
else:
168164
if self.last_cost != 0.05:
169165
self.libmpc.change_tr(MPC_COST_LONG.TTC, 0.05, MPC_COST_LONG.ACCELERATION, MPC_COST_LONG.JERK)

0 commit comments

Comments
 (0)