File size: 10,898 Bytes
f99e13f | 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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | import h5py
import numpy as np
import matplotlib.pyplot as plt
import os
from datetime import datetime
PLOT_SINGLE_JOINT = False # Whether to plot each joint separately
def smooth_curve(data, window_size=11):
"""Simple moving average filter"""
if window_size < 2:
return data
window = np.ones(int(window_size))/float(window_size)
# 'valid' mode will shorten the length, use 'edge' padding to keep the length unchanged
return np.convolve(data, window, mode='same')
def calc_numeric_velocity(time_list, position_list):
dt = np.diff(time_list)
dtheta = np.diff(position_list)
dt[dt == 0] = 0.005
vel = dtheta / dt
t_mid = (time_list[:-1] + time_list[1:]) / 2
return t_mid, vel
def plot_data(filename):
# 1. Read data
with h5py.File(f'{filename}', 'r') as f:
command_time_list = f['command_time_list'][:]
command_val_list = f['command_val_list'][:]
motion_name = f['motion_name'][()].decode('utf-8') # Get motion name
current_time = f['current_time'][()].decode('utf-8') # Get current time string
joint_time_list_left = f['robot1/joint_time_list'][:]
joint_val_list_left = f['robot1/joint_angle_list'][:] # shape: [N, 7]
joint_time_list_right = f['robot2/joint_time_list'][:]
joint_val_list_right = f['robot2/joint_angle_list'][:] # shape: [N, 7]
joint_velocity_left = f['robot1/joint_velocity_list'][:]
joint_current_left = f['robot1/joint_current_list'][:]
joint_temperature_left = f['robot1/joint_temperature_list'][:]
joint_velocity_right = f['robot2/joint_velocity_list'][:]
joint_current_right = f['robot2/joint_current_list'][:]
joint_temperature_right = f['robot2/joint_temperature_list'][:]
print(f"Loaded data: {len(command_time_list)} command points")
print(f"LeftArm joint data shape: {joint_val_list_left.shape}")
print(f"RightArm joint data shape: {joint_val_list_right.shape}")
print(f"LeftArm joint time points: {len(joint_time_list_left)}")
print(f"RightArm joint time points: {len(joint_time_list_right)}")
# 2. Generate save directory
parent_dir = os.path.abspath(os.path.dirname(__file__))
plot_data_dir = os.path.join(parent_dir, 'plot_data', motion_name + current_time)
os.makedirs(plot_data_dir, exist_ok=True)
print(f"saving images in: {plot_data_dir}")
if PLOT_SINGLE_JOINT:
# 3. Plot 14 images separately
for i in range(7):
# leftarm
plt.figure(figsize=(10, 5))
plt.plot(command_time_list, command_val_list[:, i], label='Command (LeftArm, rad)', color='b', linestyle='-')
plt.plot(joint_time_list_left, joint_val_list_left[:, i], label=f'LeftArm Joint {i+1} (rad)', color='r', linestyle='--')
plt.xlabel('Elapsed Time (s)')
plt.ylabel('Angle (rad)')
plt.title(f'Command vs LeftArm Joint {i+1} (rad)')
plt.legend(loc='best', fontsize=10)
plt.grid(True)
plt.tight_layout()
save_path = os.path.join(plot_data_dir, f'leftarm_joint_{i+1}.png')
plt.savefig(save_path)
plt.close()
print(f"image saved: {save_path}")
# rightarm
plt.figure(figsize=(10, 5))
plt.plot(command_time_list, command_val_list[:, i+7], label='Command (RightArm, rad)', color='b', linestyle='-')
plt.plot(joint_time_list_right, joint_val_list_right[:, i], label=f'RightArm Joint {i+1} (rad)', color='g', linestyle='--')
plt.xlabel('Elapsed Time (s)')
plt.ylabel('Angle (rad)')
plt.title(f'Command vs RightArm Joint {i+1} (rad)')
plt.legend(loc='best', fontsize=10)
plt.grid(True)
plt.tight_layout()
save_path = os.path.join(plot_data_dir, f'rightarm_joint_{i+1}.png')
plt.savefig(save_path)
plt.close()
print(f"image saved: {save_path}")
# 4. alldata.png: 14 subplots (7 left + 7 right)
fig, axs = plt.subplots(14, 1, figsize=(14, 32), sharex=True)
for i in range(7):
axs[i].plot(command_time_list, command_val_list[:, i], label='Command (LeftArm, rad)', color='b', linestyle='-')
axs[i].plot(joint_time_list_left, joint_val_list_left[:, i], label=f'LeftArm Joint {i+1} (rad)', color='r', linestyle='--')
axs[i].set_ylabel('Angle (rad)')
axs[i].set_title(f'LeftArm Joint {i+1}')
axs[i].legend(loc='best', fontsize=9)
axs[i].grid(True)
for i in range(7):
axs[i+7].plot(command_time_list, command_val_list[:, i+7], label='Command (RightArm, rad)', color='b', linestyle='-')
axs[i+7].plot(joint_time_list_right, joint_val_list_right[:, i], label=f'RightArm Joint {i+1} (rad)', color='g', linestyle='--')
axs[i+7].set_ylabel('Angle (rad)')
axs[i+7].set_title(f'RightArm Joint {i+1}')
axs[i+7].legend(loc='best', fontsize=9)
axs[i+7].grid(True)
axs[-1].set_xlabel('Elapsed Time (s)')
fig.suptitle(f'Command and Actual Angle || {motion_name}', fontsize=18)
plt.tight_layout(rect=[0, 0, 1, 0.97])
alldata_path = os.path.join(plot_data_dir, 'alldata.png')
plt.savefig(alldata_path)
plt.close()
print(f"all joints plot saved: {alldata_path}")
# ==== Plot "raw velocity" ====
fig, axs = plt.subplots(14, 1, figsize=(14, 32), sharex=True)
for i in range(7):
orig_vel_left = joint_velocity_left[:, i]
axs[i].plot(joint_time_list_left, orig_vel_left, label=f'LeftArm Joint {i+1} (deg/s)', color='r')
axs[i].set_ylabel('Velocity (deg/s)')
axs[i].set_title(f'LeftArm Joint {i+1}')
axs[i].legend(loc='best', fontsize=9)
axs[i].grid(True)
for i in range(7):
orig_vel_right = joint_velocity_right[:, i]
axs[i+7].plot(joint_time_list_right, orig_vel_right, label=f'RightArm Joint {i+1} (deg/s)', color='g')
axs[i+7].set_ylabel('Velocity (deg/s)')
axs[i+7].set_title(f'RightArm Joint {i+1}')
axs[i+7].legend(loc='best', fontsize=9)
axs[i+7].grid(True)
axs[-1].set_xlabel('Elapsed Time (s)')
fig.suptitle(f'Raw Joint Velocity (deg/s) || {motion_name}', fontsize=18)
plt.tight_layout(rect=[0, 0, 1, 0.97])
raw_vel_path = os.path.join(plot_data_dir, 'alldata_velocity_raw.png')
plt.savefig(raw_vel_path)
plt.close()
print(f"raw velocity plot saved: {raw_vel_path}")
# ==== Plot compare: numerical diff & smoothing vs raw velocity ====
fig, axs = plt.subplots(14, 1, figsize=(14, 32), sharex=True)
for i in range(7):
t_left, num_vel_left = calc_numeric_velocity(joint_time_list_left, joint_val_list_left[:, i])
num_vel_left_deg = num_vel_left
num_vel_left_smooth = smooth_curve(num_vel_left_deg, window_size=11)
orig_vel_left = joint_velocity_left[:, i]
axs[i].plot(t_left, num_vel_left_smooth, label=f'NumDiff Smoothed (deg/s)', color='orange', linestyle='-')
axs[i].plot(joint_time_list_left, orig_vel_left, label=f'Raw (deg/s)', color='r', linestyle='--')
axs[i].set_ylabel('Velocity (deg/s)')
axs[i].set_title(f'LeftArm Joint {i+1}')
axs[i].legend(loc='best', fontsize=9)
axs[i].grid(True)
for i in range(7):
t_right, num_vel_right = calc_numeric_velocity(joint_time_list_right, joint_val_list_right[:, i])
num_vel_right_deg = num_vel_right
num_vel_right_smooth = smooth_curve(num_vel_right_deg, window_size=11)
orig_vel_right = joint_velocity_right[:, i]
axs[i+7].plot(t_right, num_vel_right_smooth, label=f'NumDiff Smoothed (deg/s)', color='orange', linestyle='-')
axs[i+7].plot(joint_time_list_right, orig_vel_right, label=f'Raw (deg/s)', color='g', linestyle='--')
axs[i+7].set_ylabel('Velocity (deg/s)')
axs[i+7].set_title(f'RightArm Joint {i+1}')
axs[i+7].legend(loc='best', fontsize=9)
axs[i+7].grid(True)
axs[-1].set_xlabel('Elapsed Time (s)')
fig.suptitle(f'Velocity Comparison (NumDiff Smoothed vs Raw) || {motion_name}', fontsize=18)
plt.tight_layout(rect=[0, 0, 1, 0.97])
vel_compare_path = os.path.join(plot_data_dir, 'alldata_velocity_compare.png')
plt.savefig(vel_compare_path)
plt.close()
print(f"velocity compare plot saved: {vel_compare_path}")
# ==== Plot current ====
fig, axs = plt.subplots(14, 1, figsize=(14, 32), sharex=True)
for i in range(7):
axs[i].plot(joint_time_list_left, joint_current_left[:, i], label=f'LeftArm Joint {i+1} current (A)', color='r')
axs[i].set_ylabel('Current (A)')
axs[i].set_title(f'LeftArm Joint {i+1}')
axs[i].legend(loc='best', fontsize=9)
axs[i].grid(True)
for i in range(7):
axs[i+7].plot(joint_time_list_right, joint_current_right[:, i], label=f'RightArm Joint {i+1} current (A)', color='g')
axs[i+7].set_ylabel('Current (A)')
axs[i+7].set_title(f'RightArm Joint {i+1}')
axs[i+7].legend(loc='best', fontsize=9)
axs[i+7].grid(True)
axs[-1].set_xlabel('Elapsed Time (s)')
fig.suptitle(f'Joint Current || {motion_name}', fontsize=18)
plt.tight_layout(rect=[0, 0, 1, 0.97])
cur_img_path = os.path.join(plot_data_dir, 'alldata_current.png')
plt.savefig(cur_img_path)
plt.close()
print(f"current plot saved: {cur_img_path}")
# ==== Plot temperature ====
fig, axs = plt.subplots(14, 1, figsize=(14, 32), sharex=True)
for i in range(7):
axs[i].plot(joint_time_list_left, joint_temperature_left[:, i], label=f'LeftArm Joint {i+1} temperature (°C)', color='r')
axs[i].set_ylabel('Temp (°C)')
axs[i].set_title(f'LeftArm Joint {i+1}')
axs[i].legend(loc='best', fontsize=9)
axs[i].grid(True)
for i in range(7):
axs[i+7].plot(joint_time_list_right, joint_temperature_right[:, i], label=f'RightArm Joint {i+1} temperature (°C)', color='g')
axs[i+7].set_ylabel('Temp (°C)')
axs[i+7].set_title(f'RightArm Joint {i+1}')
axs[i+7].legend(loc='best', fontsize=9)
axs[i+7].grid(True)
axs[-1].set_xlabel('Elapsed Time (s)')
fig.suptitle(f'Joint Temperature || {motion_name}', fontsize=18)
plt.tight_layout(rect=[0, 0, 1, 0.97])
temp_img_path = os.path.join(plot_data_dir, 'alldata_temperature.png')
plt.savefig(temp_img_path)
plt.close()
print(f"temperature plot saved: {temp_img_path}")
if __name__ == "__main__":
run_dir = 'robot1/robot1_0kg'
files = [f for f in os.listdir(run_dir) if os.path.isfile(os.path.join(run_dir, f))]
print(f"Found {len(files)} files in {run_dir}:")
for filename in files:
print(f"Plotting {filename} ...")
plot_data(run_dir + "/" + filename)
print("All plotting completed.")
|