Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_data.py
More file actions
Latest commit
58 lines (49 loc) · 1.82 KB
/
Copy pathplot_data.py
File metadata and controls
58 lines (49 loc) · 1.82 KB
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
importcsv
importmatplotlib.pyplotasplt
importmatplotlib.animationasanimation
# Read CSV file and extract timestamp and joint1 (positions[0]) values.
timestamps= []
joint1_positions= []
withopen('joint_states.csv', 'r') ascsvfile:
reader=csv.DictReader(csvfile)
forrowinreader:
timestamps.append(float(row['timestamp']))
joint1_positions.append(float(row['joint1']))
# Determine the sampling interval based on timestamps.
iflen(timestamps) >1:
# Calculate average interval in milliseconds.
avg_interval= ((timestamps[-1] -timestamps[0]) / (len(timestamps)-1)) *1000
else:
avg_interval=100# fallback value
# Create the plot.
fig, ax=plt.subplots()
line, =ax.plot([], [], 'b-', lw=2, label='Joint 1 Position')
# Add the reference position line at 0.0 (red dashed line).
ax.axhline(y=0.0, color='red', linestyle='--', lw=2, label='Reference (0.0)')
ax.set_xlim(timestamps[0], timestamps[-1])
y_min=min(joint1_positions) -0.1
y_max=max(joint1_positions) +0.1
ax.set_ylim(y_min, y_max)
ax.set_xlabel('Time (s)')
ax.set_ylabel('Joint 1 Position (rad)')
ax.set_title('Joint 1 Position vs Time')
ax.legend()
# Initialization function: plot an empty line.
definit():
line.set_data([], [])
returnline,
# Animation update function.
defupdate(frame):
# Update the line with data up to the current frame.
x=timestamps[:frame+1]
y=joint1_positions[:frame+1]
line.set_data(x, y)
returnline,
# Create the animation.
ani=animation.FuncAnimation(fig, update, frames=len(timestamps),
init_func=init, blit=True, interval=avg_interval)
# Save the animation as an MP4 video.
output_video='joint_position_video.mp4'
ani.save(output_video, writer='ffmpeg', fps=1000/avg_interval)
print(f"Animation saved as {output_video}")
plt.show()