|
16 | 16 | import time |
17 | 17 | import numpy as np |
18 | 18 | import math |
| 19 | +import json |
| 20 | +import os |
| 21 | +from datetime import datetime |
19 | 22 |
|
20 | 23 | def normalize_yaw(yaw): |
21 | 24 | """Normalize yaw angle to [-pi, pi]""" |
@@ -437,6 +440,53 @@ def __init__(self): |
437 | 440 | self.parking_success = False |
438 | 441 | self.velocity_threshold = 0.1 # m/s |
439 | 442 | self.orientation_threshold = math.radians(10) # 10 degrees |
| 443 | + |
| 444 | + # Create logs directory if it doesn't exist |
| 445 | + self.logs_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'logs') |
| 446 | + os.makedirs(self.logs_dir, exist_ok=True) |
| 447 | + |
| 448 | + def save_parking_status(self, vehicle_state: VehicleState, goal_pose: ObjectPose, parking_success: bool): |
| 449 | + """Save parking status and metrics to a JSON file. |
| 450 | + |
| 451 | + Args: |
| 452 | + vehicle_state (VehicleState): Current state of the vehicle |
| 453 | + goal_pose (ObjectPose): Goal parking pose |
| 454 | + parking_success (bool): Whether parking was successful |
| 455 | + """ |
| 456 | + # Create timestamp for filename |
| 457 | + timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") |
| 458 | + filename = os.path.join(self.logs_dir, f'parking_status_{timestamp}.json') |
| 459 | + |
| 460 | + # Calculate metrics |
| 461 | + position_error = np.linalg.norm(np.array([vehicle_state.pose.x, vehicle_state.pose.y]) - |
| 462 | + np.array([goal_pose.x, goal_pose.y])) |
| 463 | + orientation_error = abs(normalize_yaw(vehicle_state.pose.yaw - goal_pose.yaw)) |
| 464 | + |
| 465 | + # Create data dictionary |
| 466 | + data = { |
| 467 | + 'timestamp': timestamp, |
| 468 | + 'parking_success': parking_success, |
| 469 | + 'vehicle_state': { |
| 470 | + 'position': [vehicle_state.pose.x, vehicle_state.pose.y], |
| 471 | + 'orientation': vehicle_state.pose.yaw, |
| 472 | + 'velocity': vehicle_state.v |
| 473 | + }, |
| 474 | + 'goal_state': { |
| 475 | + 'position': [goal_pose.x, goal_pose.y], |
| 476 | + 'orientation': goal_pose.yaw |
| 477 | + }, |
| 478 | + 'metrics': { |
| 479 | + 'position_error': position_error, |
| 480 | + 'orientation_error': orientation_error, |
| 481 | + 'velocity': vehicle_state.v |
| 482 | + } |
| 483 | + } |
| 484 | + |
| 485 | + # Save to JSON file |
| 486 | + with open(filename, 'w') as f: |
| 487 | + json.dump(data, f, indent=4) |
| 488 | + |
| 489 | + print(f"Parking status saved to {filename}") |
440 | 490 |
|
441 | 491 | def is_successfully_parked(self, vehicle_state: VehicleState, goal_pose: ObjectPose, obstacles: Dict[str, Obstacle]) -> bool: |
442 | 492 | """Check if the vehicle is successfully parked in the parking spot. |
@@ -573,6 +623,8 @@ def update(self, state : AllState) -> Route: |
573 | 623 | self.parking_success = self.is_successfully_parked(vehicle, goal_pose, all_obstacles) |
574 | 624 | if self.parking_success: |
575 | 625 | print("Successfully parked!") |
| 626 | + # Save parking status when successful |
| 627 | + self.save_parking_status(vehicle, goal_pose, True) |
576 | 628 | else: |
577 | 629 | print("Not yet successfully parked") |
578 | 630 |
|
|
0 commit comments