1010import matplotlib .patches as patches
1111import math
1212
13- def detect_collision (curr_x : float , curr_y : float , curr_v : float , obj : AgentState , min_deceleration : float , max_deceleration : float ) -> Tuple [bool , float ]:
13+ def detect_collision (curr_x : float , curr_y : float , curr_v : float , obj : AgentState , min_deceleration : float , max_deceleration : float , acceleration : float , max_speed : float ) -> Tuple [bool , float ]:
1414 """Detects if a collision will occur with the given object and return deceleration to avoid it."""
1515
1616 # Get the object's position and velocity
@@ -50,7 +50,7 @@ def detect_collision(curr_x: float, curr_y: float, curr_v: float, obj: AgentStat
5050 # The object is to the side of the vehicle
5151 print ("Object is to the side of the vehicle" )
5252 return False , 0.0
53- # The object overlaps with the vehicle
53+ # The object overlaps with the vehicle's buffer
5454 return True , max_deceleration
5555
5656 if vehicle_right - vehicle_buffer_y > pedestrian_left and obj_v_y <= 0 :
@@ -63,65 +63,87 @@ def detect_collision(curr_x: float, curr_y: float, curr_v: float, obj: AgentStat
6363 print ("Object is to the left of the vehicle and moving away" )
6464 return False , 0.0
6565
66- if vehicle_front + vehicle_buffer_x >= pedestrian_back :
66+ if vehicle_front + vehicle_buffer_x >= pedestrian_back and ( vehicle_right - vehicle_buffer_y <= pedestrian_left and vehicle_left + vehicle_buffer_y >= pedestrian_right ) :
6767 # The object is in front of the vehicle and within the buffer
6868 print ("Object is in front of the vehicle and within the buffer" )
6969 return True , max_deceleration
7070
7171 # Calculate the deceleration needed to avoid a collision
7272 print ("Object is in front of the vehicle and outside the buffer" )
73- distance = pedestrian_back - vehicle_front - vehicle_buffer_x
73+ distance = pedestrian_back - vehicle_front
74+ distance_with_buffer = pedestrian_back - vehicle_front - vehicle_buffer_x
7475
7576 relative_v = curr_v - obj_v_x
7677 if relative_v <= 0 :
7778 return False , 0.0
78-
79- print (relative_v , distance )
8079
81- if obj_v_y > 0 and ((obj_y - curr_y ) / relative_v ) < ((vehicle_right - vehicle_buffer_y - yield_buffer_y - pedestrian_left ) / abs (obj_v_y )):
82- # The object is to the right of the vehicle and moving towards it, but the vehicle will pass before the object reaches the vehicle
83- print ("The object is to the right of the vehicle and moving towards it, but the vehicle will pass before the object reaches the vehicle" )
84- return False , 0.0
85- if obj_v_y < 0 and ((obj_y - curr_y ) / relative_v ) < ((pedestrian_right - vehicle_left - vehicle_buffer_y - yield_buffer_y ) / abs (obj_v_y )):
86- # The object is to the left of the vehicle and moving towards it, but the vehicle will pass before the object reaches the vehicle
87- print ("The object is to the left of the vehicle and moving towards it, but the vehicle will pass before the object reaches the vehicle" )
88- return False , 0.0
80+ if obj_v_y == 0 :
81+ # The object is in front of the vehicle blocking it
82+ deceleration = relative_v ** 2 / (2 * distance_with_buffer )
83+ if deceleration > max_deceleration :
84+ return True , max_deceleration
85+ if deceleration < min_deceleration :
86+ return False , 0.0
87+
88+ return True , deceleration
8989
90- if obj_v_y != 0 :
91- if obj_v_y < 0 :
92- # The object is moving toward the right side of the vehicle
93- distance_to_pass = obj_y - (vehicle_right - vehicle_buffer_y - yield_buffer_y ) + pedestrian_width / 2
94- elif obj_v_y > 0 :
95- # The object is moving toward the left side of the vehicle
96- distance_to_pass = (vehicle_left + vehicle_buffer_y + yield_buffer_y ) - obj_y + pedestrian_width / 2
90+ print (relative_v , distance_with_buffer )
9791
98- time_to_pass = distance_to_pass / abs (obj_v_y )
92+ if obj_v_y > 0 :
93+ # The object is to the right of the vehicle and moving towards it
94+ time_to_get_close = (vehicle_right - vehicle_buffer_y - yield_buffer_y - pedestrian_left ) / abs (obj_v_y )
95+ time_to_pass = (vehicle_left + vehicle_buffer_y + yield_buffer_y - pedestrian_right ) / abs (obj_v_y )
96+ else :
97+ # The object is to the left of the vehicle and moving towards it
98+ time_to_get_close = (pedestrian_right - vehicle_left - vehicle_buffer_y - yield_buffer_y ) / abs (obj_v_y )
99+ time_to_pass = (pedestrian_left - vehicle_right + vehicle_buffer_y + yield_buffer_y ) / abs (obj_v_y )
100+
101+ time_to_accel_to_max_speed = (max_speed - curr_v ) / acceleration
102+ distance_to_accel_to_max_speed = (max_speed + curr_v - 2 * obj_v_x ) * time_to_accel_to_max_speed / 2 #area of trapezoid
103+
104+ if distance_to_accel_to_max_speed > distance_with_buffer :
105+ # The object will reach the buffer before reaching max speed
106+ time_to_buffer_when_accel = (- relative_v + (relative_v * relative_v + 2 * distance_with_buffer * acceleration ) ** 0.5 ) / acceleration
107+ else :
108+ # The object will reach the buffer after reaching max speed
109+ time_to_buffer_when_accel = time_to_accel_to_max_speed + (distance_with_buffer - distance_to_accel_to_max_speed ) / (max_speed - obj_v_x )
99110
100- distance_to_move = pedestrian_back - vehicle_front - vehicle_buffer_x + time_to_pass * obj_v_y
111+ if distance_to_accel_to_max_speed > distance :
112+ # We will collide before reaching max speed
113+ time_to_collide_when_accel = (- relative_v + (relative_v * relative_v + 2 * distance * acceleration ) ** 0.5 ) / acceleration
114+ else :
115+ # We will collide after reaching max speed
116+ time_to_collide_when_accel = time_to_accel_to_max_speed + (distance - distance_to_accel_to_max_speed ) / (max_speed - obj_v_x )
101117
118+ if time_to_get_close > time_to_collide_when_accel :
119+ # We can do normal driving and will pass the object before it gets in our way
120+ print ("We can do normal driving and will pass the object before it gets in our way" )
121+ return False , 0.0
102122
103- # if curr_v**2/2*distance_to_pass >= 1.5 :
104- # return True, curr_v**2/2*distance_to_pass
105- time_to_max_v = ( 5 - curr_v ) / 0.5
123+ if vehicle_front + vehicle_buffer_x >= pedestrian_back :
124+ # We cannot move pass the pedestrian before it reaches the buffer from side
125+ return True , max_deceleration
106126
107- if time_to_max_v > time_to_pass :
108- if curr_v * time_to_pass + 0.25 * time_to_pass ** 2 > distance_to_move :
109- return False , 0.0
110- else :
111- if (25 - curr_v ** 2 )* 4 + (time_to_pass - time_to_max_v ) * 5 >= distance_to_move :
112- return False , 0.0
127+ if time_to_pass < time_to_buffer_when_accel :
128+ # The object will pass through our front before we drive normally and reach it
129+ print ("The object will pass through our front before we drive normally and reach it" )
130+ return False , 0.0
113131
132+ distance_to_move = distance_with_buffer + time_to_pass * obj_v_y
114133
115- return True , [distance_to_move , time_to_pass ]
134+ # if curr_v**2/2*distance_to_pass >= 1.5:
135+ # return True, curr_v**2/2*distance_to_pass
136+ time_to_max_v = (5 - curr_v )/ 0.5
116137
138+ if time_to_max_v > time_to_pass :
139+ if curr_v * time_to_pass + 0.25 * time_to_pass ** 2 > distance_to_move :
140+ return False , 0.0
117141 else :
118- deceleration = relative_v ** 2 / (2 * distance )
119- if deceleration > max_deceleration :
120- return True , max_deceleration
121- if deceleration < min_deceleration :
142+ if (25 - curr_v ** 2 )* 4 + (time_to_pass - time_to_max_v ) * 5 >= distance_to_move :
122143 return False , 0.0
123144
124- return True , deceleration
145+
146+ return True , [distance_to_move , time_to_pass ]
125147
126148def longitudinal_plan (path : Path , acceleration : float , deceleration : float , max_speed : float , current_speed : float ,
127149 method : str ) -> Trajectory :
@@ -895,7 +917,7 @@ def update(self, state : AllState):
895917 #get the object we are yielding to
896918 obj = state .agents [r .obj2 ]
897919
898- detected , deceleration = detect_collision (abs_x , abs_y , curr_v , obj , self .min_deceleration , self .max_deceleration )
920+ detected , deceleration = detect_collision (abs_x , abs_y , curr_v , obj , self .min_deceleration , self .max_deceleration , self . acceleration , self . desired_speed )
899921 if isinstance (deceleration , list ):
900922 print ("@@@@@ INPUT" , deceleration )
901923 time_collision = deceleration [1 ]
0 commit comments