@@ -67,11 +67,12 @@ def set_cur_state(self, v, a):
67
67
self .cur_state [0 ].a_ego = a
68
68
69
69
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 )
75
76
return a
76
77
77
78
def save_car_data (self ):
@@ -101,7 +102,7 @@ def get_traffic_level(self): # based on fluctuation of v_lead
101
102
102
103
def smooth_follow (self ): # in m/s
103
104
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
105
106
106
107
if self .v_ego > 3.57632 : # 8 mph
107
108
TR = interp (self .v_ego , x_vel , y_mod )
@@ -116,7 +117,7 @@ def smooth_follow(self): # in m/s
116
117
TR_mod = interp (self .v_lead + self .v_ego , x , y ) # quicker acceleration/don't brake when lead is overtaking
117
118
118
119
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 ]
120
121
TR_mod += interp (self .get_acceleration (), x , y ) # when lead car has been braking over the past 3 seconds, slightly increase TR
121
122
122
123
TR += TR_mod
@@ -141,7 +142,7 @@ def get_TR(self):
141
142
142
143
if self .v_ego < 2.0 and read_distance_lines != 2 :
143
144
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
145
146
if self .last_cost != 1.0 :
146
147
self .libmpc .change_tr (MPC_COST_LONG .TTC , 1.0 , MPC_COST_LONG .ACCELERATION , MPC_COST_LONG .JERK )
147
148
self .last_cost = 1.0
@@ -152,18 +153,13 @@ def get_TR(self):
152
153
self .last_cost = 1.0
153
154
return 0.9 # 10m at 40km/hr
154
155
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 ()
160
157
TR = self .smooth_follow ()
161
- #cost = self.get_cost(TR)
162
- cost = 0.1
158
+ cost = self .get_cost (TR )
163
159
if abs (cost - self .last_cost ) > .15 :
164
160
self .libmpc .init (MPC_COST_LONG .TTC , cost , MPC_COST_LONG .ACCELERATION , MPC_COST_LONG .JERK )
165
161
self .last_cost = cost
166
- return TR'''
162
+ return TR
167
163
else :
168
164
if self .last_cost != 0.05 :
169
165
self .libmpc .change_tr (MPC_COST_LONG .TTC , 0.05 , MPC_COST_LONG .ACCELERATION , MPC_COST_LONG .JERK )
0 commit comments