-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation_code.py
More file actions
112 lines (93 loc) · 3.24 KB
/
simulation_code.py
File metadata and controls
112 lines (93 loc) · 3.24 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
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
import numpy as np
from numpy import sin, cos, pi
import matplotlib.pyplot as plt
from matplotlib import animation
from time import time
def simulate(cat_states, cat_controls, t, step_horizon, N, reference, save=False):
def create_triangle(state=[0,0,0], h=1, w=0.5, update=False):
x, y, th = state
triangle = np.array([
[h, 0 ],
[0, w/2],
[0, -w/2],
[h, 0 ]
]).T
rotation_matrix = np.array([
[cos(th), -sin(th)],
[sin(th), cos(th)]
])
coords = np.array([[x, y]]) + (rotation_matrix @ triangle).T
if update == True:
return coords
else:
return coords[:3, :]
def init():
return path, horizon, current_state, target_state,
def animate(i):
# get variables
x = cat_states[0, 0, i]
y = cat_states[1, 0, i]
th = cat_states[2, 0, i]
# update path
if i == 0:
path.set_data(np.array([]), np.array([]))
x_new = np.hstack((path.get_xdata(), x))
y_new = np.hstack((path.get_ydata(), y))
path.set_data(x_new, y_new)
# update horizon
x_new = cat_states[0, :, i]
y_new = cat_states[1, :, i]
horizon.set_data(x_new, y_new)
# update current_state
current_state.set_xy(create_triangle([x, y, th], update=True))
# update target_state
# xy = target_state.get_xy()
# target_state.set_xy(xy)
return path, horizon, current_state, target_state
# create figure and axes
fig, ax = plt.subplots(figsize=(6, 6))
min_scale = min(reference[0], reference[1], reference[3], reference[4]) - 2
max_scale = max(reference[0], reference[1], reference[3], reference[4]) + 2
ax.set_xlim(left = min_scale, right = max_scale)
ax.set_ylim(bottom = min_scale, top = max_scale)
# create lines:
# path
path, = ax.plot([], [], 'k', linewidth=2)
# horizon
horizon, = ax.plot([], [], 'x-g', alpha=0.5)
# current_state
current_triangle = create_triangle(reference[:3])
current_state = ax.fill(current_triangle[:, 0], current_triangle[:, 1], color='r')
current_state = current_state[0]
# target_state
target_triangle = create_triangle(reference[3:])
target_state = ax.fill(target_triangle[:, 0], target_triangle[:, 1], color='b')
target_state = target_state[0]
# circle obstackle
# n_obs = 1
# for i in np.linspace(5,15,n_obs):
# obs = plt.Circle((5,i), 1)
# ax.add_patch(obs)
# elipse obstackle
center = [5,5]
scale = [5,0.1]
rot = 3*pi/4
R_rot = np.array([[cos(rot), sin(rot)],[sin(rot), -cos(rot)]])
dt = np.linspace(0, 2*pi, 100)
xy_ = np.vstack((scale[0]*cos(dt), scale[1]*sin(dt))).T
xy_rot = np.dot(xy_, R_rot)
obs = plt.plot(xy_rot[:,0]+center[0], xy_rot[:,1]+center[1])
# ax.add_patch(obs)
sim = animation.FuncAnimation(
fig=fig,
func=animate,
init_func=init,
frames=len(t),
interval=step_horizon*100,
blit=True,
repeat=True
)
plt.show()
if save == True:
sim.save('./animation' + str(time()) +'.gif', writer='ffmpeg', fps=30)
return