File size: 3,990 Bytes
c09b668 8eaa027 c09b668 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
import numpy as np
import matplotlib.pyplot as plt
import argparse
import os
import glob
def load_data(log_path):
"""Load data from npz file"""
data = np.load(log_path)
return {
'states': data['states'],
'targets': data['targets'],
'costs': data['costs'],
'times': data['times'],
'actions': data['actions']
}
def plot_ball_trajectory(times, states, save_path):
"""Plot ball z-axis trajectory"""
ball_z = states[:, 0, 0] if states.ndim == 3 else states[:, 0]
plt.figure(figsize=(10, 6))
plt.plot(times, ball_z, 'b-', linewidth=2, label='Ball Z Position')
plt.xlabel('Time (s)')
plt.ylabel('Z Position (m)')
plt.title('Ball Trajectory')
plt.grid(True, alpha=0.3)
plt.legend()
plt.tight_layout()
plt.savefig(f"{save_path}/ball_trajectory.png", dpi=300)
plt.close()
def plot_reference_trajectory(times, targets, save_path):
"""Plot reference z-axis trajectory"""
ref_z = targets[:, 2] if targets.ndim == 2 else targets[:, 0, 2]
plt.figure(figsize=(10, 6))
plt.plot(times, ref_z, 'r--', linewidth=2, label='Reference Z Position')
plt.xlabel('Time (s)')
plt.ylabel('Z Position (m)')
plt.title('Reference Trajectory')
plt.grid(True, alpha=0.3)
plt.legend()
plt.tight_layout()
plt.savefig(f"{save_path}/reference_trajectory.png", dpi=300)
plt.close()
def plot_cost(times, costs, save_path):
"""Plot cost over time"""
plt.figure(figsize=(10, 6))
plt.plot(times, costs, 'g-', linewidth=2, label='Cost')
plt.xlabel('Time (s)')
plt.ylabel('Cost')
plt.title('Cost Over Time')
plt.grid(True, alpha=0.3)
plt.legend()
plt.tight_layout()
plt.savefig(f"{save_path}/cost.png", dpi=300)
plt.close()
def plot_actions(times, actions, save_path):
"""Plot actions over time"""
plt.figure(figsize=(10, 6))
plt.plot(times, actions, 'm-', linewidth=2, label='Action')
plt.xlabel('Time (s)')
plt.ylabel('Action Value')
plt.title('Actions Over Time')
plt.grid(True, alpha=0.3)
plt.legend()
plt.tight_layout()
plt.savefig(f"{save_path}/actions.png", dpi=300)
plt.close()
def plot_comparison(times, states, targets, save_path):
"""Plot ball and reference trajectories together"""
ball_z = states[:, 0, 0] if states.ndim == 3 else states[:, 0]
ref_z = targets[:, 2] if targets.ndim == 2 else targets[:, 0, 2]
plt.figure(figsize=(12, 6))
plt.plot(times, ball_z, 'b-', linewidth=2, label='Ball Z Position')
plt.plot(times, ref_z, 'r--', linewidth=2, label='Reference Z Position')
plt.xlabel('Time (s)')
plt.ylabel('Z Position (m)')
plt.title('Ball vs Reference Trajectory')
plt.grid(True, alpha=0.3)
plt.legend()
plt.tight_layout()
plt.savefig(f"{save_path}/comparison.png", dpi=300)
plt.close()
def main():
parser = argparse.ArgumentParser(description='Plot MPPI tracking results')
parser.add_argument('--log_dir',default='/home/lau/sim/DynaTraj/logs/bb/20250924_143716', type=str, help='Path to log directory')
args = parser.parse_args()
# Find npz file
npz_files = glob.glob(os.path.join(args.log_dir, "*.npz"))
if not npz_files:
print(f"No npz files found in {args.log_dir}")
return
npz_file = npz_files[0]
print(f"Loading data from {npz_file}")
# Load data
data = load_data(npz_file)
times = data['times']
states = data['states']
targets = data['targets']
costs = data['costs']
actions = data['actions']
# Create plots
print("Generating plots...")
plot_ball_trajectory(times, states, args.log_dir)
plot_reference_trajectory(times, targets, args.log_dir)
plot_cost(times, costs, args.log_dir)
plot_actions(times, actions, args.log_dir)
plot_comparison(times, states, targets, args.log_dir)
print(f"Plots saved to {args.log_dir}")
if __name__ == "__main__":
main()
|