forked from MiyagiRonin/auto_parking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
show_animation.py
75 lines (67 loc) · 2.06 KB
/
show_animation.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
import math
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as pch
from matplotlib.animation import FuncAnimation
from matplotlib.lines import Line2D
#vehicle size
HALF_WIDTH = 1.0
REAR_TO_RAC = 1.0
FRONT_TO_RAC = 3.7
env_file = open("problems/reverse_verticle_1.txt", 'r')
path_file = open("bazel-bin/output_path.txt", 'r')
def draw_vehicle_box(rac_x, rac_y, heading, front_to_rac, rear_to_rac, half_width):
x = np.array([[rac_x], [rac_y]])
sin = math.sin(heading)
cos = math.cos(heading)
matrix = np.matrix([[cos, -sin],[sin, cos]])
left_rear = matrix*np.array([[-rear_to_rac], [half_width]]) + x
right_rear = matrix*np.array([[-rear_to_rac], [-half_width]]) + x
right_front = matrix*np.array([[front_to_rac], [-half_width]]) + x
left_front = matrix*np.array([[front_to_rac], [half_width]]) + x
xy = np.vstack((np.transpose(left_rear), np.transpose(right_rear),
np.transpose(right_front), np.transpose(left_front)))
return pch.Polygon(xy, True)
fig, ax =plt.subplots(figsize=(6,6))
#load environment
env = []
line = env_file.readline()
while line:
line = env_file.readline().split()
if len(line) == 0:
break
info = [float(x) for x in line]
env.append(Line2D([info[0], info[2]], [info[1], info[3]]))
#load path
path = []
xs = []
ys = []
line = path_file.readline()
while line:
line = path_file.readline().split()
if len(line) == 0:
break
info = [float(x) for x in line]
xs.append(info[0])
ys.append(info[1])
path.append(draw_vehicle_box(info[0], info[1], info[2], FRONT_TO_RAC, REAR_TO_RAC, HALF_WIDTH))
def update(i):
ax.clear()
ax.set_facecolor(plt.cm.Blues(.2))
ax.set_xlim([-10,10])
ax.set_ylim([-2,15])
ax.set_aspect(1)
ax.set_title('Parking Simulation')
ax.add_patch(path[i])
for line_2d in env:
ax.add_line(line_2d)
ax.plot(xs[:i], ys[:i], color="k")
#[spine.set_visible(False) for spine in ax.spines.values()]
anime = FuncAnimation(
fig = fig,
func = update,
frames = len(path),
interval = 50
)
plt.show()
#anime.save('result.gif')