1- from typing import List , Tuple
1+ from typing import List , Tuple , Union
22from ..component import Component
33from ...state import AllState , VehicleState , EntityRelation , EntityRelationEnum , Path , Trajectory , Route , ObjectFrameEnum , AgentState
44from ...utils import serialization
@@ -126,10 +126,13 @@ def detect_collision(curr_x: float, curr_y: float, curr_v: float, obj: AgentStat
126126
127127 return True , deceleration
128128
129- def detect_collision_analytical (r_pedestrain_y : float , p_vehicle_left_y_after_t : float , p_vehicle_right_y_after_t : float ) -> bool :
129+ def detect_collision_analytical (r_pedestrain_x : float , r_pedestrain_y : float , p_vehicle_left_y_after_t : float , p_vehicle_right_y_after_t : float , lateral_buffer : float ) -> Union [ bool , str ] :
130130 """Detects if a collision will occur with the given object and return deceleration to avoid it. Analytical"""
131-
132- if r_pedestrain_y > p_vehicle_left_y_after_t and r_pedestrain_y < p_vehicle_right_y_after_t :
131+ if r_pedestrain_x < 0 and abs (r_pedestrain_y ) > lateral_buffer :
132+ return False
133+ elif r_pedestrain_x < 0 :
134+ return 'max'
135+ if r_pedestrain_y >= p_vehicle_left_y_after_t and r_pedestrain_y <= p_vehicle_right_y_after_t :
133136 return True
134137
135138 return False
@@ -149,25 +152,30 @@ def get_minimum_deceleration_for_collision_avoidance(curr_x: float, curr_y: floa
149152 vehicle_width = 2
150153
151154 vehicle_buffer_x = 3.0
152- vehicle_buffer_y = 1.5
155+ vehicle_buffer_y = 1.0
153156
154- vehicle_front = curr_x + vehicle_length
155- vehicle_back = curr_x
156- vehicle_left = curr_y + vehicle_width / 2
157- vehicle_right = curr_y - vehicle_width / 2
157+ obj_x = obj_x - curr_x
158+ obj_y = obj_y - curr_y
158159
159- r_vehicle_front = vehicle_front - vehicle_front - vehicle_buffer_x
160- r_vehicle_back = vehicle_back - vehicle_front - vehicle_buffer_x
160+ curr_x = curr_x - curr_x
161+ curr_y = curr_y - curr_y
162+
163+ vehicle_front = curr_x + vehicle_length + vehicle_buffer_x
164+ vehicle_back = curr_x
165+ vehicle_left = curr_y - vehicle_width / 2
166+ vehicle_right = curr_y + vehicle_width / 2
167+
168+ r_vehicle_front = vehicle_front - vehicle_front
169+ r_vehicle_back = vehicle_back - vehicle_front
161170 r_vehicle_left = vehicle_left - vehicle_buffer_y
162171 r_vehicle_right = vehicle_right + vehicle_buffer_y
163172 r_vehicle_v_x = curr_v
164173 r_vehicle_v_y = 0
165174
166175 r_pedestrain_x = obj_x - vehicle_front
167- r_pedestrain_y = obj_y
176+ r_pedestrain_y = - obj_y
168177 r_pedestrain_v_x = obj_v_x
169- r_pedestrain_v_y = obj_v_y
170-
178+ r_pedestrain_v_y = - obj_v_y
171179
172180 r_velocity_x_from_vehicle = r_vehicle_v_x - r_pedestrain_v_x
173181 r_velocity_y_from_vehicle = r_vehicle_v_y - r_pedestrain_v_y
@@ -177,36 +185,42 @@ def get_minimum_deceleration_for_collision_avoidance(curr_x: float, curr_y: floa
177185 p_vehicle_left_y_after_t = r_vehicle_left + r_velocity_y_from_vehicle * t_to_r_pedestrain_x
178186 p_vehicle_right_y_after_t = r_vehicle_right + r_velocity_y_from_vehicle * t_to_r_pedestrain_x
179187
180- if not detect_collision_analytical (r_pedestrain_y , p_vehicle_left_y_after_t , p_vehicle_right_y_after_t ):
188+ collision_flag = detect_collision_analytical (r_pedestrain_x , r_pedestrain_y , p_vehicle_left_y_after_t , p_vehicle_right_y_after_t , vehicle_buffer_y )
189+ if collision_flag == False :
190+ print ("No collision" , curr_x , curr_y , r_pedestrain_x , r_pedestrain_y , r_vehicle_left , r_vehicle_right , p_vehicle_left_y_after_t , p_vehicle_right_y_after_t )
181191 return 0.0
192+ elif collision_flag == 'max' :
193+ return max_deceleration
182194
195+ print ("Collision" , curr_x , curr_y , r_pedestrain_x , r_pedestrain_y , r_vehicle_left , r_vehicle_right , p_vehicle_left_y_after_t , p_vehicle_right_y_after_t )
183196 yaw = None
184197 minimum_deceleration = None
185198 if yaw is None :
186- if math . abs (r_velocity_y_from_vehicle ) > 0.1 :
199+ if abs (r_velocity_y_from_vehicle ) > 0.1 :
187200 if r_velocity_y_from_vehicle > 0.1 :
188201 # Vehicle Left would be used to yield
189- r_pedestrain_y_temp = r_pedestrain_y + math . abs (r_vehicle_left )
202+ r_pedestrain_y_temp = r_pedestrain_y + abs (r_vehicle_left )
190203 elif r_velocity_y_from_vehicle < - 0.1 :
191204 # Vehicle Right would be used to yield
192- r_pedestrain_y_temp = r_pedestrain_y - math . abs (r_vehicle_right )
205+ r_pedestrain_y_temp = r_pedestrain_y - abs (r_vehicle_right )
193206
194207 softest_accleration = 2 * r_velocity_y_from_vehicle * (r_velocity_y_from_vehicle * r_pedestrain_x - r_velocity_x_from_vehicle * r_pedestrain_y_temp ) / r_pedestrain_y_temp ** 2
195208 peak_y = - (r_velocity_x_from_vehicle * r_velocity_y_from_vehicle ) / softest_accleration
196209 # if the peak is within the position of the pedestrian,
197210 # then it indicates the path had already collided with the pestrain,
198211 # and so the softest acceleration should be the one the peak of the path is the same as the pedestrain's x position
199212 # and the vehicle should be stopped exactly before the pedestrain's x position
200- if math . abs (peak_y ) > math . abs (r_pedestrain_y_temp ):
201- minimum_deceleration = math . abs (softest_accleration )
213+ if abs (peak_y ) > abs (r_pedestrain_y_temp ):
214+ minimum_deceleration = abs (softest_accleration )
202215 # else: the vehicle should be stopped exactly before the pedestrain's x position the same case as the pedestrain barely move laterally
203216
204217 if minimum_deceleration is None :
205218 minimum_deceleration = r_velocity_x_from_vehicle ** 2 / (2 * r_pedestrain_x )
206219 else :
207220 pass
208221
209- return math .max (math .min (minimum_deceleration , max_deceleration ), min_deceleration )
222+ print ("calculatedminimum_deceleration: " , minimum_deceleration )
223+ return max (min (minimum_deceleration , max_deceleration ), min_deceleration )
210224
211225
212226def longitudinal_plan (path : Path , acceleration : float , deceleration : float , max_speed : float , current_speed : float ,
0 commit comments