-
Notifications
You must be signed in to change notification settings - Fork 1
/
animate.py
134 lines (103 loc) · 3.88 KB
/
animate.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
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
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from numpy import shape
import numpy as np
import pickle
import sys
def init():
qmass.set_data([], [])
qtraj.set_data([], [])
zmass.set_data([], [])
ztraj.set_data([], [])
qqmass.set_data([], [])
qqtraj.set_data([], [])
zzmass.set_data([], [])
zztraj.set_data([], [])
#qxforce = axl.arrow(0, 0, 0, 0)
return qmass, qtraj, zmass, ztraj
def animate(i):
t = float(i) / rate
newPq = s.xtopq(atj.x(t))
newqq = s.xtoq(atj.x(t))
uval = atj.u(t)
qmass.set_data(newPq)
qtraj.set_data(qtraj.get_xdata() + [newPq[0]],
qtraj.get_ydata() + [newPq[1]])
qqmass.set_data(newqq)
qqtraj.set_data(qqtraj.get_xdata() + [newqq[0]],
qqtraj.get_ydata() + [newqq[1]])
for artist in axl.artists:
artist.remove()
if uval[0] != 0 and uval[1] != 0:
qxforce = axl.arrow(newPq[0], newPq[1], 0.5*uval[0], 0.5*uval[1],
length_includes_head=True, width=0.01)
else:
qxforce = None
newPz = s.xtopz(atj.x(t))
newzb = s.xtoz(atj.x(t))
zmass.set_data(newPz)
ztraj.set_data(ztraj.get_xdata() + [newPz[0]],
ztraj.get_ydata() + [newPz[1]])
zzmass.set_data(newzb)
zztraj.set_data(zztraj.get_xdata() + [newzb[0]],
zztraj.get_ydata() + [newzb[1]])
return qmass, qtraj, zmass, ztraj, qqmass, qqtraj, zzmass, zztraj, qxforce
if __name__ == "__main__":
# parse command line arguments
# first is the reference trajectory
infile = open(sys.argv[1], 'rb')
rtj = pickle.load(infile)
infile.close()
rtj.interpolate()
# now load the animation trajectory
if len(sys.argv) > 2:
infile = open(sys.argv[2], 'rb')
atj = pickle.load(infile)
infile.close()
atj.interpolate()
tmin = atj._t[0]
tmax = atj._t[-1]
rate = 30.0 # in frames per second
fig = plt.figure(figsize=(8, 4.5)) # 16:9 ratio
xmin = -10# -3 * np.pi
xmax = 2 #np.pi
ymin = -6
ymax = 6
axl = fig.add_subplot(121, xlim=(xmin, xmax), ylim=(ymin, ymax),
aspect='equal', xlabel="$x(m)$",
ylabel="$y(m)$",
title='Euclidean Space')
# axl.tick_params(pad=-20)
axr = fig.add_subplot(122, xlim=(xmin, xmax), ylim=(ymin, ymax),
aspect='equal', xlabel=r'$\bar{x}$',
ylabel=r'$\bar{y}$',
title='Modified Space')
# axr.set_yticklabels([])
# axr.set_xticklabels([])
xarray = np.linspace(xmin, xmax, 100)
qmass, = axl.plot([], [], 'bo', ms=6)
qqmass, = axl.plot([], [], 'ro', ms=6)
qqtraj, = axl.plot([], [], 'r--', lw=1)
qtraj, = axl.plot([], [], 'b-', lw=1)
qxforce = axl.arrow(0, 0, 1, 1)
sinfloor = axl.fill_between(xarray, ymin, np.sin(xarray),
facecolor='grey', alpha=0.5)
sinlabel = axl.text(-6, -4, r"$\phi(q)<0$")
rtoq = s.xtoq(rtj.x(tmin))
axl.plot(rtoq[0], rtoq[1], 'bx', lw=6)
flatfloor = axr.fill_between(xarray, ymin, 0 * xarray,
facecolor='grey', alpha=0.5)
flatlabel = axr.text(-6, -4, r'$\bar{\phi}(\bar{q})<0$')
rtoz = s.xtoz(rtj.x(tmin))
axr.plot(rtoz[0], rtoz[1], 'bx', lw=6)
zmass, = axr.plot([], [], 'bo', ms=6)
zzmass, = axr.plot([], [], 'ro', ms=6)
zztraj, = axr.plot([], [], 'r--', lw=1)
ztraj, = axr.plot([], [], 'b-', lw=1)
ani = animation.FuncAnimation(fig, animate,
frames=int(rate * (tmax - tmin)),
interval=1000 / (rate), blit=True,
init_func=init, repeat=True)
# dpi = 720/height in inches for 720p output
ani.save('sim_real_time.mp4', fps=30, bitrate=7500, dpi=160)
plt.close()