Skip to content

Commit 1693a77

Browse files
author
maoyifei
committed
add planning time metrics
1 parent 0c82b6e commit 1693a77

1 file changed

Lines changed: 29 additions & 33 deletions

File tree

GEMstack/onboard/planning/parking_route_planner.py

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -447,22 +447,23 @@ def __init__(self):
447447
os.makedirs(self.logs_dir, exist_ok=True)
448448
self.success_file = os.path.join(self.logs_dir, 'parking_success.txt')
449449

450-
def save_parking_message(self, success: bool):
450+
def save_parking_message(self, success: bool, planning_time: float = None):
451451
"""Save a parking status message to a text file.
452452
453453
Args:
454454
success (bool): Whether parking was successful
455+
planning_time (float): Time taken by A* planner in seconds
455456
"""
456457
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
457458
status = "successful" if success else "unsuccessful"
458-
message = f"Parking {status} at {timestamp}\n"
459+
message = f"Parking {status} at {timestamp}"
460+
if planning_time is not None:
461+
message += f" (Planning time: {planning_time:.2f} seconds)"
462+
message += "\n"
459463

460464
try:
461465
with open(self.success_file, 'a') as f:
462466
f.write(message)
463-
f.write(success)
464-
f.write("End")
465-
print(f"Parking status saved to {self.success_file}")
466467
except Exception as e:
467468
print(f"Error saving parking status: {e}")
468469

@@ -535,11 +536,11 @@ def is_successfully_parked(self, final_state: List[float], goal_pose: ObjectPose
535536
# Need exactly 4 cones to form a parking spot
536537
if len(parking_spot_vertices) != 4:
537538
print("Warning: Not exactly 4 cones found for parking spot")
538-
try:
539-
with open(self.success_file, 'a') as f:
540-
f.write("No Four Cones")
541-
except Exception as e:
542-
print(f"Error saving parking status: {e}")
539+
# try:
540+
# with open(self.success_file, 'a') as f:
541+
# f.write("No Four Cones")
542+
# except Exception as e:
543+
# print(f"Error saving parking status: {e}")
543544
return False
544545

545546
# Order the vertices to form a proper polygon
@@ -560,23 +561,23 @@ def is_successfully_parked(self, final_state: List[float], goal_pose: ObjectPose
560561
for cone_object in cone_objects:
561562
if collisions.polygon_intersects_polygon_2d(vehicle_polygon, cone_object.polygon_parent()):
562563
print("Vehicle collides with a cone")
563-
try:
564-
with open(self.success_file, 'a') as f:
565-
f.write("Collides with Cone")
566-
except Exception as e:
567-
print(f"Error saving parking status: {e}")
564+
# try:
565+
# with open(self.success_file, 'a') as f:
566+
# f.write("Collides with Cone")
567+
# except Exception as e:
568+
# print(f"Error saving parking status: {e}")
568569
return False
569570

570571
# Then check if vehicle is contained within parking spot
571572
if not collisions.polygon_contains_polygon_2d(ordered_vertices, vehicle_polygon):
572573
print("Vehicle is not contained within parking spot")
573-
try:
574-
with open(self.success_file, 'a') as f:
575-
f.write(f"\nOrdered Vertices{ordered_vertices}")
576-
f.write(f"\nVehicle_Polygon{vehicle_polygon}")
577-
f.write("\nNot Contained with parking spot")
578-
except Exception as e:
579-
print(f"Error saving parking status: {e}")
574+
# try:
575+
# with open(self.success_file, 'a') as f:
576+
# f.write(f"\nOrdered Vertices{ordered_vertices}")
577+
# f.write(f"\nVehicle_Polygon{vehicle_polygon}")
578+
# f.write("\nNot Contained with parking spot")
579+
# except Exception as e:
580+
# print(f"Error saving parking status: {e}")
580581
return False
581582

582583
# Finally check if final orientation is close enough to goal orientation
@@ -671,9 +672,12 @@ def update(self, state : AllState) -> Route:
671672
self.planner.obstacles = list(all_obstacles.values())
672673
self.planner.vehicle = vehicle
673674

675+
# Measure planning time
676+
planner_start_time = time.time()
674677
# Compute the new trajectory and return it
675678
res = list(self.planner.astar(start_state, goal_state, reversePath=False, iterations=self.iterations))
676-
# points = [state[:2] for state in res] # change here to return the theta as well
679+
planning_time = time.time() - planner_start_time
680+
677681
points = []
678682
for state in res:
679683
points.append((state[0], state[1]))
@@ -690,22 +694,14 @@ def update(self, state : AllState) -> Route:
690694
# Check if final position in trajectory is within parking spot
691695
if len(res) > 0:
692696
final_state = res[-1] # Get the last state from the trajectory
693-
try:
694-
with open(self.success_file, 'a') as f:
695-
f.write("Final State")
696-
f.write(str(final_state))
697-
f.write("Final State End")
698-
print(f"Parking status saved to {self.success_file}")
699-
except Exception as e:
700-
print(f"Error saving parking status: {e}")
701697
self.parking_success = self.is_successfully_parked(final_state, goal_pose, agents)
702698
if self.parking_success:
703699
print("Final position is within parking spot!")
704700
else:
705701
print("Final position is not within parking spot")
706702

707-
# Save parking status
708-
self.save_parking_message(self.parking_success)
703+
# Save parking status with planning time
704+
self.save_parking_message(self.parking_success, planning_time)
709705
else:
710706
print("No trajectory generated")
711707

0 commit comments

Comments
 (0)