Skip to content

Commit 8aa183f

Browse files
author
maoyifei
committed
add save to file function
1 parent 4dec9b2 commit 8aa183f

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

GEMstack/onboard/planning/parking_route_planner.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
import time
1717
import numpy as np
1818
import math
19+
import json
20+
import os
21+
from datetime import datetime
1922

2023
def normalize_yaw(yaw):
2124
"""Normalize yaw angle to [-pi, pi]"""
@@ -437,6 +440,53 @@ def __init__(self):
437440
self.parking_success = False
438441
self.velocity_threshold = 0.1 # m/s
439442
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}")
440490

441491
def is_successfully_parked(self, vehicle_state: VehicleState, goal_pose: ObjectPose, obstacles: Dict[str, Obstacle]) -> bool:
442492
"""Check if the vehicle is successfully parked in the parking spot.
@@ -573,6 +623,8 @@ def update(self, state : AllState) -> Route:
573623
self.parking_success = self.is_successfully_parked(vehicle, goal_pose, all_obstacles)
574624
if self.parking_success:
575625
print("Successfully parked!")
626+
# Save parking status when successful
627+
self.save_parking_status(vehicle, goal_pose, True)
576628
else:
577629
print("Not yet successfully parked")
578630

0 commit comments

Comments
 (0)