11from typing import List , Union
22from klampt .vis import scene
33from ..component import Component
4- from ...utils import serialization
54from ...state import Route , ObjectFrameEnum , AllState , VehicleState , Roadgraph , MissionObjective , MissionEnum , ObjectPose
65import numpy as np
76import requests
8- import json
97import time
8+ import yaml
109
1110
1211def check_distance (goal_pose : Union [ObjectPose , list ], current_pose : ObjectPose ):
@@ -35,14 +34,18 @@ def next_state(self):
3534
3635
3736class SummoningMissionPlanner (Component ):
38- def __init__ (self , use_webapp , webapp_url , goal , state_machine ):
37+ def __init__ (self , use_webapp , webapp_url , goal = None , state_machine = None ):
3938 self .state_machine = StateMachine ([eval (s ) for s in state_machine ])
40- self .goal_location = goal ['location' ]
41- self .goal_frame = goal ['frame' ]
39+
40+ # if use_webapp is True, goal should be None
41+ self .goal_location = goal ['location' ] if not use_webapp else None
42+ self .goal_frame = goal ['frame' ] if not use_webapp else None
4243 self .new_goal = False
4344 self .goal_pose = None
4445 self .start_pose = None
4546 self .start_time = time .time ()
47+ self .end_of_driving_route = None
48+ self .search_count = 0
4649
4750 self .flag_use_webapp = use_webapp
4851 self .url_status = f"{ webapp_url } /api/status"
@@ -94,7 +97,7 @@ def update(self, state: AllState):
9497 goal_location = None
9598 goal_frame = None
9699 else :
97- goal_location = [data ['lat ' ] , data ['lon ' ]]
100+ goal_location = [data ['lon ' ] , data ['lat ' ]]
98101 goal_frame = 'global'
99102 print ("Goal location:" , goal_location )
100103 print ("Goal frame:" , goal_frame )
@@ -110,6 +113,8 @@ def update(self, state: AllState):
110113 goal_location = self .goal_location
111114 goal_frame = self .goal_frame
112115
116+ start_vehicle_pose = state .start_vehicle_pose
117+
113118 if goal_frame == 'global' :
114119 self .goal_pose = ObjectPose (t = time .time (), x = goal_location [0 ], y = goal_location [1 ],
115120 frame = ObjectFrameEnum .GLOBAL )
@@ -125,7 +130,16 @@ def update(self, state: AllState):
125130 raise ValueError ("Invalid frame argument" )
126131
127132 if self .goal_pose :
128- self .goal_pose = self .goal_pose .to_frame (ObjectFrameEnum .START , start_pose_abs = state .start_vehicle_pose )
133+ if start_vehicle_pose .frame == ObjectFrameEnum .ABSOLUTE_CARTESIAN and goal_frame == 'global' :
134+ # For global map simulation test
135+ with open ("scenes/summoning_map_sim_setting.yaml" , "r" ) as f :
136+ data = yaml .safe_load (f )
137+ start = data ['start_pose_global' ]
138+ start_pose_global = ObjectPose (frame = ObjectFrameEnum .GLOBAL , t = start_vehicle_pose .t , x = start [0 ], y = start [1 ], yaw = start [2 ])
139+ self .goal_pose = self .goal_pose .to_frame (ObjectFrameEnum .START , start_pose_abs = start_pose_global )
140+ else :
141+ self .goal_pose = self .goal_pose .to_frame (ObjectFrameEnum .START , start_pose_abs = start_vehicle_pose )
142+ print ("Goal pose:" , self .goal_pose )
129143
130144 # Initiate state
131145 if mission is None :
@@ -137,34 +151,52 @@ def update(self, state: AllState):
137151 if self .new_goal :
138152 mission .goal_pose = self .goal_pose
139153 mission .type = self .state_machine .next_state ()
154+ self .new_goal = False
140155 print ("============== Next state:" , mission .type )
141156
142157 # Reach the end of the route, begin to search for parking
143- elif mission .type == MissionEnum .SUMMONING_DRIVE :
158+ # elif mission.type == MissionEnum.SUMMONING_DRIVE:
159+ elif mission .type == MissionEnum .SUMMON_DRIVING :
144160 mission .goal_pose = self .goal_pose
145161 if route :
146162 _ , closest_index = route .closest_point ([vehicle .pose .x , vehicle .pose .y ], edges = False )
147- if closest_index == len (route .points ) - 1 or check_distance (mission .goal_pose , vehicle .pose ) < 1 :
148- mission .type = self .state_machine .next_state ()
149- print ("============== Next state:" , mission .type )
163+ if closest_index == len (route .points ) - 1 or check_distance (mission .goal_pose , vehicle .pose ) < 5 :
164+ if vehicle .v < 0.1 :
165+ mission .type = self .state_machine .next_state ()
166+ print ("============== Next state:" , mission .type )
167+ self .end_of_driving_route = route .points [- 1 ]
168+ else :
169+ self .search_count += 1
150170
151171 # Finish parking, back to idle and wait for the next goal location
152172 elif mission .type == MissionEnum .PARALLEL_PARKING :
153173 if route :
154- _ , closest_index = route .closest_point ([vehicle .pose .x , vehicle .pose .y ], edges = False )
155- if closest_index == len (route .points ) - 1 or check_distance (route .points [- 1 ], vehicle .pose ) < 0.1 :
156- mission .type = self .state_machine .next_state ()
157- self .goal_pose = None
158- mission .goal_pose = self .goal_pose
159- print ("============== Next state:" , mission .type )
174+ if route .points [- 1 ] != self .end_of_driving_route :
175+ _ , closest_index = route .closest_point ([vehicle .pose .x , vehicle .pose .y ], edges = False )
176+ if vehicle .v < 0.01 and (closest_index >= len (route .points ) - 1 or check_distance (route .points [- 1 ], vehicle .pose ) < 0.5 ):
177+ # Set everything to idle
178+ mission .type = self .state_machine .next_state ()
179+ print ("============== Next state:" , mission .type )
180+ else :
181+ self .search_count += 1
160182
161183 else :
162184 raise ValueError ("Invalid mission type" )
163185
186+ # Can not find a path, stop mission.
187+ if mission .type != MissionEnum .IDLE :
188+ print ("Route searching times:" , self .search_count )
189+ if self .search_count > 10 :
190+ mission .type = MissionEnum .IDLE
191+ self .goal_pose = None
192+ self .goal_location = None
193+ self .goal_frame = None
194+ self .search_count = 0
195+
164196
165197 if self .flag_use_webapp :
166198 data = {
167- "status" : mission .planner_type .name
199+ "status" : mission .type .name
168200 }
169201 print ("POST:" , data )
170202 response = requests .post (url = self .url_status , json = data )
0 commit comments