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()