66from scipy .optimize import minimize_scalar
77
88DEBUG = True # Set to False to disable debug output
9+ GEM_MODEL = 'e4' # e2 or e4
10+ buffer_x , buffer_y = 3 , 1
11+ # """ Vehicle Dimensions """
12+ if GEM_MODEL == 'e2' :
13+ # e2 axle dimensions, refer to GEMstack/knowledge/vehicle/model/gem_e2.urdf
14+ size_x , size_y = 2.53 , 1.35 # measured from base_link.STL
15+ lx_f2r = 0.88 + 0.87 # distance between front axle and rear axle, from gem_e2.urdf wheel_links
16+ ly_axle = 0.6 * 2 # length of axle
17+ l_rear_axle_to_front = 1.28 + 0.87 # measured from base_link.STL
18+ l_rear_axle_to_rear = size_x - l_rear_axle_to_front
19+ elif GEM_MODEL == 'e4' :
20+ # e4 axle dimensions, refer to GEMstack/knowledge/vehicle/model/gem_e4.urdf
21+ size_x , size_y = 3.35 , 1.35 # measured from base_link.STL
22+ lx_f2r = 1.2746 + 1.2904 # distance between front axle and rear axle, from gem_e4.urdf wheel_links
23+ ly_axle = 0.6545 * 2 # length of front and rear axles, from gem_e4.urdf wheel_links
24+ l_rear_axle_to_front = 1.68 + 1.29 # measured from base_link.STL
25+ l_rear_axle_to_rear = size_x - l_rear_axle_to_front
26+ # add buffer to outer dimensions, defined by half of the x, y buffer area dimensions
27+ buffer_front = l_rear_axle_to_front + buffer_x
28+ buffer_rear = - (l_rear_axle_to_rear + buffer_x )
29+ buffer_left = size_y / 2 + buffer_y
30+ buffer_right = - (size_y / 2 + buffer_y )
31+ # comfortable deceleration for lookahead time calculation
32+ comfort_decel = 2.0
33+
934
1035class PedestrianYielder (Component ):
1136 """Yields for all pedestrians in the scene.
@@ -22,7 +47,7 @@ def state_inputs(self):
2247 def state_outputs (self ):
2348 return ['relations' ]
2449
25- def update (self , agents : Dict [str , AgentState ], vehicle : VehicleState ) -> List [EntityRelation ]:
50+ def update (self , agents : Dict [str , AgentState ], vehicle : VehicleState ) -> List [EntityRelation ]:
2651 if DEBUG :
2752 print ("PedestrianYielder vehicle pose:" , vehicle .pose , vehicle .v )
2853 res = []
@@ -34,7 +59,8 @@ def update(self, agents: Dict[str, AgentState], vehicle : VehicleState) -> List[
3459 if a .type == AgentEnum .PEDESTRIAN :
3560 check , t_min , min_dist , pt_min = check_collision_in_vehicle_frame (a , vehicle )
3661 if DEBUG :
37- print (f"[DEBUG] ID: { n } \t relation:{ check } , minimum distance:{ min_dist } , time to min_dist: { t_min } , point of min_dist:{ pt_min } " )
62+ print (
63+ f"[DEBUG] ID: { n } \t relation:{ check } , minimum distance:{ min_dist } , time to min_dist: { t_min } , point of min_dist:{ pt_min } " )
3864 # collision may occur, slow down
3965 if check == 'YIELD' :
4066 res .append (EntityRelation (type = EntityRelationEnum .YIELDING , obj1 = '' , obj2 = n ))
@@ -45,75 +71,51 @@ def update(self, agents: Dict[str, AgentState], vehicle : VehicleState) -> List[
4571 return res
4672
4773
48- # """ Vehicle Dimensions """
49- # vehicle_model = "e2" # e2 or e4
50- # # vehicle dimensions and buffer area
51- # if vehicle_model == 'e2':
52- # # e2 axle dimensions, refer to GEMstack/knowledge/vehicle/model/gem_e2.urdf
53- # size_x, size_y = 2.53, 1.35 # measured from base_link.STL
54- # lx_f2r = 0.88 + 0.87 # distance between front axle and rear axle, from gem_e2.urdf wheel_links
55- # ly_axle = 0.6 * 2 # length of axle
56- # l_rear_axle_to_front = 1.28 + 0.87 # measured from base_link.STL
57- # l_rear_axle_to_rear = size_y - l_rear_axle_to_front
58- # elif vehicle_model == 'e4':
59- # # e4 axle dimensions, refer to GEMstack/knowledge/vehicle/model/gem_e4.urdf
60- # size_x, size_y = 3.35, 1.35 # measured from base_link.STL
61- # lx_f2r = 1.2746 + 1.2904 # distance between front axle and rear axle, from gem_e4.urdf wheel_links
62- # ly_axle = 0.6545 * 2 # length of front and rear axles, from gem_e4.urdf wheel_links
63- # l_rear_axle_to_front = 1.68 + 1.29 # measured from base_link.STL
64- # l_rear_axle_to_rear = size_y - l_rear_axle_to_front
65- # # add buffer to outer dimensions, defined by half of the x, y buffer area dimensions
66- # buffer_x, buffer_y = 3, 1
67- # buffer = size_x / 2 + buffer_x, size_y / 2 + buffer_y
68-
69-
7074""" Planning in vehicle frame without waypoints """
7175def check_collision_in_vehicle_frame (agent : AgentState , vehicle : VehicleState ):
72- buffer = 2.53 / 2 + 3 , 1.35 / 2 + 1 # e2 buffer area
73- time_scale = 100 # TODO: adjust if necessary
7476 xp , yp = agent .pose .x , agent .pose .y
7577 vx , vy = agent .velocity [:2 ]
78+ xv = vehicle .pose .x
79+ yv = vehicle .pose .y
80+ yaw = vehicle .pose .yaw
81+ vel = vehicle .v
82+ # time to stop from current velocity with comfortable deceleration
83+ t_look = vel / comfort_decel
7684 # calculate relative pedestrian position and velocity in vehicle frame
7785 if agent .pose .frame == ObjectFrameEnum .CURRENT :
7886 # xp, yp, vx, vy are already in vehicle frame
7987 pass
8088 elif agent .pose .frame == ObjectFrameEnum .START :
8189 # convert xp, yp, vx, vy to vehicle frame
82- xv = vehicle .pose .x
83- yv = vehicle .pose .y
84- yaw = vehicle .pose .yaw
85- vel = vehicle .v
8690 R = np .array ([[np .cos (yaw ), np .sin (yaw )], [- np .sin (yaw ), np .cos (yaw )]], dtype = np .float32 )
8791 dx , dy = xp - xv , yp - yv
88- dvx , dvy = vx - vel * np .cos (yaw ), vy - vel * np .sin (yaw )
92+ dvx , dvy = vx - vel * np .cos (yaw ), vy - vel * np .sin (yaw )
93+ # pedestrian pose and velocity in vehicle frame
8994 xp , yp = np .dot (R , np .array ([dx , dy ]))
9095 vx , vy = np .dot (R , np .array ([dvx , dvy ]))
9196
92- # If pedestrian alphas the car
93- # condition to stop for ped in vehic. space
94- # still testing behavior
95- if abs (xp ) <= buffer [0 ] and abs (yp ) <= buffer [1 ]:
97+ # If pedestrian already in buffer area
98+ if buffer_rear <= xp <= buffer_front and buffer_right <= yp <= buffer_left :
9699 return 'STOP' , 0 , 0 , (xp , yp )
97-
98- t_min , min_dist , pt_min = find_min_distance_and_time (xp , yp , vx , vy , buffer )
100+ t_min , min_dist , pt_min = find_min_distance_and_time (xp , yp , vx , vy )
99101 # if the minimum distance between the position and the buffer area is less than 0, than a collision is expected
100- check = None
101102 if min_dist is not None :
102103 if min_dist <= 0 :
103104 if t_min <= 0 :
104105 check = 'STOP'
105- elif t_min <= 2 :
106+ elif t_min <= t_look :
106107 check = 'YIELD'
107108 else :
108109 check = 'RUN'
109110 else :
110- check = 'run'
111+ check = 'RUN'
112+ else :
113+ check = 'RUN'
111114 return check , t_min , min_dist , pt_min
112115
113116
114- def find_min_distance_and_time (xp , yp , vx , vy , buffer ):
117+ def find_min_distance_and_time (xp , yp , vx , vy ):
115118 # path function: Ax + By + C = vy * x - vx * y + (yp * vx - xp * vy) = 0
116- x_buff , y_buff = buffer
117119 vx = vx if vx != 0 else 1e-6
118120 vy = vy if vy != 0 else 1e-6
119121 A = vy
@@ -133,102 +135,102 @@ def point_dist(x1, y1, x2, y2):
133135 return np .sqrt ((x1 - x2 ) ** 2 + (y1 - y2 ) ** 2 )
134136
135137 """ Compute intersections at the front, left and right edge """
136- if xp >= x_buff :
138+ if xp >= buffer_front :
137139 # front edge intersection: x = x_buff = xt = xp + vx * t_f
138- t_f = (x_buff - xp ) / vx
140+ t_f = (buffer_front - xp ) / vx
139141 yt = yp + vy * t_f
140- if t_f < 0 : # object moving away with higher speed than vehicle, start point has minimum distance
142+ if t_f < 0 : # object moving away with higher speed than vehicle, start point has minimum distance
141143 t_min = 0
142144 pt_min = xp , yp
143- if - y_buff <= yp <= y_buff :
144- min_dist = xp - x_buff # the distance to the front edge
145- elif yt > y_buff :
146- min_dist = point_dist (xp , yp , x_buff , y_buff ) # the distance to front left corner
145+ if buffer_right <= yp <= buffer_left :
146+ min_dist = xp - buffer_front # the distance to the front edge
147+ elif yt > buffer_left :
148+ min_dist = point_dist (xp , yp , buffer_front , buffer_left ) # the distance to front left corner
147149 else :
148- min_dist = point_dist (xp , yp , x_buff , - y_buff ) # the distance to front right corner
150+ min_dist = point_dist (xp , yp , buffer_front , buffer_right ) # the distance to front right corner
149151 else :
150- if - y_buff <= yt <= y_buff : # intersect at front edge, is collision
152+ if buffer_right <= yt <= buffer_left : # intersect at front edge, is collision
151153 t_min = t_f
152154 min_dist = 0
153- pt_min = x_buff , yt
154- elif yt > y_buff : # intersect at front left
155+ pt_min = buffer_front , yt
156+ elif yt > buffer_left : # intersect at front left
155157 if yp <= yt :
156- min_dist , pt_min = point_to_line (x_buff , y_buff , A , B , C )
158+ min_dist , pt_min = point_to_line (buffer_front , buffer_left , A , B , C )
157159 t_min = (pt_min [0 ] - xp ) / vx
158- else : # left edge interaction: y = y_buff = yt = yp + vy * t_l
159- t_l = (y_buff - yp ) / vy
160+ else : # left edge interaction: y = y_buff = yt = yp + vy * t_l
161+ t_l = (buffer_left - yp ) / vy
160162 xt = xp + vx * t_l
161- if - x_buff <= xt <= x_buff : # intersect at left edge, is collision
163+ if buffer_rear <= xt <= buffer_front : # intersect at left edge, is collision
162164 t_min = t_l
163165 min_dist = 0
164- pt_min = xt , y_buff
166+ pt_min = xt , buffer_left
165167 else :
166- min_dist , pt_min = point_to_line (- x_buff , y_buff , A , B , C )
168+ min_dist , pt_min = point_to_line (buffer_rear , buffer_left , A , B , C )
167169 t_min = (pt_min [0 ] - xp ) / vx
168- else : # intersect at front right
170+ else : # intersect at front right
169171 if yp >= yt :
170- min_dist , pt_min = point_to_line (x_buff , - y_buff , A , B , C )
172+ min_dist , pt_min = point_to_line (buffer_front , buffer_right , A , B , C )
171173 t_min = (pt_min [0 ] - xp ) / vx
172- else : # left edge interaction: y = y_buff = yt = yp + vy * t_l
173- t_r = (- y_buff - yp ) / vy
174+ else : # right edge interaction: y = y_buff = yt = yp + vy * t_l
175+ t_r = (buffer_right - yp ) / vy
174176 xt = xp + vx * t_r
175- if - x_buff <= xt <= x_buff : # intersect at left edge, is collision
177+ if buffer_rear <= xt <= buffer_front : # intersect at right edge, is collision
176178 t_min = t_r
177179 min_dist = 0
178- pt_min = xt , - y_buff
180+ pt_min = xt , buffer_right
179181 else :
180- min_dist , pt_min = point_to_line (- x_buff , - y_buff , A , B , C )
182+ min_dist , pt_min = point_to_line (buffer_rear , buffer_right , A , B , C )
181183 t_min = (pt_min [0 ] - xp ) / vx
182184 else :
183- if yp >= y_buff :
185+ if yp >= buffer_left :
184186 # left edge interaction: y = y_buff = yt = yp + vy * t_l
185- t_l = (y_buff - yp ) / vy
187+ t_l = (buffer_left - yp ) / vy
186188 xt = xp + vx * t_l
187- if t_l < 0 : # object moving away, start point has minimum distance
189+ if t_l < 0 : # object moving away, start point has minimum distance
188190 t_min = 0
189191 pt_min = xp , yp
190- if - x_buff <= xp <= x_buff :
191- min_dist = yp - y_buff # the distance to the left edge
192+ if buffer_rear <= xp <= buffer_front :
193+ min_dist = yp - buffer_left # the distance to the left edge
192194 else :
193- min_dist = point_dist (xp , yp , - x_buff , y_buff ) # the distance to rear right corner
195+ min_dist = point_dist (xp , yp , buffer_rear , buffer_left ) # the distance to rear right corner
194196 else :
195- if - x_buff <= xt <= x_buff : # intersect at left edge, is collision
197+ if buffer_rear <= xt <= buffer_front : # intersect at left edge, is collision
196198 t_min = t_l
197199 min_dist = 0
198- pt_min = xt , y_buff
199- elif xt > x_buff :
200- min_dist , pt_min = point_to_line (x_buff , y_buff , A , B , C )
200+ pt_min = xt , buffer_left
201+ elif xt > buffer_front :
202+ min_dist , pt_min = point_to_line (buffer_front , buffer_left , A , B , C )
201203 t_min = (pt_min [0 ] - xp ) / vx
202204 else :
203- min_dist , pt_min = point_to_line (- x_buff , y_buff , A , B , C )
205+ min_dist , pt_min = point_to_line (buffer_rear , buffer_left , A , B , C )
204206 t_min = (pt_min [0 ] - xp ) / vx
205- elif yp <= - y_buff :
207+ elif yp <= buffer_right :
206208 # right edge interaction: y = -y_buff = yt = yp + vy * t_l
207- t_r = (- y_buff - yp ) / vy
209+ t_r = (buffer_right - yp ) / vy
208210 xt = xp + vx * t_r
209- if t_r < 0 : # object moving away, start point has minimum distance
211+ if t_r < 0 : # object moving away, start point has minimum distance
210212 t_min = 0
211213 pt_min = xp , yp
212- if - x_buff <= xp <= x_buff :
213- min_dist = - yp - y_buff # the distance to the right edge
214+ if buffer_rear <= xp <= buffer_front :
215+ min_dist = - yp - buffer_right # the distance to the right edge
214216 else :
215- min_dist = point_dist (xp , yp , - x_buff , y_buff ) # the distance to rear right corner
217+ min_dist = point_dist (xp , yp , buffer_rear , buffer_right ) # the distance to rear right corner
216218 else :
217- if - x_buff <= xt <= x_buff : # intersect at left edge, is collision
219+ if buffer_rear <= xt <= buffer_front : # intersect at left edge, is collision
218220 t_min = t_r
219221 min_dist = 0
220- pt_min = xt , - y_buff
221- elif xt > x_buff :
222- min_dist , pt_min = point_to_line (x_buff , - y_buff , A , B , C )
222+ pt_min = xt , buffer_right
223+ elif xt > buffer_front :
224+ min_dist , pt_min = point_to_line (buffer_front , buffer_right , A , B , C )
223225 t_min = (pt_min [0 ] - xp ) / vx
224226 else :
225- min_dist , pt_min = point_to_line (- x_buff , - y_buff , A , B , C )
227+ min_dist , pt_min = point_to_line (buffer_rear , buffer_right , A , B , C )
226228 t_min = (pt_min [0 ] - xp ) / vx
227- elif xp >= - x_buff :
229+ elif xp >= buffer_rear :
228230 t_min = 0
229231 min_dist = - 1
230232 pt_min = xp , yp
231- else : # rear position, should not be seen by the front camera
233+ else : # rear position, should not be seen by the front camera
232234 t_min = None
233235 min_dist = None
234236 pt_min = None
@@ -578,4 +580,3 @@ def point_dist(x1, y1, x2, y2):
578580# if yielding == True and distance is not None:
579581# if distance < dist_min:
580582# dist_min = distance
581-
0 commit comments