-
Notifications
You must be signed in to change notification settings - Fork 32
/
car.py
85 lines (60 loc) · 2.06 KB
/
car.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
from math import pi
from matplotlib.patches import Rectangle, Arrow
import matplotlib.pyplot as plt
from matplotlib.collections import PatchCollection
import matplotlib.animation as animation
from dpp.env.environment import Environment
from dpp.test_cases.cases import TestCase
from dpp.env.car import SimpleCar
from time import time
def main():
tc = TestCase()
env = Environment()
car = SimpleCar(env, tc.start_pos, tc.end_pos)
# example controls to demonstrate car dynamics
controls = [
(pi/8, 1, 150),
(0, 1, 200)
]
path = car._get_path(car.start_pos, controls)
path = path[::5] + [path[-1]]
xl, yl = [], []
carl = []
for i in range(len(path)):
xl.append(path[i].pos[0])
yl.append(path[i].pos[1])
carl.append(path[i].model[0])
# plot and annimation
fig, ax = plt.subplots(figsize=(6,6))
ax.set_xlim(0, env.lx)
ax.set_ylim(0, env.ly)
ax.set_aspect("equal")
ax.set_xticks([])
ax.set_yticks([])
for ob in env.obs:
ax.add_patch(Rectangle((ob.x, ob.y), ob.w, ob.h, fc='gray', ec='k'))
ax.plot(car.start_pos[0], car.start_pos[1], 'ro', markersize=5)
_path, = ax.plot([], [], color='lime', linewidth=1)
_carl = PatchCollection([])
ax.add_collection(_carl)
_car = PatchCollection([])
ax.add_collection(_car)
frames = len(path) + 1
def animate(i):
_path.set_data(xl[min(i, len(path)-1):], yl[min(i, len(path)-1):])
sub_carl = carl[:min(i+1, len(path))]
_carl.set_paths(sub_carl[::4])
_carl.set_color('m')
_carl.set_alpha(0.1)
edgecolor = ['k']*5 + ['r']
facecolor = ['y'] + ['k']*4 + ['r']
_car.set_paths(path[min(i, len(path)-1)].model)
_car.set_edgecolor(edgecolor)
_car.set_facecolor(facecolor)
_car.set_zorder(3)
return _path, _carl, _car
ani = animation.FuncAnimation(fig, animate, frames=frames, interval=1,
repeat=False, blit=True)
plt.show()
if __name__ == '__main__':
main()