forked from aerorobotics/simple-quad-sim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimate_function.py
58 lines (50 loc) · 1.98 KB
/
animate_function.py
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
### Source for animation: https://github.com/antocuni/quadcopter-simulation
### author: Peter Huang, Antonio Cuni
### email: [email protected], [email protected]
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.colors import cnames
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
import sys
class QuadPlotter(object):
def __init__(self):
self.fig = plt.figure()
ax = self.fig.add_axes([0, 0, 1, 1], projection='3d')
ax.plot([], [], [], '-', c='cyan')[0]
ax.plot([], [], [], '-', c='red')[0]
ax.plot([], [], [], '-', c='blue', marker='o', markevery=2)[0]
self.set_limit((-1.0, 1.0), (-1.0, 1.0), (-1.0, 5))
def set_limit(self, x, y, z):
ax = plt.gca()
ax.set_xlim(x)
ax.set_ylim(y)
ax.set_zlim(z)
def plot_animation(self, get_world_frame):
"""
get_world_frame is a function which return the "next" world frame to be
drawn
"""
def anim_callback(i):
frame = get_world_frame(i)
self.set_frame(frame)
an = animation.FuncAnimation(self.fig,
anim_callback,
init_func=None,
frames=400, interval=1, blit=False)
if len(sys.argv) > 1 and sys.argv[1] == 'save':
an.save('sim.gif', dpi=300, writer='imagemagick', fps=60)
else:
plt.show()
def plot_step(self, world_frame):
self.set_frame(world_frame)
# plt.pause(0.00001)
def set_frame(self, frame):
# convert 3x6 world_frame matrix into three line_data objects which is 3x2 (row:point index, column:x,y,z)
lines_data = [frame[:,[0,2]], frame[:,[1,3]], frame[:,[4,5]]]
ax = plt.gca()
lines = ax.get_lines()
for line, line_data in zip(lines, lines_data):
x, y, z = line_data
line.set_data(x, y)
line.set_3d_properties(z)